mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-23 22:25:42 +00:00
Refactor tx-pool maintenance and other high-level api (#4629)
* Reduction. * Reformation. * add locked timer stuff * fix issues and introduce full pool * arrange together * fix benches * fix new_light * Add revalidation test case * review fixes * review fixes * use just ready future * address review
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
|
||||
use futures::executor::block_on;
|
||||
use futures::{future::{ready, Ready}, executor::block_on};
|
||||
use sc_transaction_graph::*;
|
||||
use sp_runtime::transaction_validity::{ValidTransaction, InvalidTransaction};
|
||||
use codec::Encode;
|
||||
@@ -49,7 +49,8 @@ impl ChainApi for TestApi {
|
||||
type Block = Block;
|
||||
type Hash = H256;
|
||||
type Error = sp_transaction_pool::error::Error;
|
||||
type ValidationFuture = futures::future::Ready<sp_transaction_pool::error::Result<TransactionValidity>>;
|
||||
type ValidationFuture = Ready<sp_transaction_pool::error::Result<TransactionValidity>>;
|
||||
type BodyFuture = Ready<sp_transaction_pool::error::Result<Option<Vec<Extrinsic>>>>;
|
||||
|
||||
fn validate_transaction(
|
||||
&self,
|
||||
@@ -61,14 +62,14 @@ impl ChainApi for TestApi {
|
||||
|
||||
match self.block_id_to_number(at) {
|
||||
Ok(Some(num)) if num > 5 => {
|
||||
return futures::future::ready(
|
||||
return ready(
|
||||
Ok(Err(InvalidTransaction::Stale.into()))
|
||||
)
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
||||
futures::future::ready(
|
||||
ready(
|
||||
Ok(Ok(ValidTransaction {
|
||||
priority: 4,
|
||||
requires: if nonce > 1 && self.nonce_dependant {
|
||||
@@ -105,6 +106,10 @@ impl ChainApi for TestApi {
|
||||
let encoded = uxt.encode();
|
||||
(blake2_256(&encoded).into(), encoded.len())
|
||||
}
|
||||
|
||||
fn block_body(&self, _id: &BlockId<Self::Block>) -> Self::BodyFuture {
|
||||
ready(Ok(None))
|
||||
}
|
||||
}
|
||||
|
||||
fn uxt(transfer: Transfer) -> Extrinsic {
|
||||
@@ -150,13 +155,13 @@ fn benchmark_main(c: &mut Criterion) {
|
||||
|
||||
c.bench_function("sequential 50 tx", |b| {
|
||||
b.iter(|| {
|
||||
bench_configured(Pool::new(Default::default(), TestApi::new_dependant()), 50);
|
||||
bench_configured(Pool::new(Default::default(), TestApi::new_dependant().into()), 50);
|
||||
});
|
||||
});
|
||||
|
||||
c.bench_function("random 100 tx", |b| {
|
||||
b.iter(|| {
|
||||
bench_configured(Pool::new(Default::default(), TestApi::default()), 100);
|
||||
bench_configured(Pool::new(Default::default(), TestApi::default().into()), 100);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -68,6 +68,8 @@ pub trait ChainApi: Send + Sync {
|
||||
type Error: From<error::Error> + error::IntoPoolError;
|
||||
/// Validate transaction future.
|
||||
type ValidationFuture: Future<Output=Result<TransactionValidity, Self::Error>> + Send + Unpin;
|
||||
/// Body future (since block body might be remote)
|
||||
type BodyFuture: Future<Output = Result<Option<Vec<<Self::Block as traits::Block>::Extrinsic>>, Self::Error>> + Unpin + Send + 'static;
|
||||
|
||||
/// Verify extrinsic at given block.
|
||||
fn validate_transaction(
|
||||
@@ -84,6 +86,9 @@ pub trait ChainApi: Send + Sync {
|
||||
|
||||
/// Returns hash and encoding length of the extrinsic.
|
||||
fn hash_and_length(&self, uxt: &ExtrinsicFor<Self>) -> (Self::Hash, usize);
|
||||
|
||||
/// Returns a block body given the block id.
|
||||
fn block_body(&self, at: &BlockId<Self::Block>) -> Self::BodyFuture;
|
||||
}
|
||||
|
||||
/// Pool configuration options.
|
||||
@@ -120,7 +125,7 @@ pub struct Pool<B: ChainApi> {
|
||||
|
||||
impl<B: ChainApi> Pool<B> {
|
||||
/// Create a new transaction pool.
|
||||
pub fn new(options: Options, api: B) -> Self {
|
||||
pub fn new(options: Options, api: Arc<B>) -> Self {
|
||||
Pool {
|
||||
validated_pool: Arc::new(ValidatedPool::new(options, api)),
|
||||
}
|
||||
@@ -488,6 +493,7 @@ mod tests {
|
||||
type Hash = u64;
|
||||
type Error = error::Error;
|
||||
type ValidationFuture = futures::future::Ready<error::Result<TransactionValidity>>;
|
||||
type BodyFuture = futures::future::Ready<error::Result<Option<Vec<Extrinsic>>>>;
|
||||
|
||||
/// Verify extrinsic at given block.
|
||||
fn validate_transaction(
|
||||
@@ -560,6 +566,10 @@ mod tests {
|
||||
len
|
||||
)
|
||||
}
|
||||
|
||||
fn block_body(&self, _id: &BlockId<Self::Block>) -> Self::BodyFuture {
|
||||
futures::future::ready(Ok(None))
|
||||
}
|
||||
}
|
||||
|
||||
fn uxt(transfer: Transfer) -> Extrinsic {
|
||||
@@ -567,7 +577,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn pool() -> Pool<TestApi> {
|
||||
Pool::new(Default::default(), TestApi::default())
|
||||
Pool::new(Default::default(), TestApi::default().into())
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -713,7 +723,7 @@ mod tests {
|
||||
ready: limit.clone(),
|
||||
future: limit.clone(),
|
||||
..Default::default()
|
||||
}, TestApi::default());
|
||||
}, TestApi::default().into());
|
||||
|
||||
let hash1 = block_on(pool.submit_one(&BlockId::Number(0), uxt(Transfer {
|
||||
from: AccountId::from_h256(H256::from_low_u64_be(1)),
|
||||
@@ -748,7 +758,7 @@ mod tests {
|
||||
ready: limit.clone(),
|
||||
future: limit.clone(),
|
||||
..Default::default()
|
||||
}, TestApi::default());
|
||||
}, TestApi::default().into());
|
||||
|
||||
// when
|
||||
block_on(pool.submit_one(&BlockId::Number(0), uxt(Transfer {
|
||||
@@ -924,7 +934,7 @@ mod tests {
|
||||
ready: limit.clone(),
|
||||
future: limit.clone(),
|
||||
..Default::default()
|
||||
}, TestApi::default());
|
||||
}, TestApi::default().into());
|
||||
|
||||
let xt = uxt(Transfer {
|
||||
from: AccountId::from_h256(H256::from_low_u64_be(1)),
|
||||
@@ -958,7 +968,7 @@ mod tests {
|
||||
let (tx, rx) = std::sync::mpsc::sync_channel(1);
|
||||
let mut api = TestApi::default();
|
||||
api.delay = Arc::new(Mutex::new(rx.into()));
|
||||
let pool = Arc::new(Pool::new(Default::default(), api));
|
||||
let pool = Arc::new(Pool::new(Default::default(), api.into()));
|
||||
|
||||
// when
|
||||
let xt = uxt(Transfer {
|
||||
|
||||
@@ -63,7 +63,7 @@ pub type ValidatedTransactionFor<B> = ValidatedTransaction<
|
||||
|
||||
/// Pool that deals with validated transactions.
|
||||
pub(crate) struct ValidatedPool<B: ChainApi> {
|
||||
api: B,
|
||||
api: Arc<B>,
|
||||
options: Options,
|
||||
listener: RwLock<Listener<ExHash<B>, BlockHash<B>>>,
|
||||
pool: RwLock<base::BasePool<
|
||||
@@ -76,7 +76,7 @@ pub(crate) struct ValidatedPool<B: ChainApi> {
|
||||
|
||||
impl<B: ChainApi> ValidatedPool<B> {
|
||||
/// Create a new transaction pool.
|
||||
pub fn new(options: Options, api: B) -> Self {
|
||||
pub fn new(options: Options, api: Arc<B>) -> Self {
|
||||
let base_pool = base::BasePool::new(options.reject_future_transactions);
|
||||
ValidatedPool {
|
||||
api,
|
||||
|
||||
Reference in New Issue
Block a user