Skip to content

Commit

Permalink
Add a BlockFetch leashing attack test
Browse files Browse the repository at this point in the history
  • Loading branch information
Niols authored and amesgen committed Aug 7, 2024
1 parent c4bfa37 commit c594c09
Showing 1 changed file with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Test.Consensus.Genesis.Tests.Uniform (
import Cardano.Slotting.Slot (SlotNo (SlotNo), WithOrigin (..))
import Control.Monad (replicateM)
import Control.Monad.Class.MonadTime.SI (Time, addTime)
import Data.List (intercalate, sort)
import Data.List (intercalate, sort, uncons)
import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe, mapMaybe)
Expand All @@ -40,7 +40,8 @@ import Test.Consensus.PeerSimulator.Run (SchedulerConfig (..),
defaultSchedulerConfig)
import Test.Consensus.PeerSimulator.StateView
import Test.Consensus.PointSchedule
import Test.Consensus.PointSchedule.Peers (Peers (..), isHonestPeerId)
import Test.Consensus.PointSchedule.Peers (Peers (..), getPeerIds,
isHonestPeerId, peers')
import Test.Consensus.PointSchedule.Shrinking
(shrinkByRemovingAdversaries, shrinkPeerSchedules)
import Test.Consensus.PointSchedule.SinglePeer
Expand Down Expand Up @@ -72,7 +73,8 @@ tests =
-- because this test writes the immutable chain to disk and `instance Binary TestBlock`
-- chokes on long chains.
adjustQuickCheckMaxSize (const 10) $
testProperty "the node is shut down and restarted after some time" prop_downtime
testProperty "the node is shut down and restarted after some time" prop_downtime,
testProperty "block fetch leashing attack" prop_blockFetchLeashingAttack
]

theProperty ::
Expand Down Expand Up @@ -416,3 +418,44 @@ prop_downtime = forAllGenesisTest
{ pgpExtraHonestPeers = fromIntegral (gtExtraHonestPeers gt)
, pgpDowntime = DowntimeWithSecurityParam (gtSecurityParam gt)
}

prop_blockFetchLeashingAttack :: Property
prop_blockFetchLeashingAttack =
forAllGenesisTest
(disableBoringTimeouts <$> genChains (pure 0) `enrichedWith` genBlockFetchLeashingSchedule)
defaultSchedulerConfig
{ scEnableLoE = True,
scEnableLoP = True,
scEnableCSJ = True
}
shrinkPeerSchedules
theProperty
where
genBlockFetchLeashingSchedule :: GenesisTest TestBlock () -> QC.Gen (PointSchedule TestBlock)
genBlockFetchLeashingSchedule genesisTest = do
PointSchedule {psSchedule, psMinEndTime} <-
stToGen $
uniformPoints
(PointsGeneratorParams {pgpExtraHonestPeers = 1, pgpDowntime = NoDowntime})
(gtBlockTree genesisTest)
peers <- QC.shuffle $ Map.elems $ honestPeers psSchedule
let (honest, adversaries) = fromMaybe (error "blockFetchLeashingAttack") $ uncons peers
adversaries' = map (filter (not . isBlockPoint . snd)) adversaries
psSchedule' = peers' [honest] adversaries'
-- Important to shuffle the order in which the peers start, otherwise the
-- honest peer starts first and systematically becomes dynamo.
psStartOrder <- shuffle $ getPeerIds psSchedule'
pure $ PointSchedule {psSchedule = psSchedule', psStartOrder, psMinEndTime}

isBlockPoint :: SchedulePoint blk -> Bool
isBlockPoint (ScheduleBlockPoint _) = True
isBlockPoint _ = False

disableBoringTimeouts gt =
gt
{ gtChainSyncTimeouts =
(gtChainSyncTimeouts gt)
{ mustReplyTimeout = Nothing,
idleTimeout = Nothing
}
}

0 comments on commit c594c09

Please sign in to comment.