Skip to content

Commit

Permalink
Various small improvements to service construction. (paritytech#6738)
Browse files Browse the repository at this point in the history
* Remove service components and add build_network, build_offchain_workers etc

* Improve transaction pool api

* Remove commented out line

* Add PartialComponents

* Add BuildNetworkParams, documentation

* Remove unused imports in tests

* Apply suggestions from code review

Co-authored-by: Nikolay Volf <[email protected]>

* Remove unused imports in node-bench

Co-authored-by: Nikolay Volf <[email protected]>
  • Loading branch information
expenses and NikVolf authored Jul 28, 2020
1 parent f488598 commit b066029
Show file tree
Hide file tree
Showing 17 changed files with 404 additions and 329 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::chain_spec;
use crate::cli::Cli;
use crate::service;
use sc_cli::{SubstrateCli, RuntimeVersion, Role, ChainSpec};
use sc_service::ServiceParams;
use crate::service::new_full_params;
use sc_service::PartialComponents;
use crate::service::new_partial;

impl SubstrateCli for Cli {
fn impl_name() -> String {
Expand Down Expand Up @@ -70,8 +70,8 @@ pub fn run() -> sc_cli::Result<()> {
Some(subcommand) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| {
let (ServiceParams { client, backend, task_manager, import_queue, .. }, ..)
= new_full_params(config)?;
let PartialComponents { client, backend, task_manager, import_queue, .. }
= new_partial(&config)?;
Ok((client, backend, import_queue, task_manager))
})
}
Expand Down
187 changes: 104 additions & 83 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use std::sync::Arc;
use std::time::Duration;
use sc_client_api::{ExecutorProvider, RemoteBackend};
use node_template_runtime::{self, opaque::Block, RuntimeApi};
use sc_service::{error::Error as ServiceError, Configuration, ServiceComponents, TaskManager};
use sc_service::{error::Error as ServiceError, Configuration, TaskManager};
use sp_inherents::InherentDataProviders;
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
use sc_finality_grandpa::{
FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider, SharedVoterState,
};
use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState};

// Our native executor instance.
native_executor_instance!(
Expand All @@ -24,18 +22,15 @@ type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;

pub fn new_full_params(config: Configuration) -> Result<(
sc_service::ServiceParams<
Block, FullClient,
sc_consensus_aura::AuraImportQueue<Block, FullClient>,
sc_transaction_pool::FullPool<Block, FullClient>,
(), FullBackend,
>,
FullSelectChain,
sp_inherents::InherentDataProviders,
sc_finality_grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>,
sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>
), ServiceError> {
pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponents<
FullClient, FullBackend, FullSelectChain,
sc_consensus_aura::AuraImportQueue<Block, FullClient>,
sc_transaction_pool::FullPool<Block, FullClient>,
(
sc_finality_grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>,
sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>
)
>, ServiceError> {
let inherent_data_providers = sp_inherents::InherentDataProviders::new();

let (client, backend, keystore, task_manager) =
Expand All @@ -44,12 +39,8 @@ pub fn new_full_params(config: Configuration) -> Result<(

let select_chain = sc_consensus::LongestChain::new(backend.clone());

let pool_api = sc_transaction_pool::FullChainApi::new(
client.clone(), config.prometheus_registry(),
);
let transaction_pool = sc_transaction_pool::BasicPool::new_full(
config.transaction_pool.clone(),
std::sync::Arc::new(pool_api),
config.prometheus_registry(),
task_manager.spawn_handle(),
client.clone(),
Expand All @@ -74,56 +65,62 @@ pub fn new_full_params(config: Configuration) -> Result<(
config.prometheus_registry(),
)?;

let provider = client.clone() as Arc<dyn StorageAndProofProvider<_, _>>;
let finality_proof_provider =
Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), provider));

let params = sc_service::ServiceParams {
backend, client, import_queue, keystore, task_manager, transaction_pool,
config,
block_announce_validator_builder: None,
finality_proof_request_builder: None,
finality_proof_provider: Some(finality_proof_provider),
on_demand: None,
remote_blockchain: None,
rpc_extensions_builder: Box::new(|_| ()),
};

Ok((
params, select_chain, inherent_data_providers,
grandpa_block_import, grandpa_link,
))
Ok(sc_service::PartialComponents {
client, backend, task_manager, import_queue, keystore, select_chain, transaction_pool,
inherent_data_providers,
other: (grandpa_block_import, grandpa_link),
})
}

/// Builds a new service for a full client.
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let (
params, select_chain, inherent_data_providers,
block_import, grandpa_link,
) = new_full_params(config)?;

let (
role, force_authoring, name, enable_grandpa, prometheus_registry,
client, transaction_pool, keystore,
) = {
let sc_service::ServiceParams {
config, client, transaction_pool, keystore, ..
} = &params;

(
config.role.clone(),
config.force_authoring,
config.network.node_name.clone(),
!config.disable_grandpa,
config.prometheus_registry().cloned(),

client.clone(), transaction_pool.clone(), keystore.clone(),
)
};
let sc_service::PartialComponents {
client, backend, mut task_manager, import_queue, keystore, select_chain, transaction_pool,
inherent_data_providers,
other: (block_import, grandpa_link),
} = new_partial(&config)?;

let finality_proof_provider =
GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());

let (network, network_status_sinks, system_rpc_tx) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: &config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
spawn_handle: task_manager.spawn_handle(),
import_queue,
on_demand: None,
block_announce_validator_builder: None,
finality_proof_request_builder: None,
finality_proof_provider: Some(finality_proof_provider.clone()),
})?;

if config.offchain_worker.enabled {
sc_service::build_offchain_workers(
&config, backend.clone(), task_manager.spawn_handle(), client.clone(), network.clone(),
);
}

let role = config.role.clone();
let force_authoring = config.force_authoring;
let name = config.network.node_name.clone();
let enable_grandpa = !config.disable_grandpa;
let prometheus_registry = config.prometheus_registry().cloned();
let telemetry_connection_sinks = sc_service::TelemetryConnectionSinks::default();

let ServiceComponents {
task_manager, network, telemetry_on_connect_sinks, ..
} = sc_service::build(params)?;
sc_service::spawn_tasks(sc_service::SpawnTasksParams {
network: network.clone(),
client: client.clone(),
keystore: keystore.clone(),
task_manager: &mut task_manager,
transaction_pool: transaction_pool.clone(),
telemetry_connection_sinks: telemetry_connection_sinks.clone(),
rpc_extensions_builder: Box::new(|_| ()),
on_demand: None,
remote_blockchain: None,
backend, network_status_sinks, system_rpc_tx, config,
})?;

if role.is_authority() {
let proposer = sc_basic_authorship::ProposerFactory::new(
Expand Down Expand Up @@ -183,7 +180,7 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
link: grandpa_link,
network,
inherent_data_providers,
telemetry_on_connect: Some(telemetry_on_connect_sinks.on_connect_stream()),
telemetry_on_connect: Some(telemetry_connection_sinks.on_connect_stream()),
voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),
prometheus_registry,
shared_voter_state: SharedVoterState::empty(),
Expand All @@ -208,18 +205,16 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {

/// Builds a new service for a light client.
pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
let (client, backend, keystore, task_manager, on_demand) =
let (client, backend, keystore, mut task_manager, on_demand) =
sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;

let transaction_pool_api = Arc::new(sc_transaction_pool::LightChainApi::new(
client.clone(), on_demand.clone(),
));
let transaction_pool = sc_transaction_pool::BasicPool::new_light(

let transaction_pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
config.transaction_pool.clone(),
transaction_pool_api,
config.prometheus_registry(),
task_manager.spawn_handle(),
);
client.clone(),
on_demand.clone(),
));

let grandpa_block_import = sc_finality_grandpa::light_block_import(
client.clone(), backend.clone(), &(client.clone() as Arc<_>),
Expand All @@ -241,16 +236,42 @@ pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
)?;

let finality_proof_provider =
Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), client.clone() as Arc<_>));
GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());

sc_service::build(sc_service::ServiceParams {
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),
let (network, network_status_sinks, system_rpc_tx) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: &config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
spawn_handle: task_manager.spawn_handle(),
import_queue,
on_demand: Some(on_demand.clone()),
block_announce_validator_builder: None,
finality_proof_request_builder: Some(finality_proof_request_builder),
finality_proof_provider: Some(finality_proof_provider),
})?;

if config.offchain_worker.enabled {
sc_service::build_offchain_workers(
&config, backend.clone(), task_manager.spawn_handle(), client.clone(), network.clone(),
);
}

sc_service::spawn_tasks(sc_service::SpawnTasksParams {
remote_blockchain: Some(backend.remote_blockchain()),
transaction_pool,
task_manager: &mut task_manager,
on_demand: Some(on_demand),
rpc_extensions_builder: Box::new(|_| ()),
transaction_pool: Arc::new(transaction_pool),
config, client, import_queue, keystore, backend, task_manager
}).map(|ServiceComponents { task_manager, .. }| task_manager)
telemetry_connection_sinks: sc_service::TelemetryConnectionSinks::default(),
config,
client,
keystore,
backend,
network,
network_status_sinks,
system_rpc_tx,
})?;

Ok(task_manager)
}
5 changes: 2 additions & 3 deletions bin/node/bench/src/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
//! The goal of this benchmark is to figure out time needed to fill
//! the transaction pool for the next block.
use std::{borrow::Cow, sync::Arc};
use std::borrow::Cow;

use node_testing::bench::{BenchDb, Profile, BlockType, KeyTypes, DatabaseType};

use sc_transaction_pool::{BasicPool, FullChainApi};
use sc_transaction_pool::BasicPool;
use sp_runtime::generic::BlockId;
use sp_transaction_pool::{TransactionPool, TransactionSource};

Expand Down Expand Up @@ -74,7 +74,6 @@ impl core::Benchmark for PoolBenchmark {
let executor = sp_core::testing::TaskExecutor::new();
let txpool = BasicPool::new_full(
Default::default(),
Arc::new(FullChainApi::new(context.client.clone(), None)),
None,
executor,
context.client.clone(),
Expand Down
8 changes: 4 additions & 4 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{chain_spec, service, Cli, Subcommand};
use node_executor::Executor;
use node_runtime::{Block, RuntimeApi};
use sc_cli::{Result, SubstrateCli, RuntimeVersion, Role, ChainSpec};
use sc_service::ServiceParams;
use crate::service::new_full_params;
use sc_service::PartialComponents;
use crate::service::new_partial;

impl SubstrateCli for Cli {
fn impl_name() -> String {
Expand Down Expand Up @@ -96,8 +96,8 @@ pub fn run() -> Result<()> {
Some(Subcommand::Base(subcommand)) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| {
let (ServiceParams { client, backend, import_queue, task_manager, .. }, ..)
= new_full_params(config)?;
let PartialComponents { client, backend, task_manager, import_queue, ..}
= new_partial(&config)?;
Ok((client, backend, import_queue, task_manager))
})
}
Expand Down
Loading

0 comments on commit b066029

Please sign in to comment.