Skip to content

Commit 82392d5

Browse files
committed
Re-enable Mempool's parallel QSM test
1 parent 7fd3638 commit 82392d5

File tree

1 file changed

+42
-93
lines changed
  • ouroboros-consensus/test/consensus-test/Test/Consensus/Mempool

1 file changed

+42
-93
lines changed

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

Lines changed: 42 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import Ouroboros.Consensus.Ledger.SupportsProtocol
5252
)
5353
import Ouroboros.Consensus.Ledger.Tables.Utils
5454
import Ouroboros.Consensus.Mempool
55-
import Ouroboros.Consensus.Mempool.Impl.Common (tickLedgerState)
55+
import Ouroboros.Consensus.Mempool.Impl.Common (MempoolLedgerDBView (..), tickLedgerState)
5656
import Ouroboros.Consensus.Mempool.TxSeq
5757
import Ouroboros.Consensus.Mock.Ledger.Address
5858
import Ouroboros.Consensus.Mock.Ledger.Block
@@ -88,16 +88,6 @@ import Test.Util.ToExpr ()
8888
Datatypes
8989
-------------------------------------------------------------------------------}
9090

91-
-- | Whether the LedgerDB should be wiped out
92-
data ModifyDB = KeepDB | ClearDB deriving (Generic, ToExpr, NoThunks)
93-
94-
instance Arbitrary ModifyDB where
95-
arbitrary = elements [KeepDB, ClearDB]
96-
97-
keepsDB :: ModifyDB -> Bool
98-
keepsDB KeepDB = True
99-
keepsDB ClearDB = False
100-
10191
-- | The model
10292
data Model blk r = Model
10393
{ modelMempoolIntermediateState :: !(TickedLedgerState blk ValuesMK)
@@ -116,12 +106,8 @@ data Model blk r = Model
116106

117107
modelLedgerDBTip :: !(LedgerState blk ValuesMK)
118108
-- ^ The current tip on the ledgerdb
119-
, modelReachableStates :: !(Set (LedgerState blk ValuesMK))
120-
-- ^ The old states which are still on the LedgerDB. These should
121-
-- technically be ancestors of the tip, but for the mempool we don't care.
122-
, modelOtherStates :: !(Set (LedgerState blk ValuesMK))
123-
-- ^ States which were previously on the LedgerDB. We keep these so that
124-
-- 'ChangeLedger' does not generate a different state with the same hash.
109+
, modelLedgerDBOtherStates :: !(Set (LedgerState blk ValuesMK))
110+
-- ^ The old states which are still on the LedgerDB.
125111
}
126112

127113
-- | The commands used by QSM
@@ -152,7 +138,6 @@ data Action blk r
152138
data Event blk r
153139
= ChangeLedger
154140
!(LedgerState blk ValuesMK)
155-
!ModifyDB
156141
deriving Generic1
157142
deriving (Rank2.Functor, Rank2.Foldable, Rank2.Traversable, CommandNames)
158143

@@ -240,27 +225,23 @@ generator ma gTxs model =
240225
( getTip modelLedgerDBTip
241226
`Set.insert` Set.map
242227
getTip
243-
( modelOtherStates
244-
`Set.union` modelReachableStates
245-
)
228+
modelLedgerDBOtherStates
246229
)
247230
. getTip
248231
)
249232
]
250-
++ (if Set.null modelReachableStates then [] else [elements (Set.toList modelReachableStates)])
251-
++ (if Set.null modelOtherStates then [] else [elements (Set.toList modelOtherStates)])
233+
++ (if Set.null modelLedgerDBOtherStates then [] else [elements (Set.toList modelLedgerDBOtherStates)])
252234
)
253235
`suchThat` (not . (== (getTip modelLedgerDBTip)) . getTip)
254-
Event . ChangeLedger ls <$> arbitrary
236+
pure $ Event $ ChangeLedger ls
255237
)
256238
, (10, pure $ Action GetSnapshot)
257239
]
258240
where
259241
Model
260242
{ modelMempoolIntermediateState
261243
, modelLedgerDBTip
262-
, modelReachableStates
263-
, modelOtherStates
244+
, modelLedgerDBOtherStates
264245
} = model
265246

266247
data Response blk r
@@ -286,14 +267,13 @@ initModel ::
286267
initModel cfg capacity initialState =
287268
Model
288269
{ modelMempoolIntermediateState = ticked
289-
, modelReachableStates = Set.empty
270+
, modelLedgerDBOtherStates = Set.empty
290271
, modelLedgerDBTip = initialState
291272
, modelTxs = []
292273
, modelCurrentSize = Measure.zero
293274
, modelLastSeenTicketNo = zeroTicketNo
294275
, modelCapacity = capacity
295276
, modelConfig = cfg
296-
, modelOtherStates = Set.empty
297277
}
298278
where
299279
ticked = tick cfg initialState
@@ -306,7 +286,7 @@ mock model = \case
306286
Action (TryAddTxs _) -> pure Void
307287
Action SyncLedger -> pure Void
308288
Action GetSnapshot -> pure $ GotSnapshot $ modelTxs model
309-
Event (ChangeLedger _ _) -> pure Void
289+
Event (ChangeLedger _) -> pure Void
310290

311291
{-------------------------------------------------------------------------------
312292
Transitions
@@ -347,25 +327,17 @@ doChangeLedger ::
347327
(StandardHash blk, GetTip (LedgerState blk)) =>
348328
Model blk r ->
349329
LedgerState blk ValuesMK ->
350-
ModifyDB ->
351330
Model blk r
352-
doChangeLedger model l' b' =
331+
doChangeLedger model l' =
353332
model
354333
{ modelLedgerDBTip = l'
355-
, modelReachableStates =
356-
if keepsDB b'
357-
then l' `Set.delete` Set.insert modelLedgerDBTip modelReachableStates
358-
else Set.empty
359-
, modelOtherStates =
360-
if keepsDB b'
361-
then modelOtherStates
362-
else modelLedgerDBTip `Set.insert` (modelOtherStates `Set.union` modelReachableStates)
334+
, modelLedgerDBOtherStates =
335+
Set.insert modelLedgerDBTip modelLedgerDBOtherStates
363336
}
364337
where
365338
Model
366339
{ modelLedgerDBTip
367-
, modelReachableStates
368-
, modelOtherStates
340+
, modelLedgerDBOtherStates
369341
} = model
370342

371343
doTryAddTxs ::
@@ -381,8 +353,8 @@ doTryAddTxs model [] = model
381353
doTryAddTxs model txs =
382354
case Foldable.find
383355
((castPoint (getTip st) ==) . getTip)
384-
(Set.insert modelLedgerDBTip modelReachableStates) of
385-
Nothing -> doTryAddTxs (doSync model) txs
356+
(Set.insert modelLedgerDBTip modelLedgerDBOtherStates) of
357+
Nothing -> error "Impossible!"
386358
Just _ ->
387359
let nextTicket = succ $ modelLastSeenTicketNo model
388360
(validTxs, tk, newSize, st'') =
@@ -399,7 +371,7 @@ doTryAddTxs model txs =
399371
{ modelMempoolIntermediateState = st
400372
, modelTxs
401373
, modelCurrentSize
402-
, modelReachableStates
374+
, modelLedgerDBOtherStates
403375
, modelLedgerDBTip
404376
, modelConfig = cfg
405377
, modelCapacity
@@ -419,7 +391,7 @@ transition ::
419391
Model blk r
420392
transition model cmd resp = case (cmd, resp) of
421393
(Action (TryAddTxs txs), Void) -> doTryAddTxs model txs
422-
(Event (ChangeLedger l b), Void) -> doChangeLedger model l b
394+
(Event (ChangeLedger l), Void) -> doChangeLedger model l
423395
(Action GetSnapshot, GotSnapshot{}) -> model
424396
(Action SyncLedger, Void) -> doSync model
425397
_ ->
@@ -539,8 +511,6 @@ data MockedLedgerDB blk = MockedLedgerDB
539511
-- ^ The current LedgerDB tip
540512
, reachableTips :: !(Set (LedgerState blk ValuesMK))
541513
-- ^ States which are still reachable in the LedgerDB
542-
, otherStates :: !(Set (LedgerState blk ValuesMK))
543-
-- ^ States which are no longer reachable in the LedgerDB
544514
}
545515
deriving Generic
546516

@@ -554,23 +524,24 @@ newLedgerInterface ::
554524
LedgerState blk ValuesMK ->
555525
m (LedgerInterface m blk, StrictTVar m (MockedLedgerDB blk))
556526
newLedgerInterface initialLedger = do
557-
t <- newTVarIO $ MockedLedgerDB initialLedger Set.empty Set.empty
527+
t <- newTVarIO $ MockedLedgerDB initialLedger Set.empty
558528
pure
559529
( LedgerInterface
560530
{ getCurrentLedgerState = \_reg -> do
561531
st <- ldbTip <$> readTVar t
562-
pure
563-
( forgetLedgerTables st
564-
, pure $
565-
Right $
566-
ReadOnlyForker
567-
{ roforkerClose = pure ()
568-
, roforkerReadStatistics = pure Nothing
569-
, roforkerReadTables = pure . (projectLedgerTables st `restrictValues'`)
570-
, roforkerRangeReadTables = const $ pure emptyLedgerTables
571-
, roforkerGetLedgerState = pure $ forgetLedgerTables st
572-
}
573-
)
532+
pure $
533+
MempoolLedgerDBView
534+
(forgetLedgerTables st)
535+
( pure $
536+
Right $
537+
ReadOnlyForker
538+
{ roforkerClose = pure ()
539+
, roforkerReadStatistics = pure Nothing
540+
, roforkerReadTables = pure . (projectLedgerTables st `restrictValues'`)
541+
, roforkerRangeReadTables = const $ pure emptyLedgerTables
542+
, roforkerGetLedgerState = pure $ forgetLedgerTables st
543+
}
544+
)
574545
}
575546
, t
576547
)
@@ -619,38 +590,20 @@ semantics trcr cmd r = do
619590
mapM_ (addTx m AddTxForRemotePeer) txs
620591
pure Void
621592
Action SyncLedger -> do
622-
void $ syncWithLedger m
593+
void $ testSyncWithLedger m
623594
pure Void
624595
Action GetSnapshot -> do
625596
txs <- snapshotTxs <$> atomically (getSnapshot m)
626597
pure $ GotSnapshot [(txForgetValidated vtx, tk) | (vtx, tk, _) <- txs]
627-
Event (ChangeLedger l' newReachable) -> do
598+
Event (ChangeLedger l') -> do
628599
CT.traceWith trcr $ "ChangingLedger to " <> show (getTip l')
629600
atomically $ do
630-
MockedLedgerDB ledgerTip oldReachableTips oldUnreachableTips <- readTVar t
601+
MockedLedgerDB ledgerTip oldReachableTips <- readTVar t
631602
if getTip l' == getTip ledgerTip
632603
then
633-
if keepsDB newReachable
634-
then pure ()
635-
else
636-
let (newReachableTips, newUnreachableTips) =
637-
( Set.empty
638-
, Set.insert ledgerTip $
639-
Set.union oldUnreachableTips oldReachableTips
640-
)
641-
in writeTVar t (MockedLedgerDB l' newReachableTips newUnreachableTips)
604+
pure ()
642605
else
643-
let
644-
(newReachableTips, newUnreachableTips) =
645-
if keepsDB newReachable
646-
then (Set.insert ledgerTip oldReachableTips, oldUnreachableTips)
647-
else
648-
( Set.empty
649-
, Set.insert ledgerTip $
650-
Set.union oldUnreachableTips oldReachableTips
651-
)
652-
in
653-
writeTVar t (MockedLedgerDB l' newReachableTips newUnreachableTips)
606+
writeTVar t (MockedLedgerDB l' (Set.insert ledgerTip oldReachableTips))
654607
pure Void
655608

656609
{-------------------------------------------------------------------------------
@@ -827,14 +780,11 @@ tests =
827780
\i -> fmap (fmap fst . fst) . genTxs i
828781
, testGroup
829782
"parallel"
830-
[ -- See ouroboros-consensus#1549 for why this test is disabled.
831-
832-
-- testProperty "atomic" $
833-
-- withMaxSuccess 1000 $
834-
-- prop_mempoolParallel testLedgerConfigNoSizeLimits txMaxBytes' testInitLedger Atomic $
835-
-- \i -> fmap (fmap fst . fst) . genTxs i
836-
-- ,
837-
testProperty "non atomic" $
783+
[ testProperty "atomic" $
784+
withMaxSuccess 10000 $
785+
prop_mempoolParallel testLedgerConfigNoSizeLimits txMaxBytes' testInitLedger Atomic $
786+
\i -> fmap (fmap fst . fst) . genTxs i
787+
, testProperty "non atomic" $
838788
withMaxSuccess 10 $
839789
prop_mempoolParallel testLedgerConfigNoSizeLimits txMaxBytes' testInitLedger NonAtomic $
840790
\i -> fmap (fmap fst . fst) . genTxs i
@@ -927,11 +877,10 @@ instance ToExpr (Action TestBlock r) where
927877
toExpr GetSnapshot = App "GetSnapshot" []
928878

929879
instance ToExpr (LedgerState blk ValuesMK) => ToExpr (Event blk r) where
930-
toExpr (ChangeLedger ls b) =
880+
toExpr (ChangeLedger ls) =
931881
Rec "ChangeLedger" $
932882
TD.fromList
933883
[ ("tip", toExpr ls)
934-
, ("newFork", toExpr b)
935884
]
936885

937886
instance ToExpr (Command TestBlock r) where

0 commit comments

Comments
 (0)