Skip to content

Commit

Permalink
CSJ happy path test
Browse files Browse the repository at this point in the history
  • Loading branch information
nbacquey authored and Niols committed Apr 29, 2024
1 parent 20786d2 commit 325304b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ test-suite consensus-test
Test.Consensus.Genesis.Setup.Classifiers
Test.Consensus.Genesis.Setup.GenChains
Test.Consensus.Genesis.Tests
Test.Consensus.Genesis.Tests.CSJ
Test.Consensus.Genesis.Tests.DensityDisconnect
Test.Consensus.Genesis.Tests.LoE
Test.Consensus.Genesis.Tests.LoP
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Test.Consensus.Genesis.Tests (tests) where

import qualified Test.Consensus.Genesis.Tests.CSJ as CSJ
import qualified Test.Consensus.Genesis.Tests.DensityDisconnect as GDD
import qualified Test.Consensus.Genesis.Tests.LoE as LoE
import qualified Test.Consensus.Genesis.Tests.LongRangeAttack as LongRangeAttack
Expand All @@ -9,7 +10,8 @@ import Test.Tasty

tests :: TestTree
tests = testGroup "Genesis tests"
[ GDD.tests
[ CSJ.tests
, GDD.tests
, LongRangeAttack.tests
, LoE.tests
, LoP.tests
Expand Down
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

0 comments on commit 325304b

Please sign in to comment.