5
5
{-# LANGUAGE RankNTypes #-}
6
6
{-# LANGUAGE ScopedTypeVariables #-}
7
7
{-# LANGUAGE TypeFamilies #-}
8
+ {-# LANGUAGE TypeOperators #-}
8
9
{-# LANGUAGE NoImplicitPrelude #-}
9
10
10
11
module Cardano.DbSync.Ledger.Types where
@@ -28,6 +29,7 @@ import Cardano.Ledger.Coin (Coin)
28
29
import Cardano.Ledger.Conway.Governance
29
30
import Cardano.Ledger.Credential (Credential (.. ))
30
31
import Cardano.Ledger.Keys (KeyRole (.. ))
32
+ import Cardano.Ledger.Shelley.LedgerState (NewEpochState ())
31
33
import Cardano.Prelude hiding (atomically )
32
34
import Cardano.Slotting.Slot (
33
35
EpochNo (.. ),
@@ -39,13 +41,17 @@ import Control.Concurrent.Class.MonadSTM.Strict (
39
41
)
40
42
import Control.Concurrent.STM.TBQueue (TBQueue )
41
43
import qualified Data.Map.Strict as Map
44
+ import Data.SOP.Strict
42
45
import qualified Data.Set as Set
43
46
import qualified Data.Strict.Maybe as Strict
47
+ import Lens.Micro (Traversal' )
44
48
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemStart (.. ))
45
- import Ouroboros.Consensus.Cardano.Block (StandardConway , StandardCrypto )
49
+ import Ouroboros.Consensus.Cardano.Block hiding (CardanoBlock , CardanoLedgerState )
50
+ import Ouroboros.Consensus.HardFork.Combinator.Basics (LedgerState (.. ))
46
51
import Ouroboros.Consensus.Ledger.Abstract (getTipSlot )
47
52
import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState (.. ))
48
53
import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus
54
+ import Ouroboros.Consensus.Shelley.Ledger (LedgerState (.. ), ShelleyBlock )
49
55
import Ouroboros.Network.AnchoredSeq (Anchorable (.. ), AnchoredSeq (.. ))
50
56
import Prelude (fail , id )
51
57
@@ -190,3 +196,61 @@ instance Anchorable (WithOrigin SlotNo) CardanoLedgerState CardanoLedgerState wh
190
196
getAnchorMeasure _ = getTipSlot . clsState
191
197
192
198
data SnapshotPoint = OnDisk LedgerStateFile | InMemory CardanoPoint
199
+
200
+ -- | Per-era pure getters and setters on @NewEpochState@. Note this is a bit of an abuse
201
+ -- of the cardano-ledger/ouroboros-consensus public APIs, because ledger state is not
202
+ -- designed to be updated this way. We are only replaying the chain, so this should be
203
+ -- safe.
204
+ class HasNewEpochState era where
205
+ getNewEpochState :: ExtLedgerState CardanoBlock -> Maybe (NewEpochState era )
206
+
207
+ applyNewEpochState ::
208
+ NewEpochState era ->
209
+ ExtLedgerState CardanoBlock ->
210
+ ExtLedgerState CardanoBlock
211
+
212
+ instance HasNewEpochState StandardShelley where
213
+ getNewEpochState st = case ledgerState st of
214
+ LedgerStateShelley shelley -> Just (shelleyLedgerState shelley)
215
+ _ -> Nothing
216
+
217
+ applyNewEpochState st =
218
+ hApplyExtLedgerState $
219
+ fn (applyNewEpochState' st) :* fn id :* fn id :* fn id :* fn id :* fn id :* Nil
220
+
221
+ instance HasNewEpochState StandardConway where
222
+ getNewEpochState st = case ledgerState st of
223
+ LedgerStateConway shelley -> Just (shelleyLedgerState shelley)
224
+ _ -> Nothing
225
+
226
+ applyNewEpochState st =
227
+ hApplyExtLedgerState $
228
+ fn id :* fn id :* fn id :* fn id :* fn id :* fn (applyNewEpochState' st) :* Nil
229
+
230
+ hApplyExtLedgerState ::
231
+ NP (LedgerState -.-> LedgerState ) (CardanoShelleyEras StandardCrypto ) ->
232
+ ExtLedgerState CardanoBlock ->
233
+ ExtLedgerState CardanoBlock
234
+ hApplyExtLedgerState f ledger =
235
+ case ledgerState ledger of
236
+ HardForkLedgerState hfState ->
237
+ let newHfState = hap (fn id :* f) hfState
238
+ in updateLedgerState $ HardForkLedgerState newHfState
239
+ where
240
+ updateLedgerState st = ledger {ledgerState = st}
241
+
242
+ applyNewEpochState' ::
243
+ NewEpochState era ->
244
+ LedgerState (ShelleyBlock proto era ) ->
245
+ LedgerState (ShelleyBlock proto era )
246
+ applyNewEpochState' newEpochState' ledger =
247
+ ledger {shelleyLedgerState = newEpochState'}
248
+
249
+ -- | A @Traversal@ that targets the @NewEpochState@ from the extended ledger state
250
+ newEpochStateT ::
251
+ HasNewEpochState era =>
252
+ Traversal' (ExtLedgerState CardanoBlock ) (NewEpochState era )
253
+ newEpochStateT f ledger =
254
+ case getNewEpochState ledger of
255
+ Just newEpochState' -> flip applyNewEpochState ledger <$> f newEpochState'
256
+ Nothing -> pure ledger
0 commit comments