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

Commit ec2ab79

Browse files
authored
Remove the service, replacing it with a struct of individual chain components (#6352)
* WIP * Making progress * Almost ready * Get service tests compiling * Fix node screenshot * Line widths * Fix node cli tests * Fix node cli warning * ChainComponents -> ServiceComponents, fix tests * make spawn_handle public * Remove spawnnamed impl for taskmanager * Move the keep alive stuff to the task manager * Move the telemetry, base path, rpc keep_alive to the service builder * Make the task manager keep alive an internal detail * Rewrite the browser start_client future * Remove run_node etc * Revert my personal changes to browser-demo/build.sh * use |config| * Add a runtime_version function to SubstrateCli * Reexport role and runtime version from sc cli * Update Cargo.lock * runtime_version -> native_runtime_version * Pass chain spec to native_runtime_version for polkadot * Fix line widths * Traitify ServiceComponents Client
1 parent 4eaea34 commit ec2ab79

File tree

19 files changed

+640
-776
lines changed

19 files changed

+640
-776
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use crate::chain_spec;
1919
use crate::cli::Cli;
2020
use crate::service;
21-
use sc_cli::SubstrateCli;
21+
use sc_cli::{SubstrateCli, RuntimeVersion, Role, ChainSpec};
2222

2323
impl SubstrateCli for Cli {
2424
fn impl_name() -> &'static str {
@@ -58,6 +58,10 @@ impl SubstrateCli for Cli {
5858
)?),
5959
})
6060
}
61+
62+
fn native_runtime_version(_: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
63+
&node_template_runtime::VERSION
64+
}
6165
}
6266

6367
/// Parse and run command line arguments
@@ -71,11 +75,10 @@ pub fn run() -> sc_cli::Result<()> {
7175
}
7276
None => {
7377
let runner = cli.create_runner(&cli.run)?;
74-
runner.run_node(
75-
service::new_light,
76-
service::new_full,
77-
node_template_runtime::VERSION
78-
)
78+
runner.run_node_until_exit(|config| match config.role {
79+
Role::Light => service::new_light(config),
80+
_ => service::new_full(config),
81+
})
7982
}
8083
}
8184
}

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ use std::time::Duration;
55
use sc_client_api::ExecutorProvider;
66
use sc_consensus::LongestChain;
77
use node_template_runtime::{self, opaque::Block, RuntimeApi};
8-
use sc_service::{error::{Error as ServiceError}, AbstractService, Configuration, ServiceBuilder};
8+
use sc_service::{
9+
error::{Error as ServiceError}, Configuration, ServiceBuilder, ServiceComponents,
10+
TaskManager,
11+
};
912
use sp_inherents::InherentDataProviders;
1013
use sc_executor::native_executor_instance;
1114
pub use sc_executor::NativeExecutor;
@@ -93,7 +96,7 @@ macro_rules! new_full_start {
9396
}
9497

9598
/// Builds a new service for a full client.
96-
pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceError> {
99+
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
97100
let role = config.role.clone();
98101
let force_authoring = config.force_authoring;
99102
let name = config.network.node_name.clone();
@@ -105,7 +108,10 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr
105108
import_setup.take()
106109
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");
107110

108-
let service = builder
111+
let ServiceComponents {
112+
client, transaction_pool, task_manager, keystore, network, select_chain,
113+
prometheus_registry, telemetry_on_connect_sinks, ..
114+
} = builder
109115
.with_finality_proof_provider(|client, backend| {
110116
// GenesisAuthoritySetProvider is implemented for StorageAndProofProvider
111117
let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;
@@ -115,40 +121,39 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr
115121

116122
if role.is_authority() {
117123
let proposer = sc_basic_authorship::ProposerFactory::new(
118-
service.client(),
119-
service.transaction_pool(),
120-
service.prometheus_registry().as_ref(),
124+
client.clone(),
125+
transaction_pool,
126+
prometheus_registry.as_ref(),
121127
);
122128

123-
let client = service.client();
124-
let select_chain = service.select_chain()
129+
let select_chain = select_chain
125130
.ok_or(ServiceError::SelectChainRequired)?;
126131

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

130135
let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>(
131136
sc_consensus_aura::slot_duration(&*client)?,
132-
client,
137+
client.clone(),
133138
select_chain,
134139
block_import,
135140
proposer,
136-
service.network(),
141+
network.clone(),
137142
inherent_data_providers.clone(),
138143
force_authoring,
139-
service.keystore(),
144+
keystore.clone(),
140145
can_author_with,
141146
)?;
142147

143148
// the AURA authoring task is considered essential, i.e. if it
144149
// fails we take down the service with it.
145-
service.spawn_essential_task_handle().spawn_blocking("aura", aura);
150+
task_manager.spawn_essential_handle().spawn_blocking("aura", aura);
146151
}
147152

148153
// if the node isn't actively participating in consensus then it doesn't
149154
// need a keystore, regardless of which protocol we use below.
150155
let keystore = if role.is_authority() {
151-
Some(service.keystore() as sp_core::traits::BareCryptoStorePtr)
156+
Some(keystore.clone() as sp_core::traits::BareCryptoStorePtr)
152157
} else {
153158
None
154159
};
@@ -174,33 +179,33 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr
174179
let grandpa_config = sc_finality_grandpa::GrandpaParams {
175180
config: grandpa_config,
176181
link: grandpa_link,
177-
network: service.network(),
182+
network: network.clone(),
178183
inherent_data_providers: inherent_data_providers.clone(),
179-
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
184+
telemetry_on_connect: Some(telemetry_on_connect_sinks.on_connect_stream()),
180185
voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),
181-
prometheus_registry: service.prometheus_registry(),
186+
prometheus_registry: prometheus_registry.clone(),
182187
shared_voter_state: SharedVoterState::empty(),
183188
};
184189

185190
// the GRANDPA voter task is considered infallible, i.e.
186191
// if it fails we take down the service with it.
187-
service.spawn_essential_task_handle().spawn_blocking(
192+
task_manager.spawn_essential_handle().spawn_blocking(
188193
"grandpa-voter",
189194
sc_finality_grandpa::run_grandpa_voter(grandpa_config)?
190195
);
191196
} else {
192197
sc_finality_grandpa::setup_disabled_grandpa(
193-
service.client(),
198+
client,
194199
&inherent_data_providers,
195-
service.network(),
200+
network.clone(),
196201
)?;
197202
}
198203

199-
Ok(service)
204+
Ok(task_manager)
200205
}
201206

202207
/// Builds a new service for a light client.
203-
pub fn new_light(config: Configuration) -> Result<impl AbstractService, ServiceError> {
208+
pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
204209
let inherent_data_providers = InherentDataProviders::new();
205210

206211
ServiceBuilder::new_light::<Block, RuntimeApi, Executor>(config)?
@@ -265,4 +270,5 @@ pub fn new_light(config: Configuration) -> Result<impl AbstractService, ServiceE
265270
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)
266271
})?
267272
.build_light()
273+
.map(|ServiceComponents { task_manager, .. }| task_manager)
268274
}

bin/node/cli/src/browser.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ async fn start_inner(chain_spec: Option<String>, log_level: String) -> Result<Cl
5252
info!("👤 Role: {:?}", config.role);
5353

5454
// Create the service. This is the most heavy initialization step.
55-
let service = crate::service::new_light(config)
56-
.map_err(|e| format!("{:?}", e))?;
55+
let (task_manager, rpc_handlers) =
56+
crate::service::new_light_base(config)
57+
.map(|(components, rpc_handlers, _, _, _)| (components, rpc_handlers))
58+
.map_err(|e| format!("{:?}", e))?;
5759

58-
Ok(browser_utils::start_client(service))
60+
Ok(browser_utils::start_client(task_manager, rpc_handlers))
5961
}

bin/node/cli/src/chain_spec.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ pub fn local_testnet_config() -> ChainSpec {
380380
#[cfg(test)]
381381
pub(crate) mod tests {
382382
use super::*;
383-
use crate::service::{new_full, new_light};
383+
use crate::service::{new_full_base, new_light_base};
384384
use sc_service_test;
385385
use sp_runtime::BuildStorage;
386386

@@ -430,8 +430,14 @@ pub(crate) mod tests {
430430
fn test_connectivity() {
431431
sc_service_test::connectivity(
432432
integration_test_config_with_two_authorities(),
433-
|config| new_full(config),
434-
|config| new_light(config),
433+
|config| {
434+
let (keep_alive, _, client, network, transaction_pool) = new_full_base(config,|_, _| ())?;
435+
Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool))
436+
},
437+
|config| {
438+
let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?;
439+
Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool))
440+
}
435441
);
436442
}
437443

bin/node/cli/src/command.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use crate::{chain_spec, service, Cli, Subcommand};
2020
use node_executor::Executor;
2121
use node_runtime::{Block, RuntimeApi};
22-
use sc_cli::{Result, SubstrateCli};
22+
use sc_cli::{Result, SubstrateCli, RuntimeVersion, Role, ChainSpec};
2323

2424
impl SubstrateCli for Cli {
2525
fn impl_name() -> &'static str {
@@ -61,6 +61,10 @@ impl SubstrateCli for Cli {
6161
)?),
6262
})
6363
}
64+
65+
fn native_runtime_version(_: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
66+
&node_runtime::VERSION
67+
}
6468
}
6569

6670
/// Parse command line arguments into service configuration.
@@ -70,11 +74,10 @@ pub fn run() -> Result<()> {
7074
match &cli.subcommand {
7175
None => {
7276
let runner = cli.create_runner(&cli.run)?;
73-
runner.run_node(
74-
service::new_light,
75-
service::new_full,
76-
node_runtime::VERSION
77-
)
77+
runner.run_node_until_exit(|config| match config.role {
78+
Role::Light => service::new_light(config),
79+
_ => service::new_full(config),
80+
})
7881
}
7982
Some(Subcommand::Inspect(cmd)) => {
8083
let runner = cli.create_runner(cmd)?;

0 commit comments

Comments
 (0)