2019-08-29 15:44:46 +00:00
|
|
|
//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
2020-03-25 03:42:27 +00:00
|
|
|
use sc_client_api::ExecutorProvider;
|
2020-05-06 20:25:03 +00:00
|
|
|
use sc_consensus::LongestChain;
|
2020-03-25 03:42:27 +00:00
|
|
|
use node_template_runtime::{self, opaque::Block, RuntimeApi};
|
2019-12-16 20:46:39 +00:00
|
|
|
use sc_service::{error::{Error as ServiceError}, AbstractService, Configuration, ServiceBuilder};
|
|
|
|
use sp_inherents::InherentDataProviders;
|
|
|
|
use sc_executor::native_executor_instance;
|
|
|
|
pub use sc_executor::NativeExecutor;
|
|
|
|
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
|
2020-05-06 20:25:03 +00:00
|
|
|
use sc_finality_grandpa::{
|
2020-05-16 13:32:11 +00:00
|
|
|
FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider, SharedVoterState,
|
2020-05-06 20:25:03 +00:00
|
|
|
};
|
2019-08-29 15:44:46 +00:00
|
|
|
|
|
|
|
// Our native executor instance.
|
|
|
|
native_executor_instance!(
|
|
|
|
pub Executor,
|
|
|
|
node_template_runtime::api::dispatch,
|
2019-10-04 13:32:53 +00:00
|
|
|
node_template_runtime::native_version,
|
2019-08-29 15:44:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// Starts a `ServiceBuilder` for a full service.
|
|
|
|
///
|
|
|
|
/// Use this macro if you don't actually need the full service, but just the builder in order to
|
|
|
|
/// be able to perform chain operations.
|
|
|
|
macro_rules! new_full_start {
|
|
|
|
($config:expr) => {{
|
2020-03-25 03:42:27 +00:00
|
|
|
use std::sync::Arc;
|
2020-05-16 13:32:11 +00:00
|
|
|
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
|
|
|
|
|
2019-08-29 15:44:46 +00:00
|
|
|
let mut import_setup = None;
|
2019-12-16 20:46:39 +00:00
|
|
|
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
|
2019-08-29 15:44:46 +00:00
|
|
|
|
2019-12-16 20:46:39 +00:00
|
|
|
let builder = sc_service::ServiceBuilder::new_full::<
|
2019-08-29 15:44:46 +00:00
|
|
|
node_template_runtime::opaque::Block, node_template_runtime::RuntimeApi, crate::service::Executor
|
|
|
|
>($config)?
|
2019-10-04 13:32:53 +00:00
|
|
|
.with_select_chain(|_config, backend| {
|
2020-05-06 20:25:03 +00:00
|
|
|
Ok(sc_consensus::LongestChain::new(backend.clone()))
|
2019-12-16 20:46:39 +00:00
|
|
|
})?
|
2020-05-06 20:25:03 +00:00
|
|
|
.with_transaction_pool(|config, client, _fetcher, prometheus_registry| {
|
2019-12-16 20:46:39 +00:00
|
|
|
let pool_api = sc_transaction_pool::FullChainApi::new(client.clone());
|
2020-05-06 20:25:03 +00:00
|
|
|
Ok(sc_transaction_pool::BasicPool::new(config, std::sync::Arc::new(pool_api), prometheus_registry))
|
2019-08-29 15:44:46 +00:00
|
|
|
})?
|
2020-05-16 13:32:11 +00:00
|
|
|
.with_import_queue(|
|
|
|
|
_config,
|
|
|
|
client,
|
|
|
|
mut select_chain,
|
|
|
|
_transaction_pool,
|
|
|
|
spawn_task_handle,
|
|
|
|
registry,
|
|
|
|
| {
|
2019-08-29 15:44:46 +00:00
|
|
|
let select_chain = select_chain.take()
|
2019-12-16 20:46:39 +00:00
|
|
|
.ok_or_else(|| sc_service::Error::SelectChainRequired)?;
|
2019-10-20 12:22:24 +00:00
|
|
|
|
2020-05-16 13:32:11 +00:00
|
|
|
let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(
|
|
|
|
client.clone(),
|
|
|
|
&(client.clone() as Arc<_>),
|
|
|
|
select_chain,
|
|
|
|
)?;
|
2019-10-04 13:32:53 +00:00
|
|
|
|
2020-01-06 20:58:38 +00:00
|
|
|
let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
|
|
|
|
grandpa_block_import.clone(), client.clone(),
|
|
|
|
);
|
|
|
|
|
2020-05-06 20:25:03 +00:00
|
|
|
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
|
2020-03-05 16:53:25 +00:00
|
|
|
sc_consensus_aura::slot_duration(&*client)?,
|
2020-01-06 20:58:38 +00:00
|
|
|
aura_block_import,
|
2019-10-20 12:22:24 +00:00
|
|
|
Some(Box::new(grandpa_block_import.clone())),
|
2019-08-29 15:44:46 +00:00
|
|
|
None,
|
|
|
|
client,
|
|
|
|
inherent_data_providers.clone(),
|
2020-05-06 20:25:03 +00:00
|
|
|
spawn_task_handle,
|
2020-05-16 13:32:11 +00:00
|
|
|
registry,
|
2019-08-29 15:44:46 +00:00
|
|
|
)?;
|
|
|
|
|
2019-10-20 12:22:24 +00:00
|
|
|
import_setup = Some((grandpa_block_import, grandpa_link));
|
2019-08-29 15:44:46 +00:00
|
|
|
|
|
|
|
Ok(import_queue)
|
|
|
|
})?;
|
|
|
|
|
2019-10-04 13:32:53 +00:00
|
|
|
(builder, import_setup, inherent_data_providers)
|
2019-08-29 15:44:46 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a new service for a full client.
|
2020-05-16 13:32:11 +00:00
|
|
|
pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceError> {
|
2020-04-15 11:33:19 +00:00
|
|
|
let role = config.role.clone();
|
2019-10-20 12:22:24 +00:00
|
|
|
let force_authoring = config.force_authoring;
|
2020-04-15 11:33:19 +00:00
|
|
|
let name = config.network.node_name.clone();
|
2019-10-04 13:32:53 +00:00
|
|
|
let disable_grandpa = config.disable_grandpa;
|
|
|
|
|
|
|
|
let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config);
|
2019-08-29 15:44:46 +00:00
|
|
|
|
2019-10-20 12:22:24 +00:00
|
|
|
let (block_import, grandpa_link) =
|
|
|
|
import_setup.take()
|
|
|
|
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
|
|
|
|
|
2020-03-05 16:53:25 +00:00
|
|
|
let service = builder
|
2020-03-25 03:42:27 +00:00
|
|
|
.with_finality_proof_provider(|client, backend| {
|
|
|
|
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
|
|
|
|
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
|
|
|
|
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
|
|
|
|
})?
|
2019-08-29 15:44:46 +00:00
|
|
|
.build()?;
|
|
|
|
|
2020-04-15 11:33:19 +00:00
|
|
|
if role.is_authority() {
|
|
|
|
let proposer =
|
|
|
|
sc_basic_authorship::ProposerFactory::new(service.client(), service.transaction_pool());
|
2019-08-29 15:44:46 +00:00
|
|
|
|
|
|
|
let client = service.client();
|
|
|
|
let select_chain = service.select_chain()
|
|
|
|
.ok_or(ServiceError::SelectChainRequired)?;
|
|
|
|
|
2019-12-16 20:46:39 +00:00
|
|
|
let can_author_with =
|
|
|
|
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());
|
|
|
|
|
2020-03-05 16:53:25 +00:00
|
|
|
let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>(
|
|
|
|
sc_consensus_aura::slot_duration(&*client)?,
|
2019-08-29 15:44:46 +00:00
|
|
|
client,
|
|
|
|
select_chain,
|
2019-10-04 13:32:53 +00:00
|
|
|
block_import,
|
2019-10-20 12:22:24 +00:00
|
|
|
proposer,
|
|
|
|
service.network(),
|
|
|
|
inherent_data_providers.clone(),
|
2019-10-04 13:32:53 +00:00
|
|
|
force_authoring,
|
2019-10-20 12:22:24 +00:00
|
|
|
service.keystore(),
|
2019-12-16 20:46:39 +00:00
|
|
|
can_author_with,
|
2019-10-20 12:22:24 +00:00
|
|
|
)?;
|
2019-08-29 15:44:46 +00:00
|
|
|
|
2019-10-20 12:22:24 +00:00
|
|
|
// the AURA authoring task is considered essential, i.e. if it
|
2019-08-29 15:44:46 +00:00
|
|
|
// fails we take down the service with it.
|
2020-03-05 16:53:25 +00:00
|
|
|
service.spawn_essential_task("aura", aura);
|
2019-08-29 15:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 20:46:39 +00:00
|
|
|
// if the node isn't actively participating in consensus then it doesn't
|
|
|
|
// need a keystore, regardless of which protocol we use below.
|
2020-04-15 11:33:19 +00:00
|
|
|
let keystore = if role.is_authority() {
|
2019-12-16 20:46:39 +00:00
|
|
|
Some(service.keystore())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-04-15 11:33:19 +00:00
|
|
|
let grandpa_config = sc_finality_grandpa::Config {
|
2019-08-29 15:44:46 +00:00
|
|
|
// FIXME #1578 make this available through chainspec
|
|
|
|
gossip_duration: Duration::from_millis(333),
|
2019-10-04 13:32:53 +00:00
|
|
|
justification_period: 512,
|
|
|
|
name: Some(name),
|
2020-03-05 16:53:25 +00:00
|
|
|
observer_enabled: false,
|
2019-12-16 20:46:39 +00:00
|
|
|
keystore,
|
2020-04-15 11:33:19 +00:00
|
|
|
is_authority: role.is_network_authority(),
|
2019-08-29 15:44:46 +00:00
|
|
|
};
|
|
|
|
|
2020-03-05 16:53:25 +00:00
|
|
|
let enable_grandpa = !disable_grandpa;
|
|
|
|
if enable_grandpa {
|
|
|
|
// start the full GRANDPA voter
|
|
|
|
// NOTE: non-authorities could run the GRANDPA observer protocol, but at
|
|
|
|
// this point the full voter should provide better guarantees of block
|
|
|
|
// and vote data availability than the observer. The observer has not
|
|
|
|
// been tested extensively yet and having most nodes in a network run it
|
|
|
|
// could lead to finality stalls.
|
2020-04-15 11:33:19 +00:00
|
|
|
let grandpa_config = sc_finality_grandpa::GrandpaParams {
|
2020-03-05 16:53:25 +00:00
|
|
|
config: grandpa_config,
|
|
|
|
link: grandpa_link,
|
|
|
|
network: service.network(),
|
|
|
|
inherent_data_providers: inherent_data_providers.clone(),
|
|
|
|
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
|
2020-04-15 11:33:19 +00:00
|
|
|
voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),
|
2020-05-06 20:25:03 +00:00
|
|
|
prometheus_registry: service.prometheus_registry(),
|
2020-05-16 13:32:11 +00:00
|
|
|
shared_voter_state: SharedVoterState::empty(),
|
2020-03-05 16:53:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// the GRANDPA voter task is considered infallible, i.e.
|
|
|
|
// if it fails we take down the service with it.
|
|
|
|
service.spawn_essential_task(
|
|
|
|
"grandpa-voter",
|
2020-04-15 11:33:19 +00:00
|
|
|
sc_finality_grandpa::run_grandpa_voter(grandpa_config)?
|
2020-03-05 16:53:25 +00:00
|
|
|
);
|
|
|
|
} else {
|
2020-04-15 11:33:19 +00:00
|
|
|
sc_finality_grandpa::setup_disabled_grandpa(
|
2020-03-05 16:53:25 +00:00
|
|
|
service.client(),
|
|
|
|
&inherent_data_providers,
|
|
|
|
service.network(),
|
|
|
|
)?;
|
2019-08-29 15:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(service)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a new service for a light client.
|
2020-05-16 13:32:11 +00:00
|
|
|
pub fn new_light(config: Configuration) -> Result<impl AbstractService, ServiceError> {
|
2019-08-29 15:44:46 +00:00
|
|
|
let inherent_data_providers = InherentDataProviders::new();
|
|
|
|
|
|
|
|
ServiceBuilder::new_light::<Block, RuntimeApi, Executor>(config)?
|
2019-10-04 13:32:53 +00:00
|
|
|
.with_select_chain(|_config, backend| {
|
|
|
|
Ok(LongestChain::new(backend.clone()))
|
2019-08-29 15:44:46 +00:00
|
|
|
})?
|
2020-05-06 20:25:03 +00:00
|
|
|
.with_transaction_pool(|config, client, fetcher, prometheus_registry| {
|
2019-12-16 20:46:39 +00:00
|
|
|
let fetcher = fetcher
|
|
|
|
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;
|
2020-03-05 16:53:25 +00:00
|
|
|
|
2019-12-16 20:46:39 +00:00
|
|
|
let pool_api = sc_transaction_pool::LightChainApi::new(client.clone(), fetcher.clone());
|
2020-03-05 16:53:25 +00:00
|
|
|
let pool = sc_transaction_pool::BasicPool::with_revalidation_type(
|
2020-05-06 20:25:03 +00:00
|
|
|
config, Arc::new(pool_api), prometheus_registry, sc_transaction_pool::RevalidationType::Light,
|
2020-03-05 16:53:25 +00:00
|
|
|
);
|
|
|
|
Ok(pool)
|
2019-12-16 20:46:39 +00:00
|
|
|
})?
|
2020-05-16 13:32:11 +00:00
|
|
|
.with_import_queue_and_fprb(|
|
|
|
|
_config,
|
|
|
|
client,
|
|
|
|
backend,
|
|
|
|
fetcher,
|
|
|
|
_select_chain,
|
|
|
|
_tx_pool,
|
|
|
|
spawn_task_handle,
|
|
|
|
prometheus_registry,
|
|
|
|
| {
|
2019-10-04 13:32:53 +00:00
|
|
|
let fetch_checker = fetcher
|
2019-08-29 15:44:46 +00:00
|
|
|
.map(|fetcher| fetcher.checker().clone())
|
|
|
|
.ok_or_else(|| "Trying to start light import queue without active fetch checker")?;
|
2020-04-15 11:33:19 +00:00
|
|
|
let grandpa_block_import = sc_finality_grandpa::light_block_import(
|
2020-03-25 03:42:27 +00:00
|
|
|
client.clone(),
|
|
|
|
backend,
|
|
|
|
&(client.clone() as Arc<_>),
|
|
|
|
Arc::new(fetch_checker),
|
2019-08-29 15:44:46 +00:00
|
|
|
)?;
|
2019-10-04 13:32:53 +00:00
|
|
|
let finality_proof_import = grandpa_block_import.clone();
|
2019-08-29 15:44:46 +00:00
|
|
|
let finality_proof_request_builder =
|
|
|
|
finality_proof_import.create_finality_proof_request_builder();
|
|
|
|
|
2020-05-06 20:25:03 +00:00
|
|
|
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
|
2020-03-05 16:53:25 +00:00
|
|
|
sc_consensus_aura::slot_duration(&*client)?,
|
2020-01-06 20:58:38 +00:00
|
|
|
grandpa_block_import,
|
2019-08-29 15:44:46 +00:00
|
|
|
None,
|
|
|
|
Some(Box::new(finality_proof_import)),
|
|
|
|
client,
|
|
|
|
inherent_data_providers.clone(),
|
2020-05-06 20:25:03 +00:00
|
|
|
spawn_task_handle,
|
2020-05-16 13:32:11 +00:00
|
|
|
prometheus_registry,
|
2019-08-29 15:44:46 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok((import_queue, finality_proof_request_builder))
|
|
|
|
})?
|
2020-03-25 03:42:27 +00:00
|
|
|
.with_finality_proof_provider(|client, backend| {
|
|
|
|
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
|
|
|
|
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
|
|
|
|
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
|
|
|
|
})?
|
2019-08-29 15:44:46 +00:00
|
|
|
.build()
|
|
|
|
}
|