Cleanup transaction pool deps (#4782)

* Cleanup transaction pool deps

* Fix it properly

* Fix doc test
This commit is contained in:
Bastian Köcher
2020-01-31 12:40:32 +01:00
committed by GitHub
parent ce47bfa2ba
commit 709a899f9d
19 changed files with 140 additions and 69 deletions
+16
View File
@@ -5409,6 +5409,7 @@ dependencies = [
"sp-runtime 2.0.0",
"sp-transaction-pool 2.0.0",
"substrate-test-runtime-client 2.0.0",
"substrate-test-runtime-transaction-pool 2.0.0",
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -5985,6 +5986,7 @@ dependencies = [
"sp-runtime 2.0.0",
"sp-transaction-pool 2.0.0",
"substrate-test-runtime-client 2.0.0",
"substrate-test-runtime-transaction-pool 2.0.0",
]
[[package]]
@@ -7103,6 +7105,20 @@ dependencies = [
"substrate-test-runtime 2.0.0",
]
[[package]]
name = "substrate-test-runtime-transaction-pool"
version = "2.0.0"
dependencies = [
"derive_more 0.99.2 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-scale-codec 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sc-transaction-graph 2.0.0",
"sp-runtime 2.0.0",
"sp-transaction-pool 2.0.0",
"substrate-test-runtime-client 2.0.0",
]
[[package]]
name = "substrate-test-utils"
version = "2.0.0"
+1
View File
@@ -147,6 +147,7 @@ members = [
"test-utils/client",
"test-utils/runtime",
"test-utils/runtime/client",
"test-utils/runtime/transaction-pool",
"utils/browser",
"utils/build-script-utils",
"utils/fork-tree",
+2 -2
View File
@@ -41,7 +41,7 @@ macro_rules! new_full_start {
})?
.with_transaction_pool(|config, client, _fetcher| {
let pool_api = sc_transaction_pool::FullChainApi::new(client.clone());
let pool = sc_transaction_pool::BasicPool::new(config, pool_api);
let pool = sc_transaction_pool::BasicPool::new(config, std::sync::Arc::new(pool_api));
Ok(pool)
})?
.with_import_queue(|_config, client, mut select_chain, transaction_pool| {
@@ -207,7 +207,7 @@ pub fn new_light(config: Configuration<GenesisConfig>)
let pool_api = sc_transaction_pool::LightChainApi::new(client.clone(), fetcher.clone());
let pool = sc_transaction_pool::BasicPool::with_revalidation_type(
config, pool_api, sc_transaction_pool::RevalidationType::Light,
config, Arc::new(pool_api), sc_transaction_pool::RevalidationType::Light,
);
Ok(pool)
})?
+2 -2
View File
@@ -63,7 +63,7 @@ macro_rules! new_full_start {
})?
.with_transaction_pool(|config, client, _fetcher| {
let pool_api = sc_transaction_pool::FullChainApi::new(client.clone());
let pool = sc_transaction_pool::BasicPool::new(config, pool_api);
let pool = sc_transaction_pool::BasicPool::new(config, std::sync::Arc::new(pool_api));
Ok(pool)
})?
.with_import_queue(|_config, client, mut select_chain, _transaction_pool| {
@@ -313,7 +313,7 @@ pub fn new_light(config: NodeConfiguration)
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;
let pool_api = sc_transaction_pool::LightChainApi::new(client.clone(), fetcher.clone());
let pool = sc_transaction_pool::BasicPool::with_revalidation_type(
config, pool_api, sc_transaction_pool::RevalidationType::Light,
config, Arc::new(pool_api), sc_transaction_pool::RevalidationType::Light,
);
Ok(pool)
})?
@@ -306,7 +306,9 @@ mod tests {
fn should_cease_building_block_when_deadline_is_reached() {
// given
let client = Arc::new(substrate_test_runtime_client::new());
let txpool = Arc::new(BasicPool::new(Default::default(), FullChainApi::new(client.clone())));
let txpool = Arc::new(
BasicPool::new(Default::default(), Arc::new(FullChainApi::new(client.clone())))
);
futures::executor::block_on(
txpool.submit_at(&BlockId::number(0), vec![extrinsic(0), extrinsic(1)])
@@ -346,7 +348,9 @@ mod tests {
let (client, backend) = substrate_test_runtime_client::TestClientBuilder::new()
.build_with_backend();
let client = Arc::new(client);
let txpool = Arc::new(BasicPool::new(Default::default(), FullChainApi::new(client.clone())));
let txpool = Arc::new(
BasicPool::new(Default::default(), Arc::new(FullChainApi::new(client.clone())))
);
let genesis_hash = client.info().best_hash;
let block_id = BlockId::Hash(genesis_hash);
+1 -1
View File
@@ -26,7 +26,7 @@
//! # use substrate_test_runtime_client::{self, runtime::{Extrinsic, Transfer}, AccountKeyring};
//! # use sc_transaction_pool::{BasicPool, FullChainApi};
//! # let client = Arc::new(substrate_test_runtime_client::new());
//! # let txpool = Arc::new(BasicPool::new(Default::default(), FullChainApi::new(client.clone())));
//! # let txpool = Arc::new(BasicPool::new(Default::default(), Arc::new(FullChainApi::new(client.clone()))));
//! // The first step is to create a `ProposerFactory`.
//! let mut proposer_factory = ProposerFactory {
//! client: client.clone(),
@@ -18,7 +18,7 @@ serde = { version = "1.0", features=["derive"] }
sc-client = { path = "../../../client" }
sc-client-api = { path = "../../../client/api" }
sc-transaction-pool = { path = "../../transaction-pool", features = ["test-helpers"] }
sc-transaction-pool = { path = "../../transaction-pool" }
sp-blockchain = { path = "../../../primitives/blockchain" }
sp-consensus = { package = "sp-consensus", path = "../../../primitives/consensus/common" }
sp-inherents = { path = "../../../primitives/inherents" }
@@ -28,6 +28,7 @@ sp-transaction-pool = { path = "../../../primitives/transaction-pool" }
[dev-dependencies]
sc-basic-authorship = { path = "../../basic-authorship" }
substrate-test-runtime-client = { path = "../../../test-utils/runtime/client" }
substrate-test-runtime-transaction-pool = { path = "../../../test-utils/runtime/transaction-pool" }
tokio = { version = "0.2", features = ["rt-core", "macros"] }
env_logger = "0.7.0"
tempfile = "3.1.0"
@@ -231,8 +231,8 @@ mod tests {
use sc_transaction_pool::{
BasicPool,
txpool::Options,
testing::api::*,
};
use substrate_test_runtime_transaction_pool::{TestApi, uxt};
use sp_transaction_pool::TransactionPool;
use sp_runtime::generic::BlockId;
use sp_blockchain::HeaderBackend;
@@ -241,8 +241,8 @@ mod tests {
use sp_inherents::InherentDataProviders;
use sc_basic_authorship::ProposerFactory;
fn api() -> TestApi {
TestApi::empty()
fn api() -> Arc<TestApi> {
Arc::new(TestApi::empty())
}
#[tokio::test]
@@ -385,7 +385,8 @@ mod tests {
let client = Arc::new(builder.build());
let select_chain = LongestChain::new(backend.clone());
let inherent_data_providers = InherentDataProviders::new();
let pool = Arc::new(BasicPool::new(Options::default(), api()));
let pool_api = api();
let pool = Arc::new(BasicPool::new(Options::default(), pool_api.clone()));
let env = ProposerFactory {
transaction_pool: pool.clone(),
client: client.clone(),
@@ -419,7 +420,7 @@ mod tests {
finalize: false,
}).await.unwrap();
let created_block = rx.await.unwrap().unwrap();
pool.api().increment_nonce(Alice.into());
pool_api.increment_nonce(Alice.into());
// assert that the background task returns ok
assert_eq!(
@@ -449,7 +450,7 @@ mod tests {
}).await.is_ok());
assert!(rx1.await.unwrap().is_ok());
assert!(backend.blockchain().header(BlockId::Number(1)).unwrap().is_some());
pool.api().increment_nonce(Alice.into());
pool_api.increment_nonce(Alice.into());
assert!(pool.submit_one(&BlockId::Number(2), uxt(Alice, 2)).await.is_ok());
let (tx2, rx2) = futures::channel::oneshot::channel();
+4 -1
View File
@@ -202,7 +202,10 @@ mod tests {
// given
let _ = env_logger::try_init();
let client = Arc::new(substrate_test_runtime_client::new());
let pool = Arc::new(TestPool(BasicPool::new(Default::default(), FullChainApi::new(client.clone()))));
let pool = Arc::new(TestPool(BasicPool::new(
Default::default(),
Arc::new(FullChainApi::new(client.clone())),
)));
client.execution_extensions()
.register_transaction_pool(Arc::downgrade(&pool.clone()) as _);
let db = sc_client_db::offchain::LocalStorage::new_test();
+9 -2
View File
@@ -56,8 +56,15 @@ struct TestSetup {
impl Default for TestSetup {
fn default() -> Self {
let keystore = KeyStore::new();
let client = Arc::new(substrate_test_runtime_client::TestClientBuilder::new().set_keystore(keystore.clone()).build());
let pool = Arc::new(BasicPool::new(Default::default(), FullChainApi::new(client.clone())));
let client = Arc::new(
substrate_test_runtime_client::TestClientBuilder::new()
.set_keystore(keystore.clone())
.build()
);
let pool = Arc::new(BasicPool::new(
Default::default(),
Arc::new(FullChainApi::new(client.clone())),
));
TestSetup {
runtime: runtime::Runtime::new().expect("Failed to create runtime in test setup"),
client,
@@ -36,7 +36,6 @@ use sp_core::{
Bytes, storage::{well_known_keys, StorageKey, StorageData, StorageChangeSet, ChildInfo},
};
use sp_version::RuntimeVersion;
use sp_state_machine::ExecutionStrategy;
use sp_runtime::{
generic::BlockId, traits::{Block as BlockT, NumberFor, SaturatedConversion},
};
+4 -1
View File
@@ -700,7 +700,10 @@ mod tests {
// given
let (client, longest_chain) = TestClientBuilder::new().build_with_longest_chain();
let client = Arc::new(client);
let pool = Arc::new(BasicPool::new(Default::default(), FullChainApi::new(client.clone())));
let pool = Arc::new(BasicPool::new(
Default::default(),
Arc::new(FullChainApi::new(client.clone())),
));
let best = longest_chain.best_chain().unwrap();
let transaction = Transfer {
amount: 5,
+9 -11
View File
@@ -12,17 +12,15 @@ futures = { version = "0.3.1", features = ["compat"] }
futures-diagnose = "1.0"
log = "0.4.8"
parking_lot = "0.9.0"
sp-core = { path = "../../primitives/core" }
sp-api = { path = "../../primitives/api" }
sp-runtime = { path = "../../primitives/runtime" }
sc-transaction-graph = { path = "./graph" }
sp-transaction-pool = { path = "../../primitives/transaction-pool" }
sc-client-api = { path = "../api" }
sp-blockchain = { path = "../../primitives/blockchain" }
substrate-test-runtime-client = { path = "../../test-utils/runtime/client" }
sp-core = { version = "2.0.0", path = "../../primitives/core" }
sp-api = { version = "2.0.0", path = "../../primitives/api" }
sp-runtime = { version = "2.0.0", path = "../../primitives/runtime" }
sc-transaction-graph = { version = "2.0.0", path = "./graph" }
sp-transaction-pool = { version = "2.0.0", path = "../../primitives/transaction-pool" }
sc-client-api = { version = "2.0.0", path = "../api" }
sp-blockchain = { version = "2.0.0", path = "../../primitives/blockchain" }
[dev-dependencies]
sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" }
[features]
test-helpers = []
substrate-test-runtime-transaction-pool = { version = "2.0.0", path = "../../test-utils/runtime/transaction-pool" }
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }
+10 -19
View File
@@ -42,8 +42,6 @@ use sp_transaction_pool::{
MaintainedTransactionPool, PoolFuture,
};
type PoolResult<T> = PoolFuture<T, error::Error>;
/// Basic implementation of transaction pool that can be customized by providing PoolApi.
pub struct BasicPool<PoolApi, Block>
where
@@ -80,7 +78,7 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
/// Create new basic transaction pool with provided api.
pub fn new(
options: sc_transaction_graph::Options,
pool_api: PoolApi,
pool_api: Arc<PoolApi>,
) -> Self {
Self::with_revalidation_type(options, pool_api, RevalidationType::Full)
}
@@ -89,14 +87,13 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
/// revalidation type.
pub fn with_revalidation_type(
options: sc_transaction_graph::Options,
pool_api: PoolApi,
pool_api: Arc<PoolApi>,
revalidation_type: RevalidationType,
) -> Self {
let api = Arc::new(pool_api);
let cloned_api = api.clone();
let cloned_api = pool_api.clone();
BasicPool {
api: cloned_api,
pool: Arc::new(sc_transaction_graph::Pool::new(options, api)),
pool: Arc::new(sc_transaction_graph::Pool::new(options, pool_api)),
revalidation_strategy: Arc::new(Mutex::new(
match revalidation_type {
RevalidationType::Light => RevalidationStrategy::Light(RevalidationStatus::NotScheduled),
@@ -111,29 +108,23 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
pub fn pool(&self) -> &Arc<sc_transaction_graph::Pool<PoolApi>> {
&self.pool
}
/// Get reference to the inner chain api, for tests only.
#[cfg(any(feature = "test-helpers", test))]
pub fn api(&self) -> &Arc<PoolApi> {
&self.api
}
}
impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
where
Block: BlockT,
PoolApi: 'static + sc_transaction_graph::ChainApi<Block=Block, Hash=Block::Hash, Error=error::Error>,
PoolApi: 'static + sc_transaction_graph::ChainApi<Block=Block, Hash=Block::Hash>,
{
type Block = PoolApi::Block;
type Hash = sc_transaction_graph::ExHash<PoolApi>;
type InPoolTransaction = sc_transaction_graph::base_pool::Transaction<TxHash<Self>, TransactionFor<Self>>;
type Error = error::Error;
type Error = PoolApi::Error;
fn submit_at(
&self,
at: &BlockId<Self::Block>,
xts: Vec<TransactionFor<Self>>,
) -> PoolResult<Vec<Result<TxHash<Self>, Self::Error>>> {
) -> PoolFuture<Vec<Result<TxHash<Self>, Self::Error>>, Self::Error> {
let pool = self.pool.clone();
let at = *at;
async move {
@@ -145,7 +136,7 @@ impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
&self,
at: &BlockId<Self::Block>,
xt: TransactionFor<Self>,
) -> PoolResult<TxHash<Self>> {
) -> PoolFuture<TxHash<Self>, Self::Error> {
let pool = self.pool.clone();
let at = *at;
async move {
@@ -157,7 +148,7 @@ impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
&self,
at: &BlockId<Self::Block>,
xt: TransactionFor<Self>,
) -> PoolResult<Box<TransactionStatusStreamFor<Self>>> {
) -> PoolFuture<Box<TransactionStatusStreamFor<Self>>, Self::Error> {
let at = *at;
let pool = self.pool.clone();
@@ -287,7 +278,7 @@ impl<N: Clone + Copy + SimpleArithmetic> RevalidationStatus<N> {
impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
where
Block: BlockT,
PoolApi: 'static + sc_transaction_graph::ChainApi<Block=Block, Hash=Block::Hash, Error=error::Error>,
PoolApi: 'static + sc_transaction_graph::ChainApi<Block=Block, Hash=Block::Hash>,
{
fn maintain(&self, id: &BlockId<Self::Block>, retracted: &[BlockHash<Self>])
-> Pin<Box<dyn Future<Output=()> + Send>>
@@ -16,8 +16,4 @@
//! Tests for top-level transaction pool api
#[cfg(any(feature = "test-helpers", test))]
pub mod api;
#[cfg(test)]
mod pool;
mod pool;
@@ -1,6 +1,21 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use crate::*;
use sc_transaction_graph::{self, Pool};
use sc_transaction_graph::Pool;
use futures::executor::block_on;
use sp_runtime::{
generic::BlockId,
@@ -10,14 +25,14 @@ use substrate_test_runtime_client::{
runtime::{Block, Hash, Index},
AccountKeyring::*,
};
use crate::testing::api::{TestApi, uxt};
use substrate_test_runtime_transaction_pool::{TestApi, uxt};
fn pool() -> Pool<TestApi> {
Pool::new(Default::default(), TestApi::with_alice_nonce(209).into())
}
fn maintained_pool() -> BasicPool<TestApi, Block> {
BasicPool::new(Default::default(), TestApi::with_alice_nonce(209))
BasicPool::new(Default::default(), std::sync::Arc::new(TestApi::with_alice_nonce(209)))
}
#[test]
@@ -0,0 +1,16 @@
[package]
name = "substrate-test-runtime-transaction-pool"
version = "2.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "GPL-3.0"
[dependencies]
substrate-test-runtime-client = { version = "2.0.0", path = "../client" }
parking_lot = "0.9.0"
codec = { package = "parity-scale-codec", version = "1.0.0" }
sp-runtime = { version = "2.0.0", path = "../../../primitives/runtime" }
sp-transaction-pool = { version = "2.0.0", path = "../../../primitives/transaction-pool" }
sc-transaction-graph = { version = "2.0.0", path = "../../../client/transaction-pool/graph" }
futures = { version = "0.3.1", features = ["compat"] }
derive_more = "0.99.2"
@@ -14,22 +14,41 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! Test ChainApi
//! Test utils for the transaction pool together with the test runtime.
//!
//! See [`TestApi`] for more information.
use crate::*;
use codec::Encode;
use parking_lot::RwLock;
use sp_runtime::{
generic::{self, BlockId},
traits::{BlakeTwo256, Hash as HashT},
transaction_validity::{TransactionValidity, ValidTransaction, TransactionValidityError, InvalidTransaction},
transaction_validity::{
TransactionValidity, ValidTransaction, TransactionValidityError, InvalidTransaction,
},
};
use std::collections::HashSet;
use std::collections::{HashSet, HashMap};
use substrate_test_runtime_client::{
runtime::{Index, AccountId, Block, BlockNumber, Extrinsic, Hash, Header, Transfer},
AccountKeyring::{self, *},
};
/// Error type used by [`TestApi`].
#[derive(Debug, derive_more::From, derive_more::Display)]
pub struct Error(sp_transaction_pool::error::Error);
impl sp_transaction_pool::error::IntoPoolError for Error {
fn into_pool_error(self) -> Result<sp_transaction_pool::error::Error, Self> {
Ok(self.0)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}
#[derive(Default)]
struct ChainState {
pub block_by_number: HashMap<BlockNumber, Vec<Extrinsic>>,
@@ -47,7 +66,6 @@ pub struct TestApi {
}
impl TestApi {
/// Test Api with Alice nonce set initially.
pub fn with_alice_nonce(nonce: u64) -> Self {
let api = TestApi {
@@ -128,9 +146,9 @@ impl TestApi {
impl sc_transaction_graph::ChainApi for TestApi {
type Block = Block;
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>>>>;
type Error = Error;
type ValidationFuture = futures::future::Ready<Result<TransactionValidity, Error>>;
type BodyFuture = futures::future::Ready<Result<Option<Vec<Extrinsic>>, Error>>;
fn validate_transaction(
&self,
@@ -149,7 +167,7 @@ impl sc_transaction_graph::ChainApi for TestApi {
if self.chain.read().invalid_hashes.contains(&self.hash_and_length(&uxt).0) {
return futures::future::ready(Ok(
Err(TransactionValidityError::Invalid(InvalidTransaction::Custom(0)))
Err(TransactionValidityError::Invalid(InvalidTransaction::Custom(0)).into())
))
}
@@ -169,14 +187,14 @@ impl sc_transaction_graph::ChainApi for TestApi {
fn block_id_to_number(
&self,
at: &BlockId<Self::Block>,
) -> error::Result<Option<sc_transaction_graph::NumberFor<Self>>> {
) -> Result<Option<sc_transaction_graph::NumberFor<Self>>, Error> {
Ok(Some(number_of(at)))
}
fn block_id_to_hash(
&self,
at: &BlockId<Self::Block>,
) -> error::Result<Option<sc_transaction_graph::BlockHash<Self>>> {
) -> Result<Option<sc_transaction_graph::BlockHash<Self>>, Error> {
Ok(match at {
generic::BlockId::Hash(x) => Some(x.clone()),
_ => Some(Default::default()),
+3 -1
View File
@@ -235,7 +235,9 @@ mod tests {
// given
let _ = env_logger::try_init();
let client = Arc::new(substrate_test_runtime_client::new());
let pool = Arc::new(BasicPool::new(Default::default(), FullChainApi::new(client.clone())));
let pool = Arc::new(
BasicPool::new(Default::default(), Arc::new(FullChainApi::new(client.clone())))
);
let new_transaction = |nonce: u64| {
let t = Transfer {