@@ -13,24 +13,22 @@ module Cardano.DbSync.Era.Shelley.Generic.StakeDist (
13
13
StakeSliceRes (.. ),
14
14
StakeSlice (.. ),
15
15
getSecurityParameter ,
16
- getStakeSlice ,
16
+ snapShotToList ,
17
+ getSnapShot ,
17
18
getPoolDistr ,
18
19
) where
19
20
20
21
import Cardano.DbSync.Types
21
22
import Cardano.Ledger.Coin (Coin (.. ))
22
23
import qualified Cardano.Ledger.Compactible as Ledger
23
- import Cardano.Ledger.Credential (Credential )
24
24
import qualified Cardano.Ledger.EpochBoundary as Ledger
25
25
import Cardano.Ledger.Era (EraCrypto )
26
- import Cardano.Ledger.Keys (KeyHash (.. ), KeyRole (.. ))
27
26
import qualified Cardano.Ledger.Shelley.LedgerState as Shelley
28
27
import Cardano.Ledger.Val ((<+>) )
29
28
import Cardano.Prelude
30
29
import qualified Data.Map.Strict as Map
31
30
import Data.VMap (VB , VMap (.. ), VP )
32
31
import qualified Data.VMap as VMap
33
- import qualified Data.Vector.Generic as VG
34
32
import Lens.Micro
35
33
import Ouroboros.Consensus.Block
36
34
import Ouroboros.Consensus.Cardano.Block (LedgerState (.. ), StandardCrypto )
@@ -53,9 +51,6 @@ data StakeSlice = StakeSlice
53
51
}
54
52
deriving (Eq )
55
53
56
- emptySlice :: EpochNo -> StakeSlice
57
- emptySlice epoch = StakeSlice epoch Map. empty
58
-
59
54
getSecurityParameter ::
60
55
ConsensusProtocol (BlockProtocol blk ) =>
61
56
ProtocolInfo blk ->
@@ -70,112 +65,50 @@ getSecurityParameter = maxRollbacks . configSecurityParam . pInfoConfig
70
65
-- On mainnet, for a value minSliceSize = 2000, it will be used as the actual size of slices
71
66
-- until the size of delegations grows up to 8.6M, in which case, the size of slices
72
67
-- will be adjusted.
73
- getStakeSlice ::
74
- ConsensusProtocol (BlockProtocol blk ) =>
75
- ProtocolInfo blk ->
76
- Word64 ->
68
+ getSnapShot ::
77
69
ExtLedgerState CardanoBlock ->
78
- Bool ->
79
- StakeSliceRes
80
- getStakeSlice pInfo ! epochBlockNo els isMigration =
70
+ Maybe (Ledger. SnapShot StandardCrypto , EpochNo )
71
+ getSnapShot els =
81
72
case ledgerState els of
82
- LedgerStateByron _ -> NoSlices
83
- LedgerStateShelley sls -> genericStakeSlice pInfo epochBlockNo sls isMigration
84
- LedgerStateAllegra als -> genericStakeSlice pInfo epochBlockNo als isMigration
85
- LedgerStateMary mls -> genericStakeSlice pInfo epochBlockNo mls isMigration
86
- LedgerStateAlonzo als -> genericStakeSlice pInfo epochBlockNo als isMigration
87
- LedgerStateBabbage bls -> genericStakeSlice pInfo epochBlockNo bls isMigration
88
- LedgerStateConway cls -> genericStakeSlice pInfo epochBlockNo cls isMigration
89
-
90
- genericStakeSlice ::
91
- forall era c blk p .
92
- (c ~ StandardCrypto , EraCrypto era ~ c , ConsensusProtocol (BlockProtocol blk )) =>
93
- ProtocolInfo blk ->
94
- Word64 ->
73
+ LedgerStateByron _ -> Nothing
74
+ LedgerStateShelley sls -> Just $ genericSnapShot sls
75
+ LedgerStateAllegra als -> Just $ genericSnapShot als
76
+ LedgerStateMary mls -> Just $ genericSnapShot mls
77
+ LedgerStateAlonzo als -> Just $ genericSnapShot als
78
+ LedgerStateBabbage bls -> Just $ genericSnapShot bls
79
+ LedgerStateConway cls -> Just $ genericSnapShot cls
80
+
81
+ genericSnapShot ::
82
+ forall era p .
83
+ (EraCrypto era ~ StandardCrypto ) =>
95
84
LedgerState (ShelleyBlock p era ) ->
96
- Bool ->
97
- StakeSliceRes
98
- genericStakeSlice pInfo epochBlockNo lstate isMigration
99
- | index > delegationsLen = NoSlices
100
- | index == delegationsLen = Slice (emptySlice epoch) True
101
- | index + size > delegationsLen = Slice (mkSlice (delegationsLen - index)) True
102
- | otherwise = Slice (mkSlice size) False
85
+ (Ledger. SnapShot StandardCrypto , EpochNo )
86
+ genericSnapShot lstate = (stakeSnapshot, epoch)
103
87
where
104
- epoch :: EpochNo
105
- epoch = EpochNo $ 1 + unEpochNo (Shelley. nesEL (Consensus. shelleyLedgerState lstate))
106
-
107
- minSliceSize :: Word64
108
- minSliceSize = 2000
109
-
110
- -- On mainnet this is 2160
111
- k :: Word64
112
- k = getSecurityParameter pInfo
113
-
114
88
-- We use 'ssStakeMark' here. That means that when these values
115
89
-- are added to the database, the epoch number where they become active is the current
116
90
-- epoch plus one.
117
- stakeSnapshot :: Ledger. SnapShot c
91
+ stakeSnapshot :: Ledger. SnapShot StandardCrypto
118
92
stakeSnapshot =
119
93
Ledger. ssStakeMark . Shelley. esSnapshots . Shelley. nesEs $
120
94
Consensus. shelleyLedgerState lstate
121
95
122
- delegations :: VMap. KVVector VB VB (Credential 'Staking c , KeyHash 'StakePool c )
123
- delegations = VMap. unVMap $ Ledger. ssDelegations stakeSnapshot
124
-
125
- delegationsLen :: Word64
126
- delegationsLen = fromIntegral $ VG. length delegations
96
+ epoch = EpochNo $ 1 + unEpochNo (Shelley. nesEL (Consensus. shelleyLedgerState lstate))
127
97
128
- stakes :: VMap VB VP (Credential 'Staking c ) (Ledger. CompactForm Coin )
129
- stakes = Ledger. unStake $ Ledger. ssStake stakeSnapshot
98
+ snapShotToList ::
99
+ Ledger. SnapShot StandardCrypto ->
100
+ [(StakeCred , (Coin , PoolKeyHash ))]
101
+ snapShotToList snapShot =
102
+ VMap. toList $
103
+ VMap. mapMaybe id $ -- This line removes entries without stake. Should we assume 0 and insert it?
104
+ VMap. mapWithKey (\ a p -> (,p) <$> lookupStake a) (Ledger. ssDelegations snapShot)
105
+ where
106
+ stakes :: VMap VB VP StakeCred (Ledger. CompactForm Coin )
107
+ stakes = Ledger. unStake $ Ledger. ssStake snapShot
130
108
131
- lookupStake :: Credential 'Staking c -> Maybe Coin
109
+ lookupStake :: StakeCred -> Maybe Coin
132
110
lookupStake cred = Ledger. fromCompact <$> VMap. lookup cred stakes
133
111
134
- -- This is deterministic for the whole epoch and is the constant size of slices
135
- -- until the data are over. This means the last slice could be of smaller size and slices
136
- -- after that will be empty.
137
- epochSliceSize :: Word64
138
- epochSliceSize =
139
- max minSliceSize defaultEpochSliceSize
140
- where
141
- -- On mainnet this is 21600
142
- expectedBlocks :: Word64
143
- expectedBlocks = 10 * k
144
-
145
- -- This size of slices is enough to cover the whole list, even if only
146
- -- the 20% of the expected blocks appear in an epoch.
147
- defaultEpochSliceSize :: Word64
148
- defaultEpochSliceSize = 1 + div (delegationsLen * 5 ) expectedBlocks
149
-
150
- -- The starting index of the data in the delegation vector.
151
- index :: Word64
152
- index
153
- | isMigration = 0
154
- | epochBlockNo < k = delegationsLen + 1 -- so it creates the empty Slice.
155
- | otherwise = (epochBlockNo - k) * epochSliceSize
156
-
157
- size :: Word64
158
- size
159
- | isMigration, epochBlockNo + 1 < k = 0
160
- | isMigration = (epochBlockNo + 1 - k) * epochSliceSize
161
- | otherwise = epochSliceSize
162
-
163
- mkSlice :: Word64 -> StakeSlice
164
- mkSlice actualSize =
165
- StakeSlice
166
- { sliceEpochNo = epoch
167
- , sliceDistr = distribution
168
- }
169
- where
170
- delegationsSliced :: VMap VB VB (Credential 'Staking c ) (KeyHash 'StakePool c )
171
- delegationsSliced = VMap $ VG. slice (fromIntegral index) (fromIntegral actualSize) delegations
172
-
173
- distribution :: Map StakeCred (Coin , PoolKeyHash )
174
- distribution =
175
- VMap. toMap $
176
- VMap. mapMaybe id $
177
- VMap. mapWithKey (\ a p -> (,p) <$> lookupStake a) delegationsSliced
178
-
179
112
getPoolDistr ::
180
113
ExtLedgerState CardanoBlock ->
181
114
Maybe (Map PoolKeyHash (Coin , Word64 ), Map PoolKeyHash Natural )
0 commit comments