Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query a node for a snapshot of big ledger peers #727

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ write-ghc-environment-files: always
-- IMPORTANT
-- Do NOT add more source-repository-package stanzas here unless they are strictly
-- temporary! Please read the section in CONTRIBUTING about updating dependencies.

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-api
tag: 64bdc19787155fba75a5dcf9072173881ded61f9
--sha256: sha256-HCTkYETqWZyiRHAyu6ynHpLAmIGZBoNHSrfQDj8gTjI=
subdir:
cardano-api

source-repository-package
type: git
location: https://github.com/IntersectMBO/ouroboros-consensus
tag: 28fdf3828f87fa9c50d78093fabfda33a78dba73
--sha256: sha256-AVFBAl6hUpWphigBw93wp0vjuoKJ9Emx20QNJsxqBEw=
subdir:
ouroboros-consensus
ouroboros-consensus-diffusion
ouroboros-consensus-cardano
ouroboros-consensus-protocol
6 changes: 3 additions & 3 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ library
binary,
bytestring,
canonical-json,
cardano-api ^>=10.1,
cardano-api,
cardano-binary,
cardano-crypto,
cardano-crypto-class ^>=2.1.2,
cardano-crypto-wrapper ^>=1.5.1,
cardano-data >=1.1,
cardano-git-rev ^>=0.2.2,
cardano-ping ^>=0.5,
cardano-ping,
cardano-prelude,
cardano-slotting ^>=0.2.0.0,
cardano-strict-containers ^>=0.1,
Expand All @@ -235,7 +235,7 @@ library
ouroboros-consensus ^>=0.21,
ouroboros-consensus-cardano ^>=0.20,
ouroboros-consensus-protocol ^>=0.9.0.2,
ouroboros-network-api ^>=0.10,
ouroboros-network-api,
ouroboros-network-protocols,
parsec,
prettyprinter,
Expand Down
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
10 changes: 5 additions & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ pNumberOfByronKeyWitnesses =

pTotalUTxOValue :: Parser Value
pTotalUTxOValue =
Opt.option (readerFromParsecParser parseValue) $
Opt.option (readerFromParsecParser $ parseValue RoleUTxO) $
mconcat
[ Opt.long "total-utxo-value"
, Opt.metavar "VALUE"
Expand Down Expand Up @@ -2136,7 +2136,7 @@ pMintMultiAsset
pMintMultiAsset sbe balanceExecUnits =
(,)
<$> Opt.option
(readerFromParsecParser parseValue)
(readerFromParsecParser $ parseValue RoleUTxO)
( Opt.long "mint"
<> Opt.metavar "VALUE"
<> Opt.help helpText
Expand Down Expand Up @@ -2194,7 +2194,7 @@ pMintMultiAsset sbe balanceExecUnits =

pPolicyId :: Parser PolicyId
pPolicyId =
Opt.option (readerFromParsecParser policyId) $
Opt.option (readerFromParsecParser parsePolicyId) $
mconcat
[ Opt.long "policy-id"
, Opt.metavar "HASH"
Expand Down Expand Up @@ -3308,7 +3308,7 @@ parseTxOutShelleyBasedEra = do
-- Accept the old style of separating the address and value in a
-- transaction output:
Parsec.option () (Parsec.char '+' >> Parsec.spaces)
val <- parseValue
val <- parseValue RoleUTxO
return (TxOutShelleyBasedEra addr val)

parseShelleyAddress :: Parsec.Parser (Address ShelleyAddr)
Expand All @@ -3326,7 +3326,7 @@ parseTxOutAnyEra = do
-- Accept the old style of separating the address and value in a
-- transaction output:
Parsec.option () (Parsec.char '+' >> Parsec.spaces)
val <- parseValue
val <- parseValue RoleUTxO
return (TxOutAnyEra addr val)

--------------------------------------------------------------------------------
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
51 changes: 51 additions & 0 deletions 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 Down Expand Up @@ -64,6 +65,7 @@ import qualified Ouroboros.Consensus.Protocol.Abstract as Consensus
import qualified Ouroboros.Consensus.Protocol.Praos.Common as Consensus
import Ouroboros.Consensus.Protocol.TPraos (StandardCrypto)
import Ouroboros.Network.Block (Serialised (..))
import Ouroboros.Network.PeerSelection.LedgerPeers.Type
import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as Consensus

import Control.Monad (forM, forM_, join)
Expand Down Expand Up @@ -107,6 +109,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 @@ -848,6 +851,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 @@ -1033,6 +1071,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 @@ -55,6 +55,7 @@ data QueryCmdError
| QueryCmdSPOKeyError !(FileError InputDecodeError)
| QueryCmdCommitteeColdKeyError !(FileError InputDecodeError)
| QueryCmdCommitteeHotKeyError !(FileError InputDecodeError)
| QueryCmdBigLedgerPeerSnapshotError DecoderError
deriving Show

renderQueryCmdError :: QueryCmdError -> Doc ann
Expand Down Expand Up @@ -115,3 +116,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
Loading