Skip to content

Commit e3cf89b

Browse files
committed
[wip] Make mutation depend on header to ensure consistency
some mutations are not possible for some content of the header, eg. if ocertN = 0 then it's not possible to generate a smaller expected value
1 parent a15fcc5 commit e3cf89b

File tree

3 files changed

+27
-16
lines changed
  • ouroboros-consensus-cardano/test/tools-test/Test/Cardano/Tools
  • ouroboros-consensus-protocol/src
    • ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol
    • unstable-protocol-testlib/Test/Ouroboros/Consensus/Protocol/Praos

3 files changed

+27
-16
lines changed

ouroboros-consensus-cardano/test/tools-test/Test/Cardano/Tools/Headers.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ prop_validate_legit_header :: Property
3131
prop_validate_legit_header =
3232
forAllBlind genContext $ \context ->
3333
forAllBlind (genMutatedHeader context) $ \(context', header) ->
34-
annotate context header $
34+
annotate context' header $
3535
case validate context' header of
3636
Valid mut -> property True & label (show mut)
3737
Invalid mut err -> property False & counterexample ("Expected: " <> show mut <> "\nError: " <> err)

ouroboros-consensus-protocol/src/ouroboros-consensus-protocol/Ouroboros/Consensus/Protocol/Praos.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ data PraosValidationErr c
348348
!Word -- current KES Period
349349
!Word -- KES start period
350350
!Word -- expected KES evolutions
351+
!Word64 -- max KES evolutions
351352
!String -- error message given by Consensus Layer
352353
| NoCounterForKeyHashOCERT
353354
!(KeyHash 'BlockIssuer c) -- stake pool key hash
@@ -602,7 +603,7 @@ doValidateKESSignature praosMaxKESEvo praosSlotsPerKESPeriod stakeDistribution o
602603
DSIGN.verifySignedDSIGN () vkcold (OCert.ocertToSignable oc) tau ?!:
603604
InvalidSignatureOCERT n c0
604605
KES.verifySignedKES () vk_hot t (Views.hvSigned b) (Views.hvSignature b) ?!:
605-
InvalidKesSignatureOCERT kp_ c0_ t
606+
InvalidKesSignatureOCERT kp_ c0_ t praosMaxKESEvo
606607

607608
case currentIssueNo of
608609
Nothing -> do

ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Ouroboros/Consensus/Protocol/Praos/Header.hs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import Ouroboros.Consensus.Protocol.Praos.VRF (InputVRF, mkInputVRF,
6161
import Ouroboros.Consensus.Protocol.TPraos (StandardCrypto)
6262
import 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

9091
genMutatedHeader :: GeneratorContext -> Gen (GeneratorContext, MutatedHeader)
9192
genMutatedHeader context = do
92-
mutation <- genMutation
9393
header <- genHeader context
94+
mutation <- genMutation header
9495
mutate context header mutation
9596

9697
shrinkSample :: 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

232239
data MutatedHeader = MutatedHeader
233240
{ header :: !(Header StandardCrypto)
@@ -441,12 +448,15 @@ genKESPeriodAfterLimit slotNo praosSlotsPerKESPeriod =
441448
currentKESPeriod = unSlotNo slotNo `div` praosSlotsPerKESPeriod
442449

443450
genSlotAfterKESPeriod :: 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

451461
genHash :: Gen (Hash Blake2b_256 a)
452462
genHash = coerce . hashWith id <$> gen32Bytes

0 commit comments

Comments
 (0)