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

Commit 08c3d65

Browse files
expensesandresilvaseunlanlege
authored
Remove the service builder (#6557)
* :) * Slight tidy * Remove ServiceBuilderCommand * Remove whitespace * Keep task manager alive for check_block/import_blocks * Pass task_manager to run_until_exit * WIP * WIP * Get rid of the macros * Simplify a few chain components creation APIs related to the service * Fix basic-authorship doc tests * Remove DefaultQueue * Update client/service/src/builder.rs Co-authored-by: André Silva <[email protected]> * Move ExecutionExtensions comment around * Remove unused BlakeTwo256 * Add sc-prelude * Rename sc-prelude to sc-service-prelude * Rename to sc-service-types * Improve service types * Fix line widths * Remove sc-service-types and move type definitions to crates * Update bin/node-template/node/src/service.rs Co-authored-by: Seun Lanlege <[email protected]> * Add TLightClientWithHash * Rework types Co-authored-by: André Silva <[email protected]> Co-authored-by: Seun Lanlege <[email protected]>
1 parent 909559c commit 08c3d65

File tree

12 files changed

+713
-1279
lines changed

12 files changed

+713
-1279
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node-template/node/src/command.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::chain_spec;
1919
use crate::cli::Cli;
2020
use crate::service;
2121
use sc_cli::{SubstrateCli, RuntimeVersion, Role, ChainSpec};
22+
use sc_service::ServiceParams;
23+
use crate::service::new_full_params;
2224

2325
impl SubstrateCli for Cli {
2426
fn impl_name() -> String {
@@ -68,8 +70,9 @@ pub fn run() -> sc_cli::Result<()> {
6870
Some(subcommand) => {
6971
let runner = cli.create_runner(subcommand)?;
7072
runner.run_subcommand(subcommand, |config| {
71-
let (builder, _, _) = new_full_start!(config);
72-
Ok(builder.to_chain_ops_parts())
73+
let (ServiceParams { client, backend, task_manager, import_queue, .. }, ..)
74+
= new_full_params(config)?;
75+
Ok((client, backend, import_queue, task_manager))
7376
})
7477
}
7578
None => {

bin/node-template/node/src/service.rs

Lines changed: 133 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
33
use std::sync::Arc;
44
use std::time::Duration;
5-
use sc_client_api::ExecutorProvider;
6-
use sc_consensus::LongestChain;
7-
use node_template_runtime::{self, opaque::Block, RuntimeApi};
8-
use sc_service::{
9-
error::{Error as ServiceError}, Configuration, ServiceBuilder, ServiceComponents,
10-
TaskManager,
11-
};
5+
use sc_client_api::{ExecutorProvider, RemoteBackend};
6+
use node_template_runtime::{self, Block, RuntimeApi};
7+
use sc_service::{error::Error as ServiceError, Configuration, ServiceComponents, TaskManager};
128
use sp_inherents::InherentDataProviders;
139
use sc_executor::native_executor_instance;
1410
pub use sc_executor::NativeExecutor;
@@ -24,103 +20,110 @@ native_executor_instance!(
2420
node_template_runtime::native_version,
2521
);
2622

27-
/// Starts a `ServiceBuilder` for a full service.
28-
///
29-
/// Use this macro if you don't actually need the full service, but just the builder in order to
30-
/// be able to perform chain operations.
31-
macro_rules! new_full_start {
32-
($config:expr) => {{
33-
use std::sync::Arc;
34-
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
23+
type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
24+
type FullBackend = sc_service::TFullBackend<Block>;
25+
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
26+
27+
pub fn new_full_params(config: Configuration) -> Result<(
28+
sc_service::ServiceParams<
29+
Block, FullClient,
30+
sc_consensus_aura::AuraImportQueue<Block, FullClient>,
31+
sc_transaction_pool::FullPool<Block, FullClient>,
32+
(), FullBackend,
33+
>,
34+
FullSelectChain,
35+
sp_inherents::InherentDataProviders,
36+
sc_finality_grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>,
37+
sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>
38+
), ServiceError> {
39+
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
3540

36-
let mut import_setup = None;
37-
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
41+
let (client, backend, keystore, task_manager) =
42+
sc_service::new_full_parts::<Block, RuntimeApi, Executor>(&config)?;
43+
let client = Arc::new(client);
3844

39-
let builder = sc_service::ServiceBuilder::new_full::<
40-
node_template_runtime::opaque::Block,
41-
node_template_runtime::RuntimeApi,
42-
crate::service::Executor
43-
>($config)?
44-
.with_select_chain(|_config, backend| {
45-
Ok(sc_consensus::LongestChain::new(backend.clone()))
46-
})?
47-
.with_transaction_pool(|builder| {
48-
let pool_api = sc_transaction_pool::FullChainApi::new(
49-
builder.client().clone(),
50-
None,
51-
);
52-
Ok(sc_transaction_pool::BasicPool::new_full(
53-
builder.config().transaction_pool.clone(),
54-
std::sync::Arc::new(pool_api),
55-
builder.prometheus_registry(),
56-
builder.spawn_handle(),
57-
builder.client().clone(),
58-
))
59-
})?
60-
.with_import_queue(|
61-
_config,
62-
client,
63-
mut select_chain,
64-
_transaction_pool,
65-
spawn_task_handle,
66-
registry,
67-
| {
68-
let select_chain = select_chain.take()
69-
.ok_or_else(|| sc_service::Error::SelectChainRequired)?;
45+
let select_chain = sc_consensus::LongestChain::new(backend.clone());
7046

71-
let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(
72-
client.clone(),
73-
&(client.clone() as Arc<_>),
74-
select_chain,
75-
)?;
47+
let pool_api = sc_transaction_pool::FullChainApi::new(
48+
client.clone(), config.prometheus_registry(),
49+
);
50+
let transaction_pool = sc_transaction_pool::BasicPool::new_full(
51+
config.transaction_pool.clone(),
52+
std::sync::Arc::new(pool_api),
53+
config.prometheus_registry(),
54+
task_manager.spawn_handle(),
55+
client.clone(),
56+
);
7657

77-
let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
78-
grandpa_block_import.clone(), client.clone(),
79-
);
58+
let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(
59+
client.clone(), &(client.clone() as Arc<_>), select_chain.clone(),
60+
)?;
8061

81-
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
82-
sc_consensus_aura::slot_duration(&*client)?,
83-
aura_block_import,
84-
Some(Box::new(grandpa_block_import.clone())),
85-
None,
86-
client,
87-
inherent_data_providers.clone(),
88-
spawn_task_handle,
89-
registry,
90-
)?;
62+
let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
63+
grandpa_block_import.clone(), client.clone(),
64+
);
9165

92-
import_setup = Some((grandpa_block_import, grandpa_link));
66+
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
67+
sc_consensus_aura::slot_duration(&*client)?,
68+
aura_block_import,
69+
Some(Box::new(grandpa_block_import.clone())),
70+
None,
71+
client.clone(),
72+
inherent_data_providers.clone(),
73+
&task_manager.spawn_handle(),
74+
config.prometheus_registry(),
75+
)?;
9376

94-
Ok(import_queue)
95-
})?;
77+
let provider = client.clone() as Arc<dyn StorageAndProofProvider<_, _>>;
78+
let finality_proof_provider =
79+
Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), provider));
80+
81+
let params = sc_service::ServiceParams {
82+
backend, client, import_queue, keystore, task_manager, transaction_pool,
83+
config,
84+
block_announce_validator_builder: None,
85+
finality_proof_request_builder: None,
86+
finality_proof_provider: Some(finality_proof_provider),
87+
on_demand: None,
88+
remote_blockchain: None,
89+
rpc_extensions_builder: Box::new(|_| ()),
90+
};
9691

97-
(builder, import_setup, inherent_data_providers)
98-
}}
92+
Ok((
93+
params, select_chain, inherent_data_providers,
94+
grandpa_block_import, grandpa_link,
95+
))
9996
}
10097

10198
/// Builds a new service for a full client.
102-
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
103-
let role = config.role.clone();
104-
let force_authoring = config.force_authoring;
105-
let name = config.network.node_name.clone();
106-
let disable_grandpa = config.disable_grandpa;
99+
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
100+
let (
101+
params, select_chain, inherent_data_providers,
102+
block_import, grandpa_link,
103+
) = new_full_params(config)?;
104+
105+
let (
106+
role, force_authoring, name, enable_grandpa, prometheus_registry,
107+
client, transaction_pool, keystore,
108+
) = {
109+
let sc_service::ServiceParams {
110+
config, client, transaction_pool, keystore, ..
111+
} = &params;
107112

108-
let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config);
113+
(
114+
config.role.clone(),
115+
config.force_authoring,
116+
config.network.node_name.clone(),
117+
!config.disable_grandpa,
118+
config.prometheus_registry().cloned(),
109119

110-
let (block_import, grandpa_link) =
111-
import_setup.take()
112-
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
120+
client.clone(), transaction_pool.clone(), keystore.clone(),
121+
)
122+
};
113123

114124
let ServiceComponents {
115-
client, transaction_pool, task_manager, keystore, network, select_chain,
116-
prometheus_registry, telemetry_on_connect_sinks, ..
117-
} = builder
118-
.with_finality_proof_provider(|client, backend| {
119-
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
120-
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
121-
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
122-
})?
123-
.build_full()?;
125+
task_manager, network, telemetry_on_connect_sinks, ..
126+
} = sc_service::build(params)?;
124127

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

132-
let select_chain = select_chain
133-
.ok_or(ServiceError::SelectChainRequired)?;
134-
135135
let can_author_with =
136136
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());
137137

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

174-
let enable_grandpa = !disable_grandpa;
175174
if enable_grandpa {
176175
// start the full GRANDPA voter
177176
// NOTE: non-authorities could run the GRANDPA observer protocol, but at
@@ -209,69 +208,49 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
209208

210209
/// Builds a new service for a light client.
211210
pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
212-
let inherent_data_providers = InherentDataProviders::new();
211+
let (client, backend, keystore, task_manager, on_demand) =
212+
sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;
213+
214+
let transaction_pool_api = Arc::new(sc_transaction_pool::LightChainApi::new(
215+
client.clone(), on_demand.clone(),
216+
));
217+
let transaction_pool = sc_transaction_pool::BasicPool::new_light(
218+
config.transaction_pool.clone(),
219+
transaction_pool_api,
220+
config.prometheus_registry(),
221+
task_manager.spawn_handle(),
222+
);
213223

214-
ServiceBuilder::new_light::<Block, RuntimeApi, Executor>(config)?
215-
.with_select_chain(|_config, backend| {
216-
Ok(LongestChain::new(backend.clone()))
217-
})?
218-
.with_transaction_pool(|builder| {
219-
let fetcher = builder.fetcher()
220-
.ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;
224+
let grandpa_block_import = sc_finality_grandpa::light_block_import(
225+
client.clone(), backend.clone(), &(client.clone() as Arc<_>),
226+
Arc::new(on_demand.checker().clone()) as Arc<_>,
227+
)?;
228+
let finality_proof_import = grandpa_block_import.clone();
229+
let finality_proof_request_builder =
230+
finality_proof_import.create_finality_proof_request_builder();
221231

222-
let pool_api = sc_transaction_pool::LightChainApi::new(
223-
builder.client().clone(),
224-
fetcher,
225-
);
226-
let pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
227-
builder.config().transaction_pool.clone(),
228-
Arc::new(pool_api),
229-
builder.prometheus_registry(),
230-
builder.spawn_handle(),
231-
));
232-
Ok(pool)
233-
})?
234-
.with_import_queue_and_fprb(|
235-
_config,
236-
client,
237-
backend,
238-
fetcher,
239-
_select_chain,
240-
_tx_pool,
241-
spawn_task_handle,
242-
prometheus_registry,
243-
| {
244-
let fetch_checker = fetcher
245-
.map(|fetcher| fetcher.checker().clone())
246-
.ok_or_else(|| "Trying to start light import queue without active fetch checker")?;
247-
let grandpa_block_import = sc_finality_grandpa::light_block_import(
248-
client.clone(),
249-
backend,
250-
&(client.clone() as Arc<_>),
251-
Arc::new(fetch_checker),
252-
)?;
253-
let finality_proof_import = grandpa_block_import.clone();
254-
let finality_proof_request_builder =
255-
finality_proof_import.create_finality_proof_request_builder();
232+
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
233+
sc_consensus_aura::slot_duration(&*client)?,
234+
grandpa_block_import,
235+
None,
236+
Some(Box::new(finality_proof_import)),
237+
client.clone(),
238+
InherentDataProviders::new(),
239+
&task_manager.spawn_handle(),
240+
config.prometheus_registry(),
241+
)?;
256242

257-
let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
258-
sc_consensus_aura::slot_duration(&*client)?,
259-
grandpa_block_import,
260-
None,
261-
Some(Box::new(finality_proof_import)),
262-
client,
263-
inherent_data_providers.clone(),
264-
spawn_task_handle,
265-
prometheus_registry,
266-
)?;
243+
let finality_proof_provider =
244+
Arc::new(GrandpaFinalityProofProvider::new(backend.clone(), client.clone() as Arc<_>));
267245

268-
Ok((import_queue, finality_proof_request_builder))
269-
})?
270-
.with_finality_proof_provider(|client, backend| {
271-
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
272-
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
273-
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
274-
})?
275-
.build_light()
276-
.map(|ServiceComponents { task_manager, .. }| task_manager)
246+
sc_service::build(sc_service::ServiceParams {
247+
block_announce_validator_builder: None,
248+
finality_proof_request_builder: Some(finality_proof_request_builder),
249+
finality_proof_provider: Some(finality_proof_provider),
250+
on_demand: Some(on_demand),
251+
remote_blockchain: Some(backend.remote_blockchain()),
252+
rpc_extensions_builder: Box::new(|_| ()),
253+
transaction_pool: Arc::new(transaction_pool),
254+
config, client, import_queue, keystore, backend, task_manager
255+
}).map(|ServiceComponents { task_manager, .. }| task_manager)
277256
}

bin/node/cli/src/command.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use crate::{chain_spec, service, Cli, Subcommand};
2020
use node_executor::Executor;
2121
use node_runtime::{Block, RuntimeApi};
2222
use sc_cli::{Result, SubstrateCli, RuntimeVersion, Role, ChainSpec};
23+
use sc_service::ServiceParams;
24+
use crate::service::new_full_params;
2325

2426
impl SubstrateCli for Cli {
2527
fn impl_name() -> String {
@@ -94,8 +96,9 @@ pub fn run() -> Result<()> {
9496
Some(Subcommand::Base(subcommand)) => {
9597
let runner = cli.create_runner(subcommand)?;
9698
runner.run_subcommand(subcommand, |config| {
97-
let (builder, _, _, _) = new_full_start!(config);
98-
Ok(builder.to_chain_ops_parts())
99+
let (ServiceParams { client, backend, import_queue, task_manager, .. }, ..)
100+
= new_full_params(config)?;
101+
Ok((client, backend, import_queue, task_manager))
99102
})
100103
}
101104
}

0 commit comments

Comments
 (0)