Skip to content

Commit 93e74d6

Browse files
committed
Preserve TP/HP/BP consistency while shrinking adversarial schedules
1 parent 8d19482 commit 93e74d6

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

  • ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PointSchedule

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PointSchedule/Shrinking.hs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Control.Monad.Class.MonadTime.SI
2020
)
2121
import Data.Containers.ListUtils (nubOrd)
2222
import Data.Foldable (toList)
23+
import Data.Function ((&))
2324
import Data.Functor ((<&>))
2425
import qualified Data.Map.Strict as Map
2526
import Data.Maybe (catMaybes, mapMaybe)
@@ -125,9 +126,21 @@ duration PointSchedule{psSchedule, psMinEndTime} =
125126
maximum $ psMinEndTime : [t | sch <- toList psSchedule, (t, _) <- take 1 (reverse sch)]
126127

127128
-- | Shrink a 'PeerSchedule' by removing ticks from it. The other ticks are kept
128-
-- unchanged.
129-
shrinkAdversarialPeer :: PeerSchedule blk -> [PeerSchedule blk]
130-
shrinkAdversarialPeer = shrinkList (const [])
129+
-- unchanged. Preserve TP/HP/BP consistency, that is, at any time, TP >= HP and HP >= BP.
130+
shrinkAdversarialPeer :: Ord blk => PeerSchedule blk -> [PeerSchedule blk]
131+
shrinkAdversarialPeer = (filter preservesConsistency) . shrinkList (const [])
132+
where
133+
preservesConsistency sch =
134+
foldr
135+
( \(_t, p) (tp, hp, acc) ->
136+
case p of
137+
ScheduleTipPoint newTp -> (newTp, hp, acc)
138+
ScheduleHeaderPoint newHp -> (tp, newHp, acc && tp >= newHp)
139+
ScheduleBlockPoint newBp -> (tp, hp, acc && hp >= newBp)
140+
)
141+
(Slot.Origin, Slot.Origin, True)
142+
sch
143+
& (\(_, _, x) -> x)
131144

132145
-- | Shrink the 'others' field of a 'Peers' structure by attempting to remove
133146
-- peers or by shrinking their values using the given shrinking function.

0 commit comments

Comments
 (0)