#![warn(missing_docs)]
pub mod trait_tests;
mod block_builder_ext;
pub use sc_consensus::LongestChain;
use std::sync::Arc;
pub use substrate_test_client::*;
pub use substrate_test_runtime as runtime;
pub use self::block_builder_ext::BlockBuilderExt;
use sp_core::storage::ChildInfo;
use substrate_test_runtime::genesismap::GenesisStorageBuilder;
pub mod prelude {
pub use super::{
BlockBuilderExt, ClientBlockImportExt, ClientExt, DefaultTestClientBuilderExt,
TestClientBuilderExt,
};
pub use super::{
Backend, ExecutorDispatch, TestClient, TestClientBuilder, WasmExecutionMethod,
};
pub use super::Sr25519Keyring;
}
pub type Backend = substrate_test_client::Backend<substrate_test_runtime::Block>;
pub type ExecutorDispatch =
client::LocalCallExecutor<substrate_test_runtime::Block, Backend, WasmExecutor>;
#[derive(Default)]
pub struct GenesisParameters {
heap_pages_override: Option<u64>,
extra_storage: Storage,
wasm_code: Option<Vec<u8>>,
}
impl GenesisParameters {
pub fn set_wasm_code(&mut self, code: Vec<u8>) {
self.wasm_code = Some(code);
}
pub fn extra_storage(&mut self) -> &mut Storage {
&mut self.extra_storage
}
}
impl GenesisInit for GenesisParameters {
fn genesis_storage(&self) -> Storage {
GenesisStorageBuilder::default()
.with_heap_pages(self.heap_pages_override)
.with_wasm_code(&self.wasm_code)
.with_extra_storage(self.extra_storage.clone())
.build()
}
}
pub type TestClientBuilder<E, B> = substrate_test_client::TestClientBuilder<
substrate_test_runtime::Block,
E,
B,
GenesisParameters,
>;
pub type Client<B> = client::Client<
B,
client::LocalCallExecutor<substrate_test_runtime::Block, B, WasmExecutor>,
substrate_test_runtime::Block,
substrate_test_runtime::RuntimeApi,
>;
pub type TestClient = Client<Backend>;
pub trait DefaultTestClientBuilderExt: Sized {
fn new() -> Self;
}
impl DefaultTestClientBuilderExt for TestClientBuilder<ExecutorDispatch, Backend> {
fn new() -> Self {
Self::with_default_backend()
}
}
pub trait TestClientBuilderExt<B>: Sized {
fn genesis_init_mut(&mut self) -> &mut GenesisParameters;
fn set_heap_pages(mut self, heap_pages: u64) -> Self {
self.genesis_init_mut().heap_pages_override = Some(heap_pages);
self
}
fn add_extra_child_storage<K: Into<Vec<u8>>, V: Into<Vec<u8>>>(
mut self,
child_info: &ChildInfo,
key: K,
value: V,
) -> Self {
let storage_key = child_info.storage_key().to_vec();
let key = key.into();
assert!(!storage_key.is_empty());
assert!(!key.is_empty());
self.genesis_init_mut()
.extra_storage
.children_default
.entry(storage_key)
.or_insert_with(|| StorageChild {
data: Default::default(),
child_info: child_info.clone(),
})
.data
.insert(key, value.into());
self
}
fn add_extra_storage<K: Into<Vec<u8>>, V: Into<Vec<u8>>>(mut self, key: K, value: V) -> Self {
let key = key.into();
assert!(!key.is_empty());
self.genesis_init_mut().extra_storage.top.insert(key, value.into());
self
}
fn build(self) -> Client<B> {
self.build_with_longest_chain().0
}
fn build_with_longest_chain(
self,
) -> (Client<B>, sc_consensus::LongestChain<B, substrate_test_runtime::Block>);
fn build_with_backend(self) -> (Client<B>, Arc<B>);
}
impl<B> TestClientBuilderExt<B>
for TestClientBuilder<client::LocalCallExecutor<substrate_test_runtime::Block, B, WasmExecutor>, B>
where
B: sc_client_api::backend::Backend<substrate_test_runtime::Block> + 'static,
{
fn genesis_init_mut(&mut self) -> &mut GenesisParameters {
Self::genesis_init_mut(self)
}
fn build_with_longest_chain(
self,
) -> (Client<B>, sc_consensus::LongestChain<B, substrate_test_runtime::Block>) {
self.build_with_native_executor(None)
}
fn build_with_backend(self) -> (Client<B>, Arc<B>) {
let backend = self.backend();
(self.build_with_native_executor(None).0, backend)
}
}
pub fn new() -> Client<Backend> {
TestClientBuilder::new().build()
}
#[deprecated(note = "Switch to `WasmExecutor:default()`.")]
pub fn new_native_or_wasm_executor() -> WasmExecutor {
WasmExecutor::default()
}