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
@@ -22,13 +22,14 @@ use super::{
BabeEpochConfiguration, Slot, BABE_ENGINE_ID,
};
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{DigestItem, RuntimeDebug};
use sp_std::vec::Vec;
use sp_consensus_vrf::schnorrkel::{Randomness, VRFOutput, VRFProof};
/// Raw BABE primary slot assignment pre-digest.
#[derive(Clone, RuntimeDebug, Encode, Decode)]
#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct PrimaryPreDigest {
/// Authority index
pub authority_index: super::AuthorityIndex,
@@ -41,7 +42,7 @@ pub struct PrimaryPreDigest {
}
/// BABE secondary slot assignment pre-digest.
#[derive(Clone, RuntimeDebug, Encode, Decode)]
#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct SecondaryPlainPreDigest {
/// Authority index
///
@@ -55,7 +56,7 @@ pub struct SecondaryPlainPreDigest {
}
/// BABE secondary deterministic slot assignment with VRF outputs.
#[derive(Clone, RuntimeDebug, Encode, Decode)]
#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct SecondaryVRFPreDigest {
/// Authority index
pub authority_index: super::AuthorityIndex,
@@ -70,7 +71,7 @@ pub struct SecondaryVRFPreDigest {
/// A BABE pre-runtime digest. This contains all data required to validate a
/// block and for the BABE runtime module. Slots can be assigned to a primary
/// (VRF based) and to a secondary (slot number based).
#[derive(Clone, RuntimeDebug, Encode, Decode)]
#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum PreDigest {
/// A primary VRF-based slot assignment.
#[codec(index = 1)]
@@ -102,6 +103,11 @@ impl PreDigest {
}
}
/// Returns true if this pre-digest is for a primary slot assignment.
pub fn is_primary(&self) -> bool {
matches!(self, PreDigest::Primary(..))
}
/// Returns the weight _added_ by this digest, not the cumulative weight
/// of the chain.
pub fn added_weight(&self) -> crate::BabeBlockWeight {
@@ -111,11 +117,12 @@ impl PreDigest {
}
}
/// Returns the VRF output, if it exists.
pub fn vrf_output(&self) -> Option<&VRFOutput> {
/// Returns the VRF output and proof, if they exist.
pub fn vrf(&self) -> Option<(&VRFOutput, &VRFProof)> {
match self {
PreDigest::Primary(primary) => Some(&primary.vrf_output),
PreDigest::SecondaryVRF(secondary) => Some(&secondary.vrf_output),
PreDigest::Primary(primary) => Some((&primary.vrf_output, &primary.vrf_proof)),
PreDigest::SecondaryVRF(secondary) =>
Some((&secondary.vrf_output, &secondary.vrf_proof)),
PreDigest::SecondaryPlain(_) => None,
}
}
@@ -14,6 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
scale-info = { version = "2.0.1", default-features = false }
schnorrkel = { version = "0.9.1", default-features = false, features = ["preaudit_deprecated", "u64_backend"] }
sp-core = { version = "6.0.0", default-features = false, path = "../../core" }
sp-runtime = { version = "6.0.0", default-features = false, path = "../../runtime" }
@@ -23,6 +24,7 @@ sp-std = { version = "4.0.0", default-features = false, path = "../../std" }
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"schnorrkel/std",
"sp-core/std",
"sp-runtime/std",
@@ -17,7 +17,8 @@
//! Schnorrkel-based VRF.
use codec::{Decode, Encode, EncodeLike};
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use scale_info::TypeInfo;
use schnorrkel::errors::MultiSignatureStage;
use sp_core::U512;
use sp_std::{
@@ -65,6 +66,20 @@ impl Decode for VRFOutput {
}
}
impl MaxEncodedLen for VRFOutput {
fn max_encoded_len() -> usize {
<[u8; VRF_OUTPUT_LENGTH]>::max_encoded_len()
}
}
impl TypeInfo for VRFOutput {
type Identity = [u8; VRF_OUTPUT_LENGTH];
fn type_info() -> scale_info::Type {
Self::Identity::type_info()
}
}
impl TryFrom<[u8; VRF_OUTPUT_LENGTH]> for VRFOutput {
type Error = SignatureError;
@@ -117,6 +132,20 @@ impl Decode for VRFProof {
}
}
impl MaxEncodedLen for VRFProof {
fn max_encoded_len() -> usize {
<[u8; VRF_PROOF_LENGTH]>::max_encoded_len()
}
}
impl TypeInfo for VRFProof {
type Identity = [u8; VRF_PROOF_LENGTH];
fn type_info() -> scale_info::Type {
Self::Identity::type_info()
}
}
impl TryFrom<[u8; VRF_PROOF_LENGTH]> for VRFProof {
type Error = SignatureError;