Add basic BABE consensus type (#2165)

* 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
This commit is contained in:
DemiMarie-parity
2019-04-15 01:41:07 -04:00
committed by Bastian Köcher
parent 72a1c6298e
commit ae916c6c5e
20 changed files with 1242 additions and 841 deletions
+847 -752
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -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",
+5 -5
View File
@@ -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"
@@ -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" }
+42 -14
View File
@@ -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};
@@ -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::{
@@ -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};
@@ -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;
@@ -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=()>;
@@ -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 }
@@ -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 {
@@ -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,
@@ -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,
@@ -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>);
+40
View File
@@ -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"
@@ -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",
]
@@ -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;
}
}
+102
View File
@@ -0,0 +1,102 @@
// 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/>.
//! BABE (Blind Assignment for Blockchain Extension) consensus in substrate.
#![forbid(warnings, unsafe_code, missing_docs)]
pub use babe_primitives::*;
use parity_codec::{Decode, Encode, Input};
use runtime_primitives::generic;
use primitives::sr25519::{
Public,
Signature,
LocalizedSignature,
};
use schnorrkel::{
SecretKey as Secret,
vrf::{VRFProof, VRF_PROOF_LENGTH},
PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH,
};
/// A BABE seal. It includes:
///
/// * The public key
/// * The VRF proof
/// * The signature
/// * The slot number
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BabeSeal {
proof: VRFProof,
signature: LocalizedSignature,
slot_num: u64,
}
impl Encode for BabeSeal {
fn encode(&self) -> Vec<u8> {
parity_codec::Encode::encode(&(
self.proof.to_bytes(),
self.signature.signature.0,
self.signature.signer.0,
self.slot_num,
))
}
}
impl Decode for BabeSeal {
fn decode<R: Input>(i: &mut R) -> Option<Self> {
let (public_key, proof, sig, slot_num): (
[u8; PUBLIC_KEY_LENGTH],
[u8; VRF_PROOF_LENGTH],
[u8; SIGNATURE_LENGTH],
u64,
) = Decode::decode(i)?;
Some(BabeSeal {
proof: VRFProof::from_bytes(&proof).ok()?,
signature: LocalizedSignature {
signature: Signature(sig),
signer: Public(public_key),
},
slot_num,
})
}
}
/// A digest item which is usable with BABE consensus.
pub trait CompatibleDigestItem: Sized {
/// Construct a digest item which contains a slot number and a signature on the
/// hash.
fn babe_seal(signature: BabeSeal) -> Self;
/// If this item is an Babe seal, return the slot number and signature.
fn as_babe_seal(&self) -> Option<BabeSeal>;
}
impl<Hash> CompatibleDigestItem for generic::DigestItem<Hash, Public, Secret> {
/// Construct a digest item which is aaAASSAAAAAASDC a slot number and a signature on the
/// hash.
fn babe_seal(signature: BabeSeal) -> Self {
generic::DigestItem::Consensus(BABE_ENGINE_ID, signature.encode())
}
/// If this item is an BABE seal, return the slot number and signature.
fn as_babe_seal(&self) -> Option<BabeSeal> {
match self {
generic::DigestItem::Consensus(BABE_ENGINE_ID, seal) => Decode::decode(&mut &seal[..]),
_ => None,
}
}
}
@@ -1,5 +1,5 @@
[package]
name = "substrate-consensus-aura-slots"
name = "substrate-consensus-slots"
version = "1.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
description = "Generic slots-based utilities for consensus"
@@ -7,12 +7,11 @@ edition = "2018"
[dependencies]
codec = { package = "parity-codec", version = "3.2" }
client = { package = "substrate-client", path = "../../../client" }
primitives = { package = "substrate-primitives", path = "../../../primitives" }
runtime_primitives = { package = "sr-primitives", path = "../../../sr-primitives" }
aura_primitives = { package = "substrate-consensus-aura-primitives", path = "../primitives" }
consensus_common = { package = "substrate-consensus-common", path = "../../common" }
inherents = { package = "substrate-inherents", path = "../../../inherents" }
client = { package = "substrate-client", path = "../../client" }
primitives = { package = "substrate-primitives", path = "../../primitives" }
runtime_primitives = { package = "sr-primitives", path = "../../sr-primitives" }
consensus_common = { package = "substrate-consensus-common", path = "../common" }
inherents = { package = "substrate-inherents", path = "../../inherents" }
futures = "0.1.17"
tokio = "0.1.7"
parking_lot = "0.7.1"
@@ -14,25 +14,35 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Slots functionality for Substrate.
//!
//! Some consensus algorithms have a concept of *slots*, which are intervals in
//! time during which certain events can and/or must occur. This crate
//! provides generic functionality for slots.
#![forbid(warnings, unsafe_code, missing_docs)]
mod slots;
pub use slots::{Slots, SlotInfo};
use std::sync::{mpsc, Arc};
use std::thread;
use std::fmt::Debug;
use futures::prelude::*;
use futures::{Future, IntoFuture, future::{self, Either}};
use log::{warn, debug, info};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{ProvideRuntimeApi, Block};
use runtime_primitives::traits::{ProvideRuntimeApi, Block, ApiRef};
use consensus_common::SyncOracle;
use inherents::{InherentData, InherentDataProviders};
use aura_primitives::AuraApi;
use client::ChainHead;
use codec::Encode;
use codec::{Encode, Decode};
/// A worker that should be invoked at every new slot.
pub trait SlotWorker<B: Block> {
/// The type fo the future that will be returned when a new slot is
/// triggered.
type OnSlot: IntoFuture<Item=(), Error=consensus_common::Error>;
/// Called when the proposer starts.
@@ -61,8 +71,8 @@ pub fn inherent_to_common_error(err: inherents::RuntimeString) -> consensus_comm
}
/// Start a new slot worker in a separate thread.
pub fn start_slot_worker_thread<B, C, W, SO, SC, OnExit>(
slot_duration: SlotDuration,
pub fn start_slot_worker_thread<B, C, W, SO, SC, T, OnExit>(
slot_duration: SlotDuration<T>,
client: Arc<C>,
worker: Arc<W>,
sync_oracle: SO,
@@ -74,7 +84,8 @@ pub fn start_slot_worker_thread<B, C, W, SO, SC, OnExit>(
W: SlotWorker<B> + Send + Sync + 'static,
SO: SyncOracle + Send + Clone + 'static,
SC: SlotCompatible + 'static,
OnExit: Future<Item=(), Error=()> + Send + 'static
OnExit: Future<Item=(), Error=()> + Send + 'static,
T: SlotData + Send + Clone + 'static,
{
use tokio::runtime::current_thread::Runtime;
@@ -89,8 +100,8 @@ pub fn start_slot_worker_thread<B, C, W, SO, SC, OnExit>(
}
};
let slot_worker_future = match start_slot_worker::<_, _, _, _, SC, _>(
slot_duration,
let slot_worker_future = match start_slot_worker::<_, _, _, _, _, SC, _>(
slot_duration.clone(),
client,
worker,
sync_oracle,
@@ -114,12 +125,12 @@ pub fn start_slot_worker_thread<B, C, W, SO, SC, OnExit>(
let _ = runtime.block_on(slot_worker_future);
});
result_recv.recv().expect("Aura start thread result sender dropped")
result_recv.recv().expect("Slots start thread result sender dropped")
}
/// Start a new slot worker.
pub fn start_slot_worker<B, C, W, SO, SC, OnExit>(
slot_duration: SlotDuration,
pub fn start_slot_worker<B, C, W, T, SO, SC, OnExit>(
slot_duration: SlotDuration<T>,
client: Arc<C>,
worker: Arc<W>,
sync_oracle: SO,
@@ -132,19 +143,20 @@ pub fn start_slot_worker<B, C, W, SO, SC, OnExit>(
SO: SyncOracle + Send + Clone,
SC: SlotCompatible,
OnExit: Future<Item=(), Error=()>,
T: SlotData + Clone,
{
worker.on_start(slot_duration.0)?;
worker.on_start(slot_duration.slot_duration())?;
let make_authorship = move || {
let client = client.clone();
let worker = worker.clone();
let sync_oracle = sync_oracle.clone();
let SlotDuration(slot_duration) = slot_duration;
let SlotDuration(slot_duration) = slot_duration.clone();
let inherent_data_providers = inherent_data_providers.clone();
// rather than use a timer interval, we schedule our waits ourselves
Slots::<SC>::new(slot_duration, inherent_data_providers)
.map_err(|e| debug!(target: "aura", "Faulty timer: {:?}", e))
Slots::<SC>::new(slot_duration.slot_duration(), inherent_data_providers)
.map_err(|e| debug!(target: "slots", "Faulty timer: {:?}", e))
.for_each(move |slot_info| {
let client = client.clone();
let worker = worker.clone();
@@ -152,7 +164,7 @@ pub fn start_slot_worker<B, C, W, SO, SC, OnExit>(
// only propose when we are not syncing.
if sync_oracle.is_major_syncing() {
debug!(target: "aura", "Skipping proposal slot due to sync.");
debug!(target: "slots", "Skipping proposal slot due to sync.");
return Either::B(future::ok(()));
}
@@ -160,7 +172,7 @@ pub fn start_slot_worker<B, C, W, SO, SC, OnExit>(
let chain_head = match client.best_block_header() {
Ok(x) => x,
Err(e) => {
warn!(target: "aura", "Unable to author block in slot {}. \
warn!(target: "slots", "Unable to author block in slot {}. \
no best block header: {:?}", slot_num, e);
return Either::B(future::ok(()))
}
@@ -168,7 +180,7 @@ pub fn start_slot_worker<B, C, W, SO, SC, OnExit>(
Either::A(
worker.on_slot(chain_head, slot_info).into_future()
.map_err(|e| debug!(target: "aura", "Encountered aura error: {:?}", e))
.map_err(|e| debug!(target: "slots", "Encountered consensus error: {:?}", e))
)
})
};
@@ -178,13 +190,13 @@ pub fn start_slot_worker<B, C, W, SO, SC, OnExit>(
authorship_task.catch_unwind().then(|res| {
match res {
Ok(Ok(())) => (),
Ok(Err(())) => warn!("Aura authorship task terminated unexpectedly. Restarting"),
Ok(Err(())) => warn!(target: "slots", "Authorship task terminated unexpectedly. Restarting"),
Err(e) => {
if let Some(s) = e.downcast_ref::<&'static str>() {
warn!("Aura authorship task panicked at {:?}", s);
warn!(target: "slots", "Authorship task panicked at {:?}", s);
}
warn!("Restarting Aura authorship task");
warn!(target: "slots", "Restarting authorship task");
}
}
@@ -207,32 +219,49 @@ pub enum CheckedHeader<H, S> {
Checked(H, S),
}
/// A type from which a slot duration can be obtained.
pub trait SlotData {
/// Gets the slot duration.
fn slot_duration(&self) -> u64;
/// The static slot key
const SLOT_KEY: &'static [u8];
}
impl SlotData for u64 {
fn slot_duration(&self) -> u64 { *self }
const SLOT_KEY: &'static [u8] = b"aura_slot_duration";
}
/// A slot duration. Create with `get_or_compute`.
// The internal member should stay private here.
#[derive(Clone, Copy, Debug)]
pub struct SlotDuration(u64);
#[derive(Clone, Copy, Debug, Encode, Decode, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct SlotDuration<T: Clone>(T);
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) -> ::client::error::Result<Self> where
impl<T: Clone> SlotDuration<T> {
/// Either fetch the slot duration from disk or compute it from the
/// genesis state.
///
/// `slot_key` is marked as `'static`, as it should really be a
/// compile-time constant.
pub fn get_or_compute<B: Block, C, CB>(client: &C, cb: CB) -> ::client::error::Result<Self> where
C: client::backend::AuxStore,
C: ProvideRuntimeApi,
C::Api: AuraApi<B>,
CB: FnOnce(ApiRef<C::Api>, &BlockId<B>) -> ::client::error::Result<T>,
T: SlotData + Encode + Decode + Debug,
{
use codec::Decode;
const SLOT_KEY: &[u8] = b"aura_slot_duration";
match client.get_aux(SLOT_KEY)? {
Some(v) => u64::decode(&mut &v[..])
match client.get_aux(T::SLOT_KEY)? {
Some(v) => <T as codec::Decode>::decode(&mut &v[..])
.map(SlotDuration)
.ok_or_else(|| ::client::error::Error::Backend(
format!("Aura slot duration kept in invalid format"),
format!("slot duration kept in invalid format"),
).into()),
None => {
use runtime_primitives::traits::Zero;
let genesis_slot_duration = client.runtime_api()
.slot_duration(&BlockId::number(Zero::zero()))?;
let genesis_slot_duration = cb(
client.runtime_api(),
&BlockId::number(Zero::zero()))?;
info!(
"Loaded block-time = {:?} seconds from genesis on first-launch",
@@ -240,7 +269,7 @@ impl SlotDuration {
);
genesis_slot_duration.using_encoded(|s| {
client.insert_aux(&[(SLOT_KEY, &s[..])], &[])
client.insert_aux(&[(T::SLOT_KEY, &s[..])], &[])
})?;
Ok(SlotDuration(genesis_slot_duration))
@@ -248,8 +277,15 @@ impl SlotDuration {
}
}
/// Returns slot duration value.
pub fn get(&self) -> u64 {
self.0
/// Returns slot data value.
pub fn get(&self) -> T {
self.0.clone()
}
/// Get the slot duration in milliseconds
pub fn slot_duration(&self) -> u64
where T: SlotData
{
self.0.slot_duration()
}
}
+6 -6
View File
@@ -6,12 +6,12 @@ edition = "2018"
[dependencies]
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
parity-codec = { version = "3.3", default-features = false, features = ["derive"] }
parity-codec = { version = "3.4.0", default-features = false, features = ["derive"] }
rustc-hex = { version = "2.0", default-features = false }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
twox-hash = { version = "1.1.0", optional = true }
byteorder = { version = "1.1", default-features = false }
twox-hash = { version = "1.2.0", optional = true }
byteorder = { version = "1.3.1", default-features = false }
primitive-types = { version = "0.2", default-features = false, features = ["codec"] }
impl-serde = { version = "0.1", optional = true }
wasmi = { version = "0.4.3", optional = true }
@@ -26,14 +26,14 @@ schnorrkel = { version = "0.1", optional = true }
rand = { version = "0.6", optional = true }
sha2 = { version = "0.8", optional = true }
substrate-bip39 = { git = "https://github.com/paritytech/substrate-bip39", optional = true }
tiny-bip39 = { version = "0.6.0", optional = true }
tiny-bip39 = { version = "0.6.1", optional = true }
hex = { version = "0.3", optional = true }
regex = {version = "1.1", optional = true }
[dev-dependencies]
substrate-serializer = { path = "../serializer" }
pretty_assertions = "0.5"
heapsize = "0.4"
pretty_assertions = "0.6.1"
heapsize = "0.4.2"
[features]
default = ["std"]
+4 -2
View File
@@ -231,8 +231,10 @@ pub struct LocalizedSignature {
impl Signature {
/// A new instance from the given 64-byte `data`.
///
/// NOTE: No checking goes on to ensure this is a real signature. Only use it if
/// you are certain that the array actually is a signature. GIGO!
/// NOTE: No checking goes on to ensure this is a real signature. Only use
/// it if you are certain that the array actually is a signature, or if you
/// immediately verify the signature. All functions that verify signatures
/// will fail if the `Signature` is not actually a valid signature.
pub fn from_raw(data: [u8; 64]) -> Signature {
Signature(data)
}
+2 -2
View File
@@ -8,8 +8,8 @@ edition = "2018"
proc-macro = true
[dependencies]
quote = "0.6"
syn = { version = "^0.15.22", features = [ "full", "fold", "extra-traits", "visit" ] }
quote = "0.6.12"
syn = { version = "^0.15.30", features = [ "full", "fold", "extra-traits", "visit" ] }
proc-macro2 = "0.4"
blake2-rfc = "0.2"
proc-macro-crate = "0.1.3"
@@ -72,7 +72,10 @@ pub enum DigestItem<Hash, AuthorityId, SealSignature> {
/// trie creation.
ChangesTrieRoot(Hash),
/// The old way to put a Seal on it. Deprecated.
#[deprecated]
#[deprecated(
since = "1.0",
note = "New versions of Substrate will never generate this, and it will be rejected on new blockchains.",
)]
Seal(u64, SealSignature),
/// Put a Seal on it
Consensus(ConsensusEngineId, Vec<u8>),
-1
View File
@@ -319,7 +319,6 @@ fn code_using_trie() -> u64 {
iter_pairs.len() as u64
}
cfg_if! {
if #[cfg(feature = "std")] {
impl_runtime_apis! {
+2 -2
View File
@@ -59,8 +59,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("node"),
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 59,
impl_version: 60,
spec_version: 62,
impl_version: 62,
apis: RUNTIME_API_VERSIONS,
};
+3 -3
View File
@@ -11,6 +11,6 @@ proc-macro = true
srml-support-procedural-tools = { path = "./tools" }
sr-api-macros = { path = "../../../core/sr-api-macros" }
proc-macro2 = "0.4"
quote = { version = "0.6" }
syn = { version = "0.15", features = ["full"] }
proc-macro2 = "0.4.27"
quote = { version = "0.6.12" }
syn = { version = "0.15.30", features = ["full"] }
@@ -6,7 +6,7 @@ edition = "2018"
[dependencies]
srml-support-procedural-tools-derive = { path = "./derive" }
proc-macro2 = "0.4"
quote = { version = "0.6" }
syn = { version = "0.15", features = ["full"] }
proc-macro2 = "0.4.27"
quote = { version = "0.6.12" }
syn = { version = "0.15.30", features = ["full"] }
proc-macro-crate = "0.1.3"
@@ -8,6 +8,6 @@ edition = "2018"
proc-macro = true
[dependencies]
proc-macro2 = "0.4.24"
quote = { version = "0.6.10", features = ["proc-macro"] }
syn = { version = "0.15.21", features = ["proc-macro" ,"full", "extra-traits", "parsing"] }
proc-macro2 = "0.4.27"
quote = { version = "0.6.12", features = ["proc-macro"] }
syn = { version = "0.15.30", features = ["proc-macro" ,"full", "extra-traits", "parsing"] }