Companion PR for Remove the service builder (#1448)

* Switch branch

* Update branch

* Change service code

* Change light service stuff to be functions ^_^

* Update substrate branch

* Remove accidental 'f'

* Rework LightBackend/LightClient types

* Update substrate branch

* Remove unused imports in test-service

* Add #[cfg(feature = full-node)]
This commit is contained in:
Ashley
2020-07-23 14:12:53 +02:00
committed by GitHub
parent 6a1fbae2df
commit 6919c3030c
4 changed files with 1182 additions and 1261 deletions
+269 -314
View File
File diff suppressed because it is too large Load Diff
+217 -199
View File
@@ -23,8 +23,7 @@ mod client;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use polkadot_primitives::v1::{AccountId, Nonce, Balance}; use polkadot_primitives::v1::{AccountId, Nonce, Balance};
#[cfg(feature = "full-node")] use service::{error::Error as ServiceError};
use service::{error::Error as ServiceError, ServiceBuilder};
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider}; use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider};
use sc_executor::native_executor_instance; use sc_executor::native_executor_instance;
use log::info; use log::info;
@@ -93,7 +92,7 @@ pub trait RuntimeApiCollection<Extrinsic: codec::Codec + Send + Sync + 'static>:
+ authority_discovery_primitives::AuthorityDiscoveryApi<Block> + authority_discovery_primitives::AuthorityDiscoveryApi<Block>
where where
Extrinsic: RuntimeExtrinsic, Extrinsic: RuntimeExtrinsic,
<Self as sp_api::ApiExt<Block>>::StateBackend: sp_api::StateBackend<BlakeTwo256>, <Self as sp_api::ApiExt<Block>>::StateBackend: sp_api::StateBackend<HashFor<Block>>,
{} {}
impl<Api, Extrinsic> RuntimeApiCollection<Extrinsic> for Api impl<Api, Extrinsic> RuntimeApiCollection<Extrinsic> for Api
@@ -111,7 +110,7 @@ where
+ sp_session::SessionKeys<Block> + sp_session::SessionKeys<Block>
+ authority_discovery_primitives::AuthorityDiscoveryApi<Block>, + authority_discovery_primitives::AuthorityDiscoveryApi<Block>,
Extrinsic: RuntimeExtrinsic, Extrinsic: RuntimeExtrinsic,
<Self as sp_api::ApiExt<Block>>::StateBackend: sp_api::StateBackend<BlakeTwo256>, <Self as sp_api::ApiExt<Block>>::StateBackend: sp_api::StateBackend<HashFor<Block>>,
{} {}
pub trait RuntimeExtrinsic: codec::Codec + Send + Sync + 'static {} pub trait RuntimeExtrinsic: codec::Codec + Send + Sync + 'static {}
@@ -145,47 +144,67 @@ fn set_prometheus_registry(config: &mut Configuration) -> Result<(), ServiceErro
Ok(()) Ok(())
} }
/// Starts a `ServiceBuilder` for a full service. type FullBackend = service::TFullBackend<Block>;
/// type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
/// Use this macro if you don't actually need the full service, but just the builder in order to type FullClient<RuntimeApi, Executor> = service::TFullClient<Block, RuntimeApi, Executor>;
/// be able to perform chain operations. type FullGrandpaBlockImport<RuntimeApi, Executor> = grandpa::GrandpaBlockImport<
macro_rules! new_full_start { FullBackend, Block, FullClient<RuntimeApi, Executor>, FullSelectChain
($config:expr, $runtime:ty, $executor:ty) => {{ >;
set_prometheus_registry(&mut $config)?;
type LightBackend = service::TLightBackendWithHash<Block, sp_runtime::traits::BlakeTwo256>;
type LightClient<RuntimeApi, Executor> =
service::TLightClientWithBackend<Block, RuntimeApi, Executor, LightBackend>;
#[cfg(feature = "full-node")]
fn full_params<RuntimeApi, Executor, Extrinsic>(mut config: Configuration) -> Result<(
service::ServiceParams<
Block,
FullClient<RuntimeApi, Executor>,
babe::BabeImportQueue<Block, FullClient<RuntimeApi, Executor>>,
sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, Executor>>,
polkadot_rpc::RpcExtension,
FullBackend,
>,
FullSelectChain,
(
babe::BabeBlockImport<
Block, FullClient<RuntimeApi, Executor>, FullGrandpaBlockImport<RuntimeApi, Executor>
>,
grandpa::LinkHalf<Block, FullClient<RuntimeApi, Executor>, FullSelectChain>,
babe::BabeLink<Block>
),
inherents::InherentDataProviders,
grandpa::SharedVoterState,
), Error>
where
RuntimeApi: ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi:
RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,
Executor: NativeExecutionDispatch + 'static,
Extrinsic: RuntimeExtrinsic,
{
set_prometheus_registry(&mut config)?;
let mut import_setup = None;
let mut rpc_setup = None;
let inherent_data_providers = inherents::InherentDataProviders::new(); let inherent_data_providers = inherents::InherentDataProviders::new();
let builder = service::ServiceBuilder::new_full::<
Block, $runtime, $executor
>($config)? let (client, backend, keystore, task_manager) =
.with_select_chain(|_, backend| { service::new_full_parts::<Block, RuntimeApi, Executor>(&config)?;
Ok(sc_consensus::LongestChain::new(backend.clone())) let client = Arc::new(client);
})?
.with_transaction_pool(|builder| { let select_chain = sc_consensus::LongestChain::new(backend.clone());
let pool_api = sc_transaction_pool::FullChainApi::new( let pool_api = sc_transaction_pool::FullChainApi::new(
builder.client().clone(), client.clone(), config.prometheus_registry(),
builder.prometheus_registry(),
); );
let pool = sc_transaction_pool::BasicPool::new_full( let transaction_pool = sc_transaction_pool::BasicPool::new_full(
builder.config().transaction_pool.clone(), config.transaction_pool.clone(),
std::sync::Arc::new(pool_api), std::sync::Arc::new(pool_api),
builder.prometheus_registry(), config.prometheus_registry(),
builder.spawn_handle(), task_manager.spawn_handle(),
builder.client().clone(), client.clone(),
); );
Ok(pool)
})?
.with_import_queue(|
config,
client,
mut select_chain,
_,
spawn_task_handle,
registry,
| {
let select_chain = select_chain.take()
.ok_or_else(|| service::Error::SelectChainRequired)?;
let grandpa_hard_forks = if config.chain_spec.is_kusama() { let grandpa_hard_forks = if config.chain_spec.is_kusama() {
grandpa_support::kusama_hard_forks() grandpa_support::kusama_hard_forks()
@@ -214,41 +233,32 @@ macro_rules! new_full_start {
block_import.clone(), block_import.clone(),
Some(Box::new(justification_import)), Some(Box::new(justification_import)),
None, None,
client, client.clone(),
select_chain, select_chain.clone(),
inherent_data_providers.clone(), inherent_data_providers.clone(),
spawn_task_handle, &task_manager.spawn_handle(),
registry, config.prometheus_registry(),
)?; )?;
import_setup = Some((block_import, grandpa_link, babe_link));
Ok(import_queue)
})?
.with_rpc_extensions_builder(|builder| {
let grandpa_link = import_setup.as_ref().map(|s| &s.1)
.expect("GRANDPA LinkHalf is present for full services or set up failed; qed.");
let shared_authority_set = grandpa_link.shared_authority_set().clone(); let shared_authority_set = grandpa_link.shared_authority_set().clone();
let shared_voter_state = grandpa::SharedVoterState::empty(); let shared_voter_state = grandpa::SharedVoterState::empty();
rpc_setup = Some((shared_voter_state.clone())); let import_setup = (block_import.clone(), grandpa_link, babe_link.clone());
let rpc_setup = shared_voter_state.clone();
let babe_link = import_setup.as_ref().map(|s| &s.2)
.expect("BabeLink is present for full services or set up faile; qed.");
let babe_config = babe_link.config().clone(); let babe_config = babe_link.config().clone();
let shared_epoch_changes = babe_link.epoch_changes().clone(); let shared_epoch_changes = babe_link.epoch_changes().clone();
let client = builder.client().clone(); let rpc_extensions_builder = {
let pool = builder.pool().clone(); let client = client.clone();
let select_chain = builder.select_chain().cloned() let keystore = keystore.clone();
.expect("SelectChain is present for full services or set up failed; qed."); let transaction_pool = transaction_pool.clone();
let keystore = builder.keystore().clone(); let select_chain = select_chain.clone();
Ok(move |deny_unsafe| -> polkadot_rpc::RpcExtension { Box::new(move |deny_unsafe| -> polkadot_rpc::RpcExtension {
let deps = polkadot_rpc::FullDeps { let deps = polkadot_rpc::FullDeps {
client: client.clone(), client: client.clone(),
pool: pool.clone(), pool: transaction_pool.clone(),
select_chain: select_chain.clone(), select_chain: select_chain.clone(),
deny_unsafe, deny_unsafe,
babe: polkadot_rpc::BabeDeps { babe: polkadot_rpc::BabeDeps {
@@ -264,10 +274,22 @@ macro_rules! new_full_start {
polkadot_rpc::create_full(deps) polkadot_rpc::create_full(deps)
}) })
})?; };
(builder, import_setup, inherent_data_providers, rpc_setup) let provider = client.clone() as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
}} let finality_proof_provider = Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), provider)) as _;
let params = service::ServiceParams {
config, backend, client, import_queue, keystore, task_manager, rpc_extensions_builder,
transaction_pool,
block_announce_validator_builder: None,
finality_proof_provider: Some(finality_proof_provider),
finality_proof_request_builder: None,
on_demand: None,
remote_blockchain: None,
};
Ok((params, select_chain, import_setup, inherent_data_providers, rpc_setup))
} }
fn real_overseer<S: SpawnNamed>( fn real_overseer<S: SpawnNamed>(
@@ -294,49 +316,54 @@ fn real_overseer<S: SpawnNamed>(
).map_err(|e| ServiceError::Other(format!("Failed to create an Overseer: {:?}", e))) ).map_err(|e| ServiceError::Other(format!("Failed to create an Overseer: {:?}", e)))
} }
/// Builds a new service for a full client. #[cfg(feature = "full-node")]
#[macro_export] fn new_full<RuntimeApi, Executor, Extrinsic>(
macro_rules! new_full { config: Configuration,
( collating_for: Option<(CollatorId, ParaId)>,
$config:expr, _max_block_data_size: Option<u64>,
$collating_for:expr, _authority_discovery_disabled: bool,
$authority_discovery_disabled:expr, _slot_duration: u64,
$grandpa_pause:expr, grandpa_pause: Option<(u32, u32)>,
$runtime:ty, ) -> Result<(
$dispatch:ty, TaskManager,
) => {{ Arc<FullClient<RuntimeApi, Executor>>,
), Error>
where
RuntimeApi: ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi:
RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,
Executor: NativeExecutionDispatch + 'static,
Extrinsic: RuntimeExtrinsic,
{
use sc_client_api::ExecutorProvider; use sc_client_api::ExecutorProvider;
use sp_core::traits::BareCryptoStorePtr; use sp_core::traits::BareCryptoStorePtr;
let is_collator = $collating_for.is_some(); let is_collator = collating_for.is_some();
let role = $config.role.clone(); let role = config.role.clone();
let is_authority = role.is_authority() && !is_collator; let is_authority = role.is_authority() && !is_collator;
let force_authoring = $config.force_authoring; let force_authoring = config.force_authoring;
let disable_grandpa = $config.disable_grandpa; let disable_grandpa = config.disable_grandpa;
let name = $config.network.node_name.clone(); let name = config.network.node_name.clone();
let (builder, mut import_setup, inherent_data_providers, mut rpc_setup) = let (params, select_chain, import_setup, inherent_data_providers, rpc_setup)
new_full_start!($config, $runtime, $dispatch); = full_params::<RuntimeApi, Executor, Extrinsic>(config)?;
let client = params.client.clone();
let keystore = params.keystore.clone();
let transaction_pool = params.transaction_pool.clone();
let prometheus_registry = params.config.prometheus_registry().cloned();
let ServiceComponents { let ServiceComponents {
client, network, select_chain, keystore, transaction_pool, prometheus_registry, network, task_manager, telemetry_on_connect_sinks, ..
task_manager, telemetry_on_connect_sinks, .. } = service::build(params)?;
} = builder
.with_finality_proof_provider(|client, backend| {
let provider = client as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
})?
.build_full()?;
let (block_import, link_half, babe_link) = import_setup.take() let (block_import, link_half, babe_link) = import_setup;
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
let shared_voter_state = rpc_setup.take() let shared_voter_state = rpc_setup;
.expect("The SharedVoterState is present for Full Services or setup failed before. qed");
let overseer_client = client.clone(); let overseer_client = client.clone();
let spawner = task_manager.spawn_handle(); let spawner = task_manager.spawn_handle();
let leaves: Vec<_> = select_chain.clone().ok_or(ServiceError::SelectChainRequired)? let leaves: Vec<_> = select_chain.clone()
.leaves() .leaves()
.unwrap_or_else(|_| vec![]) .unwrap_or_else(|_| vec![])
.into_iter() .into_iter()
@@ -376,7 +403,6 @@ macro_rules! new_full {
})); }));
if role.is_authority() { if role.is_authority() {
let select_chain = select_chain.ok_or(ServiceError::SelectChainRequired)?;
let can_author_with = let can_author_with =
consensus_common::CanAuthorWithNativeVersion::new(client.executor().clone()); consensus_common::CanAuthorWithNativeVersion::new(client.executor().clone());
@@ -433,7 +459,7 @@ macro_rules! new_full {
// add a custom voting rule to temporarily stop voting for new blocks // add a custom voting rule to temporarily stop voting for new blocks
// after the given pause block is finalized and restarting after the // after the given pause block is finalized and restarting after the
// given delay. // given delay.
let voting_rule = match $grandpa_pause { let voting_rule = match grandpa_pause {
Some((block, delay)) => { Some((block, delay)) => {
info!("GRANDPA scheduled voting pause set for block #{} with a duration of {} blocks.", info!("GRANDPA scheduled voting pause set for block #{} with a duration of {} blocks.",
block, block,
@@ -472,57 +498,42 @@ macro_rules! new_full {
)?; )?;
} }
(task_manager, client) Ok((task_manager, client))
}}
} }
pub struct FullNodeHandles; pub struct FullNodeHandles;
/// Builds a new service for a light client. /// Builds a new service for a light client.
#[macro_export] fn new_light<Runtime, Dispatch, Extrinsic>(mut config: Configuration) -> Result<TaskManager, Error>
macro_rules! new_light { where
($config:expr, $runtime:ty, $dispatch:ty) => {{ Runtime: 'static + Send + Sync + ConstructRuntimeApi<Block, LightClient<Runtime, Dispatch>>,
crate::set_prometheus_registry(&mut $config)?; <Runtime as ConstructRuntimeApi<Block, LightClient<Runtime, Dispatch>>>::RuntimeApi:
let inherent_data_providers = inherents::InherentDataProviders::new(); RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<LightBackend, Block>>,
Dispatch: NativeExecutionDispatch + 'static,
Extrinsic: RuntimeExtrinsic,
{
crate::set_prometheus_registry(&mut config)?;
use sc_client_api::backend::RemoteBackend;
let (client, backend, keystore, task_manager, on_demand) =
service::new_light_parts::<Block, Runtime, Dispatch>(&config)?;
let select_chain = sc_consensus::LongestChain::new(backend.clone());
ServiceBuilder::new_light::<Block, $runtime, $dispatch>($config)?
.with_select_chain(|_, backend| {
Ok(sc_consensus::LongestChain::new(backend.clone()))
})?
.with_transaction_pool(|builder| {
let fetcher = builder.fetcher()
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;
let pool_api = sc_transaction_pool::LightChainApi::new( let pool_api = sc_transaction_pool::LightChainApi::new(
builder.client().clone(), client.clone(),
fetcher, on_demand.clone(),
); );
let pool = Arc::new(sc_transaction_pool::BasicPool::new_light( let transaction_pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
builder.config().transaction_pool.clone(), config.transaction_pool.clone(),
Arc::new(pool_api), Arc::new(pool_api),
builder.prometheus_registry(), config.prometheus_registry(),
builder.spawn_handle(), task_manager.spawn_handle(),
)); ));
Ok(pool)
})?
.with_import_queue_and_fprb(|
_config,
client,
backend,
fetcher,
mut select_chain,
_,
spawn_task_handle,
registry,
| {
let select_chain = select_chain.take()
.ok_or_else(|| service::Error::SelectChainRequired)?;
let fetch_checker = fetcher
.map(|fetcher| fetcher.checker().clone())
.ok_or_else(|| "Trying to start light import queue without active fetch checker")?;
let grandpa_block_import = grandpa::light_block_import( let grandpa_block_import = grandpa::light_block_import(
client.clone(), backend, &(client.clone() as Arc<_>), Arc::new(fetch_checker) client.clone(), backend.clone(), &(client.clone() as Arc<_>),
Arc::new(on_demand.checker().clone()),
)?; )?;
let finality_proof_import = grandpa_block_import.clone(); let finality_proof_import = grandpa_block_import.clone();
@@ -535,94 +546,101 @@ macro_rules! new_light {
client.clone(), client.clone(),
)?; )?;
let inherent_data_providers = inherents::InherentDataProviders::new();
// FIXME: pruning task isn't started since light client doesn't do `AuthoritySetup`. // FIXME: pruning task isn't started since light client doesn't do `AuthoritySetup`.
let import_queue = babe::import_queue( let import_queue = babe::import_queue(
babe_link, babe_link,
babe_block_import, babe_block_import,
None, None,
Some(Box::new(finality_proof_import)), Some(Box::new(finality_proof_import)),
client, client.clone(),
select_chain, select_chain.clone(),
inherent_data_providers.clone(), inherent_data_providers.clone(),
spawn_task_handle, &task_manager.spawn_handle(),
registry, config.prometheus_registry(),
)?; )?;
Ok((import_queue, finality_proof_request_builder)) let provider = client.clone() as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
})? let finality_proof_provider = Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), provider));
.with_finality_proof_provider(|client, backend| {
let provider = client as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
Ok(Arc::new(grandpa::FinalityProofProvider::new(backend, provider)) as _)
})?
.with_rpc_extensions(|builder| {
let fetcher = builder.fetcher()
.ok_or_else(|| "Trying to start node RPC without active fetcher")?;
let remote_blockchain = builder.remote_backend()
.ok_or_else(|| "Trying to start node RPC without active remote blockchain")?;
let light_deps = polkadot_rpc::LightDeps { let light_deps = polkadot_rpc::LightDeps {
remote_blockchain, remote_blockchain: backend.remote_blockchain(),
fetcher, fetcher: on_demand.clone(),
client: builder.client().clone(), client: client.clone(),
pool: builder.pool(), pool: transaction_pool.clone(),
}; };
Ok(polkadot_rpc::create_light(light_deps))
})? let rpc_extensions = polkadot_rpc::create_light(light_deps);
.build_light()
.map(|ServiceComponents { task_manager, .. }| task_manager) let ServiceComponents { task_manager, .. } = service::build(service::ServiceParams {
}} config,
block_announce_validator_builder: None,
finality_proof_request_builder: Some(finality_proof_request_builder),
finality_proof_provider: Some(finality_proof_provider),
on_demand: Some(on_demand),
remote_blockchain: Some(backend.remote_blockchain()),
rpc_extensions_builder: Box::new(service::NoopRpcExtensionBuilder(rpc_extensions)),
client: client.clone(),
transaction_pool: transaction_pool.clone(),
import_queue, keystore, backend, task_manager,
})?;
Ok(task_manager)
} }
/// Builds a new object suitable for chain operations. /// Builds a new object suitable for chain operations.
#[cfg(feature = "full-node")]
pub fn new_chain_ops<Runtime, Dispatch, Extrinsic>(mut config: Configuration) -> Result< pub fn new_chain_ops<Runtime, Dispatch, Extrinsic>(mut config: Configuration) -> Result<
( (
Arc<service::TFullClient<Block, Runtime, Dispatch>>, Arc<FullClient<Runtime, Dispatch>>,
Arc<TFullBackend<Block>>, Arc<FullBackend>,
consensus_common::import_queue::BasicQueue<Block, PrefixedMemoryDB<BlakeTwo256>>, consensus_common::import_queue::BasicQueue<Block, PrefixedMemoryDB<BlakeTwo256>>,
TaskManager, TaskManager,
), ),
ServiceError ServiceError
> >
where where
Runtime: ConstructRuntimeApi<Block, service::TFullClient<Block, Runtime, Dispatch>> + Send + Sync + 'static, Runtime: ConstructRuntimeApi<Block, FullClient<Runtime, Dispatch>> + Send + Sync + 'static,
Runtime::RuntimeApi: Runtime::RuntimeApi:
RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<TFullBackend<Block>, Block>>, RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,
Dispatch: NativeExecutionDispatch + 'static, Dispatch: NativeExecutionDispatch + 'static,
Extrinsic: RuntimeExtrinsic, Extrinsic: RuntimeExtrinsic,
{ {
config.keystore = service::config::KeystoreConfig::InMemory; config.keystore = service::config::KeystoreConfig::InMemory;
let (builder, _, _, _) = new_full_start!(config, Runtime, Dispatch); let (service::ServiceParams { client, backend, import_queue, task_manager, .. }, ..)
Ok(builder.to_chain_ops_parts()) = full_params::<Runtime, Dispatch, Extrinsic>(config)?;
Ok((client, backend, import_queue, task_manager))
} }
/// Create a new Polkadot service for a full node. /// Create a new Polkadot service for a full node.
#[cfg(feature = "full-node")] #[cfg(feature = "full-node")]
pub fn polkadot_new_full( pub fn polkadot_new_full(
mut config: Configuration, config: Configuration,
collating_for: Option<(CollatorId, ParaId)>, collating_for: Option<(CollatorId, ParaId)>,
_max_block_data_size: Option<u64>, max_block_data_size: Option<u64>,
_authority_discovery_disabled: bool, authority_discovery_disabled: bool,
_slot_duration: u64, slot_duration: u64,
grandpa_pause: Option<(u32, u32)>, grandpa_pause: Option<(u32, u32)>,
) )
-> Result<( -> Result<(
TaskManager, TaskManager,
Arc<impl PolkadotClient< Arc<impl PolkadotClient<
Block, Block,
TFullBackend<Block>, FullBackend,
polkadot_runtime::RuntimeApi polkadot_runtime::RuntimeApi
>>, >>,
FullNodeHandles, FullNodeHandles,
), ServiceError> ), ServiceError>
{ {
let (components, client) = new_full!( let (components, client) = new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor, _>(
config, config,
collating_for, collating_for,
max_block_data_size,
authority_discovery_disabled, authority_discovery_disabled,
slot_duration,
grandpa_pause, grandpa_pause,
polkadot_runtime::RuntimeApi, )?;
PolkadotExecutor,
);
Ok((components, client, FullNodeHandles)) Ok((components, client, FullNodeHandles))
} }
@@ -630,31 +648,31 @@ pub fn polkadot_new_full(
/// Create a new Kusama service for a full node. /// Create a new Kusama service for a full node.
#[cfg(feature = "full-node")] #[cfg(feature = "full-node")]
pub fn kusama_new_full( pub fn kusama_new_full(
mut config: Configuration, config: Configuration,
collating_for: Option<(CollatorId, ParaId)>, collating_for: Option<(CollatorId, ParaId)>,
_max_block_data_size: Option<u64>, max_block_data_size: Option<u64>,
_authority_discovery_disabled: bool, authority_discovery_disabled: bool,
_slot_duration: u64, slot_duration: u64,
grandpa_pause: Option<(u32, u32)>, grandpa_pause: Option<(u32, u32)>,
) -> Result<( ) -> Result<(
TaskManager, TaskManager,
Arc<impl PolkadotClient< Arc<impl PolkadotClient<
Block, Block,
TFullBackend<Block>, FullBackend,
kusama_runtime::RuntimeApi kusama_runtime::RuntimeApi
> >
>, >,
FullNodeHandles, FullNodeHandles,
), ServiceError> ), ServiceError>
{ {
let (components, client) = new_full!( let (components, client) = new_full::<kusama_runtime::RuntimeApi, KusamaExecutor, _>(
config, config,
collating_for, collating_for,
max_block_data_size,
authority_discovery_disabled, authority_discovery_disabled,
slot_duration,
grandpa_pause, grandpa_pause,
kusama_runtime::RuntimeApi, )?;
KusamaExecutor,
);
Ok((components, client, FullNodeHandles)) Ok((components, client, FullNodeHandles))
} }
@@ -662,49 +680,49 @@ pub fn kusama_new_full(
/// Create a new Kusama service for a full node. /// Create a new Kusama service for a full node.
#[cfg(feature = "full-node")] #[cfg(feature = "full-node")]
pub fn westend_new_full( pub fn westend_new_full(
mut config: Configuration, config: Configuration,
collating_for: Option<(CollatorId, ParaId)>, collating_for: Option<(CollatorId, ParaId)>,
_max_block_data_size: Option<u64>, max_block_data_size: Option<u64>,
_authority_discovery_disabled: bool, authority_discovery_disabled: bool,
_slot_duration: u64, slot_duration: u64,
grandpa_pause: Option<(u32, u32)>, grandpa_pause: Option<(u32, u32)>,
) )
-> Result<( -> Result<(
TaskManager, TaskManager,
Arc<impl PolkadotClient< Arc<impl PolkadotClient<
Block, Block,
TFullBackend<Block>, FullBackend,
westend_runtime::RuntimeApi westend_runtime::RuntimeApi
>>, >>,
FullNodeHandles, FullNodeHandles,
), ServiceError> ), ServiceError>
{ {
let (components, client) = new_full!( let (components, client) = new_full::<westend_runtime::RuntimeApi, WestendExecutor, _>(
config, config,
collating_for, collating_for,
max_block_data_size,
authority_discovery_disabled, authority_discovery_disabled,
slot_duration,
grandpa_pause, grandpa_pause,
westend_runtime::RuntimeApi, )?;
WestendExecutor,
);
Ok((components, client, FullNodeHandles)) Ok((components, client, FullNodeHandles))
} }
/// Create a new Polkadot service for a light client. /// Create a new Polkadot service for a light client.
pub fn polkadot_new_light(mut config: Configuration) -> Result<TaskManager, ServiceError> pub fn polkadot_new_light(config: Configuration) -> Result<TaskManager, ServiceError>
{ {
new_light!(config, polkadot_runtime::RuntimeApi, PolkadotExecutor) new_light::<polkadot_runtime::RuntimeApi, PolkadotExecutor, _>(config)
} }
/// Create a new Kusama service for a light client. /// Create a new Kusama service for a light client.
pub fn kusama_new_light(mut config: Configuration) -> Result<TaskManager, ServiceError> pub fn kusama_new_light(config: Configuration) -> Result<TaskManager, ServiceError>
{ {
new_light!(config, kusama_runtime::RuntimeApi, KusamaExecutor) new_light::<kusama_runtime::RuntimeApi, KusamaExecutor, _>(config)
} }
/// Create a new Westend service for a light client. /// Create a new Westend service for a light client.
pub fn westend_new_light(mut config: Configuration, ) -> Result<TaskManager, ServiceError> pub fn westend_new_light(config: Configuration, ) -> Result<TaskManager, ServiceError>
{ {
new_light!(config, westend_runtime::RuntimeApi, KusamaExecutor) new_light::<westend_runtime::RuntimeApi, KusamaExecutor, _>(config)
} }
+7 -11
View File
@@ -21,17 +21,13 @@
mod chain_spec; mod chain_spec;
pub use chain_spec::*; pub use chain_spec::*;
use consensus_common::{block_validation::Chain, SelectChain};
use futures::future::Future; use futures::future::Future;
use grandpa::FinalityProofProvider as GrandpaFinalityProofProvider;
use log::info;
use polkadot_network::{legacy::gossip::Known, protocol as network_protocol};
use polkadot_primitives::v0::{ use polkadot_primitives::v0::{
Block, BlockId, Hash, CollatorId, Id as ParaId, Block, Hash, CollatorId, Id as ParaId,
}; };
use polkadot_runtime_common::{parachains, registrar, BlockHashCount}; use polkadot_runtime_common::{parachains, registrar, BlockHashCount};
use polkadot_service::{ use polkadot_service::{
new_full, new_full_start, FullNodeHandles, PolkadotClient, ServiceComponents, new_full, FullNodeHandles, PolkadotClient,
}; };
use polkadot_test_runtime::{RestrictFunctionality, Runtime, SignedExtra, SignedPayload, VERSION}; use polkadot_test_runtime::{RestrictFunctionality, Runtime, SignedExtra, SignedPayload, VERSION};
use sc_chain_spec::ChainSpec; use sc_chain_spec::ChainSpec;
@@ -54,7 +50,6 @@ use sp_keyring::Sr25519Keyring;
use sp_runtime::{codec::Encode, generic}; use sp_runtime::{codec::Encode, generic};
use sp_state_machine::BasicExternalities; use sp_state_machine::BasicExternalities;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration;
use substrate_test_client::{BlockchainEventsExt, RpcHandlersExt, RpcTransactionOutput, RpcTransactionError}; use substrate_test_client::{BlockchainEventsExt, RpcHandlersExt, RpcTransactionOutput, RpcTransactionError};
native_executor_instance!( native_executor_instance!(
@@ -81,15 +76,16 @@ pub fn polkadot_test_new_full(
), ),
ServiceError, ServiceError,
> { > {
let (task_manager, client, handles, network, rpc_handlers) = new_full!(test let (task_manager, client, handles, network, rpc_handlers) =
new_full::<polkadot_test_runtime::RuntimeApi, PolkadotTestExecutor, _>(
config, config,
collating_for, collating_for,
max_block_data_size, max_block_data_size,
authority_discovery_disabled, authority_discovery_disabled,
slot_duration, slot_duration,
polkadot_test_runtime::RuntimeApi, None,
PolkadotTestExecutor, true,
); )?;
Ok((task_manager, client, handles, network, rpc_handlers)) Ok((task_manager, client, handles, network, rpc_handlers))
} }
+222 -270
View File
@@ -25,7 +25,7 @@ use std::time::Duration;
use polkadot_primitives::v0::{self as parachain, Hash, BlockId, AccountId, Nonce, Balance}; use polkadot_primitives::v0::{self as parachain, Hash, BlockId, AccountId, Nonce, Balance};
#[cfg(feature = "full-node")] #[cfg(feature = "full-node")]
use polkadot_network::{legacy::gossip::Known, protocol as network_protocol}; use polkadot_network::{legacy::gossip::Known, protocol as network_protocol};
use service::{error::Error as ServiceError, ServiceBuilder}; use service::{error::Error as ServiceError};
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider}; use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider};
use sc_executor::native_executor_instance; use sc_executor::native_executor_instance;
use log::info; use log::info;
@@ -135,60 +135,85 @@ impl IdentifyVariant for Box<dyn ChainSpec> {
} }
} }
/// Starts a `ServiceBuilder` for a full service. type FullBackend = service::TFullBackend<Block>;
/// type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
/// Use this macro if you don't actually need the full service, but just the builder in order to type FullClient<RuntimeApi, Executor> = service::TFullClient<Block, RuntimeApi, Executor>;
/// be able to perform chain operations. type FullGrandpaBlockImport<RuntimeApi, Executor> = grandpa::GrandpaBlockImport<
#[macro_export] FullBackend, Block, FullClient<RuntimeApi, Executor>, FullSelectChain
macro_rules! new_full_start { >;
(prometheus_setup $config:expr) => {{
type LightBackend = service::TLightBackendWithHash<Block, sp_runtime::traits::BlakeTwo256>;
type LightClient<RuntimeApi, Executor> =
service::TLightClientWithBackend<Block, RuntimeApi, Executor, LightBackend>;
#[cfg(feature = "full-node")]
pub fn full_params<RuntimeApi, Executor, Extrinsic>(mut config: Configuration, test: bool) -> Result<(
service::ServiceParams<
Block,
FullClient<RuntimeApi, Executor>,
babe::BabeImportQueue<Block, FullClient<RuntimeApi, Executor>>,
sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, Executor>>,
polkadot_rpc::RpcExtension,
FullBackend,
>,
FullSelectChain,
(
babe::BabeBlockImport<
Block, FullClient<RuntimeApi, Executor>, FullGrandpaBlockImport<RuntimeApi, Executor>
>,
grandpa::LinkHalf<Block, FullClient<RuntimeApi, Executor>, FullSelectChain>,
babe::BabeLink<Block>
),
inherents::InherentDataProviders,
grandpa::SharedVoterState,
), Error>
where
RuntimeApi: ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi:
RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,
Executor: NativeExecutionDispatch + 'static,
Extrinsic: RuntimeExtrinsic,
{
if !test {
// If we're using prometheus, use a registry with a prefix of `polkadot`. // If we're using prometheus, use a registry with a prefix of `polkadot`.
if let Some(PrometheusConfig { registry, .. }) = $config.prometheus_config.as_mut() { if let Some(PrometheusConfig { registry, .. }) = config.prometheus_config.as_mut() {
*registry = Registry::new_custom(Some("polkadot".into()), None)?; *registry = Registry::new_custom(Some("polkadot".into()), None)?;
} }
}}; }
(start_builder $config:expr, $runtime:ty, $executor:ty $(,)?) => {{
service::ServiceBuilder::new_full::< let inherent_data_providers = inherents::InherentDataProviders::new();
Block, $runtime, $executor
>($config)?
.with_select_chain(|_, backend| { let (client, backend, keystore, task_manager) =
Ok(sc_consensus::LongestChain::new(backend.clone())) service::new_full_parts::<Block, RuntimeApi, Executor>(&config)?;
})? let client = Arc::new(client);
.with_transaction_pool(|builder| {
let select_chain = sc_consensus::LongestChain::new(backend.clone());
let pool_api = sc_transaction_pool::FullChainApi::new( let pool_api = sc_transaction_pool::FullChainApi::new(
builder.client().clone(), client.clone(), config.prometheus_registry(),
builder.prometheus_registry(),
); );
let pool = sc_transaction_pool::BasicPool::new_full( let transaction_pool = sc_transaction_pool::BasicPool::new_full(
builder.config().transaction_pool.clone(), config.transaction_pool.clone(),
std::sync::Arc::new(pool_api), std::sync::Arc::new(pool_api),
builder.prometheus_registry(), config.prometheus_registry(),
builder.spawn_handle(), task_manager.spawn_handle(),
builder.client().clone(), client.clone(),
); );
Ok(pool)
})? let grandpa_hard_forks = if config.chain_spec.is_kusama() && !test {
}}; crate::grandpa_support::kusama_hard_forks()
(import_queue_setup } else {
$builder:expr, $inherent_data_providers:expr, $import_setup:expr, $grandpa_hard_forks:expr, $(,)? Vec::new()
) => {{ };
$builder.with_import_queue(|
_config,
client,
mut select_chain,
_,
spawn_task_handle,
registry,
| {
let select_chain = select_chain.take()
.ok_or_else(|| service::Error::SelectChainRequired)?;
let (grandpa_block_import, grandpa_link) = let (grandpa_block_import, grandpa_link) =
grandpa::block_import_with_authority_set_hard_forks( grandpa::block_import_with_authority_set_hard_forks(
client.clone(), client.clone(),
&(client.clone() as Arc<_>), &(client.clone() as Arc<_>),
select_chain.clone(), select_chain.clone(),
$grandpa_hard_forks, grandpa_hard_forks,
)?; )?;
let justification_import = grandpa_block_import.clone(); let justification_import = grandpa_block_import.clone();
@@ -204,45 +229,32 @@ macro_rules! new_full_start {
block_import.clone(), block_import.clone(),
Some(Box::new(justification_import)), Some(Box::new(justification_import)),
None, None,
client, client.clone(),
select_chain, select_chain.clone(),
$inherent_data_providers.clone(), inherent_data_providers.clone(),
spawn_task_handle, &task_manager.spawn_handle(),
registry, config.prometheus_registry(),
)?; )?;
$import_setup = Some((block_import, grandpa_link, babe_link));
Ok(import_queue)
})?
}};
(finish_builder_setup $builder:expr, $inherent_data_providers:expr, $import_setup:expr) => {{
let mut rpc_setup = None;
let builder = $builder.with_rpc_extensions_builder(|builder| {
let grandpa_link = $import_setup.as_ref().map(|s| &s.1)
.expect("GRANDPA LinkHalf is present for full services or set up failed; qed.");
let shared_authority_set = grandpa_link.shared_authority_set().clone(); let shared_authority_set = grandpa_link.shared_authority_set().clone();
let shared_voter_state = grandpa::SharedVoterState::empty(); let shared_voter_state = grandpa::SharedVoterState::empty();
rpc_setup = Some((shared_voter_state.clone())); let import_setup = (block_import.clone(), grandpa_link, babe_link.clone());
let rpc_setup = shared_voter_state.clone();
let babe_link = $import_setup.as_ref().map(|s| &s.2)
.expect("BabeLink is present for full services or set up faile; qed.");
let babe_config = babe_link.config().clone(); let babe_config = babe_link.config().clone();
let shared_epoch_changes = babe_link.epoch_changes().clone(); let shared_epoch_changes = babe_link.epoch_changes().clone();
let client = builder.client().clone(); let rpc_extensions_builder = {
let pool = builder.pool().clone(); let client = client.clone();
let select_chain = builder.select_chain().cloned() let keystore = keystore.clone();
.expect("SelectChain is present for full services or set up failed; qed."); let transaction_pool = transaction_pool.clone();
let keystore = builder.keystore().clone(); let select_chain = select_chain.clone();
Ok(move |deny_unsafe| -> polkadot_rpc::RpcExtension { Box::new(move |deny_unsafe| -> polkadot_rpc::RpcExtension {
let deps = polkadot_rpc::FullDeps { let deps = polkadot_rpc::FullDeps {
client: client.clone(), client: client.clone(),
pool: pool.clone(), pool: transaction_pool.clone(),
select_chain: select_chain.clone(), select_chain: select_chain.clone(),
deny_unsafe, deny_unsafe,
babe: polkadot_rpc::BabeDeps { babe: polkadot_rpc::BabeDeps {
@@ -258,91 +270,83 @@ macro_rules! new_full_start {
polkadot_rpc::create_full(deps) polkadot_rpc::create_full(deps)
}) })
})?;
(builder, $import_setup, $inherent_data_providers, rpc_setup)
}};
($config:expr, $runtime:ty, $executor:ty $(,)?) => {{
let inherent_data_providers = inherents::InherentDataProviders::new();
let mut import_setup = None;
new_full_start!(prometheus_setup $config);
let grandpa_hard_forks = if $config.chain_spec.is_kusama() {
$crate::grandpa_support::kusama_hard_forks()
} else {
Vec::new()
}; };
let builder = new_full_start!(start_builder $config, $runtime, $executor);
let builder = new_full_start!(import_queue_setup let provider = client.clone() as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
builder, inherent_data_providers, import_setup, grandpa_hard_forks, let finality_proof_provider = Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), provider)) as _;
);
new_full_start!(finish_builder_setup builder, inherent_data_providers, import_setup) let params = service::ServiceParams {
}}; config, backend, client, import_queue, keystore, task_manager, rpc_extensions_builder,
(test $config:expr, $runtime:ty, $executor:ty $(,)?) => {{ transaction_pool,
let inherent_data_providers = inherents::InherentDataProviders::new(); block_announce_validator_builder: None,
let mut import_setup = None; finality_proof_provider: Some(finality_proof_provider),
let grandpa_hard_forks = Vec::new(); finality_proof_request_builder: None,
let builder = new_full_start!(start_builder $config, $runtime, $executor); on_demand: None,
let builder = new_full_start!(import_queue_setup remote_blockchain: None,
builder, inherent_data_providers, import_setup, grandpa_hard_forks, };
);
new_full_start!(finish_builder_setup builder, inherent_data_providers, import_setup) Ok((params, select_chain, import_setup, inherent_data_providers, rpc_setup))
}};
} }
/// Builds a new service for a full client. #[cfg(feature = "full-node")]
#[macro_export] pub fn new_full<RuntimeApi, Executor, Extrinsic>(
macro_rules! new_full { config: Configuration,
( collating_for: Option<(CollatorId, parachain::Id)>,
with_full_start max_block_data_size: Option<u64>,
$config:expr, authority_discovery_disabled: bool,
$collating_for:expr, slot_duration: u64,
$max_block_data_size:expr, grandpa_pause: Option<(u32, u32)>,
$authority_discovery_disabled:expr, test: bool,
$slot_duration:expr, ) -> Result<(
$grandpa_pause:expr, TaskManager,
$new_full_start:expr $(,)? Arc<FullClient<RuntimeApi, Executor>>,
) => {{ FullNodeHandles,
Arc<sc_network::NetworkService<Block, <Block as BlockT>::Hash>>,
Arc<RpcHandlers>,
), Error>
where
RuntimeApi: ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi:
RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,
Executor: NativeExecutionDispatch + 'static,
Extrinsic: RuntimeExtrinsic,
{
use sc_network::Event; use sc_network::Event;
use sc_client_api::ExecutorProvider; use sc_client_api::ExecutorProvider;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use sp_core::traits::BareCryptoStorePtr; use sp_core::traits::BareCryptoStorePtr;
let is_collator = $collating_for.is_some(); let is_collator = collating_for.is_some();
let role = $config.role.clone(); let role = config.role.clone();
let is_authority = role.is_authority() && !is_collator; let is_authority = role.is_authority() && !is_collator;
let force_authoring = $config.force_authoring; let force_authoring = config.force_authoring;
let db_path = match $config.database.path() { let db_path = match config.database.path() {
Some(path) => std::path::PathBuf::from(path), Some(path) => std::path::PathBuf::from(path),
None => return Err("Starting a Polkadot service with a custom database isn't supported".to_string().into()), None => return Err("Starting a Polkadot service with a custom database isn't supported".to_string().into()),
}; };
let max_block_data_size = $max_block_data_size; let disable_grandpa = config.disable_grandpa;
let disable_grandpa = $config.disable_grandpa; let name = config.network.node_name.clone();
let name = $config.network.node_name.clone();
let authority_discovery_disabled = $authority_discovery_disabled;
let slot_duration = $slot_duration;
let (builder, mut import_setup, inherent_data_providers, mut rpc_setup) = $new_full_start; let (params, select_chain, import_setup, inherent_data_providers, rpc_setup)
= full_params::<RuntimeApi, Executor, Extrinsic>(config, test)?;
let client = params.client.clone();
let keystore = params.keystore.clone();
let transaction_pool = params.transaction_pool.clone();
let prometheus_registry = params.config.prometheus_registry().cloned();
let ServiceComponents { let ServiceComponents {
client, network, select_chain, keystore, transaction_pool, prometheus_registry, network,
task_manager, telemetry_on_connect_sinks, rpc_handlers, .. task_manager, telemetry_on_connect_sinks, rpc_handlers, ..
} = builder } = service::build(params)?;
.with_finality_proof_provider(|client, backend| {
let provider = client as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
})?
.build_full()?;
let (block_import, link_half, babe_link) = import_setup.take() let (block_import, link_half, babe_link) = import_setup;
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
let shared_voter_state = rpc_setup.take() let shared_voter_state = rpc_setup;
.expect("The SharedVoterState is present for Full Services or setup failed before. qed");
let known_oracle = client.clone(); let known_oracle = client.clone();
let mut handles = FullNodeHandles::default(); let mut handles = FullNodeHandles::default();
let select_chain = select_chain.ok_or(ServiceError::SelectChainRequired)?;
let gossip_validator_select_chain = select_chain.clone(); let gossip_validator_select_chain = select_chain.clone();
let is_known = move |block_hash: &Hash| { let is_known = move |block_hash: &Hash| {
@@ -367,7 +371,7 @@ macro_rules! new_full {
let polkadot_network_service = network_protocol::start( let polkadot_network_service = network_protocol::start(
network.clone(), network.clone(),
network_protocol::Config { network_protocol::Config {
collating_for: $collating_for, collating_for: collating_for,
}, },
(is_known, client.clone()), (is_known, client.clone()),
client.clone(), client.clone(),
@@ -522,7 +526,7 @@ macro_rules! new_full {
// add a custom voting rule to temporarily stop voting for new blocks // add a custom voting rule to temporarily stop voting for new blocks
// after the given pause block is finalized and restarting after the // after the given pause block is finalized and restarting after the
// given delay. // given delay.
let voting_rule = match $grandpa_pause { let voting_rule = match grandpa_pause {
Some((block, delay)) => { Some((block, delay)) => {
info!("GRANDPA scheduled voting pause set for block #{} with a duration of {} blocks.", info!("GRANDPA scheduled voting pause set for block #{} with a duration of {} blocks.",
block, block,
@@ -530,7 +534,7 @@ macro_rules! new_full {
); );
grandpa::VotingRulesBuilder::default() grandpa::VotingRulesBuilder::default()
.add($crate::grandpa_support::PauseAfterBlockFor(block, delay)) .add(crate::grandpa_support::PauseAfterBlockFor(block, delay))
.build() .build()
}, },
None => None =>
@@ -562,98 +566,44 @@ macro_rules! new_full {
} }
handles.polkadot_network = Some(polkadot_network_service); handles.polkadot_network = Some(polkadot_network_service);
(task_manager, client, handles, network, rpc_handlers) Ok((task_manager, client, handles, network, rpc_handlers))
}};
(
$config:expr,
$collating_for:expr,
$max_block_data_size:expr,
$authority_discovery_disabled:expr,
$slot_duration:expr,
$grandpa_pause:expr,
$runtime:ty,
$dispatch:ty,
) => {{
new_full!(with_full_start
$config,
$collating_for,
$max_block_data_size,
$authority_discovery_disabled,
$slot_duration,
$grandpa_pause,
new_full_start!($config, $runtime, $dispatch),
)
}};
(
test
$config:expr,
$collating_for:expr,
$max_block_data_size:expr,
$authority_discovery_disabled:expr,
$slot_duration:expr,
$runtime:ty,
$dispatch:ty,
) => {{
new_full!(with_full_start
$config,
$collating_for,
$max_block_data_size,
$authority_discovery_disabled,
$slot_duration,
None,
new_full_start!(test $config, $runtime, $dispatch),
)
}};
} }
/// Builds a new service for a light client. /// Builds a new service for a light client.
#[macro_export] fn new_light<Runtime, Dispatch, Extrinsic>(mut config: Configuration) -> Result<(TaskManager, Arc<RpcHandlers>), Error>
macro_rules! new_light { where
($config:expr, $runtime:ty, $dispatch:ty) => {{ Runtime: 'static + Send + Sync + ConstructRuntimeApi<Block, LightClient<Runtime, Dispatch>>,
<Runtime as ConstructRuntimeApi<Block, LightClient<Runtime, Dispatch>>>::RuntimeApi:
RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<LightBackend, Block>>,
Dispatch: NativeExecutionDispatch + 'static,
Extrinsic: RuntimeExtrinsic,
{
use sc_client_api::backend::RemoteBackend;
// If we're using prometheus, use a registry with a prefix of `polkadot`. // If we're using prometheus, use a registry with a prefix of `polkadot`.
if let Some(PrometheusConfig { registry, .. }) = $config.prometheus_config.as_mut() { if let Some(PrometheusConfig { registry, .. }) = config.prometheus_config.as_mut() {
*registry = Registry::new_custom(Some("polkadot".into()), None)?; *registry = Registry::new_custom(Some("polkadot".into()), None)?;
} }
let inherent_data_providers = inherents::InherentDataProviders::new();
ServiceBuilder::new_light::<Block, $runtime, $dispatch>($config)? let (client, backend, keystore, task_manager, on_demand) =
.with_select_chain(|_, backend| { service::new_light_parts::<Block, Runtime, Dispatch>(&config)?;
Ok(sc_consensus::LongestChain::new(backend.clone()))
})? let select_chain = sc_consensus::LongestChain::new(backend.clone());
.with_transaction_pool(|builder| {
let fetcher = builder.fetcher()
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;
let pool_api = sc_transaction_pool::LightChainApi::new( let pool_api = sc_transaction_pool::LightChainApi::new(
builder.client().clone(), client.clone(),
fetcher, on_demand.clone(),
); );
let pool = Arc::new(sc_transaction_pool::BasicPool::new_light( let transaction_pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
builder.config().transaction_pool.clone(), config.transaction_pool.clone(),
Arc::new(pool_api), Arc::new(pool_api),
builder.prometheus_registry(), config.prometheus_registry(),
builder.spawn_handle(), task_manager.spawn_handle(),
)); ));
Ok(pool)
})?
.with_import_queue_and_fprb(|
_config,
client,
backend,
fetcher,
mut select_chain,
_,
spawn_task_handle,
registry,
| {
let select_chain = select_chain.take()
.ok_or_else(|| service::Error::SelectChainRequired)?;
let fetch_checker = fetcher
.map(|fetcher| fetcher.checker().clone())
.ok_or_else(|| "Trying to start light import queue without active fetch checker")?;
let grandpa_block_import = grandpa::light_block_import( let grandpa_block_import = grandpa::light_block_import(
client.clone(), backend, &(client.clone() as Arc<_>), Arc::new(fetch_checker) client.clone(), backend.clone(), &(client.clone() as Arc<_>),
Arc::new(on_demand.checker().clone()),
)?; )?;
let finality_proof_import = grandpa_block_import.clone(); let finality_proof_import = grandpa_block_import.clone();
@@ -666,72 +616,77 @@ macro_rules! new_light {
client.clone(), client.clone(),
)?; )?;
let inherent_data_providers = inherents::InherentDataProviders::new();
// FIXME: pruning task isn't started since light client doesn't do `AuthoritySetup`. // FIXME: pruning task isn't started since light client doesn't do `AuthoritySetup`.
let import_queue = babe::import_queue( let import_queue = babe::import_queue(
babe_link, babe_link,
babe_block_import, babe_block_import,
None, None,
Some(Box::new(finality_proof_import)), Some(Box::new(finality_proof_import)),
client, client.clone(),
select_chain, select_chain.clone(),
inherent_data_providers.clone(), inherent_data_providers.clone(),
spawn_task_handle, &task_manager.spawn_handle(),
registry, config.prometheus_registry(),
)?; )?;
Ok((import_queue, finality_proof_request_builder)) let provider = client.clone() as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
})? let finality_proof_provider = Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), provider));
.with_finality_proof_provider(|client, backend| {
let provider = client as Arc<dyn grandpa::StorageAndProofProvider<_, _>>;
Ok(Arc::new(grandpa::FinalityProofProvider::new(backend, provider)) as _)
})?
.with_rpc_extensions(|builder| {
let fetcher = builder.fetcher()
.ok_or_else(|| "Trying to start node RPC without active fetcher")?;
let remote_blockchain = builder.remote_backend()
.ok_or_else(|| "Trying to start node RPC without active remote blockchain")?;
let light_deps = polkadot_rpc::LightDeps { let light_deps = polkadot_rpc::LightDeps {
remote_blockchain, remote_blockchain: backend.remote_blockchain(),
fetcher, fetcher: on_demand.clone(),
client: builder.client().clone(), client: client.clone(),
pool: builder.pool(), pool: transaction_pool.clone(),
}; };
Ok(polkadot_rpc::create_light(light_deps))
})? let rpc_extensions = polkadot_rpc::create_light(light_deps);
.build_light()
.map(|ServiceComponents { task_manager, rpc_handlers, .. }| { let ServiceComponents { task_manager, rpc_handlers, .. } = service::build(service::ServiceParams {
(task_manager, rpc_handlers) config,
}) block_announce_validator_builder: None,
}} finality_proof_request_builder: Some(finality_proof_request_builder),
finality_proof_provider: Some(finality_proof_provider),
on_demand: Some(on_demand),
remote_blockchain: Some(backend.remote_blockchain()),
rpc_extensions_builder: Box::new(service::NoopRpcExtensionBuilder(rpc_extensions)),
client: client.clone(),
transaction_pool: transaction_pool.clone(),
import_queue, keystore, backend, task_manager,
})?;
Ok((task_manager, rpc_handlers))
} }
/// Builds a new object suitable for chain operations. /// Builds a new object suitable for chain operations.
#[cfg(feature = "full-node")]
pub fn new_chain_ops<Runtime, Dispatch, Extrinsic>(mut config: Configuration) -> Result< pub fn new_chain_ops<Runtime, Dispatch, Extrinsic>(mut config: Configuration) -> Result<
( (
Arc<service::TFullClient<Block, Runtime, Dispatch>>, Arc<FullClient<Runtime, Dispatch>>,
Arc<TFullBackend<Block>>, Arc<FullBackend>,
consensus_common::import_queue::BasicQueue<Block, PrefixedMemoryDB<BlakeTwo256>>, consensus_common::import_queue::BasicQueue<Block, PrefixedMemoryDB<BlakeTwo256>>,
TaskManager, TaskManager,
), ),
ServiceError ServiceError
> >
where where
Runtime: ConstructRuntimeApi<Block, service::TFullClient<Block, Runtime, Dispatch>> + Send + Sync + 'static, Runtime: ConstructRuntimeApi<Block, FullClient<Runtime, Dispatch>> + Send + Sync + 'static,
Runtime::RuntimeApi: Runtime::RuntimeApi:
RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<TFullBackend<Block>, Block>>, RuntimeApiCollection<Extrinsic, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,
Dispatch: NativeExecutionDispatch + 'static, Dispatch: NativeExecutionDispatch + 'static,
Extrinsic: RuntimeExtrinsic, Extrinsic: RuntimeExtrinsic,
{ {
config.keystore = service::config::KeystoreConfig::InMemory; config.keystore = service::config::KeystoreConfig::InMemory;
let (builder, _, _, _) = new_full_start!(config, Runtime, Dispatch); let (service::ServiceParams { client, backend, import_queue, task_manager, .. }, ..)
Ok(builder.to_chain_ops_parts()) = full_params::<Runtime, Dispatch, Extrinsic>(config, false)?;
Ok((client, backend, import_queue, task_manager))
} }
/// Create a new Polkadot service for a full node. /// Create a new Polkadot service for a full node.
#[cfg(feature = "full-node")] #[cfg(feature = "full-node")]
pub fn polkadot_new_full( pub fn polkadot_new_full(
mut config: Configuration, config: Configuration,
collating_for: Option<(CollatorId, parachain::Id)>, collating_for: Option<(CollatorId, parachain::Id)>,
max_block_data_size: Option<u64>, max_block_data_size: Option<u64>,
authority_discovery_disabled: bool, authority_discovery_disabled: bool,
@@ -742,22 +697,21 @@ pub fn polkadot_new_full(
TaskManager, TaskManager,
Arc<impl PolkadotClient< Arc<impl PolkadotClient<
Block, Block,
TFullBackend<Block>, FullBackend,
polkadot_runtime::RuntimeApi polkadot_runtime::RuntimeApi
>>, >>,
FullNodeHandles, FullNodeHandles,
), ServiceError> ), ServiceError>
{ {
let (service, client, handles, _, _) = new_full!( let (service, client, handles, _, _) = new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor, _>(
config, config,
collating_for, collating_for,
max_block_data_size, max_block_data_size,
authority_discovery_disabled, authority_discovery_disabled,
slot_duration, slot_duration,
grandpa_pause, grandpa_pause,
polkadot_runtime::RuntimeApi, false,
PolkadotExecutor, )?;
);
Ok((service, client, handles)) Ok((service, client, handles))
} }
@@ -765,7 +719,7 @@ pub fn polkadot_new_full(
/// Create a new Kusama service for a full node. /// Create a new Kusama service for a full node.
#[cfg(feature = "full-node")] #[cfg(feature = "full-node")]
pub fn kusama_new_full( pub fn kusama_new_full(
mut config: Configuration, config: Configuration,
collating_for: Option<(CollatorId, parachain::Id)>, collating_for: Option<(CollatorId, parachain::Id)>,
max_block_data_size: Option<u64>, max_block_data_size: Option<u64>,
authority_discovery_disabled: bool, authority_discovery_disabled: bool,
@@ -775,23 +729,22 @@ pub fn kusama_new_full(
TaskManager, TaskManager,
Arc<impl PolkadotClient< Arc<impl PolkadotClient<
Block, Block,
TFullBackend<Block>, FullBackend,
kusama_runtime::RuntimeApi kusama_runtime::RuntimeApi
> >
>, >,
FullNodeHandles FullNodeHandles
), ServiceError> ), ServiceError>
{ {
let (service, client, handles, _, _) = new_full!( let (service, client, handles, _, _) = new_full::<kusama_runtime::RuntimeApi, KusamaExecutor, _>(
config, config,
collating_for, collating_for,
max_block_data_size, max_block_data_size,
authority_discovery_disabled, authority_discovery_disabled,
slot_duration, slot_duration,
grandpa_pause, grandpa_pause,
kusama_runtime::RuntimeApi, false,
KusamaExecutor, )?;
);
Ok((service, client, handles)) Ok((service, client, handles))
} }
@@ -799,7 +752,7 @@ pub fn kusama_new_full(
/// Create a new Kusama service for a full node. /// Create a new Kusama service for a full node.
#[cfg(feature = "full-node")] #[cfg(feature = "full-node")]
pub fn westend_new_full( pub fn westend_new_full(
mut config: Configuration, config: Configuration,
collating_for: Option<(CollatorId, parachain::Id)>, collating_for: Option<(CollatorId, parachain::Id)>,
max_block_data_size: Option<u64>, max_block_data_size: Option<u64>,
authority_discovery_disabled: bool, authority_discovery_disabled: bool,
@@ -810,22 +763,21 @@ pub fn westend_new_full(
TaskManager, TaskManager,
Arc<impl PolkadotClient< Arc<impl PolkadotClient<
Block, Block,
TFullBackend<Block>, FullBackend,
westend_runtime::RuntimeApi westend_runtime::RuntimeApi
>>, >>,
FullNodeHandles, FullNodeHandles,
), ServiceError> ), ServiceError>
{ {
let (service, client, handles, _, _) = new_full!( let (service, client, handles, _, _) = new_full::<westend_runtime::RuntimeApi, WestendExecutor, _>(
config, config,
collating_for, collating_for,
max_block_data_size, max_block_data_size,
authority_discovery_disabled, authority_discovery_disabled,
slot_duration, slot_duration,
grandpa_pause, grandpa_pause,
westend_runtime::RuntimeApi, false,
WestendExecutor, )?;
);
Ok((service, client, handles)) Ok((service, client, handles))
} }
@@ -842,25 +794,25 @@ pub struct FullNodeHandles {
} }
/// Create a new Polkadot service for a light client. /// Create a new Polkadot service for a light client.
pub fn polkadot_new_light(mut config: Configuration) -> Result< pub fn polkadot_new_light(config: Configuration) -> Result<
(TaskManager, Arc<RpcHandlers>), ServiceError (TaskManager, Arc<RpcHandlers>), ServiceError
> >
{ {
new_light!(config, polkadot_runtime::RuntimeApi, PolkadotExecutor) new_light::<polkadot_runtime::RuntimeApi, PolkadotExecutor, _>(config)
} }
/// Create a new Kusama service for a light client. /// Create a new Kusama service for a light client.
pub fn kusama_new_light(mut config: Configuration) -> Result< pub fn kusama_new_light(config: Configuration) -> Result<
(TaskManager, Arc<RpcHandlers>), ServiceError (TaskManager, Arc<RpcHandlers>), ServiceError
> >
{ {
new_light!(config, kusama_runtime::RuntimeApi, KusamaExecutor) new_light::<kusama_runtime::RuntimeApi, KusamaExecutor, _>(config)
} }
/// Create a new Westend service for a light client. /// Create a new Westend service for a light client.
pub fn westend_new_light(mut config: Configuration, ) -> Result< pub fn westend_new_light(config: Configuration, ) -> Result<
(TaskManager, Arc<RpcHandlers>), ServiceError (TaskManager, Arc<RpcHandlers>), ServiceError
> >
{ {
new_light!(config, westend_runtime::RuntimeApi, KusamaExecutor) new_light::<westend_runtime::RuntimeApi, KusamaExecutor, _>(config)
} }