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
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

26 changes: 19 additions & 7 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,9 @@ impl<B, E, Block, RA> CallRuntimeAt<Block> for Client<B, E, Block, RA> where
}
}

/// NOTE: only use this implementation when you are sure there are NO consensus-level BlockImport
/// objects. Otherwise, importing blocks directly into the client would be bypassing
/// important verification work.
impl<'a, B, E, Block, RA> consensus::BlockImport<Block> for &'a Client<B, E, Block, RA> where
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync,
Expand All @@ -1491,6 +1494,13 @@ impl<'a, B, E, Block, RA> consensus::BlockImport<Block> for &'a Client<B, E, Blo

/// Import a checked and validated block. If a justification is provided in
/// `BlockImportParams` then `finalized` *must* be true.
///
/// NOTE: only use this implementation when there are NO consensus-level BlockImport
/// objects. Otherwise, importing blocks directly into the client would be bypassing
/// important verification work.
///
/// If you are not sure that there are no BlockImport objects provided by the consensus
/// algorithm, don't use this function.
fn import_block(
&mut self,
import_block: BlockImportParams<Block>,
Expand Down Expand Up @@ -1899,25 +1909,27 @@ where
/// Utility methods for the client.
pub mod utils {
use super::*;
use crate::{backend::Backend, blockchain, error};
use crate::{blockchain, error};
use primitives::H256;
use std::borrow::Borrow;

/// Returns a function for checking block ancestry, the returned function will
/// return `true` if the given hash (second parameter) is a descendent of the
/// base (first parameter). If the `current` parameter is defined, it should
/// represent the current block `hash` and its `parent hash`, if given the
/// function that's returned will assume that `hash` isn't part of the local DB
/// yet, and all searches in the DB will instead reference the parent.
pub fn is_descendent_of<'a, B, E, Block: BlockT<Hash=H256>, RA>(
client: &'a Client<B, E, Block, RA>,
current: Option<(&'a H256, &'a H256)>,
pub fn is_descendent_of<'a, Block: BlockT<Hash=H256>, T, H: Borrow<H256> + 'a>(
client: &'a T,
current: Option<(H, H)>,
) -> impl Fn(&H256, &H256) -> Result<bool, error::Error> + 'a
where B: Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync,
where T: ChainHeaderBackend<Block>,
{
move |base, hash| {
if base == hash { return Ok(false); }

let current = current.as_ref().map(|(c, p)| (c.borrow(), p.borrow()));

let mut hash = hash;
if let Some((current_hash, current_parent_hash)) = current {
if base == current_hash { return Ok(false); }
Expand All @@ -1931,7 +1943,7 @@ pub mod utils {
}

let tree_route = blockchain::tree_route(
|id| client.header(&id)?.ok_or_else(|| Error::UnknownBlock(format!("{:?}", id))),
|id| client.header(id)?.ok_or_else(|| Error::UnknownBlock(format!("{:?}", id))),
BlockId::Hash(*hash),
BlockId::Hash(*base),
)?;
Expand Down
6 changes: 3 additions & 3 deletions core/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ impl<H, B, C, E, I, P, Error, SO> slots::SimpleSlotWorker<B> for AuraWorker<C, E
self.block_import.clone()
}

fn epoch_data(&self, block: &B::Hash) -> Result<Self::EpochData, consensus_common::Error> {
authorities(self.client.as_ref(), &BlockId::Hash(*block))
fn epoch_data(&self, header: &B::Header, _slot_number: u64) -> Result<Self::EpochData, consensus_common::Error> {
authorities(self.client.as_ref(), &BlockId::Hash(header.hash()))
}

fn authorities_len(&self, epoch_data: &Self::EpochData) -> usize {
Expand Down Expand Up @@ -741,7 +741,7 @@ mod tests {
}
}

fn make_verifier(&self, client: PeersClient, _cfg: &ProtocolConfig)
fn make_verifier(&self, client: PeersClient, _cfg: &ProtocolConfig, _peer_data: &())
-> Self::Verifier
{
match client {
Expand Down
63 changes: 37 additions & 26 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 @@ -84,11 +80,12 @@ impl BabePreDigest {
}
}

/// Returns the weight of the pre digest.
pub fn weight(&self) -> BabeBlockWeight {
/// Returns the weight _added_ by this digest, not the cumulative weight
/// of the chain.
pub fn added_weight(&self) -> crate::BabeBlockWeight {
match self {
BabePreDigest::Primary { weight, .. } => *weight,
BabePreDigest::Secondary { weight, .. } => *weight,
BabePreDigest::Primary { .. } => 1,
BabePreDigest::Secondary { .. } => 0,
}
}
}
Expand All @@ -100,26 +97,29 @@ 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
///
/// This is not strictly-speaking necessary, since the secondary slots
/// are assigned based on slot number and epoch randomness. But including
/// it makes things easier for higher-level users of the chain data to
/// be aware of the author of a secondary-slot block.
authority_index: AuthorityIndex,
/// Slot number
slot_number: SlotNumber,
/// Chain weight (measured in number of Primary blocks)
weight: BabeBlockWeight,
},
}

Expand All @@ -142,25 +142,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 +172,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 +182,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 +221,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,8 +244,12 @@ 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))
.and_then(|x: super::ConsensusLog| match x {
super::ConsensusLog::NextEpochData(n) => Some(n),
_ => None,
})
}
}

Expand Down
Loading