Skip to content

Commit 0e6d25c

Browse files
committed
tx-submission: refactored test
Use `IOSim` API. `evaluateTrace` from `Test.Ouroboros.Network.LedgerPeers` has the annoying property that once the trace was evaluated in won't show the trace again, which makes it hard to work with `cabal repl`. Refactored `checkMempools` to improve readablity. Should be squashed onto `c9d45673ca New txSubmissionV2 simulation`
1 parent 5a74aea commit 0e6d25c

File tree

1 file changed

+73
-53
lines changed

1 file changed

+73
-53
lines changed

ouroboros-network/sim-tests-lib/Test/Ouroboros/Network/TxSubmission/TxSubmissionV2.hs

+73-53
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import Data.ByteString.Lazy (ByteString)
3636
import Data.ByteString.Lazy qualified as BSL
3737
import Data.Function (on)
3838
import Data.Foldable (traverse_)
39-
import Data.List (intercalate, nubBy)
39+
import Data.List (nubBy)
4040
import Data.Map.Strict (Map)
4141
import Data.Map.Strict qualified as Map
4242
import Data.Maybe (fromMaybe)
@@ -282,49 +282,58 @@ txSubmissionSimulation (TxSubmissionState state txDecisionPolicy) = do
282282
--
283283
prop_txSubmission :: TxSubmissionState -> Property
284284
prop_txSubmission st =
285-
ioProperty $ do
286-
tr' <- evaluateTrace (runSimTrace (txSubmissionSimulation st))
287-
case tr' of
288-
SimException e trace -> do
289-
return $ counterexample (intercalate "\n" $ show e : trace) False
290-
SimDeadLock trace -> do
291-
return $ counterexample (intercalate "\n" $ "Deadlock" : trace) False
292-
SimReturn (inmp, outmps) _trace -> do
293-
r <- mapM (\outmp -> do
294-
let outUniqueTxIds = nubBy (on (==) getTxId) outmp
295-
outValidTxs = filter getTxValid outmp
296-
case ( length outUniqueTxIds == length outmp
297-
, length outValidTxs == length outmp
298-
) of
299-
(True, True) ->
300-
-- If we are presented with a stream of unique txids for valid
301-
-- transactions the inbound transactions should match the outbound
302-
-- transactions exactly.
303-
return $ counterexample ("(True, True) " ++ show outmp)
304-
$ checkMempools inmp (take (length inmp) outValidTxs)
305-
306-
(True, False) ->
307-
-- If we are presented with a stream of unique txids then we should have
308-
-- fetched all valid transactions.
309-
return $ counterexample ("(True, False) " ++ show outmp)
310-
$ checkMempools inmp (take (length inmp) outValidTxs)
311-
312-
(False, True) ->
313-
-- If we are presented with a stream of valid txids then we should have
314-
-- fetched some version of those transactions.
315-
return $ counterexample ("(False, True) " ++ show outmp)
316-
$ checkMempools (map getTxId inmp)
317-
(take (length inmp)
318-
(map getTxId $ filter getTxValid outUniqueTxIds))
319-
320-
(False, False) ->
321-
-- If we are presented with a stream of valid and invalid Txs with
322-
-- duplicate txids we're content with completing the protocol
323-
-- without error.
324-
return $ property True)
325-
outmps
326-
return $ counterexample (intercalate "\n" _trace)
327-
$ conjoin r
285+
let tr = runSimTrace (txSubmissionSimulation st) in
286+
case traceResult True tr of
287+
Left e ->
288+
counterexample (show e)
289+
. counterexample (ppTrace tr)
290+
$ False
291+
Right (inmp, outmps) ->
292+
counterexample (ppTrace tr)
293+
$ conjoin (validate inmp `map` outmps)
294+
where
295+
validate :: [Tx Int] -- the inbound mempool
296+
-> [Tx Int] -- one of the outbound mempools
297+
-> Property
298+
validate inmp outmp =
299+
let outUniqueTxIds = nubBy (on (==) getTxId) outmp
300+
outValidTxs = filter getTxValid outmp
301+
in
302+
case ( length outUniqueTxIds == length outmp
303+
, length outValidTxs == length outmp
304+
) of
305+
x@(True, True) ->
306+
-- If we are presented with a stream of unique txids for valid
307+
-- transactions the inbound transactions should match the outbound
308+
-- transactions exactly.
309+
counterexample (show x)
310+
. counterexample (show inmp)
311+
. counterexample (show outmp)
312+
$ checkMempools inmp (take (length inmp) outValidTxs)
313+
314+
x@(True, False) ->
315+
-- If we are presented with a stream of unique txids then we should have
316+
-- fetched all valid transactions.
317+
counterexample (show x)
318+
. counterexample (show inmp)
319+
. counterexample (show outmp)
320+
$ checkMempools inmp (take (length inmp) outValidTxs)
321+
322+
x@(False, True) ->
323+
-- If we are presented with a stream of valid txids then we should have
324+
-- fetched some version of those transactions.
325+
counterexample (show x)
326+
. counterexample (show inmp)
327+
. counterexample (show outmp)
328+
$ checkMempools (map getTxId inmp)
329+
(take (length inmp)
330+
(map getTxId $ filter getTxValid outUniqueTxIds))
331+
332+
(False, False) ->
333+
-- If we are presented with a stream of valid and invalid Txs with
334+
-- duplicate txids we're content with completing the protocol
335+
-- without error.
336+
property True
328337

329338
-- | This test checks that all txs are downloaded from all available peers if
330339
-- available.
@@ -366,15 +375,26 @@ prop_txSubmission_inflight st@(TxSubmissionState state _) =
366375
inmp
367376
in resultRepeatedValidTxs === maxRepeatedValidTxs
368377

369-
checkMempools :: (Eq a, Show a) => [a] -> [a] -> Property
370-
checkMempools [] [] = property True
371-
checkMempools _ [] = property True
372-
checkMempools [] _ = property False
373-
checkMempools inp@(i : is) outp@(o : os) =
374-
if o == i then counterexample (show inp ++ " " ++ show outp)
375-
$ checkMempools is os
376-
else counterexample (show inp ++ " " ++ show outp)
377-
$ checkMempools is outp
378+
379+
-- | Check that the inbound mempool contains all outbound `tx`s as a proper
380+
-- subsequence. It might contain more `tx`s from other peers.
381+
--
382+
checkMempools :: Eq tx
383+
=> [tx] -- inbound mempool
384+
-> [tx] -- outbound mempool
385+
-> Bool
386+
checkMempools _ [] = True -- all outbound `tx` were found in the inbound
387+
-- mempool
388+
checkMempools [] (_:_) = False -- outbound mempool contains `tx`s which were
389+
-- not transferred to the inbound mempool
390+
checkMempools (i : is') os@(o : os')
391+
| i == o
392+
= checkMempools is' os'
393+
394+
| otherwise
395+
-- `_i` is not present in the outbound mempool, we can skip it.
396+
= checkMempools is' os
397+
378398

379399
-- | Split a list into sub list of at most `n` elements.
380400
--

0 commit comments

Comments
 (0)