4
4
{-# LANGUAGE DerivingVia #-}
5
5
{-# LANGUAGE FlexibleInstances #-}
6
6
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7
- {-# LANGUAGE LambdaCase #-}
7
+ {-# LANGUAGE NamedFieldPuns #-}
8
8
{-# LANGUAGE OverloadedStrings #-}
9
9
{-# LANGUAGE PatternSynonyms #-}
10
10
{-# LANGUAGE ScopedTypeVariables #-}
@@ -21,13 +21,20 @@ module Ouroboros.Network.PeerSelection.LedgerPeers.Type
21
21
, IsLedgerPeer (.. )
22
22
, IsBigLedgerPeer (.. )
23
23
, LedgerPeersConsensusInterface (.. )
24
+ , getRelayAccessPointsFromLedger
24
25
, mapExtraAPI
25
26
, UseLedgerPeers (.. )
26
27
, AfterSlot (.. )
27
28
, LedgerPeersKind (.. )
28
29
, LedgerPeerSnapshot (.. , LedgerPeerSnapshot )
30
+ , getRelayAccessPointsFromLedgerPeerSnapshot
29
31
, isLedgerPeersEnabled
30
32
, compareLedgerPeerSnapshotApproximate
33
+ -- * Re-exports
34
+ , SRVPrefix
35
+ , RelayAccessPoint (.. )
36
+ , LedgerRelayAccessPoint (.. )
37
+ , prefixLedgerRelayAccessPoint
31
38
) where
32
39
33
40
import GHC.Generics (Generic )
@@ -39,6 +46,7 @@ import Control.Concurrent.Class.MonadSTM
39
46
import Control.DeepSeq (NFData (.. ))
40
47
import Control.Monad (forM )
41
48
import Data.Aeson
49
+ import Data.Bifunctor (first )
42
50
import Data.List.NonEmpty (NonEmpty )
43
51
import NoThunks.Class
44
52
@@ -49,15 +57,24 @@ import Ouroboros.Network.PeerSelection.RelayAccessPoint
49
57
-- to connect to when syncing.
50
58
--
51
59
data LedgerPeerSnapshot =
52
- LedgerPeerSnapshotV2 (WithOrigin SlotNo , [(AccPoolStake , (PoolStake , NonEmpty RelayAccessPoint ))])
60
+ LedgerPeerSnapshotV2 (WithOrigin SlotNo , [(AccPoolStake , (PoolStake , NonEmpty LedgerRelayAccessPoint ))])
53
61
-- ^ Internal use for version 2, use pattern synonym for public API
54
62
deriving (Eq , Show )
55
63
64
+
65
+ getRelayAccessPointsFromLedgerPeerSnapshot
66
+ :: SRVPrefix
67
+ -> LedgerPeerSnapshot
68
+ -> (WithOrigin SlotNo , [(AccPoolStake , (PoolStake , NonEmpty RelayAccessPoint ))])
69
+ getRelayAccessPointsFromLedgerPeerSnapshot srvPrefix (LedgerPeerSnapshotV2 as) =
70
+ fmap (fmap (fmap (fmap (fmap (prefixLedgerRelayAccessPoint srvPrefix))))) as
71
+
72
+
56
73
-- | Public API to access snapshot data. Currently access to only most recent version is available.
57
74
-- Nonetheless, serialisation from the node into JSON is supported for older versions via internal
58
75
-- api so that newer CLI can still support older node formats.
59
76
--
60
- pattern LedgerPeerSnapshot :: (WithOrigin SlotNo , [(AccPoolStake , (PoolStake , NonEmpty RelayAccessPoint ))])
77
+ pattern LedgerPeerSnapshot :: (WithOrigin SlotNo , [(AccPoolStake , (PoolStake , NonEmpty LedgerRelayAccessPoint ))])
61
78
-> LedgerPeerSnapshot
62
79
pattern LedgerPeerSnapshot payload <- LedgerPeerSnapshotV2 payload where
63
80
LedgerPeerSnapshot payload = LedgerPeerSnapshotV2 payload
@@ -74,21 +91,27 @@ pattern LedgerPeerSnapshot payload <- LedgerPeerSnapshotV2 payload where
74
91
-- The two approximate values should be equal if they were created
75
92
-- from the same 'faithful' data.
76
93
--
77
- compareLedgerPeerSnapshotApproximate :: LedgerPeerSnapshot
78
- -> LedgerPeerSnapshot
94
+ compareLedgerPeerSnapshotApproximate :: [( AccPoolStake , ( PoolStake , NonEmpty RelayAccessPoint ))]
95
+ -> [( AccPoolStake , ( PoolStake , NonEmpty RelayAccessPoint ))]
79
96
-> Bool
80
97
compareLedgerPeerSnapshotApproximate baseline candidate =
81
98
case tripIt of
82
99
Success candidate' -> candidate' == baseline
83
100
Error _ -> False
84
101
where
85
- tripIt = fromJSON . toJSON $ candidate
102
+ tripIt = fmap (fmap (fmap (first unPoolStakeCoded)))
103
+ . fmap (fmap (first unAccPoolStakeCoded))
104
+ . fromJSON
105
+ . toJSON
106
+ . fmap (fmap (first PoolStakeCoded ))
107
+ . fmap (first AccPoolStakeCoded )
108
+ $ candidate
86
109
87
110
-- | In case the format changes in the future, this function provides a migration functionality
88
111
-- when possible.
89
112
--
90
113
migrateLedgerPeerSnapshot :: LedgerPeerSnapshot
91
- -> Maybe (WithOrigin SlotNo , [(AccPoolStake , (PoolStake , NonEmpty RelayAccessPoint ))])
114
+ -> Maybe (WithOrigin SlotNo , [(AccPoolStake , (PoolStake , NonEmpty LedgerRelayAccessPoint ))])
92
115
migrateLedgerPeerSnapshot (LedgerPeerSnapshotV2 lps) = Just lps
93
116
94
117
instance ToJSON LedgerPeerSnapshot where
@@ -118,7 +141,7 @@ instance FromJSON LedgerPeerSnapshot where
118
141
return (accStake, (reStake, relays))
119
142
withObject (" bigLedgerPools[" <> show idx <> " ]" ) f (Object poolO)
120
143
121
- return $ LedgerPeerSnapshotV2 (slot, bigPools')
144
+ return $ LedgerPeerSnapshot (slot, bigPools')
122
145
_ -> fail $ " Network.LedgerPeers.Type: parseJSON: failed to parse unsupported version " <> show vNum
123
146
case migrateLedgerPeerSnapshot parsedSnapshot of
124
147
Just payload -> return $ LedgerPeerSnapshot payload
@@ -198,7 +221,7 @@ newtype PoolStake = PoolStake { unPoolStake :: Rational }
198
221
deriving (Eq , Ord , Show )
199
222
deriving newtype (Fractional , Num , NFData )
200
223
201
- newtype PoolStakeCoded = PoolStakeCoded PoolStake
224
+ newtype PoolStakeCoded = PoolStakeCoded { unPoolStakeCoded :: PoolStake }
202
225
deriving (ToCBOR , FromCBOR , FromJSON , ToJSON ) via Rational
203
226
204
227
-- | The accumulated relative stake of a stake pool, like PoolStake but it also includes the
@@ -208,7 +231,7 @@ newtype AccPoolStake = AccPoolStake { unAccPoolStake :: Rational }
208
231
deriving (Eq , Ord , Show )
209
232
deriving newtype (Fractional , Num )
210
233
211
- newtype AccPoolStakeCoded = AccPoolStakeCoded AccPoolStake
234
+ newtype AccPoolStakeCoded = AccPoolStakeCoded { unAccPoolStakeCoded :: AccPoolStake }
212
235
deriving (ToCBOR , FromCBOR , FromJSON , ToJSON ) via Rational
213
236
214
237
-- | Identifies a peer as coming from ledger or not.
@@ -235,11 +258,23 @@ data IsBigLedgerPeer
235
258
--
236
259
data LedgerPeersConsensusInterface extraAPI m = LedgerPeersConsensusInterface {
237
260
lpGetLatestSlot :: STM m (WithOrigin SlotNo )
238
- , lpGetLedgerPeers :: STM m [(PoolStake , NonEmpty RelayAccessPoint )]
261
+ , lpGetLedgerPeers :: STM m [(PoolStake , NonEmpty LedgerRelayAccessPoint )]
239
262
-- | Extension point so that third party users can add more actions
240
263
, lpExtraAPI :: extraAPI
241
264
}
242
265
266
+ getRelayAccessPointsFromLedger
267
+ :: MonadSTM m
268
+ => SRVPrefix
269
+ -> LedgerPeersConsensusInterface extraAPI m
270
+ -> STM m [(PoolStake , NonEmpty RelayAccessPoint )]
271
+ getRelayAccessPointsFromLedger
272
+ srvPrefix
273
+ LedgerPeersConsensusInterface {lpGetLedgerPeers}
274
+ =
275
+ fmap (fmap (fmap (fmap (prefixLedgerRelayAccessPoint srvPrefix)))) lpGetLedgerPeers
276
+
277
+
243
278
mapExtraAPI :: (a -> b ) -> LedgerPeersConsensusInterface a m -> LedgerPeersConsensusInterface b m
244
279
mapExtraAPI f lpci@ LedgerPeersConsensusInterface { lpExtraAPI = api } =
245
280
lpci { lpExtraAPI = f api }
0 commit comments