Skip to content

Commit 8b47beb

Browse files
sgillespiekderme
authored andcommitted
feature: Implement UTxO-HD
1 parent e49508c commit 8b47beb

File tree

22 files changed

+346
-191
lines changed

22 files changed

+346
-191
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ constraints:
7878
-- then clashes with the `show` in `Prelude`.
7979
, text < 2.1.2
8080

81-
, cardano-node ^>= 10.3
81+
, cardano-node ^>= 10.4
8282

8383
if impl (ghc >= 9.12)
8484
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 (ValuesMK)
2022
import qualified Ouroboros.Consensus.Ledger.Extended as Consensus
2123
import qualified Ouroboros.Network.AnchoredFragment as AF
2224
import Ouroboros.Network.Block
@@ -30,7 +32,7 @@ data Chain' block st
3032

3133
type State block = Consensus.ExtLedgerState block
3234

33-
type Chain block = Chain' block (State block)
35+
type Chain block = Chain' block (State block ValuesMK)
3436

3537
infixl 5 :>
3638

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

Lines changed: 34 additions & 8 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,14 @@ 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 Ouroboros.Consensus.Ledger.Tables.Utils (applyDiffs)
31+
import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol ()
2632
import Ouroboros.Network.Block (Tip (..))
2733

2834
-- | Thin layer around 'Chain' that knows how to apply blocks and maintain
@@ -41,7 +47,10 @@ instance Eq (Chain block) => Eq (ChainDB block) where
4147
instance Show (Chain block) => Show (ChainDB block) where
4248
show = show . cchain
4349

44-
initChainDB :: TopLevelConfig block -> State block -> ChainDB block
50+
initChainDB ::
51+
TopLevelConfig block ->
52+
State block ValuesMK ->
53+
ChainDB block
4554
initChainDB config st = ChainDB config (Genesis st)
4655

4756
headTip :: HasHeader block => ChainDB block -> Tip block
@@ -50,20 +59,37 @@ headTip chainDB =
5059
Genesis _ -> TipGenesis
5160
(_ :> (b, _)) -> Tip (blockSlot b) (blockHash b) (blockNo b)
5261

53-
currentState :: ChainDB block -> State block
62+
currentState :: ChainDB block -> State block ValuesMK
5463
currentState chainDB =
5564
case cchain chainDB of
5665
Genesis st -> st
5766
_ :> (_, st) -> st
5867

59-
replaceGenesisDB :: ChainDB block -> State block -> ChainDB block
68+
replaceGenesisDB ::
69+
ChainDB block ->
70+
State block ValuesMK ->
71+
ChainDB block
6072
replaceGenesisDB chainDB st = chainDB {cchain = Genesis st}
6173

62-
extendChainDB :: LedgerSupportsProtocol block => ChainDB block -> block -> ChainDB block
74+
extendChainDB ::
75+
LedgerSupportsProtocol block =>
76+
ChainDB block ->
77+
block ->
78+
ChainDB block
6379
extendChainDB chainDB blk = do
6480
let !chain = cchain chainDB
65-
!st = tickThenReapply ComputeLedgerEvents (Consensus.ExtLedgerCfg $ chainConfig chainDB) blk (getTipState chain)
66-
in chainDB {cchain = chain :> (blk, st)}
81+
-- Get the current ledger state
82+
!tipState = getTipState chain
83+
-- Apply the block and compute the diffs
84+
!diffState =
85+
tickThenReapply
86+
ComputeLedgerEvents
87+
(Consensus.ExtLedgerCfg $ chainConfig chainDB)
88+
blk
89+
tipState
90+
-- Apply the diffs
91+
!newTipState = applyDiffs tipState diffState
92+
in chainDB {cchain = chain :> (blk, newTipState)}
6793

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

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ 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.Basics (ValuesMK)
59+
import Ouroboros.Consensus.Ledger.Query (BlockQuery, BlockSupportsLedgerQuery, QueryFootprint (..), ShowQuery)
5960
import Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr, GenTx, TxId)
6061
import Ouroboros.Consensus.Ledger.SupportsProtocol (LedgerSupportsProtocol)
6162
import Ouroboros.Consensus.Network.NodeToClient (Apps (..), Codecs' (..), DefaultCodecs)
@@ -116,7 +117,7 @@ data ServerHandle m blk = ServerHandle
116117
, forkAgain :: m (Async ())
117118
}
118119

119-
replaceGenesis :: MonadSTM m => ServerHandle m blk -> State blk -> STM m ()
120+
replaceGenesis :: MonadSTM m => ServerHandle m blk -> State blk ValuesMK -> STM m ()
120121
replaceGenesis handle st =
121122
modifyTVar (chainProducerState handle) $ \cps ->
122123
cps {chainDB = replaceGenesisDB (chainDB cps) st}
@@ -125,12 +126,20 @@ readChain :: MonadSTM m => ServerHandle m blk -> STM m (Chain blk)
125126
readChain handle = do
126127
cchain . chainDB <$> readTVar (chainProducerState handle)
127128

128-
addBlock :: (LedgerSupportsProtocol blk, MonadSTM m) => ServerHandle m blk -> blk -> STM m ()
129+
addBlock ::
130+
(LedgerSupportsProtocol blk, MonadSTM m) =>
131+
ServerHandle m blk ->
132+
blk ->
133+
STM m ()
129134
addBlock handle blk =
130135
modifyTVar (chainProducerState handle) $
131136
addBlockState blk
132137

133-
rollback :: (LedgerSupportsProtocol blk, MonadSTM m) => ServerHandle m blk -> Point blk -> STM m ()
138+
rollback ::
139+
(LedgerSupportsProtocol blk, MonadSTM m) =>
140+
ServerHandle m blk ->
141+
Point blk ->
142+
STM m ()
134143
rollback handle point =
135144
modifyTVar (chainProducerState handle) $ \st ->
136145
case rollbackState point st of
@@ -153,7 +162,8 @@ stopServer sh = do
153162

154163
type MockServerConstraint blk =
155164
( SerialiseNodeToClientConstraints blk
156-
, ShowQuery (BlockQuery blk)
165+
, BlockSupportsLedgerQuery blk
166+
, ShowQuery (BlockQuery blk 'QFNoTables)
157167
, StandardHash blk
158168
, ShowProxy (ApplyTxErr blk)
159169
, Serialise (HeaderHash blk)
@@ -167,11 +177,10 @@ type MockServerConstraint blk =
167177
)
168178

169179
forkServerThread ::
170-
forall blk.
171180
MockServerConstraint blk =>
172181
IOManager ->
173182
TopLevelConfig blk ->
174-
State blk ->
183+
State blk ValuesMK ->
175184
NetworkMagic ->
176185
FilePath ->
177186
IO (ServerHandle IO blk)
@@ -183,11 +192,10 @@ forkServerThread iom config initSt netMagic path = do
183192
pure $ ServerHandle chainSt threadVar runThread
184193

185194
withServerHandle ::
186-
forall blk a.
187195
MockServerConstraint blk =>
188196
IOManager ->
189197
TopLevelConfig blk ->
190-
State blk ->
198+
State blk ValuesMK ->
191199
NetworkMagic ->
192200
FilePath ->
193201
(ServerHandle IO blk -> IO a) ->

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

Lines changed: 6 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 #-}
@@ -23,6 +24,7 @@ import qualified Data.Map.Strict as Map
2324
import Ouroboros.Consensus.Block (HasHeader, HeaderHash, Point, blockPoint, castPoint)
2425
import Ouroboros.Consensus.Config (TopLevelConfig)
2526
import Ouroboros.Consensus.Ledger.SupportsProtocol (LedgerSupportsProtocol)
27+
import Ouroboros.Consensus.Ledger.Tables (ValuesMK)
2628
import Ouroboros.Network.Block (ChainUpdate (..))
2729

2830
data ChainProducerState block = ChainProducerState
@@ -52,7 +54,10 @@ data FollowerNext
5254
| FollowerForwardFrom
5355
deriving (Eq, Show)
5456

55-
initChainProducerState :: TopLevelConfig block -> Chain.State block -> ChainProducerState block
57+
initChainProducerState ::
58+
TopLevelConfig block ->
59+
Chain.State block ValuesMK ->
60+
ChainProducerState block
5661
initChainProducerState config st = ChainProducerState (initChainDB config st) Map.empty 0
5762

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

cardano-chain-gen/src/Cardano/Mock/Forging/Interpreter.hs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,22 @@ import Ouroboros.Consensus.Cardano.Block (
8181
ShelleyEra,
8282
)
8383
import Ouroboros.Consensus.Cardano.CanHardFork ()
84+
import Ouroboros.Consensus.Cardano.Ledger ()
8485
import Ouroboros.Consensus.Config (
8586
TopLevelConfig,
8687
configConsensus,
8788
configLedger,
8889
topLevelConfigLedger,
8990
)
91+
import Ouroboros.Consensus.Shelley.Ledger.Ledger
9092

9193
import Ouroboros.Consensus.Forecast (Forecast (..))
9294
import qualified Ouroboros.Consensus.HardFork.Combinator.AcrossEras as Consensus
9395
import Ouroboros.Consensus.HardFork.Combinator.Ledger ()
9496
import qualified Ouroboros.Consensus.HardFork.Combinator.Mempool as Consensus
9597
import Ouroboros.Consensus.HeaderValidation (headerStateChainDep)
9698
import Ouroboros.Consensus.Ledger.Abstract (TickedLedgerState, applyChainTick)
97-
import Ouroboros.Consensus.Ledger.Basics (ComputeLedgerEvents (..))
99+
import Ouroboros.Consensus.Ledger.Basics (ComputeLedgerEvents (..), ValuesMK)
98100
import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState, headerState, ledgerState)
99101
import Ouroboros.Consensus.Ledger.SupportsMempool (
100102
ApplyTxErr,
@@ -104,6 +106,7 @@ import Ouroboros.Consensus.Ledger.SupportsMempool (
104106
applyTx,
105107
)
106108
import Ouroboros.Consensus.Ledger.SupportsProtocol (ledgerViewForecastAt)
109+
import Ouroboros.Consensus.Ledger.Tables.Utils (forgetLedgerTables)
107110
import Ouroboros.Consensus.Node.ProtocolInfo (
108111
ProtocolInfo,
109112
pInfoConfig,
@@ -118,7 +121,7 @@ import Ouroboros.Consensus.Protocol.Abstract (
118121
import Ouroboros.Consensus.Protocol.Praos ()
119122
import Ouroboros.Consensus.Protocol.TPraos ()
120123
import Ouroboros.Consensus.Shelley.HFEras ()
121-
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock, Ticked, shelleyLedgerState)
124+
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
122125
import qualified Ouroboros.Consensus.Shelley.Ledger.Mempool as Consensus
123126
import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol ()
124127
import qualified Ouroboros.Consensus.TypeFamilyWrappers as Consensus
@@ -232,13 +235,13 @@ initInterpreter ::
232235
initInterpreter pinfo forging traceForge mFingerprintFile = do
233236
let topLeverCfg = pInfoConfig pinfo
234237
let initSt = pInfoInitLedger pinfo
235-
let ledgerView = mkForecast topLeverCfg initSt
238+
let ledgerView' = mkForecast topLeverCfg initSt
236239
(mode, fingerprint) <- mkFingerprint mFingerprintFile
237240
stvar <-
238241
newTVarIO $
239242
InterpreterState
240243
{ istChain = initChainDB topLeverCfg initSt
241-
, istForecast = ledgerView
244+
, istForecast = ledgerView'
242245
, istSlot = SlotNo 0
243246
, -- The first real Byron block (ie block that can contain txs) is number 1.
244247
istNextBlockNo = BlockNo 1
@@ -360,20 +363,22 @@ forgeNextLeaders interpreter txes possibleLeaders = do
360363
else throwIO $ FailedToValidateSlot currentSlot (lengthSlots <$> istFingerprint interState) (interpFingerFile interpreter)
361364
Just (proof, blockForging) -> do
362365
-- Tick the ledger state for the 'SlotNo' we're producing a block for
363-
let tickedLedgerSt :: Ticked (LedgerState CardanoBlock)
364-
!tickedLedgerSt =
366+
let ledgerState' = ledgerState $ currentState (istChain interState)
367+
368+
tickedLedgerSt =
365369
applyChainTick
366370
ComputeLedgerEvents
367371
(configLedger cfg)
368372
currentSlot
369-
(ledgerState . currentState $ istChain interState)
373+
(forgetLedgerTables ledgerState')
374+
370375
!blk <-
371376
Block.forgeBlock
372377
blockForging
373378
cfg
374379
(istNextBlockNo interState)
375380
currentSlot
376-
tickedLedgerSt
381+
(forgetLedgerTables tickedLedgerSt)
377382
(mkValidated <$> txes)
378383
proof
379384

@@ -384,7 +389,7 @@ forgeNextLeaders interpreter txes possibleLeaders = do
384389
_applyTxs ::
385390
[Consensus.GenTx CardanoBlock] ->
386391
SlotNo ->
387-
TickedLedgerState CardanoBlock ->
392+
TickedLedgerState CardanoBlock ValuesMK ->
388393
Either (ApplyTxErr CardanoBlock) [Validated (GenTx CardanoBlock)]
389394
_applyTxs genTxs slotNo st =
390395
runExcept
@@ -405,7 +410,7 @@ tryAllForging interpreter interState currentSlot xs = do
405410
let cfg = interpTopLeverConfig interpreter
406411

407412
-- We require the ticked ledger view in order to construct the ticked 'ChainDepState'.
408-
ledgerView <- case runExcept (forecastFor (istForecast interState) currentSlot) of
413+
ledgerView' <- case runExcept (forecastFor (istForecast interState) currentSlot) of
409414
Right lv -> pure (lv :: (LedgerView (BlockProtocol CardanoBlock)))
410415
-- Left can only happen if we cross an epoch boundary
411416
Left err -> throwIO $ ForecastError currentSlot err
@@ -417,7 +422,7 @@ tryAllForging interpreter interState currentSlot xs = do
417422
!tickedChainDepState =
418423
tickChainDepState
419424
(configConsensus cfg)
420-
ledgerView
425+
ledgerView'
421426
currentSlot
422427
(headerStateChainDep (headerState $ currentState $ istChain interState))
423428

@@ -471,7 +476,7 @@ rollbackInterpreter interpreter pnt = do
471476
getCurrentInterpreterState :: Interpreter -> IO InterpreterState
472477
getCurrentInterpreterState = readTVarIO . interpState
473478

474-
getCurrentLedgerState :: Interpreter -> IO (ExtLedgerState CardanoBlock)
479+
getCurrentLedgerState :: Interpreter -> IO (ExtLedgerState CardanoBlock ValuesMK)
475480
getCurrentLedgerState = fmap (currentState . istChain) . getCurrentInterpreterState
476481

477482
getNextBlockNo :: Interpreter -> IO BlockNo
@@ -495,7 +500,7 @@ getCurrentSlot interp = istSlot <$> readTVarIO (interpState interp)
495500

496501
withBabbageLedgerState ::
497502
Interpreter ->
498-
(LedgerState (ShelleyBlock PraosStandard BabbageEra) -> Either ForgingError a) ->
503+
(LedgerState (ShelleyBlock PraosStandard BabbageEra) ValuesMK -> Either ForgingError a) ->
499504
IO a
500505
withBabbageLedgerState inter mk = do
501506
st <- getCurrentLedgerState inter
@@ -507,7 +512,7 @@ withBabbageLedgerState inter mk = do
507512

508513
withConwayLedgerState ::
509514
Interpreter ->
510-
(LedgerState (ShelleyBlock PraosStandard ConwayEra) -> Either ForgingError a) ->
515+
(LedgerState (ShelleyBlock PraosStandard ConwayEra) ValuesMK -> Either ForgingError a) ->
511516
IO a
512517
withConwayLedgerState inter mk = do
513518
st <- getCurrentLedgerState inter
@@ -519,7 +524,7 @@ withConwayLedgerState inter mk = do
519524

520525
withAlonzoLedgerState ::
521526
Interpreter ->
522-
(LedgerState (ShelleyBlock TPraosStandard AlonzoEra) -> Either ForgingError a) ->
527+
(LedgerState (ShelleyBlock TPraosStandard AlonzoEra) ValuesMK -> Either ForgingError a) ->
523528
IO a
524529
withAlonzoLedgerState inter mk = do
525530
st <- getCurrentLedgerState inter
@@ -531,7 +536,7 @@ withAlonzoLedgerState inter mk = do
531536

532537
withShelleyLedgerState ::
533538
Interpreter ->
534-
(LedgerState (ShelleyBlock TPraosStandard ShelleyEra) -> Either ForgingError a) ->
539+
(LedgerState (ShelleyBlock TPraosStandard ShelleyEra) ValuesMK -> Either ForgingError a) ->
535540
IO a
536541
withShelleyLedgerState inter mk = do
537542
st <- getCurrentLedgerState inter
@@ -623,9 +628,12 @@ mkValidated txe =
623628

624629
mkForecast ::
625630
TopLevelConfig CardanoBlock ->
626-
ExtLedgerState CardanoBlock ->
631+
ExtLedgerState CardanoBlock ValuesMK ->
627632
Forecast (LedgerView (BlockProtocol CardanoBlock))
628-
mkForecast cfg st = ledgerViewForecastAt (configLedger cfg) (ledgerState st)
633+
mkForecast cfg st = ledgerViewForecastAt (configLedger cfg) (ledgerState st')
634+
where
635+
st' :: ExtLedgerState CardanoBlock ValuesMK
636+
st' = st
629637

630638
throwLeftIO :: Exception e => Either e a -> IO a
631639
throwLeftIO = either throwIO pure

0 commit comments

Comments
 (0)