@@ -13,6 +13,7 @@ module Cardano.DbSync.Era.Shelley.Generic.StakeDist (
1313 getSecurityParameter ,
1414 getStakeSlice ,
1515 countEpochStake ,
16+ fullEpochStake ,
1617 getPoolDistr ,
1718) where
1819
@@ -216,6 +217,70 @@ genericCountEpochStake lstate =
216217 hasStake :: Credential 'Staking -> Bool
217218 hasStake cred = isJust (VMap. lookup cred stakes)
218219
220+ fullEpochStake ::
221+ ExtLedgerState CardanoBlock mk ->
222+ StakeSliceRes
223+ fullEpochStake els =
224+ case ledgerState els of
225+ LedgerStateByron _ -> NoSlices
226+ LedgerStateShelley sls -> genericFullStakeSlice sls
227+ LedgerStateAllegra als -> genericFullStakeSlice als
228+ LedgerStateMary mls -> genericFullStakeSlice mls
229+ LedgerStateAlonzo als -> genericFullStakeSlice als
230+ LedgerStateBabbage bls -> genericFullStakeSlice bls
231+ LedgerStateConway cls -> genericFullStakeSlice cls
232+ LedgerStateDijkstra dls -> genericFullStakeSlice dls
233+
234+ genericFullStakeSlice ::
235+ forall era p mk .
236+ LedgerState (ShelleyBlock p era ) mk ->
237+ StakeSliceRes
238+ genericFullStakeSlice lstate =
239+ Slice stakeSlice True
240+ where
241+ epoch :: EpochNo
242+ epoch = EpochNo $ 1 + unEpochNo (Shelley. nesEL (Consensus. shelleyLedgerState lstate))
243+
244+ -- We use 'ssStakeMark' here. That means that when these values
245+ -- are added to the database, the epoch number where they become active is the current
246+ -- epoch plus one.
247+ stakeSnapshot :: Ledger. SnapShot
248+ stakeSnapshot =
249+ Ledger. ssStakeMark . Shelley. esSnapshots . Shelley. nesEs $
250+ Consensus. shelleyLedgerState lstate
251+
252+ delegations :: VMap. KVVector VB VB (Credential 'Staking, KeyHash 'StakePool)
253+ delegations = VMap. unVMap $ Ledger. ssDelegations stakeSnapshot
254+
255+ delegationsLen :: Word64
256+ delegationsLen = fromIntegral $ VG. length delegations
257+
258+ stakes :: VMap VB VP (Credential 'Staking) (Ledger. CompactForm Coin )
259+ stakes = Ledger. unStake $ Ledger. ssStake stakeSnapshot
260+
261+ lookupStake :: Credential 'Staking -> Maybe Coin
262+ lookupStake cred = Ledger. fromCompact <$> VMap. lookup cred stakes
263+
264+ -- The starting index of the data in the delegation vector.
265+ index :: Word64
266+ index = 0
267+
268+ stakeSlice :: StakeSlice
269+ stakeSlice =
270+ StakeSlice
271+ { sliceEpochNo = epoch
272+ , sliceDistr = distribution
273+ }
274+ where
275+ delegationsSliced :: VMap VB VB (Credential 'Staking) (KeyHash 'StakePool)
276+ delegationsSliced = VMap $ VG. slice (fromIntegral index) (fromIntegral delegationsLen) delegations
277+
278+ distribution :: Map StakeCred (Coin , PoolKeyHash )
279+ distribution =
280+ VMap. toMap $
281+ VMap. mapMaybe id $
282+ VMap. mapWithKey (\ a p -> (,p) <$> lookupStake a) delegationsSliced
283+
219284getPoolDistr ::
220285 ExtLedgerState CardanoBlock mk ->
221286 Maybe (Map PoolKeyHash (Coin , Word64 ), Map PoolKeyHash Natural )
0 commit comments