Build LongestChain with TestClientBuilder (#2792)

* Switch to `TestClientBuilder` and support generating LongestChain

* Make sure test-client works without the wasm blob

* Use `TestClientBuilder` in more places
This commit is contained in:
Bastian Köcher
2019-06-05 13:45:18 +02:00
committed by GitHub
parent 22a00a3353
commit 4f888f34d3
10 changed files with 321 additions and 334 deletions
+173 -170
View File
File diff suppressed because it is too large Load Diff
+30 -50
View File
@@ -1728,11 +1728,11 @@ pub(crate) mod tests {
use primitives::blake2_256; use primitives::blake2_256;
use runtime_primitives::traits::DigestItem as DigestItemT; use runtime_primitives::traits::DigestItem as DigestItemT;
use runtime_primitives::generic::DigestItem; use runtime_primitives::generic::DigestItem;
use test_client::{self, TestClient, AccountKeyring};
use consensus::{BlockOrigin, SelectChain}; use consensus::{BlockOrigin, SelectChain};
use test_client::client::backend::Backend as TestBackend; use test_client::{
use test_client::BlockBuilderExt; TestClient, AccountKeyring, client::backend::Backend as TestBackend, TestClientBuilder,
use test_client::runtime::{self, Block, Transfer, RuntimeApi, TestAPI}; BlockBuilderExt, runtime::{self, Block, Transfer, RuntimeApi, TestAPI}
};
/// Returns tuple, consisting of: /// Returns tuple, consisting of:
/// 1) test client pre-filled with blocks changing balances; /// 1) test client pre-filled with blocks changing balances;
@@ -1753,7 +1753,7 @@ pub(crate) mod tests {
// prepare client ang import blocks // prepare client ang import blocks
let mut local_roots = Vec::new(); let mut local_roots = Vec::new();
let remote_client = test_client::new_with_changes_trie(); let remote_client = TestClientBuilder::new().set_support_changes_trie(true).build();
let mut nonces: HashMap<_, u64> = Default::default(); let mut nonces: HashMap<_, u64> = Default::default();
for (i, block_transfers) in blocks_transfers.into_iter().enumerate() { for (i, block_transfers) in blocks_transfers.into_iter().enumerate() {
let mut builder = remote_client.new_block(Default::default()).unwrap(); let mut builder = remote_client.new_block(Default::default()).unwrap();
@@ -1854,7 +1854,10 @@ pub(crate) mod tests {
client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap(); client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
assert_eq!(client.info().chain.best_number, 1); assert_eq!(client.info().chain.best_number, 1);
assert!(client.state_at(&BlockId::Number(1)).unwrap().pairs() != client.state_at(&BlockId::Number(0)).unwrap().pairs()); assert_ne!(
client.state_at(&BlockId::Number(1)).unwrap().pairs(),
client.state_at(&BlockId::Number(0)).unwrap().pairs()
);
assert_eq!( assert_eq!(
client.runtime_api().balance_of( client.runtime_api().balance_of(
&BlockId::Number(client.info().chain.best_number), &BlockId::Number(client.info().chain.best_number),
@@ -1894,7 +1897,10 @@ pub(crate) mod tests {
client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap(); client.import(BlockOrigin::Own, builder.bake().unwrap()).unwrap();
assert_eq!(client.info().chain.best_number, 1); assert_eq!(client.info().chain.best_number, 1);
assert!(client.state_at(&BlockId::Number(1)).unwrap().pairs() != client.state_at(&BlockId::Number(0)).unwrap().pairs()); assert_ne!(
client.state_at(&BlockId::Number(1)).unwrap().pairs(),
client.state_at(&BlockId::Number(0)).unwrap().pairs()
);
assert_eq!(client.body(&BlockId::Number(1)).unwrap().unwrap().len(), 1) assert_eq!(client.body(&BlockId::Number(1)).unwrap().unwrap().len(), 1)
} }
@@ -1903,18 +1909,14 @@ pub(crate) mod tests {
// block tree: // block tree:
// G // G
let client = test_client::new(); let (client, longest_chain_select) = TestClientBuilder::new().build_with_longest_chain();
let genesis_hash = client.info().chain.genesis_hash; let genesis_hash = client.info().chain.genesis_hash;
#[allow(deprecated)]
let longest_chain_select = test_client::client::LongestChain::new( assert_eq!(
client.backend().clone(), genesis_hash.clone(),
client.import_lock() longest_chain_select.finality_target(genesis_hash.clone(), None).unwrap().unwrap()
); );
assert_eq!(genesis_hash.clone(), longest_chain_select.finality_target(
genesis_hash.clone(), None).unwrap().unwrap());
} }
#[test] #[test]
@@ -1922,18 +1924,14 @@ pub(crate) mod tests {
// block tree: // block tree:
// G // G
let client = test_client::new(); let (client, longest_chain_select) = TestClientBuilder::new().build_with_longest_chain();
let uninserted_block = client.new_block(Default::default()).unwrap().bake().unwrap(); let uninserted_block = client.new_block(Default::default()).unwrap().bake().unwrap();
#[allow(deprecated)]
let backend = client.backend().as_in_memory();
#[allow(deprecated)]
let longest_chain_select = test_client::client::LongestChain::new(
Arc::new(backend),
client.import_lock());
assert_eq!(None, longest_chain_select.finality_target( assert_eq!(
uninserted_block.hash().clone(), None).unwrap()); None,
longest_chain_select.finality_target(uninserted_block.hash().clone(), None).unwrap()
);
} }
#[test] #[test]
@@ -2052,7 +2050,7 @@ pub(crate) mod tests {
// block tree: // block tree:
// G -> A1 -> A2 // G -> A1 -> A2
let client = test_client::new(); let (client, longest_chain_select) = TestClientBuilder::new().build_with_longest_chain();
// G -> A1 // G -> A1
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap(); let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
@@ -2063,17 +2061,10 @@ pub(crate) mod tests {
client.import(BlockOrigin::Own, a2.clone()).unwrap(); client.import(BlockOrigin::Own, a2.clone()).unwrap();
let genesis_hash = client.info().chain.genesis_hash; let genesis_hash = client.info().chain.genesis_hash;
#[allow(deprecated)]
let longest_chain_select = test_client::client::LongestChain::new(
Arc::new(client.backend().as_in_memory()),
client.import_lock());
assert_eq!(a2.hash(), longest_chain_select.finality_target( assert_eq!(a2.hash(), longest_chain_select.finality_target(genesis_hash, None).unwrap().unwrap());
genesis_hash, None).unwrap().unwrap()); assert_eq!(a2.hash(), longest_chain_select.finality_target(a1.hash(), None).unwrap().unwrap());
assert_eq!(a2.hash(), longest_chain_select.finality_target( assert_eq!(a2.hash(), longest_chain_select.finality_target(a2.hash(), None).unwrap().unwrap());
a1.hash(), None).unwrap().unwrap());
assert_eq!(a2.hash(), longest_chain_select.finality_target(
a2.hash(), None).unwrap().unwrap());
} }
#[test] #[test]
@@ -2083,7 +2074,7 @@ pub(crate) mod tests {
// A1 -> B2 -> B3 -> B4 // A1 -> B2 -> B3 -> B4
// B2 -> C3 // B2 -> C3
// A1 -> D2 // A1 -> D2
let client = test_client::new(); let (client, longest_chain_select) = TestClientBuilder::new().build_with_longest_chain();
// G -> A1 // G -> A1
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap(); let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
@@ -2152,11 +2143,6 @@ pub(crate) mod tests {
assert_eq!(client.info().chain.best_hash, a5.hash()); assert_eq!(client.info().chain.best_hash, a5.hash());
let genesis_hash = client.info().chain.genesis_hash; let genesis_hash = client.info().chain.genesis_hash;
#[allow(deprecated)]
let longest_chain_select = test_client::client::LongestChain::new(
Arc::new(client.backend().as_in_memory()),
client.import_lock());
let leaves = longest_chain_select.leaves().unwrap(); let leaves = longest_chain_select.leaves().unwrap();
assert!(leaves.contains(&a5.hash())); assert!(leaves.contains(&a5.hash()));
@@ -2372,7 +2358,7 @@ pub(crate) mod tests {
// block tree: // block tree:
// G -> A1 -> A2 // G -> A1 -> A2
let client = test_client::new(); let (client, longest_chain_select) = TestClientBuilder::new().build_with_longest_chain();
// G -> A1 // G -> A1
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap(); let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
@@ -2383,14 +2369,8 @@ pub(crate) mod tests {
client.import(BlockOrigin::Own, a2.clone()).unwrap(); client.import(BlockOrigin::Own, a2.clone()).unwrap();
let genesis_hash = client.info().chain.genesis_hash; let genesis_hash = client.info().chain.genesis_hash;
#[allow(deprecated)]
let longest_chain_select = test_client::client::LongestChain::new(
Arc::new(client.backend().as_in_memory()),
client.import_lock()
);
assert_eq!(a2.hash(), longest_chain_select.finality_target( assert_eq!(a2.hash(), longest_chain_select.finality_target(genesis_hash, Some(10)).unwrap().unwrap());
genesis_hash, Some(10)).unwrap().unwrap());
} }
#[test] #[test]
@@ -133,7 +133,7 @@ pub trait NativeExecutionDispatch: Send + Sync {
/// A generic `CodeExecutor` implementation that uses a delegate to determine wasm code equivalence /// A generic `CodeExecutor` implementation that uses a delegate to determine wasm code equivalence
/// and dispatch to native code when possible, falling back on `WasmExecutor` when not. /// and dispatch to native code when possible, falling back on `WasmExecutor` when not.
#[derive(Debug)] #[derive(Debug)]
pub struct NativeExecutor<D: NativeExecutionDispatch> { pub struct NativeExecutor<D> {
/// Dummy field to avoid the compiler complaining about us not using `D`. /// Dummy field to avoid the compiler complaining about us not using `D`.
_dummy: ::std::marker::PhantomData<D>, _dummy: ::std::marker::PhantomData<D>,
/// The fallback executor in case native isn't available. /// The fallback executor in case native isn't available.
+2 -2
View File
@@ -21,7 +21,7 @@ use assert_matches::assert_matches;
use consensus::BlockOrigin; use consensus::BlockOrigin;
use primitives::storage::well_known_keys; use primitives::storage::well_known_keys;
use sr_io::blake2_256; use sr_io::blake2_256;
use test_client::{self, runtime, AccountKeyring, TestClient, BlockBuilderExt, LocalExecutor}; use test_client::{self, runtime, AccountKeyring, TestClient, BlockBuilderExt, LocalExecutor, TestClientBuilder};
use substrate_executor::NativeExecutionDispatch; use substrate_executor::NativeExecutionDispatch;
#[test] #[test]
@@ -236,7 +236,7 @@ fn should_query_storage() {
} }
run_tests(Arc::new(test_client::new())); run_tests(Arc::new(test_client::new()));
run_tests(Arc::new(test_client::new_with_changes_trie())); run_tests(Arc::new(TestClientBuilder::new().set_support_changes_trie(true).build()));
} }
#[test] #[test]
+4 -6
View File
@@ -634,12 +634,12 @@ impl<Factory: ServiceFactory> Components for LightComponents<Factory> {
mod tests { mod tests {
use super::*; use super::*;
use consensus_common::BlockOrigin; use consensus_common::BlockOrigin;
use client::LongestChain; use substrate_test_client::{TestClient, AccountKeyring, runtime::Transfer, TestClientBuilder};
use substrate_test_client::{TestClient, AccountKeyring, runtime::Transfer};
#[test] #[test]
fn should_remove_transactions_from_the_pool() { fn should_remove_transactions_from_the_pool() {
let client = Arc::new(substrate_test_client::new()); let (client, longest_chain) = TestClientBuilder::new().build_with_longest_chain();
let client = Arc::new(client);
let pool = TransactionPool::new(Default::default(), ::transaction_pool::ChainApi::new(client.clone())); let pool = TransactionPool::new(Default::default(), ::transaction_pool::ChainApi::new(client.clone()));
let transaction = Transfer { let transaction = Transfer {
amount: 5, amount: 5,
@@ -647,9 +647,7 @@ mod tests {
from: AccountKeyring::Alice.into(), from: AccountKeyring::Alice.into(),
to: Default::default(), to: Default::default(),
}.into_signed_tx(); }.into_signed_tx();
#[allow(deprecated)] let best = longest_chain.best_chain().unwrap();
let best = LongestChain::new(client.backend().clone(), client.import_lock())
.best_chain().unwrap();
// store the transaction in the pool // store the transaction in the pool
pool.submit_one(&BlockId::hash(best.hash()), transaction.clone()).unwrap(); pool.submit_one(&BlockId::hash(best.hash()), transaction.clone()).unwrap();
+4 -5
View File
@@ -749,21 +749,20 @@ macro_rules! construct_service_factory {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use client::LongestChain;
use consensus_common::SelectChain; use consensus_common::SelectChain;
use runtime_primitives::traits::BlindCheckable; use runtime_primitives::traits::BlindCheckable;
use substrate_test_client::{AccountKeyring, runtime::{Extrinsic, Transfer}}; use substrate_test_client::{AccountKeyring, runtime::{Extrinsic, Transfer}, TestClientBuilder};
#[test] #[test]
fn should_not_propagate_transactions_that_are_marked_as_such() { fn should_not_propagate_transactions_that_are_marked_as_such() {
// given // given
let client = Arc::new(substrate_test_client::new()); let (client, longest_chain) = TestClientBuilder::new().build_with_longest_chain();
let client = Arc::new(client);
let pool = Arc::new(TransactionPool::new( let pool = Arc::new(TransactionPool::new(
Default::default(), Default::default(),
transaction_pool::ChainApi::new(client.clone()) transaction_pool::ChainApi::new(client.clone())
)); ));
let best = LongestChain::new(client.backend().clone(), client.import_lock()) let best = longest_chain.best_chain().unwrap();
.best_chain().unwrap();
let transaction = Transfer { let transaction = Transfer {
amount: 5, amount: 5,
nonce: 0, nonce: 0,
@@ -16,7 +16,7 @@
use test_client::{ use test_client::{
AccountKeyring, runtime::{TestAPI, DecodeFails, Transfer, Header}, AccountKeyring, runtime::{TestAPI, DecodeFails, Transfer, Header},
NativeExecutor, LocalExecutor, NativeExecutor, LocalExecutor, TestClientBuilder
}; };
use runtime_primitives::{ use runtime_primitives::{
generic::BlockId, generic::BlockId,
@@ -27,12 +27,11 @@ use state_machine::{
execution_proof_check_on_trie_backend, execution_proof_check_on_trie_backend,
}; };
use client::LongestChain;
use consensus_common::SelectChain; use consensus_common::SelectChain;
use codec::Encode; use codec::Encode;
fn calling_function_with_strat(strat: ExecutionStrategy) { fn calling_function_with_strat(strat: ExecutionStrategy) {
let client = test_client::new_with_execution_strategy(strat); let client = TestClientBuilder::new().set_execution_strategy(strat).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
@@ -52,7 +51,7 @@ fn calling_wasm_runtime_function() {
#[test] #[test]
#[should_panic(expected = "Could not convert parameter `param` between node and runtime!")] #[should_panic(expected = "Could not convert parameter `param` between node and runtime!")]
fn calling_native_runtime_function_with_non_decodable_parameter() { fn calling_native_runtime_function_with_non_decodable_parameter() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::NativeWhenPossible); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
runtime_api.fail_convert_parameter(&block_id, DecodeFails::new()).unwrap(); runtime_api.fail_convert_parameter(&block_id, DecodeFails::new()).unwrap();
@@ -61,7 +60,7 @@ fn calling_native_runtime_function_with_non_decodable_parameter() {
#[test] #[test]
#[should_panic(expected = "Could not convert return value from runtime to node!")] #[should_panic(expected = "Could not convert return value from runtime to node!")]
fn calling_native_runtime_function_with_non_decodable_return_value() { fn calling_native_runtime_function_with_non_decodable_return_value() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::NativeWhenPossible); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
runtime_api.fail_convert_return_value(&block_id).unwrap(); runtime_api.fail_convert_return_value(&block_id).unwrap();
@@ -69,7 +68,7 @@ fn calling_native_runtime_function_with_non_decodable_return_value() {
#[test] #[test]
fn calling_native_runtime_signature_changed_function() { fn calling_native_runtime_signature_changed_function() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::NativeWhenPossible); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeWhenPossible).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
@@ -78,7 +77,7 @@ fn calling_native_runtime_signature_changed_function() {
#[test] #[test]
fn calling_wasm_runtime_signature_changed_old_function() { fn calling_wasm_runtime_signature_changed_old_function() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::AlwaysWasm); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::AlwaysWasm).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
@@ -89,7 +88,7 @@ fn calling_wasm_runtime_signature_changed_old_function() {
#[test] #[test]
fn calling_with_both_strategy_and_fail_on_wasm_should_return_error() { fn calling_with_both_strategy_and_fail_on_wasm_should_return_error() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::Both); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::Both).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
assert!(runtime_api.fail_on_wasm(&block_id).is_err()); assert!(runtime_api.fail_on_wasm(&block_id).is_err());
@@ -97,7 +96,7 @@ fn calling_with_both_strategy_and_fail_on_wasm_should_return_error() {
#[test] #[test]
fn calling_with_both_strategy_and_fail_on_native_should_work() { fn calling_with_both_strategy_and_fail_on_native_should_work() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::Both); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::Both).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
assert_eq!(runtime_api.fail_on_native(&block_id).unwrap(), 1); assert_eq!(runtime_api.fail_on_native(&block_id).unwrap(), 1);
@@ -106,7 +105,7 @@ fn calling_with_both_strategy_and_fail_on_native_should_work() {
#[test] #[test]
fn calling_with_native_else_wasm_and_faild_on_wasm_should_work() { fn calling_with_native_else_wasm_and_faild_on_wasm_should_work() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::NativeElseWasm); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeElseWasm).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
assert_eq!(runtime_api.fail_on_wasm(&block_id).unwrap(), 1); assert_eq!(runtime_api.fail_on_wasm(&block_id).unwrap(), 1);
@@ -114,7 +113,7 @@ fn calling_with_native_else_wasm_and_faild_on_wasm_should_work() {
#[test] #[test]
fn calling_with_native_else_wasm_and_fail_on_native_should_work() { fn calling_with_native_else_wasm_and_fail_on_native_should_work() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::NativeElseWasm); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::NativeElseWasm).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
assert_eq!(runtime_api.fail_on_native(&block_id).unwrap(), 1); assert_eq!(runtime_api.fail_on_native(&block_id).unwrap(), 1);
@@ -122,7 +121,7 @@ fn calling_with_native_else_wasm_and_fail_on_native_should_work() {
#[test] #[test]
fn use_trie_function() { fn use_trie_function() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::AlwaysWasm); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::AlwaysWasm).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
assert_eq!(runtime_api.use_trie(&block_id).unwrap(), 2); assert_eq!(runtime_api.use_trie(&block_id).unwrap(), 2);
@@ -130,7 +129,7 @@ fn use_trie_function() {
#[test] #[test]
fn initialize_block_works() { fn initialize_block_works() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::Both); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::Both).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
assert_eq!(runtime_api.get_block_number(&block_id).unwrap(), 1); assert_eq!(runtime_api.get_block_number(&block_id).unwrap(), 1);
@@ -138,7 +137,7 @@ fn initialize_block_works() {
#[test] #[test]
fn initialize_block_is_called_only_once() { fn initialize_block_is_called_only_once() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::Both); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::Both).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
assert_eq!(runtime_api.take_block_number(&block_id).unwrap(), Some(1)); assert_eq!(runtime_api.take_block_number(&block_id).unwrap(), Some(1));
@@ -147,7 +146,7 @@ fn initialize_block_is_called_only_once() {
#[test] #[test]
fn initialize_block_is_skipped() { fn initialize_block_is_skipped() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::Both); let client = TestClientBuilder::new().set_execution_strategy(ExecutionStrategy::Both).build();
let runtime_api = client.runtime_api(); let runtime_api = client.runtime_api();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
assert!(runtime_api.without_initialize_block(&block_id).unwrap()); assert!(runtime_api.without_initialize_block(&block_id).unwrap());
@@ -155,12 +154,12 @@ fn initialize_block_is_skipped() {
#[test] #[test]
fn record_proof_works() { fn record_proof_works() {
let client = test_client::new_with_execution_strategy(ExecutionStrategy::Both); let (client, longest_chain) = TestClientBuilder::new()
.set_execution_strategy(ExecutionStrategy::Both)
.build_with_longest_chain();
let block_id = BlockId::Number(client.info().chain.best_number); let block_id = BlockId::Number(client.info().chain.best_number);
#[allow(deprecated)] let storage_root = longest_chain.best_chain().unwrap().state_root().clone();
let storage_root = LongestChain::new(client.backend().clone(), client.import_lock())
.best_chain().unwrap().state_root().clone();
let transaction = Transfer { let transaction = Transfer {
amount: 1000, amount: 1000,
+1 -1
View File
@@ -8,7 +8,7 @@ edition = "2018"
client = { package = "substrate-client", path = "../client" } client = { package = "substrate-client", path = "../client" }
client-db = { package = "substrate-client-db", path = "../client/db", features = ["test-helpers"] } client-db = { package = "substrate-client-db", path = "../client/db", features = ["test-helpers"] }
futures = { version = "0.1.17" } futures = { version = "0.1.17" }
parity-codec = "3.3" parity-codec = "3.5.1"
executor = { package = "substrate-executor", path = "../executor" } executor = { package = "substrate-executor", path = "../executor" }
consensus = { package = "substrate-consensus-common", path = "../consensus/common" } consensus = { package = "substrate-consensus-common", path = "../consensus/common" }
keyring = { package = "substrate-keyring", path = "../../core/keyring" } keyring = { package = "substrate-keyring", path = "../../core/keyring" }
+83 -75
View File
@@ -104,22 +104,78 @@ pub type LightExecutor = client::light::call_executor::RemoteOrLocalCallExecutor
>; >;
/// A builder for creating a test client instance. /// A builder for creating a test client instance.
pub struct TestClientBuilder { pub struct TestClientBuilder<E, B> {
execution_strategies: ExecutionStrategies, execution_strategies: ExecutionStrategies,
genesis_extension: HashMap<Vec<u8>, Vec<u8>>, genesis_extension: HashMap<Vec<u8>, Vec<u8>>,
support_changes_trie: bool, support_changes_trie: bool,
backend: Arc<B>,
_phantom: std::marker::PhantomData<E>,
} }
impl TestClientBuilder { #[cfg(feature = "include-wasm-blob")]
impl<B> TestClientBuilder<LocalExecutor, 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(feature = "include-wasm-blob")]
impl TestClientBuilder<LocalExecutor, Backend> {
/// Create a new instance of the test client builder. /// Create a new instance of the test client builder.
pub fn new() -> Self { pub fn new() -> Self {
TestClientBuilder { TestClientBuilder {
execution_strategies: ExecutionStrategies::default(), execution_strategies: ExecutionStrategies::default(),
genesis_extension: HashMap::default(), genesis_extension: HashMap::default(),
support_changes_trie: false, 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(),
}
}
}
impl<E, B> TestClientBuilder<E, B> where
B: backend::LocalBackend<runtime::Block, Blake2Hasher>,
E: executor::NativeExecutionDispatch
{
/// Set the execution strategy that should be used by all contexts. /// Set the execution strategy that should be used by all contexts.
pub fn set_execution_strategy( pub fn set_execution_strategy(
mut self, mut self,
@@ -151,60 +207,46 @@ impl TestClientBuilder {
} }
/// Build the test client. /// Build the test client.
#[cfg(feature = "include-wasm-blob")]
pub fn build(self) -> client::Client< pub fn build(self) -> client::Client<
Backend, Executor, runtime::Block, runtime::RuntimeApi
> {
let backend = Arc::new(Backend::new_test(std::u32::MAX, std::u64::MAX));
self.build_with_backend(backend)
}
/// Build the test client with the given backend.
#[cfg(feature = "include-wasm-blob")]
pub fn build_with_backend<B>(self, backend: Arc<B>) -> client::Client<
B, B,
client::LocalCallExecutor<B, executor::NativeExecutor<LocalExecutor>>, client::LocalCallExecutor<B, executor::NativeExecutor<E>>,
runtime::Block, runtime::Block,
runtime::RuntimeApi runtime::RuntimeApi,
> where B: backend::LocalBackend<runtime::Block, Blake2Hasher> { > {
let executor = NativeExecutor::new(None); self.build_with_longest_chain().0
let executor = LocalCallExecutor::new(backend.clone(), executor);
client::Client::new(
backend,
executor,
genesis_storage(self.support_changes_trie, self.genesis_extension),
self.execution_strategies
).expect("Creates new client")
} }
/// Build the test client with the given native executor. /// Build the test client and longest chain as select chain.
pub fn build_with_native_executor<E>( pub fn build_with_longest_chain(self) -> (
self, client::Client<
executor: executor::NativeExecutor<E> B,
) -> client::Client< client::LocalCallExecutor<B, executor::NativeExecutor<E>>,
Backend,
client::LocalCallExecutor<Backend, executor::NativeExecutor<E>>,
runtime::Block, runtime::Block,
runtime::RuntimeApi runtime::RuntimeApi,
> where E: executor::NativeExecutionDispatch >,
{ client::LongestChain<B, runtime::Block>,
let backend = Arc::new(Backend::new_test(std::u32::MAX, std::u64::MAX)); ) {
let executor = LocalCallExecutor::new(backend.clone(), executor); let executor = NativeExecutor::<E>::new(None);
let executor = LocalCallExecutor::new(self.backend.clone(), executor);
client::Client::new( let client = client::Client::new(
backend, self.backend.clone(),
executor, executor,
genesis_storage(self.support_changes_trie, self.genesis_extension), genesis_storage(self.support_changes_trie, self.genesis_extension),
self.execution_strategies self.execution_strategies,
).expect("Creates new client") ).expect("Creates new client");
#[allow(deprecated)]
let longest_chain = client::LongestChain::new(self.backend, client.import_lock());
(client, longest_chain)
} }
} }
/// Creates new client instance used for tests. /// Creates new client instance used for tests.
#[cfg(feature = "include-wasm-blob")] #[cfg(feature = "include-wasm-blob")]
pub fn new() -> client::Client<Backend, Executor, runtime::Block, runtime::RuntimeApi> { pub fn new() -> client::Client<Backend, Executor, runtime::Block, runtime::RuntimeApi> {
new_with_backend(Arc::new(Backend::new_test(::std::u32::MAX, ::std::u64::MAX)), false) TestClientBuilder::new().build()
} }
/// Creates new light client instance used for tests. /// Creates new light client instance used for tests.
@@ -221,40 +263,6 @@ pub fn new_light() -> client::Client<LightBackend, LightExecutor, runtime::Block
client::Client::new(backend, call_executor, genesis_storage(false, Default::default()), Default::default()).unwrap() client::Client::new(backend, call_executor, genesis_storage(false, Default::default()), Default::default()).unwrap()
} }
/// Creates new client instance used for tests with the given api execution strategy.
#[cfg(feature = "include-wasm-blob")]
pub fn new_with_execution_strategy(
execution_strategy: ExecutionStrategy
) -> client::Client<Backend, Executor, runtime::Block, runtime::RuntimeApi> {
TestClientBuilder::new().set_execution_strategy(execution_strategy).build()
}
/// Creates new test client instance that suports changes trie creation.
#[cfg(feature = "include-wasm-blob")]
pub fn new_with_changes_trie()
-> client::Client<Backend, Executor, runtime::Block, runtime::RuntimeApi>
{
TestClientBuilder::new().set_support_changes_trie(true).build()
}
/// Creates new client instance used for tests with an explicitly provided backend.
/// This is useful for testing backend implementations.
#[cfg(feature = "include-wasm-blob")]
pub fn new_with_backend<B>(
backend: Arc<B>,
support_changes_trie: bool
) -> client::Client<
B,
client::LocalCallExecutor<B, executor::NativeExecutor<LocalExecutor>>,
runtime::Block,
runtime::RuntimeApi
> where B: backend::LocalBackend<runtime::Block, Blake2Hasher>
{
TestClientBuilder::new()
.set_support_changes_trie(support_changes_trie)
.build_with_backend(backend)
}
fn genesis_config(support_changes_trie: bool) -> GenesisConfig { fn genesis_config(support_changes_trie: bool) -> GenesisConfig {
GenesisConfig::new(support_changes_trie, vec![ GenesisConfig::new(support_changes_trie, vec![
AuthorityKeyring::Alice.into(), AuthorityKeyring::Alice.into(),
@@ -26,7 +26,7 @@ use crate::{TestClient, AccountKeyring};
use runtime_primitives::traits::Block as BlockT; use runtime_primitives::traits::Block as BlockT;
use crate::backend; use crate::backend;
use crate::blockchain::{Backend as BlockChainBackendT, HeaderBackend}; use crate::blockchain::{Backend as BlockChainBackendT, HeaderBackend};
use crate::{BlockBuilderExt, new_with_backend}; use crate::{BlockBuilderExt, TestClientBuilder};
use runtime::{self, Transfer}; use runtime::{self, Transfer};
use runtime_primitives::generic::BlockId; use runtime_primitives::generic::BlockId;
@@ -40,7 +40,7 @@ pub fn test_leaves_for_backend<B: 'static>(backend: Arc<B>) where
// B2 -> C3 // B2 -> C3
// A1 -> D2 // A1 -> D2
let client = new_with_backend(backend.clone(), false); let client = TestClientBuilder::new_with_backend(backend.clone()).build();
let blockchain = backend.blockchain(); let blockchain = backend.blockchain();
let genesis_hash = client.info().chain.genesis_hash; let genesis_hash = client.info().chain.genesis_hash;
@@ -156,7 +156,7 @@ pub fn test_children_for_backend<B: 'static>(backend: Arc<B>) where
// B2 -> C3 // B2 -> C3
// A1 -> D2 // A1 -> D2
let client = new_with_backend(backend.clone(), false); let client = TestClientBuilder::new_with_backend(backend.clone()).build();
let blockchain = backend.blockchain(); let blockchain = backend.blockchain();
// G -> A1 // G -> A1
@@ -246,7 +246,7 @@ pub fn test_blockchain_query_by_number_gets_canonical<B: 'static>(backend: Arc<B
// A1 -> B2 -> B3 -> B4 // A1 -> B2 -> B3 -> B4
// B2 -> C3 // B2 -> C3
// A1 -> D2 // A1 -> D2
let client = new_with_backend(backend.clone(), false); let client = TestClientBuilder::new_with_backend(backend.clone()).build();
let blockchain = backend.blockchain(); let blockchain = backend.blockchain();
// G -> A1 // G -> A1