Fixes GrandpaApi implementation and usage

This commit is contained in:
Bastian Köcher
2018-11-15 17:34:55 +01:00
parent 1cb0bbc48b
commit 3cd2739f66
10 changed files with 67 additions and 71 deletions
@@ -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",
]
@@ -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<B, E, Block: BlockT<Hash=H256>, RA> GrandpaApi<Block> for Client<B, E, Block, RA> where
B: Backend<Block, Blake2Hasher> + 'static,
E: CallExecutor<Block, Blake2Hasher> + 'static + Clone + Send + Sync,
DigestFor<Block>: Encode,
RA: CoreAPI<Block>,
{
fn grandpa_authorities(&self, at: &BlockId<Block>) -> Result<Vec<(AuthorityId, u64)>, 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<Block>, digest: DigestFor<Block>)
-> Result<Option<ScheduledChange<NumberFor<Block>>>, 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()),
}
}
}
}
+13 -16
View File
@@ -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<B, E, Block: BlockT<Hash=H256>, N, RA> voter::Environment<Block::Hash, Numb
/// This scans each imported block for signals of changing authority set.
/// When using GRANDPA, the block import worker should be using this block import
/// object.
pub struct GrandpaBlockImport<B, E, Block: BlockT<Hash=H256>, Api, RA> {
pub struct GrandpaBlockImport<B, E, Block: BlockT<Hash=H256>, RA> {
inner: Arc<Client<B, E, Block, RA>>,
authority_set: SharedAuthoritySet<Block::Hash, NumberFor<Block>>,
api_client: Api,
}
impl<B, E, Block: BlockT<Hash=H256>, Api, RA> BlockImport<Block> for GrandpaBlockImport<B, E, Block, Api, RA> where
impl<B, E, Block: BlockT<Hash=H256>, RA> BlockImport<Block> for GrandpaBlockImport<B, E, Block, RA> where
B: Backend<Block, Blake2Hasher> + 'static,
E: CallExecutor<Block, Blake2Hasher> + 'static + Clone + Send + Sync,
DigestFor<Block>: Encode,
Api: GrandpaApi<Block>,
RA: TaggedTransactionQueue<Block> + Send + Sync, // necessary for client to import `BlockImport`.
RA: GrandpaApi<Block> + TaggedTransactionQueue<Block>,
{
type Error = ClientError;
@@ -793,7 +791,7 @@ impl<B, E, Block: BlockT<Hash=H256>, Api, RA> BlockImport<Block> 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<B, E, Block: BlockT<Hash=H256>, RA> {
/// Make block importer and link half necessary to tie the background voter
/// to it.
pub fn block_import<B, E, Block: BlockT<Hash=H256>, Api, RA>(client: Arc<Client<B, E, Block, RA>>, api_client: Api)
-> Result<(GrandpaBlockImport<B, E, Block, Api, RA>, LinkHalf<B, E, Block, RA>), ClientError>
pub fn block_import<B, E, Block: BlockT<Hash=H256>, RA>(client: Arc<Client<B, E, Block, RA>>)
-> Result<(GrandpaBlockImport<B, E, Block, RA>, LinkHalf<B, E, Block, RA>), ClientError>
where
B: Backend<Block, Blake2Hasher> + 'static,
E: CallExecutor<Block, Blake2Hasher> + 'static,
Api: GrandpaApi<Block>,
RA: Send + Sync,
B: Backend<Block, Blake2Hasher> + 'static,
E: CallExecutor<Block, Blake2Hasher> + 'static + Clone + Send + Sync,
RA: GrandpaApi<Block>,
{
use runtime_primitives::traits::Zero;
let authority_set = match client.backend().get_aux(AUTHORITY_SET_KEY)? {
@@ -856,7 +853,7 @@ pub fn block_import<B, E, Block: BlockT<Hash=H256>, Api, RA>(client: Arc<Client<
// 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 = api_client
let genesis_authorities = client.runtime_api()
.grandpa_authorities(&BlockId::number(Zero::zero()))?;
let authority_set = SharedAuthoritySet::genesis(genesis_authorities);
@@ -873,7 +870,7 @@ pub fn block_import<B, E, Block: BlockT<Hash=H256>, Api, RA>(client: Arc<Client<
};
Ok((
GrandpaBlockImport { inner: client.clone(), authority_set: authority_set.clone(), api_client },
GrandpaBlockImport { inner: client.clone(), authority_set: authority_set.clone() },
LinkHalf { client, authority_set },
))
}