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 Nov 15, 2024
1 parent 5fc3d31 commit 3314f54
Show file tree
Hide file tree
Showing 19 changed files with 375 additions and 1 deletion.
13 changes: 13 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module Cardano.CLI.EraBased.Commands.Query
, QueryDRepStakeDistributionCmdArgs (..)
, QuerySPOStakeDistributionCmdArgs (..)
, QueryTreasuryValueCmdArgs (..)
, QueryLedgerPeerSnapshotCmdArgs (..)
, renderQueryCmds
, IncludeStake (..)
)
Expand Down Expand Up @@ -65,6 +66,7 @@ data QueryCmds era
| QuerySPOStakeDistributionCmd !(QuerySPOStakeDistributionCmdArgs era)
| QueryCommitteeMembersStateCmd !(QueryCommitteeMembersStateCmdArgs era)
| QueryTreasuryValueCmd !(QueryTreasuryValueCmdArgs era)
| QueryLedgerPeerSnapshotCmd !QueryLedgerPeerSnapshotCmdArgs
deriving (Generic, Show)

data QueryLeadershipScheduleCmdArgs = QueryLeadershipScheduleCmdArgs
Expand Down Expand Up @@ -148,6 +150,15 @@ data QueryLedgerStateCmdArgs = QueryLedgerStateCmdArgs
}
deriving (Generic, Show)

data QueryLedgerPeerSnapshotCmdArgs = QueryLedgerPeerSnapshotCmdArgs
{ nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
, networkId :: !NetworkId
, target :: !(Consensus.Target ChainPoint)
, outFile :: !(File () Out)
}
deriving (Generic, Show)

data QueryProtocolStateCmdArgs = QueryProtocolStateCmdArgs
{ nodeSocketPath :: !SocketPath
, consensusModeParams :: !ConsensusModeParams
Expand Down Expand Up @@ -302,6 +313,8 @@ renderQueryCmds = \case
"query utxo"
QueryLedgerStateCmd{} ->
"query ledger-state"
QueryLedgerPeerSnapshotCmd{} ->
"query ledger-peer-snapshot"
QueryProtocolStateCmd{} ->
"query protocol-state"
QueryStakeSnapshotCmd{} ->
Expand Down
17 changes: 17 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 @@ -344,6 +351,16 @@ pQueryLedgerStateCmd era envCli =
<*> pTarget era
<*> pMaybeOutputFile

pQueryLedgerPeerSnapshotCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)
pQueryLedgerPeerSnapshotCmd era envCli =
fmap QueryLedgerPeerSnapshotCmd $
QueryLedgerPeerSnapshotCmdArgs
<$> pSocketPath envCli
<*> pConsensusModeParams
<*> pNetworkId envCli
<*> pTarget era
<*> pOutputFile

pQueryProtocolStateCmd :: ShelleyBasedEra era -> EnvCli -> Parser (QueryCmds era)
pQueryProtocolStateCmd era envCli =
fmap QueryProtocolStateCmd $
Expand Down
52 changes: 51 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Run/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Cardano.CLI.EraBased.Run.Query
, runQueryKesPeriodInfoCmd
, runQueryLeadershipScheduleCmd
, runQueryLedgerStateCmd
, runQueryLedgerPeerSnapshot
, runQueryPoolStateCmd
, runQueryProtocolParametersCmd
, runQueryProtocolStateCmd
Expand All @@ -42,7 +43,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 @@ -102,6 +103,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 @@ -843,6 +845,41 @@ runQueryLedgerStateCmd
& onLeft (left . QueryCmdAcquireFailure)
& onLeft left

runQueryLedgerPeerSnapshot
:: ()
=> Cmd.QueryLedgerPeerSnapshotCmdArgs
-> ExceptT QueryCmdError IO ()
runQueryLedgerPeerSnapshot
Cmd.QueryLedgerPeerSnapshotCmdArgs
{ 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 <-
lift (queryLedgerPeerSnapshot sbe)
& onLeft (left . QueryCmdUnsupportedNtcVersion)
& onLeft (left . QueryCmdLocalStateQueryError . EraMismatchError)

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

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

-- | Writes JSON-encoded big ledger peer snapshot
writeLedgerPeerSnapshot
:: File () Out
-> Serialised LedgerPeerSnapshot
-> ExceptT QueryCmdError IO ()
writeLedgerPeerSnapshot outPath serBigLedgerPeerSnapshot = do
snapshot <-
firstExceptT QueryCmdBigLedgerPeerSnapshotError $
hoistEither (decodeBigLedgerPeerSnapshot serBigLedgerPeerSnapshot)
firstExceptT QueryCmdWriteFileError $
newExceptT . writeLazyByteStringOutput (Just outPath) $
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 @@ -54,6 +54,7 @@ data QueryCmdError
| QueryCmdSPOKeyError !(FileError InputDecodeError)
| QueryCmdCommitteeColdKeyError !(FileError InputDecodeError)
| QueryCmdCommitteeHotKeyError !(FileError InputDecodeError)
| QueryCmdBigLedgerPeerSnapshotError DecoderError
deriving Show

renderQueryCmdError :: QueryCmdError -> Doc ann
Expand Down Expand Up @@ -114,3 +115,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 @@ -1380,6 +1380,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 @@ -1468,6 +1469,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 @@ -2453,6 +2464,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 @@ -2541,6 +2553,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 @@ -3524,6 +3546,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 @@ -3612,6 +3635,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 @@ -4594,6 +4627,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 @@ -4682,6 +4716,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 @@ -5693,6 +5737,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 @@ -5781,6 +5826,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 @@ -7328,6 +7383,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 @@ -7435,6 +7491,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 @@ -9328,6 +9397,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 @@ -9435,6 +9505,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 3314f54

Please sign in to comment.