diff --git a/substrate/core/client/db/src/light.rs b/substrate/core/client/db/src/light.rs index 8d0712658b..c03ab98c02 100644 --- a/substrate/core/client/db/src/light.rs +++ b/substrate/core/client/db/src/light.rs @@ -480,17 +480,16 @@ pub(crate) mod tests { } } - pub fn insert_block_with_extrinsics_root( - db: &LightStorage, - parent: &Hash, - number: u64, - authorities: Option>, - extrinsics_root: Hash, - ) -> Hash { - let header = prepare_header(parent, number, extrinsics_root); - let hash = header.hash(); - db.import_header(header, authorities, NewBlockState::Best, Vec::new()).unwrap(); - hash + fn header_with_changes_trie(parent: &Hash, number: u64) -> Header { + let mut header = default_header(parent, number); + header.digest.logs.push(DigestItem::ChangesTrieRoot([(number % 256) as u8; 32].into())); + header + } + + fn header_with_extrinsics_root(parent: &Hash, number: u64, extrinsics_root: Hash) -> Header { + let mut header = default_header(parent, number); + header.extrinsics_root = extrinsics_root; + header } pub fn insert_block Header>( diff --git a/substrate/core/client/src/block_builder/block_builder.rs b/substrate/core/client/src/block_builder/block_builder.rs index cb67db5adf..7d5b867e3a 100644 --- a/substrate/core/client/src/block_builder/block_builder.rs +++ b/substrate/core/client/src/block_builder/block_builder.rs @@ -39,7 +39,7 @@ impl<'a, Block, A> BlockBuilder<'a, Block, A> where Block: BlockT, A: ProvideRuntimeApi + HeaderBackend + 'a, - A::Api: BlockBuilderApi + Core, + A::Api: BlockBuilderApi, { /// Create a new instance of builder from the given client, building on the latest block. pub fn new(api: &'a A) -> error::Result { @@ -84,7 +84,7 @@ where block_id: &BlockId, xt: Block::Extrinsic, extrinsics: &mut Vec - ) -> error::Result<()> where T: BlockBuilderApi + Core { + ) -> error::Result<()> where T: BlockBuilderApi { api.map_api_result(|api| { match api.apply_extrinsic(block_id, &xt)? { Ok(ApplyOutcome::Success) | Ok(ApplyOutcome::Fail) => { diff --git a/substrate/core/client/src/client.rs b/substrate/core/client/src/client.rs index df03cc8e0e..03cefca49e 100644 --- a/substrate/core/client/src/client.rs +++ b/substrate/core/client/src/client.rs @@ -547,7 +547,7 @@ impl Client where &self ) -> error::Result> where E: Clone + Send + Sync, - RA: BlockBuilderAPI + CoreAPI, + RA: BlockBuilderAPI { block_builder::BlockBuilder::new(self) } @@ -557,7 +557,7 @@ impl Client where &self, parent: &BlockId ) -> error::Result> where E: Clone + Send + Sync, - RA: BlockBuilderAPI + CoreAPI, + RA: BlockBuilderAPI { block_builder::BlockBuilder::at_block(parent, &self) } @@ -568,7 +568,7 @@ impl Client where at: Block::Hash, body: &Option> ) -> error::Result> where - RA: TaggedTransactionQueue + CoreAPI, + RA: TaggedTransactionQueue, E: CallExecutor + Send + Sync + Clone, { let id = BlockId::Hash(at); @@ -604,7 +604,7 @@ impl Client where finalized: bool, aux: Vec<(Vec, Option>)>, ) -> error::Result where - RA: TaggedTransactionQueue + CoreAPI, + RA: TaggedTransactionQueue, E: CallExecutor + Send + Sync + Clone, { let parent_hash = import_headers.post().parent_hash().clone(); @@ -1058,7 +1058,7 @@ impl consensus::BlockImport for Client B: backend::Backend, E: CallExecutor + Clone + Send + Sync, Block: BlockT, - RA: TaggedTransactionQueue + CoreAPI, + RA: TaggedTransactionQueue { type Error = Error; diff --git a/substrate/core/client/src/light/blockchain.rs b/substrate/core/client/src/light/blockchain.rs index e50f62ec20..5996732c3f 100644 --- a/substrate/core/client/src/light/blockchain.rs +++ b/substrate/core/client/src/light/blockchain.rs @@ -213,6 +213,7 @@ pub mod tests { _header: Header, _authorities: Option>, _state: NewBlockState, + _aux_ops: Vec<(Vec, Option>)>, ) -> ClientResult<()> { Err(ClientErrorKind::Backend("Test error".into()).into()) } diff --git a/substrate/core/client/src/runtime_api/macros.rs b/substrate/core/client/src/runtime_api/macros.rs index ad13d8b265..b58f2143c8 100644 --- a/substrate/core/client/src/runtime_api/macros.rs +++ b/substrate/core/client/src/runtime_api/macros.rs @@ -347,7 +347,7 @@ macro_rules! decl_runtime_apis { ) => { $( #[$attr] )* #[cfg(feature = "std")] - pub trait $name < $( $generic_param_parsed $( : $generic_bound_parsed )* ),* > { + pub trait $name < $( $generic_param_parsed $( : $generic_bound_parsed )* ),* > : $crate::runtime_api::Core { $( type $client_generic_param $( : $client_generic_bound )*; )* $( diff --git a/substrate/core/client/src/runtime_api/mod.rs b/substrate/core/client/src/runtime_api/mod.rs index fd3bc5f172..acf4db705c 100644 --- a/substrate/core/client/src/runtime_api/mod.rs +++ b/substrate/core/client/src/runtime_api/mod.rs @@ -23,7 +23,7 @@ pub use state_machine::OverlayedChanges; #[doc(hidden)] pub use runtime_primitives::{traits::Block as BlockT, generic::BlockId}; #[cfg(feature = "std")] -pub use runtime_primitives::traits::ApiRef; +use runtime_primitives::traits::ApiRef; pub use runtime_version::ApiId; #[doc(hidden)] pub use rstd::slice; @@ -32,6 +32,7 @@ use rstd::result; pub use codec::{Encode, Decode}; #[cfg(feature = "std")] use error; +pub use runtime_version::RuntimeVersion; mod core; #[macro_use] diff --git a/substrate/core/finality-grandpa/src/lib.rs b/substrate/core/finality-grandpa/src/lib.rs index 7aa377410f..6a70b3dfca 100644 --- a/substrate/core/finality-grandpa/src/lib.rs +++ b/substrate/core/finality-grandpa/src/lib.rs @@ -84,7 +84,7 @@ use futures::stream::Fuse; use futures::sync::mpsc; use client::{Client, error::Error as ClientError, ImportNotifications, backend::Backend, CallExecutor}; use client::blockchain::HeaderBackend; -use client::runtime_api::{Core as CoreAPI, TaggedTransactionQueue}; +use client::runtime_api::TaggedTransactionQueue; use codec::{Encode, Decode}; use consensus_common::{BlockImport, ImportBlock, ImportResult}; use runtime_primitives::traits::{ @@ -775,16 +775,20 @@ impl, N, RA> voter::Environment { - import: Import, +pub struct GrandpaBlockImport, RA, PRA> { + inner: Arc>, authority_set: SharedAuthoritySet>, - api: Api, + api: Arc, } -impl> BlockImport for GrandpaBlockImport where - Import: BlockImport, - Api: ProvideRuntimeApi, - Api::Api: GrandpaApi, +impl, RA, PRA> BlockImport + for GrandpaBlockImport where + B: Backend + 'static, + E: CallExecutor + 'static + Clone + Send + Sync, + DigestFor: Encode, + RA: TaggedTransactionQueue, + PRA: ProvideRuntimeApi, + PRA::Api: GrandpaApi { type Error = ClientError; @@ -818,7 +822,7 @@ impl> BlockImport for GrandpaBlockI (old_set, authorities) }); - let result = self.import.import_block(block, new_authorities); + let result = self.inner.import_block(block, new_authorities); if let Err(ref e) = result { if let Some((old_set, mut authorities)) = just_in_case { debug!(target: "afg", "Restoring old set after block import error: {:?}", e); @@ -832,19 +836,23 @@ impl> BlockImport for GrandpaBlockI /// Half of a link between a block-import worker and a the background voter. // This should remain non-clone. -pub struct LinkHalf { - client: C, +pub struct LinkHalf, RA> { + client: Arc>, authority_set: SharedAuthoritySet>, } /// Make block importer and link half necessary to tie the background voter /// to it. -pub fn block_import(client: Arc>) - -> Result<(GrandpaBlockImport, LinkHalf), ClientError> +pub fn block_import, RA, PRA>( + client: Arc>, + api: Arc +) -> Result<(GrandpaBlockImport, LinkHalf), ClientError> where B: Backend + 'static, E: CallExecutor + 'static + Clone + Send + Sync, - RA: GrandpaApi, + RA: Send + Sync, + PRA: ProvideRuntimeApi, + PRA::Api: GrandpaApi { use runtime_primitives::traits::Zero; let authority_set = match client.backend().get_aux(AUTHORITY_SET_KEY)? { @@ -855,7 +863,7 @@ pub fn block_import(client: Arc>) // no authority set on disk: fetch authorities from genesis state. // if genesis state is not available, we may be a light client, but these // are unsupported for following GRANDPA directly. - let genesis_authorities = client.runtime_api() + let genesis_authorities = api.runtime_api() .grandpa_authorities(&BlockId::number(Zero::zero()))?; let authority_set = SharedAuthoritySet::genesis(genesis_authorities); @@ -872,7 +880,11 @@ pub fn block_import(client: Arc>) }; Ok(( - GrandpaBlockImport { inner: client.clone(), authority_set: authority_set.clone() }, + GrandpaBlockImport { + inner: client.clone(), + authority_set: authority_set.clone(), + api + }, LinkHalf { client, authority_set }, )) } @@ -881,7 +893,7 @@ pub fn block_import(client: Arc>) /// block import worker that has already been instantiated with `block_import`. pub fn run_grandpa, N, RA>( config: Config, - link: LinkHalf>, + link: LinkHalf, network: N, ) -> ::client::error::Result> where Block::Hash: Ord, diff --git a/substrate/core/finality-grandpa/src/tests.rs b/substrate/core/finality-grandpa/src/tests.rs index f8ef67f4f7..967686a925 100644 --- a/substrate/core/finality-grandpa/src/tests.rs +++ b/substrate/core/finality-grandpa/src/tests.rs @@ -23,15 +23,30 @@ use network::config::{ProtocolConfig, Roles}; use parking_lot::Mutex; use tokio::runtime::current_thread; use keyring::Keyring; -use client::BlockchainEvents; +use client::{ + BlockchainEvents, runtime_api::{Core, RuntimeVersion, ApiExt, ConstructRuntimeApi, CallApiAt}, + error::Result +}; use test_client::{self, runtime::BlockNumber}; use codec::Decode; use consensus_common::BlockOrigin; -use std::collections::HashSet; +use std::{collections::HashSet, result}; +use runtime_primitives::traits::{ApiRef, ProvideRuntimeApi}; +use runtime_primitives::generic::BlockId; use authorities::AuthoritySet; -type PeerData = Mutex>>; +type PeerData = + Mutex< + Option< + LinkHalf< + test_client::Backend, + test_client::Executor, + Block, + test_client::runtime::ClientWithApi, + > + > + >; type GrandpaPeer = Peer; struct GrandpaTestNet { @@ -86,7 +101,10 @@ impl TestNetFactory for GrandpaTestNet { fn make_block_import(&self, client: Arc) -> (Arc + Send + Sync>, PeerData) { - let (import, link) = block_import(client, self.test_config.clone()).expect("Could not create block import for fresh peer."); + let (import, link) = block_import( + client, + Arc::new(self.test_config.clone()) + ).expect("Could not create block import for fresh peer."); (Arc::new(import), Mutex::new(Some(link))) } @@ -181,17 +199,69 @@ impl TestApi { } } -impl GrandpaApi for TestApi { - fn grandpa_authorities(&self, at: &BlockId) -> Result, ClientError> { +struct RuntimeApi { + inner: TestApi, +} + +impl ProvideRuntimeApi for TestApi { + type Api = RuntimeApi; + + fn runtime_api<'a>(&'a self) -> ApiRef<'a, Self::Api> { + RuntimeApi { inner: self.clone() }.into() + } +} + +impl Core for RuntimeApi { + fn version(&self, _: &BlockId) -> Result { + unimplemented!("Not required for testing!") + } + + fn authorities(&self, _: &BlockId) -> Result> { + unimplemented!("Not required for testing!") + } + + fn execute_block(&self, _: &BlockId, _: &Block) -> Result<()> { + unimplemented!("Not required for testing!") + } + + fn initialise_block( + &self, + _: &BlockId, + _: &::Header + ) -> Result<()> { + unimplemented!("Not required for testing!") + } +} + +impl ApiExt for RuntimeApi { + fn map_api_result result::Result, R, E>( + &self, + _: F + ) -> result::Result { + unimplemented!("Not required for testing!") + } +} + +impl ConstructRuntimeApi for RuntimeApi { + fn construct_runtime_api<'a, T: CallApiAt>(_: &'a T) -> ApiRef<'a, Self> { + unimplemented!("Not required for testing!") + } +} + +impl GrandpaApi for RuntimeApi { + fn grandpa_authorities( + &self, + at: &BlockId + ) -> Result> { if at == &BlockId::Number(0) { - Ok(self.genesis_authorities.clone()) + Ok(self.inner.genesis_authorities.clone()) } else { panic!("should generally only request genesis authorities") } } - fn grandpa_pending_change(&self, at: &BlockId, _digest: DigestFor) - -> Result>>, ClientError> + fn grandpa_pending_change(&self, at: &BlockId, _: &DigestFor) + -> Result>>> { let parent_hash = match at { &BlockId::Hash(at) => at, @@ -200,7 +270,7 @@ impl GrandpaApi for TestApi { // we take only scheduled changes at given block number where there are no // extrinsics. - Ok(self.scheduled_changes.lock().get(&parent_hash).map(|c| c.clone())) + Ok(self.inner.scheduled_changes.lock().get(&parent_hash).map(|c| c.clone())) } } diff --git a/substrate/core/test-client/src/block_builder_ext.rs b/substrate/core/test-client/src/block_builder_ext.rs index 81e4bf756c..3c334b07a1 100644 --- a/substrate/core/test-client/src/block_builder_ext.rs +++ b/substrate/core/test-client/src/block_builder_ext.rs @@ -22,7 +22,6 @@ use keyring; use runtime; use runtime_primitives::traits::ProvideRuntimeApi; use client::block_builder::api::BlockBuilder; -use client::runtime_api::Core as CoreAPI; /// Extension trait for test block builder. pub trait BlockBuilderExt { @@ -32,7 +31,7 @@ pub trait BlockBuilderExt { impl<'a, A> BlockBuilderExt for client::block_builder::BlockBuilder<'a, runtime::Block, A> where A: ProvideRuntimeApi + client::blockchain::HeaderBackend + 'a, - A::Api: BlockBuilder + CoreAPI, + A::Api: BlockBuilder { fn push_transfer(&mut self, transfer: runtime::Transfer) -> Result<(), client::error::Error> { self.push(sign_tx(transfer)) diff --git a/substrate/core/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm b/substrate/core/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm index 27b4e0e4ca..d7e8471570 100644 Binary files a/substrate/core/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm and b/substrate/core/test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm differ diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index dca769f4d4..41595889da 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -465,7 +465,7 @@ impl_runtime_apis! { impl GrandpaApi for ClientWithApi { - fn grandpa_pending_change(digest: DigestFor) + fn grandpa_pending_change(_digest: DigestFor) -> Option>> { unimplemented!("Robert, where is the impl?") } diff --git a/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm b/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm index 488086fdbd..9789295b32 100644 Binary files a/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm and b/substrate/node/runtime/wasm/target/wasm32-unknown-unknown/release/node_runtime.compact.wasm differ