mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 20:27:58 +00:00
babe: make secondary slot randomness available on-chain (#7053)
* babe: make secondary slot randomness available on-chain * babe: extract out vrf_output function * babe: add missing comment * babe: fix incorrectly storing primary randomness * babe: add test for onchain author vrf * babe: fix reviewer nits * runtime: bump spec_version * babe: remove outer Option for AuthorVrfRandomness * babe: fix reviewer nits on doc strings * babe: move make_vrf_output to mock.rs * babe: cleanup docs * babe: kill ephemeral entry instead of take * babe: use type alias for maybe randomness Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -26,8 +26,7 @@ use frame_support::{
|
||||
use mock::*;
|
||||
use pallet_session::ShouldEndSession;
|
||||
use sp_consensus_babe::AllowedSlots;
|
||||
use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof};
|
||||
use sp_core::crypto::{IsWrappedBy, Pair};
|
||||
use sp_core::crypto::Pair;
|
||||
|
||||
const EMPTY_RANDOMNESS: [u8; 32] = [
|
||||
74, 25, 49, 128, 53, 97, 244, 49,
|
||||
@@ -64,18 +63,7 @@ fn first_block_epoch_zero_start() {
|
||||
|
||||
ext.execute_with(|| {
|
||||
let genesis_slot = 100;
|
||||
|
||||
let pair = sp_core::sr25519::Pair::from_ref(&pairs[0]).as_ref();
|
||||
let transcript = sp_consensus_babe::make_transcript(
|
||||
&Babe::randomness(),
|
||||
genesis_slot,
|
||||
0,
|
||||
);
|
||||
let vrf_inout = pair.vrf_sign(transcript);
|
||||
let vrf_randomness: sp_consensus_vrf::schnorrkel::Randomness = vrf_inout.0
|
||||
.make_bytes::<[u8; 32]>(&sp_consensus_babe::BABE_VRF_INOUT_CONTEXT);
|
||||
let vrf_output = VRFOutput(vrf_inout.0.to_output());
|
||||
let vrf_proof = VRFProof(vrf_inout.1);
|
||||
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
|
||||
|
||||
let first_vrf = vrf_output;
|
||||
let pre_digest = make_pre_digest(
|
||||
@@ -100,6 +88,7 @@ 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();
|
||||
@@ -107,6 +96,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!(NextRandomness::get(), [0; 32]);
|
||||
|
||||
assert_eq!(header.digest.logs.len(), 2);
|
||||
@@ -126,6 +116,84 @@ fn first_block_epoch_zero_start() {
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn author_vrf_output_for_primary() {
|
||||
let (pairs, mut ext) = new_test_ext_with_pairs(1);
|
||||
|
||||
ext.execute_with(|| {
|
||||
let genesis_slot = 10;
|
||||
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
|
||||
let primary_pre_digest = make_pre_digest(0, genesis_slot, vrf_output, vrf_proof);
|
||||
|
||||
System::initialize(
|
||||
&1,
|
||||
&Default::default(),
|
||||
&Default::default(),
|
||||
&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);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn author_vrf_output_for_secondary_vrf() {
|
||||
let (pairs, mut ext) = new_test_ext_with_pairs(1);
|
||||
|
||||
ext.execute_with(|| {
|
||||
let genesis_slot = 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::initialize(
|
||||
&1,
|
||||
&Default::default(),
|
||||
&Default::default(),
|
||||
&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);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_author_vrf_output_for_secondary_plain() {
|
||||
new_test_ext(1).execute_with(|| {
|
||||
let genesis_slot = 10;
|
||||
let secondary_plain_pre_digest = make_secondary_plain_pre_digest(0, genesis_slot);
|
||||
|
||||
System::initialize(
|
||||
&1,
|
||||
&Default::default(),
|
||||
&Default::default(),
|
||||
&secondary_plain_pre_digest,
|
||||
Default::default(),
|
||||
);
|
||||
assert_eq!(Babe::author_vrf_randomness(), None);
|
||||
|
||||
Babe::do_initialize(1);
|
||||
assert_eq!(Babe::author_vrf_randomness(), None);
|
||||
|
||||
Babe::on_finalize(1);
|
||||
System::finalize();
|
||||
assert_eq!(Babe::author_vrf_randomness(), None);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authority_index() {
|
||||
new_test_ext(4).execute_with(|| {
|
||||
|
||||
Reference in New Issue
Block a user