git.delta.rocks / unique-network / refs/commits / eca8c8471489

difftreelog

source

node/src/service.rs9.8 KiBsourcehistory
1//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.23use std::sync::Arc;4use std::time::Duration;5use sc_client_api::{ExecutorProvider, RemoteBackend};6use nft_runtime::{self, opaque::Block, RuntimeApi};7use sc_service::{error::Error as ServiceError, Configuration, TaskManager};8use sp_inherents::InherentDataProviders;9use sc_executor::native_executor_instance;10pub use sc_executor::NativeExecutor;11use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};12use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState};1314// Our native executor instance.15native_executor_instance!(16	pub Executor,17    nft_runtime::api::dispatch,18    nft_runtime::native_version,19	frame_benchmarking::benchmarking::HostFunctions,20);2122type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;23type FullBackend = sc_service::TFullBackend<Block>;24type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;2526pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponents<27	FullClient, FullBackend, FullSelectChain,28	sp_consensus::DefaultImportQueue<Block, FullClient>,29	sc_transaction_pool::FullPool<Block, FullClient>,30	(31		sc_consensus_aura::AuraBlockImport<32			Block,33			FullClient,34			sc_finality_grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>,35			AuraPair36		>,37		sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>38	)39>, ServiceError> {40	let inherent_data_providers = sp_inherents::InherentDataProviders::new();4142	let (client, backend, keystore, task_manager) =43		sc_service::new_full_parts::<Block, RuntimeApi, Executor>(&config)?;44	let client = Arc::new(client);4546	let select_chain = sc_consensus::LongestChain::new(backend.clone());4748	let transaction_pool = sc_transaction_pool::BasicPool::new_full(49		config.transaction_pool.clone(),50		config.prometheus_registry(),51		task_manager.spawn_handle(),52		client.clone(),53	);5455	let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(56		client.clone(), &(client.clone() as Arc<_>), select_chain.clone(),57	)?;5859	let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(60		grandpa_block_import.clone(), client.clone(),61	);6263	let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _, _>(64		sc_consensus_aura::slot_duration(&*client)?,65		aura_block_import.clone(),66		Some(Box::new(grandpa_block_import.clone())),67		None,68		client.clone(),69		inherent_data_providers.clone(),70		&task_manager.spawn_handle(),71		config.prometheus_registry(),72		sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),73	)?;7475	Ok(sc_service::PartialComponents {76		client, backend, task_manager, import_queue, keystore, select_chain, transaction_pool,77		inherent_data_providers,78		other: (aura_block_import, grandpa_link),79	})80}8182/// Builds a new service for a full client.83pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {84	let sc_service::PartialComponents {85		client, backend, mut task_manager, import_queue, keystore, select_chain, transaction_pool,86		inherent_data_providers,87		other: (block_import, grandpa_link),88	} = new_partial(&config)?;8990	let finality_proof_provider =91		GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());9293	let (network, network_status_sinks, system_rpc_tx, network_starter) =94		sc_service::build_network(sc_service::BuildNetworkParams {95			config: &config,96			client: client.clone(),97			transaction_pool: transaction_pool.clone(),98			spawn_handle: task_manager.spawn_handle(),99			import_queue,100			on_demand: None,101			block_announce_validator_builder: None,102			finality_proof_request_builder: None,103			finality_proof_provider: Some(finality_proof_provider.clone()),104		})?;105106	if config.offchain_worker.enabled {107		sc_service::build_offchain_workers(108			&config, backend.clone(), task_manager.spawn_handle(), client.clone(), network.clone(),109		);110	}111112	let role = config.role.clone();113	let force_authoring = config.force_authoring;114	let name = config.network.node_name.clone();115	let enable_grandpa = !config.disable_grandpa;116	let prometheus_registry = config.prometheus_registry().cloned();117	let telemetry_connection_sinks = sc_service::TelemetryConnectionSinks::default();118119	let rpc_extensions_builder = {120		let client = client.clone();121		let pool = transaction_pool.clone();122123		Box::new(move |deny_unsafe, _| {124			let deps = crate::rpc::FullDeps {125				client: client.clone(),126				pool: pool.clone(),127				deny_unsafe,128			};129130			crate::rpc::create_full(deps)131		})132	};133134	sc_service::spawn_tasks(sc_service::SpawnTasksParams {135		network: network.clone(),136		client: client.clone(),137		keystore: keystore.clone(),138		task_manager: &mut task_manager,139		transaction_pool: transaction_pool.clone(),140		telemetry_connection_sinks: telemetry_connection_sinks.clone(),141		rpc_extensions_builder: rpc_extensions_builder,142		on_demand: None,143		remote_blockchain: None,144		backend, network_status_sinks, system_rpc_tx, config,145	})?;146147	if role.is_authority() {148		let proposer = sc_basic_authorship::ProposerFactory::new(149			client.clone(),150			transaction_pool,151			prometheus_registry.as_ref(),152		);153154		let can_author_with =155			sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());156157		let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>(158			sc_consensus_aura::slot_duration(&*client)?,159			client.clone(),160			select_chain,161			block_import,162			proposer,163			network.clone(),164			inherent_data_providers.clone(),165			force_authoring,166			keystore.clone(),167			can_author_with,168		)?;169170		// the AURA authoring task is considered essential, i.e. if it171		// fails we take down the service with it.172		task_manager.spawn_essential_handle().spawn_blocking("aura", aura);173	}174175	// if the node isn't actively participating in consensus then it doesn't176	// need a keystore, regardless of which protocol we use below.177	let keystore = if role.is_authority() {178		Some(keystore as sp_core::traits::BareCryptoStorePtr)179	} else {180		None181	};182183	let grandpa_config = sc_finality_grandpa::Config {184		// FIXME #1578 make this available through chainspec185		gossip_duration: Duration::from_millis(333),186		justification_period: 512,187		name: Some(name),188		observer_enabled: false,189		keystore,190		is_authority: role.is_network_authority(),191	};192193	if enable_grandpa {194		// start the full GRANDPA voter195		// NOTE: non-authorities could run the GRANDPA observer protocol, but at196		// this point the full voter should provide better guarantees of block197		// and vote data availability than the observer. The observer has not198		// been tested extensively yet and having most nodes in a network run it199		// could lead to finality stalls.200		let grandpa_config = sc_finality_grandpa::GrandpaParams {201			config: grandpa_config,202			link: grandpa_link,203			network,204			inherent_data_providers,205			telemetry_on_connect: Some(telemetry_connection_sinks.on_connect_stream()),206			voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),207			prometheus_registry,208			shared_voter_state: SharedVoterState::empty(),209		};210211		// the GRANDPA voter task is considered infallible, i.e.212		// if it fails we take down the service with it.213		task_manager.spawn_essential_handle().spawn_blocking(214			"grandpa-voter",215			sc_finality_grandpa::run_grandpa_voter(grandpa_config)?216		);217	} else {218		sc_finality_grandpa::setup_disabled_grandpa(219			client,220			&inherent_data_providers,221			network,222		)?;223	}224225	network_starter.start_network();226	Ok(task_manager)227}228229/// Builds a new service for a light client.230pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {231	let (client, backend, keystore, mut task_manager, on_demand) =232		sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;233234	let transaction_pool = Arc::new(sc_transaction_pool::BasicPool::new_light(235		config.transaction_pool.clone(),236		config.prometheus_registry(),237		task_manager.spawn_handle(),238		client.clone(),239		on_demand.clone(),240	));241242	let grandpa_block_import = sc_finality_grandpa::light_block_import(243		client.clone(), backend.clone(), &(client.clone() as Arc<_>),244		Arc::new(on_demand.checker().clone()) as Arc<_>,245	)?;246	let finality_proof_import = grandpa_block_import.clone();247	let finality_proof_request_builder =248		finality_proof_import.create_finality_proof_request_builder();249250	let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _, _>(251		sc_consensus_aura::slot_duration(&*client)?,252		grandpa_block_import,253		None,254		Some(Box::new(finality_proof_import)),255		client.clone(),256		InherentDataProviders::new(),257		&task_manager.spawn_handle(),258		config.prometheus_registry(),259		sp_consensus::NeverCanAuthor,260	)?;261262	let finality_proof_provider =263		GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());264265	let (network, network_status_sinks, system_rpc_tx, network_starter) =266		sc_service::build_network(sc_service::BuildNetworkParams {267			config: &config,268			client: client.clone(),269			transaction_pool: transaction_pool.clone(),270			spawn_handle: task_manager.spawn_handle(),271			import_queue,272			on_demand: Some(on_demand.clone()),273			block_announce_validator_builder: None,274			finality_proof_request_builder: Some(finality_proof_request_builder),275			finality_proof_provider: Some(finality_proof_provider),276		})?;277278	if config.offchain_worker.enabled {279		sc_service::build_offchain_workers(280			&config, backend.clone(), task_manager.spawn_handle(), client.clone(), network.clone(),281		);282	}283284	sc_service::spawn_tasks(sc_service::SpawnTasksParams {285		remote_blockchain: Some(backend.remote_blockchain()),286		transaction_pool,287		task_manager: &mut task_manager,288		on_demand: Some(on_demand),289		rpc_extensions_builder: Box::new(|_, _| ()),290		telemetry_connection_sinks: sc_service::TelemetryConnectionSinks::default(),291		config,292		client,293		keystore,294		backend,295		network,296		network_status_sinks,297		system_rpc_tx,298	 })?;299300	 network_starter.start_network();301302	 Ok(task_manager)303}