moonbeam_service/lazy_loading/
client.rs

1// Copyright 2024 Moonbeam foundation
2// This file is part of Moonbeam.
3
4// Moonbeam 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// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16
17use crate::lazy_loading;
18use crate::lazy_loading::{TLazyLoadingBackend, TLazyLoadingCallExecutor};
19use cumulus_primitives_core::BlockT;
20use sc_chain_spec::BuildGenesisBlock;
21use sc_client_api::execution_extensions::ExecutionExtensions;
22use sc_client_api::{BadBlocks, ForkBlocks};
23use sc_executor::RuntimeVersionOf;
24use sc_service::client::Client;
25use sc_service::{ClientConfig, LocalCallExecutor};
26use sc_telemetry::TelemetryHandle;
27use sp_core::traits::{CodeExecutor, SpawnNamed};
28use std::sync::Arc;
29
30pub fn new_client<E, Block, RA, G>(
31	backend: Arc<TLazyLoadingBackend<Block>>,
32	executor: E,
33	genesis_block_builder: G,
34	fork_blocks: ForkBlocks<Block>,
35	bad_blocks: BadBlocks<Block>,
36	execution_extensions: ExecutionExtensions<Block>,
37	spawn_handle: Box<dyn SpawnNamed>,
38	prometheus_registry: Option<substrate_prometheus_endpoint::Registry>,
39	telemetry: Option<TelemetryHandle>,
40	config: ClientConfig<Block>,
41) -> Result<
42	Client<TLazyLoadingBackend<Block>, TLazyLoadingCallExecutor<Block, E>, Block, RA>,
43	sp_blockchain::Error,
44>
45where
46	Block: BlockT + sp_runtime::DeserializeOwned,
47	Block::Hash: From<sp_core::H256>,
48	E: CodeExecutor + RuntimeVersionOf,
49	TLazyLoadingBackend<Block>: sc_client_api::Backend<Block> + 'static,
50	G: BuildGenesisBlock<
51		Block,
52		BlockImportOperation = <TLazyLoadingBackend<Block> as sc_client_api::backend::Backend<
53			Block,
54		>>::BlockImportOperation,
55	>,
56{
57	let executor =
58		lazy_loading::call_executor::LazyLoadingCallExecutor::new(LocalCallExecutor::new(
59			backend.clone(),
60			executor,
61			config.clone(),
62			execution_extensions,
63		)?)?;
64
65	Client::new(
66		backend,
67		executor,
68		spawn_handle,
69		genesis_block_builder,
70		fork_blocks,
71		bad_blocks,
72		prometheus_registry,
73		telemetry,
74		config,
75	)
76}