mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-18 15:21:05 +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:
@@ -14,29 +14,53 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
use super::*;
|
||||
|
||||
use crate::{BasicPool, MaintainedTransactionPool};
|
||||
use codec::Encode;
|
||||
use futures::executor::block_on;
|
||||
use sc_transaction_graph::{self, Pool};
|
||||
use substrate_test_runtime_client::{runtime::{AccountId, Block, Hash, Index, Extrinsic, Transfer}, AccountKeyring::{self, *}};
|
||||
use parking_lot::RwLock;
|
||||
use sc_transaction_graph::{self, ExHash, Pool};
|
||||
use sp_runtime::{
|
||||
generic::{self, BlockId},
|
||||
traits::{Hash as HashT, BlakeTwo256},
|
||||
transaction_validity::{TransactionValidity, ValidTransaction},
|
||||
traits::{BlakeTwo256, Hash as HashT},
|
||||
transaction_validity::{TransactionValidity, ValidTransaction, TransactionValidityError, InvalidTransaction},
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use substrate_test_runtime_client::{
|
||||
runtime::{AccountId, Block, BlockNumber, Extrinsic, Hash, Header, Index, Transfer},
|
||||
AccountKeyring::{self, *},
|
||||
};
|
||||
|
||||
struct TestApi {
|
||||
pub modifier: Box<dyn Fn(&mut ValidTransaction) + Send + Sync>,
|
||||
pub modifier: RwLock<Box<dyn Fn(&mut ValidTransaction) + Send + Sync>>,
|
||||
pub chain_block_by_number: RwLock<HashMap<BlockNumber, Vec<Extrinsic>>>,
|
||||
pub chain_headers_by_number: RwLock<HashMap<BlockNumber, Header>>,
|
||||
pub invalid_hashes: RwLock<HashSet<ExHash<Self>>>,
|
||||
pub validation_requests: RwLock<Vec<Extrinsic>>,
|
||||
}
|
||||
|
||||
impl TestApi {
|
||||
fn default() -> Self {
|
||||
TestApi {
|
||||
modifier: Box::new(|_| {}),
|
||||
modifier: RwLock::new(Box::new(|_| {})),
|
||||
chain_block_by_number: RwLock::new(HashMap::new()),
|
||||
invalid_hashes: RwLock::new(HashSet::new()),
|
||||
chain_headers_by_number: RwLock::new(HashMap::new()),
|
||||
validation_requests: RwLock::new(Default::default()),
|
||||
}
|
||||
}
|
||||
|
||||
fn push_block(&self, block_number: BlockNumber, xts: Vec<Extrinsic>) {
|
||||
self.chain_block_by_number.write().insert(block_number, xts);
|
||||
self.chain_headers_by_number.write().insert(block_number, Header {
|
||||
number: block_number,
|
||||
digest: Default::default(),
|
||||
extrinsics_root: Default::default(),
|
||||
parent_hash: Default::default(),
|
||||
state_root: Default::default(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl sc_transaction_graph::ChainApi for TestApi {
|
||||
@@ -44,12 +68,16 @@ impl sc_transaction_graph::ChainApi for TestApi {
|
||||
type Hash = Hash;
|
||||
type Error = error::Error;
|
||||
type ValidationFuture = futures::future::Ready<error::Result<TransactionValidity>>;
|
||||
type BodyFuture = futures::future::Ready<error::Result<Option<Vec<Extrinsic>>>>;
|
||||
|
||||
fn validate_transaction(
|
||||
&self,
|
||||
at: &BlockId<Self::Block>,
|
||||
uxt: sc_transaction_graph::ExtrinsicFor<Self>,
|
||||
) -> Self::ValidationFuture {
|
||||
|
||||
self.validation_requests.write().push(uxt.clone());
|
||||
|
||||
let expected = index(at);
|
||||
let requires = if expected == uxt.transfer().nonce {
|
||||
vec![]
|
||||
@@ -58,6 +86,12 @@ impl sc_transaction_graph::ChainApi for TestApi {
|
||||
};
|
||||
let provides = vec![vec![uxt.transfer().nonce as u8]];
|
||||
|
||||
if self.invalid_hashes.read().contains(&self.hash_and_length(&uxt).0) {
|
||||
return futures::future::ready(Ok(
|
||||
Err(TransactionValidityError::Invalid(InvalidTransaction::Custom(0)))
|
||||
))
|
||||
}
|
||||
|
||||
let mut validity = ValidTransaction {
|
||||
priority: 1,
|
||||
requires,
|
||||
@@ -66,29 +100,43 @@ impl sc_transaction_graph::ChainApi for TestApi {
|
||||
propagate: true,
|
||||
};
|
||||
|
||||
(self.modifier)(&mut validity);
|
||||
(self.modifier.read())(&mut validity);
|
||||
|
||||
futures::future::ready(Ok(
|
||||
Ok(validity)
|
||||
))
|
||||
futures::future::ready(Ok(Ok(validity)))
|
||||
}
|
||||
|
||||
fn block_id_to_number(&self, at: &BlockId<Self::Block>) -> error::Result<Option<sc_transaction_graph::NumberFor<Self>>> {
|
||||
fn block_id_to_number(
|
||||
&self,
|
||||
at: &BlockId<Self::Block>,
|
||||
) -> error::Result<Option<sc_transaction_graph::NumberFor<Self>>> {
|
||||
Ok(Some(number_of(at)))
|
||||
}
|
||||
|
||||
fn block_id_to_hash(&self, at: &BlockId<Self::Block>) -> error::Result<Option<sc_transaction_graph::BlockHash<Self>>> {
|
||||
fn block_id_to_hash(
|
||||
&self,
|
||||
at: &BlockId<Self::Block>,
|
||||
) -> error::Result<Option<sc_transaction_graph::BlockHash<Self>>> {
|
||||
Ok(match at {
|
||||
generic::BlockId::Hash(x) => Some(x.clone()),
|
||||
_ => Some(Default::default()),
|
||||
})
|
||||
}
|
||||
|
||||
fn hash_and_length(&self, ex: &sc_transaction_graph::ExtrinsicFor<Self>) -> (Self::Hash, usize) {
|
||||
fn hash_and_length(
|
||||
&self,
|
||||
ex: &sc_transaction_graph::ExtrinsicFor<Self>,
|
||||
) -> (Self::Hash, usize) {
|
||||
let encoded = ex.encode();
|
||||
(BlakeTwo256::hash(&encoded), encoded.len())
|
||||
}
|
||||
|
||||
fn block_body(&self, id: &BlockId<Self::Block>) -> Self::BodyFuture {
|
||||
futures::future::ready(Ok(if let BlockId::Number(num) = id {
|
||||
self.chain_block_by_number.read().get(num).cloned()
|
||||
} else {
|
||||
None
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
fn index(at: &BlockId<Block>) -> u64 {
|
||||
@@ -114,7 +162,11 @@ fn uxt(who: AccountKeyring, nonce: Index) -> Extrinsic {
|
||||
}
|
||||
|
||||
fn pool() -> Pool<TestApi> {
|
||||
Pool::new(Default::default(), TestApi::default())
|
||||
Pool::new(Default::default(), TestApi::default().into())
|
||||
}
|
||||
|
||||
fn maintained_pool() -> BasicPool<TestApi, Block> {
|
||||
BasicPool::new(Default::default(), TestApi::default())
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -192,11 +244,11 @@ fn should_ban_invalid_transactions() {
|
||||
|
||||
#[test]
|
||||
fn should_correctly_prune_transactions_providing_more_than_one_tag() {
|
||||
let mut api = TestApi::default();
|
||||
api.modifier = Box::new(|v: &mut ValidTransaction| {
|
||||
let api = TestApi::default();
|
||||
*api.modifier.write() = Box::new(|v: &mut ValidTransaction| {
|
||||
v.provides.push(vec![155]);
|
||||
});
|
||||
let pool = Pool::new(Default::default(), api);
|
||||
let pool = Pool::new(Default::default(), Arc::new(api));
|
||||
let xt = uxt(Alice, 209);
|
||||
block_on(pool.submit_one(&BlockId::number(0), xt.clone())).expect("1. Imported");
|
||||
assert_eq!(pool.status().ready, 1);
|
||||
@@ -220,3 +272,38 @@ fn should_correctly_prune_transactions_providing_more_than_one_tag() {
|
||||
assert_eq!(pool.status().ready, 0);
|
||||
assert_eq!(pool.status().future, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_prune_old_during_maintenance() {
|
||||
let xt = uxt(Alice, 209);
|
||||
|
||||
let pool = maintained_pool();
|
||||
|
||||
block_on(pool.submit_one(&BlockId::number(0), xt.clone())).expect("1. Imported");
|
||||
assert_eq!(pool.status().ready, 1);
|
||||
|
||||
pool.api.push_block(1, vec![xt.clone()]);
|
||||
|
||||
block_on(pool.maintain(&BlockId::number(1), &[]));
|
||||
assert_eq!(pool.status().ready, 0);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn should_revalidate_during_maintenance() {
|
||||
let xt1 = uxt(Alice, 209);
|
||||
let xt2 = uxt(Alice, 210);
|
||||
|
||||
let pool = maintained_pool();
|
||||
block_on(pool.submit_one(&BlockId::number(0), xt1.clone())).expect("1. Imported");
|
||||
block_on(pool.submit_one(&BlockId::number(0), xt2.clone())).expect("2. Imported");
|
||||
assert_eq!(pool.status().ready, 2);
|
||||
assert_eq!(pool.api.validation_requests.read().len(), 2);
|
||||
|
||||
pool.api.push_block(1, vec![xt1.clone()]);
|
||||
|
||||
block_on(pool.maintain(&BlockId::number(1), &[]));
|
||||
assert_eq!(pool.status().ready, 1);
|
||||
// test that pool revalidated transaction that left ready and not included in the block
|
||||
assert_eq!(pool.api.validation_requests.read().len(), 3);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user