Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Remove the service builder #6557

Merged
44 commits merged into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
869b70c
:)
expenses Jun 30, 2020
31c65a6
Slight tidy
expenses Jun 30, 2020
6ad0d79
Remove ServiceBuilderCommand
expenses Jun 30, 2020
b7399d6
Remove whitespace
expenses Jun 30, 2020
746186d
Merge remote-tracking branch 'parity/master' into ashley-chainops
expenses Jul 1, 2020
4db2f17
Merge branch 'ashley-chainops' of github.com:paritytech/substrate int…
expenses Jul 1, 2020
4020ff4
Keep task manager alive for check_block/import_blocks
expenses Jul 1, 2020
23c6f29
Pass task_manager to run_until_exit
expenses Jul 1, 2020
256ede8
Merge remote-tracking branch 'parity/master' into ashley-chainops
expenses Jul 1, 2020
66d0466
WIP
expenses Jul 1, 2020
8a5938c
Merge remote-tracking branch 'parity/master' into ashley-remove-servi…
expenses Jul 2, 2020
a8bdb37
Merge remote-tracking branch 'parity/master' into ashley-remove-servi…
expenses Jul 2, 2020
e44d988
WIP
expenses Jul 3, 2020
5185c16
Merge remote-tracking branch 'parity/master' into ashley-remove-servi…
expenses Jul 3, 2020
70e7fcd
Remove finality grandpa changes and fix transaction pool tests
expenses Jul 3, 2020
2d75728
Get rid of the macros
expenses Jul 3, 2020
d0f5ca2
Merge remote-tracking branch 'parity/master' into ashley-remove-servi…
expenses Jul 8, 2020
109b715
Merge remote-tracking branch 'parity/master' into ashley-remove-servi…
expenses Jul 8, 2020
2be524b
Simplify a few chain components creation APIs related to the service
expenses Jul 9, 2020
2bea18f
Merge remote-tracking branch 'parity/master' into ashley-txpool
expenses Jul 9, 2020
b452e17
Fix basic-authorship doc tests
expenses Jul 9, 2020
cf49f81
Remove DefaultQueue
expenses Jul 9, 2020
6c3b9ce
Update client/service/src/builder.rs
expenses Jul 9, 2020
fbb48b9
Move ExecutionExtensions comment around
expenses Jul 9, 2020
5abe000
Merge branch 'ashley-txpool' into ashley-remove-service-builder
expenses Jul 9, 2020
6f170f0
Remove unused BlakeTwo256
expenses Jul 9, 2020
37d1e10
Merge branch 'ashley-txpool' into ashley-remove-service-builder
expenses Jul 9, 2020
bca293d
Merge remote-tracking branch 'parity/master' into ashley-remove-servi…
expenses Jul 10, 2020
c0dc11d
Add sc-prelude
expenses Jul 10, 2020
fd699d9
Merge remote-tracking branch 'origin/master' into ashley-remove-servi…
expenses Jul 17, 2020
cce5562
Rename sc-prelude to sc-service-prelude
expenses Jul 17, 2020
03de1c7
Rename to sc-service-types
expenses Jul 17, 2020
eeb1c1d
Merge remote-tracking branch 'origin/master' into ashley-remove-servi…
expenses Jul 17, 2020
93ec9a3
Merge remote-tracking branch 'origin/master' into ashley-remove-servi…
expenses Jul 20, 2020
7744a9e
Improve service types
expenses Jul 20, 2020
5d52cd9
Merge remote-tracking branch 'origin/master' into ashley-remove-servi…
expenses Jul 20, 2020
3a1d2ec
Fix line widths
expenses Jul 20, 2020
472ce1a
Merge remote-tracking branch 'origin/master' into ashley-remove-servi…
expenses Jul 21, 2020
84022fe
Remove sc-service-types and move type definitions to crates
expenses Jul 21, 2020
a7ab615
Merge remote-tracking branch 'origin/master' into ashley-remove-servi…
expenses Jul 21, 2020
52f9c95
Update bin/node-template/node/src/service.rs
expenses Jul 21, 2020
bb1cb5a
Merge remote-tracking branch 'origin/master' into ashley-remove-servi…
expenses Jul 22, 2020
ed14ad1
Add TLightClientWithHash
expenses Jul 22, 2020
319ba0c
Rework types
expenses Jul 22, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

7 changes: 5 additions & 2 deletions bin/node-template/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +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;

impl SubstrateCli for Cli {
fn impl_name() -> String {
Expand Down Expand Up @@ -68,8 +70,9 @@ pub fn run() -> sc_cli::Result<()> {
Some(subcommand) => {
let runner = cli.create_runner(subcommand)?;
runner.run_subcommand(subcommand, |config| {
let (builder, _, _) = new_full_start!(config);
Ok(builder.to_chain_ops_parts())
let (ServiceParams { client, backend, task_manager, import_queue, .. }, ..)
= new_full_params(config)?;
Ok((client, backend, import_queue, task_manager))
})
}
None => {
Expand Down
287 changes: 133 additions & 154 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

use std::sync::Arc;
use std::time::Duration;
use sc_client_api::ExecutorProvider;
use sc_consensus::LongestChain;
use node_template_runtime::{self, opaque::Block, RuntimeApi};
use sc_service::{
error::{Error as ServiceError}, Configuration, ServiceBuilder, ServiceComponents,
TaskManager,
};
use sc_client_api::{ExecutorProvider, RemoteBackend};
use node_template_runtime::{self, Block, RuntimeApi};
use sc_service::{error::Error as ServiceError, Configuration, ServiceComponents, TaskManager};
use sp_inherents::InherentDataProviders;
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
Expand All @@ -24,103 +20,110 @@ native_executor_instance!(
node_template_runtime::native_version,
);

/// 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) => {{
use std::sync::Arc;
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
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> {
let inherent_data_providers = sp_inherents::InherentDataProviders::new();

let mut import_setup = None;
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
let (client, backend, keystore, task_manager) =
sc_service::new_full_parts::<Block, RuntimeApi, Executor>(&config)?;
let client = Arc::new(client);

let builder = sc_service::ServiceBuilder::new_full::<
node_template_runtime::opaque::Block,
node_template_runtime::RuntimeApi,
crate::service::Executor
>($config)?
.with_select_chain(|_config, backend| {
Ok(sc_consensus::LongestChain::new(backend.clone()))
})?
.with_transaction_pool(|builder| {
let pool_api = sc_transaction_pool::FullChainApi::new(
builder.client().clone(),
None,
);
Ok(sc_transaction_pool::BasicPool::new_full(
builder.config().transaction_pool.clone(),
std::sync::Arc::new(pool_api),
builder.prometheus_registry(),
builder.spawn_handle(),
builder.client().clone(),
))
})?
.with_import_queue(|
_config,
client,
mut select_chain,
_transaction_pool,
spawn_task_handle,
registry,
| {
let select_chain = select_chain.take()
.ok_or_else(|| sc_service::Error::SelectChainRequired)?;
let select_chain = sc_consensus::LongestChain::new(backend.clone());

let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(
client.clone(),
&(client.clone() as Arc<_>),
select_chain,
)?;
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(),
);

let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
grandpa_block_import.clone(), client.clone(),
);
let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(
client.clone(), &(client.clone() as Arc<_>), select_chain.clone(),
)?;

let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?,
aura_block_import,
Some(Box::new(grandpa_block_import.clone())),
None,
client,
inherent_data_providers.clone(),
spawn_task_handle,
registry,
)?;
let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
grandpa_block_import.clone(), client.clone(),
);

import_setup = Some((grandpa_block_import, grandpa_link));
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?,
aura_block_import,
Some(Box::new(grandpa_block_import.clone())),
None,
client.clone(),
inherent_data_providers.clone(),
&task_manager.spawn_handle(),
config.prometheus_registry(),
)?;

Ok(import_queue)
})?;
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(|_| ()),
};

(builder, import_setup, inherent_data_providers)
}}
Ok((
params, select_chain, inherent_data_providers,
grandpa_block_import, grandpa_link,
))
}

/// Builds a new service for a full client.
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let role = config.role.clone();
let force_authoring = config.force_authoring;
let name = config.network.node_name.clone();
let disable_grandpa = config.disable_grandpa;
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;

let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config);
(
config.role.clone(),
config.force_authoring,
config.network.node_name.clone(),
!config.disable_grandpa,
config.prometheus_registry().cloned(),

let (block_import, grandpa_link) =
import_setup.take()
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
client.clone(), transaction_pool.clone(), keystore.clone(),
)
};

let ServiceComponents {
client, transaction_pool, task_manager, keystore, network, select_chain,
prometheus_registry, telemetry_on_connect_sinks, ..
} = builder
.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 _)
})?
.build_full()?;
task_manager, network, telemetry_on_connect_sinks, ..
} = sc_service::build(params)?;

if role.is_authority() {
let proposer = sc_basic_authorship::ProposerFactory::new(
Expand All @@ -129,9 +132,6 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
prometheus_registry.as_ref(),
);

let select_chain = select_chain
.ok_or(ServiceError::SelectChainRequired)?;

let can_author_with =
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());

Expand Down Expand Up @@ -171,7 +171,6 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
is_authority: role.is_network_authority(),
};

let enable_grandpa = !disable_grandpa;
if enable_grandpa {
// start the full GRANDPA voter
// NOTE: non-authorities could run the GRANDPA observer protocol, but at
Expand Down Expand Up @@ -209,69 +208,49 @@ 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 inherent_data_providers = InherentDataProviders::new();
let (client, backend, keystore, 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(
config.transaction_pool.clone(),
transaction_pool_api,
config.prometheus_registry(),
task_manager.spawn_handle(),
);

ServiceBuilder::new_light::<Block, RuntimeApi, Executor>(config)?
.with_select_chain(|_config, backend| {
Ok(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 grandpa_block_import = sc_finality_grandpa::light_block_import(
client.clone(), backend.clone(), &(client.clone() as Arc<_>),
Arc::new(on_demand.checker().clone()) as Arc<_>,
)?;
let finality_proof_import = grandpa_block_import.clone();
let finality_proof_request_builder =
finality_proof_import.create_finality_proof_request_builder();

let pool_api = sc_transaction_pool::LightChainApi::new(
builder.client().clone(),
fetcher,
);
let pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
builder.config().transaction_pool.clone(),
Arc::new(pool_api),
builder.prometheus_registry(),
builder.spawn_handle(),
));
Ok(pool)
})?
.with_import_queue_and_fprb(|
_config,
client,
backend,
fetcher,
_select_chain,
_tx_pool,
spawn_task_handle,
prometheus_registry,
| {
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 = sc_finality_grandpa::light_block_import(
client.clone(),
backend,
&(client.clone() as Arc<_>),
Arc::new(fetch_checker),
)?;
let finality_proof_import = grandpa_block_import.clone();
let finality_proof_request_builder =
finality_proof_import.create_finality_proof_request_builder();
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?,
grandpa_block_import,
None,
Some(Box::new(finality_proof_import)),
client.clone(),
InherentDataProviders::new(),
&task_manager.spawn_handle(),
config.prometheus_registry(),
)?;

let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?,
grandpa_block_import,
None,
Some(Box::new(finality_proof_import)),
client,
inherent_data_providers.clone(),
spawn_task_handle,
prometheus_registry,
)?;
let finality_proof_provider =
Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), client.clone() as Arc<_>));

Ok((import_queue, finality_proof_request_builder))
})?
.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 _)
})?
.build_light()
.map(|ServiceComponents { task_manager, .. }| task_manager)
sc_service::build(sc_service::ServiceParams {
expenses marked this conversation as resolved.
Show resolved Hide resolved
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(|_| ()),
transaction_pool: Arc::new(transaction_pool),
config, client, import_queue, keystore, backend, task_manager
}).map(|ServiceComponents { task_manager, .. }| task_manager)
}
7 changes: 5 additions & 2 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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;

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