mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 19:21:13 +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
@@ -1,37 +0,0 @@
|
||||
// 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/>.
|
||||
|
||||
//! Block Builder extensions for tests.
|
||||
|
||||
use client;
|
||||
use runtime;
|
||||
use runtime_primitives::traits::ProvideRuntimeApi;
|
||||
use client::block_builder::api::BlockBuilder;
|
||||
|
||||
/// Extension trait for test block builder.
|
||||
pub trait BlockBuilderExt {
|
||||
/// Add transfer extrinsic to the block.
|
||||
fn push_transfer(&mut self, transfer: runtime::Transfer) -> Result<(), client::error::Error>;
|
||||
}
|
||||
|
||||
impl<'a, A> BlockBuilderExt for client::block_builder::BlockBuilder<'a, runtime::Block, A> where
|
||||
A: ProvideRuntimeApi + client::blockchain::HeaderBackend<runtime::Block> + 'a,
|
||||
A::Api: BlockBuilder<runtime::Block>
|
||||
{
|
||||
fn push_transfer(&mut self, transfer: runtime::Transfer) -> Result<(), client::error::Error> {
|
||||
self.push(transfer.into_signed_tx())
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,335 +0,0 @@
|
||||
// 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/>.
|
||||
|
||||
//! tests that should hold for all implementations of certain traits.
|
||||
//! to test implementations without duplication.
|
||||
|
||||
#![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::blockchain::{Backend as BlockChainBackendT, HeaderBackend};
|
||||
use crate::{BlockBuilderExt, TestClientBuilder};
|
||||
use runtime::{self, Transfer};
|
||||
use runtime_primitives::generic::BlockId;
|
||||
|
||||
/// helper to test the `leaves` implementation for various backends
|
||||
pub fn test_leaves_for_backend<B: 'static>(backend: Arc<B>) where
|
||||
B: backend::LocalBackend<runtime::Block, Blake2Hasher>,
|
||||
{
|
||||
// block tree:
|
||||
// G -> A1 -> A2 -> A3 -> A4 -> A5
|
||||
// A1 -> B2 -> B3 -> B4
|
||||
// B2 -> C3
|
||||
// A1 -> D2
|
||||
|
||||
let client = TestClientBuilder::new_with_backend(backend.clone()).build();
|
||||
let blockchain = backend.blockchain();
|
||||
|
||||
let genesis_hash = client.info().chain.genesis_hash;
|
||||
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![genesis_hash]);
|
||||
|
||||
// G -> A1
|
||||
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a1.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a1.hash()]);
|
||||
|
||||
// A1 -> A2
|
||||
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a2.clone()).unwrap();
|
||||
|
||||
#[allow(deprecated)]
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a2.hash()]);
|
||||
|
||||
// A2 -> A3
|
||||
let a3 = client.new_block_at(&BlockId::Hash(a2.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a3.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a3.hash()]);
|
||||
|
||||
// A3 -> A4
|
||||
let a4 = client.new_block_at(&BlockId::Hash(a3.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a4.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a4.hash()]);
|
||||
|
||||
// A4 -> A5
|
||||
let a5 = client.new_block_at(&BlockId::Hash(a4.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a5.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a5.hash()]);
|
||||
|
||||
// A1 -> B2
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise B2 has the same hash as A2 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 41,
|
||||
nonce: 0,
|
||||
}).unwrap();
|
||||
let b2 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b2.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a5.hash(), b2.hash()]);
|
||||
|
||||
// B2 -> B3
|
||||
let b3 = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b3.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a5.hash(), b3.hash()]);
|
||||
|
||||
// B3 -> B4
|
||||
let b4 = client.new_block_at(&BlockId::Hash(b3.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b4.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a5.hash(), b4.hash()]);
|
||||
|
||||
// // B2 -> C3
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise C3 has the same hash as B3 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 1,
|
||||
nonce: 1,
|
||||
}).unwrap();
|
||||
let c3 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, c3.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a5.hash(), b4.hash(), c3.hash()]);
|
||||
|
||||
// A1 -> D2
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise D2 has the same hash as B2 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 1,
|
||||
nonce: 0,
|
||||
}).unwrap();
|
||||
let d2 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, d2.clone()).unwrap();
|
||||
assert_eq!(
|
||||
blockchain.leaves().unwrap(),
|
||||
vec![a5.hash(), b4.hash(), c3.hash(), d2.hash()]);
|
||||
}
|
||||
|
||||
/// helper to test the `children` implementation for various backends
|
||||
pub fn test_children_for_backend<B: 'static>(backend: Arc<B>) where
|
||||
B: backend::LocalBackend<runtime::Block, Blake2Hasher>,
|
||||
{
|
||||
// block tree:
|
||||
// G -> A1 -> A2 -> A3 -> A4 -> A5
|
||||
// A1 -> B2 -> B3 -> B4
|
||||
// B2 -> C3
|
||||
// A1 -> D2
|
||||
|
||||
let client = TestClientBuilder::new_with_backend(backend.clone()).build();
|
||||
let blockchain = backend.blockchain();
|
||||
|
||||
// G -> A1
|
||||
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a1.clone()).unwrap();
|
||||
|
||||
// A1 -> A2
|
||||
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a2.clone()).unwrap();
|
||||
|
||||
// A2 -> A3
|
||||
let a3 = client.new_block_at(&BlockId::Hash(a2.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a3.clone()).unwrap();
|
||||
|
||||
// A3 -> A4
|
||||
let a4 = client.new_block_at(&BlockId::Hash(a3.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a4.clone()).unwrap();
|
||||
|
||||
// A4 -> A5
|
||||
let a5 = client.new_block_at(&BlockId::Hash(a4.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a5.clone()).unwrap();
|
||||
|
||||
// A1 -> B2
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise B2 has the same hash as A2 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 41,
|
||||
nonce: 0,
|
||||
}).unwrap();
|
||||
let b2 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b2.clone()).unwrap();
|
||||
|
||||
// B2 -> B3
|
||||
let b3 = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b3.clone()).unwrap();
|
||||
|
||||
// B3 -> B4
|
||||
let b4 = client.new_block_at(&BlockId::Hash(b3.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b4.clone()).unwrap();
|
||||
|
||||
// // B2 -> C3
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise C3 has the same hash as B3 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 1,
|
||||
nonce: 1,
|
||||
}).unwrap();
|
||||
let c3 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, c3.clone()).unwrap();
|
||||
|
||||
// A1 -> D2
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise D2 has the same hash as B2 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 1,
|
||||
nonce: 0,
|
||||
}).unwrap();
|
||||
let d2 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, d2.clone()).unwrap();
|
||||
|
||||
let genesis_hash = client.info().chain.genesis_hash;
|
||||
|
||||
let children1 = blockchain.children(a4.hash()).unwrap();
|
||||
assert_eq!(vec![a5.hash()], children1);
|
||||
|
||||
let children2 = blockchain.children(a1.hash()).unwrap();
|
||||
assert_eq!(vec![a2.hash(), b2.hash(), d2.hash()], children2);
|
||||
|
||||
let children3 = blockchain.children(genesis_hash).unwrap();
|
||||
assert_eq!(vec![a1.hash()], children3);
|
||||
|
||||
let children4 = blockchain.children(b2.hash()).unwrap();
|
||||
assert_eq!(vec![b3.hash(), c3.hash()], children4);
|
||||
}
|
||||
|
||||
pub fn test_blockchain_query_by_number_gets_canonical<B: 'static>(backend: Arc<B>) where
|
||||
B: backend::LocalBackend<runtime::Block, Blake2Hasher>,
|
||||
{
|
||||
// block tree:
|
||||
// G -> A1 -> A2 -> A3 -> A4 -> A5
|
||||
// A1 -> B2 -> B3 -> B4
|
||||
// B2 -> C3
|
||||
// A1 -> D2
|
||||
let client = TestClientBuilder::new_with_backend(backend.clone()).build();
|
||||
let blockchain = backend.blockchain();
|
||||
|
||||
// G -> A1
|
||||
let a1 = client.new_block(Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a1.clone()).unwrap();
|
||||
|
||||
// A1 -> A2
|
||||
let a2 = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a2.clone()).unwrap();
|
||||
|
||||
// A2 -> A3
|
||||
let a3 = client.new_block_at(&BlockId::Hash(a2.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a3.clone()).unwrap();
|
||||
|
||||
// A3 -> A4
|
||||
let a4 = client.new_block_at(&BlockId::Hash(a3.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a4.clone()).unwrap();
|
||||
|
||||
// A4 -> A5
|
||||
let a5 = client.new_block_at(&BlockId::Hash(a4.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, a5.clone()).unwrap();
|
||||
|
||||
// A1 -> B2
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise B2 has the same hash as A2 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 41,
|
||||
nonce: 0,
|
||||
}).unwrap();
|
||||
let b2 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b2.clone()).unwrap();
|
||||
|
||||
// B2 -> B3
|
||||
let b3 = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b3.clone()).unwrap();
|
||||
|
||||
// B3 -> B4
|
||||
let b4 = client.new_block_at(&BlockId::Hash(b3.hash()), Default::default()).unwrap().bake().unwrap();
|
||||
client.import(BlockOrigin::Own, b4.clone()).unwrap();
|
||||
|
||||
// // B2 -> C3
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(b2.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise C3 has the same hash as B3 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 1,
|
||||
nonce: 1,
|
||||
}).unwrap();
|
||||
let c3 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, c3.clone()).unwrap();
|
||||
|
||||
// A1 -> D2
|
||||
let mut builder = client.new_block_at(&BlockId::Hash(a1.hash()), Default::default()).unwrap();
|
||||
// this push is required as otherwise D2 has the same hash as B2 and won't get imported
|
||||
builder.push_transfer(Transfer {
|
||||
from: AccountKeyring::Alice.into(),
|
||||
to: AccountKeyring::Ferdie.into(),
|
||||
amount: 1,
|
||||
nonce: 0,
|
||||
}).unwrap();
|
||||
let d2 = builder.bake().unwrap();
|
||||
client.import(BlockOrigin::Own, d2.clone()).unwrap();
|
||||
|
||||
let genesis_hash = client.info().chain.genesis_hash;
|
||||
|
||||
assert_eq!(blockchain.header(BlockId::Number(0)).unwrap().unwrap().hash(), genesis_hash);
|
||||
assert_eq!(blockchain.hash(0).unwrap().unwrap(), genesis_hash);
|
||||
|
||||
assert_eq!(blockchain.header(BlockId::Number(1)).unwrap().unwrap().hash(), a1.hash());
|
||||
assert_eq!(blockchain.hash(1).unwrap().unwrap(), a1.hash());
|
||||
|
||||
assert_eq!(blockchain.header(BlockId::Number(2)).unwrap().unwrap().hash(), a2.hash());
|
||||
assert_eq!(blockchain.hash(2).unwrap().unwrap(), a2.hash());
|
||||
|
||||
assert_eq!(blockchain.header(BlockId::Number(3)).unwrap().unwrap().hash(), a3.hash());
|
||||
assert_eq!(blockchain.hash(3).unwrap().unwrap(), a3.hash());
|
||||
|
||||
assert_eq!(blockchain.header(BlockId::Number(4)).unwrap().unwrap().hash(), a4.hash());
|
||||
assert_eq!(blockchain.hash(4).unwrap().unwrap(), a4.hash());
|
||||
|
||||
assert_eq!(blockchain.header(BlockId::Number(5)).unwrap().unwrap().hash(), a5.hash());
|
||||
assert_eq!(blockchain.hash(5).unwrap().unwrap(), a5.hash());
|
||||
}
|
||||
Reference in New Issue
Block a user