substrate_test_runtime_client/
lib.rs1#![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
36pub mod prelude {
38 pub use super::{
40 BlockBuilderExt, ClientBlockImportExt, ClientExt, DefaultTestClientBuilderExt,
41 TestClientBuilderExt,
42 };
43 pub use super::{
45 Backend, ExecutorDispatch, TestClient, TestClientBuilder, WasmExecutionMethod,
46 };
47 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
54pub type Backend = substrate_test_client::Backend<substrate_test_runtime::Block>;
56
57pub type ExecutorDispatch =
59 client::LocalCallExecutor<substrate_test_runtime::Block, Backend, WasmExecutor>;
60
61#[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 pub fn set_wasm_code(&mut self, code: Vec<u8>) {
72 self.wasm_code = Some(code);
73 }
74
75 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
91pub type TestClientBuilder<E, B> = substrate_test_client::TestClientBuilder<
93 substrate_test_runtime::Block,
94 E,
95 B,
96 GenesisParameters,
97>;
98
99pub 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
107pub type TestClient = Client<Backend>;
109
110pub trait DefaultTestClientBuilderExt: Sized {
112 fn new() -> Self;
114}
115
116impl DefaultTestClientBuilderExt for TestClientBuilder<ExecutorDispatch, Backend> {
117 fn new() -> Self {
118 Self::with_default_backend()
119 }
120}
121
122pub trait TestClientBuilderExt<B>: Sized {
124 fn genesis_init_mut(&mut self) -> &mut GenesisParameters;
126
127 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 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 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 fn build(self) -> Client<B> {
175 self.build_with_longest_chain().0
176 }
177
178 fn build_with_longest_chain(
180 self,
181 ) -> (Client<B>, sc_consensus::LongestChain<B, substrate_test_runtime::Block>);
182
183 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
208pub fn new() -> Client<Backend> {
210 TestClientBuilder::new().build()
211}
212
213#[deprecated(note = "Switch to `WasmExecutor:default()`.")]
215pub fn new_native_or_wasm_executor() -> WasmExecutor {
216 WasmExecutor::default()
217}