babe: only process vrf on module finalization (#11113)

* babe: only process vrf on block execution finalization

* babe: rename CurrentBlockRandomness to PreviousBlockRandomness

* babe: add test for initialization ordering

* babe: rename PreviousBlockRandomness to ParentBlockRandomness

* babe: re-add CurrentBlockRandomness with deprecation notice

* babe: export CurrentBlockRandomness

* babe: silence deprecation warning when exporting CurrentBlockRandomness

* babe: suggestion from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* babe: flatten nested option

* babe: rustfmt

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
André Silva
2022-05-04 17:14:58 +01:00
committed by GitHub
parent 2dbdafb437
commit d6d4068ccc
7 changed files with 230 additions and 134 deletions
+49 -25
View File
@@ -26,6 +26,7 @@ use frame_support::{
use mock::*;
use pallet_session::ShouldEndSession;
use sp_consensus_babe::{AllowedSlots, BabeEpochConfiguration, Slot};
use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof};
use sp_core::crypto::Pair;
const EMPTY_RANDOMNESS: [u8; 32] = [
@@ -76,11 +77,11 @@ fn first_block_epoch_zero_start() {
assert_eq!(Babe::genesis_slot(), genesis_slot);
assert_eq!(Babe::current_slot(), genesis_slot);
assert_eq!(Babe::epoch_index(), 0);
assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness));
Babe::on_finalize(1);
let header = System::finalize();
assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness));
assert_eq!(SegmentIndex::<Test>::get(), 0);
assert_eq!(UnderConstruction::<Test>::get(0), vec![vrf_randomness]);
assert_eq!(Babe::randomness(), [0; 32]);
@@ -105,46 +106,69 @@ fn first_block_epoch_zero_start() {
}
#[test]
fn author_vrf_output_for_primary() {
fn current_slot_is_processed_on_initialization() {
let (pairs, mut ext) = new_test_ext_with_pairs(1);
ext.execute_with(|| {
let genesis_slot = Slot::from(10);
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
let primary_pre_digest = make_primary_pre_digest(0, genesis_slot, vrf_output, vrf_proof);
let pre_digest = make_primary_pre_digest(0, genesis_slot, vrf_output, vrf_proof);
System::reset_events();
System::initialize(&1, &Default::default(), &primary_pre_digest);
System::initialize(&1, &Default::default(), &pre_digest);
assert_eq!(Babe::current_slot(), Slot::from(0));
assert!(Babe::initialized().is_none());
Babe::do_initialize(1);
// current slot is updated on initialization
Babe::initialize(1);
assert_eq!(Babe::current_slot(), genesis_slot);
assert!(Babe::initialized().is_some());
// but author vrf randomness isn't
assert_eq!(Babe::author_vrf_randomness(), None);
// instead it is updated on block finalization
Babe::on_finalize(1);
assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness));
})
}
fn test_author_vrf_output<F>(make_pre_digest: F)
where
F: Fn(sp_consensus_babe::AuthorityIndex, Slot, VRFOutput, VRFProof) -> sp_runtime::Digest,
{
let (pairs, mut ext) = new_test_ext_with_pairs(1);
ext.execute_with(|| {
let genesis_slot = Slot::from(10);
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
let pre_digest = make_pre_digest(0, genesis_slot, vrf_output, vrf_proof);
System::reset_events();
System::initialize(&1, &Default::default(), &pre_digest);
// author vrf randomness is not updated on initialization
Babe::initialize(1);
assert_eq!(Babe::author_vrf_randomness(), None);
// instead it is updated on block finalization to account for any
// epoch changes that might happen during the block
Babe::on_finalize(1);
assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness));
Babe::on_finalize(1);
// and it is kept after finalizing the block
System::finalize();
assert_eq!(Babe::author_vrf_randomness(), Some(vrf_randomness));
})
}
#[test]
fn author_vrf_output_for_primary() {
test_author_vrf_output(make_primary_pre_digest);
}
#[test]
fn author_vrf_output_for_secondary_vrf() {
let (pairs, mut ext) = new_test_ext_with_pairs(1);
ext.execute_with(|| {
let genesis_slot = Slot::from(10);
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
let secondary_vrf_pre_digest =
make_secondary_vrf_pre_digest(0, genesis_slot, vrf_output, vrf_proof);
System::reset_events();
System::initialize(&1, &Default::default(), &secondary_vrf_pre_digest);
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(), Some(vrf_randomness));
})
test_author_vrf_output(make_secondary_vrf_pre_digest);
}
#[test]
@@ -157,7 +181,7 @@ fn no_author_vrf_output_for_secondary_plain() {
System::initialize(&1, &Default::default(), &secondary_plain_pre_digest);
assert_eq!(Babe::author_vrf_randomness(), None);
Babe::do_initialize(1);
Babe::initialize(1);
assert_eq!(Babe::author_vrf_randomness(), None);
Babe::on_finalize(1);