Skip to content

Commit 96ea6a2

Browse files
author
Robert 'Probie' Offner
committed
Add tx-mempool command to CLI
1 parent 8832f86 commit 96ea6a2

File tree

10 files changed

+116
-7
lines changed

10 files changed

+116
-7
lines changed

cardano-api/src/Cardano/Api.hs

+3
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ module Cardano.Api (
595595
MempoolSizeAndCapacity(..),
596596
queryTxMonitoringLocal,
597597

598+
TxIdInMode(..),
599+
598600
EraHistory(..),
599601
getProgress,
600602

@@ -718,6 +720,7 @@ import Cardano.Api.Fees
718720
import Cardano.Api.GenesisParameters
719721
import Cardano.Api.Hash
720722
import Cardano.Api.HasTypeProxy
723+
import Cardano.Api.InMode
721724
import Cardano.Api.IPC
722725
import Cardano.Api.IPC.Monad
723726
import Cardano.Api.Key

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

+31
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ import Prelude
8484

8585
import Data.Void (Void)
8686

87+
import Data.Aeson (ToJSON, (.=), object, toJSON)
8788
import Data.Bifunctor (first)
8889
import qualified Data.ByteString.Lazy as LBS
8990
import qualified Data.Map.Strict as Map
@@ -133,6 +134,7 @@ import Cardano.Api.Modes
133134
import Cardano.Api.NetworkId
134135
import Cardano.Api.Protocol.Types
135136
import Cardano.Api.Query
137+
import Cardano.Api.Tx (getTxBody)
136138
import Cardano.Api.TxBody
137139

138140
-- ----------------------------------------------------------------------------
@@ -661,6 +663,35 @@ data LocalTxMonitoringResult mode
661663
Consensus.MempoolSizeAndCapacity
662664
SlotNo -- ^ Slot number at which the mempool snapshot was taken
663665

666+
instance ToJSON (LocalTxMonitoringResult mode) where
667+
toJSON result =
668+
object $ case result of
669+
LocalTxMonitoringTxExists tx slot ->
670+
[ "exists" .= True
671+
, "txId" .= tx
672+
, "slot" .= slot
673+
]
674+
LocalTxMonitoringTxDoesNotExist tx slot ->
675+
[ "exists" .= False
676+
, "txId" .= tx
677+
, "slot" .= slot
678+
]
679+
LocalTxMonitoringNextTx txInMode slot ->
680+
[ "nextTx" .= txId
681+
, "slot" .= slot
682+
]
683+
where
684+
txId = case txInMode of
685+
Just (TxInMode tx _) -> Just $ getTxId $ getTxBody tx
686+
-- TODO: support fetching the ID of a Byron Era transaction
687+
_ -> Nothing
688+
LocalTxMonitoringMempoolSizeAndCapacity mempool slot ->
689+
[ "capacityInBytes" .= Consensus.capacityInBytes mempool
690+
, "sizeInBytes" .= Consensus.sizeInBytes mempool
691+
, "numberOfTxs" .= Consensus.numberOfTxs mempool
692+
, "slot" .= slot
693+
]
694+
664695
data LocalTxMonitoringQuery mode
665696
-- | Query if a particular tx exists in the mempool. Note that, the absence
666697
-- of a transaction does not imply anything about how the transaction was

cardano-cli/ChangeLog.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
## vNext
44

5-
Default to the ledger's CDDL format for transaction body creation by removing flags `--cddl-format` and `--cli-format` from `build` and `build-raw` ([PR 4303](https://github.com/input-output-hk/cardano-node/pull/4303))
5+
### Features
6+
7+
- Default to the ledger's CDDL format for transaction body creation by removing flags `--cddl-format` and `--cli-format` from `build` and `build-raw` ([PR 4303](https://github.com/input-output-hk/cardano-node/pull/4303))
8+
9+
- Add `query tx-mempool` ([PR 4276](https://github.com/input-output-hk/cardano-node/pull/4276))
610

711
### Bugs
812

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

+9
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ data QueryCmd =
371371
-- ^ Node operational certificate
372372
(Maybe OutputFile)
373373
| QueryPoolState' AnyConsensusModeParams NetworkId [Hash StakePoolKey]
374+
| QueryTxMempool AnyConsensusModeParams NetworkId TxMempoolQuery (Maybe OutputFile)
374375
deriving Show
375376

376377
renderQueryCmd :: QueryCmd -> Text
@@ -388,6 +389,14 @@ renderQueryCmd cmd =
388389
QueryStakeSnapshot' {} -> "query stake-snapshot"
389390
QueryKesPeriodInfo {} -> "query kes-period-info"
390391
QueryPoolState' {} -> "query pool-state"
392+
QueryTxMempool _ _ query _ -> "query tx-mempool" <> renderTxMempoolQuery query
393+
where
394+
renderTxMempoolQuery query =
395+
case query of
396+
TxMempoolQueryTxExists tx -> "tx-exists " <> serialiseToRawBytesHexText tx
397+
TxMempoolQueryNextTx -> "next-tx"
398+
TxMempoolQueryInfo -> "info"
399+
391400

392401
data GovernanceCmd
393402
= GovernanceMIRPayStakeAddressesCertificate

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

+21
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,8 @@ pQueryCmd =
939939
(Opt.info pKesPeriodInfo $ Opt.progDesc "Get information about the current KES period and your node's operational certificate.")
940940
, subParser "pool-state"
941941
(Opt.info pQueryPoolState $ Opt.progDesc "Dump the pool state")
942+
, subParser "tx-mempool"
943+
(Opt.info pQueryTxMempool $ Opt.progDesc "Local Mempool info")
942944
]
943945
where
944946
pQueryProtocolParameters :: Parser QueryCmd
@@ -1008,6 +1010,25 @@ pQueryCmd =
10081010
<*> pNetworkId
10091011
<*> many pStakePoolVerificationKeyHash
10101012

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

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

+32
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ runQueryCmd cmd =
198198
runQueryKesPeriodInfo consensusModeParams network nodeOpCert mOutFile
199199
QueryPoolState' consensusModeParams network poolid ->
200200
runQueryPoolState consensusModeParams network poolid
201+
QueryTxMempool consensusModeParams network op mOutFile ->
202+
runQueryTxMempool consensusModeParams network op mOutFile
201203

202204
runQueryProtocolParameters
203205
:: AnyConsensusModeParams
@@ -633,6 +635,36 @@ runQueryPoolState (AnyConsensusModeParams cModeParams) network poolIds = do
633635
result <- executeQuery era cModeParams localNodeConnInfo qInMode
634636
obtainLedgerEraClassConstraints sbe writePoolState result
635637

638+
-- | Query the local mempool state
639+
runQueryTxMempool
640+
:: AnyConsensusModeParams
641+
-> NetworkId
642+
-> TxMempoolQuery
643+
-> Maybe OutputFile
644+
-> ExceptT ShelleyQueryCmdError IO ()
645+
runQueryTxMempool (AnyConsensusModeParams cModeParams) network query mOutFile = do
646+
SocketPath sockPath <- firstExceptT ShelleyQueryCmdEnvVarSocketErr
647+
$ newExceptT readEnvSocketPath
648+
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath
649+
650+
localQuery <- case query of
651+
TxMempoolQueryTxExists tx -> do
652+
anyE@(AnyCardanoEra era) <- firstExceptT ShelleyQueryCmdAcquireFailure
653+
. newExceptT $ determineEra cModeParams localNodeConnInfo
654+
let cMode = consensusModeOnly cModeParams
655+
eInMode <- toEraInMode era cMode
656+
& hoistMaybe (ShelleyQueryCmdEraConsensusModeMismatch (AnyConsensusMode cMode) anyE)
657+
pure $ LocalTxMonitoringQueryTx $ TxIdInMode tx eInMode
658+
TxMempoolQueryNextTx -> pure LocalTxMonitoringSendNextTx
659+
TxMempoolQueryInfo -> pure LocalTxMonitoringMempoolInformation
660+
661+
result <- liftIO $ queryTxMonitoringLocal localNodeConnInfo localQuery
662+
let renderedResult = encodePretty result
663+
case mOutFile of
664+
Nothing -> liftIO $ LBS.putStrLn renderedResult
665+
Just (OutputFile oFp) -> handleIOExceptT (ShelleyQueryCmdWriteFileError . FileIOError oFp)
666+
$ LBS.writeFile oFp renderedResult
667+
636668

637669
-- | Obtain stake snapshot information for a pool, plus information about the total active stake.
638670
-- 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
@@ -34,6 +34,7 @@ module Cardano.CLI.Types
3434
, TxOutChangeAddress (..)
3535
, TxOutDatumAnyEra (..)
3636
, TxFile (..)
37+
, TxMempoolQuery (..)
3738
, UpdateProposalFile (..)
3839
, VerificationKeyFile (..)
3940
, Stakes (..)
@@ -51,8 +52,8 @@ import Data.Word (Word64)
5152
import qualified Cardano.Chain.Slotting as Byron
5253

5354
import Cardano.Api (AddressAny, AnyScriptLanguage, EpochNo, ExecutionUnits, Hash,
54-
InAnyCardanoEra, PaymentKey, PolicyId, ScriptData, SlotNo (SlotNo), Tx, TxIn,
55-
Value, WitCtxMint, WitCtxStake, WitCtxTxIn)
55+
InAnyCardanoEra, PaymentKey, PolicyId, ScriptData, SlotNo (SlotNo), Tx, TxId,
56+
TxIn, Value, WitCtxMint, WitCtxStake, WitCtxTxIn)
5657

5758
import qualified Cardano.Ledger.Crypto as Crypto
5859

@@ -383,4 +384,8 @@ newtype TxFile
383384
= TxFile FilePath
384385
deriving Show
385386

386-
387+
data TxMempoolQuery =
388+
TxMempoolQueryTxExists TxId
389+
| TxMempoolQueryNextTx
390+
| TxMempoolQueryInfo
391+
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
@@ -21,7 +21,7 @@ ls -al "$CARDANO_NODE_SOCKET_PATH"
2121
plutusspendingscript="$BASE/scripts/plutus/scripts/v2/required-redeemer.plutus"
2222
plutusmintingscript="$BASE/scripts/plutus/scripts/v2/minting-script.plutus"
2323
plutusstakescript="scripts/plutus/scripts/v2/stake-script.plutus"
24-
mintpolicyid=$(cardano-cli transaction policyid --script-file $plutusmintingscript)
24+
mintpolicyid=$($CARDANO_CLI transaction policyid --script-file $plutusmintingscript)
2525
## This datum hash is the hash of the untyped 42
2626
scriptdatumhash="9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b"
2727
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)