1212{-# LANGUAGE TypeFamilies #-}
1313{-# LANGUAGE ViewPatterns #-}
1414
15- {-# OPTIONS_GHC -fno-warn-orphans #-}
16-
1715-- | Various types related to ledger peers. This module is re-exported from
1816-- "Ouroboros.Network.PeerSelection.LedgerPeers".
1917--
@@ -144,8 +142,8 @@ instance FromJSON LedgerPeerSnapshot where
144142 slot <- v .: " slotNo"
145143 bigPools' <- forM (zip [0 :: Int .. ] bigPools ) \ (idx, poolO) -> do
146144 let f poolV = do
147- AccPoolStakeCoded accStake <- poolV .: " accumulatedStake"
148- PoolStakeCoded reStake <- poolV .: " relativeStake"
145+ accStake <- poolV .: " accumulatedStake"
146+ reStake <- poolV .: " relativeStake"
149147 -- decode using `LedgerRelayAccessPointV1` instance
150148 relays <- fmap getLedgerReelayAccessPointV1 <$> poolV .: " relays"
151149 return (accStake, (reStake, relays))
@@ -156,9 +154,9 @@ instance FromJSON LedgerPeerSnapshot where
156154 slot <- v .: " slotNo"
157155 bigPools' <- forM (zip [0 :: Int .. ] bigPools ) \ (idx, poolO) -> do
158156 let f poolV = do
159- AccPoolStakeCoded accStake <- poolV .: " accumulatedStake"
160- PoolStakeCoded reStake <- poolV .: " relativeStake"
161- relays <- poolV .: " relays"
157+ accStake <- poolV .: " accumulatedStake"
158+ reStake <- poolV .: " relativeStake"
159+ relays <- poolV .: " relays"
162160 return (accStake, (reStake, relays))
163161 withObject (" bigLedgerPools[" <> show idx <> " ]" ) f (Object poolO)
164162
@@ -167,9 +165,9 @@ instance FromJSON LedgerPeerSnapshot where
167165 point <- v .: " Point"
168166 bigPools' <- forM (zip [0 :: Int .. ] bigPools ) \ (idx, poolO) -> do
169167 let f poolV = do
170- AccPoolStakeCoded accStake <- poolV .: " accumulatedStake"
171- PoolStakeCoded reStake <- poolV .: " relativeStake"
172- relays <- poolV .: " relays"
168+ accStake <- poolV .: " accumulatedStake"
169+ reStake <- poolV .: " relativeStake"
170+ relays <- poolV .: " relays"
173171 return (accStake, (reStake, relays))
174172 withObject (" bigLedgerPools[" <> show idx <> " ]" ) f (Object poolO)
175173
@@ -207,47 +205,39 @@ data LedgerPeerSnapshotSRVSupport
207205
208206encodeLedgerPeerSnapshot :: LedgerPeerSnapshotSRVSupport -> LedgerPeerSnapshot -> Codec. Encoding
209207encodeLedgerPeerSnapshot LedgerPeerSnapshotDoesntSupportSRV (LedgerPeerSnapshotV2 (wOrigin, pools)) =
210- Codec. encodeListLen 2
211- <> Codec. encodeWord8 1 -- internal version
212- <> Codec. encodeListLen 2
213- <> encodeWithOrigin wOrigin
214- <> toCBOR pools'
215- where
216- pools' =
217- [(AccPoolStakeCoded accPoolStake, (PoolStakeCoded relStake, relays))
218- | (accPoolStake, (relStake, relays)) <-
219- -- filter out SRV domains, not supported by `< NodeToClientV_22`
220- map
221- (second $ second $ NonEmpty. filter
222- (\ case
223- LedgerRelayAccessSRVDomain {} -> False
224- _ -> True )
225- )
226- pools
227- , not (null relays)
228- ]
208+ Codec. encodeListLen 2
209+ <> Codec. encodeWord8 1 -- internal version
210+ <> Codec. encodeListLen 2
211+ <> encodeWithOrigin wOrigin
212+ <> toCBOR pools'
213+ where
214+ pools' =
215+ [(accPoolStake, (relStake, NonEmpty. fromList relays))
216+ | (accPoolStake, (relStake, relays)) <-
217+ -- filter out SRV domains, not supported by `< NodeToClientV_22`
218+ map
219+ (second $ second $ NonEmpty. filter
220+ (\ case
221+ LedgerRelayAccessSRVDomain {} -> False
222+ _ -> True )
223+ )
224+ pools
225+ , not (null relays)
226+ ]
227+
229228encodeLedgerPeerSnapshot LedgerPeerSnapshotSupportsSRV (LedgerPeerSnapshotV2 (wOrigin, pools)) =
230- Codec. encodeListLen 2
231- <> Codec. encodeWord8 1 -- internal version
232- <> Codec. encodeListLen 2
233- <> encodeWithOrigin wOrigin
234- <> toCBOR pools'
235- where
236- pools' =
237- [(AccPoolStakeCoded accPoolStake, (PoolStakeCoded relStake, relays))
238- | (accPoolStake, (relStake, relays)) <- pools
239- ]
229+ Codec. encodeListLen 2
230+ <> Codec. encodeWord8 1 -- internal version
231+ <> Codec. encodeListLen 2
232+ <> encodeWithOrigin wOrigin
233+ <> toCBOR pools
234+
240235encodeLedgerPeerSnapshot _LedgerPeerSnapshotSupportsSRV (LedgerPeerSnapshotV3 pt pools) =
241- Codec. encodeListLen 2
242- <> Codec. encodeWord8 3 -- internal version
243- <> Codec. encodeListLen 2
244- <> encodeLedgerPeerSnapshotPoint pt
245- <> toCBOR pools'
246- where
247- pools' =
248- [(AccPoolStakeCoded accPoolStake, (PoolStakeCoded relStake, relays))
249- | (accPoolStake, (relStake, relays)) <- pools
250- ]
236+ Codec. encodeListLen 2
237+ <> Codec. encodeWord8 3 -- internal version
238+ <> Codec. encodeListLen 2
239+ <> encodeLedgerPeerSnapshotPoint pt
240+ <> encodeStakePools pools
251241
252242
253243encodeLedgerPeerSnapshotPoint :: Point LedgerPeerSnapshot -> Codec. Encoding
@@ -284,6 +274,33 @@ decodeLedgerPeerSnapshotPoint = do
284274 _ -> fail " LedgerPeers.Type: Unrecognized CBOR encoding of Point for LedgerPeerSnapshot"
285275
286276
277+ encodeStakePools :: [(AccPoolStake , (PoolStake , NonEmpty LedgerRelayAccessPoint ))]
278+ -> Codec. Encoding
279+ encodeStakePools pools =
280+ Codec. encodeListLenIndef
281+ <> foldr (\ (AccPoolStake accPoolStake, (PoolStake poolStake, relays)) r ->
282+ Codec. encodeListLen 3
283+ <> toCBOR accPoolStake
284+ <> toCBOR poolStake
285+ <> toCBOR relays
286+ <> r)
287+ Codec. encodeBreak
288+ pools
289+
290+
291+ decodeStakePools :: Codec. Decoder s [(AccPoolStake , (PoolStake , NonEmpty LedgerRelayAccessPoint ))]
292+ decodeStakePools = do
293+ Codec. decodeListLenIndef
294+ Codec. decodeSequenceLenIndef
295+ (flip (:) ) [] reverse
296+ do
297+ Codec. decodeListLenOf 3
298+ accPoolStake <- AccPoolStake <$> fromCBOR
299+ poolStake <- PoolStake <$> fromCBOR
300+ relays <- fromCBOR
301+ return (accPoolStake, (poolStake, relays))
302+
303+
287304-- | Used by functions to indicate what kind of ledger peer to process
288305--
289306data LedgerPeersKind = AllLedgerPeers | BigLedgerPeers
@@ -306,25 +323,24 @@ isLedgerPeersEnabled :: UseLedgerPeers -> Bool
306323isLedgerPeersEnabled DontUseLedgerPeers = False
307324isLedgerPeersEnabled UseLedgerPeers {} = True
308325
326+
309327-- | The relative stake of a stakepool in relation to the total amount staked.
310328-- A value in the [0, 1] range.
311329--
312330newtype PoolStake = PoolStake { unPoolStake :: Rational }
313331 deriving (Eq , Ord , Show )
314- deriving newtype (Fractional , Num , NFData )
332+ deriving newtype (Fractional , Num , NFData , FromJSON , ToJSON , ToCBOR , FromCBOR )
333+ -- the ToCBOR and FromCBOR instances can be removed once V22 is no longer supported
315334
316- newtype PoolStakeCoded = PoolStakeCoded PoolStake
317- deriving (ToCBOR , FromCBOR , FromJSON , ToJSON ) via Rational
318335
319336-- | The accumulated relative stake of a stake pool, like PoolStake but it also includes the
320337-- relative stake of all preceding pools. A value in the range [0, 1].
321338--
322339newtype AccPoolStake = AccPoolStake { unAccPoolStake :: Rational }
323- deriving (Eq , Ord , Show )
324- deriving newtype (Fractional , Num , NFData )
340+ deriving (Eq , Ord , Show )
341+ deriving newtype (Fractional , Num , NFData , FromJSON , ToJSON , FromCBOR , ToCBOR )
342+ -- the ToCBOR and FromCBOR instances can be removed once V22 is no longer supported
325343
326- newtype AccPoolStakeCoded = AccPoolStakeCoded AccPoolStake
327- deriving (ToCBOR , FromCBOR , FromJSON , ToJSON ) via Rational
328344
329345-- | Identifies a peer as coming from ledger or not.
330346data IsLedgerPeer = IsLedgerPeer
0 commit comments