22{-# LANGUAGE DerivingVia #-}
33{-# LANGUAGE FlexibleContexts #-}
44{-# LANGUAGE MonoLocalBinds #-}
5+ {-# LANGUAGE RankNTypes #-}
6+ {-# LANGUAGE ScopedTypeVariables #-}
57{-# LANGUAGE UndecidableInstances #-}
68
79module Cardano.Mock.ChainDB (
810 ChainDB (.. ),
11+ currentState ,
912 initChainDB ,
1013 headTip ,
11- currentState ,
1214 replaceGenesisDB ,
1315 extendChainDB ,
1416 findFirstPoint ,
@@ -19,10 +21,15 @@ module Cardano.Mock.ChainDB (
1921
2022import Cardano.Mock.Chain
2123import Ouroboros.Consensus.Block
24+ import Ouroboros.Consensus.Cardano.CanHardFork ()
25+ import Ouroboros.Consensus.Cardano.Ledger ()
2226import Ouroboros.Consensus.Config
2327import Ouroboros.Consensus.Ledger.Abstract
2428import qualified Ouroboros.Consensus.Ledger.Extended as Consensus
25- import Ouroboros.Consensus.Ledger.SupportsProtocol
29+ import Ouroboros.Consensus.Ledger.SupportsProtocol (LedgerSupportsProtocol )
30+ import qualified Ouroboros.Consensus.Ledger.Tables as Consensus
31+ import Ouroboros.Consensus.Ledger.Tables.Utils (applyDiffsMK , forgetLedgerTables , restrictValuesMK )
32+ import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol ()
2633import Ouroboros.Network.Block (Tip (.. ))
2734
2835-- | Thin layer around 'Chain' that knows how to apply blocks and maintain
@@ -41,7 +48,10 @@ instance Eq (Chain block) => Eq (ChainDB block) where
4148instance Show (Chain block ) => Show (ChainDB block ) where
4249 show = show . cchain
4350
44- initChainDB :: TopLevelConfig block -> State block -> ChainDB block
51+ initChainDB ::
52+ TopLevelConfig block ->
53+ State block ->
54+ ChainDB block
4555initChainDB config st = ChainDB config (Genesis st)
4656
4757headTip :: HasHeader block => ChainDB block -> Tip block
@@ -56,14 +66,42 @@ currentState chainDB =
5666 Genesis st -> st
5767 _ :> (_, st) -> st
5868
59- replaceGenesisDB :: ChainDB block -> State block -> ChainDB block
69+ replaceGenesisDB ::
70+ ChainDB block ->
71+ State block ->
72+ ChainDB block
6073replaceGenesisDB chainDB st = chainDB {cchain = Genesis st}
6174
62- extendChainDB :: LedgerSupportsProtocol block => ChainDB block -> block -> ChainDB block
75+ extendChainDB ::
76+ forall block .
77+ LedgerSupportsProtocol block =>
78+ ChainDB block ->
79+ block ->
80+ ChainDB block
6381extendChainDB chainDB blk = do
6482 let ! chain = cchain chainDB
65- ! st = tickThenReapply ComputeLedgerEvents (Consensus. ExtLedgerCfg $ chainConfig chainDB) blk (getTipState chain)
66- in chainDB {cchain = chain :> (blk, st)}
83+ -- Get the current ledger state
84+ (tipState, tables) = getTipState chain
85+ -- Apply the block and compute the diffs
86+ keys :: LedgerTables (Consensus. ExtLedgerState block ) KeysMK
87+ keys = getBlockKeySets blk
88+ ledgerTables = Consensus. getLedgerTables tables
89+ restrictedTables = restrictValuesMK ledgerTables (Consensus. getLedgerTables keys)
90+ ledgerState = Consensus. withLedgerTables tipState (Consensus. LedgerTables restrictedTables)
91+ ! diffState =
92+ tickThenReapply
93+ ComputeLedgerEvents
94+ (Consensus. ExtLedgerCfg $ chainConfig chainDB)
95+ blk
96+ ledgerState
97+ ! ledgerTables' =
98+ Consensus. LedgerTables
99+ . applyDiffsMK ledgerTables
100+ . Consensus. getLedgerTables
101+ . Consensus. projectLedgerTables
102+ $ diffState
103+ ! ledgerState' = forgetLedgerTables diffState
104+ in chainDB {cchain = chain :> (blk, (ledgerState', ledgerTables'))}
67105
68106findFirstPoint :: HasHeader block => [Point block ] -> ChainDB block -> Maybe (Point block )
69107findFirstPoint points chainDB = findFirstPointChain points (cchain chainDB)
0 commit comments