@@ -61,6 +61,7 @@ import Ouroboros.Consensus.Protocol.Praos.VRF (InputVRF, mkInputVRF,
6161import Ouroboros.Consensus.Protocol.TPraos (StandardCrypto )
6262import Test.QuickCheck (Gen , arbitrary , choose , frequency , generate ,
6363 getPositive , resize , shrinkList , sized , suchThat , vectorOf )
64+ import Debug.Trace (trace )
6465
6566-- * Test Vectors
6667
@@ -89,8 +90,8 @@ genSample = do
8990
9091genMutatedHeader :: GeneratorContext -> Gen (GeneratorContext , MutatedHeader )
9192genMutatedHeader context = do
92- mutation <- genMutation
9393 header <- genHeader context
94+ mutation <- genMutation header
9495 mutate context header mutation
9596
9697shrinkSample :: Sample -> [Sample ]
@@ -133,19 +134,19 @@ mutate context header mutation =
133134 pure (context, Header newBody (KES. SignedKES sig'))
134135 MutateKESPeriodBefore -> do
135136 let Header body _ = header
136- let OCert {ocertKESPeriod = KESPeriod kesPeriod} = hbOCert body
137+ OCert {ocertKESPeriod = KESPeriod kesPeriod} = hbOCert body
137138 newSlotNo <- genSlotAfterKESPeriod (fromIntegral kesPeriod) praosMaxKESEvo praosSlotsPerKESPeriod
138139 let rho' = mkInputVRF newSlotNo nonce
140+ period' = unSlotNo newSlotNo `div` praosSlotsPerKESPeriod
139141 hbVrfRes = VRF. evalCertified () rho' vrfSignKey
140142 newBody = body{hbSlotNo = newSlotNo, hbVrfRes}
141- sig' = KES. signKES () kesPeriod newBody kesSignKey
143+ sig' = KES. signKES () ( fromIntegral period' - kesPeriod) newBody kesSignKey
142144 pure (context, Header newBody (KES. SignedKES sig'))
143145 MutateCounterOver1 -> do
144146 let poolId = coerce $ hashKey $ VKey $ deriveVerKeyDSIGN coldSignKey
145- oldCounter = fromMaybe 0 $ Map. lookup poolId (ocertCounters context)
146- -- FIXME: assumes oldCounter is greater than 1, which is the case in the base generator
147- -- but is not guaranteed. If oldCounter == 0 then the mutation will fail
148- newCounter <- choose (0 , oldCounter)
147+ Header body _ = header
148+ OCert {ocertN} = hbOCert body
149+ newCounter <- choose (0 , ocertN - 2 )
149150 let context' = context{ocertCounters = Map. insert poolId newCounter (ocertCounters context)}
150151 pure (context', header)
151152 MutateCounterUnder -> do
@@ -217,17 +218,23 @@ expectedError = \case
217218 CounterTooSmallOCERT {} -> True
218219 _ -> False
219220
220- genMutation :: Gen Mutation
221- genMutation =
222- frequency
221+ genMutation :: Header StandardCrypto -> Gen Mutation
222+ genMutation header =
223+ frequency $
223224 [ (4 , pure NoMutation )
224225 , (1 , pure MutateKESKey )
225226 , (1 , pure MutateColdKey )
226227 , (1 , pure MutateKESPeriod )
227228 , (1 , pure MutateKESPeriodBefore )
228- , (1 , pure MutateCounterOver1 )
229229 , (1 , pure MutateCounterUnder )
230- ]
230+ ] <> maybeCounterOver1
231+ where
232+ Header body _ = header
233+ OCert {ocertN} = hbOCert body
234+ maybeCounterOver1 =
235+ if ocertN > 10
236+ then [(1 , pure MutateCounterOver1 )]
237+ else []
231238
232239data MutatedHeader = MutatedHeader
233240 { header :: ! (Header StandardCrypto )
@@ -441,12 +448,15 @@ genKESPeriodAfterLimit slotNo praosSlotsPerKESPeriod =
441448 currentKESPeriod = unSlotNo slotNo `div` praosSlotsPerKESPeriod
442449
443450genSlotAfterKESPeriod :: Word64 -> Word64 -> Word64 -> Gen SlotNo
444- genSlotAfterKESPeriod ocertKESPeriod praosMaxKESEvo praosSlotsPerKESPeriod =
451+ genSlotAfterKESPeriod ocertKESPeriod praosMaxKESEvo praosSlotsPerKESPeriod = do
445452 -- kp_ < c0_ + praosMaxKESEvo
446453 -- ! =>
447454 -- kp >= c0_ + praosMaxKESEvo
448455 -- c0 <= kp - praosMaxKESEvo
449- SlotNo <$> arbitrary `suchThat` (> (ocertKESPeriod + praosMaxKESEvo) * praosSlotsPerKESPeriod)
456+ s <- SlotNo <$> arbitrary `suchThat` (> threshold)
457+ pure $ trace (" new slot no: " <> show s <> " , threshold: " <> show threshold ) $ s
458+ where
459+ threshold = (ocertKESPeriod + praosMaxKESEvo + 1 ) * praosSlotsPerKESPeriod
450460
451461genHash :: Gen (Hash Blake2b_256 a )
452462genHash = coerce . hashWith id <$> gen32Bytes
0 commit comments