diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index e5e0991212..b79b9c0827 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -2191,6 +2191,7 @@ dependencies = [ "substrate-keyring 2.0.0", "substrate-primitives 2.0.0", "substrate-state-machine 2.0.0", + "substrate-test-client 2.0.0", "substrate-trie 2.0.0", "trie-root 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3349,7 +3350,7 @@ dependencies = [ "substrate-consensus-common 2.0.0", "substrate-primitives 2.0.0", "substrate-state-machine 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)", "trybuild 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -3935,7 +3936,7 @@ dependencies = [ "substrate-inherents 2.0.0", "substrate-primitives 2.0.0", "substrate-telemetry 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "substrate-transaction-pool 2.0.0", ] @@ -4010,7 +4011,7 @@ dependencies = [ "substrate-primitives 2.0.0", "substrate-state-machine 2.0.0", "substrate-telemetry 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "substrate-trie 2.0.0", ] @@ -4035,7 +4036,7 @@ dependencies = [ "substrate-primitives 2.0.0", "substrate-state-db 2.0.0", "substrate-state-machine 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "substrate-trie 2.0.0", ] @@ -4066,7 +4067,7 @@ dependencies = [ "substrate-primitives 2.0.0", "substrate-service 2.0.0", "substrate-telemetry 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "tokio 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4124,7 +4125,7 @@ dependencies = [ "substrate-primitives 2.0.0", "substrate-service 2.0.0", "substrate-telemetry 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "tokio 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4155,7 +4156,7 @@ dependencies = [ "sr-version 2.0.0", "substrate-inherents 2.0.0", "substrate-primitives 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4197,7 +4198,7 @@ dependencies = [ "substrate-consensus-common 2.0.0", "substrate-inherents 2.0.0", "substrate-primitives 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4250,7 +4251,7 @@ dependencies = [ "substrate-primitives 2.0.0", "substrate-service 2.0.0", "substrate-telemetry 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "tokio 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4323,7 +4324,7 @@ dependencies = [ "substrate-network-libp2p 2.0.0", "substrate-peerset 2.0.0", "substrate-primitives 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "tokio 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4372,7 +4373,7 @@ dependencies = [ "substrate-consensus-common 2.0.0", "substrate-offchain-primitives 2.0.0", "substrate-primitives 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "substrate-transaction-pool 2.0.0", "tokio 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4460,13 +4461,11 @@ dependencies = [ "sr-primitives 2.0.0", "sr-version 2.0.0", "substrate-client 2.0.0", - "substrate-consensus-common 2.0.0", "substrate-executor 2.0.0", "substrate-network 2.0.0", "substrate-primitives 2.0.0", "substrate-state-machine 2.0.0", - "substrate-test-client 2.0.0", - "substrate-test-runtime 2.0.0", + "substrate-test-runtime-client 2.0.0", "substrate-transaction-pool 2.0.0", "tokio 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4522,7 +4521,7 @@ dependencies = [ "substrate-primitives 2.0.0", "substrate-rpc-servers 2.0.0", "substrate-telemetry 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "substrate-transaction-pool 2.0.0", "target_info 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", @@ -4599,6 +4598,7 @@ name = "substrate-test-client" version = "2.0.0" dependencies = [ "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", + "hash-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec 3.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "sr-primitives 2.0.0", "substrate-client 2.0.0", @@ -4608,7 +4608,6 @@ dependencies = [ "substrate-keyring 2.0.0", "substrate-primitives 2.0.0", "substrate-state-machine 2.0.0", - "substrate-test-runtime 2.0.0", ] [[package]] @@ -4635,11 +4634,21 @@ dependencies = [ "substrate-keyring 2.0.0", "substrate-offchain-primitives 2.0.0", "substrate-primitives 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "substrate-trie 2.0.0", "trie-db 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "substrate-test-runtime-client" +version = "2.0.0" +dependencies = [ + "sr-primitives 2.0.0", + "substrate-primitives 2.0.0", + "substrate-test-client 2.0.0", + "substrate-test-runtime 2.0.0", +] + [[package]] name = "substrate-transaction-graph" version = "2.0.0" @@ -4670,7 +4679,7 @@ dependencies = [ "substrate-client 2.0.0", "substrate-keyring 2.0.0", "substrate-primitives 2.0.0", - "substrate-test-client 2.0.0", + "substrate-test-runtime-client 2.0.0", "substrate-transaction-graph 2.0.0", ] diff --git a/substrate/Cargo.toml b/substrate/Cargo.toml index 5710d08aff..0f681dc766 100644 --- a/substrate/Cargo.toml +++ b/substrate/Cargo.toml @@ -22,15 +22,17 @@ members = [ "core/cli", "core/client", "core/client/db", - "core/consensus/common", "core/consensus/aura", "core/consensus/babe", + "core/consensus/common", "core/consensus/rhd", "core/consensus/slots", "core/executor", "core/finality-grandpa", "core/finality-grandpa/primitives", + "core/inherents", "core/keyring", + "core/keystore", "core/network", "core/panic-handler", "core/primitives", @@ -46,13 +48,13 @@ members = [ "core/sr-std", "core/sr-version", "core/state-machine", - "core/test-runtime", "core/telemetry", - "core/trie", - "core/keystore", + "core/test-client", + "core/test-runtime", + "core/test-runtime/client", "core/transaction-pool", "core/transaction-pool/graph", - "core/inherents", + "core/trie", "core/util/fork-tree", "srml/support", "srml/support/procedural", diff --git a/substrate/core/basic-authorship/Cargo.toml b/substrate/core/basic-authorship/Cargo.toml index 63408b4329..547fca9030 100644 --- a/substrate/core/basic-authorship/Cargo.toml +++ b/substrate/core/basic-authorship/Cargo.toml @@ -17,4 +17,4 @@ transaction_pool = { package = "substrate-transaction-pool", path = "../../core/ substrate-telemetry = { path = "../telemetry" } [dev-dependencies] -test-client = { package = "substrate-test-client", path = "../../core/test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client" } diff --git a/substrate/core/client/Cargo.toml b/substrate/core/client/Cargo.toml index 0978b897ec..510ec50b8b 100644 --- a/substrate/core/client/Cargo.toml +++ b/substrate/core/client/Cargo.toml @@ -28,7 +28,7 @@ inherents = { package = "substrate-inherents", path = "../inherents", default-fe sr-api-macros = { path = "../sr-api-macros" } [dev-dependencies] -test-client = { package = "substrate-test-client", path = "../test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../test-runtime/client" } kvdb-memorydb = { git = "https://github.com/paritytech/parity-common", rev="b0317f649ab2c665b7987b8475878fc4d2e1f81d" } [features] diff --git a/substrate/core/client/db/Cargo.toml b/substrate/core/client/db/Cargo.toml index 64051164b8..e40b1568f6 100644 --- a/substrate/core/client/db/Cargo.toml +++ b/substrate/core/client/db/Cargo.toml @@ -25,7 +25,7 @@ consensus_common = { package = "substrate-consensus-common", path = "../../conse [dev-dependencies] substrate-keyring = { path = "../../keyring" } -test-client = { package = "substrate-test-client", path = "../../test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../test-runtime/client" } env_logger = { version = "0.6" } [features] diff --git a/substrate/core/client/src/client.rs b/substrate/core/client/src/client.rs index 59af1b6419..8336a90ca4 100644 --- a/substrate/core/client/src/client.rs +++ b/substrate/core/client/src/client.rs @@ -1709,8 +1709,9 @@ pub(crate) mod tests { use runtime_primitives::generic::DigestItem; use consensus::{BlockOrigin, SelectChain}; use test_client::{ - TestClient, AccountKeyring, client::backend::Backend as TestBackend, TestClientBuilder, - BlockBuilderExt, runtime::{self, Block, Transfer, RuntimeApi, TestAPI} + prelude::*, + client::backend::Backend as TestBackend, + runtime::{self, Block, Transfer, RuntimeApi, TestAPI}, }; /// Returns tuple, consisting of: diff --git a/substrate/core/client/src/light/call_executor.rs b/substrate/core/client/src/light/call_executor.rs index a6b381a8b1..a95b73a960 100644 --- a/substrate/core/client/src/light/call_executor.rs +++ b/substrate/core/client/src/light/call_executor.rs @@ -473,7 +473,7 @@ pub fn check_execution_proof( #[cfg(test)] mod tests { use consensus::BlockOrigin; - use test_client::{self, runtime::{Block, Header}, runtime::RuntimeApi, TestClient}; + use test_client::{self, runtime::Header, ClientExt, TestClient}; use executor::NativeExecutionDispatch; use crate::backend::{Backend, NewBlockState}; use crate::in_mem::Backend as InMemBackend; @@ -482,13 +482,6 @@ mod tests { #[test] fn execution_proof_is_generated_and_checked() { - type TestClient = test_client::client::Client< - test_client::Backend, - test_client::Executor, - Block, - RuntimeApi - >; - fn execute(remote_client: &TestClient, at: u64, method: &'static str) -> (Vec, Vec) { let remote_block_id = BlockId::Number(at); let remote_root = remote_client.state_at(&remote_block_id) diff --git a/substrate/core/client/src/light/fetcher.rs b/substrate/core/client/src/light/fetcher.rs index 985aa5bdaa..bacfb45a7e 100644 --- a/substrate/core/client/src/light/fetcher.rs +++ b/substrate/core/client/src/light/fetcher.rs @@ -491,7 +491,7 @@ pub mod tests { use executor::{self, NativeExecutionDispatch}; use crate::error::Error as ClientError; use test_client::{ - self, TestClient, blockchain::HeaderBackend, AccountKeyring, + self, ClientExt, blockchain::HeaderBackend, AccountKeyring, runtime::{self, Hash, Block, Header, Extrinsic} }; use consensus::BlockOrigin; diff --git a/substrate/core/consensus/aura/Cargo.toml b/substrate/core/consensus/aura/Cargo.toml index 9c46b942a4..c8711d0289 100644 --- a/substrate/core/consensus/aura/Cargo.toml +++ b/substrate/core/consensus/aura/Cargo.toml @@ -31,6 +31,6 @@ keyring = { package = "substrate-keyring", path = "../../keyring" } substrate-executor = { path = "../../executor" } network = { package = "substrate-network", path = "../../network", features = ["test-helpers"]} service = { package = "substrate-service", path = "../../service" } -test_client = { package = "substrate-test-client", path = "../../test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../test-runtime/client" } tokio = "0.1.7" env_logger = "0.6" diff --git a/substrate/core/consensus/babe/Cargo.toml b/substrate/core/consensus/babe/Cargo.toml index 020497a104..1eaf05e4a0 100644 --- a/substrate/core/consensus/babe/Cargo.toml +++ b/substrate/core/consensus/babe/Cargo.toml @@ -35,6 +35,6 @@ keyring = { package = "substrate-keyring", path = "../../keyring" } substrate-executor = { path = "../../executor" } network = { package = "substrate-network", path = "../../network", features = ["test-helpers"]} service = { package = "substrate-service", path = "../../service" } -test_client = { package = "substrate-test-client", path = "../../test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../test-runtime/client" } tokio = "0.1.18" env_logger = "0.6.1" diff --git a/substrate/core/consensus/common/Cargo.toml b/substrate/core/consensus/common/Cargo.toml index 3d133461e7..2852f4aa0f 100644 --- a/substrate/core/consensus/common/Cargo.toml +++ b/substrate/core/consensus/common/Cargo.toml @@ -21,7 +21,7 @@ parity-codec = { version = "3.3", features = ["derive"] } parking_lot = "0.8.0" [dev-dependencies] -test_client = { package = "substrate-test-client", path = "../../test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../test-runtime/client" } [features] default = [] diff --git a/substrate/core/consensus/slots/Cargo.toml b/substrate/core/consensus/slots/Cargo.toml index b82c2fc9d7..5c187024c9 100644 --- a/substrate/core/consensus/slots/Cargo.toml +++ b/substrate/core/consensus/slots/Cargo.toml @@ -18,4 +18,4 @@ parking_lot = "0.8.0" log = "0.4" [dev-dependencies] -test_client = { package = "substrate-test-client", path = "../../test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../test-runtime/client" } diff --git a/substrate/core/finality-grandpa/Cargo.toml b/substrate/core/finality-grandpa/Cargo.toml index b00c646286..bbd6297d35 100644 --- a/substrate/core/finality-grandpa/Cargo.toml +++ b/substrate/core/finality-grandpa/Cargo.toml @@ -29,7 +29,7 @@ grandpa = { package = "finality-grandpa", version = "0.7.2", features = ["derive consensus_common = { package = "substrate-consensus-common", path = "../consensus/common", features = ["test-helpers"] } network = { package = "substrate-network", path = "../network", features = ["test-helpers"] } keyring = { package = "substrate-keyring", path = "../keyring" } -test_client = { package = "substrate-test-client", path = "../test-client"} +test-client = { package = "substrate-test-runtime-client", path = "../test-runtime/client"} env_logger = "0.6" [features] diff --git a/substrate/core/network/Cargo.toml b/substrate/core/network/Cargo.toml index 8fbf7daa9c..1b3a473288 100644 --- a/substrate/core/network/Cargo.toml +++ b/substrate/core/network/Cargo.toml @@ -30,16 +30,16 @@ peerset = { package = "substrate-peerset", path = "../../core/peerset" } tokio-timer = "0.2.11" tokio = { version = "0.1.11", optional = true } keyring = { package = "substrate-keyring", path = "../../core/keyring", optional = true } -test_client = { package = "substrate-test-client", path = "../../core/test-client", optional = true } +test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client", optional = true } void = "1.0" [dev-dependencies] env_logger = { version = "0.6" } keyring = { package = "substrate-keyring", path = "../../core/keyring" } -test_client = { package = "substrate-test-client", path = "../../core/test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client" } consensus = { package = "substrate-consensus-common", path = "../../core/consensus/common", features = ["test-helpers"] } tokio = "0.1.11" [features] default = [] -test-helpers = ["keyring", "test_client", "consensus/test-helpers", "tokio"] +test-helpers = ["keyring", "test-client", "consensus/test-helpers", "tokio"] diff --git a/substrate/core/network/src/test/block_import.rs b/substrate/core/network/src/test/block_import.rs index 4cf639484f..f10338a62d 100644 --- a/substrate/core/network/src/test/block_import.rs +++ b/substrate/core/network/src/test/block_import.rs @@ -17,7 +17,7 @@ //! Testing block import logic. use consensus::import_queue::{import_single_block, BasicQueue, BlockImportError, BlockImportResult}; -use test_client::{self, TestClient}; +use test_client::{self, prelude::*}; use test_client::runtime::{Block, Hash}; use runtime_primitives::generic::BlockId; use super::*; @@ -26,12 +26,7 @@ struct TestLink {} impl Link for TestLink {} -fn prepare_good_block() -> (client::Client< - test_client::Backend, - test_client::Executor, - Block, - test_client::runtime::RuntimeApi, - >, Hash, u64, PeerId, IncomingBlock) { +fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock) { let client = test_client::new(); let block = client.new_block(Default::default()).unwrap().bake().unwrap(); client.import(BlockOrigin::File, block).unwrap(); diff --git a/substrate/core/offchain/Cargo.toml b/substrate/core/offchain/Cargo.toml index 0672ebcc0c..a7b94bdc56 100644 --- a/substrate/core/offchain/Cargo.toml +++ b/substrate/core/offchain/Cargo.toml @@ -21,7 +21,7 @@ transaction_pool = { package = "substrate-transaction-pool", path = "../../core/ [dev-dependencies] env_logger = "0.6" -test_client = { package = "substrate-test-client", path = "../../core/test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client" } [features] default = [] diff --git a/substrate/core/rpc/Cargo.toml b/substrate/core/rpc/Cargo.toml index c621f71a43..6ef9af2a0a 100644 --- a/substrate/core/rpc/Cargo.toml +++ b/substrate/core/rpc/Cargo.toml @@ -29,7 +29,5 @@ tokio = "0.1.7" assert_matches = "1.1" futures = "0.1.17" sr-io = { path = "../sr-io" } -test_client = { package = "substrate-test-client", path = "../test-client" } -test_runtime = { package = "substrate-test-runtime", path = "../test-runtime" } -consensus = { package = "substrate-consensus-common", path = "../consensus/common" } +test-client = { package = "substrate-test-runtime-client", path = "../test-runtime/client" } rustc-hex = "2.0" diff --git a/substrate/core/rpc/src/chain/tests.rs b/substrate/core/rpc/src/chain/tests.rs index 02507442d5..eed9ae836b 100644 --- a/substrate/core/rpc/src/chain/tests.rs +++ b/substrate/core/rpc/src/chain/tests.rs @@ -16,9 +16,11 @@ use super::*; use assert_matches::assert_matches; -use test_client::{self, TestClient}; -use test_client::runtime::{H256, Block, Header}; -use consensus::BlockOrigin; +use test_client::{ + prelude::*, + consensus::BlockOrigin, + runtime::{H256, Block, Header}, +}; #[test] fn should_return_header() { diff --git a/substrate/core/rpc/src/state/tests.rs b/substrate/core/rpc/src/state/tests.rs index b40ac06c60..498b58b40b 100644 --- a/substrate/core/rpc/src/state/tests.rs +++ b/substrate/core/rpc/src/state/tests.rs @@ -18,10 +18,13 @@ use super::*; use self::error::Error; use assert_matches::assert_matches; -use consensus::BlockOrigin; use primitives::storage::well_known_keys; use sr_io::blake2_256; -use test_client::{self, runtime, AccountKeyring, TestClient, BlockBuilderExt, LocalExecutor, TestClientBuilder}; +use test_client::{ + prelude::*, + consensus::BlockOrigin, + runtime, +}; use substrate_executor::NativeExecutionDispatch; #[test] @@ -50,7 +53,9 @@ fn should_return_storage() { #[test] fn should_return_child_storage() { let core = tokio::runtime::Runtime::new().unwrap(); - let client = Arc::new(test_client::new()); + let client = Arc::new(test_client::TestClientBuilder::new() + .add_child_storage("test", "key", vec![42_u8]) + .build()); let genesis_hash = client.genesis_hash(); let client = State::new(client, Subscriptions::new(core.executor())); let child_key = StorageKey(well_known_keys::CHILD_STORAGE_KEY_PREFIX.iter().chain(b"test").cloned().collect()); @@ -125,7 +130,7 @@ fn should_send_initial_storage_changes_and_notifications() { { let api = State::new(Arc::new(test_client::new()), Subscriptions::new(remote)); - let alice_balance_key = blake2_256(&test_runtime::system::balance_of_key(AccountKeyring::Alice.into())); + let alice_balance_key = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Alice.into())); api.subscribe_storage(Default::default(), subscriber, Some(vec![ StorageKey(alice_balance_key.to_vec()), @@ -156,13 +161,6 @@ fn should_send_initial_storage_changes_and_notifications() { #[test] fn should_query_storage() { - type TestClient = test_client::client::Client< - test_client::Backend, - test_client::Executor, - runtime::Block, - runtime::RuntimeApi - >; - fn run_tests(client: Arc) { let core = tokio::runtime::Runtime::new().unwrap(); let api = State::new(client.clone(), Subscriptions::new(core.executor())); @@ -184,7 +182,7 @@ fn should_query_storage() { let block2_hash = add_block(1); let genesis_hash = client.genesis_hash(); - let alice_balance_key = blake2_256(&test_runtime::system::balance_of_key(AccountKeyring::Alice.into())); + let alice_balance_key = blake2_256(&runtime::system::balance_of_key(AccountKeyring::Alice.into())); let mut expected = vec![ StorageChangeSet { diff --git a/substrate/core/service/Cargo.toml b/substrate/core/service/Cargo.toml index 0a0cd91553..471594aad8 100644 --- a/substrate/core/service/Cargo.toml +++ b/substrate/core/service/Cargo.toml @@ -32,7 +32,7 @@ tel = { package = "substrate-telemetry", path = "../../core/telemetry" } offchain = { package = "substrate-offchain", path = "../../core/offchain" } [dev-dependencies] -substrate-test-client = { path = "../test-client" } +substrate-test-runtime-client = { path = "../test-runtime/client" } node-executor = { path = "../../node/executor" } node-primitives = { path = "../../node/primitives" } node-runtime = { path = "../../node/runtime" } diff --git a/substrate/core/service/src/components.rs b/substrate/core/service/src/components.rs index 30cb1cb764..0f5afb7772 100644 --- a/substrate/core/service/src/components.rs +++ b/substrate/core/service/src/components.rs @@ -634,7 +634,7 @@ impl Components for LightComponents { mod tests { use super::*; use consensus_common::BlockOrigin; - use substrate_test_client::{TestClient, AccountKeyring, runtime::Transfer, TestClientBuilder}; + use substrate_test_runtime_client::{prelude::*, runtime::Transfer}; #[test] fn should_remove_transactions_from_the_pool() { diff --git a/substrate/core/service/src/lib.rs b/substrate/core/service/src/lib.rs index b33608392d..92ddcd0b58 100644 --- a/substrate/core/service/src/lib.rs +++ b/substrate/core/service/src/lib.rs @@ -757,7 +757,7 @@ mod tests { use super::*; use consensus_common::SelectChain; use runtime_primitives::traits::BlindCheckable; - use substrate_test_client::{AccountKeyring, runtime::{Extrinsic, Transfer}, TestClientBuilder}; + use substrate_test_runtime_client::{prelude::*, runtime::{Extrinsic, Transfer}}; #[test] fn should_not_propagate_transactions_that_are_marked_as_such() { diff --git a/substrate/core/sr-api-macros/Cargo.toml b/substrate/core/sr-api-macros/Cargo.toml index a87b53b732..9f2145d958 100644 --- a/substrate/core/sr-api-macros/Cargo.toml +++ b/substrate/core/sr-api-macros/Cargo.toml @@ -16,7 +16,7 @@ proc-macro-crate = "0.1.3" [dev-dependencies] client = { package = "substrate-client", path = "../client" } -test_client = { package = "substrate-test-client", path = "../test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../test-runtime/client" } state_machine = { package = "substrate-state-machine", path = "../state-machine" } runtime_primitives = { package = "sr-primitives", path = "../sr-primitives" } sr-version = { path = "../sr-version" } diff --git a/substrate/core/sr-api-macros/tests/runtime_calls.rs b/substrate/core/sr-api-macros/tests/runtime_calls.rs index 0671c4f41d..6fa155437b 100644 --- a/substrate/core/sr-api-macros/tests/runtime_calls.rs +++ b/substrate/core/sr-api-macros/tests/runtime_calls.rs @@ -15,8 +15,8 @@ // along with Substrate. If not, see . use test_client::{ - AccountKeyring, runtime::{TestAPI, DecodeFails, Transfer, Header}, - NativeExecutor, LocalExecutor, TestClientBuilder + prelude::*, + runtime::{TestAPI, DecodeFails, Transfer, Header}, }; use runtime_primitives::{ generic::BlockId, @@ -190,4 +190,4 @@ fn record_proof_works() { "Core_execute_block", &block.encode(), ).expect("Executes block while using the proof backend"); -} \ No newline at end of file +} diff --git a/substrate/core/test-client/Cargo.toml b/substrate/core/test-client/Cargo.toml index 8de313f9cb..b459e02fbc 100644 --- a/substrate/core/test-client/Cargo.toml +++ b/substrate/core/test-client/Cargo.toml @@ -7,25 +7,13 @@ edition = "2018" [dependencies] client = { package = "substrate-client", path = "../client" } client-db = { package = "substrate-client-db", path = "../client/db", features = ["test-helpers"] } -futures = { version = "0.1.17" } -parity-codec = "3.5.1" -executor = { package = "substrate-executor", path = "../executor" } consensus = { package = "substrate-consensus-common", path = "../consensus/common" } -keyring = { package = "substrate-keyring", path = "../../core/keyring" } +executor = { package = "substrate-executor", path = "../executor" } +futures = { version = "0.1.27" } +hash-db = "0.12" +keyring = { package = "substrate-keyring", path = "../keyring" } +parity-codec = "3.5.1" primitives = { package = "substrate-primitives", path = "../primitives" } -state_machine = { package = "substrate-state-machine", path = "../state-machine" } -runtime = { package = "substrate-test-runtime", path = "../test-runtime", default-features = false } runtime_primitives = { package = "sr-primitives", path = "../sr-primitives" } +state_machine = { package = "substrate-state-machine", path = "../state-machine" } -[features] -default = [ - "include-wasm-blob", - "std", -] -std = [ - "runtime/std", -] -# If enabled, the WASM blob is added to the `GenesisConfig`. -include-wasm-blob = [ - "runtime/include-wasm-blob", -] \ No newline at end of file diff --git a/substrate/core/test-client/src/client_ext.rs b/substrate/core/test-client/src/client_ext.rs index d285bb726b..7d05b1f570 100644 --- a/substrate/core/test-client/src/client_ext.rs +++ b/substrate/core/test-client/src/client_ext.rs @@ -21,52 +21,55 @@ use consensus::{ ImportBlock, BlockImport, BlockOrigin, Error as ConsensusError, ForkChoiceStrategy, }; +use hash_db::Hasher; use runtime_primitives::Justification; +use runtime_primitives::traits::{Block as BlockT}; use runtime_primitives::generic::BlockId; use primitives::Blake2Hasher; -use runtime; use parity_codec::alloc::collections::hash_map::HashMap; /// Extension trait for a test client. -pub trait TestClient: Sized { +pub trait ClientExt: Sized { /// Import block to the chain. No finality. - fn import(&self, origin: BlockOrigin, block: runtime::Block) + fn import(&self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError>; /// Import block with justification, finalizes block. fn import_justified( &self, origin: BlockOrigin, - block: runtime::Block, + block: Block, justification: Justification ) -> Result<(), ConsensusError>; /// Finalize a block. fn finalize_block( &self, - id: BlockId, + id: BlockId, justification: Option, ) -> client::error::Result<()>; /// Returns hash of the genesis block. - fn genesis_hash(&self) -> runtime::Hash; + fn genesis_hash(&self) -> ::Hash; } -impl TestClient for Client +impl ClientExt for Client where - B: client::backend::Backend, - E: client::CallExecutor, - Self: BlockImport, + B: client::backend::Backend, + E: client::CallExecutor, + Self: BlockImport, + Block: BlockT::Out>, { - fn import(&self, origin: BlockOrigin, block: runtime::Block) + fn import(&self, origin: BlockOrigin, block: Block) -> Result<(), ConsensusError> { + let (header, extrinsics) = block.deconstruct(); let import = ImportBlock { origin, - header: block.header, + header, justification: None, post_digests: vec![], - body: Some(block.extrinsics), + body: Some(extrinsics), finalized: false, auxiliary: Vec::new(), fork_choice: ForkChoiceStrategy::LongestChain, @@ -78,15 +81,16 @@ impl TestClient for Client fn import_justified( &self, origin: BlockOrigin, - block: runtime::Block, + block: Block, justification: Justification, ) -> Result<(), ConsensusError> { + let (header, extrinsics) = block.deconstruct(); let import = ImportBlock { origin, - header: block.header, + header, justification: Some(justification), post_digests: vec![], - body: Some(block.extrinsics), + body: Some(extrinsics), finalized: true, auxiliary: Vec::new(), fork_choice: ForkChoiceStrategy::LongestChain, @@ -97,13 +101,13 @@ impl TestClient for Client fn finalize_block( &self, - id: BlockId, + id: BlockId, justification: Option, ) -> client::error::Result<()> { self.finalize_block(id, justification, true) } - fn genesis_hash(&self) -> runtime::Hash { - self.block_hash(0).unwrap().unwrap() + fn genesis_hash(&self) -> ::Hash { + self.block_hash(0.into()).unwrap().unwrap() } } diff --git a/substrate/core/test-client/src/lib.rs b/substrate/core/test-client/src/lib.rs index f39ad86b64..40fbd10d9e 100644 --- a/substrate/core/test-client/src/lib.rs +++ b/substrate/core/test-client/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2018-2019 Parity Technologies (UK) Ltd. +// Copyright 2019 Parity Technologies (UK) Ltd. // This file is part of Substrate. // Substrate is free software: you can redistribute it and/or modify @@ -19,61 +19,30 @@ #![warn(missing_docs)] pub mod client_ext; -#[cfg(feature = "include-wasm-blob")] -pub mod trait_tests; -mod block_builder_ext; -pub use client_ext::TestClient; -pub use block_builder_ext::BlockBuilderExt; pub use client::{ExecutionStrategies, blockchain, backend, self}; -pub use executor::{NativeExecutor, self}; -pub use runtime; +pub use client_db::{Backend, self}; +pub use client_ext::ClientExt; pub use consensus; +pub use executor::{NativeExecutor, self}; pub use keyring::{sr25519::Keyring as AuthorityKeyring, AccountKeyring}; +pub use primitives::Blake2Hasher; +pub use runtime_primitives::{StorageOverlay, ChildrenStorageOverlay}; +pub use state_machine::ExecutionStrategy; -use std::{sync::Arc, collections::HashMap}; +use std::sync::Arc; +use std::collections::HashMap; use futures::future::FutureResult; -use primitives::Blake2Hasher; +use hash_db::Hasher; use primitives::storage::well_known_keys; -use runtime_primitives::{StorageOverlay, ChildrenStorageOverlay}; use runtime_primitives::traits::{ - Block as BlockT, Header as HeaderT, Hash as HashT, NumberFor + Block as BlockT, NumberFor }; -use runtime::genesismap::{GenesisConfig, additional_storage_with_genesis}; -use state_machine::ExecutionStrategy; use client::LocalCallExecutor; -#[cfg(feature = "include-wasm-blob")] -mod local_executor { - #![allow(missing_docs)] - use runtime; - use executor::native_executor_instance; - // FIXME #1576 change the macro and pass in the `BlakeHasher` that dispatch needs from here instead - native_executor_instance!( - pub LocalExecutor, - runtime::api::dispatch, - runtime::native_version, - include_bytes!("../../test-runtime/wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm") - ); -} - -/// Native executor used for tests. -#[cfg(feature = "include-wasm-blob")] -pub use local_executor::LocalExecutor; - -/// Test client database backend. -pub type Backend = client_db::Backend; - -/// Test client executor. -#[cfg(feature = "include-wasm-blob")] -pub type Executor = client::LocalCallExecutor< - Backend, - executor::NativeExecutor, ->; - /// Test client light database backend. -pub type LightBackend = client::light::backend::Backend< - client_db::light::LightStorage, +pub type LightBackend = client::light::backend::Backend< + client_db::light::LightStorage, LightFetcher, Blake2Hasher, >; @@ -81,101 +50,85 @@ pub type LightBackend = client::light::backend::Backend< /// Test client light fetcher. pub struct LightFetcher; -/// Test client light executor. -#[cfg(feature = "include-wasm-blob")] -pub type LightExecutor = client::light::call_executor::RemoteOrLocalCallExecutor< - runtime::Block, - LightBackend, - client::light::call_executor::RemoteCallExecutor< - client::light::blockchain::Blockchain< - client_db::light::LightStorage, - LightFetcher - >, - LightFetcher - >, - client::LocalCallExecutor< - client::light::backend::Backend< - client_db::light::LightStorage, - LightFetcher, - Blake2Hasher - >, - executor::NativeExecutor - > ->; +/// A genesis storage initialisation trait. +pub trait GenesisInit: Default { + /// Construct genesis storage. + fn genesis_storage(&self) -> (StorageOverlay, ChildrenStorageOverlay); +} + +impl GenesisInit for () { + fn genesis_storage(&self) -> (StorageOverlay, ChildrenStorageOverlay) { + Default::default() + } +} /// A builder for creating a test client instance. -pub struct TestClientBuilder { +pub struct TestClientBuilder { execution_strategies: ExecutionStrategies, - genesis_extension: HashMap, Vec>, - support_changes_trie: bool, - backend: Arc, - _phantom: std::marker::PhantomData, + genesis_init: G, + child_storage_extension: HashMap, Vec<(Vec, Vec)>>, + backend: Arc, + _executor: std::marker::PhantomData, } -#[cfg(feature = "include-wasm-blob")] -impl TestClientBuilder where - B: backend::LocalBackend, +impl Default for TestClientBuilder< + Executor, + Backend, +> where + Block: BlockT::Out>, { - /// Create a new instance of the test client builder using the given backend. - pub fn new_with_backend(backend: Arc) -> Self { - TestClientBuilder { - execution_strategies: ExecutionStrategies::default(), - genesis_extension: HashMap::default(), - support_changes_trie: false, - backend, - _phantom: Default::default(), - } + fn default() -> Self { + Self::with_default_backend() } } -#[cfg(feature = "include-wasm-blob")] -impl TestClientBuilder { +impl TestClientBuilder< + Executor, + Backend, + G, +> where + Block: BlockT::Out>, +{ + /// Create new `TestClientBuilder` with default backend. + pub fn with_default_backend() -> Self { + let backend = Arc::new(Backend::new_test(std::u32::MAX, std::u64::MAX)); + Self::with_backend(backend) + } +} + +impl TestClientBuilder< + Executor, + Backend, + G, +> { /// Create a new instance of the test client builder. - pub fn new() -> Self { + pub fn with_backend(backend: Arc) -> Self { TestClientBuilder { - execution_strategies: ExecutionStrategies::default(), - genesis_extension: HashMap::default(), - support_changes_trie: false, - backend: Arc::new(Backend::new_test(std::u32::MAX, std::u64::MAX)), - _phantom: Default::default(), - } - } -} - -#[cfg(not(feature = "include-wasm-blob"))] -impl TestClientBuilder where - B: backend::LocalBackend, -{ - /// Create a new instance of the test client builder using the given backend. - pub fn new_with_backend(backend: Arc) -> Self { - TestClientBuilder { - execution_strategies: ExecutionStrategies::default(), - genesis_extension: HashMap::default(), - support_changes_trie: false, backend, - _phantom: Default::default(), - } - } -} - -#[cfg(not(feature = "include-wasm-blob"))] -impl TestClientBuilder { - /// Create a new instance of the test client builder. - pub fn new() -> Self { - TestClientBuilder { execution_strategies: ExecutionStrategies::default(), - genesis_extension: HashMap::default(), - support_changes_trie: false, - backend: Arc::new(Backend::new_test(std::u32::MAX, std::u64::MAX)), - _phantom: Default::default(), + child_storage_extension: Default::default(), + genesis_init: Default::default(), + _executor: Default::default(), } } -} -impl TestClientBuilder where - B: backend::LocalBackend, - E: executor::NativeExecutionDispatch -{ + /// Alter the genesis storage parameters. + pub fn genesis_init_mut(&mut self) -> &mut G { + &mut self.genesis_init + } + + /// Extend child storage + pub fn add_child_storage( + mut self, + key: impl AsRef<[u8]>, + child_key: impl AsRef<[u8]>, + value: impl AsRef<[u8]>, + ) -> Self { + let entry = self.child_storage_extension.entry(key.as_ref().to_vec()).or_insert_with(Vec::new); + entry.push((child_key.as_ref().to_vec(), value.as_ref().to_vec())); + self + } + /// Set the execution strategy that should be used by all contexts. pub fn set_execution_strategy( mut self, @@ -191,112 +144,79 @@ impl TestClientBuilder where self } - /// Set an extension of the genesis storage. - pub fn set_genesis_extension( - mut self, - extension: HashMap, Vec> - ) -> Self { - self.genesis_extension = extension; - self - } - - /// Enable/Disable changes trie support. - pub fn set_support_changes_trie(mut self, enable: bool) -> Self { - self.support_changes_trie = enable; - self - } - - /// Build the test client. - pub fn build(self) -> client::Client< - B, - client::LocalCallExecutor>, - runtime::Block, - runtime::RuntimeApi, - > { - self.build_with_longest_chain().0 - } - - /// Build the test client and longest chain as select chain. - pub fn build_with_longest_chain(self) -> ( + /// Build the test client with the given native executor. + pub fn build_with_executor( + self, + executor: Executor, + ) -> ( client::Client< - B, - client::LocalCallExecutor>, - runtime::Block, - runtime::RuntimeApi, + Backend, + Executor, + Block, + RuntimeApi, >, - client::LongestChain, - ) { - let executor = NativeExecutor::::new(None); - let executor = LocalCallExecutor::new(self.backend.clone(), executor); + client::LongestChain, + ) where + Executor: client::CallExecutor, + Backend: client::backend::Backend, + Block: BlockT::Out>, + { + + let storage = { + let mut storage = self.genesis_init.genesis_storage(); + + // Add some child storage keys. + for (key, value) in self.child_storage_extension { + storage.1.insert( + well_known_keys::CHILD_STORAGE_KEY_PREFIX.iter().cloned().chain(key).collect(), + value.into_iter().collect(), + ); + } + + storage + }; let client = client::Client::new( self.backend.clone(), executor, - genesis_storage(self.support_changes_trie, self.genesis_extension), - self.execution_strategies, + storage, + self.execution_strategies ).expect("Creates new client"); - #[allow(deprecated)] let longest_chain = client::LongestChain::new(self.backend); (client, longest_chain) } } -/// Creates new client instance used for tests. -#[cfg(feature = "include-wasm-blob")] -pub fn new() -> client::Client { - TestClientBuilder::new().build() -} +impl TestClientBuilder< + client::LocalCallExecutor>, + Backend, + G, +> { + /// Build the test client with the given native executor. + pub fn build_with_native_executor( + self, + executor: I, + ) -> ( + client::Client< + Backend, + client::LocalCallExecutor>, + Block, + RuntimeApi + >, + client::LongestChain, + ) where + I: Into>>, + E: executor::NativeExecutionDispatch, + Backend: client::backend::Backend, + Block: BlockT::Out>, + { + let executor = executor.into().unwrap_or_else(|| executor::NativeExecutor::new(None)); + let executor = LocalCallExecutor::new(self.backend.clone(), executor); -/// Creates new light client instance used for tests. -#[cfg(feature = "include-wasm-blob")] -pub fn new_light() -> client::Client { - let storage = client_db::light::LightStorage::new_test(); - let blockchain = Arc::new(client::light::blockchain::Blockchain::new(storage)); - let backend = Arc::new(LightBackend::new(blockchain.clone())); - let executor = NativeExecutor::new(None); - let fetcher = Arc::new(LightFetcher); - let remote_call_executor = client::light::call_executor::RemoteCallExecutor::new(blockchain.clone(), fetcher); - let local_call_executor = client::LocalCallExecutor::new(backend.clone(), executor); - let call_executor = LightExecutor::new(backend.clone(), remote_call_executor, local_call_executor); - client::Client::new(backend, call_executor, genesis_storage(false, Default::default()), Default::default()).unwrap() -} - -fn genesis_config(support_changes_trie: bool) -> GenesisConfig { - GenesisConfig::new(support_changes_trie, vec![ - AuthorityKeyring::Alice.into(), - AuthorityKeyring::Bob.into(), - AuthorityKeyring::Charlie.into(), - ], vec![ - AccountKeyring::Alice.into(), - AccountKeyring::Bob.into(), - AccountKeyring::Charlie.into(), - ], - 1000 - ) -} - -fn genesis_storage( - support_changes_trie: bool, - extension: HashMap, Vec> -) -> (StorageOverlay, ChildrenStorageOverlay) { - let mut storage = genesis_config(support_changes_trie).genesis_map(); - storage.extend(extension.into_iter()); - - let state_root = <<::Header as HeaderT>::Hashing as HashT>::trie_root( - storage.clone().into_iter() - ); - let block: runtime::Block = client::genesis::construct_genesis_block(state_root); - storage.extend(additional_storage_with_genesis(&block)); - - let mut child_storage = ChildrenStorageOverlay::default(); - child_storage.insert( - well_known_keys::CHILD_STORAGE_KEY_PREFIX.iter().chain(b"test").cloned().collect(), - vec![(b"key".to_vec(), vec![42_u8])].into_iter().collect() - ); - - (storage, child_storage) + self.build_with_executor(executor) + } } impl client::light::fetcher::Fetcher for LightFetcher { diff --git a/substrate/core/test-runtime/Cargo.toml b/substrate/core/test-runtime/Cargo.toml index 4244dcc58b..b0815c513b 100644 --- a/substrate/core/test-runtime/Cargo.toml +++ b/substrate/core/test-runtime/Cargo.toml @@ -29,7 +29,7 @@ consensus_authorities = { package = "substrate-consensus-authorities", path = ". [dev-dependencies] substrate-executor = { path = "../executor" } -substrate-test-client = { path = "../test-client" } +substrate-test-runtime-client = { path = "./client" } [features] default = [ diff --git a/substrate/core/test-runtime/client/Cargo.toml b/substrate/core/test-runtime/client/Cargo.toml new file mode 100644 index 0000000000..9ddeb7ba25 --- /dev/null +++ b/substrate/core/test-runtime/client/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "substrate-test-runtime-client" +version = "2.0.0" +authors = ["Parity Technologies "] +edition = "2018" + +[dependencies] +generic-test-client = { package = "substrate-test-client", path = "../../test-client" } +primitives = { package = "substrate-primitives", path = "../../primitives" } +runtime = { package = "substrate-test-runtime", path = "../../test-runtime", default-features = false } +runtime_primitives = { package = "sr-primitives", path = "../../sr-primitives" } + +[features] +default = [ + "std", + "runtime/include-wasm-blob", +] +std = [ + "runtime/std", +] diff --git a/substrate/core/test-client/src/block_builder_ext.rs b/substrate/core/test-runtime/client/src/block_builder_ext.rs similarity index 93% rename from substrate/core/test-client/src/block_builder_ext.rs rename to substrate/core/test-runtime/client/src/block_builder_ext.rs index 15861ce3d0..ae95ecd7ab 100644 --- a/substrate/core/test-client/src/block_builder_ext.rs +++ b/substrate/core/test-runtime/client/src/block_builder_ext.rs @@ -16,10 +16,10 @@ //! Block Builder extensions for tests. -use client; use runtime; use runtime_primitives::traits::ProvideRuntimeApi; -use client::block_builder::api::BlockBuilder; +use generic_test_client::client; +use generic_test_client::client::block_builder::api::BlockBuilder; /// Extension trait for test block builder. pub trait BlockBuilderExt { diff --git a/substrate/core/test-runtime/client/src/lib.rs b/substrate/core/test-runtime/client/src/lib.rs new file mode 100644 index 0000000000..ee1ad24214 --- /dev/null +++ b/substrate/core/test-runtime/client/src/lib.rs @@ -0,0 +1,208 @@ +// Copyright 2018-2019 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 . + +//! Client testing utilities. + +#![warn(missing_docs)] + +pub mod trait_tests; + +mod block_builder_ext; + +pub use block_builder_ext::BlockBuilderExt; +pub use generic_test_client::*; +pub use runtime; + +use runtime::genesismap::{GenesisConfig, additional_storage_with_genesis}; +use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, Hash as HashT}; + +/// A prelude to import in tests. +pub mod prelude { + // Trait extensions + pub use super::{BlockBuilderExt, DefaultTestClientBuilderExt, TestClientBuilderExt, ClientExt}; + // Client structs + pub use super::{ + TestClient, TestClientBuilder, Backend, LightBackend, + Executor, LightExecutor, LocalExecutor, NativeExecutor, + }; + // Keyring + pub use super::{AccountKeyring, AuthorityKeyring}; +} + +mod local_executor { + #![allow(missing_docs)] + use runtime; + use crate::executor::native_executor_instance; + // FIXME #1576 change the macro and pass in the `BlakeHasher` that dispatch needs from here instead + native_executor_instance!( + pub LocalExecutor, + runtime::api::dispatch, + runtime::native_version, + include_bytes!("../../wasm/target/wasm32-unknown-unknown/release/substrate_test_runtime.compact.wasm") + ); +} + +/// Native executor used for tests. +pub use local_executor::LocalExecutor; + +/// Test client database backend. +pub type Backend = generic_test_client::Backend; + +/// Test client executor. +pub type Executor = client::LocalCallExecutor< + Backend, + NativeExecutor, +>; + +/// Test client light database backend. +pub type LightBackend = generic_test_client::LightBackend; + +/// Test client light executor. +pub type LightExecutor = client::light::call_executor::RemoteOrLocalCallExecutor< + runtime::Block, + LightBackend, + client::light::call_executor::RemoteCallExecutor< + client::light::blockchain::Blockchain< + client_db::light::LightStorage, + LightFetcher + >, + LightFetcher + >, + client::LocalCallExecutor< + client::light::backend::Backend< + client_db::light::LightStorage, + LightFetcher, + Blake2Hasher + >, + NativeExecutor + > +>; + +/// Parameters of test-client builder with test-runtime. +#[derive(Default)] +pub struct GenesisParameters { + support_changes_trie: bool, +} + +impl generic_test_client::GenesisInit for GenesisParameters { + fn genesis_storage(&self) -> (StorageOverlay, ChildrenStorageOverlay) { + let mut storage = genesis_config(self.support_changes_trie).genesis_map(); + + let state_root = <<::Header as HeaderT>::Hashing as HashT>::trie_root( + storage.clone().into_iter() + ); + let block: runtime::Block = client::genesis::construct_genesis_block(state_root); + storage.extend(additional_storage_with_genesis(&block)); + + (storage, Default::default()) + } +} + +/// A `TestClient` with `test-runtime` builder. +pub type TestClientBuilder = generic_test_client::TestClientBuilder; + +/// Test client type with `LocalExecutor` and generic Backend. +pub type Client = client::Client< + B, + client::LocalCallExecutor>, + runtime::Block, + runtime::RuntimeApi, +>; + +/// A test client with default backend. +pub type TestClient = Client; + +/// A `TestClientBuilder` with default backend and executor. +pub trait DefaultTestClientBuilderExt: Sized { + /// Create new `TestClientBuilder` + fn new() -> Self; +} + +impl DefaultTestClientBuilderExt for TestClientBuilder< + Executor, + Backend, +> { + fn new() -> Self { + Self::with_default_backend() + } +} + +/// A `test-runtime` extensions to `TestClientBuilder`. +pub trait TestClientBuilderExt: Sized { + /// Enable or disable support for changes trie in genesis. + fn set_support_changes_trie(self, support_changes_trie: bool) -> Self; + + /// Build the test client. + fn build(self) -> Client { + self.build_with_longest_chain().0 + } + + /// Build the test client and longest chain selector. + fn build_with_longest_chain(self) -> (Client, client::LongestChain); +} + +impl TestClientBuilderExt for TestClientBuilder< + client::LocalCallExecutor>, + B +> where + B: client::backend::Backend, +{ + fn set_support_changes_trie(mut self, support_changes_trie: bool) -> Self { + self.genesis_init_mut().support_changes_trie = support_changes_trie; + self + } + + fn build_with_longest_chain(self) -> (Client, client::LongestChain) { + self.build_with_native_executor(None) + } +} + +fn genesis_config(support_changes_trie: bool) -> GenesisConfig { + GenesisConfig::new(support_changes_trie, vec![ + AuthorityKeyring::Alice.into(), + AuthorityKeyring::Bob.into(), + AuthorityKeyring::Charlie.into(), + ], vec![ + AccountKeyring::Alice.into(), + AccountKeyring::Bob.into(), + AccountKeyring::Charlie.into(), + ], + 1000 + ) +} + +/// Creates new client instance used for tests. +pub fn new() -> Client { + TestClientBuilder::new().build() +} + +/// Creates new light client instance used for tests. +pub fn new_light() -> client::Client { + use std::sync::Arc; + + let storage = client_db::light::LightStorage::new_test(); + let blockchain = Arc::new(client::light::blockchain::Blockchain::new(storage)); + let backend = Arc::new(LightBackend::new(blockchain.clone())); + let executor = NativeExecutor::new(None); + let fetcher = Arc::new(LightFetcher); + let remote_call_executor = client::light::call_executor::RemoteCallExecutor::new(blockchain.clone(), fetcher); + let local_call_executor = client::LocalCallExecutor::new(backend.clone(), executor); + let call_executor = LightExecutor::new(backend.clone(), remote_call_executor, local_call_executor); + + TestClientBuilder::with_backend(backend) + .build_with_executor(call_executor) + .0 +} diff --git a/substrate/core/test-client/src/trait_tests.rs b/substrate/core/test-runtime/client/src/trait_tests.rs similarity index 96% rename from substrate/core/test-client/src/trait_tests.rs rename to substrate/core/test-runtime/client/src/trait_tests.rs index f39d85a1ca..3d013e3e74 100644 --- a/substrate/core/test-client/src/trait_tests.rs +++ b/substrate/core/test-runtime/client/src/trait_tests.rs @@ -20,15 +20,16 @@ #![allow(missing_docs)] use std::sync::Arc; -use consensus::BlockOrigin; -use primitives::Blake2Hasher; -use crate::{TestClient, AccountKeyring}; -use runtime_primitives::traits::Block as BlockT; + use crate::backend; +use crate::block_builder_ext::BlockBuilderExt; use crate::blockchain::{Backend as BlockChainBackendT, HeaderBackend}; -use crate::{BlockBuilderExt, TestClientBuilder}; +use crate::{AccountKeyring, ClientExt, TestClientBuilder, TestClientBuilderExt}; +use generic_test_client::consensus::BlockOrigin; +use primitives::Blake2Hasher; use runtime::{self, Transfer}; use runtime_primitives::generic::BlockId; +use runtime_primitives::traits::Block as BlockT; /// helper to test the `leaves` implementation for various backends pub fn test_leaves_for_backend(backend: Arc) where @@ -40,7 +41,7 @@ pub fn test_leaves_for_backend(backend: Arc) where // B2 -> C3 // A1 -> D2 - let client = TestClientBuilder::new_with_backend(backend.clone()).build(); + let client = TestClientBuilder::with_backend(backend.clone()).build(); let blockchain = backend.blockchain(); let genesis_hash = client.info().chain.genesis_hash; @@ -156,7 +157,7 @@ pub fn test_children_for_backend(backend: Arc) where // B2 -> C3 // A1 -> D2 - let client = TestClientBuilder::new_with_backend(backend.clone()).build(); + let client = TestClientBuilder::with_backend(backend.clone()).build(); let blockchain = backend.blockchain(); // G -> A1 @@ -246,7 +247,7 @@ pub fn test_blockchain_query_by_number_gets_canonical(backend: Arc B2 -> B3 -> B4 // B2 -> C3 // A1 -> D2 - let client = TestClientBuilder::new_with_backend(backend.clone()).build(); + let client = TestClientBuilder::with_backend(backend.clone()).build(); let blockchain = backend.blockchain(); // G -> A1 diff --git a/substrate/core/test-runtime/src/system.rs b/substrate/core/test-runtime/src/system.rs index 491ca0db36..57a5cb9ea2 100644 --- a/substrate/core/test-runtime/src/system.rs +++ b/substrate/core/test-runtime/src/system.rs @@ -311,7 +311,7 @@ mod tests { use runtime_io::{with_externalities, twox_128, blake2_256, TestExternalities}; use parity_codec::{Joiner, KeyedVec}; - use substrate_test_client::{AuthorityKeyring, AccountKeyring}; + use substrate_test_runtime_client::{AuthorityKeyring, AccountKeyring}; use crate::{Header, Transfer}; use primitives::{Blake2Hasher, map}; use primitives::storage::well_known_keys; diff --git a/substrate/core/transaction-pool/Cargo.toml b/substrate/core/transaction-pool/Cargo.toml index ed2cd27833..2bcad4d4d6 100644 --- a/substrate/core/transaction-pool/Cargo.toml +++ b/substrate/core/transaction-pool/Cargo.toml @@ -17,4 +17,4 @@ txpool = { package = "substrate-transaction-graph", path = "./graph" } [dev-dependencies] keyring = { package = "substrate-keyring", path = "../../core/keyring" } -test_client = { package = "substrate-test-client", path = "../../core/test-client" } +test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client" } diff --git a/substrate/node/executor/Cargo.toml b/substrate/node/executor/Cargo.toml index 89b448ec90..ee126d19ea 100644 --- a/substrate/node/executor/Cargo.toml +++ b/substrate/node/executor/Cargo.toml @@ -17,6 +17,7 @@ node-primitives = { path = "../primitives" } node-runtime = { path = "../runtime" } [dev-dependencies] +test-client = { package = "substrate-test-client", path = "../../core/test-client" } keyring = { package = "substrate-keyring", path = "../../core/keyring" } runtime_primitives = { package = "sr-primitives", path = "../../core/sr-primitives" } runtime_support = { package = "srml-support", path = "../../srml/support" } diff --git a/substrate/node/executor/src/lib.rs b/substrate/node/executor/src/lib.rs index 05dc424d49..aabee6805e 100644 --- a/substrate/node/executor/src/lib.rs +++ b/substrate/node/executor/src/lib.rs @@ -895,6 +895,21 @@ mod tests { assert!(t.storage_changes_root(GENESIS_HASH.into()).unwrap().is_some()); } + #[test] + fn should_import_block_with_test_client() { + use test_client::{ClientExt, TestClientBuilder, consensus::BlockOrigin}; + + let client = TestClientBuilder::default() + .build_with_native_executor::(executor()) + .0; + + let block1 = changes_trie_block(); + let block_data = block1.0; + let block = Block::decode(&mut &block_data[..]).unwrap(); + + client.import(BlockOrigin::Own, block).unwrap(); + } + #[cfg(feature = "benchmarks")] mod benches { use super::*;