referrerpolicy=no-referrer-when-downgrade

substrate_test_runtime_client/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Client testing utilities.
19
20#![warn(missing_docs)]
21
22pub mod trait_tests;
23
24mod block_builder_ext;
25
26pub use sc_consensus::LongestChain;
27use std::sync::Arc;
28pub use substrate_test_client::*;
29pub use substrate_test_runtime as runtime;
30
31pub use self::block_builder_ext::BlockBuilderExt;
32
33use sp_core::storage::ChildInfo;
34use substrate_test_runtime::genesismap::GenesisStorageBuilder;
35
36/// A prelude to import in tests.
37pub mod prelude {
38	// Trait extensions
39	pub use super::{
40		BlockBuilderExt, ClientBlockImportExt, ClientExt, DefaultTestClientBuilderExt,
41		TestClientBuilderExt,
42	};
43	// Client structs
44	pub use super::{
45		Backend, ExecutorDispatch, TestClient, TestClientBuilder, WasmExecutionMethod,
46	};
47	// Keyring
48	pub use super::Sr25519Keyring;
49	pub use futures::executor::block_on;
50	pub use sc_block_builder::BlockBuilderBuilder;
51	pub use sp_blockchain::HeaderBackend;
52}
53
54/// Test client database backend.
55pub type Backend = substrate_test_client::Backend<substrate_test_runtime::Block>;
56
57/// Test client executor.
58pub type ExecutorDispatch =
59	client::LocalCallExecutor<substrate_test_runtime::Block, Backend, WasmExecutor>;
60
61/// Parameters of test-client builder with test-runtime.
62#[derive(Default)]
63pub struct GenesisParameters {
64	heap_pages_override: Option<u64>,
65	extra_storage: Storage,
66	wasm_code: Option<Vec<u8>>,
67}
68
69impl GenesisParameters {
70	/// Set the wasm code that should be used at genesis.
71	pub fn set_wasm_code(&mut self, code: Vec<u8>) {
72		self.wasm_code = Some(code);
73	}
74
75	/// Access extra genesis storage.
76	pub fn extra_storage(&mut self) -> &mut Storage {
77		&mut self.extra_storage
78	}
79}
80
81impl GenesisInit for GenesisParameters {
82	fn genesis_storage(&self) -> Storage {
83		GenesisStorageBuilder::default()
84			.with_heap_pages(self.heap_pages_override)
85			.with_wasm_code(&self.wasm_code)
86			.with_extra_storage(self.extra_storage.clone())
87			.build()
88	}
89}
90
91/// A `TestClient` with `test-runtime` builder.
92pub type TestClientBuilder<E, B> = substrate_test_client::TestClientBuilder<
93	substrate_test_runtime::Block,
94	E,
95	B,
96	GenesisParameters,
97>;
98
99/// Test client type with `WasmExecutor` and generic Backend.
100pub type Client<B> = client::Client<
101	B,
102	client::LocalCallExecutor<substrate_test_runtime::Block, B, WasmExecutor>,
103	substrate_test_runtime::Block,
104	substrate_test_runtime::RuntimeApi,
105>;
106
107/// A test client with default backend.
108pub type TestClient = Client<Backend>;
109
110/// A `TestClientBuilder` with default backend and executor.
111pub trait DefaultTestClientBuilderExt: Sized {
112	/// Create new `TestClientBuilder`
113	fn new() -> Self;
114}
115
116impl DefaultTestClientBuilderExt for TestClientBuilder<ExecutorDispatch, Backend> {
117	fn new() -> Self {
118		Self::with_default_backend()
119	}
120}
121
122/// A `test-runtime` extensions to `TestClientBuilder`.
123pub trait TestClientBuilderExt<B>: Sized {
124	/// Returns a mutable reference to the genesis parameters.
125	fn genesis_init_mut(&mut self) -> &mut GenesisParameters;
126
127	/// Override the default value for Wasm heap pages.
128	fn set_heap_pages(mut self, heap_pages: u64) -> Self {
129		self.genesis_init_mut().heap_pages_override = Some(heap_pages);
130		self
131	}
132
133	/// Add an extra value into the genesis storage.
134	///
135	/// # Panics
136	///
137	/// Panics if the key is empty.
138	fn add_extra_child_storage<K: Into<Vec<u8>>, V: Into<Vec<u8>>>(
139		mut self,
140		child_info: &ChildInfo,
141		key: K,
142		value: V,
143	) -> Self {
144		let storage_key = child_info.storage_key().to_vec();
145		let key = key.into();
146		assert!(!storage_key.is_empty());
147		assert!(!key.is_empty());
148		self.genesis_init_mut()
149			.extra_storage
150			.children_default
151			.entry(storage_key)
152			.or_insert_with(|| StorageChild {
153				data: Default::default(),
154				child_info: child_info.clone(),
155			})
156			.data
157			.insert(key, value.into());
158		self
159	}
160
161	/// Add an extra child value into the genesis storage.
162	///
163	/// # Panics
164	///
165	/// Panics if the key is empty.
166	fn add_extra_storage<K: Into<Vec<u8>>, V: Into<Vec<u8>>>(mut self, key: K, value: V) -> Self {
167		let key = key.into();
168		assert!(!key.is_empty());
169		self.genesis_init_mut().extra_storage.top.insert(key, value.into());
170		self
171	}
172
173	/// Build the test client.
174	fn build(self) -> Client<B> {
175		self.build_with_longest_chain().0
176	}
177
178	/// Build the test client and longest chain selector.
179	fn build_with_longest_chain(
180		self,
181	) -> (Client<B>, sc_consensus::LongestChain<B, substrate_test_runtime::Block>);
182
183	/// Build the test client and the backend.
184	fn build_with_backend(self) -> (Client<B>, Arc<B>);
185}
186
187impl<B> TestClientBuilderExt<B>
188	for TestClientBuilder<client::LocalCallExecutor<substrate_test_runtime::Block, B, WasmExecutor>, B>
189where
190	B: sc_client_api::backend::Backend<substrate_test_runtime::Block> + 'static,
191{
192	fn genesis_init_mut(&mut self) -> &mut GenesisParameters {
193		Self::genesis_init_mut(self)
194	}
195
196	fn build_with_longest_chain(
197		self,
198	) -> (Client<B>, sc_consensus::LongestChain<B, substrate_test_runtime::Block>) {
199		self.build_with_native_executor(None)
200	}
201
202	fn build_with_backend(self) -> (Client<B>, Arc<B>) {
203		let backend = self.backend();
204		(self.build_with_native_executor(None).0, backend)
205	}
206}
207
208/// Creates new client instance used for tests.
209pub fn new() -> Client<Backend> {
210	TestClientBuilder::new().build()
211}
212
213/// Create a new native executor.
214#[deprecated(note = "Switch to `WasmExecutor:default()`.")]
215pub fn new_native_or_wasm_executor() -> WasmExecutor {
216	WasmExecutor::default()
217}