Skip to content

Commit 9b35a39

Browse files
committed
WIP add test for Peras
1 parent 30341ff commit 9b35a39

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Block/SupportsPeras.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Quiet (Quiet (..))
2929
newtype PerasRoundNo = PerasRoundNo {unPerasRoundNo :: Word64}
3030
deriving Show via Quiet PerasRoundNo
3131
deriving stock Generic
32-
deriving newtype (Eq, Ord, NoThunks, Serialise)
32+
deriving newtype (Enum, Eq, Ord, NoThunks, Serialise)
3333

3434
defaultPerasRoundNo :: PerasRoundNo
3535
defaultPerasRoundNo = PerasRoundNo 0

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/History/Summary.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,16 @@ summarize ::
347347
Transitions xs ->
348348
Summary xs
349349
summarize ledgerTip = \(Shape shape) (Transitions transitions) ->
350-
Summary $ go initBound shape transitions
350+
Summary $ go initBoundWithPeras shape transitions
351351
where
352+
-- as noted in the haddock, this function is only used for testing purposes,
353+
-- therefore we make the initial era is Peras-enabled, which means
354+
-- we only test Peras-enabled eras. It is rather difficult
355+
-- to parameterise the test suite, as it requires also parameterise many non-test functions, like
356+
-- 'HF.initBound', and leads to a huge diff. Therefore, we make the judgement call to
357+
-- only test Peras-enabled eras.
358+
initBoundWithPeras = initBound{boundPerasRound = SJust . PerasRoundNo $ 0}
359+
352360
go ::
353361
Bound -> -- Lower bound for current era
354362
Exactly (x ': xs) EraParams -> -- params for all eras

ouroboros-consensus/test/consensus-test/Test/Consensus/HardFork/History.hs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
-- them to be).
2626
module Test.Consensus.HardFork.History (tests) where
2727

28+
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
2829
import Cardano.Slotting.EpochInfo
2930
import Control.Exception (throw)
3031
import Control.Monad.Except
@@ -65,11 +66,11 @@ import Test.Util.QuickCheck
6566
-- General approach:
6667
--
6768
-- * Generate a chain of events
68-
-- * Each event records its own 'RelativeTime', 'SlotNo', and 'EpochNo'
69+
-- * Each event records its own 'RelativeTime', 'SlotNo', 'EpochNo', and 'PerasRoundNo'
6970
-- * We then construct a 'HF.Summary' from a /prefix/ of this chain
7071
-- * We then pick an arbitrary event from the (full) chain:
7172
-- a. If that event is on the prefix of the chain, or within the safe zone, we
72-
-- expect to be able to do any slot/epoch or slot/time conversion, and we
73+
-- expect to be able to do any slot/epoch, slot/time or Peras round/slot conversion, and we
7374
-- can easily verify the result by comparing it to the values the 'Event'
7475
-- itself reports.
7576
-- b. If the event is outside of safe zone, we expect the conversion to throw
@@ -96,6 +97,7 @@ tests =
9697
, testProperty "eventWallclockToSlot" eventWallclockToSlot
9798
, testProperty "epochInfoSlotToEpoch" epochInfoSlotToEpoch
9899
, testProperty "epochInfoEpochToSlot" epochInfoEpochToSlot
100+
, testProperty "eventPerasRounNoToSlot" eventPerasRounNoToSlot
99101
, testProperty "query vs expr" queryVsExprConsistency
100102
]
101103
]
@@ -208,6 +210,16 @@ eventWallclockToSlot chain@ArbitraryChain{..} =
208210
diff :: NominalDiffTime
209211
diff = arbitraryDiffTime arbitraryParams
210212

213+
eventPerasRounNoToSlot :: ArbitraryChain -> Property
214+
eventPerasRounNoToSlot chain@ArbitraryChain{..} =
215+
testSkeleton chain (HF.perasRoundNoToSlot eventTimePerasRoundNo) $
216+
\(startOfPerasRound, _roundLength) ->
217+
conjoin
218+
[ eventTimeSlot === HF.addSlots eventTimeSlotInPerasRound startOfPerasRound
219+
]
220+
where
221+
EventTime{..} = eventTime arbitraryEvent
222+
211223
-- | Composing queries should be equivalent to composing expressions.
212224
--
213225
-- This is a regression test. Each expression in a query should be evaluated in
@@ -503,7 +515,13 @@ data EventTime = EventTime
503515
{ eventTimeSlot :: SlotNo
504516
, eventTimeEpochNo :: EpochNo
505517
, eventTimeEpochSlot :: Word64
518+
-- ^ Relative slot withing the current epoch round,
519+
-- needed to be able to advance the epoch number
506520
, eventTimeRelative :: RelativeTime
521+
, eventTimePerasRoundNo :: PerasRoundNo
522+
, eventTimeSlotInPerasRound :: Word64
523+
-- ^ Relative slot withing the current Peras round,
524+
-- needed to be able to advance the round number
507525
}
508526
deriving Show
509527

@@ -514,6 +532,8 @@ initEventTime =
514532
, eventTimeEpochNo = EpochNo 0
515533
, eventTimeEpochSlot = 0
516534
, eventTimeRelative = RelativeTime 0
535+
, eventTimePerasRoundNo = PerasRoundNo 0
536+
, eventTimeSlotInPerasRound = 0
517537
}
518538

519539
-- | Next time slot
@@ -526,6 +546,8 @@ stepEventTime HF.EraParams{..} EventTime{..} =
526546
, eventTimeRelative =
527547
addRelTime (getSlotLength eraSlotLength) $
528548
eventTimeRelative
549+
, eventTimePerasRoundNo = perasRoundNo'
550+
, eventTimeSlotInPerasRound = slotInPerasRound'
529551
}
530552
where
531553
epoch' :: EpochNo
@@ -535,6 +557,16 @@ stepEventTime HF.EraParams{..} EventTime{..} =
535557
then (succ eventTimeEpochNo, 0)
536558
else (eventTimeEpochNo, succ eventTimeEpochSlot)
537559

560+
perasRoundNo' :: PerasRoundNo
561+
slotInPerasRound' :: Word64
562+
args@(perasRoundNo', slotInPerasRound') =
563+
case eraPerasRoundLength of
564+
SNothing -> args
565+
SJust (PerasRoundLength perasRoundLength) ->
566+
if succ eventTimeSlotInPerasRound == perasRoundLength
567+
then (succ eventTimePerasRoundNo, 0)
568+
else (eventTimePerasRoundNo, succ eventTimeSlotInPerasRound)
569+
538570
{-------------------------------------------------------------------------------
539571
Chain model
540572
-----------------------------------------------------------------------------}

0 commit comments

Comments
 (0)