Skip to content

Commit 03b91c7

Browse files
author
Robert 'Probie' Offner
committed
Add tx-mempool command to CLI
1 parent 866c421 commit 03b91c7

File tree

10 files changed

+115
-6
lines changed

10 files changed

+115
-6
lines changed

cardano-api/src/Cardano/Api.hs

+3
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ module Cardano.Api (
591591
MempoolSizeAndCapacity(..),
592592
queryTxMonitoringLocal,
593593

594+
TxIdInMode(..),
595+
594596
EraHistory(..),
595597
getProgress,
596598

@@ -681,6 +683,7 @@ import Cardano.Api.Fees
681683
import Cardano.Api.GenesisParameters
682684
import Cardano.Api.Hash
683685
import Cardano.Api.HasTypeProxy
686+
import Cardano.Api.InMode
684687
import Cardano.Api.IPC
685688
import Cardano.Api.IPC.Monad
686689
import Cardano.Api.Key

cardano-api/src/Cardano/Api/IPC.hs

+31
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import Prelude
8383

8484
import Data.Void (Void)
8585

86+
import Data.Aeson (ToJSON, (.=), object, toJSON)
8687
import qualified Data.ByteString.Lazy as LBS
8788
import qualified Data.Map.Strict as Map
8889

@@ -131,6 +132,7 @@ import Cardano.Api.Modes
131132
import Cardano.Api.NetworkId
132133
import Cardano.Api.Protocol.Types
133134
import Cardano.Api.Query
135+
import Cardano.Api.Tx (getTxBody)
134136
import Cardano.Api.TxBody
135137

136138
-- ----------------------------------------------------------------------------
@@ -650,6 +652,35 @@ data LocalTxMonitoringResult mode
650652
Consensus.MempoolSizeAndCapacity
651653
SlotNo -- ^ Slot number at which the mempool snapshot was taken
652654

655+
instance ToJSON (LocalTxMonitoringResult mode) where
656+
toJSON result =
657+
object $ case result of
658+
LocalTxMonitoringTxExists tx slot ->
659+
[ "exists" .= True
660+
, "txId" .= tx
661+
, "slot" .= slot
662+
]
663+
LocalTxMonitoringTxDoesNotExist tx slot ->
664+
[ "exists" .= False
665+
, "txId" .= tx
666+
, "slot" .= slot
667+
]
668+
LocalTxMonitoringNextTx txInMode slot ->
669+
[ "nextTx" .= txId
670+
, "slot" .= slot
671+
]
672+
where
673+
txId = case txInMode of
674+
Just (TxInMode tx _) -> Just $ getTxId $ getTxBody tx
675+
-- TODO: support fetching the ID of a Byron Era transaction
676+
_ -> Nothing
677+
LocalTxMonitoringMempoolSizeAndCapacity mempool slot ->
678+
[ "capacityInBytes" .= Consensus.capacityInBytes mempool
679+
, "sizeInBytes" .= Consensus.sizeInBytes mempool
680+
, "numberOfTxs" .= Consensus.numberOfTxs mempool
681+
, "slot" .= slot
682+
]
683+
653684
data LocalTxMonitoringQuery mode
654685
-- | Query if a particular tx exists in the mempool. Note that, the absence
655686
-- of a transaction does not imply anything about how the transaction was

cardano-cli/ChangeLog.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog for cardano-cli
22

3+
## vNext
4+
5+
### Features
6+
7+
- Add `query tx-mempool` ([PR 4276](https://github.com/input-output-hk/cardano-node/pull/4276))
8+
39
## 1.33.0 -- December 2021
410
## 1.32.1 -- November 2021
511

cardano-cli/src/Cardano/CLI/Shelley/Commands.hs

+9
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ data QueryCmd =
373373
-- ^ Node operational certificate
374374
(Maybe OutputFile)
375375
| QueryPoolState' AnyConsensusModeParams NetworkId [Hash StakePoolKey]
376+
| QueryTxMempool AnyConsensusModeParams NetworkId TxMempoolQuery (Maybe OutputFile)
376377
deriving Show
377378

378379
renderQueryCmd :: QueryCmd -> Text
@@ -390,6 +391,14 @@ renderQueryCmd cmd =
390391
QueryStakeSnapshot' {} -> "query stake-snapshot"
391392
QueryKesPeriodInfo {} -> "query kes-period-info"
392393
QueryPoolState' {} -> "query pool-state"
394+
QueryTxMempool _ _ query _ -> "query tx-mempool" <> renderTxMempoolQuery query
395+
where
396+
renderTxMempoolQuery query =
397+
case query of
398+
TxMempoolQueryTxExists tx -> "tx-exists " <> serialiseToRawBytesHexText tx
399+
TxMempoolQueryNextTx -> "next-tx"
400+
TxMempoolQueryInfo -> "info"
401+
393402

394403
data GovernanceCmd
395404
= GovernanceMIRPayStakeAddressesCertificate

cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs

+21
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,8 @@ pQueryCmd =
941941
(Opt.info pKesPeriodInfo $ Opt.progDesc "Get information about the current KES period and your node's operational certificate.")
942942
, subParser "pool-state"
943943
(Opt.info pQueryPoolState $ Opt.progDesc "Dump the pool state")
944+
, subParser "tx-mempool"
945+
(Opt.info pQueryTxMempool $ Opt.progDesc "Local Mempool info")
944946
]
945947
where
946948
pQueryProtocolParameters :: Parser QueryCmd
@@ -1010,6 +1012,25 @@ pQueryCmd =
10101012
<*> pNetworkId
10111013
<*> many pStakePoolVerificationKeyHash
10121014

1015+
pQueryTxMempool :: Parser QueryCmd
1016+
pQueryTxMempool = QueryTxMempool
1017+
<$> pConsensusModeParams
1018+
<*> pNetworkId
1019+
<*> pTxMempoolQuery
1020+
<*> pMaybeOutputFile
1021+
where
1022+
pTxMempoolQuery :: Parser TxMempoolQuery
1023+
pTxMempoolQuery = asum
1024+
[ subParser "info"
1025+
(Opt.info (pure TxMempoolQueryInfo) $
1026+
Opt.progDesc "Ask the node about the current mempool's capacity and sizes")
1027+
, subParser "next-tx"
1028+
(Opt.info (pure TxMempoolQueryNextTx) $
1029+
Opt.progDesc "Requests the next transaction from the mempool's current list")
1030+
, subParser "tx-exists"
1031+
(Opt.info (TxMempoolQueryTxExists <$> argument Opt.str (metavar "TX_ID")) $
1032+
Opt.progDesc "Query if a particular transaction exists in the mempool")
1033+
]
10131034
pLeadershipSchedule :: Parser QueryCmd
10141035
pLeadershipSchedule = QueryLeadershipSchedule
10151036
<$> pConsensusModeParams

cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs

+30
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ runQueryCmd cmd =
199199
runQueryKesPeriodInfo consensusModeParams network nodeOpCert mOutFile
200200
QueryPoolState' consensusModeParams network poolid ->
201201
runQueryPoolState consensusModeParams network poolid
202+
QueryTxMempool consensusModeParams network op mOutFile ->
203+
runQueryTxMempool consensusModeParams network op mOutFile
202204

203205
runQueryProtocolParameters
204206
:: AnyConsensusModeParams
@@ -620,6 +622,34 @@ runQueryPoolState (AnyConsensusModeParams cModeParams) network poolIds = do
620622
result <- executeQuery era cModeParams localNodeConnInfo qInMode
621623
obtainLedgerEraClassConstraints sbe writePoolState result
622624

625+
-- | Query the local mempool state
626+
runQueryTxMempool
627+
:: AnyConsensusModeParams
628+
-> NetworkId
629+
-> TxMempoolQuery
630+
-> Maybe OutputFile
631+
-> ExceptT ShelleyQueryCmdError IO ()
632+
runQueryTxMempool (AnyConsensusModeParams cModeParams) network query mOutFile = do
633+
SocketPath sockPath <- firstExceptT ShelleyQueryCmdEnvVarSocketErr readEnvSocketPath
634+
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath
635+
636+
localQuery <- case query of
637+
TxMempoolQueryTxExists tx -> do
638+
anyE@(AnyCardanoEra era) <- determineEra cModeParams localNodeConnInfo
639+
let cMode = consensusModeOnly cModeParams
640+
eInMode <- toEraInMode era cMode
641+
& hoistMaybe (ShelleyQueryCmdEraConsensusModeMismatch (AnyConsensusMode cMode) anyE)
642+
pure $ LocalTxMonitoringQueryTx $ TxIdInMode tx eInMode
643+
TxMempoolQueryNextTx -> pure LocalTxMonitoringSendNextTx
644+
TxMempoolQueryInfo -> pure LocalTxMonitoringMempoolInformation
645+
646+
result <- liftIO $ queryTxMonitoringLocal localNodeConnInfo localQuery
647+
let renderedResult = encodePretty result
648+
case mOutFile of
649+
Nothing -> liftIO $ LBS.putStrLn renderedResult
650+
Just (OutputFile oFp) -> handleIOExceptT (ShelleyQueryCmdWriteFileError . FileIOError oFp)
651+
$ LBS.writeFile oFp renderedResult
652+
623653

624654
-- | Obtain stake snapshot information for a pool, plus information about the total active stake.
625655
-- This information can be used for leader slot calculation, for example, and has been requested by SPOs.

cardano-cli/src/Cardano/CLI/Types.hs

+8-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module Cardano.CLI.Types
3636
, TxOutChangeAddress (..)
3737
, TxOutDatumAnyEra (..)
3838
, TxFile (..)
39+
, TxMempoolQuery (..)
3940
, UpdateProposalFile (..)
4041
, VerificationKeyFile (..)
4142
, Stakes (..)
@@ -53,8 +54,8 @@ import Data.Word (Word64)
5354
import qualified Cardano.Chain.Slotting as Byron
5455

5556
import Cardano.Api (AddressAny, AnyScriptLanguage, EpochNo, ExecutionUnits, Hash,
56-
InAnyCardanoEra, PaymentKey, PolicyId, ScriptData, SlotNo (SlotNo), Tx, TxIn,
57-
Value, WitCtxMint, WitCtxStake, WitCtxTxIn)
57+
InAnyCardanoEra, PaymentKey, PolicyId, ScriptData, SlotNo (SlotNo), Tx, TxId,
58+
TxIn, Value, WitCtxMint, WitCtxStake, WitCtxTxIn)
5859

5960
import qualified Cardano.Ledger.Crypto as Crypto
6061

@@ -395,4 +396,8 @@ newtype TxFile
395396
= TxFile FilePath
396397
deriving Show
397398

398-
399+
data TxMempoolQuery =
400+
TxMempoolQueryTxExists TxId
401+
| TxMempoolQueryNextTx
402+
| TxMempoolQueryInfo
403+
deriving Show

doc/reference/cardano-node-cli-reference.md

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ The `query` command contains the following subcommands:
7878
* `pool-params` (advanced): gets the current and future parameters for a stake pool
7979
* `leadership-schedule`: gets the slots in which the node is slot leader for the current or following epoch
8080
* `kes-period-info` (advanced): returns diagnostic information about your operational certificate
81+
* `tx-mempool info`: returns details about a node's mempool's resource usage
82+
* `tx-mempool next-tx`: returns the next transaction to be processed
83+
* `tx-mempool tx-exists`: queries whether or not a transaction is in the node's mempool
8184

8285
*cardano-cli governance*
8386
The `governance` command contains the following subcommands:

scripts/babbage/example-babbage-script-usage.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ls -al "$CARDANO_NODE_SOCKET_PATH"
2222
plutusspendingscript="$BASE/scripts/plutus/scripts/v2/required-redeemer.plutus"
2323
plutusmintingscript="$BASE/scripts/plutus/scripts/v2/minting-script.plutus"
2424
plutusstakescript="scripts/plutus/scripts/v2/stake-script.plutus"
25-
mintpolicyid=$(cardano-cli transaction policyid --script-file $plutusmintingscript)
25+
mintpolicyid=$($CARDANO_CLI transaction policyid --script-file $plutusmintingscript)
2626
## This datum hash is the hash of the untyped 42
2727
scriptdatumhash="9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b"
2828
datumfilepath="$BASE/scripts/plutus/data/42.datum"

scripts/babbage/mkfiles.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ case $UNAME in
3131
DATE="date";;
3232
esac
3333

34+
CARDANO_CLI="${CARDANO_CLI:-cardano-cli}"
3435
NETWORK_MAGIC=42
3536
SECURITY_PARAM=10
3637
NUM_SPO_NODES=3
@@ -65,7 +66,7 @@ cat > "${ROOT}/byron.genesis.spec.json" <<EOF
6566
}
6667
EOF
6768

68-
cardano-cli byron genesis genesis \
69+
$CARDANO_CLI byron genesis genesis \
6970
--protocol-magic ${NETWORK_MAGIC} \
7071
--start-time "${START_TIME}" \
7172
--k ${SECURITY_PARAM} \
@@ -107,7 +108,7 @@ $SED -i "${ROOT}/configuration.yaml" \
107108
# Copy the cost mode
108109

109110

110-
cardano-cli genesis create-staked --genesis-dir "${ROOT}" \
111+
$CARDANO_CLI genesis create-staked --genesis-dir "${ROOT}" \
111112
--testnet-magic "${NETWORK_MAGIC}" \
112113
--gen-pools 3 \
113114
--supply 1000000000000 \

0 commit comments

Comments
 (0)