Skip to content

Commit 6902f92

Browse files
committed
Store previous epoch nonce in PraosState
1 parent 346d9cd commit 6902f92

File tree

8 files changed

+20
-7
lines changed

8 files changed

+20
-7
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ data PraosState = PraosState
279279
-- ^ Candidate nonce
280280
, praosStateEpochNonce :: !Nonce
281281
-- ^ Epoch nonce
282+
, praosStatePreviousEpochNonce :: !Nonce
283+
-- ^ Previous epoch nonce
282284
, praosStateLabNonce :: !Nonce
283285
-- ^ Nonce constructed from the hash of the previous block
284286
, praosStateLastEpochBlockNonce :: !Nonce
@@ -303,17 +305,19 @@ instance Serialise PraosState where
303305
, praosStateEvolvingNonce
304306
, praosStateCandidateNonce
305307
, praosStateEpochNonce
308+
, praosStatePreviousEpochNonce
306309
, praosStateLabNonce
307310
, praosStateLastEpochBlockNonce
308311
} =
309312
encodeVersion 0 $
310313
mconcat
311-
[ CBOR.encodeListLen 7
314+
[ CBOR.encodeListLen 8
312315
, toCBOR praosStateLastSlot
313316
, toCBOR praosStateOCertCounters
314317
, toCBOR praosStateEvolvingNonce
315318
, toCBOR praosStateCandidateNonce
316319
, toCBOR praosStateEpochNonce
320+
, toCBOR praosStatePreviousEpochNonce
317321
, toCBOR praosStateLabNonce
318322
, toCBOR praosStateLastEpochBlockNonce
319323
]
@@ -323,7 +327,7 @@ instance Serialise PraosState where
323327
[(0, Decode decodePraosState)]
324328
where
325329
decodePraosState = do
326-
enforceSize "PraosState" 7
330+
enforceSize "PraosState" 8
327331
PraosState
328332
<$> fromCBOR
329333
<*> fromCBOR
@@ -332,6 +336,7 @@ instance Serialise PraosState where
332336
<*> fromCBOR
333337
<*> fromCBOR
334338
<*> fromCBOR
339+
<*> fromCBOR
335340

336341
data instance Ticked PraosState = TickedPraosState
337342
{ tickedPraosStateChainDepState :: PraosState
@@ -423,11 +428,13 @@ instance PraosCrypto c => ConsensusProtocol (Praos c) where
423428
-- Updating the chain dependent state for Praos.
424429
--
425430
-- If we are not in a new epoch, then nothing happens. If we are in a new
426-
-- epoch, we do two things:
431+
-- epoch, we do three things:
427432
-- - Update the epoch nonce to the combination of the candidate nonce and the
428433
-- nonce derived from the last block of the previous epoch.
429-
-- - Update the "last block of previous epoch" nonce to the nonce derived from
430-
-- the last applied block.
434+
-- - Store the current epoch nonce as the "previous epoch" nonce. This is
435+
-- needed by Peras to be able to validate slightly old certificates.
436+
-- - Update the "last block of previous epoch" nonce to the nonce derived
437+
-- from the last applied block.
431438
tickChainDepState
432439
PraosConfig{praosEpochInfo}
433440
lv
@@ -450,7 +457,10 @@ instance PraosCrypto c => ConsensusProtocol (Praos c) where
450457
{ praosStateEpochNonce =
451458
praosStateCandidateNonce st
452459
praosStateLastEpochBlockNonce st
453-
, praosStateLastEpochBlockNonce = praosStateLabNonce st
460+
, praosStatePreviousEpochNonce =
461+
praosStateEpochNonce st
462+
, praosStateLastEpochBlockNonce =
463+
praosStateLabNonce st
454464
}
455465
else st
456466

@@ -758,7 +768,8 @@ instance TranslateProto (TPraos c) (Praos c) where
758768
, praosStateOCertCounters = Map.mapKeysMonotonic coerce certCounters
759769
, praosStateEvolvingNonce = evolvingNonce
760770
, praosStateCandidateNonce = candidateNonce
761-
, praosStateEpochNonce = SL.ticknStateEpochNonce csTickn
771+
, praosStateEpochNonce = epochNonce
772+
, praosStatePreviousEpochNonce = epochNonce -- same as current epoch nonce
762773
, praosStateLabNonce = csLabNonce
763774
, praosStateLastEpochBlockNonce = SL.ticknStatePrevHashNonce csTickn
764775
}
@@ -767,6 +778,7 @@ instance TranslateProto (TPraos c) (Praos c) where
767778
tpraosStateChainDepState tpState
768779
SL.PrtclState certCounters evolvingNonce candidateNonce =
769780
csProtocol
781+
epochNonce = SL.ticknStateEpochNonce csTickn
770782

771783
{-------------------------------------------------------------------------------
772784
Util

ouroboros-consensus-protocol/src/unstable-protocol-testlib/Test/Consensus/Protocol/Serialisation/Generators.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,4 @@ instance Arbitrary PraosState where
8181
<*> arbitrary
8282
<*> arbitrary
8383
<*> arbitrary
84+
<*> arbitrary

0 commit comments

Comments
 (0)