git.delta.rocks / unique-network / refs/commits / 8154d7d843bf

difftreelog

source

node/src/service.rs9.9 KiBsourcehistory
1//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.23//4// This file is subject to the terms and conditions defined in5// file 'LICENSE', which is part of this source code package.6//78use std::sync::Arc;9use std::time::Duration;10use sc_client_api::{ExecutorProvider, RemoteBackend};11use nft_runtime::{self, opaque::Block, RuntimeApi};12use sc_service::{error::Error as ServiceError, Configuration, TaskManager};13use sp_inherents::InherentDataProviders;14use sc_executor::native_executor_instance;15pub use sc_executor::NativeExecutor;16use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};17use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState};1819// Our native executor instance.20native_executor_instance!(21	pub Executor,22    nft_runtime::api::dispatch,23    nft_runtime::native_version,24	frame_benchmarking::benchmarking::HostFunctions,25);2627type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;28type FullBackend = sc_service::TFullBackend<Block>;29type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;3031pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponents<32	FullClient, FullBackend, FullSelectChain,33	sp_consensus::DefaultImportQueue<Block, FullClient>,34	sc_transaction_pool::FullPool<Block, FullClient>,35	(36		sc_consensus_aura::AuraBlockImport<37			Block,38			FullClient,39			sc_finality_grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>,40			AuraPair41		>,42		sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>43	)44>, ServiceError> {45	let inherent_data_providers = sp_inherents::InherentDataProviders::new();4647	let (client, backend, keystore, task_manager) =48		sc_service::new_full_parts::<Block, RuntimeApi, Executor>(&config)?;49	let client = Arc::new(client);5051	let select_chain = sc_consensus::LongestChain::new(backend.clone());5253	let transaction_pool = sc_transaction_pool::BasicPool::new_full(54		config.transaction_pool.clone(),55		config.prometheus_registry(),56		task_manager.spawn_handle(),57		client.clone(),58	);5960	let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(61		client.clone(), &(client.clone() as Arc<_>), select_chain.clone(),62	)?;6364	let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(65		grandpa_block_import.clone(), client.clone(),66	);6768	let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _, _>(69		sc_consensus_aura::slot_duration(&*client)?,70		aura_block_import.clone(),71		Some(Box::new(grandpa_block_import.clone())),72		None,73		client.clone(),74		inherent_data_providers.clone(),75		&task_manager.spawn_handle(),76		config.prometheus_registry(),77		sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),78	)?;7980	Ok(sc_service::PartialComponents {81		client, backend, task_manager, import_queue, keystore, select_chain, transaction_pool,82		inherent_data_providers,83		other: (aura_block_import, grandpa_link),84	})85}8687/// Builds a new service for a full client.88pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {89	let sc_service::PartialComponents {90		client, backend, mut task_manager, import_queue, keystore, select_chain, transaction_pool,91		inherent_data_providers,92		other: (block_import, grandpa_link),93	} = new_partial(&config)?;9495	let finality_proof_provider =96		GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());9798	let (network, network_status_sinks, system_rpc_tx, network_starter) =99		sc_service::build_network(sc_service::BuildNetworkParams {100			config: &config,101			client: client.clone(),102			transaction_pool: transaction_pool.clone(),103			spawn_handle: task_manager.spawn_handle(),104			import_queue,105			on_demand: None,106			block_announce_validator_builder: None,107			finality_proof_request_builder: None,108			finality_proof_provider: Some(finality_proof_provider.clone()),109		})?;110111	if config.offchain_worker.enabled {112		sc_service::build_offchain_workers(113			&config, backend.clone(), task_manager.spawn_handle(), client.clone(), network.clone(),114		);115	}116117	let role = config.role.clone();118	let force_authoring = config.force_authoring;119	let name = config.network.node_name.clone();120	let enable_grandpa = !config.disable_grandpa;121	let prometheus_registry = config.prometheus_registry().cloned();122	let telemetry_connection_sinks = sc_service::TelemetryConnectionSinks::default();123124	let rpc_extensions_builder = {125		let client = client.clone();126		let pool = transaction_pool.clone();127128		Box::new(move |deny_unsafe, _| {129			let deps = crate::rpc::FullDeps {130				client: client.clone(),131				pool: pool.clone(),132				deny_unsafe,133			};134135			crate::rpc::create_full(deps)136		})137	};138139	sc_service::spawn_tasks(sc_service::SpawnTasksParams {140		network: network.clone(),141		client: client.clone(),142		keystore: keystore.clone(),143		task_manager: &mut task_manager,144		transaction_pool: transaction_pool.clone(),145		telemetry_connection_sinks: telemetry_connection_sinks.clone(),146		rpc_extensions_builder: rpc_extensions_builder,147		on_demand: None,148		remote_blockchain: None,149		backend, network_status_sinks, system_rpc_tx, config,150	})?;151152	if role.is_authority() {153		let proposer = sc_basic_authorship::ProposerFactory::new(154			client.clone(),155			transaction_pool,156			prometheus_registry.as_ref(),157		);158159		let can_author_with =160			sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());161162		let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>(163			sc_consensus_aura::slot_duration(&*client)?,164			client.clone(),165			select_chain,166			block_import,167			proposer,168			network.clone(),169			inherent_data_providers.clone(),170			force_authoring,171			keystore.clone(),172			can_author_with,173		)?;174175		// the AURA authoring task is considered essential, i.e. if it176		// fails we take down the service with it.177		task_manager.spawn_essential_handle().spawn_blocking("aura", aura);178	}179180	// if the node isn't actively participating in consensus then it doesn't181	// need a keystore, regardless of which protocol we use below.182	let keystore = if role.is_authority() {183		Some(keystore as sp_core::traits::BareCryptoStorePtr)184	} else {185		None186	};187188	let grandpa_config = sc_finality_grandpa::Config {189		// FIXME #1578 make this available through chainspec190		gossip_duration: Duration::from_millis(333),191		justification_period: 512,192		name: Some(name),193		observer_enabled: false,194		keystore,195		is_authority: role.is_network_authority(),196	};197198	if enable_grandpa {199		// start the full GRANDPA voter200		// NOTE: non-authorities could run the GRANDPA observer protocol, but at201		// this point the full voter should provide better guarantees of block202		// and vote data availability than the observer. The observer has not203		// been tested extensively yet and having most nodes in a network run it204		// could lead to finality stalls.205		let grandpa_config = sc_finality_grandpa::GrandpaParams {206			config: grandpa_config,207			link: grandpa_link,208			network,209			inherent_data_providers,210			telemetry_on_connect: Some(telemetry_connection_sinks.on_connect_stream()),211			voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),212			prometheus_registry,213			shared_voter_state: SharedVoterState::empty(),214		};215216		// the GRANDPA voter task is considered infallible, i.e.217		// if it fails we take down the service with it.218		task_manager.spawn_essential_handle().spawn_blocking(219			"grandpa-voter",220			sc_finality_grandpa::run_grandpa_voter(grandpa_config)?221		);222	} else {223		sc_finality_grandpa::setup_disabled_grandpa(224			client,225			&inherent_data_providers,226			network,227		)?;228	}229230	network_starter.start_network();231	Ok(task_manager)232}233234/// Builds a new service for a light client.235pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {236	let (client, backend, keystore, mut task_manager, on_demand) =237		sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;238239	let transaction_pool = Arc::new(sc_transaction_pool::BasicPool::new_light(240		config.transaction_pool.clone(),241		config.prometheus_registry(),242		task_manager.spawn_handle(),243		client.clone(),244		on_demand.clone(),245	));246247	let grandpa_block_import = sc_finality_grandpa::light_block_import(248		client.clone(), backend.clone(), &(client.clone() as Arc<_>),249		Arc::new(on_demand.checker().clone()) as Arc<_>,250	)?;251	let finality_proof_import = grandpa_block_import.clone();252	let finality_proof_request_builder =253		finality_proof_import.create_finality_proof_request_builder();254255	let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _, _>(256		sc_consensus_aura::slot_duration(&*client)?,257		grandpa_block_import,258		None,259		Some(Box::new(finality_proof_import)),260		client.clone(),261		InherentDataProviders::new(),262		&task_manager.spawn_handle(),263		config.prometheus_registry(),264		sp_consensus::NeverCanAuthor,265	)?;266267	let finality_proof_provider =268		GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());269270	let (network, network_status_sinks, system_rpc_tx, network_starter) =271		sc_service::build_network(sc_service::BuildNetworkParams {272			config: &config,273			client: client.clone(),274			transaction_pool: transaction_pool.clone(),275			spawn_handle: task_manager.spawn_handle(),276			import_queue,277			on_demand: Some(on_demand.clone()),278			block_announce_validator_builder: None,279			finality_proof_request_builder: Some(finality_proof_request_builder),280			finality_proof_provider: Some(finality_proof_provider),281		})?;282283	if config.offchain_worker.enabled {284		sc_service::build_offchain_workers(285			&config, backend.clone(), task_manager.spawn_handle(), client.clone(), network.clone(),286		);287	}288289	sc_service::spawn_tasks(sc_service::SpawnTasksParams {290		remote_blockchain: Some(backend.remote_blockchain()),291		transaction_pool,292		task_manager: &mut task_manager,293		on_demand: Some(on_demand),294		rpc_extensions_builder: Box::new(|_, _| ()),295		telemetry_connection_sinks: sc_service::TelemetryConnectionSinks::default(),296		config,297		client,298		keystore,299		backend,300		network,301		network_status_sinks,302		system_rpc_tx,303	 })?;304305	 network_starter.start_network();306307	 Ok(task_manager)308}