{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | Tests for the time-capped snapshot reapplication
-- ('Ouroboros.Consensus.Mempool.Query').
module Test.Consensus.Mempool.Query (tests) where

import Cardano.Slotting.Slot (SlotNo, WithOrigin (At, Origin))
import Control.Monad (unless)
import Control.Monad.Class.MonadTimer.SI (MonadTimer)
import Control.Monad.Except (runExcept)
import Control.Monad.IOSim (runSimOrThrow)
import Control.Tracer (nullTracer)
import Data.Foldable (foldlM)
import Data.List (sort)
import qualified Data.Map.Strict as Map
import qualified Data.Measure as Measure
import Data.Word (Word64)
import Ouroboros.Consensus.Ledger.Abstract
  ( ComputeLedgerEvents (OmitLedgerEvents)
  , DiffMK
  , HasLedgerTables (projectLedgerTables)
  , KeysMK (KeysMK)
  , LedgerConfig
  , TickedLedgerState
  , ValuesMK (ValuesMK)
  , applyChainTick
  , ledgerTipSlot
  )
import Ouroboros.Consensus.Ledger.SupportsMempool
  ( ByteSize32 (ByteSize32)
  , Invalidated (getInvalidated)
  , LedgerSupportsMempool (applyTx, txForgetValidated)
  , TxId
  , WhetherToIntervene (DoNotIntervene)
  , txId
  )
import Ouroboros.Consensus.Ledger.Tables.Utils
  ( applyDiffs
  , emptyLedgerTables
  , forgetLedgerTables
  , noNewTickingDiffs
  )
import Ouroboros.Consensus.Mempool
  ( Mempool (getSnapshotFor)
  , MempoolSnapshot (snapshotTxs)
  , addTxs
  , isMempoolTxAdded
  , openMempoolWithoutSyncThread
  )
import Ouroboros.Consensus.Mempool.API (MempoolMeasure)
import Ouroboros.Consensus.Mempool.Capacity
  ( MempoolCapacityBytesOverride
      ( MempoolCapacityBytesOverride
      , NoMempoolCapacityBytesOverride
      )
  )
import Ouroboros.Consensus.Mempool.Impl.Common
  ( InternalState (isLedgerState, isTxs)
  , LedgerInterface (..)
  , MempoolEnv (..)
  , MempoolLedgerDBView (..)
  , RevalidateTxsResult (..)
  , TraceEventMempool (..)
  , ValidatedTxWithDiffs (..)
  , initInternalState
  )
import Ouroboros.Consensus.Mempool.Query
  ( revalidateUntilTimeout
  , snapshotTxsPerStep
  )
import Ouroboros.Consensus.Mempool.TxSeq (TxTicket (..))
import qualified Ouroboros.Consensus.Mempool.TxSeq as TxSeq
import Ouroboros.Consensus.Mock.Ledger hiding (TxId, TxIn, TxOut)
import Ouroboros.Consensus.Storage.LedgerDB.Forker
  ( ReadOnlyForker (..)
  , Statistics (..)
  )
import Ouroboros.Consensus.Util.IOLike
import Ouroboros.Consensus.Util.TimeBoundedLoop (DoneType (..))
import Test.Consensus.Mempool.Util
  ( TestBlock
  , TestTx
  , applyTxToLedger
  , applyTxToTickedLedger
  , bumpTip
  , genConnectedTxLattice
  , genTickedLedgerState
  , genValidTxs
  , mustBeValid
  , testInitLedger
  , testLedgerConfigNoSizeLimits
  )
import Test.Crypto.Hash ()
import Test.QuickCheck
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)
import Test.Util.Orphans.IOLike ()
import Test.Util.Tracer (recordingTracerTVar)

tests :: TestTree
tests :: TestTree
tests =
  TestName -> [TestTree] -> TestTree
testGroup
    TestName
"Mempool.Query"
    [ TestName -> Property -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty
        TestName
"a dependent tx lattice reapplies fully, in order, for any step size"
        Property
prop_latticeAllValid
    , TestName -> Property -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty
        TestName
"txs not included by the new tip survive reapplication, for any step size"
        Property
prop_txChainSurvivesNewTip
    , TestName -> Property -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty
        TestName
"getSnapshotFor at the cached state reads nothing and returns everything"
        Property
prop_getSnapshotForCachedPaths
    , TestName -> Property -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty
        TestName
"getSnapshotFor at a new tip containing some mempool txs drops exactly those txs"
        Property
prop_getSnapshotForNewTip
    , TestName -> Property -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty
        TestName
"getSnapshotFor stops at the time limit, snapshotting a prefix"
        Property
prop_getSnapshotForTimeout
    ]

-------------------------------------------------------------------------------
-- Properties
-------------------------------------------------------------------------------

-- | Reapplying a connected lattice of dependent valid transactions at the
-- same tip keeps every transaction, in order, regardless of the step size,
-- and the resulting ledger state matches applying the transactions
-- sequentially.
prop_latticeAllValid :: Property
prop_latticeAllValid :: Property
prop_latticeAllValid =
  Gen (SlotNo, TickedLedgerState TestBlock ValuesMK)
-> ((SlotNo, TickedLedgerState TestBlock ValuesMK) -> Property)
-> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen (SlotNo, TickedLedgerState TestBlock ValuesMK)
genTickedLedgerState (((SlotNo, TickedLedgerState TestBlock ValuesMK) -> Property)
 -> Property)
-> ((SlotNo, TickedLedgerState TestBlock ValuesMK) -> Property)
-> Property
forall a b. (a -> b) -> a -> b
$ \(SlotNo
slot, TickedLedgerState TestBlock ValuesMK
initSt) ->
    Gen [TestTx] -> ([TestTx] -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind ((Int, Int)
-> SlotNo -> TickedLedgerState TestBlock ValuesMK -> Gen [TestTx]
genLatticeTxs (Int
1, Int
5) SlotNo
slot TickedLedgerState TestBlock ValuesMK
initSt) (([TestTx] -> Property) -> Property)
-> ([TestTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[TestTx]
allTxs ->
      Gen Int -> (Int -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
1, [TestTx] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TestTx]
allTxs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
stepSize ->
        let tickets :: [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
tickets = SlotNo
-> [TestTx]
-> TickedLedgerState TestBlock ValuesMK
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
buildTicketList SlotNo
slot [TestTx]
allTxs TickedLedgerState TestBlock ValuesMK
initSt
            (RevalidateTxsResult InternalState TestBlock
is [Invalidated TestBlock]
removed, DoneType
done) =
              Int
-> TickedLedgerState TestBlock ValuesMK
-> SlotNo
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> (RevalidateTxsResult TestBlock, DoneType)
runRevalidate Int
stepSize TickedLedgerState TestBlock ValuesMK
initSt SlotNo
slot [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
tickets
            expectedSt :: TickedLedgerState TestBlock ValuesMK
expectedSt =
              Except TestTxError (TickedLedgerState TestBlock ValuesMK)
-> TickedLedgerState TestBlock ValuesMK
forall a. HasCallStack => Except TestTxError a -> a
mustBeValid (Except TestTxError (TickedLedgerState TestBlock ValuesMK)
 -> TickedLedgerState TestBlock ValuesMK)
-> Except TestTxError (TickedLedgerState TestBlock ValuesMK)
-> TickedLedgerState TestBlock ValuesMK
forall a b. (a -> b) -> a -> b
$
                (TickedLedgerState TestBlock ValuesMK
 -> TestTx
 -> Except TestTxError (TickedLedgerState TestBlock ValuesMK))
-> TickedLedgerState TestBlock ValuesMK
-> [TestTx]
-> Except TestTxError (TickedLedgerState TestBlock ValuesMK)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM
                  ((TestTx
 -> TickedLedgerState TestBlock ValuesMK
 -> ExceptT
      (MockError TestBlock)
      Identity
      (TickedLedgerState TestBlock ValuesMK))
-> TickedLedgerState TestBlock ValuesMK
-> TestTx
-> ExceptT
     (MockError TestBlock)
     Identity
     (TickedLedgerState TestBlock ValuesMK)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (LedgerConfig TestBlock
-> WhetherToIntervene
-> SlotNo
-> TestTx
-> TickedLedgerState TestBlock ValuesMK
-> Except TestTxError (TickedLedgerState TestBlock ValuesMK)
applyTxToTickedLedger LedgerConfig TestBlock
testCfg WhetherToIntervene
DoNotIntervene SlotNo
slot))
                  TickedLedgerState TestBlock ValuesMK
initSt
                  [TestTx]
allTxs
         in [Property] -> Property
forall prop. Testable prop => [prop] -> Property
conjoin
              [ TestName -> Property -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample TestName
"all txs must reapply, in order" (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                  [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> [TxId TestTx]
ticketIds (TxSeq (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
forall sz tx. TxSeq sz tx -> [TxTicket sz tx]
TxSeq.toList (InternalState TestBlock
-> TxSeq
     (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
forall blk.
InternalState blk
-> TxSeq (MempoolMeasure blk) (ValidatedTxWithDiffs blk)
isTxs InternalState TestBlock
is)) [TxId TestTx] -> [TxId TestTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> [TxId TestTx]
ticketIds [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
tickets
              , TestName -> Property -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample TestName
"no tx may be removed" (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                  (Invalidated TestBlock -> TxId TestTx)
-> [Invalidated TestBlock] -> [TxId TestTx]
forall a b. (a -> b) -> [a] -> [b]
map (TestTx -> TxId TestTx
forall tx. HasTxId tx => tx -> TxId tx
txId (TestTx -> TxId TestTx)
-> (Invalidated TestBlock -> TestTx)
-> Invalidated TestBlock
-> TxId TestTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Invalidated TestBlock -> TestTx
forall blk. Invalidated blk -> GenTx blk
getInvalidated) [Invalidated TestBlock]
removed [TxId TestTx] -> [TxId TestTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== []
              , TestName -> Property -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample TestName
"the loop must stop because it ran out of txs" (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                  DoneType
done DoneType -> DoneType -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== DoneType
EndReached
              , TestName -> Property -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample TestName
"the reapplied ledger state must match sequential application" (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                  TickedLedgerState TestBlock ValuesMK
-> LedgerState TestBlock ValuesMK
forall c ext (mk :: MapKind).
Ticked LedgerState (SimpleBlock c ext) mk
-> LedgerState (SimpleBlock c ext) mk
getTickedSimpleLedgerState (TickedLedgerState TestBlock ValuesMK
-> Ticked LedgerState TestBlock DiffMK
-> TickedLedgerState TestBlock ValuesMK
forall (l :: * -> MapKind -> *) blk (l' :: * -> MapKind -> *).
(HasLedgerTables l blk, HasLedgerTables l' blk) =>
l blk ValuesMK -> l' blk DiffMK -> l' blk ValuesMK
applyDiffs TickedLedgerState TestBlock ValuesMK
initSt (InternalState TestBlock -> Ticked LedgerState TestBlock DiffMK
forall blk. InternalState blk -> TickedLedgerState blk DiffMK
isLedgerState InternalState TestBlock
is))
                    LedgerState TestBlock ValuesMK
-> LedgerState TestBlock ValuesMK -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== TickedLedgerState TestBlock ValuesMK
-> LedgerState TestBlock ValuesMK
forall c ext (mk :: MapKind).
Ticked LedgerState (SimpleBlock c ext) mk
-> LedgerState (SimpleBlock c ext) mk
getTickedSimpleLedgerState TickedLedgerState TestBlock ValuesMK
expectedSt
              ]

-- | Reapply a connected lattice of dependent transactions at a tip whose
-- block contains a prefix of them (the lattice is flattened in dependency
-- order, so any prefix forms a valid block).
--
-- Exactly the included transactions must be removed — their inputs are
-- already spent on-chain — and every other transaction must survive, in
-- order: their inputs exist either at the new tip, as outputs of the
-- included transactions, or as outputs of earlier surviving transactions.
-- Checked for any step size, so survivors resolve their inputs across step
-- boundaries.
prop_txChainSurvivesNewTip :: Property
prop_txChainSurvivesNewTip :: Property
prop_txChainSurvivesNewTip =
  Gen (SlotNo, TickedLedgerState TestBlock ValuesMK)
-> ((SlotNo, TickedLedgerState TestBlock ValuesMK) -> Property)
-> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen (SlotNo, TickedLedgerState TestBlock ValuesMK)
genTickedLedgerState (((SlotNo, TickedLedgerState TestBlock ValuesMK) -> Property)
 -> Property)
-> ((SlotNo, TickedLedgerState TestBlock ValuesMK) -> Property)
-> Property
forall a b. (a -> b) -> a -> b
$ \(SlotNo
slot, TickedLedgerState TestBlock ValuesMK
initSt) ->
    Gen [TestTx] -> ([TestTx] -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind ((Int, Int)
-> SlotNo -> TickedLedgerState TestBlock ValuesMK -> Gen [TestTx]
genLatticeTxs (Int
1, Int
5) SlotNo
slot TickedLedgerState TestBlock ValuesMK
initSt) (([TestTx] -> Property) -> Property)
-> ([TestTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[TestTx]
allTxs ->
      Gen Int -> (Int -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
1, [TestTx] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TestTx]
allTxs)) ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
nOnChain ->
        Gen Int -> (Int -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
1, [TestTx] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TestTx]
allTxs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
stepSize ->
          let tickets :: [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
tickets = SlotNo
-> [TestTx]
-> TickedLedgerState TestBlock ValuesMK
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
buildTicketList SlotNo
slot [TestTx]
allTxs TickedLedgerState TestBlock ValuesMK
initSt
              ([TestTx]
onChainTxs, [TestTx]
offChainTxs) = Int -> [TestTx] -> ([TestTx], [TestTx])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
nOnChain [TestTx]
allTxs
              -- The new tip: a block containing the first 'nOnChain' txs.
              newTipSt :: TickedLedgerState TestBlock ValuesMK
newTipSt =
                (TickedLedgerState TestBlock ValuesMK
 -> TestTx -> TickedLedgerState TestBlock ValuesMK)
-> TickedLedgerState TestBlock ValuesMK
-> [TestTx]
-> TickedLedgerState TestBlock ValuesMK
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl
                  (\TickedLedgerState TestBlock ValuesMK
st TestTx
tx -> Except TestTxError (TickedLedgerState TestBlock ValuesMK)
-> TickedLedgerState TestBlock ValuesMK
forall a. HasCallStack => Except TestTxError a -> a
mustBeValid (LedgerConfig TestBlock
-> WhetherToIntervene
-> SlotNo
-> TestTx
-> TickedLedgerState TestBlock ValuesMK
-> Except TestTxError (TickedLedgerState TestBlock ValuesMK)
applyTxToTickedLedger LedgerConfig TestBlock
testCfg WhetherToIntervene
DoNotIntervene SlotNo
slot TestTx
tx TickedLedgerState TestBlock ValuesMK
st))
                  TickedLedgerState TestBlock ValuesMK
initSt
                  [TestTx]
onChainTxs
              (RevalidateTxsResult InternalState TestBlock
is [Invalidated TestBlock]
removed, DoneType
done) =
                Int
-> TickedLedgerState TestBlock ValuesMK
-> SlotNo
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> (RevalidateTxsResult TestBlock, DoneType)
runRevalidate Int
stepSize TickedLedgerState TestBlock ValuesMK
newTipSt SlotNo
slot [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
tickets
           in [Property] -> Property
forall prop. Testable prop => [prop] -> Property
conjoin
                [ TestName -> Property -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample TestName
"exactly the on-chain txs are removed" (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                    [TxId TestTx] -> [TxId TestTx]
forall a. Ord a => [a] -> [a]
sort ((Invalidated TestBlock -> TxId TestTx)
-> [Invalidated TestBlock] -> [TxId TestTx]
forall a b. (a -> b) -> [a] -> [b]
map (TestTx -> TxId TestTx
forall tx. HasTxId tx => tx -> TxId tx
txId (TestTx -> TxId TestTx)
-> (Invalidated TestBlock -> TestTx)
-> Invalidated TestBlock
-> TxId TestTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Invalidated TestBlock -> TestTx
forall blk. Invalidated blk -> GenTx blk
getInvalidated) [Invalidated TestBlock]
removed) [TxId TestTx] -> [TxId TestTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== [TxId TestTx] -> [TxId TestTx]
forall a. Ord a => [a] -> [a]
sort ((TestTx -> TxId TestTx) -> [TestTx] -> [TxId TestTx]
forall a b. (a -> b) -> [a] -> [b]
map TestTx -> TxId TestTx
forall tx. HasTxId tx => tx -> TxId tx
txId [TestTx]
onChainTxs)
                , TestName -> Property -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample TestName
"all off-chain txs survive, in order" (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                    [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> [TxId TestTx]
ticketIds (TxSeq (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
forall sz tx. TxSeq sz tx -> [TxTicket sz tx]
TxSeq.toList (InternalState TestBlock
-> TxSeq
     (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
forall blk.
InternalState blk
-> TxSeq (MempoolMeasure blk) (ValidatedTxWithDiffs blk)
isTxs InternalState TestBlock
is)) [TxId TestTx] -> [TxId TestTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== (TestTx -> TxId TestTx) -> [TestTx] -> [TxId TestTx]
forall a b. (a -> b) -> [a] -> [b]
map TestTx -> TxId TestTx
forall tx. HasTxId tx => tx -> TxId tx
txId [TestTx]
offChainTxs
                , TestName -> Property -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample TestName
"the loop must stop because it ran out of txs" (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                    DoneType
done DoneType -> DoneType -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== DoneType
EndReached
                ]

-- | On the cached paths -- same tip and slot, and same tip at a later slot --
-- 'getSnapshotFor' must return the full mempool without a single LedgerDB
-- read.
prop_getSnapshotForCachedPaths :: Property
prop_getSnapshotForCachedPaths :: Property
prop_getSnapshotForCachedPaths =
  Gen (LedgerState TestBlock ValuesMK)
-> (LedgerState TestBlock ValuesMK -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen (LedgerState TestBlock ValuesMK)
genBaseState ((LedgerState TestBlock ValuesMK -> Property) -> Property)
-> (LedgerState TestBlock ValuesMK -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \LedgerState TestBlock ValuesMK
baseSt ->
    Gen [TestTx] -> ([TestTx] -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind ((Int, Int) -> LedgerState TestBlock ValuesMK -> Gen [TestTx]
genLatticeOn (Int
1, Int
5) LedgerState TestBlock ValuesMK
baseSt) (([TestTx] -> Property) -> Property)
-> ([TestTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[TestTx]
txs ->
      (forall s. IOSim s Property) -> Property
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ((forall s. IOSim s Property) -> Property)
-> (forall s. IOSim s Property) -> Property
forall a b. (a -> b) -> a -> b
$
        LedgerState TestBlock ValuesMK
-> [TestTx]
-> (Mempool (IOSim s) TestBlock
    -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
-> IOSim s Property
forall (m :: * -> *).
(IOLike m, MonadTimer m) =>
LedgerState TestBlock ValuesMK
-> [TestTx]
-> (Mempool m TestBlock
    -> m [TraceEventMempool TestBlock] -> m Property)
-> m Property
withMempool LedgerState TestBlock ValuesMK
baseSt [TestTx]
txs ((Mempool (IOSim s) TestBlock
  -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
 -> IOSim s Property)
-> (Mempool (IOSim s) TestBlock
    -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
-> IOSim s Property
forall a b. (a -> b) -> a -> b
$ \Mempool (IOSim s) TestBlock
mempool IOSim s [TraceEventMempool TestBlock]
getEvents -> do
          varReads <- Int -> IOSim s (StrictTVar (IOSim s) Int)
forall (m :: * -> *) a. MonadSTM m => a -> m (StrictTVar m a)
uncheckedNewTVarM (Int
0 :: Int)
          let snapSlot = LedgerState TestBlock ValuesMK -> SlotNo
tipSuccSlot LedgerState TestBlock ValuesMK
baseSt
              countingReads LedgerTables TestBlock KeysMK
keys = do
                STM (IOSim s) () -> IOSim s ()
forall a. HasCallStack => STM (IOSim s) a -> IOSim s a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM (IOSim s) () -> IOSim s ()) -> STM (IOSim s) () -> IOSim s ()
forall a b. (a -> b) -> a -> b
$ StrictTVar (IOSim s) Int -> (Int -> Int) -> STM (IOSim s) ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> (a -> a) -> STM m ()
modifyTVar StrictTVar (IOSim s) Int
varReads (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
                LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> IOSim s (LedgerTables TestBlock ValuesMK)
forall (m :: * -> *).
Monad m =>
LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
resolverFromTables (LedgerState TestBlock ValuesMK -> LedgerTables TestBlock ValuesMK
forall (mk :: MapKind).
(CanMapMK mk, CanMapKeysMK mk, ZeroableMK mk) =>
LedgerState TestBlock mk -> LedgerTables TestBlock mk
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
(HasLedgerTables l blk, CanMapMK mk, CanMapKeysMK mk,
 ZeroableMK mk) =>
l blk mk -> LedgerTables blk mk
projectLedgerTables LedgerState TestBlock ValuesMK
baseSt) LedgerTables TestBlock KeysMK
keys
          snapSame <- getSnapshotFor mempool snapSlot (tickTo snapSlot baseSt) countingReads
          snapNext <-
            getSnapshotFor mempool (succ snapSlot) (tickTo (succ snapSlot) baseSt) countingReads
          outcomes <- snapshotOutcomes <$> getEvents
          nReads <- atomically $ readTVar varReads
          pure $
            conjoin
              [ counterexample "same tip and slot: full snapshot" $
                  snapshotTxIds snapSame === map txId txs
              , counterexample "same tip, later slot: full snapshot" $
                  snapshotTxIds snapNext === map txId txs
              , counterexample "the cached paths must not read the LedgerDB" $
                  nReads === 0
              , counterexample "first snapshot cached, second computed to completion" $
                  case outcomes of
                    [Maybe (Int, DoneType)
Nothing, Just (Int
_, DoneType
EndReached)] -> Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True
                    [Maybe (Int, DoneType)]
other -> TestName -> Bool -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample ([Maybe (Int, DoneType)] -> TestName
forall a. Show a => a -> TestName
show [Maybe (Int, DoneType)]
other) Bool
False
              ]

-- | At a new tip whose block contains a prefix of the mempool's transactions,
-- 'getSnapshotFor' must drop exactly the included transactions and keep the
-- rest, in order (see 'prop_txChainSurvivesNewTip' for the seed-only scenario
-- against the bare loop).
--
-- The lattice is flattened in dependency order — every parent precedes all of
-- its descendants — so any prefix of it is closed under dependencies and
-- hence forms a valid block.
--
-- The lattice is at least @'snapshotTxsPerStep' + 1@ transactions deep, so the
-- reapplication always spans multiple steps, with surviving transactions
-- depending on on-chain and in-mempool outputs across step boundaries.
prop_getSnapshotForNewTip :: Property
prop_getSnapshotForNewTip :: Property
prop_getSnapshotForNewTip =
  Gen (LedgerState TestBlock ValuesMK)
-> (LedgerState TestBlock ValuesMK -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen (LedgerState TestBlock ValuesMK)
genBaseState ((LedgerState TestBlock ValuesMK -> Property) -> Property)
-> (LedgerState TestBlock ValuesMK -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \LedgerState TestBlock ValuesMK
baseSt ->
    Gen [TestTx] -> ([TestTx] -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind ((Int, Int) -> LedgerState TestBlock ValuesMK -> Gen [TestTx]
genLatticeOn (Int
snapshotTxsPerStep, Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
snapshotTxsPerStep) LedgerState TestBlock ValuesMK
baseSt) (([TestTx] -> Property) -> Property)
-> ([TestTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[TestTx]
txs ->
      Gen Int -> (Int -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll ((Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
1, [TestTx] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TestTx]
txs)) ((Int -> Property) -> Property) -> (Int -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \Int
nOnChain ->
        (forall s. IOSim s Property) -> Property
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ((forall s. IOSim s Property) -> Property)
-> (forall s. IOSim s Property) -> Property
forall a b. (a -> b) -> a -> b
$
          LedgerState TestBlock ValuesMK
-> [TestTx]
-> (Mempool (IOSim s) TestBlock
    -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
-> IOSim s Property
forall (m :: * -> *).
(IOLike m, MonadTimer m) =>
LedgerState TestBlock ValuesMK
-> [TestTx]
-> (Mempool m TestBlock
    -> m [TraceEventMempool TestBlock] -> m Property)
-> m Property
withMempool LedgerState TestBlock ValuesMK
baseSt [TestTx]
txs ((Mempool (IOSim s) TestBlock
  -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
 -> IOSim s Property)
-> (Mempool (IOSim s) TestBlock
    -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
-> IOSim s Property
forall a b. (a -> b) -> a -> b
$ \Mempool (IOSim s) TestBlock
mempool IOSim s [TraceEventMempool TestBlock]
getEvents -> do
            -- The new tip: a block containing the first 'nOnChain' of the
            -- mempool's transactions. Built with 'applyTxToLedger' (not
            -- 'applyTxToTickedLedger') so the tip point changes and
            -- 'getSnapshotFor' takes the different-tip path.
            let ([TestTx]
onChainTxs, [TestTx]
offChainTxs) = Int -> [TestTx] -> ([TestTx], [TestTx])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
nOnChain [TestTx]
txs
                newTipSt :: LedgerState TestBlock ValuesMK
newTipSt =
                  (LedgerState TestBlock ValuesMK
 -> TestTx -> LedgerState TestBlock ValuesMK)
-> LedgerState TestBlock ValuesMK
-> [TestTx]
-> LedgerState TestBlock ValuesMK
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl
                    (\LedgerState TestBlock ValuesMK
st TestTx
tx -> Except TestTxError (LedgerState TestBlock ValuesMK)
-> LedgerState TestBlock ValuesMK
forall a. HasCallStack => Except TestTxError a -> a
mustBeValid (LedgerConfig TestBlock
-> LedgerState TestBlock ValuesMK
-> TestTx
-> Except TestTxError (LedgerState TestBlock ValuesMK)
applyTxToLedger LedgerConfig TestBlock
testCfg LedgerState TestBlock ValuesMK
st TestTx
tx))
                    LedgerState TestBlock ValuesMK
baseSt
                    [TestTx]
onChainTxs
                snapSlot :: SlotNo
snapSlot = LedgerState TestBlock ValuesMK -> SlotNo
tipSuccSlot LedgerState TestBlock ValuesMK
newTipSt
            snap <-
              Mempool (IOSim s) TestBlock
-> SlotNo
-> Ticked LedgerState TestBlock DiffMK
-> (LedgerTables TestBlock KeysMK
    -> IOSim s (LedgerTables TestBlock ValuesMK))
-> IOSim s (MempoolSnapshot TestBlock)
forall (m :: * -> *) blk.
Mempool m blk
-> SlotNo
-> TickedLedgerState blk DiffMK
-> (LedgerTables blk KeysMK -> m (LedgerTables blk ValuesMK))
-> m (MempoolSnapshot blk)
getSnapshotFor
                Mempool (IOSim s) TestBlock
mempool
                SlotNo
snapSlot
                (SlotNo
-> LedgerState TestBlock ValuesMK
-> Ticked LedgerState TestBlock DiffMK
tickTo SlotNo
snapSlot LedgerState TestBlock ValuesMK
newTipSt)
                (LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> IOSim s (LedgerTables TestBlock ValuesMK)
forall (m :: * -> *).
Monad m =>
LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
resolverFromTables (LedgerState TestBlock ValuesMK -> LedgerTables TestBlock ValuesMK
forall (mk :: MapKind).
(CanMapMK mk, CanMapKeysMK mk, ZeroableMK mk) =>
LedgerState TestBlock mk -> LedgerTables TestBlock mk
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
(HasLedgerTables l blk, CanMapMK mk, CanMapKeysMK mk,
 ZeroableMK mk) =>
l blk mk -> LedgerTables blk mk
projectLedgerTables LedgerState TestBlock ValuesMK
newTipSt))
            outcomes <- snapshotOutcomes <$> getEvents
            pure $ case outcomes of
              [Just (Int
nSteps, DoneType
EndReached)] ->
                TestName -> [TestName] -> Property -> Property
forall prop.
Testable prop =>
TestName -> [TestName] -> prop -> Property
tabulate TestName
"getSnapshotFor steps" [Int -> TestName
forall a. Show a => a -> TestName
show Int
nSteps] (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                  TestName -> Property -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample TestName
"the on-chain prefix is dropped, the rest survive in order" (Property -> Property) -> Property -> Property
forall a b. (a -> b) -> a -> b
$
                    MempoolSnapshot TestBlock -> [TxId TestTx]
snapshotTxIds MempoolSnapshot TestBlock
snap [TxId TestTx] -> [TxId TestTx] -> Property
forall a. (Eq a, Show a) => a -> a -> Property
=== (TestTx -> TxId TestTx) -> [TestTx] -> [TxId TestTx]
forall a b. (a -> b) -> [a] -> [b]
map TestTx -> TxId TestTx
forall tx. HasTxId tx => tx -> TxId tx
txId [TestTx]
offChainTxs
              [Maybe (Int, DoneType)]
other ->
                TestName -> Bool -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample
                  (TestName
"expected exactly one snapshot computed to completion, got: " TestName -> TestName -> TestName
forall a. Semigroup a => a -> a -> a
<> [Maybe (Int, DoneType)] -> TestName
forall a. Show a => a -> TestName
show [Maybe (Int, DoneType)]
other)
                  Bool
False

-- | With a LedgerDB read that takes 0.06s (simulated) per step and the 0.1s
-- default time limit, 'getSnapshotFor' deterministically performs exactly two
-- steps and returns the corresponding prefix of the mempool.
prop_getSnapshotForTimeout :: Property
prop_getSnapshotForTimeout :: Property
prop_getSnapshotForTimeout =
  Gen (LedgerState TestBlock ValuesMK)
-> (LedgerState TestBlock ValuesMK -> Property) -> Property
forall a prop.
(Show a, Testable prop) =>
Gen a -> (a -> prop) -> Property
forAll Gen (LedgerState TestBlock ValuesMK)
genBaseState ((LedgerState TestBlock ValuesMK -> Property) -> Property)
-> (LedgerState TestBlock ValuesMK -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \LedgerState TestBlock ValuesMK
baseSt ->
    Gen [TestTx] -> ([TestTx] -> Property) -> Property
forall prop a. Testable prop => Gen a -> (a -> prop) -> Property
forAllBlind (LedgerState TestBlock ValuesMK -> Gen [TestTx]
genLongTxChain LedgerState TestBlock ValuesMK
baseSt) (([TestTx] -> Property) -> Property)
-> ([TestTx] -> Property) -> Property
forall a b. (a -> b) -> a -> b
$ \[TestTx]
txs ->
      (forall s. IOSim s Property) -> Property
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ((forall s. IOSim s Property) -> Property)
-> (forall s. IOSim s Property) -> Property
forall a b. (a -> b) -> a -> b
$
        LedgerState TestBlock ValuesMK
-> [TestTx]
-> (Mempool (IOSim s) TestBlock
    -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
-> IOSim s Property
forall (m :: * -> *).
(IOLike m, MonadTimer m) =>
LedgerState TestBlock ValuesMK
-> [TestTx]
-> (Mempool m TestBlock
    -> m [TraceEventMempool TestBlock] -> m Property)
-> m Property
withMempool LedgerState TestBlock ValuesMK
baseSt [TestTx]
txs ((Mempool (IOSim s) TestBlock
  -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
 -> IOSim s Property)
-> (Mempool (IOSim s) TestBlock
    -> IOSim s [TraceEventMempool TestBlock] -> IOSim s Property)
-> IOSim s Property
forall a b. (a -> b) -> a -> b
$ \Mempool (IOSim s) TestBlock
mempool IOSim s [TraceEventMempool TestBlock]
getEvents -> do
          let newTipSt :: LedgerState TestBlock ValuesMK
newTipSt = LedgerState TestBlock ValuesMK -> LedgerState TestBlock ValuesMK
bumpTip LedgerState TestBlock ValuesMK
baseSt
              snapSlot :: SlotNo
snapSlot = LedgerState TestBlock ValuesMK -> SlotNo
tipSuccSlot LedgerState TestBlock ValuesMK
newTipSt
              slowReads :: LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
slowReads LedgerTables TestBlock KeysMK
keys = do
                DiffTime -> m ()
forall (m :: * -> *). MonadDelay m => DiffTime -> m ()
threadDelay DiffTime
0.06
                LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
forall (m :: * -> *).
Monad m =>
LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
resolverFromTables (LedgerState TestBlock ValuesMK -> LedgerTables TestBlock ValuesMK
forall (mk :: MapKind).
(CanMapMK mk, CanMapKeysMK mk, ZeroableMK mk) =>
LedgerState TestBlock mk -> LedgerTables TestBlock mk
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
(HasLedgerTables l blk, CanMapMK mk, CanMapKeysMK mk,
 ZeroableMK mk) =>
l blk mk -> LedgerTables blk mk
projectLedgerTables LedgerState TestBlock ValuesMK
newTipSt) LedgerTables TestBlock KeysMK
keys
          snap <- Mempool (IOSim s) TestBlock
-> SlotNo
-> Ticked LedgerState TestBlock DiffMK
-> (LedgerTables TestBlock KeysMK
    -> IOSim s (LedgerTables TestBlock ValuesMK))
-> IOSim s (MempoolSnapshot TestBlock)
forall (m :: * -> *) blk.
Mempool m blk
-> SlotNo
-> TickedLedgerState blk DiffMK
-> (LedgerTables blk KeysMK -> m (LedgerTables blk ValuesMK))
-> m (MempoolSnapshot blk)
getSnapshotFor Mempool (IOSim s) TestBlock
mempool SlotNo
snapSlot (SlotNo
-> LedgerState TestBlock ValuesMK
-> Ticked LedgerState TestBlock DiffMK
tickTo SlotNo
snapSlot LedgerState TestBlock ValuesMK
newTipSt) LedgerTables TestBlock KeysMK
-> IOSim s (LedgerTables TestBlock ValuesMK)
forall {m :: * -> *}.
MonadDelay m =>
LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
slowReads
          outcomes <- snapshotOutcomes <$> getEvents
          pure $
            conjoin
              [ counterexample "the snapshot is the prefix reached before the time limit" $
                  snapshotTxIds snap === map txId (take (2 * snapshotTxsPerStep) txs)
              , counterexample "the trace records two steps and TimeoutReached" $
                  case outcomes of
                    [Just (Int
2, DoneType
TimeoutReached)] -> Bool -> Property
forall prop. Testable prop => prop -> Property
property Bool
True
                    [Maybe (Int, DoneType)]
other -> TestName -> Bool -> Property
forall prop. Testable prop => TestName -> prop -> Property
counterexample ([Maybe (Int, DoneType)] -> TestName
forall a. Show a => a -> TestName
show [Maybe (Int, DoneType)]
other) Bool
False
              ]

-------------------------------------------------------------------------------
-- Infrastructure
-------------------------------------------------------------------------------

testCfg :: LedgerConfig TestBlock
testCfg :: LedgerConfig TestBlock
testCfg = LedgerConfig TestBlock
testLedgerConfigNoSizeLimits

-- | A budget large enough to never fire in these tests (IOSim time only
-- advances on explicit delays, so the loop is effectively unbounded).
unlimitedBudget :: DiffTime
unlimitedBudget :: DiffTime
unlimitedBudget = DiffTime
1_000_000

-- | A connected lattice of dependent transactions (seed first), valid in
-- order against the given ticked state, with the depth drawn from the given
-- range. Every layer holds between 1 and 5 transactions, so the lattice has
-- between @depth + 1@ and @5 * depth + 1@ transactions — pick the lower bound
-- of the range to guarantee a transaction count.
genLatticeTxs ::
  (Int, Int) ->
  SlotNo ->
  TickedLedgerState TestBlock ValuesMK ->
  Gen [TestTx]
genLatticeTxs :: (Int, Int)
-> SlotNo -> TickedLedgerState TestBlock ValuesMK -> Gen [TestTx]
genLatticeTxs (Int, Int)
depthRange SlotNo
slot TickedLedgerState TestBlock ValuesMK
tickedSt = do
  depth <- (Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int, Int)
depthRange
  (txSeed, innerTxs) <- genConnectedTxLattice slot depth tickedSt
  pure (txSeed : innerTxs)

-- | Apply one valid tx against a ticked-values state; return a ticket and the
-- next state.
buildTicket ::
  SlotNo ->
  TestTx ->
  TxSeq.TicketNo ->
  TickedLedgerState TestBlock ValuesMK ->
  ( TxTicket (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
  , TickedLedgerState TestBlock ValuesMK
  )
buildTicket :: SlotNo
-> TestTx
-> TicketNo
-> TickedLedgerState TestBlock ValuesMK
-> (TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock),
    TickedLedgerState TestBlock ValuesMK)
buildTicket SlotNo
slot TestTx
tx TicketNo
ticketNo TickedLedgerState TestBlock ValuesMK
st =
  case Except
  (MockError TestBlock)
  (Ticked LedgerState TestBlock DiffMK, Validated TestTx)
-> Either
     (MockError TestBlock)
     (Ticked LedgerState TestBlock DiffMK, Validated TestTx)
forall e a. Except e a -> Either e a
runExcept (LedgerConfig TestBlock
-> WhetherToIntervene
-> SlotNo
-> TestTx
-> TickedLedgerState TestBlock ValuesMK
-> Except
     TestTxError (Ticked LedgerState TestBlock DiffMK, Validated TestTx)
forall blk.
LedgerSupportsMempool blk =>
LedgerConfig blk
-> WhetherToIntervene
-> SlotNo
-> GenTx blk
-> TickedLedgerState blk ValuesMK
-> Except
     (ApplyTxErr blk)
     (TickedLedgerState blk DiffMK, Validated (GenTx blk))
applyTx LedgerConfig TestBlock
testCfg WhetherToIntervene
DoNotIntervene SlotNo
slot TestTx
tx TickedLedgerState TestBlock ValuesMK
st) of
    Left MockError TestBlock
err -> TestName
-> (TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock),
    TickedLedgerState TestBlock ValuesMK)
forall a. HasCallStack => TestName -> a
error (TestName
 -> (TxTicket
       (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock),
     TickedLedgerState TestBlock ValuesMK))
-> TestName
-> (TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock),
    TickedLedgerState TestBlock ValuesMK)
forall a b. (a -> b) -> a -> b
$ TestName
"buildTicket: " TestName -> TestName -> TestName
forall a. [a] -> [a] -> [a]
++ MockError TestBlock -> TestName
forall a. Show a => a -> TestName
show MockError TestBlock
err
    Right (Ticked LedgerState TestBlock DiffMK
stDiff, Validated TestTx
vtx) ->
      ( TxTicket
          { txTicketTx :: ValidatedTxWithDiffs TestBlock
txTicketTx = Validated TestTx
-> LedgerTables TestBlock DiffMK -> ValidatedTxWithDiffs TestBlock
forall blk.
Validated (GenTx blk)
-> LedgerTables blk DiffMK -> ValidatedTxWithDiffs blk
ValidatedTxWithDiffs Validated TestTx
vtx (Ticked LedgerState TestBlock DiffMK
-> LedgerTables TestBlock DiffMK
forall (mk :: MapKind).
(CanMapMK mk, CanMapKeysMK mk, ZeroableMK mk) =>
Ticked LedgerState TestBlock mk -> LedgerTables TestBlock mk
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
(HasLedgerTables l blk, CanMapMK mk, CanMapKeysMK mk,
 ZeroableMK mk) =>
l blk mk -> LedgerTables blk mk
projectLedgerTables Ticked LedgerState TestBlock DiffMK
stDiff)
          , txTicketNo :: TicketNo
txTicketNo = TicketNo
ticketNo
          , txTicketSize :: MempoolMeasure TestBlock
txTicketSize = MempoolMeasure TestBlock
forall a. Measure a => a
Measure.zero
          }
      , TickedLedgerState TestBlock ValuesMK
-> Ticked LedgerState TestBlock DiffMK
-> TickedLedgerState TestBlock ValuesMK
forall (l :: * -> MapKind -> *) blk (l' :: * -> MapKind -> *).
(HasLedgerTables l blk, HasLedgerTables l' blk) =>
l blk ValuesMK -> l' blk DiffMK -> l' blk ValuesMK
applyDiffs TickedLedgerState TestBlock ValuesMK
st Ticked LedgerState TestBlock DiffMK
stDiff
      )

-- | Build tickets for a list of txs, threading the ticked state and assigning
-- ticket numbers starting at 1.
buildTicketList ::
  SlotNo ->
  [TestTx] ->
  TickedLedgerState TestBlock ValuesMK ->
  [TxTicket (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
buildTicketList :: SlotNo
-> [TestTx]
-> TickedLedgerState TestBlock ValuesMK
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
buildTicketList SlotNo
slot [TestTx]
txs TickedLedgerState TestBlock ValuesMK
st0 =
  ([TxTicket
    (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)],
 TickedLedgerState TestBlock ValuesMK)
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
forall a b. (a, b) -> a
fst (([TxTicket
     (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)],
  TickedLedgerState TestBlock ValuesMK)
 -> [TxTicket
       (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)])
-> ([TxTicket
       (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)],
    TickedLedgerState TestBlock ValuesMK)
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
forall a b. (a -> b) -> a -> b
$
    (([TxTicket
     (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)],
  TickedLedgerState TestBlock ValuesMK)
 -> (Word64, TestTx)
 -> ([TxTicket
        (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)],
     TickedLedgerState TestBlock ValuesMK))
-> ([TxTicket
       (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)],
    TickedLedgerState TestBlock ValuesMK)
-> [(Word64, TestTx)]
-> ([TxTicket
       (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)],
    TickedLedgerState TestBlock ValuesMK)
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl
      ( \([TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
acc, TickedLedgerState TestBlock ValuesMK
st) (Word64
n, TestTx
tx) ->
          let (TxTicket
  (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
tkt, TickedLedgerState TestBlock ValuesMK
st') = SlotNo
-> TestTx
-> TicketNo
-> TickedLedgerState TestBlock ValuesMK
-> (TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock),
    TickedLedgerState TestBlock ValuesMK)
buildTicket SlotNo
slot TestTx
tx (Word64 -> TicketNo
TxSeq.TicketNo Word64
n) TickedLedgerState TestBlock ValuesMK
st
           in ([TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
acc [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
forall a. [a] -> [a] -> [a]
++ [TxTicket
  (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
tkt], TickedLedgerState TestBlock ValuesMK
st')
      )
      ([], TickedLedgerState TestBlock ValuesMK
st0)
      ([Word64] -> [TestTx] -> [(Word64, TestTx)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Word64
1 :: Word64 ..] [TestTx]
txs)

-- | Resolve keys against the given UTxO table, like a LedgerDB read would.
resolverFromTables ::
  Monad m =>
  LedgerTables TestBlock ValuesMK ->
  LedgerTables TestBlock KeysMK ->
  m (LedgerTables TestBlock ValuesMK)
resolverFromTables :: forall (m :: * -> *).
Monad m =>
LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
resolverFromTables (LedgerTables (ValuesMK Map (TxIn TestBlock) (TxOut TestBlock)
utxos)) (LedgerTables (KeysMK Set (TxIn TestBlock)
keys)) =
  LedgerTables TestBlock ValuesMK
-> m (LedgerTables TestBlock ValuesMK)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LedgerTables TestBlock ValuesMK
 -> m (LedgerTables TestBlock ValuesMK))
-> LedgerTables TestBlock ValuesMK
-> m (LedgerTables TestBlock ValuesMK)
forall a b. (a -> b) -> a -> b
$ ValuesMK (TxIn TestBlock) (TxOut TestBlock)
-> LedgerTables TestBlock ValuesMK
forall blk (mk :: MapKind).
mk (TxIn blk) (TxOut blk) -> LedgerTables blk mk
LedgerTables (Map TxIn TxOut -> ValuesMK TxIn TxOut
forall k v. Map k v -> ValuesMK k v
ValuesMK (Map TxIn TxOut -> Set TxIn -> Map TxIn TxOut
forall k a. Ord k => Map k a -> Set k -> Map k a
Map.restrictKeys Map TxIn TxOut
Map (TxIn TestBlock) (TxOut TestBlock)
utxos Set TxIn
Set (TxIn TestBlock)
keys))

-- | Resolve keys against the UTxO set of the given ledger state, like a
-- LedgerDB read at that state would.
resolverFromState ::
  Monad m =>
  TickedLedgerState TestBlock ValuesMK ->
  LedgerTables TestBlock KeysMK ->
  m (LedgerTables TestBlock ValuesMK)
resolverFromState :: forall (m :: * -> *).
Monad m =>
TickedLedgerState TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
resolverFromState = LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
forall (m :: * -> *).
Monad m =>
LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
resolverFromTables (LedgerTables TestBlock ValuesMK
 -> LedgerTables TestBlock KeysMK
 -> m (LedgerTables TestBlock ValuesMK))
-> (TickedLedgerState TestBlock ValuesMK
    -> LedgerTables TestBlock ValuesMK)
-> TickedLedgerState TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TickedLedgerState TestBlock ValuesMK
-> LedgerTables TestBlock ValuesMK
forall (mk :: MapKind).
(CanMapMK mk, CanMapKeysMK mk, ZeroableMK mk) =>
Ticked LedgerState TestBlock mk -> LedgerTables TestBlock mk
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
(HasLedgerTables l blk, CanMapMK mk, CanMapKeysMK mk,
 ZeroableMK mk) =>
l blk mk -> LedgerTables blk mk
projectLedgerTables

ticketIds ::
  [TxTicket (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)] ->
  [TxId TestTx]
ticketIds :: [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> [TxId TestTx]
ticketIds = (TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
 -> TxId TestTx)
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> [TxId TestTx]
forall a b. (a -> b) -> [a] -> [b]
map (TestTx -> TxId TestTx
forall tx. HasTxId tx => tx -> TxId tx
txId (TestTx -> TxId TestTx)
-> (TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
    -> TestTx)
-> TxTicket
     (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
-> TxId TestTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Validated TestTx -> TestTx
forall blk.
LedgerSupportsMempool blk =>
Validated (GenTx blk) -> GenTx blk
txForgetValidated (Validated TestTx -> TestTx)
-> (TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
    -> Validated TestTx)
-> TxTicket
     (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
-> TestTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ValidatedTxWithDiffs TestBlock -> Validated TestTx
forall blk. ValidatedTxWithDiffs blk -> Validated (GenTx blk)
validatedTx (ValidatedTxWithDiffs TestBlock -> Validated TestTx)
-> (TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
    -> ValidatedTxWithDiffs TestBlock)
-> TxTicket
     (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
-> Validated TestTx
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxTicket
  (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)
-> ValidatedTxWithDiffs TestBlock
forall sz tx. TxTicket sz tx -> tx
txTicketTx)

-- | A mempool environment carrying only what 'revalidateUntilTimeout' reads:
-- the ledger config and the capacity override. Every other field is an error
-- stub.
testMempoolEnv :: Monad m => MempoolEnv m TestBlock
testMempoolEnv :: forall (m :: * -> *). Monad m => MempoolEnv m TestBlock
testMempoolEnv =
  MempoolEnv
    { mpEnvLedger :: LedgerInterface m TestBlock
mpEnvLedger = TestName -> LedgerInterface m TestBlock
forall a. HasCallStack => TestName -> a
error TestName
"testMempoolEnv: mpEnvLedger is not used"
    , mpEnvForker :: StrictMVar m (ReadOnlyForker m LedgerState TestBlock)
mpEnvForker = TestName -> StrictMVar m (ReadOnlyForker m LedgerState TestBlock)
forall a. HasCallStack => TestName -> a
error TestName
"testMempoolEnv: mpEnvForker is not used"
    , mpEnvLedgerCfg :: LedgerConfig TestBlock
mpEnvLedgerCfg = LedgerConfig TestBlock
testCfg
    , mpEnvStateVar :: StrictTMVar m (InternalState TestBlock)
mpEnvStateVar = TestName -> StrictTMVar m (InternalState TestBlock)
forall a. HasCallStack => TestName -> a
error TestName
"testMempoolEnv: mpEnvStateVar is not used"
    , mpEnvAddTxsRemoteFifo :: StrictMVar m ()
mpEnvAddTxsRemoteFifo = TestName -> StrictMVar m ()
forall a. HasCallStack => TestName -> a
error TestName
"testMempoolEnv: mpEnvAddTxsRemoteFifo is not used"
    , mpEnvAddTxsAllFifo :: StrictMVar m ()
mpEnvAddTxsAllFifo = TestName -> StrictMVar m ()
forall a. HasCallStack => TestName -> a
error TestName
"testMempoolEnv: mpEnvAddTxsAllFifo is not used"
    , mpEnvTracer :: Tracer m (TraceEventMempool TestBlock)
mpEnvTracer = Tracer m (TraceEventMempool TestBlock)
forall (m :: * -> *) a. Monad m => Tracer m a
nullTracer
    , mpEnvCapacityOverride :: MempoolCapacityBytesOverride
mpEnvCapacityOverride = MempoolCapacityBytesOverride
NoMempoolCapacityBytesOverride
    , mpEnvTimeoutConfig :: Maybe MempoolTimeoutConfig
mpEnvTimeoutConfig = Maybe MempoolTimeoutConfig
forall a. Maybe a
Nothing
    }

-- | Run 'revalidateUntilTimeout' in IOSim with an effectively unlimited
-- budget.
runRevalidate ::
  Int ->
  TickedLedgerState TestBlock ValuesMK ->
  SlotNo ->
  [TxTicket (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)] ->
  (RevalidateTxsResult TestBlock, DoneType)
runRevalidate :: Int
-> TickedLedgerState TestBlock ValuesMK
-> SlotNo
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> (RevalidateTxsResult TestBlock, DoneType)
runRevalidate Int
stepSize TickedLedgerState TestBlock ValuesMK
baseSt SlotNo
slot [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
tickets =
  (forall s. IOSim s (RevalidateTxsResult TestBlock, DoneType))
-> (RevalidateTxsResult TestBlock, DoneType)
forall a. (forall s. IOSim s a) -> a
runSimOrThrow ((forall s. IOSim s (RevalidateTxsResult TestBlock, DoneType))
 -> (RevalidateTxsResult TestBlock, DoneType))
-> (forall s. IOSim s (RevalidateTxsResult TestBlock, DoneType))
-> (RevalidateTxsResult TestBlock, DoneType)
forall a b. (a -> b) -> a -> b
$ do
    (result, _remaining, done) <-
      MempoolEnv (IOSim s) TestBlock
-> DiffTime
-> Int
-> (LedgerTables TestBlock KeysMK
    -> IOSim s (LedgerTables TestBlock ValuesMK))
-> RevalidateTxsResult TestBlock
-> [TxTicket
      (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> IOSim s (RevalidateTxsResult TestBlock, Int, DoneType)
forall blk (m :: * -> *).
(IOLike m, LedgerSupportsMempool blk, HasTxId (GenTx blk)) =>
MempoolEnv m blk
-> DiffTime
-> Int
-> (LedgerTables blk KeysMK -> m (LedgerTables blk ValuesMK))
-> RevalidateTxsResult blk
-> [TxTicket (MempoolMeasure blk) (ValidatedTxWithDiffs blk)]
-> m (RevalidateTxsResult blk, Int, DoneType)
revalidateUntilTimeout
        MempoolEnv (IOSim s) TestBlock
forall (m :: * -> *). Monad m => MempoolEnv m TestBlock
testMempoolEnv
        DiffTime
unlimitedBudget
        Int
stepSize
        (TickedLedgerState TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> IOSim s (LedgerTables TestBlock ValuesMK)
forall (m :: * -> *).
Monad m =>
TickedLedgerState TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
resolverFromState TickedLedgerState TestBlock ValuesMK
baseSt)
        RevalidateTxsResult TestBlock
seedCandidate
        [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
tickets
    pure (result, done)
 where
  seedCandidate :: RevalidateTxsResult TestBlock
seedCandidate =
    InternalState TestBlock
-> [Invalidated TestBlock] -> RevalidateTxsResult TestBlock
forall blk.
InternalState blk -> [Invalidated blk] -> RevalidateTxsResult blk
RevalidateTxsResult
      ( MempoolCapacityBytesOverride
-> TicketNo
-> LedgerConfig TestBlock
-> SlotNo
-> Ticked LedgerState TestBlock DiffMK
-> InternalState TestBlock
forall blk.
LedgerSupportsMempool blk =>
MempoolCapacityBytesOverride
-> TicketNo
-> LedgerConfig blk
-> SlotNo
-> TickedLedgerState blk DiffMK
-> InternalState blk
initInternalState
          MempoolCapacityBytesOverride
NoMempoolCapacityBytesOverride
          (Word64 -> TicketNo
TxSeq.TicketNo (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
-> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TxTicket
   (MempoolMeasure TestBlock) (ValidatedTxWithDiffs TestBlock)]
tickets)))
          LedgerConfig TestBlock
testCfg
          SlotNo
slot
          (TickedLedgerState TestBlock ValuesMK
-> Ticked LedgerState TestBlock DiffMK
forall (l :: * -> MapKind -> *) blk (any :: MapKind).
HasLedgerTables l blk =>
l blk any -> l blk DiffMK
noNewTickingDiffs TickedLedgerState TestBlock ValuesMK
baseSt)
      )
      []

-- | Base chain state for the integration properties: genesis evolved by a few
-- valid transactions, so the tip is not at 'Origin'.
genBaseState :: Gen (LedgerState TestBlock ValuesMK)
genBaseState :: Gen (LedgerState TestBlock ValuesMK)
genBaseState = do
  n <- (Int, Int) -> Gen Int
forall a. Random a => (a, a) -> Gen a
choose (Int
1, Int
5)
  (_txs, st) <- genValidTxs n testInitLedger
  pure st

-- | The slot the mempool ticks the given state to ('ForgeInUnknownSlot'): the
-- successor of the tip slot.
tipSuccSlot :: LedgerState TestBlock ValuesMK -> SlotNo
tipSuccSlot :: LedgerState TestBlock ValuesMK -> SlotNo
tipSuccSlot LedgerState TestBlock ValuesMK
st = case LedgerState TestBlock ValuesMK -> WithOrigin SlotNo
forall blk (mk :: MapKind).
UpdateLedger blk =>
LedgerState blk mk -> WithOrigin SlotNo
ledgerTipSlot LedgerState TestBlock ValuesMK
st of
  WithOrigin SlotNo
Origin -> TestName -> SlotNo
forall a. HasCallStack => TestName -> a
error TestName
"tipSuccSlot: the integration tests require a non-genesis tip"
  At SlotNo
s -> SlotNo -> SlotNo
forall a. Enum a => a -> a
succ SlotNo
s

-- | Tick the given state to the given slot, keeping its tip.
tickTo :: SlotNo -> LedgerState TestBlock ValuesMK -> TickedLedgerState TestBlock DiffMK
tickTo :: SlotNo
-> LedgerState TestBlock ValuesMK
-> Ticked LedgerState TestBlock DiffMK
tickTo SlotNo
slot LedgerState TestBlock ValuesMK
st = ComputeLedgerEvents
-> LedgerConfig TestBlock
-> SlotNo
-> LedgerState TestBlock EmptyMK
-> Ticked LedgerState TestBlock DiffMK
forall (l :: * -> MapKind -> *) blk.
IsLedger l blk =>
ComputeLedgerEvents
-> LedgerCfg l blk
-> SlotNo
-> l blk EmptyMK
-> Ticked l blk DiffMK
applyChainTick ComputeLedgerEvents
OmitLedgerEvents LedgerConfig TestBlock
testCfg SlotNo
slot (LedgerState TestBlock ValuesMK -> LedgerState TestBlock EmptyMK
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
HasLedgerTables l blk =>
l blk mk -> l blk EmptyMK
forgetLedgerTables LedgerState TestBlock ValuesMK
st)

snapshotTxIds :: MempoolSnapshot TestBlock -> [TxId TestTx]
snapshotTxIds :: MempoolSnapshot TestBlock -> [TxId TestTx]
snapshotTxIds MempoolSnapshot TestBlock
snap = [TestTx -> TxId TestTx
forall tx. HasTxId tx => tx -> TxId tx
txId (Validated TestTx -> TestTx
forall blk.
LedgerSupportsMempool blk =>
Validated (GenTx blk) -> GenTx blk
txForgetValidated Validated TestTx
vtx) | (Validated TestTx
vtx, TicketNo
_, TxMeasure TestBlock
_) <- MempoolSnapshot TestBlock
-> [(Validated TestTx, TicketNo, TxMeasure TestBlock)]
forall blk.
MempoolSnapshot blk
-> [(Validated (GenTx blk), TicketNo, TxMeasure blk)]
snapshotTxs MempoolSnapshot TestBlock
snap]

-- | The @Maybe (StepCount, DoneType)@ of each 'TraceGetSnapshotFor' event, in
-- emission order.
snapshotOutcomes :: [TraceEventMempool TestBlock] -> [Maybe (Int, DoneType)]
snapshotOutcomes :: [TraceEventMempool TestBlock] -> [Maybe (Int, DoneType)]
snapshotOutcomes [TraceEventMempool TestBlock]
events = [Maybe (Int, DoneType)
mb | TraceGetSnapshotFor DiffTime
_ SlotNo
_ ChainHash TestBlock
_ DiffTime
_ Maybe (Int, DoneType)
mb <- [TraceEventMempool TestBlock]
events]

-- | Open a real mempool over a mock LedgerDB fixed at the given base state,
-- add the given transactions (all of which must be accepted), and run the
-- continuation with the mempool and a reader for the traced events.
withMempool ::
  forall m.
  (IOLike m, MonadTimer m) =>
  LedgerState TestBlock ValuesMK ->
  [TestTx] ->
  (Mempool m TestBlock -> m [TraceEventMempool TestBlock] -> m Property) ->
  m Property
withMempool :: forall (m :: * -> *).
(IOLike m, MonadTimer m) =>
LedgerState TestBlock ValuesMK
-> [TestTx]
-> (Mempool m TestBlock
    -> m [TraceEventMempool TestBlock] -> m Property)
-> m Property
withMempool LedgerState TestBlock ValuesMK
baseSt [TestTx]
txs Mempool m TestBlock
-> m [TraceEventMempool TestBlock] -> m Property
k = do
  (tracer, getEvents) <- m (Tracer m (TraceEventMempool TestBlock),
   m [TraceEventMempool TestBlock])
forall (m :: * -> *) ev. MonadSTM m => m (Tracer m ev, m [ev])
recordingTracerTVar
  mempool <-
    openMempoolWithoutSyncThread
      ledgerInterface
      testCfg
      (MempoolCapacityBytesOverride (ByteSize32 100_000_000))
      Nothing
      tracer
  results <- addTxs mempool txs
  unless (all isMempoolTxAdded results) $
    error "withMempool: every generated transaction must be accepted"
  k mempool getEvents
 where
  ledgerInterface :: LedgerInterface m TestBlock
ledgerInterface =
    LedgerInterface
      { getCurrentLedgerState :: STM m (MempoolLedgerDBView m TestBlock)
getCurrentLedgerState =
          MempoolLedgerDBView m TestBlock
-> STM m (MempoolLedgerDBView m TestBlock)
forall a. a -> STM m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MempoolLedgerDBView m TestBlock
 -> STM m (MempoolLedgerDBView m TestBlock))
-> MempoolLedgerDBView m TestBlock
-> STM m (MempoolLedgerDBView m TestBlock)
forall a b. (a -> b) -> a -> b
$
            LedgerState TestBlock EmptyMK
-> m (Either
        GetForkerError (ReadOnlyForker m LedgerState TestBlock))
-> MempoolLedgerDBView m TestBlock
forall (m :: * -> *) blk.
LedgerState blk EmptyMK
-> m (Either GetForkerError (ReadOnlyForker m LedgerState blk))
-> MempoolLedgerDBView m blk
MempoolLedgerDBView
              (LedgerState TestBlock ValuesMK -> LedgerState TestBlock EmptyMK
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
HasLedgerTables l blk =>
l blk mk -> l blk EmptyMK
forgetLedgerTables LedgerState TestBlock ValuesMK
baseSt)
              ( Either GetForkerError (ReadOnlyForker m LedgerState TestBlock)
-> m (Either
        GetForkerError (ReadOnlyForker m LedgerState TestBlock))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either GetForkerError (ReadOnlyForker m LedgerState TestBlock)
 -> m (Either
         GetForkerError (ReadOnlyForker m LedgerState TestBlock)))
-> Either GetForkerError (ReadOnlyForker m LedgerState TestBlock)
-> m (Either
        GetForkerError (ReadOnlyForker m LedgerState TestBlock))
forall a b. (a -> b) -> a -> b
$
                  ReadOnlyForker m LedgerState TestBlock
-> Either GetForkerError (ReadOnlyForker m LedgerState TestBlock)
forall a b. b -> Either a b
Right (ReadOnlyForker m LedgerState TestBlock
 -> Either GetForkerError (ReadOnlyForker m LedgerState TestBlock))
-> ReadOnlyForker m LedgerState TestBlock
-> Either GetForkerError (ReadOnlyForker m LedgerState TestBlock)
forall a b. (a -> b) -> a -> b
$
                    ReadOnlyForker
                      { roforkerClose :: m ()
roforkerClose = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
                      , roforkerReadTables :: LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
roforkerReadTables = LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
forall (m :: * -> *).
Monad m =>
LedgerTables TestBlock ValuesMK
-> LedgerTables TestBlock KeysMK
-> m (LedgerTables TestBlock ValuesMK)
resolverFromTables (LedgerState TestBlock ValuesMK -> LedgerTables TestBlock ValuesMK
forall (mk :: MapKind).
(CanMapMK mk, CanMapKeysMK mk, ZeroableMK mk) =>
LedgerState TestBlock mk -> LedgerTables TestBlock mk
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
(HasLedgerTables l blk, CanMapMK mk, CanMapKeysMK mk,
 ZeroableMK mk) =>
l blk mk -> LedgerTables blk mk
projectLedgerTables LedgerState TestBlock ValuesMK
baseSt)
                      , roforkerRangeReadTables :: RangeQueryPrevious TestBlock
-> m (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock))
roforkerRangeReadTables = m (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock))
-> RangeQueryPrevious TestBlock
-> m (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock))
forall a b. a -> b -> a
const (m (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock))
 -> RangeQueryPrevious TestBlock
 -> m (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock)))
-> m (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock))
-> RangeQueryPrevious TestBlock
-> m (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock))
forall a b. (a -> b) -> a -> b
$ (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock))
-> m (LedgerTables TestBlock ValuesMK, Maybe (TxIn TestBlock))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LedgerTables TestBlock ValuesMK
forall (mk :: MapKind) blk.
(ZeroableMK mk, LedgerTableConstraints blk) =>
LedgerTables blk mk
emptyLedgerTables, Maybe TxIn
Maybe (TxIn TestBlock)
forall a. Maybe a
Nothing)
                      , roforkerGetLedgerState :: STM m (LedgerState TestBlock EmptyMK)
roforkerGetLedgerState = LedgerState TestBlock EmptyMK
-> STM m (LedgerState TestBlock EmptyMK)
forall a. a -> STM m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LedgerState TestBlock EmptyMK
 -> STM m (LedgerState TestBlock EmptyMK))
-> LedgerState TestBlock EmptyMK
-> STM m (LedgerState TestBlock EmptyMK)
forall a b. (a -> b) -> a -> b
$ LedgerState TestBlock ValuesMK -> LedgerState TestBlock EmptyMK
forall (l :: * -> MapKind -> *) blk (mk :: MapKind).
HasLedgerTables l blk =>
l blk mk -> l blk EmptyMK
forgetLedgerTables LedgerState TestBlock ValuesMK
baseSt
                      , roforkerReadStatistics :: m Statistics
roforkerReadStatistics = Statistics -> m Statistics
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Statistics -> m Statistics) -> Statistics -> m Statistics
forall a b. (a -> b) -> a -> b
$ Int -> Statistics
Statistics Int
0
                      }
              )
      }

-- | Like 'genLatticeTxs', but against an unticked base state (as the
-- integration properties hold it).
genLatticeOn :: (Int, Int) -> LedgerState TestBlock ValuesMK -> Gen [TestTx]
genLatticeOn :: (Int, Int) -> LedgerState TestBlock ValuesMK -> Gen [TestTx]
genLatticeOn (Int, Int)
depthRange LedgerState TestBlock ValuesMK
baseSt =
  (Int, Int)
-> SlotNo -> TickedLedgerState TestBlock ValuesMK -> Gen [TestTx]
genLatticeTxs (Int, Int)
depthRange SlotNo
slot (LedgerState TestBlock ValuesMK
-> Ticked LedgerState TestBlock DiffMK
-> TickedLedgerState TestBlock ValuesMK
forall (l :: * -> MapKind -> *) blk (l' :: * -> MapKind -> *).
(HasLedgerTables l blk, HasLedgerTables l' blk) =>
l blk ValuesMK -> l' blk DiffMK -> l' blk ValuesMK
applyDiffs LedgerState TestBlock ValuesMK
baseSt (SlotNo
-> LedgerState TestBlock ValuesMK
-> Ticked LedgerState TestBlock DiffMK
tickTo SlotNo
slot LedgerState TestBlock ValuesMK
baseSt))
 where
  slot :: SlotNo
slot = LedgerState TestBlock ValuesMK -> SlotNo
tipSuccSlot LedgerState TestBlock ValuesMK
baseSt

-- | A sequentially valid tx chain long enough to span more than two
-- reapplication steps.
genLongTxChain :: LedgerState TestBlock ValuesMK -> Gen [TestTx]
genLongTxChain :: LedgerState TestBlock ValuesMK -> Gen [TestTx]
genLongTxChain LedgerState TestBlock ValuesMK
baseSt = ([TestTx], LedgerState TestBlock ValuesMK) -> [TestTx]
forall a b. (a, b) -> a
fst (([TestTx], LedgerState TestBlock ValuesMK) -> [TestTx])
-> Gen ([TestTx], LedgerState TestBlock ValuesMK) -> Gen [TestTx]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> LedgerState TestBlock ValuesMK
-> Gen ([TestTx], LedgerState TestBlock ValuesMK)
genValidTxs (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
snapshotTxsPerStep Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
50) LedgerState TestBlock ValuesMK
baseSt