From 3f419faf0e7c6a6a7ebb7fe07a9b2dd298367559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20W=C3=B3jtowicz?= Date: Sat, 30 Mar 2024 23:03:48 +0100 Subject: [PATCH] Added GetPeerSnapshot block query: This change enables retrieval of big ledger peers. This data can be saved and loaded later, and used by the network layer to facilitate syncing up a node from a fresh or stale state. --- .../Shelley/Ledger/NetworkProtocolVersion.hs | 4 +++ .../Consensus/Shelley/Ledger/Query.hs | 29 ++++++++++++++++++- ouroboros-consensus-diffusion/CHANGELOG.md | 4 +++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/NetworkProtocolVersion.hs b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/NetworkProtocolVersion.hs index 283312674d..be56523d45 100644 --- a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/NetworkProtocolVersion.hs +++ b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/NetworkProtocolVersion.hs @@ -37,6 +37,9 @@ data ShelleyNodeToClientVersion = -- | New queries introduced: GetConstitutionHash, GetFilteredVoteDelegatees | ShelleyNodeToClientVersion8 + + -- | New queries introduced: GetPeerSnapshot + | ShelleyNodeToClientVersion9 deriving (Show, Eq, Ord, Enum, Bounded) instance HasNetworkProtocolVersion (ShelleyBlock proto era) where @@ -63,6 +66,7 @@ instance SupportedNetworkProtocolVersion (ShelleyBlock proto era) where , (NodeToClientV_14, ShelleyNodeToClientVersion6) , (NodeToClientV_15, ShelleyNodeToClientVersion7) , (NodeToClientV_16, ShelleyNodeToClientVersion8) + , (NodeToClientV_16, ShelleyNodeToClientVersion9) ] latestReleasedNodeVersion = latestReleasedNodeVersionDefault diff --git a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs index def3c190a1..9350c3513f 100644 --- a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs +++ b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Query.hs @@ -74,6 +74,7 @@ import Ouroboros.Consensus.Config import Ouroboros.Consensus.HeaderValidation import Ouroboros.Consensus.Ledger.Extended import Ouroboros.Consensus.Ledger.Query +import Ouroboros.Consensus.Ledger.SupportsPeerSelection import Ouroboros.Consensus.Protocol.Abstract (ChainDepState) import Ouroboros.Consensus.Shelley.Eras (EraCrypto) import qualified Ouroboros.Consensus.Shelley.Eras as SE @@ -87,6 +88,8 @@ import Ouroboros.Consensus.Shelley.Protocol.Abstract (ProtoCrypto) import Ouroboros.Consensus.Util (ShowProxy (..)) import Ouroboros.Network.Block (Serialised (..), decodePoint, encodePoint, mkSerialised) +import Ouroboros.Network.PeerSelection.LedgerPeers.Type +import Ouroboros.Network.PeerSelection.LedgerPeers.Util {------------------------------------------------------------------------------- QueryLedger @@ -283,6 +286,14 @@ data instance BlockQuery (ShelleyBlock proto era) :: Type -> Type where => Set (SL.Credential 'SL.Staking (EraCrypto era)) -> BlockQuery (ShelleyBlock proto era) (VoteDelegatees (EraCrypto era)) + -- | Obtain a snapshot of big ledger peers. CLI can serialize these, + -- and if made available to the node by topology configuration, + -- the diffusion layer can use these peers when syncing up from some + -- fresh or stale ledger state - especially useful for Genesis mode + GetPeerSnapshot + :: LedgerSupportsPeerSelection (ShelleyBlock proto era) + => BlockQuery (ShelleyBlock proto era) LedgerPeerSnapshot + -- WARNING: please add new queries to the end of the list and stick to this -- order in all other pattern matches on queries. This helps in particular -- with the en/decoders, as we want the CBOR tags to be ordered. @@ -434,6 +445,12 @@ instance (ShelleyCompatible proto era, ProtoCrypto proto ~ crypto) SL.queryCommitteeMembersState coldCreds hotCreds statuses st GetFilteredVoteDelegatees stakeCreds -> getFilteredVoteDelegatees st stakeCreds + GetPeerSnapshot -> + let slot = shelleyTipSlotNo <$> shelleyLedgerTip lst + second f (a, b) = (a, f b) + ledgerPeers = second (fmap stakePoolRelayAccessPoint) <$> getPeers lst + bigLedgerPeers = accBigPoolStake ledgerPeers + in LedgerPeerSnapshot (slot, bigLedgerPeers) where lcfg = configLedger $ getExtLedgerCfg cfg globals = shelleyLedgerGlobals lcfg @@ -585,6 +602,8 @@ instance SameDepIndex (BlockQuery (ShelleyBlock proto era)) where | otherwise = Nothing sameDepIndex GetFilteredVoteDelegatees {} _ = Nothing + sameDepIndex GetPeerSnapshot GetPeerSnapshot = Just Refl + sameDepIndex GetPeerSnapshot _ = Nothing deriving instance Eq (BlockQuery (ShelleyBlock proto era) result) deriving instance Show (BlockQuery (ShelleyBlock proto era) result) @@ -620,6 +639,7 @@ instance ShelleyCompatible proto era => ShowQuery (BlockQuery (ShelleyBlock prot GetDRepStakeDistr {} -> show GetCommitteeMembersState {} -> show GetFilteredVoteDelegatees {} -> show + GetPeerSnapshot -> show -- | Is the given query supported by the given 'ShelleyNodeToClientVersion'? querySupportedVersion :: BlockQuery (ShelleyBlock proto era) result -> ShelleyNodeToClientVersion -> Bool @@ -653,6 +673,7 @@ querySupportedVersion = \case GetDRepStakeDistr {} -> (>= v8) GetCommitteeMembersState {} -> (>= v8) GetFilteredVoteDelegatees {} -> (>= v8) + GetPeerSnapshot -> (>= v9) -- WARNING: when adding a new query, a new @ShelleyNodeToClientVersionX@ -- must be added. See #2830 for a template on how to do this. where @@ -664,6 +685,7 @@ querySupportedVersion = \case v6 = ShelleyNodeToClientVersion6 v7 = ShelleyNodeToClientVersion7 v8 = ShelleyNodeToClientVersion8 + v9 = ShelleyNodeToClientVersion9 {------------------------------------------------------------------------------- Auxiliary @@ -774,9 +796,11 @@ encodeShelleyQuery query = case query of CBOR.encodeListLen 4 <> CBOR.encodeWord8 27 <> toCBOR coldCreds <> toCBOR hotCreds <> LC.toEraCBOR @era statuses GetFilteredVoteDelegatees stakeCreds -> CBOR.encodeListLen 2 <> CBOR.encodeWord8 28 <> LC.toEraCBOR @era stakeCreds + GetPeerSnapshot -> + CBOR.encodeListLen 1 <> CBOR.encodeWord8 29 decodeShelleyQuery :: - forall era proto. ShelleyBasedEra era + forall era proto . (ShelleyBasedEra era, LedgerSupportsPeerSelection (ShelleyBlock proto era)) => forall s. Decoder s (SomeSecond BlockQuery (ShelleyBlock proto era)) decodeShelleyQuery = do len <- CBOR.decodeListLen @@ -830,6 +854,7 @@ decodeShelleyQuery = do return $ SomeSecond $ GetCommitteeMembersState coldCreds hotCreds statuses (2, 28) -> requireCG $ do SomeSecond . GetFilteredVoteDelegatees <$> LC.fromEraCBOR @era + (1, 29) -> return $ SomeSecond GetPeerSnapshot _ -> failmsg "invalid" encodeShelleyResult :: @@ -866,6 +891,7 @@ encodeShelleyResult v query = case query of GetDRepStakeDistr {} -> LC.toEraCBOR @era GetCommitteeMembersState {} -> LC.toEraCBOR @era GetFilteredVoteDelegatees {} -> LC.toEraCBOR @era + GetPeerSnapshot -> toCBOR decodeShelleyResult :: forall proto era result. ShelleyCompatible proto era @@ -902,6 +928,7 @@ decodeShelleyResult v query = case query of GetDRepStakeDistr {} -> LC.fromEraCBOR @era GetCommitteeMembersState {} -> LC.fromEraCBOR @era GetFilteredVoteDelegatees {} -> LC.fromEraCBOR @era + GetPeerSnapshot -> fromCBOR currentPParamsEnDecoding :: forall era s. diff --git a/ouroboros-consensus-diffusion/CHANGELOG.md b/ouroboros-consensus-diffusion/CHANGELOG.md index 6be934ded1..b031d0bc44 100644 --- a/ouroboros-consensus-diffusion/CHANGELOG.md +++ b/ouroboros-consensus-diffusion/CHANGELOG.md @@ -13,6 +13,10 @@ NOTE: version jumps from `0.13.0.0` to `0.15.0.0` because `0.14.0.0` was created `LowLevelRounNodeArgs` and `NodeKernel` records hold `PublicPeerSelectionState` variable. +### Non-Breaking + +- Added `GetPeerSnapshot` block query + ## 0.13.0.0 — 2024-04-03