sc_simnode/
client.rs

1// Copyright (C) 2023 Polytope Labs (Caymans) Ltd.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17//! Utilities for creating the neccessary client subsystems.
18
19use polkadot_sdk::*;
20
21#[cfg(feature = "aura")]
22pub mod aura;
23
24#[cfg(feature = "parachain")]
25pub mod parachain;
26
27#[cfg(feature = "babe")]
28pub mod babe;
29pub mod timestamp;
30
31use crate::{ChainInfo, SignatureVerificationOverride};
32use jsonrpsee::RpcModule;
33use sc_executor::{HeapAllocStrategy, WasmExecutor, DEFAULT_HEAP_ALLOC_STRATEGY};
34use sc_rpc::SubscriptionTaskExecutor;
35use sc_service::{Configuration, PartialComponents, TFullBackend, TFullClient};
36use sc_telemetry::Telemetry;
37use sp_runtime::{generic::UncheckedExtrinsic, MultiAddress, MultiSignature};
38use sp_wasm_interface::ExtendedHostFunctions;
39
40/// The simnode executor type, we use the wasm executor to force the runtime use host functions
41/// instead of native code for signature verification, this in turn uses our signature verification
42/// overrides.
43pub type Executor = WasmExecutor<(
44	ExtendedHostFunctions<sp_io::SubstrateHostFunctions, SignatureVerificationOverride>,
45	cumulus_client_service::storage_proof_size::HostFunctions,
46)>;
47
48/// Creates a [`WasmExecutor`] according to [`Configuration`].
49pub fn new_wasm_executor(config: &Configuration) -> Executor {
50	let strategy = config
51		.executor
52		.default_heap_pages
53		.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |p| HeapAllocStrategy::Static { extra_pages: p as _ });
54	WasmExecutor::builder()
55		.with_execution_method(config.executor.wasm_method)
56		.with_onchain_heap_alloc_strategy(strategy)
57		.with_offchain_heap_alloc_strategy(strategy)
58		.with_max_runtime_instances(config.executor.max_runtime_instances)
59		.with_runtime_cache_size(config.executor.runtime_cache_size)
60		.build()
61}
62
63/// Type alias for [`sc_service::TFullClient`]
64pub type FullClientFor<C> =
65	TFullClient<<C as ChainInfo>::Block, <C as ChainInfo>::RuntimeApi, Executor>;
66
67/// UncheckedExtrinsic type for Simnode
68pub type UncheckedExtrinsicFor<T> = UncheckedExtrinsic<
69	MultiAddress<
70		<<T as ChainInfo>::Runtime as frame_system::Config>::AccountId,
71		<<T as ChainInfo>::Runtime as frame_system::Config>::Nonce,
72	>,
73	<<T as ChainInfo>::Runtime as frame_system::Config>::RuntimeCall,
74	MultiSignature,
75	<T as ChainInfo>::SignedExtras,
76>;
77
78/// Type alias for [`sc_service::TFullBackend`]
79pub type FullBackendFor<C> = TFullBackend<<C as ChainInfo>::Block>;
80
81/// Params needed to initialize simnode's subsystems.
82pub struct SimnodeParams<Client, Backend, SelectChain, Pool, ImportQueue, BlockImport, U> {
83	/// The necessary subsystems to run simnode.
84	pub components: PartialComponents<
85		Client,
86		Backend,
87		SelectChain,
88		ImportQueue,
89		Pool,
90		(BlockImport, Option<Telemetry>, U),
91	>,
92	/// Config needed for simnode's own subsystems.
93	pub config: Configuration,
94	/// Use instant sealing for block production? if not uses manual seal.
95	pub instant: bool,
96	/// rpc builder.
97	pub rpc_builder:
98		Box<dyn Fn(SubscriptionTaskExecutor) -> Result<RpcModule<()>, sc_service::Error>>,
99}