Skip to content

Commit 52e9e6e

Browse files
committed
Fix missing epoch_stake
Fixes #1678
1 parent aaad29d commit 52e9e6e

File tree

3 files changed

+99
-25
lines changed

3 files changed

+99
-25
lines changed

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/StakeDist.hs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
219284
getPoolDistr ::
220285
ExtLedgerState CardanoBlock mk ->
221286
Maybe (Map PoolKeyHash (Coin, Word64), Map PoolKeyHash Natural)

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Insert/LedgerEvent.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ insertNewEpochLedgerEvents syncEnv applyRes currentEpochNo@(EpochNo curEpoch) =
7474
handler ev =
7575
case ev of
7676
LedgerNewEpoch en ss -> do
77-
validateEpochStake tracer applyRes
77+
validateEpochStake syncEnv applyRes
7878
databaseCacheSize <- lift DB.queryStatementCacheSize
7979
liftIO . logInfo tracer $ "Database Statement Cache size is " <> textShow databaseCacheSize
8080
currentTime <- liftIO getCurrentTime

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Validate.hs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,42 @@ import Cardano.DbSync.Ledger.Event
2727
import Cardano.DbSync.Types
2828
import Cardano.DbSync.Ledger.Types
2929
import qualified Data.Strict.Maybe as Strict
30+
import Cardano.DbSync.Api.Types
31+
import Cardano.DbSync.Api
32+
import Cardano.DbSync.Era.Universal.Epoch
33+
import Cardano.DbSync.Util.Constraint
3034

3135
validateEpochStake ::
32-
Trace IO Text ->
36+
SyncEnv ->
3337
ApplyResult -> ExceptT SyncNodeError DB.DbM ()
34-
validateEpochStake tracer applyRes = case apOldLedger applyRes of
35-
Strict.Just lstate | Just (expectedCount, epoch) <- Generic.countEpochStake (clsState lstate) -> do
36-
actualCount <- lift $ DB.queryNormalEpochStakeCount (unEpochNo epoch)
37-
if actualCount /= expectedCount then
38-
liftIO
39-
. logWarning tracer
40-
$ mconcat
41-
[ "validateEpochStake: epoch stake in epoch "
42-
, textShow (unEpochNo epoch)
43-
, " expected total of "
44-
, textShow expectedCount
45-
, " but got "
46-
, textShow actualCount
47-
]
48-
else
49-
liftIO $ logInfo tracer
50-
$ mconcat
51-
[ "Validate Epoch Stake: total entries in epoch "
52-
, textShow (unEpochNo epoch)
53-
, " are "
54-
, textShow actualCount
55-
]
56-
_ -> pure ()
38+
validateEpochStake syncEnv applyRes = case apOldLedger applyRes of
39+
Strict.Just lstate | Just (expectedCount, epoch) <- Generic.countEpochStake (clsState lstate) -> do
40+
actualCount <- lift $ DB.queryNormalEpochStakeCount (unEpochNo epoch)
41+
if actualCount /= expectedCount then do
42+
liftIO
43+
. logWarning tracer
44+
$ mconcat
45+
[ "validateEpochStake: epoch stake in epoch "
46+
, textShow (unEpochNo epoch)
47+
, " expected total of "
48+
, textShow expectedCount
49+
, " but got "
50+
, textShow actualCount
51+
]
52+
let slice = Generic.fullEpochStake (clsState lstate)
53+
addStakeConstraintsIfNotExist syncEnv tracer
54+
insertStakeSlice syncEnv slice
55+
else
56+
liftIO $ logInfo tracer
57+
$ mconcat
58+
[ "Validate Epoch Stake: total entries in epoch "
59+
, textShow (unEpochNo epoch)
60+
, " are "
61+
, textShow actualCount
62+
]
63+
_ -> pure ()
64+
where
65+
tracer = getTrace syncEnv
5766

5867

5968
validateEpochRewards ::

0 commit comments

Comments
 (0)