Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

BABE Epochs #3028

Merged
merged 128 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 104 commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
8e01c0a
Add `epoch` field to `SlotInfo`
Demi-Marie Jul 4, 2019
5f45181
Add slot calculations
Demi-Marie Jul 4, 2019
93e9bbf
More work on epochs in BABE
Demi-Marie Jul 5, 2019
2a40e5c
Apply suggestions from code review
Demi-Marie Jul 5, 2019
d844317
Typo: `/` not `%` for division
Demi-Marie Jul 5, 2019
92b4b5e
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 5, 2019
527620e
Delete useless `LastSlotInEpoch::put(false)`
Demi-Marie Jul 5, 2019
a2bb5a3
Bump `spec_version`
Demi-Marie Jul 5, 2019
49a094d
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 6, 2019
d851119
Make test suite pass again
Demi-Marie Jul 7, 2019
a250c8d
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 8, 2019
fb033dd
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 8, 2019
1fce6b6
Implement BABE epoch randomness signing
Demi-Marie Jul 9, 2019
92e1d43
Merge remote-tracking branch 'origin/master' into demi-sign-epoch-ran…
Demi-Marie Jul 9, 2019
696ad68
Try to fix compilation
Demi-Marie Jul 9, 2019
34db79b
Fix rustc stack overflow
bkchr Jul 9, 2019
c1b84ed
Add missing `PartialEq` and `Eq` implementations
Demi-Marie Jul 9, 2019
816f488
Fix compile errors in test suite
Demi-Marie Jul 9, 2019
43132cb
Another silly compile error
Demi-Marie Jul 9, 2019
27d7070
Clone `epoch`
Demi-Marie Jul 9, 2019
2a99589
Fix compile error in benchmarks
Demi-Marie Jul 9, 2019
c859168
Implement `clone` for `Epoch`
Demi-Marie Jul 9, 2019
835de44
Merge remote-tracking branch 'origin/master' into demi-sign-epoch-ran…
Demi-Marie Jul 10, 2019
87efaaa
Merge master
Demi-Marie Jul 10, 2019
9db6862
AUTHORING TEST PASSES!!!
Demi-Marie Jul 10, 2019
a0104cf
Fix compilation
Demi-Marie Jul 10, 2019
20a1070
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 10, 2019
9f91a31
Bump `spec_version`
Demi-Marie Jul 10, 2019
b3e3ba6
Fix compilation
Demi-Marie Jul 10, 2019
5ceaf29
Fix compilation (again)
Demi-Marie Jul 10, 2019
d7fb02d
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 10, 2019
45c0303
Remove an outdated FIXME
Demi-Marie Jul 10, 2019
028ce81
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 11, 2019
05b215e
Fix run.sh and move it to scripts/
Demi-Marie Jul 11, 2019
8d8cd43
Delete commented-out code
Demi-Marie Jul 11, 2019
0e84df8
Fix documentation
Demi-Marie Jul 11, 2019
f2d7779
Fix BABE initialization and refactor
Demi-Marie Jul 11, 2019
ad8eccb
Respond to review
Demi-Marie Jul 11, 2019
6f3a52b
typo
Demi-Marie Jul 11, 2019
7e5c15e
Remove useless data in `CheckedHeader::Deferred`
Demi-Marie Jul 11, 2019
c17b020
Remove `slot_number` from Epoch
Demi-Marie Jul 11, 2019
260c968
Remove epoch from BABE digests
Demi-Marie Jul 11, 2019
9f327f3
Move digest.rs to primitives
Demi-Marie Jul 11, 2019
0b0a6a2
Fix incorrect warning names
Demi-Marie Jul 11, 2019
6b3d7b2
Fix compile error
Demi-Marie Jul 11, 2019
6978cfc
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 11, 2019
44c865f
Consistent field naming for BABE digests
Demi-Marie Jul 11, 2019
a0213a7
More compiler error fixex
Demi-Marie Jul 11, 2019
5e8363a
Unbound variable
Demi-Marie Jul 11, 2019
709ba89
more compile errors
Demi-Marie Jul 11, 2019
f34a715
another compile error
Demi-Marie Jul 11, 2019
557de53
Fix compile errors in runtime
Demi-Marie Jul 11, 2019
1987bfa
another compile error
Demi-Marie Jul 12, 2019
af63e26
Another compile error
Demi-Marie Jul 12, 2019
e7e0b8f
Fix wasm build
Demi-Marie Jul 12, 2019
ba11417
missing import
Demi-Marie Jul 12, 2019
b2564cd
Fix more compile errors
Demi-Marie Jul 12, 2019
6b8f277
yet another compile error
Demi-Marie Jul 12, 2019
5087d09
compile fix in test runtime
Demi-Marie Jul 12, 2019
141a341
Fix and simplify the BABE runtime
Demi-Marie Jul 13, 2019
67838be
Use `system` to get the test authorities
Demi-Marie Jul 13, 2019
8e5ac5c
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 13, 2019
c2483d1
Fix test runtime impl for BabeApi::epoch() with std
Demi-Marie Jul 13, 2019
a6746a0
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 13, 2019
a3e3309
Fix compilation
Demi-Marie Jul 13, 2019
9fab8e6
Cached authorities are in the form of an epoch
Demi-Marie Jul 13, 2019
dc06615
`slots_per_epoch` is not fixed in general
Demi-Marie Jul 15, 2019
2d71676
fix compile error
Demi-Marie Jul 15, 2019
078c30f
Merge remote-tracking branch 'origin/master' into demi-sign-epoch-ran…
Demi-Marie Jul 15, 2019
3f53747
Implement epoch checker
Demi-Marie Jul 16, 2019
0ddf0c4
Fix runtime compilation
Demi-Marie Jul 16, 2019
f4f6157
Merge remote-tracking branch 'origin/master' into demi-sign-epoch-ran…
Demi-Marie Jul 17, 2019
f8c20f8
fork-tree: add method for finding a node in the tree
andresilva Jul 17, 2019
548dafc
babe: register epoch transitions in fork tree and validate them
andresilva Jul 17, 2019
ca21779
fork-tree: add method for arbitrary pruning
andresilva Jul 17, 2019
49c9f6d
Merge remote-tracking branch 'origin/andre/validate-epoch-transitions…
Demi-Marie Jul 17, 2019
b34b1aa
Merge remote-tracking branch 'origin/master' into demi-sign-epoch-ran…
Demi-Marie Jul 17, 2019
ad3db4f
Expose the queued validator set to SRML modules
Demi-Marie Jul 18, 2019
5e93ed0
Bump hex-literal
Demi-Marie Jul 18, 2019
11a9cbf
babe: prune epoch change fork tree on finality
andresilva Jul 18, 2019
f2369be
babe: validate epoch index on transition
andresilva Jul 18, 2019
843b0a0
babe: persist epoch changes tree
andresilva Jul 18, 2019
62a1558
Fix compile error in tests
Demi-Marie Jul 18, 2019
ba3b73c
Fix compile error in tests
Demi-Marie Jul 18, 2019
9da2be3
Another compile error in tests
Demi-Marie Jul 18, 2019
ae05a4b
Fix compilation of tests
Demi-Marie Jul 18, 2019
b36ce84
core: move grandpa::is_descendent_of to client utils
andresilva Jul 18, 2019
b6e1433
babe: use is_descendent_of from client utils
andresilva Jul 18, 2019
c9490b7
babe: extract slot_number from pre_digest in import_block
andresilva Jul 18, 2019
f8933ca
Merge remote-tracking branch 'origin/master' into demi-sign-epoch-ran…
Demi-Marie Jul 18, 2019
5411f10
Move BABE testsuite to its own file
Demi-Marie Jul 18, 2019
9e3533f
Merge remote-tracking branch 'origin/master' into demi-sign-epoch-ran…
Demi-Marie Jul 18, 2019
64444e2
Initial part of test code
Demi-Marie Jul 19, 2019
9f9a7e5
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 19, 2019
133ae59
Missing `WeightMultiplierUpdate` in test-runtime
Demi-Marie Jul 19, 2019
22f67cb
bump `spec_version`
Demi-Marie Jul 19, 2019
e415249
Add a test that a very bogus is rejected
Demi-Marie Jul 19, 2019
122fc7d
Run the tests again
Demi-Marie Jul 19, 2019
bc4780e
Merge branch 'master' into demi-sign-epoch-randomness
Demi-Marie Jul 21, 2019
ecbdad1
Fix compiler diagnostics
Demi-Marie Jul 21, 2019
d7bc7f6
Bump `spec_version`
Demi-Marie Jul 21, 2019
2f1b26f
Initial infrastructure for mutation testing
Demi-Marie Jul 21, 2019
88b6ef1
Merge branch 'master' into HEAD
Demi-Marie Jul 22, 2019
d7352a4
Mutation testing of block import
Demi-Marie Jul 21, 2019
bbd4e53
babe: revert epoch changes in case of block import error
andresilva Jul 22, 2019
0caa5eb
babe: fix logging target
andresilva Jul 22, 2019
9268546
babe: BabeBlockImport doesn't box inner BlockImport
andresilva Jul 22, 2019
0a0b219
babe: fix epoch check in block import
andresilva Jul 22, 2019
d6b823d
babe: populate authorities cache on block authorship
andresilva Jul 22, 2019
a880626
babe: remove unused functions
andresilva Jul 22, 2019
296f30b
babe: use RANDOMNESS_LENGTH const
andresilva Jul 22, 2019
ac256c4
babe: remove unneeded config parameters
andresilva Jul 22, 2019
56c9661
core: revert change to hex dependency version
andresilva Jul 22, 2019
b786d51
Merge branch 'master' into demi-sign-epoch-randomness
andresilva Jul 22, 2019
39ec92e
cleanup gitignore
andresilva Jul 22, 2019
44a5b8f
babe: add docs to aux_schema
andresilva Jul 22, 2019
9484a48
babe: remove useless drops in tests
andresilva Jul 22, 2019
dd4dcd1
babe: remove annoying macos smart quotes
andresilva Jul 22, 2019
bba81f7
fork-tree: docs
andresilva Jul 22, 2019
6d50604
fork-tree: add tests
andresilva Jul 22, 2019
c7dd556
babe: style
andresilva Jul 22, 2019
e3f0c8e
babe: rename randomness config variable
andresilva Jul 22, 2019
fb09ef0
babe: remove randomness helper function
andresilva Jul 22, 2019
2017cd4
babe: style fixes
andresilva Jul 22, 2019
5eec764
babe: add docs
andresilva Jul 22, 2019
0cb53cc
babe: fix tests
andresilva Jul 22, 2019
a7a5d86
node: bump spec_version
andresilva Jul 22, 2019
0a2599a
babe: fix tests
andresilva Jul 22, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ rls*.log
*.orig
*.rej
**/wip/*.stderr
[0-9][0-9][0-9][0-9]-*.patch
Demi-Marie marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ WORKDIR /substrate
COPY . /substrate

RUN apt-get update && \
apt-get upgrade -y && \
apt-get dist-upgrade -y && \
apt-get install -y cmake pkg-config libssl-dev git clang

RUN curl https://sh.rustup.rs -sSf | sh -s -- -y && \
Expand All @@ -21,7 +21,7 @@ RUN curl https://sh.rustup.rs -sSf | sh -s -- -y && \
cargo install --git https://github.com/alexcrichton/wasm-gc && \
rustup default nightly && \
rustup default stable && \
cargo build --$PROFILE
cargo build "--$PROFILE"

# ===== SECOND STAGE ======

Expand Down
2 changes: 1 addition & 1 deletion core/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ derive_more = { version = "0.14.0", optional = true }
fnv = { version = "1.0", optional = true }
log = { version = "0.4", optional = true }
parking_lot = { version = "0.8.0", optional = true }
hex = { package = "hex-literal", version = "0.2", optional = true }
hex = { package = "hex-literal", version = "0.2.0", optional = true }
andresilva marked this conversation as resolved.
Show resolved Hide resolved
futures-preview = { version = "0.3.0-alpha.17", optional = true }
consensus = { package = "substrate-consensus-common", path = "../consensus/common", optional = true }
executor = { package = "substrate-executor", path = "../executor", optional = true }
Expand Down
47 changes: 47 additions & 0 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,53 @@ impl<B, E, Block, RA> backend::AuxStore for Client<B, E, Block, RA>
crate::backend::AuxStore::get_aux(&*self.backend, key)
}
}

/// Utility methods for the client.
pub mod utils {
use super::*;
use crate::{backend::Backend, blockchain, error};
use primitives::H256;

/// 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)>,
) -> impl Fn(&H256, &H256) -> Result<bool, error::Error> + 'a
where B: Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync,
{
move |base, hash| {
if base == hash { return Ok(false); }

let mut hash = hash;
if let Some((current_hash, current_parent_hash)) = current {
if base == current_hash { return Ok(false); }
if hash == current_hash {
if base == current_parent_hash {
return Ok(true);
} else {
hash = current_parent_hash;
}
}
}

let tree_route = blockchain::tree_route(
#[allow(deprecated)]
client.backend().blockchain(),
BlockId::Hash(*hash),
BlockId::Hash(*base),
)?;

Ok(tree_route.common_block().hash == *base)
}
}
}

#[cfg(test)]
pub(crate) mod tests {
use std::collections::HashMap;
Expand Down
1 change: 1 addition & 0 deletions core/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub use crate::client::{
BlockBody, BlockStatus, ImportNotifications, FinalityNotifications, BlockchainEvents,
BlockImportNotification, Client, ClientInfo, ExecutionStrategies, FinalityNotification,
LongestChain,
utils,
};
#[cfg(feature = "std")]
pub use crate::notifications::{StorageEventStream, StorageChangeSet};
Expand Down
3 changes: 2 additions & 1 deletion core/consensus/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ client = { package = "substrate-client", path = "../../client" }
consensus_common = { package = "substrate-consensus-common", path = "../common" }
slots = { package = "substrate-consensus-slots", path = "../slots" }
runtime_primitives = { package = "sr-primitives", path = "../../sr-primitives" }
fork-tree = { path = "../../utils/fork-tree" }
futures = "0.1.26"
futures03 = { package = "futures-preview", version = "0.3.0-alpha.17", features = ["compat"] }
tokio-timer = "0.2.11"
parking_lot = "0.8.0"
log = "0.4.6"
Expand All @@ -28,7 +30,6 @@ rand = "0.6.5"
merlin = "1.0.3"

[dev-dependencies]
futures03 = { package = "futures-preview", version = "0.3.0-alpha.17", features = ["compat"] }
keyring = { package = "substrate-keyring", path = "../../keyring" }
substrate-executor = { path = "../../executor" }
network = { package = "substrate-network", path = "../../network", features = ["test-helpers"]}
Expand Down
2 changes: 2 additions & 0 deletions core/consensus/babe/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ runtime_primitives = { package = "sr-primitives", path = "../../../sr-primitives
substrate-primitives = { path = "../../../primitives", default-features = false }
slots = { package = "substrate-consensus-slots", path = "../../slots", optional = true }
parity-codec = { version = "4.1.1", default-features = false }
schnorrkel = { version = "0.1.1", optional = true }

[features]
default = ["std"]
Expand All @@ -20,5 +21,6 @@ std = [
"runtime_primitives/std",
"substrate-client/std",
"parity-codec/std",
"schnorrkel",
"slots",
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,85 @@

//! Private implementation details of BABE digests.

use primitives::sr25519::Signature;
use babe_primitives::{self, BABE_ENGINE_ID, SlotNumber};
#[cfg(feature = "std")]
use substrate_primitives::sr25519::Signature;
#[cfg(feature = "std")]
use super::{BABE_ENGINE_ID, Epoch};
#[cfg(not(feature = "std"))]
use super::{VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH};
use super::SlotNumber;
#[cfg(feature = "std")]
use runtime_primitives::{DigestItem, generic::OpaqueDigestItemId};
#[cfg(feature = "std")]
use std::fmt::Debug;
use parity_codec::{Decode, Encode, Codec, Input};
use schnorrkel::{vrf::{VRFProof, VRFOutput, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH}};

/// A BABE pre-digest. It includes:
///
/// * The public key of the author.
/// * The VRF proof.
/// * The VRF output.
/// * The slot number.
#[derive(Clone, Debug, PartialEq, Eq)]
use parity_codec::{Decode, Encode};
#[cfg(feature = "std")]
use parity_codec::{Codec, Input};
#[cfg(feature = "std")]
use schnorrkel::vrf::{VRFProof, VRFOutput, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH};

/// A BABE pre-digest
#[cfg(feature = "std")]
#[derive(Clone, Debug)]
pub struct BabePreDigest {
pub(super) vrf_output: VRFOutput,
pub(super) proof: VRFProof,
pub(super) index: babe_primitives::AuthorityIndex,
pub(super) slot_num: SlotNumber,
/// 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,
}

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

type RawBabePreDigest = (
[u8; VRF_OUTPUT_LENGTH],
[u8; VRF_PROOF_LENGTH],
u64,
u64,
);
/// 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],
}

#[cfg(feature = "std")]
impl Encode for BabePreDigest {
fn encode(&self) -> Vec<u8> {
let tmp: RawBabePreDigest = (
*self.vrf_output.as_bytes(),
self.proof.to_bytes(),
self.index,
self.slot_num,
);
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,
};
parity_codec::Encode::encode(&tmp)
}
}

#[cfg(feature = "std")]
impl Decode for BabePreDigest {
fn decode<R: Input>(i: &mut R) -> Option<Self> {
let (output, proof, index, slot_num): RawBabePreDigest = Decode::decode(i)?;
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; babe_primitives::VRF_OUTPUT_LENGTH] = output;
let _: [u8; babe_primitives::VRF_PROOF_LENGTH] = proof;
let _: [u8; super::VRF_OUTPUT_LENGTH] = vrf_output;
let _: [u8; super::VRF_PROOF_LENGTH] = vrf_proof;
Some(BabePreDigest {
proof: VRFProof::from_bytes(&proof).ok()?,
vrf_output: VRFOutput::from_bytes(&output).ok()?,
index,
slot_num,
vrf_proof: VRFProof::from_bytes(&vrf_proof).ok()?,
vrf_output: VRFOutput::from_bytes(&vrf_output).ok()?,
authority_index,
slot_number,
})
}
}

/// A digest item which is usable with BABE consensus.
#[cfg(feature = "std")]
pub trait CompatibleDigestItem: Sized {
/// Construct a digest item which contains a BABE pre-digest.
fn babe_pre_digest(seal: BabePreDigest) -> Self;
Expand All @@ -88,8 +107,12 @@ pub trait CompatibleDigestItem: Sized {

/// If this item is a BABE signature, return the signature.
fn as_babe_seal(&self) -> Option<Signature>;

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

#[cfg(feature = "std")]
impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
Hash: Debug + Send + Sync + Eq + Clone + Codec + 'static
{
Expand All @@ -108,4 +131,8 @@ impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
fn as_babe_seal(&self) -> Option<Signature> {
self.try_to(OpaqueDigestItemId::Seal(&BABE_ENGINE_ID))
}

fn as_babe_epoch(&self) -> Option<Epoch> {
self.try_to(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID))
}
}
37 changes: 32 additions & 5 deletions core/consensus/babe/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Primitives for BABE.
#![deny(warnings, unsafe_code, missing_docs)]
#![deny(warnings)]
#![forbid(unsafe_code, missing_docs, unused_variables, unused_imports)]
#![cfg_attr(not(feature = "std"), no_std)]

mod digest;

use parity_codec::{Encode, Decode};
use rstd::vec::Vec;
use runtime_primitives::ConsensusEngineId;
use substrate_primitives::sr25519::Public;
use substrate_client::decl_runtime_apis;

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

/// A Babe authority identifier. Necessarily equivalent to the schnorrkel public key used in
/// the main Babe module. If that ever changes, then this must, too.
pub type AuthorityId = Public;
Expand All @@ -49,15 +56,29 @@ pub type SlotNumber = u64;
/// The weight of an authority.
pub type Weight = u64;

/// BABE epoch information
#[derive(Decode, Encode, Default, PartialEq, Eq, Clone)]
#[cfg_attr(any(feature = "std", test), derive(Debug))]
pub struct Epoch {
Demi-Marie marked this conversation as resolved.
Show resolved Hide resolved
/// The authorities and their weights
pub authorities: Vec<(AuthorityId, Weight)>,
/// The epoch index
pub epoch_index: u64,
/// Randomness for this epoch
pub randomness: [u8; VRF_OUTPUT_LENGTH],
/// The duration of this epoch
pub duration: SlotNumber,
}

/// An consensus log item for BABE.
#[derive(Decode, Encode)]
#[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.
#[codec(index = "1")]
NextEpochData(SlotNumber, Vec<(AuthorityId, Weight)>, [u8; VRF_OUTPUT_LENGTH]),
NextEpochData(Epoch),
Demi-Marie marked this conversation as resolved.
Show resolved Hide resolved
/// Disable the authority with given index.
#[codec(index = "2")]
OnDisabled(AuthorityIndex),
Expand All @@ -72,6 +93,12 @@ pub struct BabeConfiguration {
/// Dynamic slot duration may be supported in the future.
pub slot_duration: u64,

/// The number of slots per BABE epoch. Currently, only
/// the value provided by this type at genesis will be used.
///
/// Dynamic slot duration may be supported in the future.
pub slots_per_epoch: u64,

/// The expected block time in milliseconds for BABE. Currently,
/// only the value provided by this type at genesis will be used.
///
Expand Down Expand Up @@ -116,7 +143,7 @@ decl_runtime_apis! {
/// Dynamic configuration may be supported in the future.
fn startup_data() -> BabeConfiguration;

/// Get the current authorites for Babe.
fn authorities() -> Vec<AuthorityId>;
/// Get the current epoch data for Babe.
fn epoch() -> Epoch;
}
}
Loading