Skip to content

Commit

Permalink
Add ledger-peer-snapshot to query command:
Browse files Browse the repository at this point in the history
This change introduces query subcommand ledger-peer-snapshot to
serialize a snapshot of big ledger peers from the tip of the current
chain.
  • Loading branch information
crocodile-dentist committed Jan 10, 2025
1 parent abb632c commit 83b745c
Show file tree
Hide file tree
Showing 19 changed files with 372 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Cardano.CLI.EraBased.Commands.Query
, QueryDRepStakeDistributionCmdArgs (..)
, QuerySPOStakeDistributionCmdArgs (..)
, QueryTreasuryValueCmdArgs (..)
, QueryLedgerPeerSnapshotCmdArgs (..)
, renderQueryCmds
, IncludeStake (..)
)
Expand Down Expand Up @@ -69,6 +70,7 @@ data QueryCmds era
| QueryCommitteeMembersStateCmd !(QueryCommitteeMembersStateCmdArgs era)
| QueryTreasuryValueCmd !(QueryTreasuryValueCmdArgs era)
| QueryProposalsCmd !(QueryProposalsCmdArgs era)
| QueryLedgerPeerSnapshotCmd !QueryLedgerPeerSnapshotCmdArgs
deriving (Generic, Show)

-- | Fields that are common to most queries
Expand Down Expand Up @@ -140,6 +142,12 @@ data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
}
deriving (Generic, Show)

data QueryLedgerPeerSnapshotCmdArgs = QueryLedgerPeerSnapshotCmdArgs
{ commons :: !QueryCommons
, outFile :: !(Maybe (File () Out))
}
deriving (Generic, Show)

data QueryProtocolStateCmdArgs = QueryProtocolStateCmdArgs
{ commons :: !QueryCommons
, mOutFile :: !(Maybe (File () Out))
Expand Down Expand Up @@ -266,6 +274,8 @@ renderQueryCmds = \case
"query utxo"
QueryLedgerStateCmd{} ->
"query ledger-state"
QueryLedgerPeerSnapshotCmd{} ->
"query ledger-peer-snapshot"
QueryProtocolStateCmd{} ->
"query protocol-state"
QueryStakeSnapshotCmd{} ->
Expand Down
14 changes: 14 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ pQueryCmds era envCli =
mconcat
[ "Dump the current ledger state of the node (Ledger.NewEpochState -- advanced command)"
]
, Just $
subParser "ledger-peer-snapshot" $
Opt.info (pQueryLedgerPeerSnapshotCmd era envCli) $
Opt.progDesc $
mconcat
[ "Dump the current snapshot of ledger peers"
]
, Just $
subParser "protocol-state" $
Opt.info (pQueryProtocolStateCmd era envCli) $
Expand Down Expand Up @@ -327,6 +334,13 @@ pQueryLedgerStateCmd era envCli =
<$> pQueryCommons era envCli
<*> pMaybeOutputFile

pQueryLedgerPeerSnapshotCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)
pQueryLedgerPeerSnapshotCmd era envCli =
fmap QueryLedgerPeerSnapshotCmd $
QueryLedgerPeerSnapshotCmdArgs
<$> pQueryCommons era envCli
<*> pMaybeOutputFile

pQueryProtocolStateCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)
pQueryProtocolStateCmd era envCli =
fmap QueryProtocolStateCmd $
Expand Down
55 changes: 54 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Cardano.CLI.EraBased.Run.Query
, runQueryKesPeriodInfoCmd
, runQueryLeadershipScheduleCmd
, runQueryLedgerStateCmd
, runQueryLedgerPeerSnapshot
, runQueryPoolStateCmd
, runQueryProtocolParametersCmd
, runQueryProtocolStateCmd
Expand All @@ -43,7 +44,7 @@ import qualified Cardano.Api as Api
import qualified Cardano.Api.Consensus as Consensus
import Cardano.Api.Ledger (StandardCrypto, strictMaybeToMaybe)
import qualified Cardano.Api.Ledger as L
import Cardano.Api.Network (Serialised (..))
import Cardano.Api.Network (LedgerPeerSnapshot, Serialised (..))
import qualified Cardano.Api.Network as Consensus
import Cardano.Api.Shelley hiding (QueryInShelleyBasedEra (..))

Expand Down Expand Up @@ -103,6 +104,7 @@ runQueryCmds = \case
Cmd.QueryStakeDistributionCmd args -> runQueryStakeDistributionCmd args
Cmd.QueryStakeAddressInfoCmd args -> runQueryStakeAddressInfoCmd args
Cmd.QueryLedgerStateCmd args -> runQueryLedgerStateCmd args
Cmd.QueryLedgerPeerSnapshotCmd args -> runQueryLedgerPeerSnapshot args
Cmd.QueryStakeSnapshotCmd args -> runQueryStakeSnapshotCmd args
Cmd.QueryProtocolStateCmd args -> runQueryProtocolStateCmd args
Cmd.QueryUTxOCmd args -> runQueryUTxOCmd args
Expand Down Expand Up @@ -834,6 +836,41 @@ runQueryLedgerStateCmd
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left

runQueryLedgerPeerSnapshot
:: ()
=> Cmd.QueryLedgerPeerSnapshotCmdArgs
-> ExceptT QueryCmdError IO ()
runQueryLedgerPeerSnapshot
Cmd.QueryLedgerPeerSnapshotCmdArgs
{ Cmd.commons =
Cmd.QueryCommons
{ Cmd.nodeSocketPath
, Cmd.consensusModeParams
, Cmd.networkId
, Cmd.target
}
, Cmd.outFile
} = do
let localNodeConnInfo = LocalNodeConnectInfo consensusModeParams networkId nodeSocketPath

join $
lift
( executeLocalStateQueryExpr localNodeConnInfo target $ runExceptT $ do
AnyCardanoEra era <-
lift queryCurrentEra
& onLeft (left . QueryCmdUnsupportedNtcVersion)

sbe <-
requireShelleyBasedEra era
& onNothing (left QueryCmdByronEra)

result <- easyRunQuery (queryLedgerPeerSnapshot sbe)

pure $ shelleyBasedEraConstraints sbe (writeLedgerPeerSnapshot outFile) result
)
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left

runQueryProtocolStateCmd
:: ()
=> Cmd.QueryProtocolStateCmdArgs
Expand Down Expand Up @@ -1040,6 +1077,22 @@ writeLedgerState mOutFile qState@(SerialisedDebugLedgerState serLedgerState) =
LBS.writeFile fpath $
unSerialised serLedgerState

-- | Writes JSON-encoded big ledger peer snapshot
writeLedgerPeerSnapshot
:: Maybe (File () Out)
-> Serialised LedgerPeerSnapshot
-> ExceptT QueryCmdError IO ()
writeLedgerPeerSnapshot mOutPath serBigLedgerPeerSnapshot = do
case decodeBigLedgerPeerSnapshot serBigLedgerPeerSnapshot of
Left (bs, _decoderError) ->
firstExceptT QueryCmdHelpersError $ pPrintCBOR bs
Right snapshot ->
case mOutPath of
Nothing -> liftIO . LBS.putStrLn $ Aeson.encode snapshot
Just fpath ->
firstExceptT QueryCmdWriteFileError $
newExceptT . writeLazyByteStringFile fpath $ encodePretty snapshot

writeStakeSnapshots
:: forall era ledgerera
. ()
Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Errors/QueryCmdError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ data QueryCmdError
| QueryCmdSPOKeyError !(FileError InputDecodeError)
| QueryCmdCommitteeColdKeyError !(FileError InputDecodeError)
| QueryCmdCommitteeHotKeyError !(FileError InputDecodeError)
| QueryCmdBigLedgerPeerSnapshotError DecoderError
deriving Show

mkEraMismatchError :: NodeEraMismatchError -> QueryCmdError
Expand Down Expand Up @@ -121,3 +122,5 @@ renderQueryCmdError = \case
"Error reading committee cold key: " <> prettyError e
QueryCmdCommitteeHotKeyError e ->
"Error reading committee hot key: " <> prettyError e
QueryCmdBigLedgerPeerSnapshotError decoderError ->
"Error decoding big ledger peer snapshot: " <> pshow decoderError
83 changes: 83 additions & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,7 @@ Usage: cardano-cli shelley query
| stake-address-info
| utxo
| ledger-state
| ledger-peer-snapshot
| protocol-state
| stake-snapshot
| leadership-schedule
Expand Down Expand Up @@ -1448,6 +1449,16 @@ Usage: cardano-cli shelley query ledger-state --socket-path SOCKET_PATH
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
command)

Usage: cardano-cli shelley query ledger-peer-snapshot --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
( --mainnet
| --testnet-magic NATURAL
)
--out-file FILEPATH

Dump the current snapshot of ledger peers

Usage: cardano-cli shelley query protocol-state --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
Expand Down Expand Up @@ -2411,6 +2422,7 @@ Usage: cardano-cli allegra query
| stake-address-info
| utxo
| ledger-state
| ledger-peer-snapshot
| protocol-state
| stake-snapshot
| leadership-schedule
Expand Down Expand Up @@ -2499,6 +2511,16 @@ Usage: cardano-cli allegra query ledger-state --socket-path SOCKET_PATH
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
command)

Usage: cardano-cli allegra query ledger-peer-snapshot --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
( --mainnet
| --testnet-magic NATURAL
)
--out-file FILEPATH

Dump the current snapshot of ledger peers

Usage: cardano-cli allegra query protocol-state --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
Expand Down Expand Up @@ -3460,6 +3482,7 @@ Usage: cardano-cli mary query
| stake-address-info
| utxo
| ledger-state
| ledger-peer-snapshot
| protocol-state
| stake-snapshot
| leadership-schedule
Expand Down Expand Up @@ -3548,6 +3571,16 @@ Usage: cardano-cli mary query ledger-state --socket-path SOCKET_PATH
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
command)

Usage: cardano-cli mary query ledger-peer-snapshot --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
( --mainnet
| --testnet-magic NATURAL
)
--out-file FILEPATH

Dump the current snapshot of ledger peers

Usage: cardano-cli mary query protocol-state --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
Expand Down Expand Up @@ -4508,6 +4541,7 @@ Usage: cardano-cli alonzo query
| stake-address-info
| utxo
| ledger-state
| ledger-peer-snapshot
| protocol-state
| stake-snapshot
| leadership-schedule
Expand Down Expand Up @@ -4596,6 +4630,16 @@ Usage: cardano-cli alonzo query ledger-state --socket-path SOCKET_PATH
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
command)

Usage: cardano-cli alonzo query ledger-peer-snapshot --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
( --mainnet
| --testnet-magic NATURAL
)
--out-file FILEPATH

Dump the current snapshot of ledger peers

Usage: cardano-cli alonzo query protocol-state --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
Expand Down Expand Up @@ -5605,6 +5649,7 @@ Usage: cardano-cli babbage query
| stake-address-info
| utxo
| ledger-state
| ledger-peer-snapshot
| protocol-state
| stake-snapshot
| leadership-schedule
Expand Down Expand Up @@ -5693,6 +5738,16 @@ Usage: cardano-cli babbage query ledger-state --socket-path SOCKET_PATH
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
command)

Usage: cardano-cli babbage query ledger-peer-snapshot --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
( --mainnet
| --testnet-magic NATURAL
)
--out-file FILEPATH

Dump the current snapshot of ledger peers

Usage: cardano-cli babbage query protocol-state --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
Expand Down Expand Up @@ -7243,6 +7298,7 @@ Usage: cardano-cli conway query
| stake-address-info
| utxo
| ledger-state
| ledger-peer-snapshot
| protocol-state
| stake-snapshot
| leadership-schedule
Expand Down Expand Up @@ -7351,6 +7407,19 @@ Usage: cardano-cli conway query ledger-state --socket-path SOCKET_PATH
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
command)

Usage: cardano-cli conway query ledger-peer-snapshot --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
( --mainnet
| --testnet-magic NATURAL
)
[ --volatile-tip
| --immutable-tip
]
--out-file FILEPATH

Dump the current snapshot of ledger peers

Usage: cardano-cli conway query protocol-state --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
Expand Down Expand Up @@ -9264,6 +9333,7 @@ Usage: cardano-cli latest query
| stake-address-info
| utxo
| ledger-state
| ledger-peer-snapshot
| protocol-state
| stake-snapshot
| leadership-schedule
Expand Down Expand Up @@ -9372,6 +9442,19 @@ Usage: cardano-cli latest query ledger-state --socket-path SOCKET_PATH
Dump the current ledger state of the node (Ledger.NewEpochState -- advanced
command)

Usage: cardano-cli latest query ledger-peer-snapshot --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
( --mainnet
| --testnet-magic NATURAL
)
[ --volatile-tip
| --immutable-tip
]
--out-file FILEPATH

Dump the current snapshot of ledger peers

Usage: cardano-cli latest query protocol-state --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Usage: cardano-cli allegra query
| stake-address-info
| utxo
| ledger-state
| ledger-peer-snapshot
| protocol-state
| stake-snapshot
| leadership-schedule
Expand Down Expand Up @@ -33,6 +34,7 @@ Available commands:
address or the whole.
ledger-state Dump the current ledger state of the node
(Ledger.NewEpochState -- advanced command)
ledger-peer-snapshot Dump the current snapshot of ledger peers
protocol-state Dump the current protocol state of the node
(Ledger.ChainDepState -- advanced command)
stake-snapshot Obtain the three stake snapshots for a pool, plus the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Usage: cardano-cli allegra query ledger-peer-snapshot --socket-path SOCKET_PATH
[--cardano-mode
[--epoch-slots SLOTS]]
( --mainnet
| --testnet-magic NATURAL
)
--out-file FILEPATH

Dump the current snapshot of ledger peers

Available options:
--socket-path SOCKET_PATH
Path to the node socket. This overrides the
CARDANO_NODE_SOCKET_PATH environment variable. The
argument is optional if CARDANO_NODE_SOCKET_PATH is
defined and mandatory otherwise.
--cardano-mode For talking to a node running in full Cardano mode
(default).
--epoch-slots SLOTS The number of slots per epoch for the Byron era.
(default: 21600)
--mainnet Use the mainnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--testnet-magic NATURAL Specify a testnet magic id. This overrides the
CARDANO_NODE_NETWORK_ID environment variable
--out-file FILEPATH The output file.
-h,--help Show this help text
Loading

0 comments on commit 83b745c

Please sign in to comment.