From 86ab838b17c9ab59424f59b50a6770e3643a1ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 7 Jul 2021 13:44:05 +0200 Subject: [PATCH] Keep current block randomness in state (#9294) * Keep current block randomness in state Instead of killing it at the end of the block, it stays in the block for inspection. This is required by parachains to get access to this randomness of the relay chain. * Fix tests --- substrate/frame/babe/src/lib.rs | 8 +++----- substrate/frame/babe/src/tests.rs | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/substrate/frame/babe/src/lib.rs b/substrate/frame/babe/src/lib.rs index 6ec199925b..b52868d1d0 100644 --- a/substrate/frame/babe/src/lib.rs +++ b/substrate/frame/babe/src/lib.rs @@ -256,9 +256,10 @@ pub mod pallet { #[pallet::getter(fn initialized)] pub(super) type Initialized = StorageValue<_, MaybeRandomness>; - /// Temporary value (cleared at block finalization) that includes the VRF output generated - /// at this block. This field should always be populated during block processing unless + /// This field should always be populated during block processing unless /// secondary plain slots are enabled (which don't contain a VRF output). + /// + /// It is set in `on_initialize`, before it will contain the value from the last block. #[pallet::storage] #[pallet::getter(fn author_vrf_randomness)] pub(super) type AuthorVrfRandomness = StorageValue<_, MaybeRandomness, ValueQuery>; @@ -337,9 +338,6 @@ pub mod pallet { Self::deposit_randomness(&randomness); } - // The stored author generated VRF output is ephemeral. - AuthorVrfRandomness::::kill(); - // remove temporary "environment" entry from storage Lateness::::kill(); } diff --git a/substrate/frame/babe/src/tests.rs b/substrate/frame/babe/src/tests.rs index dfb398a4f4..520a808ab4 100644 --- a/substrate/frame/babe/src/tests.rs +++ b/substrate/frame/babe/src/tests.rs @@ -95,7 +95,7 @@ fn first_block_epoch_zero_start() { assert_eq!(SegmentIndex::::get(), 0); assert_eq!(UnderConstruction::::get(0), vec![vrf_randomness]); assert_eq!(Babe::randomness(), [0; 32]); - assert_eq!(Babe::author_vrf_randomness(), None); + assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness)); assert_eq!(NextRandomness::::get(), [0; 32]); assert_eq!(header.digest.logs.len(), 2); @@ -130,14 +130,13 @@ fn author_vrf_output_for_primary() { &primary_pre_digest, Default::default(), ); - assert_eq!(Babe::author_vrf_randomness(), None); Babe::do_initialize(1); assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness)); Babe::on_finalize(1); System::finalize(); - assert_eq!(Babe::author_vrf_randomness(), None); + assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness)); }) } @@ -156,14 +155,13 @@ fn author_vrf_output_for_secondary_vrf() { &secondary_vrf_pre_digest, Default::default(), ); - assert_eq!(Babe::author_vrf_randomness(), None); Babe::do_initialize(1); assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness)); Babe::on_finalize(1); System::finalize(); - assert_eq!(Babe::author_vrf_randomness(), None); + assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness)); }) }