diff --git a/ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/Consensus/Byron/Examples.hs b/ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/Consensus/Byron/Examples.hs index 9626d98aa9..1176d0f5b3 100644 --- a/ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/Consensus/Byron/Examples.hs +++ b/ouroboros-consensus-cardano/src/unstable-byron-testlib/Test/Consensus/Byron/Examples.hs @@ -122,7 +122,7 @@ exampleBlock = cfg (BlockNo 1) (SlotNo 1) - (applyChainTick ledgerConfig (SlotNo 1) ledgerStateAfterEBB) + (applyChainTick OmitLedgerEvents ledgerConfig (SlotNo 1) ledgerStateAfterEBB) [ValidatedByronTx exampleGenTx] (fakeMkIsLeader leaderCredentials) where @@ -180,14 +180,14 @@ emptyLedgerState = ByronLedgerState { ledgerStateAfterEBB :: LedgerState ByronBlock ledgerStateAfterEBB = - reapplyLedgerBlock ledgerConfig exampleEBB - . applyChainTick ledgerConfig (SlotNo 0) + reapplyLedgerBlock OmitLedgerEvents ledgerConfig exampleEBB + . applyChainTick OmitLedgerEvents ledgerConfig (SlotNo 0) $ emptyLedgerState exampleLedgerState :: LedgerState ByronBlock exampleLedgerState = - reapplyLedgerBlock ledgerConfig exampleBlock - . applyChainTick ledgerConfig (SlotNo 1) + reapplyLedgerBlock OmitLedgerEvents ledgerConfig exampleBlock + . applyChainTick OmitLedgerEvents ledgerConfig (SlotNo 1) $ ledgerStateAfterEBB exampleHeaderState :: HeaderState ByronBlock diff --git a/ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Ledger.hs b/ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Ledger.hs index 685056d3f3..379949fb89 100644 --- a/ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Ledger.hs +++ b/ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Ledger.hs @@ -33,7 +33,6 @@ import qualified Ouroboros.Consensus.ByronSpec.Ledger.Rules as Rules import Ouroboros.Consensus.Ledger.Abstract import Ouroboros.Consensus.Ledger.CommonProtocolParams import Ouroboros.Consensus.Ticked -import Ouroboros.Consensus.Util ((..:)) {------------------------------------------------------------------------------- State @@ -103,7 +102,7 @@ instance IsLedger (LedgerState ByronSpecBlock) where type AuxLedgerEvent (LedgerState ByronSpecBlock) = VoidLedgerEvent (LedgerState ByronSpecBlock) - applyChainTickLedgerResult cfg slot (ByronSpecLedgerState tip state) = + applyChainTickLedgerResult _evs cfg slot (ByronSpecLedgerState tip state) = pureLedgerResult $ TickedByronSpecLedgerState { untickedByronSpecLedgerTip = tip @@ -118,7 +117,7 @@ instance IsLedger (LedgerState ByronSpecBlock) where -------------------------------------------------------------------------------} instance ApplyBlock (LedgerState ByronSpecBlock) ByronSpecBlock where - applyBlockLedgerResult cfg block (TickedByronSpecLedgerState _tip state) = + applyBlockLedgerResultWithValidation _ _ cfg block (TickedByronSpecLedgerState _tip state) = withExcept ByronSpecLedgerError $ fmap (pureLedgerResult . ByronSpecLedgerState (Just (blockSlot block))) $ -- Note that the CHAIN rule also applies the chain tick. So even @@ -131,14 +130,9 @@ instance ApplyBlock (LedgerState ByronSpecBlock) ByronSpecBlock where (byronSpecBlock block) state + applyBlockLedgerResult = defaultApplyBlockLedgerResult reapplyBlockLedgerResult = - -- The spec doesn't have a "reapply" mode - dontExpectError ..: applyBlockLedgerResult - where - dontExpectError :: Except a b -> b - dontExpectError mb = case runExcept mb of - Left _ -> error "reapplyBlockLedgerResult: unexpected error" - Right b -> b + defaultReapplyBlockLedgerResult (error . ("reapplyBlockLedgerResult: unexpected error " ++) . show) {------------------------------------------------------------------------------- CommonProtocolParams diff --git a/ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/TxGen/Cardano.hs b/ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/TxGen/Cardano.hs index 961a7b5555..52ad5f1921 100644 --- a/ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/TxGen/Cardano.hs +++ b/ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/TxGen/Cardano.hs @@ -41,8 +41,8 @@ import Ouroboros.Consensus.HardFork.Combinator.Ledger (tickedHardForkLedgerStatePerEra) import Ouroboros.Consensus.HardFork.Combinator.State.Types (currentState, getHardForkState) -import Ouroboros.Consensus.Ledger.Basics (LedgerConfig, LedgerState, - applyChainTick) +import Ouroboros.Consensus.Ledger.Basics (ComputeLedgerEvents (..), + LedgerConfig, LedgerState, applyChainTick) import Ouroboros.Consensus.NodeId (CoreNodeId (..)) import Ouroboros.Consensus.Protocol.TPraos (TPraos) import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock, mkShelleyTx) @@ -211,7 +211,7 @@ migrateUTxO migrationInfo curSlot lcfg lst mbUTxO = fmap getUTxOShelley $ ejectShelleyTickedLedgerState $ - applyChainTick lcfg curSlot $ + applyChainTick OmitLedgerEvents lcfg curSlot $ lst MigrationInfo diff --git a/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis.hs b/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis.hs index 6dfd65d6b2..ebf78c3628 100644 --- a/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis.hs +++ b/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis.hs @@ -56,8 +56,8 @@ import Ouroboros.Consensus.Ledger.Abstract (ApplyBlock (reapplyBlockLedgerResult), LedgerCfg, LedgerConfig, applyBlockLedgerResult, applyChainTick, tickThenApply, tickThenApplyLedgerResult, tickThenReapply) -import Ouroboros.Consensus.Ledger.Basics (LedgerResult (..), - LedgerState, getTipSlot) +import Ouroboros.Consensus.Ledger.Basics (ComputeLedgerEvents (..), + LedgerResult (..), LedgerState, getTipSlot) import Ouroboros.Consensus.Ledger.Extended import Ouroboros.Consensus.Ledger.SupportsMempool (LedgerSupportsMempool) @@ -74,7 +74,7 @@ import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB import Ouroboros.Consensus.Storage.LedgerDB (DiskSnapshot (..), writeSnapshot) import Ouroboros.Consensus.Storage.Serialisation (encodeDisk) -import Ouroboros.Consensus.Util (Flag (..), (..:)) +import Ouroboros.Consensus.Util (Flag (..), (...:)) import qualified Ouroboros.Consensus.Util.IOLike as IOLike import Ouroboros.Network.SizeInBytes import System.FS.API (SomeHasFS (..)) @@ -394,7 +394,7 @@ storeLedgerStateAt slotNo ledgerAppMode doChecksum env = do process :: ExtLedgerState blk -> blk -> IO (NextStep, ExtLedgerState blk) process oldLedger blk = do let ledgerCfg = ExtLedgerCfg cfg - case runExcept $ tickThenXApply ledgerCfg blk oldLedger of + case runExcept $ tickThenXApply OmitLedgerEvents ledgerCfg blk oldLedger of Right newLedger -> do when (blockSlot blk >= slotNo) $ storeLedgerState newLedger when (blockSlot blk > slotNo) $ issueWarning blk @@ -406,7 +406,7 @@ storeLedgerStateAt slotNo ledgerAppMode doChecksum env = do pure (Stop, oldLedger) tickThenXApply = case ledgerAppMode of - LedgerReapply -> pure ..: tickThenReapply + LedgerReapply -> pure ...: tickThenReapply LedgerApply -> tickThenApply continue :: blk -> NextStep @@ -473,7 +473,7 @@ checkNoThunksEvery process :: ExtLedgerState blk -> blk -> IO (ExtLedgerState blk) process oldLedger blk = do let ledgerCfg = ExtLedgerCfg cfg - appliedResult = tickThenApplyLedgerResult ledgerCfg blk oldLedger + appliedResult = tickThenApplyLedgerResult OmitLedgerEvents ledgerCfg blk oldLedger newLedger = either (error . show) lrResult $ runExcept $ appliedResult bn = blockNo blk when (unBlockNo bn `mod` nBlocks == 0 ) $ IOLike.evaluate (ledgerState newLedger) >>= checkNoThunks bn @@ -511,7 +511,7 @@ traceLedgerProcessing -> IO (ExtLedgerState blk) process oldLedger blk = do let ledgerCfg = ExtLedgerCfg cfg - appliedResult = tickThenApplyLedgerResult ledgerCfg blk oldLedger + appliedResult = tickThenApplyLedgerResult OmitLedgerEvents ledgerCfg blk oldLedger newLedger = either (error . show) lrResult $ runExcept $ appliedResult traces = (HasAnalysis.emitTraces $ @@ -667,18 +667,18 @@ benchmarkLedgerOps mOutfile ledgerAppMode AnalysisEnv {db, registry, startFrom, -> ExtLedgerState blk -> IO (Ticked (LedgerState blk)) tickTheLedgerState slot st = - pure $ applyChainTick lcfg slot (ledgerState st) + pure $ applyChainTick OmitLedgerEvents lcfg slot (ledgerState st) applyTheBlock :: Ticked (LedgerState blk) -> IO (LedgerState blk) applyTheBlock tickedLedgerSt = case ledgerAppMode of LedgerApply -> - case runExcept (lrResult <$> applyBlockLedgerResult lcfg blk tickedLedgerSt) of + case runExcept (lrResult <$> applyBlockLedgerResult OmitLedgerEvents lcfg blk tickedLedgerSt) of Left err -> fail $ "benchmark doesn't support invalid blocks: " <> show rp <> " " <> show err Right x -> pure x LedgerReapply -> - pure $! lrResult $ reapplyBlockLedgerResult lcfg blk tickedLedgerSt + pure $! lrResult $ reapplyBlockLedgerResult OmitLedgerEvents lcfg blk tickedLedgerSt withFile :: Maybe FilePath -> (IO.Handle -> IO r) -> IO r withFile (Just outfile) = IO.withFile outfile IO.WriteMode @@ -707,7 +707,7 @@ getBlockApplicationMetrics (NumberOfBlocks nrBlocks) mOutFile env = do process :: IO.Handle -> ExtLedgerState blk -> blk -> IO (ExtLedgerState blk) process outFileHandle currLedgerSt blk = do - let nextLedgerSt = tickThenReapply (ExtLedgerCfg cfg) blk currLedgerSt + let nextLedgerSt = tickThenReapply OmitLedgerEvents (ExtLedgerCfg cfg) blk currLedgerSt when (unBlockNo (blockNo blk) `mod` nrBlocks == 0) $ do let blockApplication = HasAnalysis.WithLedgerState blk @@ -830,7 +830,7 @@ reproMempoolForge numBlks env = do do let slot = blockSlot blk (ticked, durTick, mutTick, gcTick) <- timed $ IOLike.evaluate $ - applyChainTick lCfg slot (ledgerState st) + applyChainTick OmitLedgerEvents lCfg slot (ledgerState st) ((), durSnap, mutSnap, gcSnap) <- timed $ IOLike.atomically $ do snap <- Mempool.getSnapshotFor mempool $ Mempool.ForgeInKnownSlot slot ticked @@ -858,7 +858,7 @@ reproMempoolForge numBlks env = do -- since it currently matches the call in the forging thread, which is -- the primary intention of this Analysis. Maybe GHC's CSE is already -- doing this sharing optimization? - IOLike.atomically $ IOLike.writeTVar ref $! tickThenReapply elCfg blk st + IOLike.atomically $ IOLike.writeTVar ref $! tickThenReapply OmitLedgerEvents elCfg blk st -- this flushes blk from the mempool, since every tx in it is now on the chain void $ Mempool.syncWithLedger mempool diff --git a/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBSynthesizer/Forging.hs b/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBSynthesizer/Forging.hs index f49771ea4a..1b15158cb9 100644 --- a/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBSynthesizer/Forging.hs +++ b/ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBSynthesizer/Forging.hs @@ -160,6 +160,7 @@ runForge epochSize_ nextSlot opts chainDB blockForging cfg genTxs = do let tickedLedgerState :: Ticked (LedgerState blk) tickedLedgerState = applyChainTick + OmitLedgerEvents (configLedger cfg) currentSlot (ledgerState unticked) diff --git a/ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/TxGen/Shelley.hs b/ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/TxGen/Shelley.hs index 90d8dd5dad..9e47eb74bc 100644 --- a/ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/TxGen/Shelley.hs +++ b/ouroboros-consensus-cardano/src/unstable-shelley-testlib/Test/ThreadNet/TxGen/Shelley.hs @@ -62,7 +62,7 @@ instance HashAlgorithm h => TxGen (ShelleyBlock (TPraos (MockCrypto h)) (MockShe then pure [] else do n <- choose (0, 20) - go [] n $ applyChainTick lcfg curSlotNo lst + go [] n $ applyChainTick OmitLedgerEvents lcfg curSlotNo lst where ShelleyTxGenExtra { stgeGenEnv diff --git a/ouroboros-consensus-cardano/test/byron-test/Test/ThreadNet/DualByron.hs b/ouroboros-consensus-cardano/test/byron-test/Test/ThreadNet/DualByron.hs index d06d4ef4dc..f0daf083ad 100644 --- a/ouroboros-consensus-cardano/test/byron-test/Test/ThreadNet/DualByron.hs +++ b/ouroboros-consensus-cardano/test/byron-test/Test/ThreadNet/DualByron.hs @@ -257,7 +257,7 @@ byronPBftParams ByronSpecGenesis{..} = instance TxGen DualByronBlock where testGenTxs _coreNodeId _numCoreNodes curSlotNo cfg () = \st -> do n <- choose (0, 20) - go [] n $ applyChainTick (configLedger cfg) curSlotNo st + go [] n $ applyChainTick OmitLedgerEvents (configLedger cfg) curSlotNo st where -- Attempt to produce @n@ transactions -- Stops when the transaction generator cannot produce more txs diff --git a/ouroboros-consensus-cardano/test/shelley-test/Test/ThreadNet/Shelley.hs b/ouroboros-consensus-cardano/test/shelley-test/Test/ThreadNet/Shelley.hs index c75fb66f8f..fadf7e5a03 100644 --- a/ouroboros-consensus-cardano/test/shelley-test/Test/ThreadNet/Shelley.hs +++ b/ouroboros-consensus-cardano/test/shelley-test/Test/ThreadNet/Shelley.hs @@ -314,7 +314,7 @@ prop_simple_real_tpraos_convergence TestSetup -- slots to reach the epoch transition but the last several -- slots end up empty. Shelley.tickedShelleyLedgerState $ - applyChainTick ledgerConfig sentinel lsUnticked + applyChainTick OmitLedgerEvents ledgerConfig sentinel lsUnticked msg = "The ticked final ledger state of " <> show nid <> diff --git a/ouroboros-consensus-diffusion/src/unstable-diffusion-testlib/Test/ThreadNet/Network.hs b/ouroboros-consensus-diffusion/src/unstable-diffusion-testlib/Test/ThreadNet/Network.hs index d7dd9e75cc..727cbb7f08 100644 --- a/ouroboros-consensus-diffusion/src/unstable-diffusion-testlib/Test/ThreadNet/Network.hs +++ b/ouroboros-consensus-diffusion/src/unstable-diffusion-testlib/Test/ThreadNet/Network.hs @@ -622,11 +622,11 @@ runThreadNetwork systemTime ThreadNetworkArgs snap1 <- getSnapshotFor mempool $ -- This node would include these crucial txs if it leads in -- this slot. - ForgeInKnownSlot slot $ applyChainTick lcfg slot ledger + ForgeInKnownSlot slot $ applyChainTick OmitLedgerEvents lcfg slot ledger snap2 <- getSnapshotFor mempool $ -- Other nodes might include these crucial txs when leading -- in the next slot. - ForgeInKnownSlot (succ slot) $ applyChainTick lcfg (succ slot) ledger + ForgeInKnownSlot (succ slot) $ applyChainTick OmitLedgerEvents lcfg (succ slot) ledger -- This loop will repeat for the next slot, so we only need to -- check for this one and the next. pure (snap1, snap2) @@ -887,10 +887,10 @@ runThreadNetwork systemTime ThreadNetworkArgs -- fail if the EBB is invalid -- if it is valid, we retick to the /same/ slot - let apply = applyLedgerBlock (configLedger pInfoConfig) + let apply = applyLedgerBlock OmitLedgerEvents (configLedger pInfoConfig) tickedLdgSt' <- case Exc.runExcept $ apply ebb tickedLdgSt of Left e -> Exn.throw $ JitEbbError @blk e - Right st -> pure $ applyChainTick + Right st -> pure $ applyChainTick OmitLedgerEvents (configLedger pInfoConfig) currentSlot st diff --git a/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs b/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs index 4aa3b65074..a5d83d4182 100644 --- a/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs +++ b/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs @@ -41,7 +41,6 @@ module Test.Consensus.HardFork.Combinator.A ( import Cardano.Slotting.EpochInfo import Codec.Serialise import Control.Monad (guard) -import Control.Monad.Except (runExcept) import qualified Data.Binary as B import Data.ByteString as Strict import qualified Data.ByteString.Lazy as Lazy @@ -81,7 +80,7 @@ import Ouroboros.Consensus.Node.Serialisation import Ouroboros.Consensus.Protocol.Abstract import Ouroboros.Consensus.Storage.ImmutableDB (simpleChunkInfo) import Ouroboros.Consensus.Storage.Serialisation -import Ouroboros.Consensus.Util (repeatedlyM, (..:), (.:)) +import Ouroboros.Consensus.Util (repeatedlyM, (.:)) import Ouroboros.Consensus.Util.Condense import Ouroboros.Consensus.Util.Orphans () import Ouroboros.Network.Block (Serialised, unwrapCBORinCBOR, @@ -213,10 +212,10 @@ instance IsLedger (LedgerState BlockA) where type AuxLedgerEvent (LedgerState BlockA) = VoidLedgerEvent (LedgerState BlockA) - applyChainTickLedgerResult _ _ = pureLedgerResult . TickedLedgerStateA + applyChainTickLedgerResult _ _ _ = pureLedgerResult . TickedLedgerStateA instance ApplyBlock (LedgerState BlockA) BlockA where - applyBlockLedgerResult cfg blk = + applyBlockLedgerResultWithValidation _ _ cfg blk = fmap (pureLedgerResult . setTip) . repeatedlyM (fmap fst .: applyTx cfg DoNotIntervene (blockSlot blk)) @@ -225,13 +224,9 @@ instance ApplyBlock (LedgerState BlockA) BlockA where setTip :: TickedLedgerState BlockA -> LedgerState BlockA setTip (TickedLedgerStateA st) = st { lgrA_tip = blockPoint blk } + applyBlockLedgerResult = defaultApplyBlockLedgerResult reapplyBlockLedgerResult = - dontExpectError ..: applyBlockLedgerResult - where - dontExpectError :: Except a b -> b - dontExpectError mb = case runExcept mb of - Left _ -> error "reapplyBlockLedgerResult: unexpected error" - Right b -> b + defaultReapplyBlockLedgerResult absurd instance UpdateLedger BlockA diff --git a/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/B.hs b/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/B.hs index 7c45c64137..7d4bbbe075 100644 --- a/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/B.hs +++ b/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/B.hs @@ -186,11 +186,12 @@ instance IsLedger (LedgerState BlockB) where type AuxLedgerEvent (LedgerState BlockB) = VoidLedgerEvent (LedgerState BlockB) - applyChainTickLedgerResult _ _ = pureLedgerResult . TickedLedgerStateB + applyChainTickLedgerResult _ _ _ = pureLedgerResult . TickedLedgerStateB instance ApplyBlock (LedgerState BlockB) BlockB where - applyBlockLedgerResult = \_ b _ -> return $ pureLedgerResult $ LgrB (blockPoint b) - reapplyBlockLedgerResult = \_ b _ -> pureLedgerResult $ LgrB (blockPoint b) + applyBlockLedgerResultWithValidation = \_ _ _ b _ -> return $ pureLedgerResult $ LgrB (blockPoint b) + applyBlockLedgerResult = defaultApplyBlockLedgerResult + reapplyBlockLedgerResult = defaultReapplyBlockLedgerResult absurd instance UpdateLedger BlockB diff --git a/ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/ChainDB.hs b/ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/ChainDB.hs index 493bc743c8..190c1df05d 100644 --- a/ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/ChainDB.hs +++ b/ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/ChainDB.hs @@ -19,6 +19,7 @@ import Ouroboros.Consensus.Block.Abstract import Ouroboros.Consensus.Config (TopLevelConfig (topLevelConfigLedger), configCodec) import Ouroboros.Consensus.HardFork.History.EraParams (eraEpochSize) +import Ouroboros.Consensus.Ledger.Basics (ComputeLedgerEvents (..)) import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState) import Ouroboros.Consensus.Protocol.Abstract import Ouroboros.Consensus.Storage.ChainDB hiding @@ -117,7 +118,7 @@ fromMinimalChainDbArgs MinimalChainDbArgs {..} = ChainDbArgs { , lgrGenesis = return mcdbInitLedger , lgrHasFS = SomeHasFS $ simHasFS (nodeDBsLgr mcdbNodeDBs) , lgrTracer = nullTracer - , lgrConfig = configLedgerDb mcdbTopLevelConfig + , lgrConfig = configLedgerDb mcdbTopLevelConfig OmitLedgerEvents } , cdbsArgs = ChainDbSpecificArgs { cdbsBlocksToAddSize = 1 diff --git a/ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/TestBlock.hs b/ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/TestBlock.hs index 5ed7674f03..75c68e9d54 100644 --- a/ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/TestBlock.hs +++ b/ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/TestBlock.hs @@ -490,7 +490,7 @@ instance ( Typeable ptype instance PayloadSemantics ptype => ApplyBlock (LedgerState (TestBlockWith ptype)) (TestBlockWith ptype) where - applyBlockLedgerResult _ tb@TestBlockWith{..} (TickedTestLedger TestLedger{..}) + applyBlockLedgerResultWithValidation _validation _events _ tb@TestBlockWith{..} (TickedTestLedger TestLedger{..}) | blockPrevHash tb /= pointHash lastAppliedPoint = throwError $ InvalidHash (pointHash lastAppliedPoint) (blockPrevHash tb) | tbValid == Invalid @@ -504,15 +504,9 @@ instance PayloadSemantics ptype , payloadDependentState = st' } - reapplyBlockLedgerResult _ tb@TestBlockWith{..} (TickedTestLedger TestLedger{..}) = - case applyPayload payloadDependentState tbPayload of - Left err -> error $ "Found an error when reapplying a block: " ++ show err - Right st' -> pureLedgerResult - $ TestLedger { - lastAppliedPoint = Chain.blockPoint tb - , payloadDependentState = st' - } - + applyBlockLedgerResult = defaultApplyBlockLedgerResult + reapplyBlockLedgerResult = + defaultReapplyBlockLedgerResult (error . ("Found an error when reapplying a block: " ++) . show) data instance LedgerState (TestBlockWith ptype) = TestLedger { @@ -573,7 +567,7 @@ instance PayloadSemantics ptype => IsLedger (LedgerState (TestBlockWith ptype)) type AuxLedgerEvent (LedgerState (TestBlockWith ptype)) = VoidLedgerEvent (LedgerState (TestBlockWith ptype)) - applyChainTickLedgerResult _ _ = pureLedgerResult . TickedTestLedger + applyChainTickLedgerResult _ _ _ = pureLedgerResult . TickedTestLedger instance PayloadSemantics ptype => UpdateLedger (TestBlockWith ptype) diff --git a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs index f6db8e90cf..fb7f07e4b1 100644 --- a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs +++ b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs @@ -351,17 +351,16 @@ instance MockProtocolSpecific c ext type AuxLedgerEvent (LedgerState (SimpleBlock c ext)) = VoidLedgerEvent (SimpleBlock c ext) - applyChainTickLedgerResult _ _ = pureLedgerResult . TickedSimpleLedgerState + applyChainTickLedgerResult _ _ _ = pureLedgerResult . TickedSimpleLedgerState instance MockProtocolSpecific c ext => ApplyBlock (LedgerState (SimpleBlock c ext)) (SimpleBlock c ext) where - applyBlockLedgerResult = fmap pureLedgerResult ..: updateSimpleLedgerState + applyBlockLedgerResultWithValidation _validation _events = + fmap pureLedgerResult ..: updateSimpleLedgerState + applyBlockLedgerResult = defaultApplyBlockLedgerResult reapplyBlockLedgerResult = - (mustSucceed . runExcept) ..: applyBlockLedgerResult - where - mustSucceed (Left err) = error ("reapplyBlockLedgerResult: unexpected error: " <> show err) - mustSucceed (Right st) = st + defaultReapplyBlockLedgerResult (error . ("reapplyBlockLedgerResult: unexpected error: " <>) . show) newtype instance LedgerState (SimpleBlock c ext) = SimpleLedgerState { simpleLedgerState :: MockState (SimpleBlock c ext) diff --git a/ouroboros-consensus/src/unstable-tutorials/Ouroboros/Consensus/Tutorial/Simple.lhs b/ouroboros-consensus/src/unstable-tutorials/Ouroboros/Consensus/Tutorial/Simple.lhs index 69af4e6f8d..70ecf79b78 100644 --- a/ouroboros-consensus/src/unstable-tutorials/Ouroboros/Consensus/Tutorial/Simple.lhs +++ b/ouroboros-consensus/src/unstable-tutorials/Ouroboros/Consensus/Tutorial/Simple.lhs @@ -36,7 +36,7 @@ This example uses several extensions: First, some imports we'll need: -> import Data.Void(Void) +> import Data.Void(Void, absurd) > import Data.Set(Set) > import qualified Data.Set as Set > import Data.Word(Word64, Word8) @@ -56,7 +56,8 @@ First, some imports we'll need: > import Ouroboros.Consensus.Ledger.Abstract > (GetTip(..), IsLedger(..), LedgerCfg, > LedgerResult(LedgerResult, lrEvents, lrResult), -> LedgerState, ApplyBlock(..), UpdateLedger) +> LedgerState, ApplyBlock(..), UpdateLedger, +> defaultApplyBlockLedgerResult, defaultReapplyBlockLedgerResult) > import Ouroboros.Consensus.Ledger.SupportsProtocol > (LedgerSupportsProtocol(..)) > import Ouroboros.Consensus.Forecast (trivialForecast) @@ -559,7 +560,8 @@ types for a ledger. Though we are here using > type instance LedgerErr (LedgerState BlockC) = Void > type instance AuxLedgerEvent (LedgerState BlockC) = Void > -> applyChainTickLedgerResult _cfg _slot ldgrSt = + +> applyChainTickLedgerResult _events _cfg _slot ldgrSt = > LedgerResult { lrEvents = [] > , lrResult = TickedLedgerStateC ldgrSt > } @@ -609,17 +611,14 @@ The interface used by the rest of the ledger infrastructure to access this is the `ApplyBlock` typeclass: > instance ApplyBlock (LedgerState BlockC) BlockC where -> applyBlockLedgerResult _ldgrCfg block tickedLdgrSt = +> applyBlockLedgerResultWithValidation _validation _events _ldgrCfg block tickedLdgrSt = > pure $ LedgerResult { lrEvents = [] > , lrResult = block `applyBlockTo` tickedLdgrSt > } > -> reapplyBlockLedgerResult _ldgrCfg block tickedLdgrSt = -> LedgerResult { lrEvents = [] -> , lrResult = block `applyBlockTo` tickedLdgrSt -> } -> > +> applyBlockLedgerResult = defaultApplyBlockLedgerResult +> reapplyBlockLedgerResult = defaultReapplyBlockLedgerResult absurd `applyBlockLedgerResult` tries to apply a block to the ledger and fails with a `LedgerErr` corresponding to the particular `LedgerState blk` if for whatever diff --git a/ouroboros-consensus/src/unstable-tutorials/Ouroboros/Consensus/Tutorial/WithEpoch.lhs b/ouroboros-consensus/src/unstable-tutorials/Ouroboros/Consensus/Tutorial/WithEpoch.lhs index bc8345b871..f3b47d17f9 100644 --- a/ouroboros-consensus/src/unstable-tutorials/Ouroboros/Consensus/Tutorial/WithEpoch.lhs +++ b/ouroboros-consensus/src/unstable-tutorials/Ouroboros/Consensus/Tutorial/WithEpoch.lhs @@ -57,6 +57,7 @@ And imports, of course: > import Control.Monad () > import Control.Monad.Except (MonadError (throwError)) > import Data.Word (Word64) +> import Data.Void (Void, absurd) > import GHC.Generics (Generic) > import NoThunks.Class (NoThunks, OnlyCheckWhnfNamed (..)) > import Data.Hashable (Hashable (hash)) @@ -77,8 +78,9 @@ And imports, of course: > import Ouroboros.Consensus.Ticked (Ticked) > import Ouroboros.Consensus.Ledger.Abstract > (LedgerState, LedgerCfg, GetTip, LedgerResult (..), ApplyBlock (..), -> UpdateLedger, IsLedger (..)) -> +> UpdateLedger, IsLedger (..), defaultApplyBlockLedgerResult, +> defaultReapplyBlockLedgerResult) + > import Ouroboros.Consensus.Ledger.SupportsMempool () > import Ouroboros.Consensus.Ledger.SupportsProtocol > (LedgerSupportsProtocol (..)) @@ -371,10 +373,11 @@ blocks are applied during the span of time represented by the slot argument. We can now use `tickLedgerStateD` to instantiate `IsLedger`: > instance IsLedger (LedgerState BlockD) where -> type instance LedgerErr (LedgerState BlockD) = String +> type instance LedgerErr (LedgerState BlockD) = Void > type instance AuxLedgerEvent (LedgerState BlockD) = () > -> applyChainTickLedgerResult _cfg slot ldgrSt = + +> applyChainTickLedgerResult _events _cfg slot ldgrSt = > LedgerResult { lrEvents = [] > , lrResult = tickLedgerStateD slot ldgrSt > } @@ -403,15 +406,13 @@ applying each individual transaction - exactly as it was in for `BlockC`: > Dec -> i - 1 > instance ApplyBlock (LedgerState BlockD) BlockD where -> applyBlockLedgerResult _ldgrCfg b tickedLdgrSt = +> applyBlockLedgerResultWithValidation _validation _events _ldgrCfg b tickedLdgrSt = > pure LedgerResult { lrResult = b `applyBlockTo` tickedLdgrSt > , lrEvents = [] > } > -> reapplyBlockLedgerResult _ldgrCfg b tickedLdgrSt = -> LedgerResult { lrResult = b `applyBlockTo` tickedLdgrSt -> , lrEvents = [] -> } +> applyBlockLedgerResult = defaultApplyBlockLedgerResult +> reapplyBlockLedgerResult = defaultReapplyBlockLedgerResult absurd Note that prior to `applyBlockLedgerResult` being invoked, the calling code will have already established that the header is valid and that the header matches diff --git a/ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/ChainSync/Client.hs b/ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/ChainSync/Client.hs index 63e810a572..b07cababbc 100644 --- a/ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/ChainSync/Client.hs +++ b/ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/ChainSync/Client.hs @@ -776,7 +776,7 @@ computePastLedger cfg pt chain | castPoint (getTip st) == pt = st | blk:blks' <- blks - = go (tickThenReapply (ExtLedgerCfg cfg) blk st) blks' + = go (tickThenReapply OmitLedgerEvents (ExtLedgerCfg cfg) blk st) blks' | otherwise = error "point not in the list of blocks" diff --git a/ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/LocalStateQuery/Server.hs b/ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/LocalStateQuery/Server.hs index 5310647fd7..292867fb56 100644 --- a/ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/LocalStateQuery/Server.hs +++ b/ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/LocalStateQuery/Server.hs @@ -29,6 +29,7 @@ import Ouroboros.Consensus.Block import Ouroboros.Consensus.BlockchainTime import Ouroboros.Consensus.Config import qualified Ouroboros.Consensus.HardFork.History as HardFork +import Ouroboros.Consensus.Ledger.Basics import Ouroboros.Consensus.Ledger.Extended import Ouroboros.Consensus.Ledger.Query (Query (..)) import Ouroboros.Consensus.MiniProtocol.LocalStateQuery.Server @@ -216,7 +217,7 @@ initLgrDB k chain = do blockMapping = Map.fromList [(blockRealPoint b, b) | b <- Chain.toOldestFirst chain] - cfg = configLedgerDb $ testCfg k + cfg = configLedgerDb (testCfg k) OmitLedgerEvents genesisLedgerDB = LgrDB.ledgerDbWithAnchor testInitExtLedger diff --git a/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/ChainDB/Model.hs b/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/ChainDB/Model.hs index 0e277dde9d..b7e5ec6713 100644 --- a/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/ChainDB/Model.hs +++ b/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/ChainDB/Model.hs @@ -350,8 +350,9 @@ getLedgerDB cfg m@Model{..} = k = configSecurityParam cfg ledgerDbCfg = LedgerDbCfg { - ledgerDbCfgSecParam = k - , ledgerDbCfg = ExtLedgerCfg cfg + ledgerDbCfgSecParam = k + , ledgerDbCfg = ExtLedgerCfg cfg + , ledgerDbCfgComputeLedgerEvents = OmitLedgerEvents } getLoEFragment :: Model blk -> LoE (AnchoredFragment blk) @@ -741,7 +742,7 @@ validate cfg Model { initLedger, invalid } chain = go ledger validPrefix = \case -- Return 'mbFinal' if it contains an "earlier" result [] -> ValidatedChain validPrefix ledger invalid - b:bs' -> case runExcept (tickThenApply (ExtLedgerCfg cfg) b ledger) of + b:bs' -> case runExcept (tickThenApply OmitLedgerEvents (ExtLedgerCfg cfg) b ledger) of -- Invalid block according to the ledger Left e -> ValidatedChain diff --git a/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/InMemory.hs b/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/InMemory.hs index 2904da9c0a..a6e606e115 100644 --- a/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/InMemory.hs +++ b/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/InMemory.hs @@ -140,7 +140,7 @@ prop_pushExpectedLedger :: ChainSetup -> Property prop_pushExpectedLedger setup@ChainSetup{..} = classify (chainSetupSaturated setup) "saturated" $ conjoin [ - l === refoldLedger cfg (expectedChain o) testInitLedger + l === refoldLedger OmitLedgerEvents cfg (expectedChain o) testInitLedger | (o, l) <- ledgerDbSnapshots csPushed ] where @@ -206,7 +206,7 @@ prop_switchExpectedLedger :: SwitchSetup -> Property prop_switchExpectedLedger setup@SwitchSetup{..} = classify (switchSetupSaturated setup) "saturated" $ conjoin [ - l === refoldLedger cfg (expectedChain o) testInitLedger + l === refoldLedger OmitLedgerEvents cfg (expectedChain o) testInitLedger | (o, l) <- ledgerDbSnapshots ssSwitched ] where @@ -274,10 +274,11 @@ csBlockConfig = csBlockConfig' . csSecParam csBlockConfig' :: SecurityParam -> LedgerDbCfg (LedgerState TestBlock) csBlockConfig' secParam = LedgerDbCfg { - ledgerDbCfgSecParam = secParam - , ledgerDbCfg = + ledgerDbCfgSecParam = secParam + , ledgerDbCfg = testBlockLedgerConfigFrom $ HardFork.defaultEraParams secParam slotLength + , ledgerDbCfgComputeLedgerEvents = OmitLedgerEvents } where slotLength = slotLengthFromSec 20 diff --git a/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/OnDisk.hs b/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/OnDisk.hs index 37f2092dd3..eb41f63b9e 100644 --- a/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/OnDisk.hs +++ b/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/OnDisk.hs @@ -256,8 +256,15 @@ genBlockFromLedgerState = pure . genBlock . lastAppliedPoint . ledgerState extLedgerDbConfig :: SecurityParam -> LedgerDbCfg (ExtLedgerState TestBlock) extLedgerDbConfig secParam = LedgerDbCfg { - ledgerDbCfgSecParam = secParam - , ledgerDbCfg = ExtLedgerCfg $ singleNodeTestConfigWith TestBlockCodecConfig TestBlockStorageConfig secParam (GenesisWindow (2 * maxRollbacks secParam)) + ledgerDbCfgSecParam = secParam + , ledgerDbCfg = + ExtLedgerCfg $ + singleNodeTestConfigWith + TestBlockCodecConfig + TestBlockStorageConfig + secParam + (GenesisWindow (2 * maxRollbacks secParam)) + , ledgerDbCfgComputeLedgerEvents = OmitLedgerEvents } @@ -579,7 +586,7 @@ runMock cmd initMock = push :: TestBlock -> StateT MockLedger (Except (ExtValidationError TestBlock)) () push b = do ls <- State.get - l' <- State.lift $ tickThenApply (ledgerDbCfg cfg) b (cur ls) + l' <- State.lift $ tickThenApply (ledgerDbCfgComputeLedgerEvents cfg) (ledgerDbCfg cfg) b (cur ls) State.put ((b, l'):ls) switch :: Word64 diff --git a/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/TestBlock.hs b/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/TestBlock.hs index 979dabf525..128c15098d 100644 --- a/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/TestBlock.hs +++ b/ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/TestBlock.hs @@ -556,10 +556,10 @@ instance IsLedger (LedgerState TestBlock) where type AuxLedgerEvent (LedgerState TestBlock) = VoidLedgerEvent (LedgerState TestBlock) - applyChainTickLedgerResult _ _ = pureLedgerResult . TickedTestLedger + applyChainTickLedgerResult _ _ _ = pureLedgerResult . TickedTestLedger instance ApplyBlock (LedgerState TestBlock) TestBlock where - applyBlockLedgerResult _ tb@TestBlock{..} (TickedTestLedger TestLedger{..}) + applyBlockLedgerResultWithValidation _ _ _ tb@TestBlock{..} (TickedTestLedger TestLedger{..}) | blockPrevHash tb /= lastAppliedHash = throwError $ InvalidHash lastAppliedHash (blockPrevHash tb) | not $ tbIsValid testBody @@ -567,8 +567,9 @@ instance ApplyBlock (LedgerState TestBlock) TestBlock where | otherwise = return $ pureLedgerResult $ TestLedger (Chain.blockPoint tb) (BlockHash (blockHash tb)) - reapplyBlockLedgerResult _ tb _ = - pureLedgerResult $ TestLedger (Chain.blockPoint tb) (BlockHash (blockHash tb)) + applyBlockLedgerResult = defaultApplyBlockLedgerResult + reapplyBlockLedgerResult = + defaultReapplyBlockLedgerResult (error . ("reapplyBlockLedgerResult: impossible " <>) . show) data instance LedgerState TestBlock = TestLedger {