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 8 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
9fffb56
always fetch epoch from runtime
rphmeier Sep 3, 2019
86fb033
node integration tests don't test light nodes
rphmeier Sep 3, 2019
82d654b
give stand-in full node a FULL role
rphmeier Sep 10, 2019
1c8c47f
rejig babe APIs
rphmeier Sep 12, 2019
0feea79
introduce next-epoch-descriptor type
rphmeier Sep 12, 2019
75c1b59
overhaul srml-BABE epoch logic
rphmeier Sep 13, 2019
182e344
ensure VRF outputs end up in the right epoch-randomness
rphmeier Sep 13, 2019
d2d3942
rewrite `do_initialize` to remove unnecessary loop
rphmeier Sep 16, 2019
59bc715
begin accounting for next epoch in epoch function
rphmeier Sep 11, 2019
8f4f0b4
slots passes header to epoch_data
rphmeier Sep 11, 2019
e9ee8cb
pass slot_number to SlotWorker::epoch_data
rphmeier Sep 12, 2019
e8a7e58
begin extracting epoch-change logic into its own module
rphmeier Sep 16, 2019
645f5f9
aux methods for block weight
rphmeier Sep 16, 2019
3a72f79
aux methods for genesis configuration
rphmeier Sep 16, 2019
eba402b
comment-out most, refactor header-check pipeline
rphmeier Sep 16, 2019
ddb07b0
mostly flesh out verifier again
rphmeier Sep 16, 2019
769eebc
reinstantiate babe BlockImport implementation
rphmeier Sep 16, 2019
b420b4e
reinstate import-queue instantiation
rphmeier Sep 16, 2019
c183bed
reintroduce slot-worker implementation
rphmeier Sep 16, 2019
58caeba
reinstate pretty much all the rest
rphmeier Sep 16, 2019
39ed2dd
move fork-choice logic to BlockImport
rphmeier Sep 16, 2019
d857474
fix some, but not all errors
rphmeier Sep 16, 2019
da2de6d
patch test-runtime
rphmeier Sep 16, 2019
84e9847
make is_descendent of slightly more generic
rphmeier Sep 16, 2019
e98afca
get skeleton compiling when passing is_descendent_of
rphmeier Sep 16, 2019
0af3ddc
make descendent-of-builder more succinct
rphmeier Sep 16, 2019
c333c7a
restore ordering of authority_index / slot_number
rphmeier Sep 16, 2019
73ff5ab
start fiddling with tests
rphmeier Sep 16, 2019
d3d5cbe
fix warnings
rphmeier Sep 17, 2019
d33d9e7
improve initialization architecture and handle genesis
rphmeier Sep 17, 2019
8f3862d
tests use correct block-import
rphmeier Sep 17, 2019
fbed0f5
fix BABE tests
rphmeier Sep 17, 2019
aff86a1
fix some compiler errors
rphmeier Sep 17, 2019
8af8961
fix node-cli compilation
rphmeier Sep 18, 2019
ff05c25
all crates compile
rphmeier Sep 18, 2019
77e20e3
bump runtime versions and fix some warnings
rphmeier Sep 18, 2019
172c2b5
tweak fork-tree search implementation
rphmeier Sep 18, 2019
60c5564
do backtracking search in fork-tree
rphmeier Sep 18, 2019
6612892
node-cli integration tests now work
rphmeier Sep 18, 2019
a82f7f6
fix broken assumption in test_connectivity
rphmeier Sep 19, 2019
058c262
babe tests fail for the right reasons.
rphmeier Sep 19, 2019
5831814
test genesis epoch logic for epoch_changes
rphmeier Sep 19, 2019
5cb0615
test that epochs can change between blocks
rphmeier Sep 19, 2019
c9a3321
First BABE SRML test
Demi-Marie Aug 5, 2019
301fdde
Testing infrastructure for BABE
Demi-Marie Aug 6, 2019
cf659a6
Apply suggestions from code review
Demi-Marie Aug 6, 2019
72b65e9
A little more test progress
Demi-Marie Aug 7, 2019
a3abe47
More work on BABE testing
Demi-Marie Aug 9, 2019
2a33d8e
Try to get the tests working
Demi-Marie Aug 9, 2019
bb46c41
Implement `UintAuthorityId`-based test mocks
Demi-Marie Aug 9, 2019
49bee9e
Fix compilation errors
Demi-Marie Aug 16, 2019
56cd1ed
Adjust to upstream changes
Demi-Marie Aug 18, 2019
9b0b11a
Block numbers are ignored in BABE epoch calculation
Demi-Marie Aug 19, 2019
f4e7039
authority_index() should ignore invalid authorities
Demi-Marie Aug 19, 2019
1a19b73
Fix compile error
Demi-Marie Aug 26, 2019
5d5e96c
Add tests that session transitions happen
Demi-Marie Aug 26, 2019
9071f07
Check if BABE produces logs
Demi-Marie Aug 28, 2019
834c70e
Fix test suite
Demi-Marie Aug 28, 2019
357603d
Add additional tests
Demi-Marie Aug 29, 2019
8f37eb2
Make the tests more readable
Demi-Marie Aug 30, 2019
e330702
Fix excessive line width
Demi-Marie Aug 30, 2019
c8a2224
Remove unused imports
Demi-Marie Sep 3, 2019
f2d1621
Update srml/babe/src/lib.rs
Demi-Marie Sep 3, 2019
3224a5c
try to fix imports
Demi-Marie Sep 12, 2019
bea957c
Fix build errors in test suite
Demi-Marie Sep 12, 2019
ab61939
tests did not pass
Demi-Marie Sep 13, 2019
f632d33
Try to get at least one digest to be output
Demi-Marie Sep 17, 2019
52ba30f
More tests
Demi-Marie Sep 18, 2019
1be5b38
fix silly error
Demi-Marie Sep 18, 2019
4bba10c
Don’t even try to compile a broken test
Demi-Marie Sep 18, 2019
28c9eaf
remove broken check_epoch test and add one for genesis epoch
rphmeier Sep 19, 2019
309f73c
Check that the length of the pre-digests is correct
Demi-Marie Sep 19, 2019
6e43426
Bump `impl_version`
Demi-Marie Sep 19, 2019
c15a2c3
use epoch_for_descendent_of even for genesis
rphmeier Sep 20, 2019
0316da0
account for competing block 1s
rphmeier Sep 23, 2019
158bce7
Merge branch 'master' into rh-fix-babe-epochs
rphmeier Sep 23, 2019
2a8fff1
finish srml-babe docs
rphmeier Sep 23, 2019
c7d8ea6
address grumbles
rphmeier Sep 23, 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
53 changes: 23 additions & 30 deletions core/consensus/babe/primitives/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
//! Private implementation details of BABE digests.

#[cfg(feature = "std")]
use super::AuthoritySignature;
#[cfg(feature = "std")]
use super::{BABE_ENGINE_ID, Epoch};
use super::{BABE_ENGINE_ID, AuthoritySignature};
#[cfg(not(feature = "std"))]
use super::{VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH};
use super::{AuthorityIndex, BabeBlockWeight, SlotNumber};
use super::{AuthorityId, AuthorityIndex, SlotNumber, BabeAuthorityWeight};
#[cfg(feature = "std")]
use sr_primitives::{DigestItem, generic::OpaqueDigestItemId};
#[cfg(feature = "std")]
Expand All @@ -35,6 +33,8 @@ use schnorrkel::{
SignatureError, errors::MultiSignatureStage,
vrf::{VRFProof, VRFOutput, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH}
};
use rstd::vec::Vec;


/// 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
Expand All @@ -52,17 +52,13 @@ pub enum BabePreDigest {
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,
},
}

Expand All @@ -83,14 +79,6 @@ impl BabePreDigest {
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.
Expand All @@ -100,26 +88,24 @@ pub const BABE_VRF_PREFIX: &'static [u8] = b"substrate-babe-vrf";
#[derive(Copy, Clone, Encode, Decode)]
pub enum RawBabePreDigest {
/// A primary VRF-based slot assignment.
#[codec(index = "1")]
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.
#[codec(index = "2")]
Secondary {
/// Authority index
authority_index: AuthorityIndex,
/// Slot number
slot_number: SlotNumber,
/// Chain weight (measured in number of Primary blocks)
weight: BabeBlockWeight,
},
}

Expand All @@ -142,25 +128,21 @@ impl Encode for BabePreDigest {
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,
}
},
};
Expand All @@ -176,7 +158,7 @@ impl codec::EncodeLike for BabePreDigest {}
impl Decode for BabePreDigest {
fn decode<R: Input>(i: &mut R) -> Result<Self, Error> {
let pre_digest = match Decode::decode(i)? {
RawBabePreDigest::Primary { vrf_output, vrf_proof, authority_index, slot_number, weight } => {
RawBabePreDigest::Primary { vrf_output, vrf_proof, authority_index, slot_number } => {
// 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;
Expand All @@ -186,18 +168,29 @@ impl Decode for BabePreDigest {
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 }
RawBabePreDigest::Secondary { authority_index, slot_number } => {
BabePreDigest::Secondary { authority_index, slot_number }
},
};

Ok(pre_digest)
}
}

/// Information about the next epoch. This is broadcast in the first block
/// of the epoch.
#[derive(Decode, Encode, Default, PartialEq, Eq, Clone)]
#[cfg_attr(any(feature = "std", test), derive(Debug))]
pub struct NextEpochDescriptor {
/// The authorities.
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,

/// The value of randomness to use for the slot-assignment.
pub randomness: [u8; VRF_OUTPUT_LENGTH],
}

/// A digest item which is usable with BABE consensus.
#[cfg(feature = "std")]
pub trait CompatibleDigestItem: Sized {
Expand All @@ -214,7 +207,7 @@ pub trait CompatibleDigestItem: Sized {
fn as_babe_seal(&self) -> Option<AuthoritySignature>;

/// If this item is a BABE epoch, return it.
fn as_babe_epoch(&self) -> Option<Epoch>;
fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor>;
}

#[cfg(feature = "std")]
Expand All @@ -237,7 +230,7 @@ impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
self.try_to(OpaqueDigestItemId::Seal(&BABE_ENGINE_ID))
}

fn as_babe_epoch(&self) -> Option<Epoch> {
fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor> {
self.try_to(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID))
}
}
Expand Down
54 changes: 27 additions & 27 deletions core/consensus/babe/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use substrate_client::decl_runtime_apis;

#[cfg(feature = "std")]
pub use digest::{BabePreDigest, CompatibleDigestItem};
pub use digest::{BABE_VRF_PREFIX, RawBabePreDigest};
pub use digest::{BABE_VRF_PREFIX, RawBabePreDigest, NextEpochDescriptor};

mod app {
use app_crypto::{app_crypto, key_types::BABE, sr25519};
Expand Down Expand Up @@ -59,6 +59,11 @@ pub const VRF_PROOF_LENGTH: usize = 64;
/// The length of the public key
pub const PUBLIC_KEY_LENGTH: usize = 32;

/// How many blocks to wait before running the median algorithm for relative time
/// This will not vary from chain to chain as it is not dependent on slot duration
/// or epoch length.
pub const MEDIAN_ALGORITHM_CARDINALITY: usize = 1200; // arbitrary suggestion by w3f-research.

/// The index of an authority.
pub type AuthorityIndex = u32;

Expand Down Expand Up @@ -87,57 +92,55 @@ pub struct Epoch {
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.
#[derive(Decode, Encode, Clone, PartialEq, Eq)]
pub enum ConsensusLog {
/// The epoch has changed. This provides information about the
/// epoch _after_ next: what slot number it will start at, who are the authorities (and their weights)
/// and the next epoch randomness. The information for the _next_ epoch should already
/// be available.
/// The epoch has changed. This provides information about the _next_
/// epoch - information about the _current_ epoch (i.e. the one we've just
/// entered) should already be available earlier in the chain.
#[codec(index = "1")]
NextEpochData(Epoch),
NextEpochData(NextEpochDescriptor),
/// Disable the authority with given index.
#[codec(index = "2")]
OnDisabled(AuthorityIndex),
}

/// Configuration data used by the BABE consensus engine.
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug, Encode, Decode)]
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(any(feature = "std", test), derive(Debug))]
pub struct BabeConfiguration {
/// The slot duration in milliseconds for BABE. Currently, only
/// the value provided by this type at genesis will be used.
///
/// Dynamic slot duration may be supported in the future.
pub slot_duration: u64,

/// The duration of epochs in slots.
pub epoch_length: SlotNumber,

/// A constant value that is used in the threshold calculation formula.
/// Expressed as a fraction where the first member of the tuple is the
/// numerator and the second is the denominator. The fraction should
/// Expressed as a rational where the first member of the tuple is the
/// numerator and the second is the denominator. The rational should
/// represent a value between 0 and 1.
/// In the threshold formula calculation, `1 - c` represents the probability
/// of a slot being empty.
pub c: (u64, u64),

/// The minimum number of blocks that must be received before running the
/// median algorithm to compute the offset between the on-chain time and the
/// local time. Currently, only the value provided by this type at genesis
/// will be used, but this is subject to change.
///
/// Blocks less than `self.median_required_blocks` must be generated by an
/// *initial validator* ― that is, a node that was a validator at genesis.
pub median_required_blocks: u64,
/// The authorities for the genesis epoch.
pub genesis_authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,

/// The randomness for this epoch.
pub randomness: [u8; VRF_OUTPUT_LENGTH],

/// Whether this chain should run with secondary slots, which are assigned
/// in round-robin manner.
pub secondary_slots: bool,
}

#[cfg(feature = "std")]
impl slots::SlotData for BabeConfiguration {
/// Return the slot duration in milliseconds for BABE. Currently, only
/// the value provided by this type at genesis will be used.
///
/// Dynamic slot duration may be supported in the future.
fn slot_duration(&self) -> u64 {
self.slot_duration
}
Expand All @@ -152,9 +155,6 @@ decl_runtime_apis! {
/// only the value provided by this type at genesis will be used.
///
/// Dynamic configuration may be supported in the future.
fn startup_data() -> BabeConfiguration;

/// Get the current epoch data for Babe.
fn epoch() -> Epoch;
fn configuration() -> BabeConfiguration;
}
}
Loading