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

Commit

Permalink
Rewrite Inherent data (#1488)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bkchr authored Jan 22, 2019
1 parent 9edfc24 commit 53bf81e
Show file tree
Hide file tree
Showing 55 changed files with 1,512 additions and 660 deletions.
108 changes: 60 additions & 48 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ members = [
"core/keystore",
"core/transaction-pool",
"core/transaction-pool/graph",
"core/inherents",
"srml/support",
"srml/support/procedural",
"srml/support/procedural/tools",
Expand Down
13 changes: 7 additions & 6 deletions core/basic-authorship/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
71 changes: 20 additions & 51 deletions core/basic-authorship/src/basic_authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -85,21 +82,21 @@ 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;

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))?;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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>,
Expand All @@ -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,
Expand Down Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion core/basic-authorship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion core/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -39,6 +40,7 @@ std = [
"codec/std",
"consensus",
"primitives/std",
"inherents/std",
"parking_lot",
"error-chain",
"fnv",
Expand Down
7 changes: 4 additions & 3 deletions core/client/src/block_builder/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@

//! 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.
fn finalise_block() -> <Block as BlockT>::Header;
/// 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;
}
Expand Down
9 changes: 3 additions & 6 deletions core/client/src/block_builder/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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> {
Expand Down Expand Up @@ -75,7 +73,6 @@ where
extrinsics: Vec::new(),
api,
block_id: *block_id,
_marker: PhantomData,
})
}

Expand Down
19 changes: 11 additions & 8 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion core/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 53bf81e

Please sign in to comment.