@@ -35,7 +35,7 @@ module Test.Consensus.Mempool (tests) where
3535import Cardano.Binary (Encoding , toCBOR )
3636import Cardano.Crypto.Hash
3737import Control.Exception (assert )
38- import Control.Monad (foldM , forM , forM_ , void )
38+ import Control.Monad (foldM , forM , forM_ , guard , void )
3939import Control.Monad.Except (Except , runExcept )
4040import Control.Monad.IOSim (runSimOrThrow )
4141import Control.Monad.State (State , evalState , get , modify )
@@ -47,6 +47,7 @@ import Data.Map.Strict (Map)
4747import qualified Data.Map.Strict as Map
4848import Data.Maybe (mapMaybe )
4949import Data.Semigroup (stimes )
50+ import Data.Set (Set )
5051import qualified Data.Set as Set
5152import Data.Word (Word32 )
5253import GHC.Stack (HasCallStack )
@@ -65,6 +66,7 @@ import Ouroboros.Consensus.Util (repeatedly, repeatedlyM,
6566 safeMaximumOn , (.:) )
6667import Ouroboros.Consensus.Util.Condense (condense )
6768import Ouroboros.Consensus.Util.IOLike
69+ import Test.Crypto.Hash ()
6870import Test.QuickCheck hiding (elements )
6971import Test.Tasty (TestTree , testGroup )
7072import 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