Skip to content

Commit

Permalink
Add tx-mempool command to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert 'Probie' Offner committed Aug 10, 2022
1 parent 866c421 commit f4ee3fd
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ module Cardano.Api (
MempoolSizeAndCapacity(..),
queryTxMonitoringLocal,

TxIdInMode(..),

EraHistory(..),
getProgress,

Expand Down Expand Up @@ -681,6 +683,7 @@ import Cardano.Api.Fees
import Cardano.Api.GenesisParameters
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.InMode
import Cardano.Api.IPC
import Cardano.Api.IPC.Monad
import Cardano.Api.Key
Expand Down
31 changes: 31 additions & 0 deletions cardano-api/src/Cardano/Api/IPC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import Prelude

import Data.Void (Void)

import Data.Aeson (ToJSON, (.=), object, toJSON)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Map.Strict as Map

Expand Down Expand Up @@ -131,6 +132,7 @@ import Cardano.Api.Modes
import Cardano.Api.NetworkId
import Cardano.Api.Protocol.Types
import Cardano.Api.Query
import Cardano.Api.Tx (getTxBody)
import Cardano.Api.TxBody

-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -650,6 +652,35 @@ data LocalTxMonitoringResult mode
Consensus.MempoolSizeAndCapacity
SlotNo -- ^ Slot number at which the mempool snapshot was taken

instance ToJSON (LocalTxMonitoringResult mode) where
toJSON result =
object $ case result of
LocalTxMonitoringTxExists tx slot ->
[ "exists" .= True
, "txId" .= tx
, "slot" .= slot
]
LocalTxMonitoringTxDoesNotExist tx slot ->
[ "exists" .= False
, "txId" .= tx
, "slot" .= slot
]
LocalTxMonitoringNextTx txInMode slot ->
[ "nextTx" .= txId
, "slot" .= slot
]
where
txId = case txInMode of
Just (TxInMode tx _) -> Just $ getTxId $ getTxBody tx
-- TODO: support fetching the ID of a Byron Era transaction
_ -> Nothing
LocalTxMonitoringMempoolSizeAndCapacity mempool slot ->
[ "capacityInBytes" .= Consensus.capacityInBytes mempool
, "sizeInBytes" .= Consensus.sizeInBytes mempool
, "numberOfTxs" .= Consensus.numberOfTxs mempool
, "slot" .= slot
]

data LocalTxMonitoringQuery mode
-- | Query if a particular tx exists in the mempool. Note that, the absence
-- of a transaction does not imply anything about how the transaction was
Expand Down
6 changes: 6 additions & 0 deletions cardano-cli/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog for cardano-cli

## vNext

### Features

- Add `query tx-mempool` ([PR 4276](https://github.com/input-output-hk/cardano-node/pull/4276))

## 1.33.0 -- December 2021
## 1.32.1 -- November 2021

Expand Down
9 changes: 9 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ data QueryCmd =
-- ^ Node operational certificate
(Maybe OutputFile)
| QueryPoolState' AnyConsensusModeParams NetworkId [Hash StakePoolKey]
| QueryTxMempool AnyConsensusModeParams NetworkId TxMempoolQuery (Maybe OutputFile)
deriving Show

renderQueryCmd :: QueryCmd -> Text
Expand All @@ -390,6 +391,14 @@ renderQueryCmd cmd =
QueryStakeSnapshot' {} -> "query stake-snapshot"
QueryKesPeriodInfo {} -> "query kes-period-info"
QueryPoolState' {} -> "query pool-state"
QueryTxMempool _ _ query _ -> "query tx-mempool" <> renderTxMempoolQuery query
where
renderTxMempoolQuery query =
case query of
TxMempoolQueryTxExists tx -> "tx-exists " <> serialiseToRawBytesHexText tx
TxMempoolQueryNextTx -> "next-tx"
TxMempoolQueryInfo -> "info"


data GovernanceCmd
= GovernanceMIRPayStakeAddressesCertificate
Expand Down
21 changes: 21 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,8 @@ pQueryCmd =
(Opt.info pKesPeriodInfo $ Opt.progDesc "Get information about the current KES period and your node's operational certificate.")
, subParser "pool-state"
(Opt.info pQueryPoolState $ Opt.progDesc "Dump the pool state")
, subParser "tx-mempool"
(Opt.info pQueryTxMempool $ Opt.progDesc "Local Mempool info")
]
where
pQueryProtocolParameters :: Parser QueryCmd
Expand Down Expand Up @@ -1010,6 +1012,25 @@ pQueryCmd =
<*> pNetworkId
<*> many pStakePoolVerificationKeyHash

pQueryTxMempool :: Parser QueryCmd
pQueryTxMempool = QueryTxMempool
<$> pConsensusModeParams
<*> pNetworkId
<*> pTxMempoolQuery
<*> pMaybeOutputFile
where
pTxMempoolQuery :: Parser TxMempoolQuery
pTxMempoolQuery = asum
[ subParser "info"
(Opt.info (pure TxMempoolQueryInfo) $
Opt.progDesc "Ask the node about the current mempool's capacity and sizes")
, subParser "next-tx"
(Opt.info (pure TxMempoolQueryNextTx) $
Opt.progDesc "Requests the next transaction from the mempool's current list")
, subParser "tx-exists"
(Opt.info (TxMempoolQueryTxExists <$> argument Opt.str (metavar "TX_ID")) $
Opt.progDesc "Query if a particular transaction exists in the mempool")
]
pLeadershipSchedule :: Parser QueryCmd
pLeadershipSchedule = QueryLeadershipSchedule
<$> pConsensusModeParams
Expand Down
30 changes: 30 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ runQueryCmd cmd =
runQueryKesPeriodInfo consensusModeParams network nodeOpCert mOutFile
QueryPoolState' consensusModeParams network poolid ->
runQueryPoolState consensusModeParams network poolid
QueryTxMempool consensusModeParams network op mOutFile ->
runQueryTxMempool consensusModeParams network op mOutFile

runQueryProtocolParameters
:: AnyConsensusModeParams
Expand Down Expand Up @@ -620,6 +622,34 @@ runQueryPoolState (AnyConsensusModeParams cModeParams) network poolIds = do
result <- executeQuery era cModeParams localNodeConnInfo qInMode
obtainLedgerEraClassConstraints sbe writePoolState result

-- | Query the local mempool state
runQueryTxMempool
:: AnyConsensusModeParams
-> NetworkId
-> TxMempoolQuery
-> Maybe OutputFile
-> ExceptT ShelleyQueryCmdError IO ()
runQueryTxMempool (AnyConsensusModeParams cModeParams) network query mOutFile = do
SocketPath sockPath <- firstExceptT ShelleyQueryCmdEnvVarSocketErr readEnvSocketPath
let localNodeConnInfo = LocalNodeConnectInfo cModeParams network sockPath

localQuery <- case query of
TxMempoolQueryTxExists tx -> do
anyE@(AnyCardanoEra era) <- determineEra cModeParams localNodeConnInfo
let cMode = consensusModeOnly cModeParams
eInMode <- toEraInMode era cMode
& hoistMaybe (ShelleyQueryCmdEraConsensusModeMismatch (AnyConsensusMode cMode) anyE)
pure $ LocalTxMonitoringQueryTx $ TxIdInMode tx eInMode
TxMempoolQueryNextTx -> pure LocalTxMonitoringSendNextTx
TxMempoolQueryInfo -> pure LocalTxMonitoringMempoolInformation

result <- liftIO $ queryTxMonitoringLocal localNodeConnInfo localQuery
let renderedResult = encodePretty result
case mOutFile of
Nothing -> liftIO $ LBS.putStrLn renderedResult
Just (OutputFile oFp) -> handleIOExceptT (ShelleyQueryCmdWriteFileError . FileIOError oFp)
$ LBS.writeFile oFp renderedResult


-- | Obtain stake snapshot information for a pool, plus information about the total active stake.
-- This information can be used for leader slot calculation, for example, and has been requested by SPOs.
Expand Down
11 changes: 8 additions & 3 deletions cardano-cli/src/Cardano/CLI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Cardano.CLI.Types
, TxOutChangeAddress (..)
, TxOutDatumAnyEra (..)
, TxFile (..)
, TxMempoolQuery (..)
, UpdateProposalFile (..)
, VerificationKeyFile (..)
, Stakes (..)
Expand All @@ -53,8 +54,8 @@ import Data.Word (Word64)
import qualified Cardano.Chain.Slotting as Byron

import Cardano.Api (AddressAny, AnyScriptLanguage, EpochNo, ExecutionUnits, Hash,
InAnyCardanoEra, PaymentKey, PolicyId, ScriptData, SlotNo (SlotNo), Tx, TxIn,
Value, WitCtxMint, WitCtxStake, WitCtxTxIn)
InAnyCardanoEra, PaymentKey, PolicyId, ScriptData, SlotNo (SlotNo), Tx, TxId,
TxIn, Value, WitCtxMint, WitCtxStake, WitCtxTxIn)

import qualified Cardano.Ledger.Crypto as Crypto

Expand Down Expand Up @@ -395,4 +396,8 @@ newtype TxFile
= TxFile FilePath
deriving Show


data TxMempoolQuery =
TxMempoolQueryTxExists TxId
| TxMempoolQueryNextTx
| TxMempoolQueryInfo
deriving Show
2 changes: 1 addition & 1 deletion scripts/babbage/example-babbage-script-usage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ls -al "$CARDANO_NODE_SOCKET_PATH"
plutusspendingscript="$BASE/scripts/plutus/scripts/v2/required-redeemer.plutus"
plutusmintingscript="$BASE/scripts/plutus/scripts/v2/minting-script.plutus"
plutusstakescript="scripts/plutus/scripts/v2/stake-script.plutus"
mintpolicyid=$(cardano-cli transaction policyid --script-file $plutusmintingscript)
mintpolicyid=$($CARDANO_CLI transaction policyid --script-file $plutusmintingscript)
## This datum hash is the hash of the untyped 42
scriptdatumhash="9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88b"
datumfilepath="$BASE/scripts/plutus/data/42.datum"
Expand Down
5 changes: 3 additions & 2 deletions scripts/babbage/mkfiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ case $UNAME in
DATE="date";;
esac

CARDANO_CLI="${CARDANO_CLI:-cardano-cli}"
NETWORK_MAGIC=42
SECURITY_PARAM=10
NUM_SPO_NODES=3
Expand Down Expand Up @@ -65,7 +66,7 @@ cat > "${ROOT}/byron.genesis.spec.json" <<EOF
}
EOF

cardano-cli byron genesis genesis \
$CARDANO_CLI byron genesis genesis \
--protocol-magic ${NETWORK_MAGIC} \
--start-time "${START_TIME}" \
--k ${SECURITY_PARAM} \
Expand Down Expand Up @@ -107,7 +108,7 @@ $SED -i "${ROOT}/configuration.yaml" \
# Copy the cost mode


cardano-cli genesis create-staked --genesis-dir "${ROOT}" \
$CARDANO_CLI genesis create-staked --genesis-dir "${ROOT}" \
--testnet-magic "${NETWORK_MAGIC}" \
--gen-pools 3 \
--supply 1000000000000 \
Expand Down

0 comments on commit f4ee3fd

Please sign in to comment.