removes use of sc_client::Client from sc-rpc (#5063)

* removes use of sc_client::Client from sc-rpc

* remove Client impl from sc-finality-benches

* remove client impl from sc-finality-grandpa

* read_proof accepts iterator

* remove generic Executor param from ExecutorProvider

* fix long ass line

* code style changes

* merge with master

Co-authored-by: Arkadiy Paronyan <arkady.paronyan@gmail.com>
This commit is contained in:
Benjamin Kampmann
2020-03-05 16:41:10 +01:00
committed by GitHub
parent dc85ccb7df
commit 99ae5342eb
34 changed files with 739 additions and 544 deletions
+14 -12
View File
@@ -20,37 +20,39 @@ use std::sync::Arc;
use rpc::futures::future::result;
use sc_rpc_api::Subscriptions;
use sc_client_api::{CallExecutor, backend::Backend};
use sc_client::Client;
use sc_client_api::{BlockchainEvents, BlockBody};
use sp_runtime::{generic::{BlockId, SignedBlock}, traits::{Block as BlockT}};
use super::{ChainBackend, client_err, error::FutureResult};
use std::marker::PhantomData;
use sp_blockchain::HeaderBackend;
/// Blockchain API backend for full nodes. Reads all the data from local database.
pub struct FullChain<B, E, Block: BlockT, RA> {
pub struct FullChain<Block: BlockT, Client> {
/// Substrate client.
client: Arc<Client<B, E, Block, RA>>,
client: Arc<Client>,
/// Current subscriptions.
subscriptions: Subscriptions,
/// phantom member to pin the block type
_phantom: PhantomData<Block>,
}
impl<B, E, Block: BlockT, RA> FullChain<B, E, Block, RA> {
impl<Block: BlockT, Client> FullChain<Block, Client> {
/// Create new Chain API RPC handler.
pub fn new(client: Arc<Client<B, E, Block, RA>>, subscriptions: Subscriptions) -> Self {
pub fn new(client: Arc<Client>, subscriptions: Subscriptions) -> Self {
Self {
client,
subscriptions,
_phantom: PhantomData,
}
}
}
impl<B, E, Block, RA> ChainBackend<B, E, Block, RA> for FullChain<B, E, Block, RA> where
impl<Block, Client> ChainBackend<Client, Block> for FullChain<Block, Client> where
Block: BlockT + 'static,
B: Backend<Block> + Send + Sync + 'static,
E: CallExecutor<Block> + Send + Sync + 'static,
RA: Send + Sync + 'static,
Client: BlockBody<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
fn client(&self) -> &Arc<Client<B, E, Block, RA>> {
fn client(&self) -> &Arc<Client> {
&self.client
}
@@ -60,7 +62,7 @@ impl<B, E, Block, RA> ChainBackend<B, E, Block, RA> for FullChain<B, E, Block, R
fn header(&self, hash: Option<Block::Hash>) -> FutureResult<Option<Block::Header>> {
Box::new(result(self.client
.header(&BlockId::Hash(self.unwrap_or_best(hash)))
.header(BlockId::Hash(self.unwrap_or_best(hash)))
.map_err(client_err)
))
}
+10 -10
View File
@@ -22,7 +22,7 @@ use rpc::futures::future::{result, Future, Either};
use sc_rpc_api::Subscriptions;
use sc_client::{
Client, light::{fetcher::{Fetcher, RemoteBodyRequest}, blockchain::RemoteBlockchain},
light::{fetcher::{Fetcher, RemoteBodyRequest}, blockchain::RemoteBlockchain},
};
use sp_runtime::{
generic::{BlockId, SignedBlock},
@@ -30,12 +30,14 @@ use sp_runtime::{
};
use super::{ChainBackend, client_err, error::FutureResult};
use sp_blockchain::HeaderBackend;
use sc_client_api::BlockchainEvents;
/// Blockchain API backend for light nodes. Reads all the data from local
/// database, if available, or fetches it from remote node otherwise.
pub struct LightChain<B, E, Block: BlockT, RA, F> {
pub struct LightChain<Block: BlockT, Client, F> {
/// Substrate client.
client: Arc<Client<B, E, Block, RA>>,
client: Arc<Client>,
/// Current subscriptions.
subscriptions: Subscriptions,
/// Remote blockchain reference
@@ -44,10 +46,10 @@ pub struct LightChain<B, E, Block: BlockT, RA, F> {
fetcher: Arc<F>,
}
impl<B, E, Block: BlockT, RA, F: Fetcher<Block>> LightChain<B, E, Block, RA, F> {
impl<Block: BlockT, Client, F: Fetcher<Block>> LightChain<Block, Client, F> {
/// Create new Chain API RPC handler.
pub fn new(
client: Arc<Client<B, E, Block, RA>>,
client: Arc<Client>,
subscriptions: Subscriptions,
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
@@ -61,14 +63,12 @@ impl<B, E, Block: BlockT, RA, F: Fetcher<Block>> LightChain<B, E, Block, RA, F>
}
}
impl<B, E, Block, RA, F> ChainBackend<B, E, Block, RA> for LightChain<B, E, Block, RA, F> where
impl<Block, Client, F> ChainBackend<Client, Block> for LightChain<Block, Client, F> where
Block: BlockT + 'static,
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
E: sc_client::CallExecutor<Block> + Send + Sync + 'static,
RA: Send + Sync + 'static,
Client: BlockchainEvents<Block> + HeaderBackend<Block> + Send + Sync + 'static,
F: Fetcher<Block> + Send + Sync + 'static,
{
fn client(&self) -> &Arc<Client<B, E, Block, RA>> {
fn client(&self) -> &Arc<Client> {
&self.client
}
+32 -36
View File
@@ -32,7 +32,7 @@ use rpc::{
use sc_rpc_api::Subscriptions;
use sc_client::{
self, Client, BlockchainEvents,
self, BlockchainEvents,
light::{fetcher::Fetcher, blockchain::RemoteBlockchain},
};
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
@@ -45,16 +45,17 @@ use sp_runtime::{
use self::error::{Result, Error, FutureResult};
pub use sc_rpc_api::chain::*;
use sp_blockchain::HeaderBackend;
use sc_client_api::BlockBody;
/// Blockchain backend API
trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
where
Block: BlockT + 'static,
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
E: sc_client::CallExecutor<Block> + Send + Sync + 'static,
Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
/// Get client reference.
fn client(&self) -> &Arc<Client<B, E, Block, RA>>;
fn client(&self) -> &Arc<Client>;
/// Get subscriptions reference.
fn subscriptions(&self) -> &Subscriptions;
@@ -62,7 +63,7 @@ trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
/// Tries to unwrap passed block hash, or uses best block hash otherwise.
fn unwrap_or_best(&self, hash: Option<Block::Hash>) -> Block::Hash {
match hash.into() {
None => self.client().chain_info().best_hash,
None => self.client().info().best_hash,
Some(hash) => hash,
}
}
@@ -81,9 +82,9 @@ trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
number: Option<NumberOrHex<NumberFor<Block>>>,
) -> Result<Option<Block::Hash>> {
Ok(match number {
None => Some(self.client().chain_info().best_hash),
None => Some(self.client().info().best_hash),
Some(num_or_hex) => self.client()
.header(&BlockId::number(num_or_hex.to_number()?))
.header(BlockId::number(num_or_hex.to_number()?))
.map_err(client_err)?
.map(|h| h.hash()),
})
@@ -91,7 +92,7 @@ trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
/// Get hash of the last finalized block in the canon chain.
fn finalized_head(&self) -> Result<Block::Hash> {
Ok(self.client().chain_info().finalized_hash)
Ok(self.client().info().finalized_hash)
}
/// All new head subscription
@@ -104,7 +105,7 @@ trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
self.client(),
self.subscriptions(),
subscriber,
|| self.client().chain_info().best_hash,
|| self.client().info().best_hash,
|| self.client().import_notification_stream()
.map(|notification| Ok::<_, ()>(notification.header))
.compat(),
@@ -130,7 +131,7 @@ trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
self.client(),
self.subscriptions(),
subscriber,
|| self.client().chain_info().best_hash,
|| self.client().info().best_hash,
|| self.client().import_notification_stream()
.filter(|notification| future::ready(notification.is_new_best))
.map(|notification| Ok::<_, ()>(notification.header))
@@ -157,7 +158,7 @@ trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
self.client(),
self.subscriptions(),
subscriber,
|| self.client().chain_info().finalized_hash,
|| self.client().info().finalized_hash,
|| self.client().finality_notification_stream()
.map(|notification| Ok::<_, ()>(notification.header))
.compat(),
@@ -175,15 +176,13 @@ trait ChainBackend<B, E, Block: BlockT, RA>: Send + Sync + 'static
}
/// Create new state API that works on full node.
pub fn new_full<B, E, Block: BlockT, RA>(
client: Arc<Client<B, E, Block, RA>>,
pub fn new_full<Block: BlockT, Client>(
client: Arc<Client>,
subscriptions: Subscriptions,
) -> Chain<B, E, Block, RA>
) -> Chain<Block, Client>
where
Block: BlockT + 'static,
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
E: sc_client::CallExecutor<Block> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
Client: BlockBody<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
Chain {
backend: Box::new(self::chain_full::FullChain::new(client, subscriptions)),
@@ -191,17 +190,15 @@ pub fn new_full<B, E, Block: BlockT, RA>(
}
/// Create new state API that works on light node.
pub fn new_light<B, E, Block: BlockT, RA, F: Fetcher<Block>>(
client: Arc<Client<B, E, Block, RA>>,
pub fn new_light<Block: BlockT, Client, F: Fetcher<Block>>(
client: Arc<Client>,
subscriptions: Subscriptions,
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
) -> Chain<B, E, Block, RA>
) -> Chain<Block, Client>
where
Block: BlockT + 'static,
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
E: sc_client::CallExecutor<Block> + Send + Sync + 'static + Clone,
RA: Send + Sync + 'static,
Client: BlockBody<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
F: Send + Sync + 'static,
{
Chain {
@@ -215,15 +212,15 @@ pub fn new_light<B, E, Block: BlockT, RA, F: Fetcher<Block>>(
}
/// Chain API with subscriptions support.
pub struct Chain<B, E, Block: BlockT, RA> {
backend: Box<dyn ChainBackend<B, E, Block, RA>>,
pub struct Chain<Block: BlockT, Client> {
backend: Box<dyn ChainBackend<Client, Block>>,
}
impl<B, E, Block, RA> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, SignedBlock<Block>> for Chain<B, E, Block, RA> where
Block: BlockT + 'static,
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
E: sc_client::CallExecutor<Block> + Send + Sync + 'static,
RA: Send + Sync + 'static
impl<Block, Client> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, SignedBlock<Block>> for
Chain<Block, Client>
where
Block: BlockT + 'static,
Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
type Metadata = crate::metadata::Metadata;
@@ -281,16 +278,15 @@ impl<B, E, Block, RA> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Sig
}
/// Subscribe to new headers.
fn subscribe_headers<B, E, Block, RA, F, G, S, ERR>(
client: &Arc<Client<B, E, Block, RA>>,
fn subscribe_headers<Block, Client, F, G, S, ERR>(
client: &Arc<Client>,
subscriptions: &Subscriptions,
subscriber: Subscriber<Block::Header>,
best_block_hash: G,
stream: F,
) where
Block: BlockT + 'static,
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
E: sc_client::CallExecutor<Block> + Send + Sync + 'static,
Client: HeaderBackend<Block> + 'static,
F: FnOnce() -> S,
G: FnOnce() -> Block::Hash,
ERR: ::std::fmt::Debug,
@@ -298,7 +294,7 @@ fn subscribe_headers<B, E, Block, RA, F, G, S, ERR>(
{
subscriptions.add(subscriber, |sink| {
// send current head right at the start.
let header = client.header(&BlockId::Hash(best_block_hash()))
let header = client.header(BlockId::Hash(best_block_hash()))
.map_err(client_err)
.and_then(|header| {
header.ok_or_else(|| "Best header missing.".to_owned().into())