-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/Genesis/Tests/CSJ.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
|
||
module Test.Consensus.Genesis.Tests.CSJ (tests) where | ||
|
||
import Control.Monad (replicateM) | ||
import Data.Functor (($>)) | ||
import Data.List (nub) | ||
import Data.Maybe (mapMaybe) | ||
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client | ||
(TraceChainSyncClientEvent (..)) | ||
import Ouroboros.Consensus.Util.Condense (PaddingDirection (..), | ||
condenseListWithPadding) | ||
import Test.Consensus.Genesis.Setup | ||
import Test.Consensus.Genesis.Tests.Uniform (genUniformSchedulePoints) | ||
import Test.Consensus.PeerSimulator.Run (SchedulerConfig (..), | ||
defaultSchedulerConfig) | ||
import Test.Consensus.PeerSimulator.StateView (StateView (..)) | ||
import Test.Consensus.PeerSimulator.Trace (TraceEvent (..)) | ||
import Test.Consensus.PointSchedule | ||
import Test.Consensus.PointSchedule.Peers (Peer (..), Peers (..), | ||
mkPeers) | ||
import Test.Consensus.PointSchedule.Shrinking (shrinkPeerSchedules) | ||
import Test.Tasty | ||
import Test.Tasty.QuickCheck | ||
import Test.Util.Orphans.IOLike () | ||
import Test.Util.TestBlock (TestBlock) | ||
|
||
tests :: TestTree | ||
tests = | ||
testGroup | ||
"CSJ" | ||
[ testProperty "\"Happy Path\" synchronous scenario only speaks to one peer" $ prop_happyPath True | ||
, testProperty "\"Happy Path\" asynchronous scenario only speaks to one peer" $ prop_happyPath False | ||
] | ||
|
||
prop_happyPath :: Bool -> Property | ||
prop_happyPath synchronized = | ||
forAllGenesisTest | ||
( do | ||
gt <- genChains $ pure 0 | ||
honest <- genHonestSchedule gt | ||
numOthers <- choose (0, 3) | ||
otherHonests <- if synchronized | ||
then pure $ replicate numOthers honest | ||
else replicateM numOthers (genHonestSchedule gt) | ||
pure $ gt $> mkPeers honest otherHonests | ||
) | ||
( defaultSchedulerConfig | ||
{ scEnableCSJ = True | ||
, scEnableLoE = True | ||
, scEnableLoP = True | ||
} | ||
) | ||
shrinkPeerSchedules | ||
( \_ StateView{svTrace} -> | ||
let | ||
headerDownloadEvents = | ||
mapMaybe | ||
(\case | ||
TraceChainSyncClientEvent pid (TraceDownloadedHeader hdr) -> Just (pid, hdr) | ||
_ -> Nothing | ||
) | ||
svTrace | ||
receivedHeadersOnlyOnce = length (nub $ snd <$> headerDownloadEvents) == length headerDownloadEvents | ||
receivedHeadersFromOnlyOnePeer = length (nub $ fst <$> headerDownloadEvents) == 1 | ||
in | ||
counterexample | ||
("Downloaded headers:\n" ++ | ||
( unlines $ fmap (" " ++) $ zipWith | ||
(\peer header -> peer ++ " | " ++ header) | ||
(condenseListWithPadding PadRight $ fst <$> headerDownloadEvents) | ||
(condenseListWithPadding PadRight $ snd <$> headerDownloadEvents) | ||
) | ||
) | ||
(receivedHeadersOnlyOnce && receivedHeadersFromOnlyOnePeer) | ||
) | ||
where | ||
-- | This might seem wasteful, as we discard generated adversarial schedules. | ||
-- It actually isn't, since we call it on trees that have no branches besides | ||
-- the trunk, so no adversaries are generated. | ||
genHonestSchedule :: GenesisTest TestBlock () -> Gen (PeerSchedule TestBlock) | ||
genHonestSchedule gt = do | ||
ps <- genUniformSchedulePoints gt | ||
pure $ value $ honest ps |