Skip to content

Commit

Permalink
Added GetPeerSnapshot block query:
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crocodile-dentist committed Apr 18, 2024
1 parent 568897d commit 3f419fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Expand Up @@ -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
Expand All @@ -63,6 +66,7 @@ instance SupportedNetworkProtocolVersion (ShelleyBlock proto era) where
, (NodeToClientV_14, ShelleyNodeToClientVersion6)
, (NodeToClientV_15, ShelleyNodeToClientVersion7)
, (NodeToClientV_16, ShelleyNodeToClientVersion8)
, (NodeToClientV_16, ShelleyNodeToClientVersion9)
]

latestReleasedNodeVersion = latestReleasedNodeVersionDefault
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -664,6 +685,7 @@ querySupportedVersion = \case
v6 = ShelleyNodeToClientVersion6
v7 = ShelleyNodeToClientVersion7
v8 = ShelleyNodeToClientVersion8
v9 = ShelleyNodeToClientVersion9

{-------------------------------------------------------------------------------
Auxiliary
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ::
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions ouroboros-consensus-diffusion/CHANGELOG.md
Expand Up @@ -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

<a id='changelog-0.13.0.0'></a>
## 0.13.0.0 — 2024-04-03

Expand Down

0 comments on commit 3f419fa

Please sign in to comment.