6
6
7
7
module Cardano.DbSync.LedgerState
8
8
( CardanoLedgerState (.. )
9
+ , EpochUpdate (.. )
9
10
, LedgerStateSnapshot (.. )
10
11
, LedgerStateVar (.. )
11
12
, applyBlock
@@ -29,7 +30,7 @@ import Cardano.Prelude
29
30
import Cardano.Slotting.EpochInfo (EpochInfo , epochInfoEpoch )
30
31
import Cardano.Slotting.Slot (EpochNo (.. ), SlotNo (.. ), fromWithOrigin )
31
32
32
- import Control.Concurrent.STM.TVar (TVar , newTVarIO , writeTVar , readTVar )
33
+ import Control.Concurrent.STM.TVar (TVar , newTVarIO , writeTVar , readTVar , readTVarIO )
33
34
import Control.Exception (IOException , handle )
34
35
import qualified Control.Exception as Exception
35
36
import Control.Monad.Extra (firstJustM )
@@ -54,6 +55,7 @@ import Ouroboros.Consensus.Shelley.Protocol (StandardShelley)
54
55
import Ouroboros.Consensus.Storage.Serialisation (DecodeDisk (.. ), EncodeDisk (.. ))
55
56
56
57
import qualified Shelley.Spec.Ledger.BaseTypes as Shelley
58
+ import qualified Shelley.Spec.Ledger.EpochBoundary as Shelley
57
59
import qualified Shelley.Spec.Ledger.LedgerState as Shelley
58
60
import qualified Shelley.Spec.Ledger.PParams as Shelley
59
61
@@ -66,6 +68,12 @@ data CardanoLedgerState = CardanoLedgerState
66
68
, clsCodec :: ! (CodecConfig CardanoBlock )
67
69
}
68
70
71
+ data EpochUpdate = EpochUpdate
72
+ { esParamUpdate :: ! (Shelley. PParams StandardShelley )
73
+ , esRewardUpdate :: ! (Shelley. RewardUpdate StandardShelley )
74
+ , esStakeUpdate :: ! (Shelley. Stake StandardShelley )
75
+ }
76
+
69
77
newtype LedgerStateVar = LedgerStateVar
70
78
{ unLedgerStateVar :: TVar CardanoLedgerState
71
79
}
@@ -77,10 +85,10 @@ data LedgerStateFile = LedgerStateFile -- Internal use only.
77
85
78
86
data LedgerStateSnapshot = LedgerStateSnapshot
79
87
{ lssState :: ! CardanoLedgerState
80
- , lssRewardUpdate :: ! (Maybe (Shelley. RewardUpdate StandardShelley ))
81
- , lssParamUpdate :: ! (Maybe (Shelley. PParams StandardShelley ))
88
+ , lssEpochUpdate :: ! (Maybe EpochUpdate ) -- Only Just for a single block at the epoch boundary
82
89
}
83
90
91
+
84
92
initLedgerStateVar :: GenesisConfig -> IO LedgerStateVar
85
93
initLedgerStateVar genesisConfig = do
86
94
LedgerStateVar <$>
@@ -108,19 +116,12 @@ applyBlock (LedgerStateVar stateVar) blk =
108
116
then do
109
117
let ! newState = oldState { clsState = applyBlk (clsConfig oldState) blk (clsState oldState) }
110
118
writeTVar stateVar newState
111
- let mRewards =
112
- case (ledgerRewardUpdate (clsState newState), ledgerRewardUpdate (clsState oldState)) of
113
- (Nothing , Just r) -> Just r
114
- _otherwise -> Nothing
115
- mParams =
116
- if ledgerEpochNo newState == ledgerEpochNo oldState + 1
117
- then ledgerEpochProtocolParams (clsState newState)
118
- else Nothing
119
-
120
119
pure $ LedgerStateSnapshot
121
120
{ lssState = newState
122
- , lssRewardUpdate = mRewards
123
- , lssParamUpdate = mParams
121
+ , lssEpochUpdate =
122
+ if ledgerEpochNo newState == ledgerEpochNo oldState + 1
123
+ then Just $ ledgerEpochUpdate (clsState newState) (ledgerRewardUpdate $ clsState oldState)
124
+ else Nothing
124
125
}
125
126
else panic $ mconcat
126
127
[ " applyBlock: Hash mismatch when applying block with slot no " , textShow (blockSlot blk), " \n "
@@ -179,7 +180,7 @@ cleanupLedgerStateFiles stateDir slotNo = do
179
180
-- Remove invalid (ie SlotNo >= current) ledger state files (occurs on rollback).
180
181
mapM_ safeRemoveFile invalid
181
182
-- Remove all but 8 most recent state files.
182
- mapM_ safeRemoveFile $ map lsfFilePath (List. drop 8 valid)
183
+ mapM_ ( safeRemoveFile . lsfFilePath) (List. drop 8 valid)
183
184
where
184
185
-- Left files are deleted, Right files are kept.
185
186
keepFile :: LedgerStateFile -> Either FilePath LedgerStateFile
@@ -246,8 +247,7 @@ listLedgerStateSlotNos :: LedgerStateDir -> IO [SlotNo]
246
247
listLedgerStateSlotNos = fmap3 (SlotNo . lsfSlotNo) listLedgerStateFilesOrdered
247
248
248
249
readLedgerState :: LedgerStateVar -> IO CardanoLedgerState
249
- readLedgerState (LedgerStateVar stateVar) =
250
- atomically $ readTVar stateVar
250
+ readLedgerState (LedgerStateVar stateVar) = readTVarIO stateVar
251
251
252
252
-- | Remove given file path and ignore any IOEXceptions.
253
253
safeRemoveFile :: FilePath -> IO ()
@@ -262,15 +262,30 @@ ledgerEpochNo cls =
262
262
epochInfo :: EpochInfo Identity
263
263
epochInfo = epochInfoLedger (clsConfig cls) $ hardForkLedgerStatePerEra (clsState cls)
264
264
265
- ledgerEpochProtocolParams :: LedgerState CardanoBlock -> Maybe (Shelley. PParams StandardShelley )
266
- ledgerEpochProtocolParams lsc =
267
- case lsc of
268
- LedgerStateByron _ -> Nothing
269
- LedgerStateShelley sls -> Just $ Shelley. esPp (Shelley. nesEs $ Consensus. shelleyLedgerState sls)
270
-
265
+ -- Create an EpochUpdate from the current epoch state and the rewards from the last epoch.
266
+ ledgerEpochUpdate :: LedgerState CardanoBlock -> Maybe (Shelley. RewardUpdate StandardShelley ) -> EpochUpdate
267
+ ledgerEpochUpdate lcs mRewards =
268
+ case lcs of
269
+ LedgerStateByron _ -> panic " ledgerEpochUpdate: LedgerStateByron but should be Shelley"
270
+ LedgerStateShelley sls ->
271
+ EpochUpdate
272
+ { esParamUpdate = Shelley. esPp $ Shelley. nesEs (Consensus. shelleyLedgerState sls)
273
+ , esRewardUpdate = fromMaybe Shelley. emptyRewardUpdate mRewards
274
+
275
+ -- Use '_pstakeSet' here instead of '_pstateMark' because the stake addresses for the
276
+ -- later may not have been added to the database yet. That means that whne these values
277
+ -- are added to the database, the epoch number where they become active is the current
278
+ -- epoch plus one.
279
+ , esStakeUpdate = Shelley. _stake . Shelley. _pstakeSet . Shelley. esSnapshots
280
+ $ Shelley. nesEs (Consensus. shelleyLedgerState sls)
281
+ }
282
+
283
+ -- This will return a 'Just' from the time the rewards are updated until the end of the
284
+ -- epoch. It is 'Nothing' for the first block of a new epoch (which is slightly inconvenient).
271
285
ledgerRewardUpdate :: LedgerState CardanoBlock -> Maybe (Shelley. RewardUpdate StandardShelley )
272
286
ledgerRewardUpdate lsc =
273
287
case lsc of
274
- LedgerStateByron _ -> Nothing
288
+ LedgerStateByron _ -> Nothing -- This actually happens on the Byron/Shelley boundary.
275
289
LedgerStateShelley sls -> Shelley. strictMaybeToMaybe . Shelley. nesRu
276
290
$ Consensus. shelleyLedgerState sls
291
+
0 commit comments