@@ -22,6 +22,7 @@ import Data.Functor ((<&>))
2222import Data.List.NonEmpty qualified as NonEmpty
2323import Data.Map.Strict qualified as Map
2424import Data.Proxy
25+ import Data.SOP.Strict.NS (NS (.. ))
2526import Data.Void
2627
2728import Cardano.Chain.Slotting (EpochSlots (.. ))
@@ -32,9 +33,13 @@ import Cardano.Network.PeerSelection (LedgerPeerSnapshot (..),
3233import Cardano.Slotting.EpochInfo.API
3334import Cardano.Slotting.Slot (EpochNo )
3435import Cardano.Slotting.Time
36+
3537import DMQ.Diffusion.NodeKernel
3638import Ouroboros.Consensus.Cardano.Block
3739import Ouroboros.Consensus.Cardano.Node
40+ import Ouroboros.Consensus.HardFork.Combinator (EraIndex )
41+ import Ouroboros.Consensus.HardFork.Combinator.Abstract.SingleEraBlock
42+ (EraIndex (.. ))
3843import Ouroboros.Consensus.HardFork.Combinator.Ledger.Query
3944import Ouroboros.Consensus.HardFork.History.EpochInfo (interpreterToEpochInfo )
4045import Ouroboros.Consensus.HardFork.History.Qry (PastHorizonException )
@@ -84,6 +89,12 @@ instance ToJSON TraceLocalStateQueryClient where
8489 LedgerPeersNotAvailable ->
8590 object [ " kind" .= Aeson. String " LedgerPeersNotAvailable" ]
8691
92+
93+ data QueryError = UnsupportedEra
94+ deriving Show
95+
96+ instance Exception QueryError where
97+
8798-- TODO generalize to handle ledger eras other than Conway
8899-- | connects the dmq node to cardano node via local state query
89100-- and updates the node kernel with stake pool data necessary to perform message
@@ -126,6 +137,9 @@ cardanoClient tracer ledgerPeers
126137 throwIO . userError $ " recvMsgFailure: " <> show failure
127138 }
128139
140+ wrappingMismatch :: forall err r .
141+ (r -> m (ClientStAcquired block point query m Void ))
142+ -> ClientStQuerying block point query m Void (Either err r )
129143 wrappingMismatch k = ClientStQuerying $
130144 either (const . throwIO . userError $ " mismatch era info" ) k
131145
@@ -152,11 +166,10 @@ cardanoClient tracer ledgerPeers
152166 Right relativeTime -> do
153167 let nextEpoch = fromRelativeTime systemStart relativeTime
154168 -- continue with stake snapshot query
155- pure $ queryStakeSnapshots systemStart nextEpoch
169+ pure $ queryCurrentEra systemStart nextEpoch
156170
157171
158- -- query stake snapshot
159- queryStakeSnapshots
172+ queryCurrentEra
160173 :: SystemStart
161174 -> UTCTime
162175 -> ClientStAcquired
@@ -165,9 +178,44 @@ cardanoClient tracer ledgerPeers
165178 (Query (CardanoBlock crypto ))
166179 m
167180 Void
168- queryStakeSnapshots systemStart nextEpoch =
169- SendMsgQuery (BlockQuery . QueryIfCurrentConway $ GetStakeSnapshots Nothing )
170- $ wrappingMismatch handleStakeSnapshots
181+ queryCurrentEra systemStart nextEpoch =
182+ SendMsgQuery (BlockQuery (QueryHardFork GetCurrentEra ))
183+ $ ClientStQuerying $ \ era -> queryStakeSnapshots systemStart nextEpoch era
184+
185+ -- query stake snapshot
186+ queryStakeSnapshots
187+ :: SystemStart
188+ -> UTCTime
189+ -> EraIndex idx
190+ -> m (ClientStAcquired
191+ (CardanoBlock crypto )
192+ (Point (CardanoBlock crypto ))
193+ (Query (CardanoBlock crypto ))
194+ m
195+ Void )
196+ queryStakeSnapshots systemStart nextEpoch era =
197+ case getEraIndex era of
198+ Z _ -> throwIO UnsupportedEra
199+ S Z {} -> return $ SendMsgQuery (BlockQuery (QueryIfCurrentShelley (GetStakeSnapshots Nothing )))
200+ $ wrappingMismatch handleStakeSnapshots
201+ S (S Z {}) -> return $ SendMsgQuery (BlockQuery (QueryIfCurrentAllegra (GetStakeSnapshots Nothing )))
202+ $ wrappingMismatch handleStakeSnapshots
203+ S (S (S Z {})) -> return $ SendMsgQuery (BlockQuery (QueryIfCurrentMary (GetStakeSnapshots Nothing )))
204+ $ wrappingMismatch handleStakeSnapshots
205+ S (S (S (S Z {}))) -> return $ SendMsgQuery (BlockQuery (QueryIfCurrentAlonzo (GetStakeSnapshots Nothing )))
206+ $ wrappingMismatch handleStakeSnapshots
207+ S (S (S (S (S Z {})))) -> return $ SendMsgQuery (BlockQuery (QueryIfCurrentBabbage (GetStakeSnapshots Nothing )))
208+ $ wrappingMismatch handleStakeSnapshots
209+ S (S (S (S (S (S Z {}))))) -> return $ SendMsgQuery (BlockQuery (QueryIfCurrentConway (GetStakeSnapshots Nothing )))
210+ $ wrappingMismatch handleStakeSnapshots
211+ S (S (S (S (S (S (S Z {})))))) -> return $ SendMsgQuery (BlockQuery (QueryIfCurrentDijkstra (GetStakeSnapshots Nothing )))
212+ $ wrappingMismatch handleStakeSnapshots
213+ -- TODO: requires manual intervention when new era is introduced, it
214+ -- would be nice if `ouroboros-consensus` exposed its
215+ -- `TagByron..TagDjikstra` patterns and made them complete as all the
216+ -- other patterns are. Then we'd get an incomplete GHC warning when
217+ -- a new era is introduced.
218+ _ -> throwIO UnsupportedEra
171219 where
172220 handleStakeSnapshots
173221 :: StakeSnapshots
0 commit comments