Skip to content
Open

Utxo hd #1999

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
matrix:
os: [ubuntu-latest]
# TODO: Add ghc910 when input-output-hk/devx is fixed
compiler-nix-name: [ghc810, ghc96, ghc98, ghc912]
compiler-nix-name: [ghc96, ghc98, ghc910, ghc912]
include:
# We want a single job, because macOS runners are scarce.
- os: macos-latest
Expand Down
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ constraints:
-- then clashes with the `show` in `Prelude`.
, text < 2.1.2

, cardano-node ^>= 10.3
, cardano-node ^>= 10.4

if impl (ghc >= 9.12)
allow-newer:
Expand Down
4 changes: 3 additions & 1 deletion cardano-chain-gen/src/Cardano/Mock/Chain.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

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

import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Ledger.Basics (EmptyMK, ValuesMK)
import qualified Ouroboros.Consensus.Ledger.Extended as Consensus
import qualified Ouroboros.Network.AnchoredFragment as AF
import Ouroboros.Network.Block
Expand All @@ -28,7 +30,7 @@ data Chain' block st
| Chain' block st :> (block, st)
deriving (Eq, Ord, Show, Functor)

type State block = Consensus.ExtLedgerState block
type State block = (Consensus.ExtLedgerState block EmptyMK, Consensus.LedgerTables (Consensus.ExtLedgerState block) ValuesMK)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the outer parentheses necessary?


type Chain block = Chain' block (State block)

Expand Down
52 changes: 45 additions & 7 deletions cardano-chain-gen/src/Cardano/Mock/ChainDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}

module Cardano.Mock.ChainDB (
ChainDB (..),
currentState,
initChainDB,
headTip,
currentState,
replaceGenesisDB,
extendChainDB,
findFirstPoint,
Expand All @@ -19,10 +21,15 @@ module Cardano.Mock.ChainDB (

import Cardano.Mock.Chain
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Cardano.CanHardFork ()
import Ouroboros.Consensus.Cardano.Ledger ()
import Ouroboros.Consensus.Config
import Ouroboros.Consensus.Ledger.Abstract
import qualified Ouroboros.Consensus.Ledger.Extended as Consensus
import Ouroboros.Consensus.Ledger.SupportsProtocol
import Ouroboros.Consensus.Ledger.SupportsProtocol (LedgerSupportsProtocol)
import qualified Ouroboros.Consensus.Ledger.Tables as Consensus
import Ouroboros.Consensus.Ledger.Tables.Utils (applyDiffsMK, forgetLedgerTables, restrictValuesMK)
import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol ()
import Ouroboros.Network.Block (Tip (..))

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

initChainDB :: TopLevelConfig block -> State block -> ChainDB block
initChainDB ::
TopLevelConfig block ->
State block ->
ChainDB block
initChainDB config st = ChainDB config (Genesis st)

headTip :: HasHeader block => ChainDB block -> Tip block
Expand All @@ -56,14 +66,42 @@ currentState chainDB =
Genesis st -> st
_ :> (_, st) -> st

replaceGenesisDB :: ChainDB block -> State block -> ChainDB block
replaceGenesisDB ::
ChainDB block ->
State block ->
ChainDB block
replaceGenesisDB chainDB st = chainDB {cchain = Genesis st}

extendChainDB :: LedgerSupportsProtocol block => ChainDB block -> block -> ChainDB block
extendChainDB ::
forall block.
LedgerSupportsProtocol block =>
ChainDB block ->
block ->
ChainDB block
extendChainDB chainDB blk = do
let !chain = cchain chainDB
!st = tickThenReapply ComputeLedgerEvents (Consensus.ExtLedgerCfg $ chainConfig chainDB) blk (getTipState chain)
in chainDB {cchain = chain :> (blk, st)}
-- Get the current ledger state
(tipState, tables) = getTipState chain
-- Apply the block and compute the diffs
keys :: LedgerTables (Consensus.ExtLedgerState block) KeysMK
keys = getBlockKeySets blk
ledgerTables = Consensus.getLedgerTables tables
restrictedTables = restrictValuesMK ledgerTables (Consensus.getLedgerTables keys)
ledgerState = Consensus.withLedgerTables tipState (Consensus.LedgerTables restrictedTables)
!diffState =
tickThenReapply
ComputeLedgerEvents
(Consensus.ExtLedgerCfg $ chainConfig chainDB)
blk
ledgerState
!ledgerTables' =
Consensus.LedgerTables
. applyDiffsMK ledgerTables
. Consensus.getLedgerTables
. Consensus.projectLedgerTables
$ diffState
!ledgerState' = forgetLedgerTables diffState
in chainDB {cchain = chain :> (blk, (ledgerState', ledgerTables'))}

findFirstPoint :: HasHeader block => [Point block] -> ChainDB block -> Maybe (Point block)
findFirstPoint points chainDB = findFirstPointChain points (cchain chainDB)
Expand Down
19 changes: 13 additions & 6 deletions cardano-chain-gen/src/Cardano/Mock/ChainSync/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import Network.TypedProtocol.Stateful.Codec ()
import qualified Network.TypedProtocol.Stateful.Peer as St
import Ouroboros.Consensus.Block (CodecConfig, HasHeader, Point, StandardHash, castPoint)
import Ouroboros.Consensus.Config (TopLevelConfig, configCodec)
import Ouroboros.Consensus.Ledger.Query (BlockQuery, ShowQuery)
import Ouroboros.Consensus.Ledger.Query (BlockQuery, BlockSupportsLedgerQuery, QueryFootprint (..), ShowQuery)
import Ouroboros.Consensus.Ledger.SupportsMempool (ApplyTxErr, GenTx, TxId)
import Ouroboros.Consensus.Ledger.SupportsProtocol (LedgerSupportsProtocol)
import Ouroboros.Consensus.Network.NodeToClient (Apps (..), Codecs' (..), DefaultCodecs)
Expand Down Expand Up @@ -125,12 +125,20 @@ readChain :: MonadSTM m => ServerHandle m blk -> STM m (Chain blk)
readChain handle = do
cchain . chainDB <$> readTVar (chainProducerState handle)

addBlock :: (LedgerSupportsProtocol blk, MonadSTM m) => ServerHandle m blk -> blk -> STM m ()
addBlock ::
(LedgerSupportsProtocol blk, MonadSTM m) =>
ServerHandle m blk ->
blk ->
STM m ()
addBlock handle blk =
modifyTVar (chainProducerState handle) $
addBlockState blk

rollback :: (LedgerSupportsProtocol blk, MonadSTM m) => ServerHandle m blk -> Point blk -> STM m ()
rollback ::
(LedgerSupportsProtocol blk, MonadSTM m) =>
ServerHandle m blk ->
Point blk ->
STM m ()
rollback handle point =
modifyTVar (chainProducerState handle) $ \st ->
case rollbackState point st of
Expand All @@ -153,7 +161,8 @@ stopServer sh = do

type MockServerConstraint blk =
( SerialiseNodeToClientConstraints blk
, ShowQuery (BlockQuery blk)
, BlockSupportsLedgerQuery blk
, ShowQuery (BlockQuery blk 'QFNoTables)
, StandardHash blk
, ShowProxy (ApplyTxErr blk)
, Serialise (HeaderHash blk)
Expand All @@ -167,7 +176,6 @@ type MockServerConstraint blk =
)

forkServerThread ::
forall blk.
MockServerConstraint blk =>
IOManager ->
TopLevelConfig blk ->
Expand All @@ -183,7 +191,6 @@ forkServerThread iom config initSt netMagic path = do
pure $ ServerHandle chainSt threadVar runThread

withServerHandle ::
forall blk a.
MockServerConstraint blk =>
IOManager ->
TopLevelConfig blk ->
Expand Down
6 changes: 5 additions & 1 deletion cardano-chain-gen/src/Cardano/Mock/ChainSync/State.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
Expand Down Expand Up @@ -52,7 +53,10 @@ data FollowerNext
| FollowerForwardFrom
deriving (Eq, Show)

initChainProducerState :: TopLevelConfig block -> Chain.State block -> ChainProducerState block
initChainProducerState ::
TopLevelConfig block ->
Chain.State block ->
ChainProducerState block
initChainProducerState config st = ChainProducerState (initChainDB config st) Map.empty 0

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