referrerpolicy=no-referrer-when-downgrade

node_testing/
client.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Utilities to build a `TestClient` for `kitchensink-runtime`.
20
21use sp_runtime::BuildStorage;
22/// Re-export test-client utilities.
23pub use substrate_test_client::*;
24
25/// Call executor for `kitchensink-runtime` `TestClient`.
26use node_cli::service::RuntimeExecutor;
27
28/// Default backend type.
29pub type Backend = sc_client_db::Backend<node_primitives::Block>;
30
31/// Test client type.
32pub type Client = client::Client<
33	Backend,
34	client::LocalCallExecutor<node_primitives::Block, Backend, RuntimeExecutor>,
35	node_primitives::Block,
36	kitchensink_runtime::RuntimeApi,
37>;
38
39/// Genesis configuration parameters for `TestClient`.
40#[derive(Default)]
41pub struct GenesisParameters;
42
43impl substrate_test_client::GenesisInit for GenesisParameters {
44	fn genesis_storage(&self) -> Storage {
45		let mut storage = crate::genesis::config().build_storage().unwrap();
46		storage.top.insert(
47			sp_core::storage::well_known_keys::CODE.to_vec(),
48			kitchensink_runtime::wasm_binary_unwrap().into(),
49		);
50		storage
51	}
52}
53
54/// A `test-runtime` extensions to `TestClientBuilder`.
55pub trait TestClientBuilderExt: Sized {
56	/// Create test client builder.
57	fn new() -> Self;
58
59	/// Build the test client.
60	fn build(self) -> Client;
61}
62
63impl TestClientBuilderExt
64	for substrate_test_client::TestClientBuilder<
65		node_primitives::Block,
66		client::LocalCallExecutor<node_primitives::Block, Backend, RuntimeExecutor>,
67		Backend,
68		GenesisParameters,
69	>
70{
71	fn new() -> Self {
72		Self::default()
73	}
74	fn build(self) -> Client {
75		let executor = RuntimeExecutor::builder().build();
76		use sc_service::client::LocalCallExecutor;
77		use std::sync::Arc;
78		let executor = LocalCallExecutor::new(
79			self.backend().clone(),
80			executor.clone(),
81			Default::default(),
82			ExecutionExtensions::new(None, Arc::new(executor)),
83		)
84		.expect("Creates LocalCallExecutor");
85		self.build_with_executor(executor).0
86	}
87}