Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e70f3bd
babe: initial implementation of secondary slots
andresilva Aug 13, 2019
65370d2
babe: validate secondary slot author
andresilva Aug 13, 2019
086b01d
babe: implement weight based fork choice
andresilva Aug 13, 2019
86468f3
babe: remove unused
andresilva Aug 13, 2019
46005e9
aura: cleanup unused imports
andresilva Aug 13, 2019
1fb767f
babe: pass in parent weight when authoring and verifying
andresilva Aug 13, 2019
36d7896
babe: use epoch randomness for picking secondary slot authors
andresilva Aug 13, 2019
e277fcf
babe: fix tests
andresilva Aug 13, 2019
1643b71
babe: fix wasm build
andresilva Aug 13, 2019
0da1733
babe: node-side code for disabling secondary slots
andresilva Aug 13, 2019
730c328
babe: allow enabling/disabling secondary slots from runtime
andresilva Aug 13, 2019
3568ce8
babe: fix test
andresilva Aug 13, 2019
763c617
babe: use blake2_256 for secondary slot assignment
andresilva Aug 13, 2019
8c0ff02
babe: run block initialization in should_end_session
andresilva Aug 13, 2019
3b38b14
node: increase slot duration to 6s
andresilva Aug 13, 2019
ca6b9a0
babe: add docs
andresilva Aug 15, 2019
b5b2d6e
node: bump spec_version
andresilva Aug 15, 2019
fed3e6f
Apply suggestions from code review
andresilva Aug 15, 2019
f28273c
babe: simplify secondary slot assignment calculation
andresilva Aug 15, 2019
382aefa
Merge branch 'master' into andre/aurababeous
andresilva Aug 16, 2019
cca85f9
babe: remove unnecessary comment
andresilva Aug 16, 2019
8a95966
node: bump spec_version
andresilva Aug 16, 2019
11fe7a6
Merge branch 'master' into andre/aurababeous
andresilva Aug 16, 2019
4f75dd8
babe: fix bad merge
andresilva Aug 16, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion core/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,12 @@ impl<H, B, C, E, I, P, Error, SO> slots::SimpleSlotWorker<B> for AuraWorker<C, E
epoch_data.len()
}

fn claim_slot(&self, slot_number: u64, epoch_data: &Self::EpochData) -> Option<Self::Claim> {
fn claim_slot(
&self,
_header: &B::Header,
slot_number: u64,
epoch_data: &Self::EpochData,
) -> Option<Self::Claim> {
let expected_author = slot_author::<P>(slot_number, epoch_data);

expected_author.and_then(|p| {
Expand Down
175 changes: 136 additions & 39 deletions core/consensus/babe/primitives/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::AuthoritySignature;
use super::{BABE_ENGINE_ID, Epoch};
#[cfg(not(feature = "std"))]
use super::{VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH};
use super::SlotNumber;
use super::{AuthorityIndex, BabeBlockWeight, SlotNumber};
#[cfg(feature = "std")]
use sr_primitives::{DigestItem, generic::OpaqueDigestItemId};
#[cfg(feature = "std")]
Expand All @@ -36,46 +36,136 @@ use schnorrkel::{
vrf::{VRFProof, VRFOutput, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH}
};

/// A BABE pre-digest
/// 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).
#[cfg(feature = "std")]
#[derive(Clone, Debug)]
pub struct BabePreDigest {
/// VRF output
pub vrf_output: VRFOutput,
/// VRF proof
pub vrf_proof: VRFProof,
/// Authority index
pub authority_index: super::AuthorityIndex,
/// Slot number
pub slot_number: SlotNumber,
pub enum BabePreDigest {
/// A primary VRF-based slot assignment.
Primary {
/// VRF output
vrf_output: VRFOutput,
/// VRF proof
vrf_proof: VRFProof,
/// Authority index
authority_index: super::AuthorityIndex,
/// Slot number
slot_number: SlotNumber,
/// Chain weight (measured in number of Primary blocks)
weight: BabeBlockWeight,
},
/// A secondary deterministic slot assignment.
Secondary {
/// Authority index
authority_index: super::AuthorityIndex,
/// Slot number
slot_number: SlotNumber,
/// Chain weight (measured in number of Primary blocks)
weight: BabeBlockWeight,
},
}

#[cfg(feature = "std")]
impl BabePreDigest {
/// Returns the slot number of the pre digest.
pub fn authority_index(&self) -> AuthorityIndex {
match self {
BabePreDigest::Primary { authority_index, .. } => *authority_index,
BabePreDigest::Secondary { authority_index, .. } => *authority_index,
}
}

/// Returns the slot number of the pre digest.
pub fn slot_number(&self) -> SlotNumber {
match self {
BabePreDigest::Primary { slot_number, .. } => *slot_number,
BabePreDigest::Secondary { slot_number, .. } => *slot_number,
}
}

/// Returns the weight of the pre digest.
pub fn weight(&self) -> BabeBlockWeight {
match self {
BabePreDigest::Primary { weight, .. } => *weight,
BabePreDigest::Secondary { weight, .. } => *weight,
}
}
}

/// The prefix used by BABE for its VRF keys.
pub const BABE_VRF_PREFIX: &'static [u8] = b"substrate-babe-vrf";

/// A raw version of `BabePreDigest`, usable on `no_std`.
#[derive(Copy, Clone, Encode, Decode)]
pub struct RawBabePreDigest {
/// Slot number
pub slot_number: SlotNumber,
/// Authority index
pub authority_index: super::AuthorityIndex,
/// VRF output
pub vrf_output: [u8; VRF_OUTPUT_LENGTH],
/// VRF proof
pub vrf_proof: [u8; VRF_PROOF_LENGTH],
pub enum RawBabePreDigest {
/// A primary VRF-based slot assignment.
Primary {
/// Authority index
authority_index: AuthorityIndex,
/// Slot number
slot_number: SlotNumber,
/// Chain weight (measured in number of Primary blocks)
weight: BabeBlockWeight,
/// VRF output
vrf_output: [u8; VRF_OUTPUT_LENGTH],
/// VRF proof
vrf_proof: [u8; VRF_PROOF_LENGTH],
},
/// A secondary deterministic slot assignment.
Secondary {
/// Authority index
authority_index: AuthorityIndex,
/// Slot number
slot_number: SlotNumber,
/// Chain weight (measured in number of Primary blocks)
weight: BabeBlockWeight,
},
}

impl RawBabePreDigest {
/// Returns the slot number of the pre digest.
pub fn slot_number(&self) -> SlotNumber {
match self {
RawBabePreDigest::Primary { slot_number, .. } => *slot_number,
RawBabePreDigest::Secondary { slot_number, .. } => *slot_number,
}
}
}

#[cfg(feature = "std")]
impl Encode for BabePreDigest {
fn encode(&self) -> Vec<u8> {
let tmp = RawBabePreDigest {
vrf_output: *self.vrf_output.as_bytes(),
vrf_proof: self.vrf_proof.to_bytes(),
authority_index: self.authority_index,
slot_number: self.slot_number,
let raw = match self {
BabePreDigest::Primary {
vrf_output,
vrf_proof,
authority_index,
slot_number,
weight,
} => {
RawBabePreDigest::Primary {
vrf_output: *vrf_output.as_bytes(),
vrf_proof: vrf_proof.to_bytes(),
authority_index: *authority_index,
slot_number: *slot_number,
weight: *weight,
}
},
BabePreDigest::Secondary {
authority_index,
slot_number,
weight,
} => {
RawBabePreDigest::Secondary {
authority_index: *authority_index,
slot_number: *slot_number,
weight: *weight,
}
},
};
codec::Encode::encode(&tmp)

codec::Encode::encode(&raw)
}
}

Expand All @@ -85,19 +175,26 @@ impl codec::EncodeLike for BabePreDigest {}
#[cfg(feature = "std")]
impl Decode for BabePreDigest {
fn decode<R: Input>(i: &mut R) -> Result<Self, Error> {
let RawBabePreDigest { vrf_output, vrf_proof, authority_index, slot_number } = Decode::decode(i)?;

// Verify (at compile time) that the sizes in babe_primitives are correct
let _: [u8; super::VRF_OUTPUT_LENGTH] = vrf_output;
let _: [u8; super::VRF_PROOF_LENGTH] = vrf_proof;
Ok(BabePreDigest {
vrf_proof: VRFProof::from_bytes(&vrf_proof)
.map_err(convert_error)?,
vrf_output: VRFOutput::from_bytes(&vrf_output)
.map_err(convert_error)?,
authority_index,
slot_number,
})
let pre_digest = match Decode::decode(i)? {
RawBabePreDigest::Primary { vrf_output, vrf_proof, authority_index, slot_number, weight } => {
// Verify (at compile time) that the sizes in babe_primitives are correct
let _: [u8; super::VRF_OUTPUT_LENGTH] = vrf_output;
let _: [u8; super::VRF_PROOF_LENGTH] = vrf_proof;

BabePreDigest::Primary {
vrf_proof: VRFProof::from_bytes(&vrf_proof).map_err(convert_error)?,
vrf_output: VRFOutput::from_bytes(&vrf_output).map_err(convert_error)?,
authority_index,
slot_number,
weight,
}
},
RawBabePreDigest::Secondary { authority_index, slot_number, weight } => {
BabePreDigest::Secondary { authority_index, slot_number, weight }
},
};

Ok(pre_digest)
}
}

Expand Down
9 changes: 7 additions & 2 deletions core/consensus/babe/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ pub type SlotNumber = u64;
/// The weight of an authority.
// NOTE: we use a unique name for the weight to avoid conflicts with other
// `Weight` types, since the metadata isn't able to disambiguate.
pub type BabeWeight = u64;
pub type BabeAuthorityWeight = u64;

/// The weight of a BABE block.
pub type BabeBlockWeight = u32;

/// BABE epoch information
#[derive(Decode, Encode, Default, PartialEq, Eq, Clone)]
Expand All @@ -81,9 +84,11 @@ pub struct Epoch {
/// The duration of this epoch
pub duration: SlotNumber,
/// The authorities and their weights
pub authorities: Vec<(AuthorityId, BabeWeight)>,
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
/// Randomness for this epoch
pub randomness: [u8; VRF_OUTPUT_LENGTH],
/// Whether secondary slot assignments should be used during the epoch.
pub secondary_slots: bool,
}

/// An consensus log item for BABE.
Expand Down
Loading