Skip to content

Commit

Permalink
Add basic BABE consensus type (#2165)
Browse files Browse the repository at this point in the history
* Add basic BABE consensus type

* Update core/consensus/babe/slots/Cargo.toml

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Fix parameterization and run `rustfmt`

* Respond to review comments

* Update various Cargo.lock files

* Revert "Update various Cargo.lock files"

This reverts commit af53d7624752a744320e9cbb25749fdd8e6f46d2.

* `BabeSealSignature` → `BabeSeal`

* Move slot code to its own crate

This was highly non-trivial, due to cyclic dependencies.

* Remove redundancy between AuRa and BABE

Some of the code duplication was removed using a macro.

* Fix build error

* Avoid non-`#[doc(hidden)]` re-exports

Also, bump some library versions in `Cargo.toml`.

* Remove dead code in AuRa

* Remove impl_slot macro

It was more trouble than it was worth.

Also, delete useless dependencies on Serde.

* AuRa and BABE need different DB keys

* Bring back `aura::Network`, but deprecate it.

* Improve docs and add `slot_duration` inherent method

* Add docs to `substrate_consensus_aura::SlotDuration`

* Add missing documentation and #![forbid(missing_docs, unsafe_code)]

* Add a #![forbid(missing_docs)]

* Remove dependency of `test-runtime` on `slots`

* Update core/consensus/babe/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Remove wrongly added file

* Fix copyright notice

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Bump `impl_version` and `spec_version`

* Fix deprecation version; remove spurious carets

* Update Cargo.lock

* Update dependencies
  • Loading branch information
Demi-Marie authored and bkchr committed Apr 15, 2019
1 parent 72a1c62 commit ae916c6
Show file tree
Hide file tree
Showing 20 changed files with 1,234 additions and 833 deletions.
1,583 changes: 839 additions & 744 deletions substrate/Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ members = [
"core/client/db",
"core/consensus/common",
"core/consensus/aura",
"core/consensus/babe",
"core/consensus/rhd",
"core/consensus/slots",
"core/executor",
"core/finality-grandpa",
"core/finality-grandpa/primitives",
Expand Down
10 changes: 5 additions & 5 deletions substrate/core/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
[package]
name = "substrate-consensus-aura"
version = "1.0.0"
version = "1.0.1"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Aura consensus algorithm for substrate"
edition = "2018"

[dependencies]
parity-codec = "3.3"
client = { package = "substrate-client", path = "../../client" }
parity-codec = "3.4"
primitives = { package = "substrate-primitives", path = "../../primitives" }
runtime_support = { package = "srml-support", path = "../../../srml/support" }
runtime_primitives = { package = "sr-primitives", path = "../../sr-primitives" }
runtime_version = { package = "sr-version", path = "../../sr-version" }
runtime_io = { package = "sr-io", path = "../../sr-io" }
aura_slots = { package = "substrate-consensus-aura-slots", path = "slots" }
slots = { package = "substrate-consensus-slots", path = "../slots" }
aura_primitives = { package = "substrate-consensus-aura-primitives", path = "primitives" }
inherents = { package = "substrate-inherents", path = "../../inherents" }
srml-consensus = { path = "../../../srml/consensus" }
srml-aura = { path = "../../../srml/aura" }
client = { package = "substrate-client", path = "../../client" }
substrate-telemetry = { path = "../../telemetry" }
futures = "0.1.17"
tokio = "0.1.7"
Expand All @@ -26,6 +25,7 @@ error-chain = "0.12"
log = "0.4"
consensus_common = { package = "substrate-consensus-common", path = "../common" }
authorities = { package = "substrate-consensus-authorities", path = "../authorities" }
runtime_primitives = { package = "sr-primitives", path = "../../sr-primitives" }

[dev-dependencies]
keyring = { package = "substrate-keyring", path = "../../keyring" }
Expand Down
56 changes: 42 additions & 14 deletions substrate/core/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
//!
//! Blocks from future steps will be either deferred or rejected depending on how
//! far in the future they are.
#![deny(deprecated)]
#![deny(warnings)]
#![forbid(missing_docs, unsafe_code)]
use std::{sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fmt::Debug};

use parity_codec::{Encode, Decode};
Expand All @@ -34,10 +35,14 @@ use consensus_common::{self, Authorities, BlockImport, Environment, Proposer,
};
use consensus_common::well_known_cache_keys;
use consensus_common::import_queue::{Verifier, BasicQueue, SharedBlockImport, SharedJustificationImport};
use client::ChainHead;
use client::block_builder::api::BlockBuilder as BlockBuilderApi;
use client::blockchain::ProvideCache;
use client::runtime_api::{ApiExt, Core as CoreApi};
use client::{
ChainHead,
block_builder::api::BlockBuilder as BlockBuilderApi,
blockchain::ProvideCache,
runtime_api::{ApiExt, Core as CoreApi},
error::Result as CResult,
backend::AuxStore,
};
use aura_primitives::AURA_ENGINE_ID;
use runtime_primitives::{generic, generic::BlockId, Justification};
use runtime_primitives::traits::{
Expand All @@ -47,7 +52,7 @@ use primitives::Pair;
use inherents::{InherentDataProviders, InherentData, RuntimeString};
use authorities::AuthoritiesApi;

use futures::{Stream, Future, IntoFuture, future};
use futures::{Future, IntoFuture, future, stream::Stream};
use tokio::timer::Timeout;
use log::{warn, debug, info, trace};

Expand All @@ -57,9 +62,8 @@ use srml_aura::{
};
use substrate_telemetry::{telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG, CONSENSUS_WARN, CONSENSUS_INFO};

use aura_slots::{CheckedHeader, SlotWorker, SlotInfo, SlotCompatible};
use slots::{CheckedHeader, SlotWorker, SlotInfo, SlotCompatible};

pub use aura_slots::SlotDuration;
pub use aura_primitives::*;
pub use consensus_common::SyncOracle;

Expand All @@ -70,6 +74,10 @@ type Signature<P> = <P as Pair>::Signature;
/// handle to a gossip service or similar.
///
/// Intended to be a lightweight handle such as an `Arc`.
#[deprecated(
since = "1.0.1",
note = "This is dead code and will be removed in a future release",
)]
pub trait Network: Clone {
/// A stream of input messages for a topic.
type In: Stream<Item=Vec<u8>,Error=()>;
Expand All @@ -78,6 +86,25 @@ pub trait Network: Clone {
fn send_message(&self, slot: u64, message: Vec<u8>);
}

/// A slot duration. Create with `get_or_compute`.
pub struct SlotDuration(slots::SlotDuration<u64>);

impl SlotDuration {
/// Either fetch the slot duration from disk or compute it from the genesis
/// state.
pub fn get_or_compute<B: Block, C>(client: &C) -> CResult<Self>
where
C: AuxStore, C: ProvideRuntimeApi, C::Api: AuraApi<B>,
{
slots::SlotDuration::get_or_compute(client, |a, b| a.slot_duration(b)).map(Self)
}

/// Get the slot duration in milliseconds.
pub fn get(&self) -> u64 {
self.0.get()
}
}

/// Get slot author for given block along with authorities.
fn slot_author<P: Pair>(slot_num: u64, authorities: &[AuthorityId<P>]) -> Option<&AuthorityId<P>> {
if authorities.is_empty() { return None }
Expand Down Expand Up @@ -153,6 +180,7 @@ impl<P, Hash> CompatibleDigestItem<P> for generic::DigestItem<Hash, P::Public, P
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
struct AuraSlotCompatible;

impl SlotCompatible for AuraSlotCompatible {
Expand Down Expand Up @@ -203,8 +231,8 @@ pub fn start_aura_thread<B, C, E, I, P, SO, Error, OnExit>(
force_authoring,
};

aura_slots::start_slot_worker_thread::<_, _, _, _, AuraSlotCompatible, _>(
slot_duration,
slots::start_slot_worker_thread::<_, _, _, _, AuraSlotCompatible, u64, _>(
slot_duration.0,
client,
Arc::new(worker),
sync_oracle,
Expand Down Expand Up @@ -249,8 +277,8 @@ pub fn start_aura<B, C, E, I, P, SO, Error, OnExit>(
sync_oracle: sync_oracle.clone(),
force_authoring,
};
aura_slots::start_slot_worker::<_, _, _, _, AuraSlotCompatible, _>(
slot_duration,
slots::start_slot_worker::<_, _, _, _, _, AuraSlotCompatible, _>(
slot_duration.0,
client,
Arc::new(worker),
sync_oracle,
Expand Down Expand Up @@ -803,9 +831,9 @@ mod tests {
use client::BlockchainEvents;
use test_client;

type Error = ::client::error::Error;
type Error = client::error::Error;

type TestClient = ::client::Client<test_client::Backend, test_client::Executor, TestBlock, test_client::runtime::RuntimeApi>;
type TestClient = client::Client<test_client::Backend, test_client::Executor, TestBlock, test_client::runtime::RuntimeApi>;

struct DummyFactory(Arc<TestClient>);
struct DummyProposer(u64, Arc<TestClient>);
Expand Down
40 changes: 40 additions & 0 deletions substrate/core/consensus/babe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "substrate-consensus-babe"
version = "1.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "BABE consensus algorithm for substrate"
edition = "2018"

[dependencies]
parity-codec = "3.4.0"
parity-codec-derive = "3.3.0"
babe_primitives = { package = "substrate-consensus-babe-primitives", path = "primitives" }
primitives = { package = "substrate-primitives", path = "../../primitives" }
runtime_support = { package = "srml-support", path = "../../../srml/support" }
runtime_version = { package = "sr-version", path = "../../sr-version" }
runtime_io = { package = "sr-io", path = "../../sr-io" }
inherents = { package = "substrate-inherents", path = "../../inherents" }
srml-consensus = { path = "../../../srml/consensus" }
substrate-telemetry = { path = "../../telemetry" }
client = { package = "substrate-client", path = "../../client" }
consensus_common = { package = "substrate-consensus-common", path = "../common" }
authorities = { package = "substrate-consensus-authorities", path = "../authorities" }
slots = { package = "substrate-consensus-slots", path = "../slots" }
runtime_primitives = { package = "sr-primitives", path = "../../sr-primitives" }
futures = "0.1.26"
tokio = "0.1.18"
parking_lot = "0.7.1"
error-chain = "0.12.0"
log = "0.4.6"

[dependencies.schnorrkel]
git = "https://github.com/paritytech/schnorrkel"
branch = "master"

[dev-dependencies]
keyring = { package = "substrate-keyring", path = "../../keyring" }
substrate-executor = { path = "../../executor" }
network = { package = "substrate-network", path = "../../network", features = ["test-helpers"]}
service = { package = "substrate-service", path = "../../service" }
test_client = { package = "substrate-test-client", path = "../../test-client" }
env_logger = "0.6.1"
19 changes: 19 additions & 0 deletions substrate/core/consensus/babe/primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "substrate-consensus-babe-primitives"
version = "1.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Primitives for BABE consensus"
edition = "2018"

[dependencies]
substrate-client = { path = "../../../client", default-features = false }
runtime_primitives = { package = "sr-primitives", path = "../../../sr-primitives", default-features = false }
slots = { package = "substrate-consensus-slots", path = "../../slots", optional = true }
parity-codec = "^3.4.0"

[features]
default = ["std"]
std = [
"substrate-client/std",
"slots",
]
76 changes: 76 additions & 0 deletions substrate/core/consensus/babe/primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2019 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

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

use runtime_primitives::ConsensusEngineId;
use substrate_client::decl_runtime_apis;

use parity_codec::{Encode, Decode};

/// The `ConsensusEngineId` of BABE.
pub const BABE_ENGINE_ID: ConsensusEngineId = [b'b', b'a', b'b', b'e'];

/// Configuration data used by the BABE consensus engine.
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug, Encode, Decode)]
pub struct BabeConfiguration {
slot_duration: u64,
expected_block_time: u64,
}

impl BabeConfiguration {
/// Return the expected block time in milliseconds for BABE. Currently,
/// only the value provided by this type at genesis will be used.
///
/// Dynamic expected block time may be supported in the future.
pub fn expected_block_time(&self) -> u64 {
self.expected_block_time
}

/// 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.
pub fn slot_duration(&self) -> u64 {
self.slot_duration
}
}

#[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
}

const SLOT_KEY: &'static [u8] = b"babe_bootstrap_data";
}

decl_runtime_apis! {
/// API necessary for block authorship with BABE.
pub trait BabeApi {
/// Return the configuration for BABE. Currently,
/// only the value provided by this type at genesis will be used.
///
/// Dynamic configuration may be supported in the future.
fn startup_data() -> BabeConfiguration;
}
}
Loading

0 comments on commit ae916c6

Please sign in to comment.