Rewrite Inherent data (#1488)

* Implement new inherent data

* Fixes compilation on wasm

* Fixes after rebase

* Switch back to generate inherent stuff by macro

* Update after rebase

* Apply suggestions from code review

Co-Authored-By: bkchr <bkchr@users.noreply.github.com>

* Fix compilation after rebase

* Address grumbles

* Remove `InherentDataProviders` from `Client`

* Update wasm files after rebase

* Address grumbles

* Fixes compilation after latest merge

* Last fix
This commit is contained in:
Bastian Köcher
2019-01-22 17:52:08 +01:00
committed by GitHub
parent b14917e63f
commit 70b1af7b1e
55 changed files with 1513 additions and 661 deletions
+7 -6
View File
@@ -6,9 +6,10 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
log = "0.4"
parity-codec = "2.2"
sr-primitives = { path = "../../core/sr-primitives" }
substrate-client = { path = "../../core/client" }
substrate-consensus-aura-primitives = { path = "../../core/consensus/aura/primitives" }
substrate-consensus-common = { path = "../../core/consensus/common" }
substrate-primitives = { path = "../../core/primitives" }
substrate-transaction-pool = { path = "../../core/transaction-pool" }
sr-primitives = { path = "../sr-primitives" }
substrate-client = { path = "../client" }
substrate-consensus-aura-primitives = { path = "../consensus/aura/primitives" }
substrate-consensus-common = { path = "../consensus/common" }
substrate-primitives = { path = "../primitives" }
substrate-inherents = { path = "../inherents" }
substrate-transaction-pool = { path = "../transaction-pool" }
@@ -18,9 +18,7 @@
// FIXME: move this into substrate-consensus-common - https://github.com/paritytech/substrate/issues/1021
use std::sync::Arc;
use std::time;
use std;
use std::{sync::Arc, self};
use client::{
self, error, Client as SubstrateClient, CallExecutor,
@@ -29,13 +27,12 @@ use client::{
use codec::{Decode, Encode};
use consensus_common::{self, evaluation};
use primitives::{H256, Blake2Hasher};
use runtime_primitives::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, ProvideRuntimeApi, AuthorityIdFor};
use runtime_primitives::traits::{
Block as BlockT, Hash as HashT, Header as HeaderT, ProvideRuntimeApi, AuthorityIdFor
};
use runtime_primitives::generic::BlockId;
use runtime_primitives::BasicInherentData;
use transaction_pool::txpool::{self, Pool as TransactionPool};
use aura_primitives::AuraConsensusData;
type Timestamp = u64;
use inherents::InherentData;
// block size limit.
const MAX_TRANSACTIONS_SIZE: usize = 4 * 1024 * 1024;
@@ -59,20 +56,20 @@ pub trait AuthoringApi: Send + Sync + ProvideRuntimeApi where
fn build_block<F: FnMut(&mut BlockBuilder<Self::Block>) -> ()>(
&self,
at: &BlockId<Self::Block>,
inherent_data: BasicInherentData,
inherent_data: InherentData,
build_ctx: F,
) -> Result<Self::Block, error::Error>;
}
impl<'a, B, E, Block, RA> BlockBuilder<Block>
for client::block_builder::BlockBuilder<'a, Block, BasicInherentData, SubstrateClient<B, E, Block, RA>>
for client::block_builder::BlockBuilder<'a, Block, SubstrateClient<B, E, Block, RA>>
where
B: client::backend::Backend<Block, Blake2Hasher> + 'static,
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + Clone + 'static,
Block: BlockT<Hash=H256>,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA> : ProvideRuntimeApi,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api: BlockBuilderApi<Block, BasicInherentData>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api: BlockBuilderApi<Block>,
{
fn push_extrinsic(&mut self, extrinsic: <Block as BlockT>::Extrinsic) -> Result<(), error::Error> {
client::block_builder::BlockBuilder::push(self, extrinsic).map_err(Into::into)
@@ -85,7 +82,7 @@ impl<B, E, Block, RA> AuthoringApi for SubstrateClient<B, E, Block, RA> where
Block: BlockT<Hash=H256>,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA> : ProvideRuntimeApi,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api: BlockBuilderApi<Block, BasicInherentData>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi>::Api: BlockBuilderApi<Block>,
{
type Block = Block;
type Error = client::error::Error;
@@ -93,13 +90,13 @@ impl<B, E, Block, RA> AuthoringApi for SubstrateClient<B, E, Block, RA> where
fn build_block<F: FnMut(&mut BlockBuilder<Self::Block>) -> ()>(
&self,
at: &BlockId<Self::Block>,
inherent_data: BasicInherentData,
inherent_data: InherentData,
mut build_ctx: F,
) -> Result<Self::Block, error::Error> {
let mut block_builder = self.new_block_at(at)?;
let runtime_api = self.runtime_api();
if runtime_api.has_api::<BlockBuilderApi<Block, BasicInherentData>>(at)? {
if runtime_api.has_api::<BlockBuilderApi<Block>>(at)? {
runtime_api.inherent_extrinsics(at, inherent_data)?
.into_iter().try_for_each(|i| block_builder.push(i))?;
}
@@ -118,12 +115,12 @@ pub struct ProposerFactory<C, A> where A: txpool::ChainApi {
pub transaction_pool: Arc<TransactionPool<A>>,
}
impl<C, A, ConsensusData> consensus_common::Environment<<C as AuthoringApi>::Block, ConsensusData> for ProposerFactory<C, A> where
impl<C, A> consensus_common::Environment<<C as AuthoringApi>::Block> for ProposerFactory<C, A> where
C: AuthoringApi,
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<<C as AuthoringApi>::Block, BasicInherentData>,
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<<C as AuthoringApi>::Block>,
A: txpool::ChainApi<Block=<C as AuthoringApi>::Block>,
client::error::Error: From<<C as AuthoringApi>::Error>,
Proposer<<C as AuthoringApi>::Block, C, A>: consensus_common::Proposer<<C as AuthoringApi>::Block, ConsensusData>,
Proposer<<C as AuthoringApi>::Block, C, A>: consensus_common::Proposer<<C as AuthoringApi>::Block>,
{
type Proposer = Proposer<<C as AuthoringApi>::Block, C, A>;
type Error = error::Error;
@@ -151,10 +148,6 @@ impl<C, A, ConsensusData> consensus_common::Environment<<C as AuthoringApi>::Blo
}
}
struct ConsensusData {
timestamp: Option<u64>,
}
/// The proposer logic.
pub struct Proposer<Block: BlockT, C, A: txpool::ChainApi> {
client: Arc<C>,
@@ -164,53 +157,35 @@ pub struct Proposer<Block: BlockT, C, A: txpool::ChainApi> {
transaction_pool: Arc<TransactionPool<A>>,
}
impl<Block, C, A> consensus_common::Proposer<<C as AuthoringApi>::Block, AuraConsensusData> for Proposer<Block, C, A> where
impl<Block, C, A> consensus_common::Proposer<<C as AuthoringApi>::Block> for Proposer<Block, C, A> where
Block: BlockT,
C: AuthoringApi<Block=Block>,
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<Block, BasicInherentData>,
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<Block>,
A: txpool::ChainApi<Block=Block>,
client::error::Error: From<<C as AuthoringApi>::Error>
{
type Create = Result<<C as AuthoringApi>::Block, error::Error>;
type Error = error::Error;
fn propose(&self, consensus_data: AuraConsensusData)
fn propose(&self, inherent_data: InherentData)
-> Result<<C as AuthoringApi>::Block, error::Error>
{
self.propose_with(ConsensusData { timestamp: Some(consensus_data.timestamp) })
}
}
impl<Block, C, A> consensus_common::Proposer<<C as AuthoringApi>::Block, ()> for Proposer<Block, C, A> where
Block: BlockT,
C: AuthoringApi<Block=Block>,
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<Block, BasicInherentData>,
A: txpool::ChainApi<Block=Block>,
client::error::Error: From<<C as AuthoringApi>::Error>
{
type Create = Result<<C as AuthoringApi>::Block, error::Error>;
type Error = error::Error;
fn propose(&self, _consensus_data: ()) -> Result<<C as AuthoringApi>::Block, error::Error> {
self.propose_with(ConsensusData { timestamp: None })
self.propose_with(inherent_data)
}
}
impl<Block, C, A> Proposer<Block, C, A> where
Block: BlockT,
C: AuthoringApi<Block=Block>,
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<Block, BasicInherentData>,
<C as ProvideRuntimeApi>::Api: BlockBuilderApi<Block>,
A: txpool::ChainApi<Block=Block>,
client::error::Error: From<<C as AuthoringApi>::Error>,
{
fn propose_with(&self, consensus_data: ConsensusData)
fn propose_with(&self, inherent_data: InherentData)
-> Result<<C as AuthoringApi>::Block, error::Error>
{
use runtime_primitives::traits::BlakeTwo256;
let timestamp = consensus_data.timestamp.unwrap_or_else(current_timestamp);
let inherent_data = BasicInherentData::new(timestamp, 0);
let block = self.client.build_block(
&self.parent_id,
inherent_data,
@@ -261,9 +236,3 @@ impl<Block, C, A> Proposer<Block, C, A> where
Ok(substrate_block)
}
}
fn current_timestamp() -> Timestamp {
time::SystemTime::now().duration_since(time::UNIX_EPOCH)
.expect("now always later than unix epoch; qed")
.as_secs()
}
+1 -1
View File
@@ -18,13 +18,13 @@
#![warn(unused_extern_crates)]
extern crate substrate_consensus_aura_primitives as aura_primitives;
extern crate substrate_primitives as primitives;
extern crate sr_primitives as runtime_primitives;
extern crate substrate_consensus_common as consensus_common;
extern crate substrate_client as client;
extern crate parity_codec as codec;
extern crate substrate_transaction_pool as transaction_pool;
extern crate substrate_inherents as inherents;
#[macro_use]
extern crate log;
+3 -1
View File
@@ -22,11 +22,12 @@ substrate-telemetry = { path = "../telemetry", optional = true }
hash-db = { version = "0.9" , optional = true }
kvdb = { git = "https://github.com/paritytech/parity-common", optional = true, rev="b0317f649ab2c665b7987b8475878fc4d2e1f81d" }
codec = { package = "parity-codec", version = "2.1", default-features = false }
codec = { package = "parity-codec", version = "2.2", default-features = false }
primitives = { package = "substrate-primitives", path = "../primitives", default-features = false }
runtime-primitives = { package = "sr-primitives", path = "../sr-primitives", default-features = false }
runtime-version = { package = "sr-version", path = "../sr-version", default-features = false }
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
inherents = { package = "substrate-inherents", path = "../inherents", default-features = false }
sr-api-macros = { path = "../sr-api-macros" }
[dev-dependencies]
@@ -39,6 +40,7 @@ std = [
"codec/std",
"consensus",
"primitives/std",
"inherents/std",
"parking_lot",
"error-chain",
"fnv",
@@ -16,13 +16,14 @@
//! The runtime api for building blocks.
use runtime_primitives::{traits::Block as BlockT, ApplyResult, CheckInherentError};
use runtime_primitives::{traits::Block as BlockT, ApplyResult};
use rstd::vec::Vec;
use sr_api_macros::decl_runtime_apis;
pub use inherents::{InherentData, CheckInherentsResult};
decl_runtime_apis! {
/// The `BlockBuilder` api trait that provides required functions for building a block for a runtime.
pub trait BlockBuilder<InherentData> {
pub trait BlockBuilder {
/// Apply the given extrinsics.
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult;
/// Finish the current block.
@@ -30,7 +31,7 @@ decl_runtime_apis! {
/// Generate inherent extrinsics. The inherent data will vary from chain to chain.
fn inherent_extrinsics(inherent: InherentData) -> Vec<<Block as BlockT>::Extrinsic>;
/// Check that the inherents are valid. The inherent data will vary from chain to chain.
fn check_inherents(block: Block, data: InherentData) -> Result<(), CheckInherentError>;
fn check_inherents(block: Block, data: InherentData) -> CheckInherentsResult;
/// Generate a random seed.
fn random_seed() -> <Block as BlockT>::Hash;
}
@@ -16,7 +16,6 @@
use super::api::BlockBuilder as BlockBuilderApi;
use std::vec::Vec;
use std::marker::PhantomData;
use codec::Encode;
use crate::blockchain::HeaderBackend;
use runtime_primitives::traits::{
@@ -30,19 +29,18 @@ use runtime_primitives::ApplyOutcome;
/// Utility for building new (valid) blocks from a stream of extrinsics.
pub struct BlockBuilder<'a, Block, InherentData, A: ProvideRuntimeApi> where Block: BlockT {
pub struct BlockBuilder<'a, Block, A: ProvideRuntimeApi> where Block: BlockT {
header: <Block as BlockT>::Header,
extrinsics: Vec<<Block as BlockT>::Extrinsic>,
api: ApiRef<'a, A::Api>,
block_id: BlockId<Block>,
_marker: PhantomData<InherentData>,
}
impl<'a, Block, A, InherentData> BlockBuilder<'a, Block, InherentData, A>
impl<'a, Block, A> BlockBuilder<'a, Block, A>
where
Block: BlockT<Hash=H256>,
A: ProvideRuntimeApi + HeaderBackend<Block> + 'a,
A::Api: BlockBuilderApi<Block, InherentData>,
A::Api: BlockBuilderApi<Block>,
{
/// Create a new instance of builder from the given client, building on the latest block.
pub fn new(api: &'a A) -> error::Result<Self> {
@@ -75,7 +73,6 @@ where
extrinsics: Vec::new(),
api,
block_id: *block_id,
_marker: PhantomData,
})
}
+11 -8
View File
@@ -47,7 +47,9 @@ use state_machine::{
};
use crate::backend::{self, BlockImportOperation, PrunableStateChangesTrieStorage};
use crate::blockchain::{self, Info as ChainInfo, Backend as ChainBackend, HeaderBackend as ChainHeaderBackend};
use crate::blockchain::{
self, Info as ChainInfo, Backend as ChainBackend, HeaderBackend as ChainHeaderBackend
};
use crate::call_executor::{CallExecutor, LocalCallExecutor};
use executor::{RuntimeVersion, RuntimeInfo};
use crate::notifications::{StorageNotifications, StorageEventStream};
@@ -78,7 +80,8 @@ pub struct Client<B, E, Block, RA> where Block: BlockT {
import_notification_sinks: Mutex<Vec<mpsc::UnboundedSender<BlockImportNotification<Block>>>>,
finality_notification_sinks: Mutex<Vec<mpsc::UnboundedSender<FinalityNotification<Block>>>>,
import_lock: Mutex<()>,
importing_block: RwLock<Option<Block::Hash>>, // holds the block hash currently being imported. TODO: replace this with block queue
// holds the block hash currently being imported. TODO: replace this with block queue
importing_block: RwLock<Option<Block::Hash>>,
block_execution_strategy: ExecutionStrategy,
api_execution_strategy: ExecutionStrategy,
_phantom: PhantomData<RA>,
@@ -557,25 +560,25 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
}
/// Create a new block, built on the head of the chain.
pub fn new_block<InherentData>(
pub fn new_block(
&self
) -> error::Result<block_builder::BlockBuilder<Block, InherentData, Self>> where
) -> error::Result<block_builder::BlockBuilder<Block, Self>> where
E: Clone + Send + Sync,
RA: Send + Sync,
Self: ProvideRuntimeApi,
<Self as ProvideRuntimeApi>::Api: BlockBuilderAPI<Block, InherentData>
<Self as ProvideRuntimeApi>::Api: BlockBuilderAPI<Block>
{
block_builder::BlockBuilder::new(self)
}
/// Create a new block, built on top of `parent`.
pub fn new_block_at<InherentData>(
pub fn new_block_at(
&self, parent: &BlockId<Block>
) -> error::Result<block_builder::BlockBuilder<Block, InherentData, Self>> where
) -> error::Result<block_builder::BlockBuilder<Block, Self>> where
E: Clone + Send + Sync,
RA: Send + Sync,
Self: ProvideRuntimeApi,
<Self as ProvideRuntimeApi>::Api: BlockBuilderAPI<Block, InherentData>
<Self as ProvideRuntimeApi>::Api: BlockBuilderAPI<Block>
{
block_builder::BlockBuilder::at_block(parent, &self)
}
+2 -1
View File
@@ -13,8 +13,9 @@ sr-primitives = { path = "../../sr-primitives" }
sr-version = { path = "../../sr-version" }
sr-io = { path = "../../sr-io" }
substrate-consensus-aura-primitives = { path = "primitives" }
substrate-inherents = { path = "../../inherents" }
srml-consensus = { path = "../../../srml/consensus" }
srml-aura = { path = "../../../srml/aura" }
futures = "0.1.17"
tokio = "0.1.7"
parking_lot = "0.7.1"
+193 -101
View File
@@ -33,6 +33,8 @@ extern crate srml_support as runtime_support;
extern crate sr_io as runtime_io;
extern crate sr_primitives as runtime_primitives;
extern crate substrate_consensus_aura_primitives as aura_primitives;
extern crate srml_aura;
extern crate substrate_inherents as inherents;
extern crate substrate_consensus_common as consensus_common;
extern crate tokio;
@@ -57,24 +59,33 @@ extern crate env_logger;
mod slots;
use std::sync::Arc;
use std::time::Duration;
use std::{sync::{Arc, mpsc}, time::Duration, thread};
use codec::Encode;
use consensus_common::{Authorities, BlockImport, Environment, Error as ConsensusError, Proposer, ForkChoiceStrategy};
use consensus_common::{
Authorities, BlockImport, Environment, Error as ConsensusError, Proposer, ForkChoiceStrategy
};
use consensus_common::import_queue::{Verifier, BasicQueue};
use client::ChainHead;
use client::block_builder::api::BlockBuilder as BlockBuilderApi;
use consensus_common::{ImportBlock, BlockOrigin};
use runtime_primitives::{generic, generic::BlockId, Justification, BasicInherentData};
use runtime_primitives::traits::{Block, Header, Digest, DigestItemFor, DigestItem, ProvideRuntimeApi};
use runtime_primitives::{generic, generic::BlockId, Justification};
use runtime_primitives::traits::{
Block, Header, Digest, DigestItemFor, DigestItem, ProvideRuntimeApi
};
use primitives::{Ed25519AuthorityId, ed25519};
use inherents::{InherentDataProviders, InherentData, RuntimeString};
use futures::{Stream, Future, IntoFuture, future::{self, Either}};
use tokio::timer::Timeout;
use api::AuraApi;
use slots::Slots;
use srml_aura::{
InherentType as AuraInherent, AuraInherentData,
timestamp::{TimestampInherentData, InherentType as TimestampInherent, InherentError as TIError}
};
pub use aura_primitives::*;
pub use consensus_common::SyncOracle;
@@ -114,18 +125,23 @@ fn duration_now() -> Option<Duration> {
}).ok()
}
fn timestamp_and_slot_now(slot_duration: u64) -> Option<(u64, u64)> {
duration_now().map(|s| {
let s = s.as_secs();
(s, s / slot_duration)
})
}
/// Get the slot for now.
fn slot_now(slot_duration: u64) -> Option<u64> {
duration_now().map(|s| s.as_secs() / slot_duration)
}
fn extract_timestamp_and_slot(
data: &InherentData
) -> Result<(TimestampInherent, AuraInherent), consensus_common::Error> {
data.timestamp_inherent_data()
.and_then(|t| data.aura_inherent_data().map(|a| (t, a)))
.map_err(inherent_to_common_error)
}
fn inherent_to_common_error(err: RuntimeString) -> consensus_common::Error {
consensus_common::ErrorKind::InherentData(err.into()).into()
}
/// A digest item which is usable with aura consensus.
pub trait CompatibleDigestItem: Sized {
/// Construct a digest item which is a slot number and a signature on the
@@ -160,11 +176,12 @@ pub fn start_aura_thread<B, C, E, I, SO, Error>(
env: Arc<E>,
sync_oracle: SO,
on_exit: impl Future<Item=(),Error=()> + Send + 'static,
) where
inherent_data_providers: InherentDataProviders,
) -> Result<(), consensus_common::Error> where
B: Block + 'static,
C: Authorities<B> + ChainHead<B> + Send + Sync + 'static,
E: Environment<B, AuraConsensusData, Error=Error> + Send + Sync + 'static,
E::Proposer: Proposer<B, AuraConsensusData, Error=Error> + 'static,
E: Environment<B, Error=Error> + Send + Sync + 'static,
E::Proposer: Proposer<B, Error=Error> + 'static,
I: BlockImport<B> + Send + Sync + 'static,
Error: From<C::Error> + From<I::Error> + 'static,
SO: SyncOracle + Send + Clone + 'static,
@@ -173,7 +190,9 @@ pub fn start_aura_thread<B, C, E, I, SO, Error>(
{
use tokio::runtime::current_thread::Runtime;
::std::thread::spawn(move || {
let (result_sender, result_recv) = mpsc::channel();
thread::spawn(move || {
let mut runtime = match Runtime::new() {
Ok(r) => r,
Err(e) => {
@@ -182,7 +201,7 @@ pub fn start_aura_thread<B, C, E, I, SO, Error>(
}
};
let _ = runtime.block_on(start_aura(
let aura_future = match start_aura(
slot_duration,
local_key,
client,
@@ -190,8 +209,26 @@ pub fn start_aura_thread<B, C, E, I, SO, Error>(
env,
sync_oracle,
on_exit,
));
inherent_data_providers,
) {
Ok(aura_future) => {
result_sender
.send(Ok(()))
.expect("Receive is not dropped before receiving a result; qed");
aura_future
},
Err(e) => {
result_sender
.send(Err(e))
.expect("Receive is not dropped before receiving a result; qed");
return;
}
};
let _ = runtime.block_on(aura_future);
});
result_recv.recv().expect("Aura start thread result sender dropped")
}
/// Start the aura worker. The returned future should be run in a tokio runtime.
@@ -203,17 +240,20 @@ pub fn start_aura<B, C, E, I, SO, Error>(
env: Arc<E>,
sync_oracle: SO,
on_exit: impl Future<Item=(),Error=()>,
) -> impl Future<Item=(),Error=()> where
inherent_data_providers: InherentDataProviders,
) -> Result<impl Future<Item=(), Error=()>, consensus_common::Error> where
B: Block,
C: Authorities<B> + ChainHead<B>,
E: Environment<B, AuraConsensusData, Error=Error>,
E::Proposer: Proposer<B, AuraConsensusData, Error=Error>,
E: Environment<B, Error=Error>,
E::Proposer: Proposer<B, Error=Error>,
I: BlockImport<B>,
Error: From<C::Error> + From<I::Error>,
SO: SyncOracle + Send + Clone,
DigestItemFor<B>: CompatibleDigestItem + DigestItem<AuthorityId=Ed25519AuthorityId>,
Error: ::std::error::Error + Send + 'static + From<::consensus_common::Error>,
{
register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.0)?;
let make_authorship = move || {
let client = client.clone();
@@ -222,9 +262,10 @@ pub fn start_aura<B, C, E, I, SO, Error>(
let env = env.clone();
let sync_oracle = sync_oracle.clone();
let SlotDuration(slot_duration) = slot_duration;
let inherent_data_providers = inherent_data_providers.clone();
// rather than use a timer interval, we schedule our waits ourselves
Slots::new(slot_duration)
Slots::new(slot_duration, inherent_data_providers)
.map_err(|e| debug!(target: "aura", "Faulty timer: {:?}", e))
.for_each(move |slot_info| {
let client = client.clone();
@@ -244,7 +285,7 @@ pub fn start_aura<B, C, E, I, SO, Error>(
let chain_head = match client.best_block_header() {
Ok(x) => x,
Err(e) => {
warn!(target:"aura", "Unable to author block in slot {}. \
warn!(target: "aura", "Unable to author block in slot {}. \
no best block header: {:?}", slot_num, e);
return Either::B(future::ok(()))
}
@@ -253,8 +294,11 @@ pub fn start_aura<B, C, E, I, SO, Error>(
let authorities = match client.authorities(&BlockId::Hash(chain_head.hash())) {
Ok(authorities) => authorities,
Err(e) => {
warn!("Unable to fetch authorities at\
block {:?}: {:?}", chain_head.hash(), e);
warn!(
"Unable to fetch authorities at block {:?}: {:?}",
chain_head.hash(),
e
);
return Either::B(future::ok(()));
}
};
@@ -262,8 +306,11 @@ pub fn start_aura<B, C, E, I, SO, Error>(
let proposal_work = match slot_author(slot_num, &authorities) {
None => return Either::B(future::ok(())),
Some(author) => if author.0 == public_key.0 {
debug!(target: "aura", "Starting authorship at slot {}; timestamp = {}",
slot_num, timestamp);
debug!(
target: "aura", "Starting authorship at slot {}; timestamp = {}",
slot_num,
timestamp
);
// we are the slot author. make a block and sign it.
let proposer = match env.init(&chain_head, &authorities) {
@@ -274,17 +321,12 @@ pub fn start_aura<B, C, E, I, SO, Error>(
}
};
let consensus_data = AuraConsensusData {
timestamp,
slot: slot_num,
slot_duration,
};
let remaining_duration = slot_info.remaining_duration();
// deadline our production to approx. the end of the
// slot
Timeout::new(
proposer.propose(consensus_data).into_future(),
slot_info.remaining_duration(),
proposer.propose(slot_info.inherent_data).into_future(),
remaining_duration,
)
} else {
return Either::B(future::ok(()));
@@ -298,8 +340,10 @@ pub fn start_aura<B, C, E, I, SO, Error>(
// that is actually set by the proposer.
let slot_after_building = slot_now(slot_duration);
if slot_after_building != Some(slot_num) {
info!("Discarding proposal for slot {}; block production took too long",
slot_num);
info!(
"Discarding proposal for slot {}; block production took too long",
slot_num
);
return
}
@@ -363,7 +407,7 @@ pub fn start_aura<B, C, E, I, SO, Error>(
})
});
work.select(on_exit).then(|_| Ok(()))
Ok(work.select(on_exit).then(|_| Ok(())))
}
// a header which has been checked
@@ -431,11 +475,58 @@ pub trait ExtraVerification<B: Block>: Send + Sync {
}
/// A verifier for Aura blocks.
pub struct AuraVerifier<C, E, MakeInherent> {
slot_duration: SlotDuration,
pub struct AuraVerifier<C, E> {
client: Arc<C>,
make_inherent: MakeInherent,
extra: E,
inherent_data_providers: inherents::InherentDataProviders,
}
impl<C, E> AuraVerifier<C, E>
{
fn check_inherents<B: Block>(
&self,
block: B,
block_id: BlockId<B>,
inherent_data: InherentData,
timestamp_now: u64,
) -> Result<(), String>
where C: ProvideRuntimeApi, C::Api: BlockBuilderApi<B>
{
const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60;
let inherent_res = self.client.runtime_api().check_inherents(
&block_id,
block,
inherent_data,
).map_err(|e| format!("{:?}", e))?;
if !inherent_res.ok() {
inherent_res
.into_errors()
.try_for_each(|(i, e)| match TIError::try_from(&i, &e) {
Some(TIError::ValidAtTimestamp(timestamp)) => {
// halt import until timestamp is valid.
// reject when too far ahead.
if timestamp > timestamp_now + MAX_TIMESTAMP_DRIFT_SECS {
return Err("Rejecting block too far in future".into());
}
let diff = timestamp.saturating_sub(timestamp_now);
info!(
target: "aura",
"halting for block {} seconds in the future",
diff
);
thread::sleep(Duration::from_secs(diff));
Ok(())
},
Some(TIError::Other(e)) => Err(e.into()),
None => Err(self.inherent_data_providers.error_to_string(&i, &e)),
})
} else {
Ok(())
}
}
}
/// No-op extra verification.
@@ -450,12 +541,11 @@ impl<B: Block> ExtraVerification<B> for NothingExtra {
}
}
impl<B: Block, C, E, MakeInherent, Inherent> Verifier<B> for AuraVerifier<C, E, MakeInherent> where
impl<B: Block, C, E> Verifier<B> for AuraVerifier<C, E> where
C: Authorities<B> + BlockImport<B> + ProvideRuntimeApi + Send + Sync,
C::Api: BlockBuilderApi<B, Inherent>,
C::Api: BlockBuilderApi<B>,
DigestItemFor<B>: CompatibleDigestItem + DigestItem<AuthorityId=Ed25519AuthorityId>,
E: ExtraVerification<B>,
MakeInherent: Fn(u64, u64) -> Inherent + Send + Sync,
{
fn verify(
&self,
@@ -464,11 +554,9 @@ impl<B: Block, C, E, MakeInherent, Inherent> Verifier<B> for AuraVerifier<C, E,
justification: Option<Justification>,
mut body: Option<Vec<B::Extrinsic>>,
) -> Result<(ImportBlock<B>, Option<Vec<Ed25519AuthorityId>>), String> {
use runtime_primitives::CheckInherentError;
const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60;
let (timestamp_now, slot_now) = timestamp_and_slot_now(self.slot_duration.0)
.ok_or("System time is before UnixTime?".to_owned())?;
let mut inherent_data = self.inherent_data_providers.create_inherent_data().map_err(String::from)?;
let (timestamp_now, slot_now) = extract_timestamp_and_slot(&inherent_data)
.map_err(|e| format!("Could not extract timestamp and slot: {:?}", e))?;
let hash = header.hash();
let parent_hash = *header.parent_hash();
let authorities = self.client.authorities(&BlockId::Hash(parent_hash))
@@ -491,30 +579,15 @@ impl<B: Block, C, E, MakeInherent, Inherent> Verifier<B> for AuraVerifier<C, E,
// to check that the internally-set timestamp in the inherents
// actually matches the slot set in the seal.
if let Some(inner_body) = body.take() {
let inherent = (self.make_inherent)(timestamp_now, slot_num);
let block: B = Block::new(pre_header.clone(), inner_body);
inherent_data.aura_replace_inherent_data(slot_num);
let block = B::new(pre_header.clone(), inner_body);
let inherent_res = self.client.runtime_api().check_inherents(
&BlockId::Hash(parent_hash),
self.check_inherents(
block.clone(),
inherent,
).map_err(|e| format!("{:?}", e))?;
match inherent_res {
Ok(()) => {}
Err(CheckInherentError::ValidAtTimestamp(timestamp)) => {
// halt import until timestamp is valid.
// reject when too far ahead.
if timestamp > timestamp_now + MAX_TIMESTAMP_DRIFT_SECS {
return Err("Rejecting block too far in future".into());
}
let diff = timestamp.saturating_sub(timestamp_now);
info!(target: "aura", "halting for block {} seconds in the future", diff);
::std::thread::sleep(Duration::from_secs(diff));
},
Err(CheckInherentError::Other(s)) => return Err(s.into_owned()),
}
BlockId::Hash(parent_hash),
inherent_data,
timestamp_now,
)?;
let (_, inner_body) = block.deconstruct();
body = Some(inner_body);
@@ -546,16 +619,8 @@ impl<B: Block, C, E, MakeInherent, Inherent> Verifier<B> for AuraVerifier<C, E,
}
}
/// A utility for making the basic-inherent data.
pub fn make_basic_inherent(timestamp: u64, slot_now: u64) -> BasicInherentData {
BasicInherentData::new(timestamp, slot_now)
}
/// A type for a function which produces inherent.
pub type InherentProducingFn<I> = fn(u64, u64) -> I;
/// The Aura import queue type.
pub type AuraImportQueue<B, C, E, MakeInherent> = BasicQueue<B, AuraVerifier<C, E, MakeInherent>>;
pub type AuraImportQueue<B, C, E> = BasicQueue<B, AuraVerifier<C, E>>;
/// A slot duration. Create with `get_or_compute`.
// The internal member should stay private here.
@@ -584,8 +649,10 @@ impl SlotDuration {
let genesis_slot_duration = client.runtime_api()
.slot_duration(&BlockId::number(Zero::zero()))?;
info!("Loaded block-time = {:?} seconds from genesis on first-launch",
genesis_slot_duration);
info!(
"Loaded block-time = {:?} seconds from genesis on first-launch",
genesis_slot_duration
);
genesis_slot_duration.using_encoded(|s| {
client.insert_aux(&[(SLOT_KEY, &s[..])], &[])
@@ -597,22 +664,39 @@ impl SlotDuration {
}
}
/// Register the aura inherent data provider, if not registered already.
fn register_aura_inherent_data_provider(
inherent_data_providers: &InherentDataProviders,
slot_duration: u64,
) -> Result<(), consensus_common::Error> {
if !inherent_data_providers.has_provider(&srml_aura::INHERENT_IDENTIFIER) {
inherent_data_providers
.register_provider(srml_aura::InherentDataProvider::new(slot_duration))
.map_err(inherent_to_common_error)
} else {
Ok(())
}
}
/// Start an import queue for the Aura consensus algorithm.
pub fn import_queue<B, C, E, MakeInherent, Inherent>(
pub fn import_queue<B, C, E>(
slot_duration: SlotDuration,
client: Arc<C>,
extra: E,
make_inherent: MakeInherent,
) -> AuraImportQueue<B, C, E, MakeInherent> where
inherent_data_providers: InherentDataProviders,
) -> Result<AuraImportQueue<B, C, E>, consensus_common::Error> where
B: Block,
C: Authorities<B> + BlockImport<B,Error=ConsensusError> + ProvideRuntimeApi + Send + Sync,
C::Api: BlockBuilderApi<B, Inherent>,
C: Authorities<B> + BlockImport<B, Error=ConsensusError> + ProvideRuntimeApi + Send + Sync,
C::Api: BlockBuilderApi<B>,
DigestItemFor<B>: CompatibleDigestItem + DigestItem<AuthorityId=Ed25519AuthorityId>,
E: ExtraVerification<B>,
MakeInherent: Fn(u64, u64) -> Inherent + Send + Sync,
{
let verifier = Arc::new(AuraVerifier { slot_duration, client: client.clone(), extra, make_inherent });
BasicQueue::new(verifier, client)
register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.0)?;
let verifier = Arc::new(
AuraVerifier { client: client.clone(), extra, inherent_data_providers }
);
Ok(BasicQueue::new(verifier, client))
}
#[cfg(test)]
@@ -636,7 +720,7 @@ mod tests {
struct DummyFactory(Arc<TestClient>);
struct DummyProposer(u64, Arc<TestClient>);
impl Environment<TestBlock, AuraConsensusData> for DummyFactory {
impl Environment<TestBlock> for DummyFactory {
type Proposer = DummyProposer;
type Error = Error;
@@ -647,11 +731,11 @@ mod tests {
}
}
impl Proposer<TestBlock, AuraConsensusData> for DummyProposer {
impl Proposer<TestBlock> for DummyProposer {
type Error = Error;
type Create = Result<TestBlock, Error>;
fn propose(&self, _consensus_data: AuraConsensusData) -> Result<TestBlock, Error> {
fn propose(&self, _: InherentData) -> Result<TestBlock, Error> {
self.1.new_block().unwrap().bake().map_err(|e| e.into())
}
}
@@ -663,36 +747,38 @@ mod tests {
peers: Vec<Arc<Peer<AuraVerifier<
PeersClient,
NothingExtra,
InherentProducingFn<()>,
>, ()>>>,
started: bool
started: bool,
}
impl TestNetFactory for AuraTestNet {
type Verifier = AuraVerifier<PeersClient, NothingExtra, InherentProducingFn<()>>;
type Verifier = AuraVerifier<PeersClient, NothingExtra>;
type PeerData = ();
/// Create new test network with peers and given config.
fn from_config(_config: &ProtocolConfig) -> Self {
AuraTestNet {
peers: Vec::new(),
started: false
started: false,
}
}
fn make_verifier(&self, client: Arc<PeersClient>, _cfg: &ProtocolConfig)
-> Arc<Self::Verifier>
{
fn make_inherent(_: u64, _: u64) { () }
let slot_duration = SlotDuration::get_or_compute(&*client)
.expect("slot duration available");
let inherent_data_providers = InherentDataProviders::new();
register_aura_inherent_data_provider(
&inherent_data_providers,
slot_duration.0
).expect("Registers aura inherent data provider");
assert_eq!(slot_duration.0, SLOT_DURATION);
Arc::new(AuraVerifier {
client,
slot_duration,
extra: NothingExtra,
make_inherent: make_inherent as _,
inherent_data_providers,
})
}
@@ -748,6 +834,11 @@ mod tests {
let slot_duration = SlotDuration::get_or_compute(&*client)
.expect("slot duration available");
let inherent_data_providers = InherentDataProviders::new();
register_aura_inherent_data_provider(
&inherent_data_providers, slot_duration.0
).expect("Registers aura inherent data provider");
let aura = start_aura(
slot_duration,
Arc::new(key.clone().into()),
@@ -756,7 +847,8 @@ mod tests {
environ.clone(),
DummyOracle,
futures::empty(),
);
inherent_data_providers,
).expect("Starts aura");
runtime.spawn(aura);
}
+26 -16
View File
@@ -22,6 +22,10 @@ use std::time::{Instant, Duration};
use tokio::timer::Delay;
use futures::prelude::*;
use inherents::{InherentDataProviders, InherentData};
use consensus_common::{Error, ErrorKind};
/// Returns the duration until the next slot, based on current duration since
pub(crate) fn time_until_next(now: Duration, slot_duration: u64) -> Duration {
let remaining_full_secs = slot_duration - (now.as_secs() % slot_duration) - 1;
@@ -30,7 +34,6 @@ pub(crate) fn time_until_next(now: Duration, slot_duration: u64) -> Duration {
}
/// Information about a slot.
#[derive(Debug, Clone)]
pub(crate) struct SlotInfo {
/// The slot number.
pub(crate) number: u64,
@@ -38,6 +41,8 @@ pub(crate) struct SlotInfo {
pub(crate) timestamp: u64,
/// The instant at which the slot ends.
pub(crate) ends_at: Instant,
/// The inherent data.
pub(crate) inherent_data: InherentData,
}
impl SlotInfo {
@@ -57,22 +62,24 @@ pub(crate) struct Slots {
last_slot: u64,
slot_duration: u64,
inner_delay: Option<Delay>,
inherent_data_providers: InherentDataProviders,
}
impl Slots {
/// Create a new `slots` stream.
pub(crate) fn new(slot_duration: u64) -> Self {
/// Create a new `Slots` stream.
pub(crate) fn new(slot_duration: u64, inherent_data_providers: InherentDataProviders) -> Self {
Slots {
last_slot: 0,
slot_duration,
inner_delay: None,
inherent_data_providers,
}
}
}
impl Stream for Slots {
type Item = SlotInfo;
type Error = tokio::timer::Error;
type Error = Error;
fn poll(&mut self) -> Poll<Option<SlotInfo>, Self::Error> {
let slot_duration = self.slot_duration;
@@ -90,30 +97,33 @@ impl Stream for Slots {
};
if let Some(ref mut inner_delay) = self.inner_delay {
try_ready!(inner_delay.poll());
try_ready!(inner_delay.poll().map_err(|e| Error::from(ErrorKind::FaultyTimer(e))));
}
// timeout has fired.
let (timestamp, slot_num) = match ::timestamp_and_slot_now(slot_duration) {
None => return Ok(Async::Ready(None)),
Some(x) => x,
};
let inherent_data = self.inherent_data_providers.create_inherent_data()
.map_err(::inherent_to_common_error)?;
let (timestamp, slot_num) = ::extract_timestamp_and_slot(&inherent_data)?;
// reschedule delay for next slot.
let ends_at = Instant::now()
+ time_until_next(Duration::from_secs(timestamp), slot_duration);
let ends_at = Instant::now() + time_until_next(Duration::from_secs(timestamp), slot_duration);
self.inner_delay = Some(Delay::new(ends_at));
// never yield the same slot twice.
if slot_num > self.last_slot {
self.last_slot = slot_num;
Ok(Async::Ready(Some(SlotInfo {
number: slot_num,
timestamp,
ends_at,
})))
Ok(
Async::Ready(
Some(SlotInfo {
number: slot_num,
timestamp,
ends_at,
inherent_data,
})
)
)
} else {
// re-poll until we get a new slot.
self.poll()
@@ -8,6 +8,7 @@ description = "Common utilities for substrate consensus"
log = "0.4"
parking_lot = "0.7.1"
substrate-primitives = { path= "../../primitives" }
substrate-inherents = { path= "../../inherents" }
error-chain = "0.12"
futures = "0.1"
sr-version = { path = "../../sr-version" }
@@ -37,6 +37,12 @@ error_chain! {
display("Timer error: {}", e),
}
/// Error while working with inherent data.
InherentData(e: String) {
description("InherentData error"),
display("InherentData error: {}", e),
}
/// Unable to propose a block.
CannotPropose {
description("Unable to create block proposal."),
+7 -5
View File
@@ -27,6 +27,7 @@
#![recursion_limit="128"]
extern crate substrate_primitives as primitives;
extern crate substrate_inherents as inherents;
extern crate futures;
extern crate parking_lot;
extern crate sr_version as runtime_version;
@@ -47,6 +48,7 @@ use std::sync::Arc;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{AuthorityIdFor, Block};
use futures::prelude::*;
pub use inherents::InherentData;
pub mod offline_tracker;
pub mod error;
@@ -67,9 +69,9 @@ pub trait Authorities<B: Block> {
}
/// Environment producer for a Consensus instance. Creates proposer instance and communication streams.
pub trait Environment<B: Block, ConsensusData> {
pub trait Environment<B: Block> {
/// The proposer type this creates.
type Proposer: Proposer<B, ConsensusData>;
type Proposer: Proposer<B>;
/// Error which can occur upon creation.
type Error: From<Error>;
@@ -85,13 +87,13 @@ pub trait Environment<B: Block, ConsensusData> {
/// block.
///
/// Proposers are generic over bits of "consensus data" which are engine-specific.
pub trait Proposer<B: Block, ConsensusData> {
pub trait Proposer<B: Block> {
/// Error type which can occur when proposing or evaluating.
type Error: From<Error> + ::std::fmt::Debug + 'static;
/// Future that resolves to a committed proposal.
type Create: IntoFuture<Item=B,Error=Self::Error>;
type Create: IntoFuture<Item=B, Error=Self::Error>;
/// Create a proposal.
fn propose(&self, consensus_data: ConsensusData) -> Self::Create;
fn propose(&self, inherent_data: InherentData) -> Self::Create;
}
/// An oracle for when major synchronization work is being undertaken.
+21
View File
@@ -0,0 +1,21 @@
[package]
name = "substrate-inherents"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
parking_lot = { version = "0.7", optional = true }
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
parity-codec = { version = "2.2", default-features = false }
parity-codec-derive = { version = "2.2", default-features = false }
runtime_primitives = { package = "sr-primitives", path = "../sr-primitives", default-features = false }
[features]
default = [ "std" ]
std = [
"parking_lot",
"rstd/std",
"parity-codec/std",
"runtime_primitives/std",
]
+575
View File
@@ -0,0 +1,575 @@
// 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/>.
//! Provides types and traits for creating and checking inherents.
//!
//! Each inherent is added to a produced block. Each runtime decides on which inherents its
//! want to attach to its blocks. All data that is required for the runtime to create the inherents
//! is stored in the `InherentData`. This `InherentData` is constructed by the node and given to
//! the runtime.
//!
//! Types that provide data for inherents, should implement `InherentDataProvider` and need to be
//! registered at `InherentDataProviders`.
//!
//! In the runtime, modules need to implement `ProvideInherent` when they can create and/or check
//! inherents. By implementing `ProvideInherent`, a module is not enforced to create an inherent.
//! A module can also just check given inherents. For using a module as inherent provider, it needs
//! to be registered by the `construct_runtime!` macro. The macro documentation gives more
//! information on how that is done.
#![cfg_attr(not(feature = "std"), no_std)]
extern crate parity_codec as codec;
use parity_codec_derive::{Encode, Decode};
use rstd::{collections::btree_map::{BTreeMap, IntoIter, Entry}, vec::Vec};
#[cfg(feature = "std")]
use parking_lot::RwLock;
#[cfg(feature = "std")]
use std::{sync::Arc, format};
pub use runtime_primitives::RuntimeString;
/// An identifier for an inherent.
pub type InherentIdentifier = [u8; 8];
/// Inherent data to include in a block.
pub struct InherentData {
/// All inherent data encoded with parity-codec and an identifier.
data: BTreeMap<InherentIdentifier, Vec<u8>>
}
impl InherentData {
/// Create a new instance.
pub fn new() -> Self {
Self {
data: Default::default(),
}
}
/// Put data for an inherent into the internal storage.
///
/// # Return
///
/// Returns `Ok(())` if the data could be inserted an no data for an inherent with the same
/// identifier existed, otherwise an error is returned.
///
/// Inherent identifiers need to be unique, otherwise decoding of these values will not work!
pub fn put_data<I: codec::Encode>(
&mut self,
identifier: InherentIdentifier,
inherent: &I,
) -> Result<(), RuntimeString> {
match self.data.entry(identifier) {
Entry::Vacant(entry) => {
entry.insert(inherent.encode());
Ok(())
},
Entry::Occupied(_) => {
Err("Inherent with same identifier already exists!".into())
}
}
}
/// Replace the data for an inherent.
///
/// If it does not exist, the data is just inserted.
pub fn replace_data<I: codec::Encode>(
&mut self,
identifier: InherentIdentifier,
inherent: &I,
) {
self.data.insert(identifier, inherent.encode());
}
/// Returns the data for the requested inherent.
///
/// # Return
///
/// - `Ok(Some(I))` if the data could be found and deserialized.
/// - `Ok(None)` if the data could not be found.
/// - `Err(_)` if the data could be found, but deserialization did not work.
pub fn get_data<I: codec::Decode>(
&self,
identifier: &InherentIdentifier,
) -> Result<Option<I>, RuntimeString> {
match self.data.get(identifier) {
Some(inherent) =>
I::decode(&mut &inherent[..])
.ok_or_else(|| "Could not decode requested inherent type!".into())
.map(Some),
None => Ok(None)
}
}
}
impl codec::Encode for InherentData {
fn encode(&self) -> Vec<u8> {
let keys = self.data.keys().collect::<Vec<_>>();
let values = self.data.values().collect::<Vec<_>>();
let mut encoded = keys.encode();
encoded.extend(values.encode());
encoded
}
}
impl codec::Decode for InherentData {
fn decode<I: codec::Input>(value: &mut I) -> Option<Self> {
Vec::<InherentIdentifier>::decode(value)
.and_then(|i| Vec::<Vec<u8>>::decode(value).map(|v| (i, v)))
.and_then(|(i, v)| {
if i.len() == v.len() {
Some(Self {
data: i.into_iter().zip(v.into_iter()).collect()
})
} else {
None
}
})
}
}
/// The result of checking inherents.
///
/// It either returns okay for all checks, stores all occurred errors or just one fatal error.
///
/// When a fatal error occurres, all other errors are removed and the implementation needs to
/// abbort checking inherents.
#[derive(Encode, Decode)]
pub struct CheckInherentsResult {
/// Did the check succeed?
okay: bool,
/// Did we encounter a fatal error?
fatal_error: bool,
/// We use the `InherentData` to store our errors.
errors: InherentData,
}
impl CheckInherentsResult {
/// Create a new instance.
pub fn new() -> Self {
Self {
okay: true,
errors: InherentData::new(),
fatal_error: false,
}
}
/// Put an error into the result.
///
/// This makes this result resolve to `ok() == false`.
///
/// # Parameters
///
/// - identifier - The identifier of the inherent that generated the error.
/// - error - The error that will be encoded.
pub fn put_error<E: codec::Encode + IsFatalError>(
&mut self,
identifier: InherentIdentifier,
error: &E,
) -> Result<(), RuntimeString> {
// Don't accept any other error
if self.fatal_error {
return Err("No other errors are accepted after an hard error!".into())
}
if error.is_fatal_error() {
// remove the other errors.
self.errors.data.clear();
}
self.errors.put_data(identifier, error)?;
self.okay = false;
self.fatal_error = error.is_fatal_error();
Ok(())
}
/// Get an error out of the result.
///
/// # Return
///
/// - `Ok(Some(I))` if the error could be found and deserialized.
/// - `Ok(None)` if the error could not be found.
/// - `Err(_)` if the error could be found, but deserialization did not work.
pub fn get_error<E: codec::Decode>(
&self,
identifier: &InherentIdentifier,
) -> Result<Option<E>, RuntimeString> {
self.errors.get_data(identifier)
}
/// Convert into an iterator over all contained errors.
pub fn into_errors(self) -> IntoIter<InherentIdentifier, Vec<u8>> {
self.errors.data.into_iter()
}
/// Is this result ok?
pub fn ok(&self) -> bool {
self.okay
}
/// Is this a fatal error?
pub fn fatal_error(&self) -> bool {
self.fatal_error
}
}
#[cfg(feature = "std")]
impl PartialEq for CheckInherentsResult {
fn eq(&self, other: &Self) -> bool {
self.fatal_error == other.fatal_error &&
self.okay == other.okay &&
self.errors.data == other.errors.data
}
}
/// All `InherentData` providers.
#[cfg(feature = "std")]
#[derive(Clone)]
pub struct InherentDataProviders {
providers: Arc<RwLock<Vec<Box<dyn ProvideInherentData + Send + Sync>>>>,
}
#[cfg(feature = "std")]
impl InherentDataProviders {
/// Create a new instance.
pub fn new() -> Self {
Self {
providers: Default::default(),
}
}
/// Register an `InherentData` provider.
///
/// The registration order is preserved and this order will also be used when creating the
/// inherent data.
///
/// # Result
///
/// Will return an error, if a provider with the same identifier already exists.
pub fn register_provider<P: ProvideInherentData + Send + Sync +'static>(
&self,
provider: P,
) -> Result<(), RuntimeString> {
if self.has_provider(&provider.inherent_identifier()) {
Err(
format!(
"Inherent data provider with identifier {:?} already exists!",
&provider.inherent_identifier()
).into()
)
} else {
provider.on_register(self)?;
self.providers.write().push(Box::new(provider));
Ok(())
}
}
/// Returns if a provider for the given identifier exists.
pub fn has_provider(&self, identifier: &InherentIdentifier) -> bool {
self.providers.read().iter().any(|p| p.inherent_identifier() == identifier)
}
/// Create inherent data.
pub fn create_inherent_data(&self) -> Result<InherentData, RuntimeString> {
let mut data = InherentData::new();
self.providers.read().iter().try_for_each(|p| {
p.provide_inherent_data(&mut data)
.map_err(|e| format!("Error for `{:?}`: {:?}", p.inherent_identifier(), e))
})?;
Ok(data)
}
/// Converts a given encoded error into a `String`.
///
/// Useful if the implementation encouters an error for an identifier it does not know.
pub fn error_to_string(&self, identifier: &InherentIdentifier, error: &[u8]) -> String {
let res = self.providers.read().iter().filter_map(|p|
if p.inherent_identifier() == identifier {
Some(
p.error_to_string(error)
.unwrap_or_else(|| error_to_string_fallback(identifier))
)
} else {
None
}
).next();
match res {
Some(res) => res,
None => format!(
"Error while checking inherent of type \"{}\", but this inherent type is unknown.",
String::from_utf8_lossy(identifier)
)
}
}
}
/// Something that provides inherent data.
#[cfg(feature = "std")]
pub trait ProvideInherentData {
/// Is called when this inherent data provider is registered at the given
/// `InherentDataProviders`.
fn on_register(&self, _: &InherentDataProviders) -> Result<(), RuntimeString> {
Ok(())
}
/// The identifier of the inherent for that data will be provided.
fn inherent_identifier(&self) -> &'static InherentIdentifier;
/// Provide inherent data that should be included in a block.
///
/// The data should be stored in the given `InherentData` structure.
fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), RuntimeString>;
/// Convert the given encoded error to a string.
///
/// If the given error could not be decoded, `None` should be returned.
fn error_to_string(&self, error: &[u8]) -> Option<String>;
}
/// A fallback function, if the decoding of an error fails.
#[cfg(feature = "std")]
fn error_to_string_fallback(identifier: &InherentIdentifier) -> String {
format!(
"Error while checking inherent of type \"{}\", but error could not be decoded.",
String::from_utf8_lossy(identifier)
)
}
/// Did we encounter a fatal error while checking an inherent?
///
/// A fatal error is everything that fails while checking an inherent error, e.g. the inherent
/// was not found, could not be decoded etc.
/// Then there are cases where you not want the inherent check to fail, but report that there is an
/// action required. For example a timestamp of a block is in the future, the timestamp is still
/// correct, but it is required to verify the block at a later time again and then the inherent
/// check will succeed.
pub trait IsFatalError {
/// Is this a fatal error?
fn is_fatal_error(&self) -> bool;
}
/// Auxiliary to make any given error resolve to `is_fatal_error() == true`.
#[derive(Encode)]
pub struct MakeFatalError<E: codec::Encode>(E);
impl<E: codec::Encode> From<E> for MakeFatalError<E> {
fn from(err: E) -> Self {
MakeFatalError(err)
}
}
impl<E: codec::Encode> IsFatalError for MakeFatalError<E> {
fn is_fatal_error(&self) -> bool {
true
}
}
/// A module that provides an inherent and may also verifies it.
pub trait ProvideInherent {
/// The call type of the module.
type Call;
/// The error returned by `check_inherent`.
type Error: codec::Encode + IsFatalError;
/// The inherent identifier used by this inherent.
const INHERENT_IDENTIFIER: self::InherentIdentifier;
/// Create an inherent out of the given `InherentData`.
fn create_inherent(data: &InherentData) -> Option<Self::Call>;
/// Check the given inherent if it is valid.
/// Checking the inherent is optional and can be ommitted.
fn check_inherent(_: &Self::Call, _: &InherentData) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use codec::{Encode, Decode};
const TEST_INHERENT_0: InherentIdentifier = *b"testinh0";
const TEST_INHERENT_1: InherentIdentifier = *b"testinh1";
#[derive(Encode)]
struct NoFatalError<E: codec::Encode>(E);
impl<E: codec::Encode> IsFatalError for NoFatalError<E> {
fn is_fatal_error(&self) -> bool {
false
}
}
#[test]
fn inherent_data_encodes_and_decodes() {
let inherent_0 = vec![1, 2, 3];
let inherent_1: u32 = 7;
let mut data = InherentData::new();
data.put_data(TEST_INHERENT_0, &inherent_0).unwrap();
data.put_data(TEST_INHERENT_1, &inherent_1).unwrap();
let encoded = data.encode();
let decoded = InherentData::decode(&mut &encoded[..]).unwrap();
assert_eq!(decoded.get_data::<Vec<u32>>(&TEST_INHERENT_0).unwrap().unwrap(), inherent_0);
assert_eq!(decoded.get_data::<u32>(&TEST_INHERENT_1).unwrap().unwrap(), inherent_1);
}
#[test]
fn adding_same_inherent_returns_an_error() {
let mut data = InherentData::new();
data.put_data(TEST_INHERENT_0, &8).unwrap();
assert!(data.put_data(TEST_INHERENT_0, &10).is_err());
}
#[derive(Clone)]
struct TestInherentDataProvider {
registered: Arc<RwLock<bool>>,
}
impl TestInherentDataProvider {
fn new() -> Self {
let inst = Self {
registered: Default::default(),
};
// just make sure
assert!(!inst.is_registered());
inst
}
fn is_registered(&self) -> bool {
*self.registered.read()
}
}
const ERROR_TO_STRING: &str = "Found error!";
impl ProvideInherentData for TestInherentDataProvider {
fn on_register(&self, _: &InherentDataProviders) -> Result<(), RuntimeString> {
*self.registered.write() = true;
Ok(())
}
fn inherent_identifier(&self) -> &'static InherentIdentifier {
&TEST_INHERENT_0
}
fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), RuntimeString> {
data.put_data(TEST_INHERENT_0, &42)
}
fn error_to_string(&self, _: &[u8]) -> Option<String> {
Some(ERROR_TO_STRING.into())
}
}
#[test]
fn registering_inherent_provider() {
let provider = TestInherentDataProvider::new();
let providers = InherentDataProviders::new();
providers.register_provider(provider.clone()).unwrap();
assert!(provider.is_registered());
assert!(providers.has_provider(provider.inherent_identifier()));
// Second time should fail
assert!(providers.register_provider(provider.clone()).is_err());
}
#[test]
fn create_inherent_data_from_all_providers() {
let provider = TestInherentDataProvider::new();
let providers = InherentDataProviders::new();
providers.register_provider(provider.clone()).unwrap();
assert!(provider.is_registered());
let inherent_data = providers.create_inherent_data().unwrap();
assert_eq!(
inherent_data.get_data::<u32>(provider.inherent_identifier()).unwrap().unwrap(),
42u32
);
}
#[test]
fn encoded_error_to_string() {
let provider = TestInherentDataProvider::new();
let providers = InherentDataProviders::new();
providers.register_provider(provider.clone()).unwrap();
assert!(provider.is_registered());
assert_eq!(
&providers.error_to_string(&TEST_INHERENT_0, &[1, 2]), ERROR_TO_STRING
);
assert!(
providers
.error_to_string(&TEST_INHERENT_1, &[1, 2])
.contains("inherent type is unknown")
);
}
#[test]
fn check_inherents_result_encodes_and_decodes() {
let mut result = CheckInherentsResult::new();
assert!(result.ok());
result.put_error(TEST_INHERENT_0, &NoFatalError(2u32)).unwrap();
assert!(!result.ok());
assert!(!result.fatal_error());
let encoded = result.encode();
let decoded = CheckInherentsResult::decode(&mut &encoded[..]).unwrap();
assert_eq!(decoded.get_error::<u32>(&TEST_INHERENT_0).unwrap().unwrap(), 2);
assert!(!decoded.ok());
assert!(!decoded.fatal_error());
}
#[test]
fn check_inherents_result_removes_other_errors_on_fatal_error() {
let mut result = CheckInherentsResult::new();
assert!(result.ok());
result.put_error(TEST_INHERENT_0, &NoFatalError(2u32)).unwrap();
assert!(!result.ok());
assert!(!result.fatal_error());
result.put_error(TEST_INHERENT_1, &MakeFatalError(4u32)).unwrap();
assert!(!result.ok());
assert!(result.fatal_error());
assert!(result.put_error(TEST_INHERENT_0, &NoFatalError(5u32)).is_err());
result.into_errors().for_each(|(i, e)| match i {
TEST_INHERENT_1 => assert_eq!(u32::decode(&mut &e[..]).unwrap(), 4),
_ => panic!("There should be no other error!"),
});
}
}
+1 -2
View File
@@ -406,7 +406,7 @@ impl<V: 'static + Verifier<Block>, D> Peer<V, D> {
/// Add blocks to the peer -- edit the block before adding
pub fn generate_blocks<F>(&self, count: usize, origin: BlockOrigin, mut edit_block: F)
where F: FnMut(BlockBuilder<Block, (), PeersClient>) -> Block
where F: FnMut(BlockBuilder<Block, PeersClient>) -> Block
{
for _ in 0..count {
let builder = self.client.new_block().unwrap();
@@ -493,7 +493,6 @@ pub trait TestNetFactory: Sized {
fn from_config(config: &ProtocolConfig) -> Self;
fn make_verifier(&self, client: Arc<PeersClient>, config: &ProtocolConfig) -> Arc<Self::Verifier>;
/// Get reference to peer.
fn peer(&self, i: usize) -> &Peer<Self::Verifier, Self::PeerData>;
fn peers(&self) -> &Vec<Arc<Peer<Self::Verifier, Self::PeerData>>>;
+19 -4
View File
@@ -32,7 +32,14 @@ use error;
use chain_spec::ChainSpec;
/// Export a range of blocks to a binary stream.
pub fn export_blocks<F, E, W>(config: FactoryFullConfiguration<F>, exit: E, mut output: W, from: FactoryBlockNumber<F>, to: Option<FactoryBlockNumber<F>>, json: bool) -> error::Result<()>
pub fn export_blocks<F, E, W>(
config: FactoryFullConfiguration<F>,
exit: E,
mut output: W,
from: FactoryBlockNumber<F>,
to: Option<FactoryBlockNumber<F>>,
json: bool
) -> error::Result<()>
where
F: ServiceFactory,
E: Future<Item=(),Error=()> + Send + 'static,
@@ -68,7 +75,8 @@ pub fn export_blocks<F, E, W>(config: FactoryFullConfiguration<F>, exit: E, mut
match client.block(&BlockId::number(block))? {
Some(block) => {
if json {
serde_json::to_writer(&mut output, &block).map_err(|e| format!("Eror writing JSON: {}", e))?;
serde_json::to_writer(&mut output, &block)
.map_err(|e| format!("Eror writing JSON: {}", e))?;
} else {
output.write(&block.encode())?;
}
@@ -87,7 +95,11 @@ pub fn export_blocks<F, E, W>(config: FactoryFullConfiguration<F>, exit: E, mut
}
/// Import blocks from a binary stream.
pub fn import_blocks<F, E, R>(mut config: FactoryFullConfiguration<F>, exit: E, mut input: R) -> error::Result<()>
pub fn import_blocks<F, E, R>(
mut config: FactoryFullConfiguration<F>,
exit: E,
mut input: R
) -> error::Result<()>
where F: ServiceFactory, E: Future<Item=(),Error=()> + Send + 'static, R: Read,
{
struct DummyLink;
@@ -148,7 +160,10 @@ pub fn import_blocks<F, E, R>(mut config: FactoryFullConfiguration<F>, exit: E,
}
/// Revert the chain.
pub fn revert_chain<F>(config: FactoryFullConfiguration<F>, blocks: FactoryBlockNumber<F>) -> error::Result<()>
pub fn revert_chain<F>(
config: FactoryFullConfiguration<F>,
blocks: FactoryBlockNumber<F>
) -> error::Result<()>
where F: ServiceFactory,
{
let client = new_client::<F>(&config)?;
+3 -1
View File
@@ -27,7 +27,9 @@ use consensus_common::import_queue::ImportQueue;
use network::{self, OnDemand};
use substrate_executor::{NativeExecutor, NativeExecutionDispatch};
use transaction_pool::txpool::{self, Options as TransactionPoolOptions, Pool as TransactionPool};
use runtime_primitives::{BuildStorage, traits::{Block as BlockT, Header as HeaderT, ProvideRuntimeApi}, generic::BlockId};
use runtime_primitives::{
BuildStorage, traits::{Block as BlockT, Header as HeaderT, ProvideRuntimeApi}, generic::BlockId
};
use config::Configuration;
use primitives::{Blake2Hasher, H256};
use rpc::{self, apis::system::SystemInfo};
+2
View File
@@ -19,6 +19,7 @@
use client;
use network;
use keystore;
use consensus_common;
error_chain! {
foreign_links {
@@ -27,6 +28,7 @@ error_chain! {
links {
Client(client::error::Error, client::error::ErrorKind) #[doc="Client error"];
Consensus(consensus_common::Error, consensus_common::ErrorKind) #[doc="Consesus error"];
Network(network::error::Error, network::error::ErrorKind) #[doc="Network error"];
Keystore(keystore::Error, keystore::ErrorKind) #[doc="Keystore error"];
}
+10 -5
View File
@@ -77,7 +77,9 @@ use codec::{Encode, Decode};
pub use self::error::{ErrorKind, Error};
pub use config::{Configuration, Roles, PruningMode};
pub use chain_spec::{ChainSpec, Properties};
pub use transaction_pool::txpool::{self, Pool as TransactionPool, Options as TransactionPoolOptions, ChainApi, IntoPoolError};
pub use transaction_pool::txpool::{
self, Pool as TransactionPool, Options as TransactionPoolOptions, ChainApi, IntoPoolError
};
pub use client::{ExecutionStrategy, FinalityNotifications};
pub use components::{ServiceFactory, FullBackend, FullExecutor, LightBackend,
@@ -294,7 +296,8 @@ impl<Components: components::Components> Service<Components> {
properties: config.chain_spec.properties(),
};
let rpc = Components::RPC::start_rpc(
client.clone(), network.clone(), has_bootnodes, system_info, config.rpc_http, config.rpc_ws, task_executor.clone(), transaction_pool.clone(),
client.clone(), network.clone(), has_bootnodes, system_info, config.rpc_http,
config.rpc_ws, task_executor.clone(), transaction_pool.clone(),
)?;
// Telemetry
@@ -392,8 +395,8 @@ impl<Components> Drop for Service<Components> where Components: components::Comp
}
}
fn maybe_start_server<T, F>(address: Option<SocketAddr>, start: F) -> Result<Option<T>, io::Error> where
F: Fn(&SocketAddr) -> Result<T, io::Error>,
fn maybe_start_server<T, F>(address: Option<SocketAddr>, start: F) -> Result<Option<T>, io::Error>
where F: Fn(&SocketAddr) -> Result<T, io::Error>,
{
Ok(match address {
Some(mut address) => Some(start(&address)
@@ -428,7 +431,9 @@ impl<C: Components> TransactionPoolAdapter<C> {
}
}
impl<C: Components> network::TransactionPool<ComponentExHash<C>, ComponentBlock<C>> for TransactionPoolAdapter<C> where <C as components::Components>::RuntimeApi: Send + Sync{
impl<C: Components> network::TransactionPool<ComponentExHash<C>, ComponentBlock<C>> for
TransactionPoolAdapter<C> where <C as components::Components>::RuntimeApi: Send + Sync
{
fn transactions(&self) -> Vec<(ComponentExHash<C>, ComponentExtrinsic<C>)> {
self.pool.ready()
.map(|t| {
+1 -1
View File
@@ -182,7 +182,7 @@ impl<F: ServiceFactory> TestNet<F> {
}
}
pub fn connectivity<F: ServiceFactory, Inherent>(spec: FactoryChainSpec<F>) {
pub fn connectivity<F: ServiceFactory>(spec: FactoryChainSpec<F>) {
const NUM_NODES: u32 = 10;
{
let temp = TempDir::new("substrate-connectivity-test").expect("Error creating test dir");
-2
View File
@@ -30,5 +30,3 @@ std = [
"parity-codec/std",
"substrate-primitives/std",
]
api-for-runtime = []
-51
View File
@@ -511,57 +511,6 @@ macro_rules! impl_outer_log {
};
}
//TODO: https://github.com/paritytech/substrate/issues/1022
/// Basic Inherent data to include in a block; used by simple runtimes.
#[derive(Encode, Decode)]
pub struct BasicInherentData {
/// Current timestamp.
pub timestamp: u64,
/// Blank report.
pub consensus: (),
/// Aura expected slot. Can take any value during block construction.
pub aura_expected_slot: u64,
}
impl BasicInherentData {
/// Create a new `BasicInherentData` instance.
pub fn new(timestamp: u64, expected_slot: u64) -> Self {
Self {
timestamp,
consensus: (),
aura_expected_slot: expected_slot,
}
}
}
//TODO: https://github.com/paritytech/substrate/issues/1022
/// Error type used while checking inherents.
#[derive(Encode, PartialEq)]
#[cfg_attr(feature = "std", derive(Decode))]
pub enum CheckInherentError {
/// The inherents are generally valid but a delay until the given timestamp
/// is required.
ValidAtTimestamp(u64),
/// Some other error has occurred.
Other(RuntimeString),
}
impl CheckInherentError {
/// Combine two results, taking the "worse" of the two.
pub fn combine_results<F: FnOnce() -> Result<(), Self>>(this: Result<(), Self>, other: F) -> Result<(), Self> {
match this {
Ok(()) => other(),
Err(CheckInherentError::Other(s)) => Err(CheckInherentError::Other(s)),
Err(CheckInherentError::ValidAtTimestamp(x)) => match other() {
Ok(()) => Err(CheckInherentError::ValidAtTimestamp(x)),
Err(CheckInherentError::ValidAtTimestamp(y))
=> Err(CheckInherentError::ValidAtTimestamp(rstd::cmp::max(x, y))),
Err(CheckInherentError::Other(s)) => Err(CheckInherentError::Other(s)),
}
}
}
}
/// Simple blob to hold an extrinsic without commiting to its format and ensure it is serialized
/// correctly.
#[derive(PartialEq, Eq, Clone, Default, Encode, Decode)]
@@ -621,26 +621,6 @@ pub trait DigestItem: Codec + Member + MaybeSerializeDebugButNotDeserialize {
fn as_changes_trie_root(&self) -> Option<&Self::Hash>;
}
/// Something that provides an inherent for a runtime.
pub trait ProvideInherent {
/// The inherent that is provided.
type Inherent: Encode + MaybeDecode;
/// The call for setting the inherent.
type Call: Encode + MaybeDecode;
/// Create the inherent extrinsics.
///
/// # Return
///
/// Returns a vector with tuples containing the index for the extrinsic and the extrinsic itself.
fn create_inherent_extrinsics(data: Self::Inherent) -> Vec<(u32, Self::Call)>;
/// Check that the given inherent is valid.
fn check_inherent<Block: self::Block, F: Fn(&Block::Extrinsic) -> Option<&Self::Call>>(
block: &Block, data: Self::Inherent, extract_function: &F
) -> Result<(), super::CheckInherentError>;
}
/// Auxiliary wrapper that holds an api instance and binds it to the given lifetime.
pub struct ApiRef<'a, T>(T, rstd::marker::PhantomData<&'a ()>);
@@ -29,9 +29,9 @@ pub trait BlockBuilderExt {
fn push_transfer(&mut self, transfer: runtime::Transfer) -> Result<(), client::error::Error>;
}
impl<'a, A> BlockBuilderExt for client::block_builder::BlockBuilder<'a, runtime::Block, (), A> where
impl<'a, A> BlockBuilderExt for client::block_builder::BlockBuilder<'a, runtime::Block, A> where
A: ProvideRuntimeApi + client::blockchain::HeaderBackend<runtime::Block> + 'a,
A::Api: BlockBuilder<runtime::Block, ()>
A::Api: BlockBuilder<runtime::Block>
{
fn push_transfer(&mut self, transfer: runtime::Transfer) -> Result<(), client::error::Error> {
self.push(sign_tx(transfer))
@@ -39,6 +39,9 @@ impl<'a, A> BlockBuilderExt for client::block_builder::BlockBuilder<'a, runtime:
}
fn sign_tx(transfer: runtime::Transfer) -> runtime::Extrinsic {
let signature = keyring::Keyring::from_raw_public(transfer.from.to_fixed_bytes()).unwrap().sign(&codec::Encode::encode(&transfer)).into();
let signature = keyring::Keyring::from_raw_public(transfer.from.to_fixed_bytes())
.unwrap()
.sign(&codec::Encode::encode(&transfer))
.into();
runtime::Extrinsic::Transfer(transfer, signature)
}
+2
View File
@@ -13,6 +13,7 @@ parity-codec-derive = { version = "2.1", default-features = false }
substrate-keyring = { path = "../keyring", optional = true }
substrate-client = { path = "../client", default-features = false }
substrate-primitives = { path = "../primitives", default-features = false }
substrate-inherents = { path = "../inherents", default-features = false }
substrate-consensus-aura-primitives = { path = "../consensus/aura/primitives", default-features = false }
sr-std = { path = "../sr-std", default-features = false }
sr-io = { path = "../sr-io", default-features = false }
@@ -37,6 +38,7 @@ std = [
"sr-io/std",
"srml-support/std",
"substrate-primitives/std",
"substrate-inherents/std",
"sr-primitives/std",
"sr-version/std",
"substrate-consensus-aura-primitives/std",
+7 -5
View File
@@ -24,6 +24,7 @@ extern crate serde;
extern crate sr_std as rstd;
extern crate parity_codec as codec;
extern crate sr_primitives as runtime_primitives;
extern crate substrate_inherents as inherents;
extern crate substrate_consensus_aura_primitives as consensus_aura;
#[macro_use]
@@ -59,7 +60,7 @@ use runtime_primitives::{
traits::{
BlindCheckable, BlakeTwo256, Block as BlockT, Extrinsic as ExtrinsicT,
GetNodeBlockType, GetRuntimeBlockType
}, CheckInherentError
}
};
use runtime_version::RuntimeVersion;
pub use primitives::hash::H256;
@@ -67,6 +68,7 @@ use primitives::{Ed25519AuthorityId, OpaqueMetadata};
#[cfg(any(feature = "std", test))]
use runtime_version::NativeVersion;
use consensus_aura::api as aura_api;
use inherents::{CheckInherentsResult, InherentData};
/// Test runtime version.
pub const VERSION: RuntimeVersion = RuntimeVersion {
@@ -238,7 +240,7 @@ impl_runtime_apis! {
}
}
impl block_builder_api::BlockBuilder<Block, ()> for Runtime {
impl block_builder_api::BlockBuilder<Block> for Runtime {
fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyResult {
system::execute_transaction(extrinsic)
}
@@ -247,12 +249,12 @@ impl_runtime_apis! {
system::finalise_block()
}
fn inherent_extrinsics(_data: ()) -> Vec<<Block as BlockT>::Extrinsic> {
fn inherent_extrinsics(_data: InherentData) -> Vec<<Block as BlockT>::Extrinsic> {
unimplemented!()
}
fn check_inherents(_block: Block, _data: ()) -> Result<(), CheckInherentError> {
Ok(())
fn check_inherents(_block: Block, _data: InherentData) -> CheckInherentsResult {
CheckInherentsResult::new()
}
fn random_seed() -> <Block as BlockT>::Hash {
+24 -20
View File
@@ -542,12 +542,12 @@ dependencies = [
[[package]]
name = "parity-codec-derive"
version = "2.1.0"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -914,7 +914,7 @@ dependencies = [
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-io 0.1.0",
@@ -935,7 +935,7 @@ version = "0.1.0"
dependencies = [
"impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 0.1.0",
@@ -947,7 +947,7 @@ name = "srml-metadata"
version = "0.1.0"
dependencies = [
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-std 0.1.0",
@@ -969,6 +969,7 @@ dependencies = [
"sr-std 0.1.0",
"srml-metadata 0.1.0",
"srml-support-procedural 0.1.0",
"substrate-inherents 0.1.0",
]
[[package]]
@@ -1032,6 +1033,7 @@ dependencies = [
"sr-version 0.1.0",
"substrate-consensus-common 0.1.0",
"substrate-executor 0.1.0",
"substrate-inherents 0.1.0",
"substrate-keyring 0.1.0",
"substrate-primitives 0.1.0",
"substrate-state-machine 0.1.0",
@@ -1060,10 +1062,11 @@ dependencies = [
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 0.1.0",
"sr-version 0.1.0",
"substrate-inherents 0.1.0",
"substrate-primitives 0.1.0",
"tokio 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -1090,6 +1093,17 @@ dependencies = [
"wasmi 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "substrate-inherents"
version = "0.1.0"
dependencies = [
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 0.1.0",
"sr-std 0.1.0",
]
[[package]]
name = "substrate-keyring"
version = "0.1.0"
@@ -1111,7 +1125,7 @@ dependencies = [
"hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"primitive-types 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hex 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1168,7 +1182,7 @@ dependencies = [
"hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-codec-derive 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.81 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-io 0.1.0",
@@ -1178,6 +1192,7 @@ dependencies = [
"srml-support 0.1.0",
"substrate-client 0.1.0",
"substrate-consensus-aura-primitives 0.1.0",
"substrate-inherents 0.1.0",
"substrate-keyring 0.1.0",
"substrate-primitives 0.1.0",
]
@@ -1200,16 +1215,6 @@ dependencies = [
"trie-root 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.14.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.15.22"
@@ -1625,7 +1630,7 @@ dependencies = [
"checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13"
"checksum parity-bytes 0.1.0 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)" = "<none>"
"checksum parity-codec 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e7b6a1290fe78aa6bbb5f3338ecede3062687a98b9e40cd1dbcaa47261d44097"
"checksum parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ffa42c2cb493b60b12c75b26e8c94cb734af4df4d7f2cc229dc04c1953dac189"
"checksum parity-codec-derive 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2632f530f37c8b939c7c194636a82ecbe41ab115e74e88f947ad41e483bbf19"
"checksum parity-wasm 0.31.3 (registry+https://github.com/rust-lang/crates.io-index)" = "511379a8194230c2395d2f5fa627a5a7e108a9f976656ce723ae68fca4097bfc"
"checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5"
"checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337"
@@ -1669,7 +1674,6 @@ dependencies = [
"checksum smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "b73ea3738b47563803ef814925e69be00799a8c07420be8b996f8e98fb2336db"
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
"checksum static_assertions 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c19be23126415861cb3a23e501d34a708f7f9b2183c5252d690941c2e69199d5"
"checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741"
"checksum syn 0.15.22 (registry+https://github.com/rust-lang/crates.io-index)" = "ae8b29eb5210bc5cf63ed6149cbf9adfc82ac0be023d8735c176ee74a2db4da7"
"checksum take_mut 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
+2 -3
View File
@@ -34,10 +34,10 @@ impl Encode for NodeHeader {
fn encode_to<T: Output>(&self, output: &mut T) {
match self {
NodeHeader::Null => output.push_byte(EMPTY_TRIE),
NodeHeader::Branch(true) => output.push_byte(BRANCH_NODE_WITH_VALUE),
NodeHeader::Branch(false) => output.push_byte(BRANCH_NODE_NO_VALUE),
NodeHeader::Leaf(nibble_count) if *nibble_count < LEAF_NODE_THRESHOLD as usize =>
output.push_byte(LEAF_NODE_OFFSET + *nibble_count as u8),
NodeHeader::Leaf(nibble_count) => {
@@ -72,7 +72,6 @@ impl Decode for NodeHeader {
BRANCH_NODE_NO_VALUE => NodeHeader::Branch(false), // 254
BRANCH_NODE_WITH_VALUE => NodeHeader::Branch(true), // 255
_ => unreachable!(),
})
}