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
+31 -35
View File
@@ -16,31 +16,19 @@
//! Chain api required for the transaction pool.
use std::{
marker::PhantomData,
pin::Pin,
sync::Arc,
};
use client::{runtime_api::TaggedTransactionQueue, blockchain::HeaderBackend};
use codec::Encode;
use futures::{
channel::oneshot,
executor::{ThreadPool, ThreadPoolBuilder},
future::Future,
};
use txpool;
use primitives::{
H256,
Blake2Hasher,
Hasher,
};
use sr_primitives::{
generic::BlockId,
traits,
transaction_validity::TransactionValidity,
};
use std::{marker::PhantomData, pin::Pin, sync::Arc};
use crate::error;
use codec::Encode;
use futures::{channel::oneshot, executor::{ThreadPool, ThreadPoolBuilder}, future::Future};
use primitives::{H256, Blake2Hasher, Hasher};
use sr_primitives::{generic::BlockId, traits, transaction_validity::TransactionValidity};
use tx_runtime_api::TaggedTransactionQueue;
use crate::error::{self, Error};
/// The transaction pool logic
pub struct FullChainApi<T, Block> {
@@ -51,7 +39,7 @@ pub struct FullChainApi<T, Block> {
impl<T, Block> FullChainApi<T, Block> where
Block: traits::Block,
T: traits::ProvideRuntimeApi + HeaderBackend<Block> {
T: traits::ProvideRuntimeApi + traits::BlockIdTo<Block> {
/// Create new transaction pool logic.
pub fn new(client: Arc<T>) -> Self {
FullChainApi {
@@ -67,14 +55,15 @@ impl<T, Block> FullChainApi<T, Block> where
}
impl<T, Block> txpool::ChainApi for FullChainApi<T, Block> where
Block: traits::Block<Hash=H256>,
T: traits::ProvideRuntimeApi + HeaderBackend<Block> + 'static,
T::Api: TaggedTransactionQueue<Block>
Block: traits::Block<Hash = H256>,
T: traits::ProvideRuntimeApi + traits::BlockIdTo<Block> + 'static + Send + Sync,
T::Api: TaggedTransactionQueue<Block>,
sr_api::ApiErrorFor<T, Block>: Send,
{
type Block = Block;
type Hash = H256;
type Error = error::Error;
type ValidationFuture = Pin<Box<dyn Future<Output=error::Result<TransactionValidity>> + Send>>;
type ValidationFuture = Pin<Box<dyn Future<Output = error::Result<TransactionValidity>> + Send>>;
fn validate_transaction(
&self,
@@ -86,7 +75,8 @@ impl<T, Block> txpool::ChainApi for FullChainApi<T, Block> where
let at = at.clone();
self.pool.spawn_ok(async move {
let res = client.runtime_api().validate_transaction(&at, uxt).map_err(Into::into);
let res = client.runtime_api().validate_transaction(&at, uxt)
.map_err(|e| Error::RuntimeApi(format!("{:?}", e)));
if let Err(e) = tx.send(res) {
log::warn!("Unable to send a validate transaction result: {:?}", e);
}
@@ -95,17 +85,23 @@ impl<T, Block> txpool::ChainApi for FullChainApi<T, Block> where
Box::pin(async move {
match rx.await {
Ok(r) => r,
Err(e) => Err(client::error::Error::Msg(format!("{}", e)))?,
Err(_) => Err(Error::RuntimeApi("Validation was canceled".into())),
}
})
}
fn block_id_to_number(&self, at: &BlockId<Self::Block>) -> error::Result<Option<txpool::NumberFor<Self>>> {
Ok(self.client.block_number_from_id(at)?)
fn block_id_to_number(
&self,
at: &BlockId<Self::Block>,
) -> error::Result<Option<txpool::NumberFor<Self>>> {
self.client.to_number(at).map_err(|e| Error::BlockIdConversion(format!("{:?}", e)))
}
fn block_id_to_hash(&self, at: &BlockId<Self::Block>) -> error::Result<Option<txpool::BlockHash<Self>>> {
Ok(self.client.block_hash_from_id(at)?)
fn block_id_to_hash(
&self,
at: &BlockId<Self::Block>,
) -> error::Result<Option<txpool::BlockHash<Self>>> {
self.client.to_hash(at).map_err(|e| Error::BlockIdConversion(format!("{:?}", e)))
}
fn hash_and_length(&self, ex: &txpool::ExtrinsicFor<Self>) -> (Self::Hash, usize) {