Recover transaction pool on light client (#3833)

* recover tx pool on light client

* revert local tests fix

* removed import renamings

* futures03::Future -> std::future::Future

* Update core/transaction-pool/graph/src/error.rs

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

* replace remove_from_ready with remove_invalid

* avoid excess hashing

* debug -> warn

* TransactionPool + BasicTransactionPool

* pause future tx reject when resubmitting

* bump impl_version to make CI happy

* and revert back local test fixes

* alter doc to restart CI

* Transaction::clone() -> Transaction::duplicate()

* transactions -> updated_tranasctions

* remove explicit consensus-common ref

* ::std:: -> std::

* manual set/unset flag -> calling clusore with given flag value

* removed comments

* removed force argument

* BestIterator -> Box<Iterator>

* separate crate for TxPool + Maintainer trait

* long line fix

* pos-merge fix

* fix benches compilation

* Rename txpoolapi to txpool_api

* Clean up.

* Finalize merge.

* post-merge fix

* Move transaction pool api to primitives directly.

* Consistent naming for txpool-runtime-api

* Warn about missing docs.

* Move  abstraction for offchain calls to tx-pool-api.

* Merge RPC instantiation.

* Update cargo.lock

* Post merge fixes.

* Avoid depending on client.

* Fix build
This commit is contained in:
Svyatoslav Nikolsky
2019-11-28 03:00:54 +03:00
committed by Gavin Wood
parent 3e26fceda4
commit a782021ee8
64 changed files with 2370 additions and 667 deletions
+92 -11
View File
@@ -17,20 +17,20 @@
//! Chain api required for the transaction pool.
use std::{marker::PhantomData, pin::Pin, sync::Arc};
use codec::{Decode, Encode};
use futures::{channel::oneshot, executor::{ThreadPool, ThreadPoolBuilder}, future::{Future, FutureExt, ready}};
use codec::Encode;
use futures::{channel::oneshot, executor::{ThreadPool, ThreadPoolBuilder}, future::Future};
use client_api::{
blockchain::HeaderBackend,
light::{Fetcher, RemoteCallRequest}
};
use primitives::{H256, Blake2Hasher, Hasher};
use sr_primitives::{generic::BlockId, traits, transaction_validity::TransactionValidity};
use tx_runtime_api::TaggedTransactionQueue;
use sr_primitives::{generic::BlockId, traits::{self, Block as BlockT}, transaction_validity::TransactionValidity};
use txpool_runtime_api::TaggedTransactionQueue;
use crate::error::{self, Error};
/// The transaction pool logic
/// The transaction pool logic for full client.
pub struct FullChainApi<T, Block> {
client: Arc<T>,
pool: ThreadPool,
@@ -38,7 +38,7 @@ pub struct FullChainApi<T, Block> {
}
impl<T, Block> FullChainApi<T, Block> where
Block: traits::Block,
Block: BlockT,
T: traits::ProvideRuntimeApi + traits::BlockIdTo<Block> {
/// Create new transaction pool logic.
pub fn new(client: Arc<T>) -> Self {
@@ -55,7 +55,7 @@ impl<T, Block> FullChainApi<T, Block> where
}
impl<T, Block> txpool::ChainApi for FullChainApi<T, Block> where
Block: traits::Block<Hash = H256>,
Block: BlockT<Hash = H256>,
T: traits::ProvideRuntimeApi + traits::BlockIdTo<Block> + 'static + Send + Sync,
T::Api: TaggedTransactionQueue<Block>,
sr_api::ApiErrorFor<T, Block>: Send,
@@ -110,3 +110,84 @@ impl<T, Block> txpool::ChainApi for FullChainApi<T, Block> where
})
}
}
/// The transaction pool logic for light client.
pub struct LightChainApi<T, F, Block> {
client: Arc<T>,
fetcher: Arc<F>,
_phantom: PhantomData<Block>,
}
impl<T, F, Block> LightChainApi<T, F, Block> where
Block: BlockT,
T: HeaderBackend<Block>,
F: Fetcher<Block>,
{
/// Create new transaction pool logic.
pub fn new(client: Arc<T>, fetcher: Arc<F>) -> Self {
LightChainApi {
client,
fetcher,
_phantom: Default::default(),
}
}
}
impl<T, F, Block> txpool::ChainApi for LightChainApi<T, F, Block> where
Block: BlockT<Hash=H256>,
T: HeaderBackend<Block> + 'static,
F: Fetcher<Block> + 'static,
{
type Block = Block;
type Hash = H256;
type Error = error::Error;
type ValidationFuture = Box<dyn Future<Output = error::Result<TransactionValidity>> + Send + Unpin>;
fn validate_transaction(
&self,
at: &BlockId<Self::Block>,
uxt: txpool::ExtrinsicFor<Self>,
) -> Self::ValidationFuture {
let header_hash = self.client.expect_block_hash_from_id(at);
let header_and_hash = header_hash
.and_then(|header_hash| self.client.expect_header(BlockId::Hash(header_hash))
.map(|header| (header_hash, header)));
let (block, header) = match header_and_hash {
Ok((header_hash, header)) => (header_hash, header),
Err(err) => return Box::new(ready(Err(err.into()))),
};
let remote_validation_request = self.fetcher.remote_call(RemoteCallRequest {
block,
header,
method: "TaggedTransactionQueue_validate_transaction".into(),
call_data: uxt.encode(),
retry_count: None,
});
let remote_validation_request = remote_validation_request.then(move |result| {
let result: error::Result<TransactionValidity> = result
.map_err(Into::into)
.and_then(|result| Decode::decode(&mut &result[..])
.map_err(|e| Error::RuntimeApi(
format!("Error decoding tx validation result: {:?}", e)
))
);
ready(result)
});
Box::new(remote_validation_request)
}
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_hash(&self, at: &BlockId<Self::Block>) -> error::Result<Option<txpool::BlockHash<Self>>> {
Ok(self.client.block_hash_from_id(at)?)
}
fn hash_and_length(&self, ex: &txpool::ExtrinsicFor<Self>) -> (Self::Hash, usize) {
ex.using_encoded(|x| {
(Blake2Hasher::hash(x), x.len())
})
}
}