Skip to content

Commit cb8d629

Browse files
committed
Mempool test: generate txs larger than the entire mempool
1 parent aa3f118 commit cb8d629

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ test-suite consensus-test
542542
base-deriving-via,
543543
cardano-binary,
544544
cardano-crypto-class,
545+
cardano-crypto-tests,
545546
cardano-slotting:{cardano-slotting, testlib},
546547
cborg,
547548
containers,

ouroboros-consensus/test/consensus-test/Test/Consensus/Mempool.hs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module Test.Consensus.Mempool (tests) where
3535
import Cardano.Binary (Encoding, toCBOR)
3636
import Cardano.Crypto.Hash
3737
import Control.Exception (assert)
38-
import Control.Monad (foldM, forM, forM_, void)
38+
import Control.Monad (foldM, forM, forM_, guard, void)
3939
import Control.Monad.Except (Except, runExcept)
4040
import Control.Monad.IOSim (runSimOrThrow)
4141
import Control.Monad.State (State, evalState, get, modify)
@@ -47,6 +47,7 @@ import Data.Map.Strict (Map)
4747
import qualified Data.Map.Strict as Map
4848
import Data.Maybe (mapMaybe)
4949
import Data.Semigroup (stimes)
50+
import Data.Set (Set)
5051
import qualified Data.Set as Set
5152
import Data.Word (Word32)
5253
import GHC.Stack (HasCallStack)
@@ -65,6 +66,7 @@ import Ouroboros.Consensus.Util (repeatedly, repeatedlyM,
6566
safeMaximumOn, (.:))
6667
import Ouroboros.Consensus.Util.Condense (condense)
6768
import Ouroboros.Consensus.Util.IOLike
69+
import Test.Crypto.Hash ()
6870
import Test.QuickCheck hiding (elements)
6971
import Test.Tasty (TestTree, testGroup)
7072
import Test.Tasty.QuickCheck (testProperty)
@@ -557,6 +559,23 @@ genInvalidTx ledgerState@(SimpleLedgerState MockState { mockUtxo = utxo }) = do
557559
tx = mkSimpleGenTx $ Tx DoNotExpire ins outs
558560
return $ assert (not (txIsValid testLedgerConfigNoSizeLimits ledgerState tx)) tx
559561

562+
-- | Generate an invalid tx that is larger than the given measure.
563+
genLargeInvalidTx :: TheMeasure -> Gen TestTx
564+
genLargeInvalidTx (IgnoringOverflow sz) = go Set.empty
565+
where
566+
go ins = case isLargeTx ins of
567+
Just tx -> pure tx
568+
Nothing -> do
569+
newTxIn <- arbitrary
570+
go (Set.insert newTxIn ins)
571+
572+
isLargeTx :: Set TxIn -> Maybe TestTx
573+
isLargeTx ins = do
574+
let outs = []
575+
tx = mkSimpleGenTx $ Tx DoNotExpire ins outs
576+
guard $ genTxSize tx > sz
577+
pure tx
578+
560579
-- | Apply a transaction to the ledger
561580
--
562581
-- We don't have blocks in this test, but transactions only. In this function
@@ -639,7 +658,24 @@ instance Arbitrary TestSetupWithTxs where
639658
then NoMempoolCapacityBytesOverride
640659
else MempoolCapacityBytesOverride $ mpCap <> newSize
641660
}
642-
return TestSetupWithTxs { testSetup = testSetup', txs }
661+
let mempoolCap :: TheMeasure
662+
mempoolCap = computeMempoolCapacity
663+
testLedgerConfigNoSizeLimits
664+
(TickedSimpleLedgerState ledger)
665+
(testMempoolCapOverride testSetup)
666+
largeInvalidTx <- genLargeInvalidTx mempoolCap
667+
let txs' = (largeInvalidTx, False) : txs
668+
-- Set the maximum tx size to the mempool capacity. This won't
669+
-- invalidate any valid tx in @txs@ as the capacity was chosen such that
670+
-- all @txs@ fit into the mempool.
671+
testSetup'' = testSetup' { testLedgerCfg =
672+
(testLedgerCfg testSetup') { simpleLedgerMockConfig =
673+
MockConfig {
674+
mockCfgMaxTxSize = Just (unIgnoringOverflow mempoolCap)
675+
}
676+
}
677+
}
678+
return TestSetupWithTxs { testSetup = testSetup'', txs = txs' }
643679

644680
shrink TestSetupWithTxs { testSetup, txs } =
645681
[ TestSetupWithTxs { testSetup = testSetup', txs }

0 commit comments

Comments
 (0)