diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 32eb742442..993b4bdd6b 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -1804,6 +1804,7 @@ dependencies = [ "srml-treasury 0.1.0", "srml-upgrade-key 0.1.0", "substrate-client 0.1.0", + "substrate-fg-primitives 0.1.0", "substrate-keyring 0.1.0", "substrate-primitives 0.1.0", ] @@ -3186,6 +3187,7 @@ dependencies = [ "parity-codec 2.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "sr-primitives 0.1.0", + "sr-std 0.1.0", "substrate-client 0.1.0", "substrate-primitives 0.1.0", ] diff --git a/substrate/core/finality-grandpa/primitives/Cargo.toml b/substrate/core/finality-grandpa/primitives/Cargo.toml index 2b2711fab8..8f9c6628b8 100644 --- a/substrate/core/finality-grandpa/primitives/Cargo.toml +++ b/substrate/core/finality-grandpa/primitives/Cargo.toml @@ -9,6 +9,7 @@ substrate-primitives = { path = "../../primitives", default-features = false } parity-codec = { version = "2.1", default-features = false } parity-codec-derive = { version = "2.1", default-features = false } sr-primitives = { path = "../../sr-primitives", default-features = false } +sr-std = { path = "../../sr-std", default-features = false } [features] default = ["std"] @@ -18,4 +19,5 @@ std = [ "parity-codec/std", "parity-codec-derive/std", "sr-primitives/std", + "sr-std/std", ] diff --git a/substrate/core/finality-grandpa/primitives/src/lib.rs b/substrate/core/finality-grandpa/primitives/src/lib.rs index ed8ae408b7..aaca423ecd 100644 --- a/substrate/core/finality-grandpa/primitives/src/lib.rs +++ b/substrate/core/finality-grandpa/primitives/src/lib.rs @@ -28,8 +28,11 @@ extern crate parity_codec_derive; #[macro_use] extern crate substrate_client as client; +extern crate sr_std as rstd; + use substrate_primitives::AuthorityId; use sr_primitives::traits::{Block as BlockT, DigestFor, NumberFor}; +use rstd::vec::Vec; /// A scheduled change of authority set. #[cfg_attr(feature = "std", derive(Debug, PartialEq))] @@ -81,55 +84,3 @@ decl_runtime_apis! { fn grandpa_authorities() -> Vec<(AuthorityId, u64)>; } } - -#[cfg(feature = "std")] -mod implementation { - use super::{GrandpaApi, ScheduledChange}; - use sr_primitives::traits::{Block as BlockT, DigestFor, NumberFor}; - use sr_primitives::generic::BlockId; - use parity_codec::{Encode, Decode}; - use client::{Client, error::Error as ClientError, backend::Backend, CallExecutor}; - use client::runtime_api::{CallApiAt, Core as CoreAPI}; - use substrate_primitives::{AuthorityId, H256, Blake2Hasher}; - - // TODO [basti]: do this implementation in runtime. - impl, RA> GrandpaApi for Client where - B: Backend + 'static, - E: CallExecutor + 'static + Clone + Send + Sync, - DigestFor: Encode, - RA: CoreAPI, - { - fn grandpa_authorities(&self, at: &BlockId) -> Result, ClientError> { - let raw = self.call_api_at( - &at, - ::AUTHORITIES_CALL, - Encode::encode(&()), - &mut Default::default(), - &mut None, - ); - - // TODO [basti]: implement this in runtime with macro. - match Decode::decode(&mut &raw[..]) { - Some(x) => Ok(x), - None => Err(::client::error::ErrorKind::CallResultDecode(::AUTHORITIES_CALL).into()), - } - } - - fn grandpa_pending_change(&self, at: &BlockId, digest: DigestFor) - -> Result>>, ClientError> - { - let raw = self.call_api_at( - at, - ::PENDING_CHANGE_CALL, - digest.encode(), - &mut Default::default(), - &mut None, - ); - - match Decode::decode(&mut &raw[..]) { - Some(x) => Ok(x), - None => Err(::client::error::ErrorKind::CallResultDecode(::PENDING_CHANGE_CALL).into()), - } - } - } -} diff --git a/substrate/core/finality-grandpa/src/lib.rs b/substrate/core/finality-grandpa/src/lib.rs index 1a0f10334a..b107c3089e 100644 --- a/substrate/core/finality-grandpa/src/lib.rs +++ b/substrate/core/finality-grandpa/src/lib.rs @@ -84,11 +84,11 @@ 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::{CallApiAt, TaggedTransactionQueue, Core as CoreAPI}; +use client::runtime_api::TaggedTransactionQueue; use codec::{Encode, Decode}; use consensus_common::{BlockImport, ImportBlock, ImportResult}; use runtime_primitives::traits::{ - NumberFor, Block as BlockT, Header as HeaderT, DigestFor, + NumberFor, Block as BlockT, Header as HeaderT, DigestFor, ProvideRuntimeApi }; use fg_primitives::GrandpaApi; use runtime_primitives::generic::BlockId; @@ -773,18 +773,16 @@ impl, N, RA> voter::Environment, Api, RA> { +pub struct GrandpaBlockImport, RA> { inner: Arc>, authority_set: SharedAuthoritySet>, - api_client: Api, } -impl, Api, RA> BlockImport for GrandpaBlockImport where +impl, RA> BlockImport for GrandpaBlockImport where B: Backend + 'static, E: CallExecutor + 'static + Clone + Send + Sync, DigestFor: Encode, - Api: GrandpaApi, - RA: TaggedTransactionQueue + Send + Sync, // necessary for client to import `BlockImport`. + RA: GrandpaApi + TaggedTransactionQueue, { type Error = ClientError; @@ -793,7 +791,7 @@ impl, Api, RA> BlockImport for GrandpaBloc { use authorities::PendingChange; - let maybe_change = self.api_client.grandpa_pending_change( + let maybe_change = self.inner.runtime_api().grandpa_pending_change( &BlockId::hash(*block.header.parent_hash()), &block.header.digest().clone(), )?; @@ -839,13 +837,12 @@ pub struct LinkHalf, RA> { /// Make block importer and link half necessary to tie the background voter /// to it. -pub fn block_import, Api, RA>(client: Arc>, api_client: Api) - -> Result<(GrandpaBlockImport, LinkHalf), ClientError> +pub fn block_import, RA>(client: Arc>) + -> Result<(GrandpaBlockImport, LinkHalf), ClientError> where - B: Backend + 'static, - E: CallExecutor + 'static, - Api: GrandpaApi, - RA: Send + Sync, + B: Backend + 'static, + E: CallExecutor + 'static + Clone + Send + Sync, + RA: GrandpaApi, { use runtime_primitives::traits::Zero; let authority_set = match client.backend().get_aux(AUTHORITY_SET_KEY)? { @@ -856,7 +853,7 @@ pub fn block_import, Api, RA>(client: Arc, Api, RA>(client: Arc for ClientWithApi { } } +#[cfg(feature = "std")] +impl substrate_fg_primitives::GrandpaApi for ClientWithApi { + fn grandpa_pending_change(&self, at: &GBlockId, digest: &DigestFor) + -> Result>>, client::error::Error> { + self.call_api_at(at, "grandpa_pending_change", digest) + } + + fn grandpa_authorities(&self, at: &GBlockId) + -> Result, client::error::Error> { + self.call_api_at(at, "grandpa_authorities", &()) + } +} + impl_runtime_apis! { impl Core for Runtime { fn version() -> RuntimeVersion { @@ -447,4 +462,16 @@ impl_runtime_apis! { Executive::validate_transaction(tx) } } + + + impl GrandpaApi for ClientWithApi { + fn grandpa_pending_change(digest: DigestFor) + -> Option>> { + unimplemented!("Robert, where is the impl?") + } + + fn grandpa_authorities() -> Vec<(SessionKey, u64)> { + unimplemented!("Robert, where is the impl?") + } + } } diff --git a/substrate/node/runtime/wasm/Cargo.lock b/substrate/node/runtime/wasm/Cargo.lock index bb067bc400..4eeae1fd9d 100644 --- a/substrate/node/runtime/wasm/Cargo.lock +++ b/substrate/node/runtime/wasm/Cargo.lock @@ -526,6 +526,7 @@ dependencies = [ "srml-treasury 0.1.0", "srml-upgrade-key 0.1.0", "substrate-client 0.1.0", + "substrate-fg-primitives 0.1.0", "substrate-primitives 0.1.0", ] @@ -1272,6 +1273,18 @@ dependencies = [ "wasmi 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "substrate-fg-primitives" +version = "0.1.0" +dependencies = [ + "parity-codec 2.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sr-primitives 0.1.0", + "sr-std 0.1.0", + "substrate-client 0.1.0", + "substrate-primitives 0.1.0", +] + [[package]] name = "substrate-keyring" version = "0.1.0" diff --git a/substrate/node/runtime/wasm/Cargo.toml b/substrate/node/runtime/wasm/Cargo.toml index 9e74ff9d5d..6e9cfd0910 100644 --- a/substrate/node/runtime/wasm/Cargo.toml +++ b/substrate/node/runtime/wasm/Cargo.toml @@ -12,6 +12,7 @@ safe-mix = { version = "1.0", default-features = false } parity-codec-derive = { version = "2.1" } parity-codec = { version = "2.1", default-features = false } substrate-primitives = { path = "../../../core/primitives", default-features = false } +substrate-fg-primitives = { path = "../../../core/finality-grandpa/primitives", default-features = false } substrate-client = { path = "../../../core/client", default-features = false } sr-std = { path = "../../../core/sr-std", default-features = false } srml-support = { path = "../../../srml/support", default-features = false } @@ -38,6 +39,7 @@ std = [ "parity-codec/std", "substrate-primitives/std", "substrate-client/std", + "substrate-fg-primitives/std", "sr-std/std", "srml-support/std", "srml-balances/std", 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 766de4bda3..8a89da66f1 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