Skip to content

Commit 0ac15e3

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

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
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-cli/src/Cardano/CLI/Shelley/Commands.hs

+19-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ module Cardano.CLI.Shelley.Commands
1717
, TextViewCmd (..)
1818
, renderShelleyCommand
1919

20+
-- * Subqueries for the local mempool command
21+
, TxMempoolQuery (..)
22+
2023
-- * CLI flag types
2124
, AddressKeyType (..)
2225
, ByronKeyType (..)
@@ -49,7 +52,7 @@ import Prelude
4952

5053
import Cardano.Api.Shelley
5154

52-
import Data.Text (Text)
55+
import Data.Text (Text, pack)
5356

5457
import Cardano.CLI.Shelley.Key (PaymentVerifier, StakeVerifier, VerificationKeyOrFile,
5558
VerificationKeyOrHashOrFile, VerificationKeyTextOrFile)
@@ -348,6 +351,19 @@ renderPoolCmd cmd =
348351
PoolGetId {} -> "stake-pool id"
349352
PoolMetadataHash {} -> "stake-pool metadata-hash"
350353

354+
data TxMempoolQuery =
355+
TxMempoolQueryTxExists TxId
356+
| TxMempoolQueryNextTx
357+
| TxMempoolQueryInfo
358+
deriving Show
359+
360+
renderTxMempoolQuery :: TxMempoolQuery -> Text
361+
renderTxMempoolQuery query =
362+
case query of
363+
TxMempoolQueryTxExists tx -> "tx-exists " <> pack (show tx) -- TODO render this properly
364+
TxMempoolQueryNextTx -> "next-tx"
365+
TxMempoolQueryInfo -> "info"
366+
351367
data QueryCmd =
352368
QueryLeadershipSchedule
353369
AnyConsensusModeParams
@@ -373,6 +389,7 @@ data QueryCmd =
373389
-- ^ Node operational certificate
374390
(Maybe OutputFile)
375391
| QueryPoolState' AnyConsensusModeParams NetworkId [Hash StakePoolKey]
392+
| QueryTxMempool AnyConsensusModeParams NetworkId TxMempoolQuery
376393
deriving Show
377394

378395
renderQueryCmd :: QueryCmd -> Text
@@ -390,6 +407,7 @@ renderQueryCmd cmd =
390407
QueryStakeSnapshot' {} -> "query stake-snapshot"
391408
QueryKesPeriodInfo {} -> "query kes-period-info"
392409
QueryPoolState' {} -> "query pool-state"
410+
QueryTxMempool _ _ query -> "query tx-mempool" <> renderTxMempoolQuery query
393411

394412
data GovernanceCmd
395413
= GovernanceMIRPayStakeAddressesCertificate

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

+13
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,17 @@ pQueryCmd =
10101012
<*> pNetworkId
10111013
<*> many pStakePoolVerificationKeyHash
10121014

1015+
pQueryTxMempool :: Parser QueryCmd
1016+
pQueryTxMempool = QueryTxMempool
1017+
<$> pConsensusModeParams
1018+
<*> pNetworkId
1019+
<*> pTxMempoolQuery
1020+
where
1021+
pTxMempoolQuery :: Parser TxMempoolQuery
1022+
pTxMempoolQuery = subparser $
1023+
command "info" (info (pure TxMempoolQueryInfo) (progDesc "Mempool info"))
1024+
<> command "next-tx" (info (pure TxMempoolQueryNextTx) (progDesc "Next transaction"))
1025+
<> command "tx-exists" (info (TxMempoolQueryTxExists <$> argument Opt.str (metavar "TX_ID")) (progDesc "Check if transaction exists"))
10131026
pLeadershipSchedule :: Parser QueryCmd
10141027
pLeadershipSchedule = QueryLeadershipSchedule
10151028
<$> pConsensusModeParams

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

+48
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 ->
203+
runQueryTxMempool consensusModeParams network op
202204

203205
runQueryProtocolParameters
204206
:: AnyConsensusModeParams
@@ -620,6 +622,52 @@ 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+
-> ExceptT ShelleyQueryCmdError IO ()
631+
runQueryTxMempool (AnyConsensusModeParams cModeParams) network query = do
632+
SocketPath sockPath <- firstExceptT ShelleyQueryCmdEnvVarSocketErr readEnvSocketPath
633+
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath
634+
635+
localQuery <- case query of
636+
TxMempoolQueryTxExists tx -> do
637+
anyE@(AnyCardanoEra era) <- determineEra cModeParams localNodeConnInfo
638+
let cMode = consensusModeOnly cModeParams
639+
eInMode <- toEraInMode era cMode
640+
& hoistMaybe (ShelleyQueryCmdEraConsensusModeMismatch (AnyConsensusMode cMode) anyE)
641+
pure $ LocalTxMonitoringQueryTx $ TxIdInMode tx eInMode
642+
TxMempoolQueryNextTx -> pure LocalTxMonitoringSendNextTx
643+
TxMempoolQueryInfo -> pure LocalTxMonitoringMempoolInformation
644+
645+
result <- liftIO $ queryTxMonitoringLocal localNodeConnInfo localQuery
646+
liftIO $ LBS.putStrLn (renderResult result)
647+
where
648+
renderResult result =
649+
case result of
650+
LocalTxMonitoringTxExists tx slot -> encodePretty $ object
651+
[ "exists" .= True
652+
, "txId" .= tx
653+
, "slot" .= slot
654+
]
655+
LocalTxMonitoringTxDoesNotExist tx slot -> encodePretty $ object
656+
[ "exists" .= False
657+
, "txId" .= tx
658+
, "slot" .= slot
659+
]
660+
LocalTxMonitoringNextTx tx slot -> encodePretty $ object
661+
[ "nextTx" .= (show tx :: String) -- TODO Render this properly
662+
, "slot" .= slot
663+
]
664+
LocalTxMonitoringMempoolSizeAndCapacity mempool slot ->
665+
encodePretty $ object
666+
[ "capacityInBytes" .= capacityInBytes mempool
667+
, "sizeInBytes" .= sizeInBytes mempool
668+
, "numberOfTxs" .= numberOfTxs mempool
669+
, "slot" .= slot
670+
]
623671

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

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"

0 commit comments

Comments
 (0)