Skip to content

Commit 44eceb0

Browse files
authored
Merge pull request #1999 from IntersectMBO/utxo-hd
Utxo hd
2 parents 49f9fc2 + 49c5224 commit 44eceb0

File tree

25 files changed

+403
-224
lines changed

25 files changed

+403
-224
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ constraints:
7575
-- then clashes with the `show` in `Prelude`.
7676
, text < 2.1.2
7777

78-
, cardano-node ^>= 10.3
78+
, cardano-node ^>= 10.4
7979

8080
if impl (ghc >= 9.12)
8181
allow-newer:

cardano-chain-gen/src/Cardano/Mock/Chain.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DeriveFunctor #-}
2+
{-# LANGUAGE PartialTypeSignatures #-}
23
{-# LANGUAGE RankNTypes #-}
34
{-# LANGUAGE ScopedTypeVariables #-}
45

@@ -17,6 +18,7 @@ module Cardano.Mock.Chain (
1718
) where
1819

1920
import Ouroboros.Consensus.Block
21+
import Ouroboros.Consensus.Ledger.Basics (EmptyMK, ValuesMK)
2022
import qualified Ouroboros.Consensus.Ledger.Extended as Consensus
2123
import qualified Ouroboros.Network.AnchoredFragment as AF
2224
import Ouroboros.Network.Block
@@ -28,7 +30,7 @@ data Chain' block st
2830
| Chain' block st :> (block, st)
2931
deriving (Eq, Ord, Show, Functor)
3032

31-
type State block = Consensus.ExtLedgerState block
33+
type State block = (Consensus.ExtLedgerState block EmptyMK, Consensus.LedgerTables (Consensus.ExtLedgerState block) ValuesMK)
3234

3335
type Chain block = Chain' block (State block)
3436

cardano-chain-gen/src/Cardano/Mock/ChainDB.hs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
{-# LANGUAGE DerivingVia #-}
33
{-# LANGUAGE FlexibleContexts #-}
44
{-# LANGUAGE MonoLocalBinds #-}
5+
{-# LANGUAGE RankNTypes #-}
6+
{-# LANGUAGE ScopedTypeVariables #-}
57
{-# LANGUAGE UndecidableInstances #-}
68

79
module 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

2022
import Cardano.Mock.Chain
2123
import Ouroboros.Consensus.Block
24+
import Ouroboros.Consensus.Cardano.CanHardFork ()
25+
import Ouroboros.Consensus.Cardano.Ledger ()
2226
import Ouroboros.Consensus.Config
2327
import Ouroboros.Consensus.Ledger.Abstract
2428
import 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 ()
2633
import 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
4148
instance 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
4555
initChainDB config st = ChainDB config (Genesis st)
4656

4757
headTip :: 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
6073
replaceGenesisDB 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
6381
extendChainDB 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

68106
findFirstPoint :: HasHeader block => [Point block] -> ChainDB block -> Maybe (Point block)
69107
findFirstPoint points chainDB = findFirstPointChain points (cchain chainDB)

cardano-chain-gen/src/Cardano/Mock/ChainSync/Server.hs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Network.TypedProtocol.Stateful.Codec ()
5555
import qualified Network.TypedProtocol.Stateful.Peer as St
5656
import Ouroboros.Consensus.Block (CodecConfig, HasHeader, Point, StandardHash, castPoint)
5757
import Ouroboros.Consensus.Config (TopLevelConfig, configCodec)
58-
import Ouroboros.Consensus.Ledger.Query (BlockQuery, ShowQuery)
58+
import Ouroboros.Consensus.Ledger.Query (BlockQuery, BlockSupportsLedgerQuery, QueryFootprint (..), ShowQuery)
5959
import Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr, GenTx, TxId)
6060
import Ouroboros.Consensus.Ledger.SupportsProtocol (LedgerSupportsProtocol)
6161
import Ouroboros.Consensus.Network.NodeToClient (Apps (..), Codecs' (..), DefaultCodecs)
@@ -125,12 +125,20 @@ readChain :: MonadSTM m => ServerHandle m blk -> STM m (Chain blk)
125125
readChain handle = do
126126
cchain . chainDB <$> readTVar (chainProducerState handle)
127127

128-
addBlock :: (LedgerSupportsProtocol blk, MonadSTM m) => ServerHandle m blk -> blk -> STM m ()
128+
addBlock ::
129+
(LedgerSupportsProtocol blk, MonadSTM m) =>
130+
ServerHandle m blk ->
131+
blk ->
132+
STM m ()
129133
addBlock handle blk =
130134
modifyTVar (chainProducerState handle) $
131135
addBlockState blk
132136

133-
rollback :: (LedgerSupportsProtocol blk, MonadSTM m) => ServerHandle m blk -> Point blk -> STM m ()
137+
rollback ::
138+
(LedgerSupportsProtocol blk, MonadSTM m) =>
139+
ServerHandle m blk ->
140+
Point blk ->
141+
STM m ()
134142
rollback handle point =
135143
modifyTVar (chainProducerState handle) $ \st ->
136144
case rollbackState point st of
@@ -153,7 +161,8 @@ stopServer sh = do
153161

154162
type MockServerConstraint blk =
155163
( SerialiseNodeToClientConstraints blk
156-
, ShowQuery (BlockQuery blk)
164+
, BlockSupportsLedgerQuery blk
165+
, ShowQuery (BlockQuery blk 'QFNoTables)
157166
, StandardHash blk
158167
, ShowProxy (ApplyTxErr blk)
159168
, Serialise (HeaderHash blk)
@@ -167,7 +176,6 @@ type MockServerConstraint blk =
167176
)
168177

169178
forkServerThread ::
170-
forall blk.
171179
MockServerConstraint blk =>
172180
IOManager ->
173181
TopLevelConfig blk ->
@@ -183,7 +191,6 @@ forkServerThread iom config initSt netMagic path = do
183191
pure $ ServerHandle chainSt threadVar runThread
184192

185193
withServerHandle ::
186-
forall blk a.
187194
MockServerConstraint blk =>
188195
IOManager ->
189196
TopLevelConfig blk ->

cardano-chain-gen/src/Cardano/Mock/ChainSync/State.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE FlexibleContexts #-}
12
{-# LANGUAGE NamedFieldPuns #-}
23
{-# LANGUAGE TypeFamilies #-}
34
{-# LANGUAGE TypeOperators #-}
@@ -52,7 +53,10 @@ data FollowerNext
5253
| FollowerForwardFrom
5354
deriving (Eq, Show)
5455

55-
initChainProducerState :: TopLevelConfig block -> Chain.State block -> ChainProducerState block
56+
initChainProducerState ::
57+
TopLevelConfig block ->
58+
Chain.State block ->
59+
ChainProducerState block
5660
initChainProducerState config st = ChainProducerState (initChainDB config st) Map.empty 0
5761

5862
-- | Add a block to the chain. It does not require any follower's state changes.

0 commit comments

Comments
 (0)