mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Make test-client generic over runtime (#2824)
* Make test-client generic over runtime. * Make sure genesis storage is constructed correctly. * Use prelude in tests. * Add an example of how to use test-client with node/runtime. * Bump version. * Rename test-clients.
This commit is contained in:
committed by
Bastian Köcher
parent
cae324598f
commit
ad2d958248
Generated
+27
-18
@@ -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",
|
||||
]
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -473,7 +473,7 @@ pub fn check_execution_proof<Header, E, H>(
|
||||
#[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<u8>, Vec<u8>) {
|
||||
let remote_block_id = BlockId::Number(at);
|
||||
let remote_root = remote_client.state_at(&remote_block_id)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 = []
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -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<Block> for TestLink {}
|
||||
|
||||
fn prepare_good_block() -> (client::Client<
|
||||
test_client::Backend,
|
||||
test_client::Executor,
|
||||
Block,
|
||||
test_client::runtime::RuntimeApi,
|
||||
>, Hash, u64, PeerId, IncomingBlock<Block>) {
|
||||
fn prepare_good_block() -> (TestClient, Hash, u64, PeerId, IncomingBlock<Block>) {
|
||||
let client = test_client::new();
|
||||
let block = client.new_block(Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::File, block).unwrap();
|
||||
|
||||
@@ -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 = []
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<TestClient>) {
|
||||
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 {
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -634,7 +634,7 @@ impl<Factory: ServiceFactory> Components for LightComponents<Factory> {
|
||||
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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
]
|
||||
@@ -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<Block: BlockT>: 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<runtime::Block>,
|
||||
id: BlockId<Block>,
|
||||
justification: Option<Justification>,
|
||||
) -> client::error::Result<()>;
|
||||
|
||||
/// Returns hash of the genesis block.
|
||||
fn genesis_hash(&self) -> runtime::Hash;
|
||||
fn genesis_hash(&self) -> <Block as BlockT>::Hash;
|
||||
}
|
||||
|
||||
impl<B, E, RA> TestClient for Client<B, E, runtime::Block, RA>
|
||||
impl<B, E, RA, Block> ClientExt<Block> for Client<B, E, Block, RA>
|
||||
where
|
||||
B: client::backend::Backend<runtime::Block, Blake2Hasher>,
|
||||
E: client::CallExecutor<runtime::Block, Blake2Hasher>,
|
||||
Self: BlockImport<runtime::Block, Error=ConsensusError>,
|
||||
B: client::backend::Backend<Block, Blake2Hasher>,
|
||||
E: client::CallExecutor<Block, Blake2Hasher>,
|
||||
Self: BlockImport<Block, Error=ConsensusError>,
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::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<B, E, RA> TestClient for Client<B, E, runtime::Block, RA>
|
||||
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<B, E, RA> TestClient for Client<B, E, runtime::Block, RA>
|
||||
|
||||
fn finalize_block(
|
||||
&self,
|
||||
id: BlockId<runtime::Block>,
|
||||
id: BlockId<Block>,
|
||||
justification: Option<Justification>,
|
||||
) -> 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) -> <Block as BlockT>::Hash {
|
||||
self.block_hash(0.into()).unwrap().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<runtime::Block>;
|
||||
|
||||
/// Test client executor.
|
||||
#[cfg(feature = "include-wasm-blob")]
|
||||
pub type Executor = client::LocalCallExecutor<
|
||||
Backend,
|
||||
executor::NativeExecutor<LocalExecutor>,
|
||||
>;
|
||||
|
||||
/// Test client light database backend.
|
||||
pub type LightBackend = client::light::backend::Backend<
|
||||
client_db::light::LightStorage<runtime::Block>,
|
||||
pub type LightBackend<Block> = client::light::backend::Backend<
|
||||
client_db::light::LightStorage<Block>,
|
||||
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<runtime::Block>,
|
||||
LightFetcher
|
||||
>,
|
||||
LightFetcher
|
||||
>,
|
||||
client::LocalCallExecutor<
|
||||
client::light::backend::Backend<
|
||||
client_db::light::LightStorage<runtime::Block>,
|
||||
LightFetcher,
|
||||
Blake2Hasher
|
||||
>,
|
||||
executor::NativeExecutor<LocalExecutor>
|
||||
>
|
||||
>;
|
||||
/// 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<E, B> {
|
||||
pub struct TestClientBuilder<Executor, Backend, G: GenesisInit = ()> {
|
||||
execution_strategies: ExecutionStrategies,
|
||||
genesis_extension: HashMap<Vec<u8>, Vec<u8>>,
|
||||
support_changes_trie: bool,
|
||||
backend: Arc<B>,
|
||||
_phantom: std::marker::PhantomData<E>,
|
||||
genesis_init: G,
|
||||
child_storage_extension: HashMap<Vec<u8>, Vec<(Vec<u8>, Vec<u8>)>>,
|
||||
backend: Arc<Backend>,
|
||||
_executor: std::marker::PhantomData<Executor>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "include-wasm-blob")]
|
||||
impl<B> TestClientBuilder<LocalExecutor, B> where
|
||||
B: backend::LocalBackend<runtime::Block, Blake2Hasher>,
|
||||
impl<Block, Executor> Default for TestClientBuilder<
|
||||
Executor,
|
||||
Backend<Block>,
|
||||
> where
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::Out>,
|
||||
{
|
||||
/// Create a new instance of the test client builder using the given backend.
|
||||
pub fn new_with_backend(backend: Arc<B>) -> 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<LocalExecutor, Backend> {
|
||||
impl<Block, Executor, G: GenesisInit> TestClientBuilder<
|
||||
Executor,
|
||||
Backend<Block>,
|
||||
G,
|
||||
> where
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::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<Executor, Backend, G: GenesisInit> TestClientBuilder<
|
||||
Executor,
|
||||
Backend,
|
||||
G,
|
||||
> {
|
||||
/// Create a new instance of the test client builder.
|
||||
pub fn new() -> Self {
|
||||
pub fn with_backend(backend: Arc<Backend>) -> 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<E, B> TestClientBuilder<E, B> where
|
||||
B: backend::LocalBackend<runtime::Block, Blake2Hasher>,
|
||||
{
|
||||
/// Create a new instance of the test client builder using the given backend.
|
||||
pub fn new_with_backend(backend: Arc<B>) -> 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<E> TestClientBuilder<E, Backend> {
|
||||
/// 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<E, B> TestClientBuilder<E, B> where
|
||||
B: backend::LocalBackend<runtime::Block, Blake2Hasher>,
|
||||
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<E, B> TestClientBuilder<E, B> where
|
||||
self
|
||||
}
|
||||
|
||||
/// Set an extension of the genesis storage.
|
||||
pub fn set_genesis_extension(
|
||||
mut self,
|
||||
extension: HashMap<Vec<u8>, Vec<u8>>
|
||||
) -> 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<B, executor::NativeExecutor<E>>,
|
||||
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<Block, RuntimeApi>(
|
||||
self,
|
||||
executor: Executor,
|
||||
) -> (
|
||||
client::Client<
|
||||
B,
|
||||
client::LocalCallExecutor<B, executor::NativeExecutor<E>>,
|
||||
runtime::Block,
|
||||
runtime::RuntimeApi,
|
||||
Backend,
|
||||
Executor,
|
||||
Block,
|
||||
RuntimeApi,
|
||||
>,
|
||||
client::LongestChain<B, runtime::Block>,
|
||||
) {
|
||||
let executor = NativeExecutor::<E>::new(None);
|
||||
let executor = LocalCallExecutor::new(self.backend.clone(), executor);
|
||||
client::LongestChain<Backend, Block>,
|
||||
) where
|
||||
Executor: client::CallExecutor<Block, Blake2Hasher>,
|
||||
Backend: client::backend::Backend<Block, Blake2Hasher>,
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::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<Backend, Executor, runtime::Block, runtime::RuntimeApi> {
|
||||
TestClientBuilder::new().build()
|
||||
}
|
||||
impl<E, Backend, G: GenesisInit> TestClientBuilder<
|
||||
client::LocalCallExecutor<Backend, executor::NativeExecutor<E>>,
|
||||
Backend,
|
||||
G,
|
||||
> {
|
||||
/// Build the test client with the given native executor.
|
||||
pub fn build_with_native_executor<Block, RuntimeApi, I>(
|
||||
self,
|
||||
executor: I,
|
||||
) -> (
|
||||
client::Client<
|
||||
Backend,
|
||||
client::LocalCallExecutor<Backend, executor::NativeExecutor<E>>,
|
||||
Block,
|
||||
RuntimeApi
|
||||
>,
|
||||
client::LongestChain<Backend, Block>,
|
||||
) where
|
||||
I: Into<Option<executor::NativeExecutor<E>>>,
|
||||
E: executor::NativeExecutionDispatch,
|
||||
Backend: client::backend::Backend<Block, Blake2Hasher>,
|
||||
Block: BlockT<Hash=<Blake2Hasher as Hasher>::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<LightBackend, LightExecutor, runtime::Block, runtime::RuntimeApi> {
|
||||
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<u8>, Vec<u8>>
|
||||
) -> (StorageOverlay, ChildrenStorageOverlay) {
|
||||
let mut storage = genesis_config(support_changes_trie).genesis_map();
|
||||
storage.extend(extension.into_iter());
|
||||
|
||||
let state_root = <<<runtime::Block as BlockT>::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<Block: BlockT> client::light::fetcher::Fetcher<Block> for LightFetcher {
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "substrate-test-runtime-client"
|
||||
version = "2.0.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
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",
|
||||
]
|
||||
+2
-2
@@ -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 {
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! 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<runtime::Block>;
|
||||
|
||||
/// Test client executor.
|
||||
pub type Executor = client::LocalCallExecutor<
|
||||
Backend,
|
||||
NativeExecutor<LocalExecutor>,
|
||||
>;
|
||||
|
||||
/// Test client light database backend.
|
||||
pub type LightBackend = generic_test_client::LightBackend<runtime::Block>;
|
||||
|
||||
/// 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<runtime::Block>,
|
||||
LightFetcher
|
||||
>,
|
||||
LightFetcher
|
||||
>,
|
||||
client::LocalCallExecutor<
|
||||
client::light::backend::Backend<
|
||||
client_db::light::LightStorage<runtime::Block>,
|
||||
LightFetcher,
|
||||
Blake2Hasher
|
||||
>,
|
||||
NativeExecutor<LocalExecutor>
|
||||
>
|
||||
>;
|
||||
|
||||
/// 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 = <<<runtime::Block as BlockT>::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<E, B> = generic_test_client::TestClientBuilder<E, B, GenesisParameters>;
|
||||
|
||||
/// Test client type with `LocalExecutor` and generic Backend.
|
||||
pub type Client<B> = client::Client<
|
||||
B,
|
||||
client::LocalCallExecutor<B, executor::NativeExecutor<LocalExecutor>>,
|
||||
runtime::Block,
|
||||
runtime::RuntimeApi,
|
||||
>;
|
||||
|
||||
/// A test client with default backend.
|
||||
pub type TestClient = Client<Backend>;
|
||||
|
||||
/// 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<B>: 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<B> {
|
||||
self.build_with_longest_chain().0
|
||||
}
|
||||
|
||||
/// Build the test client and longest chain selector.
|
||||
fn build_with_longest_chain(self) -> (Client<B>, client::LongestChain<B, runtime::Block>);
|
||||
}
|
||||
|
||||
impl<B> TestClientBuilderExt<B> for TestClientBuilder<
|
||||
client::LocalCallExecutor<B, executor::NativeExecutor<LocalExecutor>>,
|
||||
B
|
||||
> where
|
||||
B: client::backend::Backend<runtime::Block, Blake2Hasher>,
|
||||
{
|
||||
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<B>, client::LongestChain<B, runtime::Block>) {
|
||||
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<Backend> {
|
||||
TestClientBuilder::new().build()
|
||||
}
|
||||
|
||||
/// Creates new light client instance used for tests.
|
||||
pub fn new_light() -> client::Client<LightBackend, LightExecutor, runtime::Block, runtime::RuntimeApi> {
|
||||
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
|
||||
}
|
||||
+9
-8
@@ -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<B: 'static>(backend: Arc<B>) where
|
||||
@@ -40,7 +41,7 @@ pub fn test_leaves_for_backend<B: 'static>(backend: Arc<B>) 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<B: 'static>(backend: Arc<B>) 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<B: 'static>(backend: Arc<B
|
||||
// A1 -> 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
|
||||
@@ -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;
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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::<Block, node_runtime::RuntimeApi, _>(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::*;
|
||||
|
||||
Reference in New Issue
Block a user