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

            
17
use crate::lazy_loading;
18
use cumulus_primitives_core::BlockT;
19
use moonbeam_cli_opt::LazyLoadingConfig;
20
use sc_chain_spec::BuildGenesisBlock;
21
use sc_client_api::execution_extensions::ExecutionExtensions;
22
use sc_client_api::{BadBlocks, ForkBlocks};
23
use sc_executor::RuntimeVersionOf;
24
use sc_service::client::Client;
25
use sc_service::ClientConfig;
26
use sc_telemetry::TelemetryHandle;
27
use sp_core::traits::{CodeExecutor, SpawnNamed};
28
use std::sync::Arc;
29

            
30
pub fn new_client<E, Block, RA, G, Backend>(
31
	backend: Arc<Backend>,
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
	lazy_loading_config: &LazyLoadingConfig,
42
) -> Result<
43
	Client<
44
		Backend,
45
		lazy_loading::call_executor::LazyLoadingCallExecutor<Block, Backend, E>,
46
		Block,
47
		RA,
48
	>,
49
	sp_blockchain::Error,
50
>
51
	where
52
		Block: BlockT + sp_runtime::DeserializeOwned,
53
		Block::Hash: From<sp_core::H256>,
54
		E: CodeExecutor + RuntimeVersionOf,
55
		Backend: sc_client_api::Backend<Block> + 'static,
56
		G: BuildGenesisBlock<
57
			Block,
58
			BlockImportOperation = <Backend as sc_client_api::backend::Backend<Block>>::BlockImportOperation
59
		>
60
{
61
	let executor = lazy_loading::call_executor::LazyLoadingCallExecutor::new(
62
		backend.clone(),
63
		lazy_loading_config,
64
		executor,
65
		config.clone(),
66
		execution_extensions,
67
	)?;
68

            
69
	Client::new(
70
		backend,
71
		executor,
72
		spawn_handle,
73
		genesis_block_builder,
74
		fork_blocks,
75
		bad_blocks,
76
		prometheus_registry,
77
		telemetry,
78
		config,
79
	)
80
}