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,22 +91,29 @@ 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
- migrateLedgerPeerSnapshot :: LedgerPeerSnapshot
91
- -> Maybe (WithOrigin SlotNo , [(AccPoolStake , (PoolStake , NonEmpty RelayAccessPoint ))])
92
- migrateLedgerPeerSnapshot (LedgerPeerSnapshotV2 lps) = Just lps
113
+ migrateLedgerPeerSnapshot
114
+ :: LedgerPeerSnapshot
115
+ -> Maybe LedgerPeerSnapshot
116
+ migrateLedgerPeerSnapshot snapshot@ LedgerPeerSnapshotV2 {} = Just snapshot
93
117
94
118
instance ToJSON LedgerPeerSnapshot where
95
119
toJSON (LedgerPeerSnapshotV2 (slot, pools)) =
@@ -105,7 +129,7 @@ instance ToJSON LedgerPeerSnapshot where
105
129
instance FromJSON LedgerPeerSnapshot where
106
130
parseJSON = withObject " LedgerPeerSnapshot" $ \ v -> do
107
131
vNum :: Int <- v .: " version"
108
- parsedSnapshot <-
132
+ ledgerPeerSnapshot <-
109
133
case vNum of
110
134
2 -> do
111
135
slot <- v .: " slotNo"
@@ -120,9 +144,9 @@ instance FromJSON LedgerPeerSnapshot where
120
144
121
145
return $ LedgerPeerSnapshotV2 (slot, bigPools')
122
146
_ -> fail $ " Network.LedgerPeers.Type: parseJSON: failed to parse unsupported version " <> show vNum
123
- case migrateLedgerPeerSnapshot parsedSnapshot of
124
- Just payload -> return $ LedgerPeerSnapshot payload
125
- Nothing -> fail " Network.LedgerPeers.Type: parseJSON: failed to migrate big ledger peer snapshot"
147
+ case migrateLedgerPeerSnapshot ledgerPeerSnapshot of
148
+ Just ledgerPeerSnapshot' -> return ledgerPeerSnapshot'
149
+ Nothing -> fail " Network.LedgerPeers.Type: parseJSON: failed to migrate big ledger peer snapshot"
126
150
127
151
-- | cardano-slotting provides its own {To,From}CBOR instances for WithOrigin a
128
152
-- but to pin down the encoding for CDDL we provide a wrapper with custom
@@ -198,7 +222,7 @@ newtype PoolStake = PoolStake { unPoolStake :: Rational }
198
222
deriving (Eq , Ord , Show )
199
223
deriving newtype (Fractional , Num , NFData )
200
224
201
- newtype PoolStakeCoded = PoolStakeCoded PoolStake
225
+ newtype PoolStakeCoded = PoolStakeCoded { unPoolStakeCoded :: PoolStake }
202
226
deriving (ToCBOR , FromCBOR , FromJSON , ToJSON ) via Rational
203
227
204
228
-- | The accumulated relative stake of a stake pool, like PoolStake but it also includes the
@@ -208,7 +232,7 @@ newtype AccPoolStake = AccPoolStake { unAccPoolStake :: Rational }
208
232
deriving (Eq , Ord , Show )
209
233
deriving newtype (Fractional , Num )
210
234
211
- newtype AccPoolStakeCoded = AccPoolStakeCoded AccPoolStake
235
+ newtype AccPoolStakeCoded = AccPoolStakeCoded { unAccPoolStakeCoded :: AccPoolStake }
212
236
deriving (ToCBOR , FromCBOR , FromJSON , ToJSON ) via Rational
213
237
214
238
-- | Identifies a peer as coming from ledger or not.
@@ -235,11 +259,23 @@ data IsBigLedgerPeer
235
259
--
236
260
data LedgerPeersConsensusInterface extraAPI m = LedgerPeersConsensusInterface {
237
261
lpGetLatestSlot :: STM m (WithOrigin SlotNo )
238
- , lpGetLedgerPeers :: STM m [(PoolStake , NonEmpty RelayAccessPoint )]
262
+ , lpGetLedgerPeers :: STM m [(PoolStake , NonEmpty LedgerRelayAccessPoint )]
239
263
-- | Extension point so that third party users can add more actions
240
264
, lpExtraAPI :: extraAPI
241
265
}
242
266
267
+ getRelayAccessPointsFromLedger
268
+ :: MonadSTM m
269
+ => SRVPrefix
270
+ -> LedgerPeersConsensusInterface extraAPI m
271
+ -> STM m [(PoolStake , NonEmpty RelayAccessPoint )]
272
+ getRelayAccessPointsFromLedger
273
+ srvPrefix
274
+ LedgerPeersConsensusInterface {lpGetLedgerPeers}
275
+ =
276
+ fmap (fmap (fmap (fmap (prefixLedgerRelayAccessPoint srvPrefix)))) lpGetLedgerPeers
277
+
278
+
243
279
mapExtraAPI :: (a -> b ) -> LedgerPeersConsensusInterface a m -> LedgerPeersConsensusInterface b m
244
280
mapExtraAPI f lpci@ LedgerPeersConsensusInterface { lpExtraAPI = api } =
245
281
lpci { lpExtraAPI = f api }
0 commit comments