Refactor sr-api to not depend on client anymore (#4086)

* Refactor sr-api to not depend on client anymore

* Fix benches

* Apply suggestions from code review

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Apply suggestions from code review
This commit is contained in:
Bastian Köcher
2019-11-11 16:26:49 +01:00
committed by Benjamin Kampmann
parent e26d1a0b3e
commit 2ecffa1cd0
140 changed files with 1514 additions and 984 deletions
+1
View File
@@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
api = { package = "substrate-rpc-api", path = "./api" }
client = { package = "substrate-client", path = "../client" }
sr-api = { path = "../sr-api" }
codec = { package = "parity-scale-codec", version = "1.0.0" }
futures03 = { package = "futures-preview", version = "0.3.0-alpha.19", features = ["compat"] }
jsonrpc-pubsub = "14.0.3"
+8 -5
View File
@@ -23,7 +23,8 @@ use std::{sync::Arc, convert::TryInto};
use futures03::future::{FutureExt, TryFutureExt};
use log::warn;
use client::{self, Client};
use client::{Client, error::Error as ClientError};
use rpc::futures::{
Sink, Future,
future::result,
@@ -87,7 +88,8 @@ impl<B, E, P, RA> AuthorApi<ExHash<P>, BlockHash<P>> for Author<B, E, P, RA> whe
P::Error: 'static,
RA: Send + Sync + 'static,
Client<B, E, P::Block, RA>: ProvideRuntimeApi,
<Client<B, E, P::Block, RA> as ProvideRuntimeApi>::Api: SessionKeys<P::Block>,
<Client<B, E, P::Block, RA> as ProvideRuntimeApi>::Api:
SessionKeys<P::Block, Error = ClientError>,
{
type Metadata = crate::metadata::Metadata;
@@ -131,8 +133,9 @@ impl<B, E, P, RA> AuthorApi<ExHash<P>, BlockHash<P>> for Author<B, E, P, RA> whe
Ok(self.pool.ready().map(|tx| tx.data.encode().into()).collect())
}
fn remove_extrinsic(&self,
bytes_or_hash: Vec<hash::ExtrinsicOrHash<ExHash<P>>>
fn remove_extrinsic(
&self,
bytes_or_hash: Vec<hash::ExtrinsicOrHash<ExHash<P>>>,
) -> Result<Vec<ExHash<P>>> {
let hashes = bytes_or_hash.into_iter()
.map(|x| match x {
@@ -155,7 +158,7 @@ impl<B, E, P, RA> AuthorApi<ExHash<P>, BlockHash<P>> for Author<B, E, P, RA> whe
fn watch_extrinsic(&self,
_metadata: Self::Metadata,
subscriber: Subscriber<Status<ExHash<P>, BlockHash<P>>>,
xt: Bytes
xt: Bytes,
) {
let submit = || -> Result<_> {
let best_block_hash = self.client.info().chain.best_hash;
+6 -10
View File
@@ -24,17 +24,10 @@ mod tests;
use std::sync::Arc;
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use rpc::{
Result as RpcResult,
futures::Future,
};
use rpc::{Result as RpcResult, futures::Future};
use api::Subscriptions;
use client::{
Client, CallExecutor,
runtime_api::Metadata,
light::{blockchain::RemoteBlockchain, fetcher::Fetcher},
};
use client::{Client, CallExecutor, light::{blockchain::RemoteBlockchain, fetcher::Fetcher}};
use primitives::{
Blake2Hasher, Bytes, H256,
storage::{StorageKey, StorageData, StorageChangeSet},
@@ -44,6 +37,8 @@ use sr_primitives::{
traits::{Block as BlockT, ProvideRuntimeApi},
};
use sr_api::Metadata;
use self::error::{Error, FutureResult};
pub use api::state::*;
@@ -188,7 +183,8 @@ pub fn new_full<B, E, Block: BlockT, RA>(
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
Client<B, E, Block, RA>: ProvideRuntimeApi,
<Client<B, E, Block, RA> as ProvideRuntimeApi>::Api: Metadata<Block>,
<Client<B, E, Block, RA> as ProvideRuntimeApi>::Api:
Metadata<Block, Error = client::error::Error>,
{
State {
backend: Box::new(self::state_full::FullState::new(client, subscriptions)),
+8 -4
View File
@@ -29,8 +29,7 @@ use rpc::{
use api::Subscriptions;
use client::{
Client, CallExecutor, BlockchainEvents, runtime_api::Metadata,
backend::Backend, error::Result as ClientResult,
Client, CallExecutor, BlockchainEvents, backend::Backend, error::Result as ClientResult,
};
use primitives::{
H256, Blake2Hasher, Bytes, storage::{well_known_keys, StorageKey, StorageData, StorageChangeSet},
@@ -42,6 +41,8 @@ use sr_primitives::{
traits::{Block as BlockT, Header, NumberFor, ProvideRuntimeApi, SaturatedConversion},
};
use sr_api::Metadata;
use super::{StateBackend, error::{FutureResult, Error, Result}, client_err};
/// Ranges to query in state_queryStorage.
@@ -229,7 +230,8 @@ impl<B, E, Block, RA> StateBackend<B, E, Block, RA> for FullState<B, E, Block, R
E: CallExecutor<Block, Blake2Hasher> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
Client<B, E, Block, RA>: ProvideRuntimeApi,
<Client<B, E, Block, RA> as ProvideRuntimeApi>::Api: Metadata<Block>,
<Client<B, E, Block, RA> as ProvideRuntimeApi>::Api:
Metadata<Block, Error = client::error::Error>,
{
fn call(
&self,
@@ -326,7 +328,9 @@ impl<B, E, Block, RA> StateBackend<B, E, Block, RA> for FullState<B, E, Block, R
fn metadata(&self, block: Option<Block::Hash>) -> FutureResult<Bytes> {
Box::new(result(
self.block_or_best(block)
.and_then(|block| self.client.runtime_api().metadata(&BlockId::Hash(block)).map(Into::into))
.and_then(|block|
self.client.runtime_api().metadata(&BlockId::Hash(block)).map(Into::into)
)
.map_err(client_err)))
}