Skip to content

Commit 6bb5bc0

Browse files
nazar-pcniklasad1
authored andcommitted
Improve sc-service API (#5364)
This improves `sc-service` API by not requiring the whole `&Configuration`, using specific configuration options instead. `RpcConfiguration` was also extracted from `Configuration` to group all RPC options together. We don't use Substrate's CLI and would rather not use `Configuration` either, but some key public functions require it even though they ignored most of the fields anyway. `RpcConfiguration` is very helpful not just for consolidation of the fields, but also to finally make RPC optional for our use case, while Substrate still runs RPC server on localhost even if listening address is explicitly set to `None`, which is annoying (and I suspect there is a reason for it, so didn't want to change the default just yet). While this is a breaking change, most developers will not notice it if they use higher-level APIs. Fixes #2897 --------- Co-authored-by: Niklas Adolfsson <[email protected]>
1 parent db7ed6d commit 6bb5bc0

File tree

22 files changed

+195
-144
lines changed

22 files changed

+195
-144
lines changed

cumulus/client/relay-chain-minimal-node/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async fn new_minimal_relay_chain<Block: BlockT, Network: NetworkBackend<RelayBlo
175175
collator_pair: CollatorPair,
176176
relay_chain_rpc_client: Arc<BlockChainRpcClient>,
177177
) -> Result<NewMinimalNode, RelayChainError> {
178-
let role = config.role.clone();
178+
let role = config.role;
179179
let mut net_config = sc_network::config::FullNetworkConfiguration::<_, _, Network>::new(
180180
&config.network,
181181
config.prometheus_config.as_ref().map(|cfg| cfg.registry.clone()),

cumulus/client/relay-chain-minimal-node/src/network.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(crate) fn build_collator_network<Network: NetworkBackend<Block, Hash>>(
6565
spawn_handle.spawn("peer-store", Some("networking"), peer_store.run());
6666

6767
let network_params = sc_network::config::Params::<Block, Hash, Network> {
68-
role: config.role.clone(),
68+
role: config.role,
6969
executor: {
7070
let spawn_handle = Clone::clone(&spawn_handle);
7171
Box::new(move |fut| {

cumulus/polkadot-parachain/polkadot-parachain-lib/src/cli.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,9 @@ impl<Config: CliConfig> CliConfiguration<Self> for RelayChainCli<Config> {
317317
_support_url: &String,
318318
_impl_version: &String,
319319
_logger_hook: F,
320-
_config: &sc_service::Configuration,
321320
) -> sc_cli::Result<()>
322321
where
323-
F: FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration),
322+
F: FnOnce(&mut sc_cli::LoggerBuilder),
324323
{
325324
unreachable!("PolkadotCli is never initialized; qed");
326325
}

cumulus/test/service/src/cli.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ impl CliConfiguration<Self> for RelayChainCli {
139139
_support_url: &String,
140140
_impl_version: &String,
141141
_logger_hook: F,
142-
_config: &sc_service::Configuration,
143142
) -> CliResult<()>
144143
where
145-
F: FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration),
144+
F: FnOnce(&mut sc_cli::LoggerBuilder),
146145
{
147146
unreachable!("PolkadotCli is never initialized; qed");
148147
}

polkadot/node/service/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,15 +437,16 @@ fn new_partial_basics(
437437
.transpose()?;
438438

439439
let heap_pages = config
440+
.executor
440441
.default_heap_pages
441442
.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |h| HeapAllocStrategy::Static { extra_pages: h as _ });
442443

443444
let executor = WasmExecutor::builder()
444-
.with_execution_method(config.wasm_method)
445+
.with_execution_method(config.executor.wasm_method)
445446
.with_onchain_heap_alloc_strategy(heap_pages)
446447
.with_offchain_heap_alloc_strategy(heap_pages)
447-
.with_max_runtime_instances(config.max_runtime_instances)
448-
.with_runtime_cache_size(config.runtime_cache_size)
448+
.with_max_runtime_instances(config.executor.max_runtime_instances)
449+
.with_runtime_cache_size(config.executor.runtime_cache_size)
449450
.build();
450451

451452
let (client, backend, keystore_container, task_manager) =
@@ -767,7 +768,7 @@ pub fn new_full<
767768
use sc_network_sync::WarpSyncConfig;
768769

769770
let is_offchain_indexing_enabled = config.offchain_worker.indexing_enabled;
770-
let role = config.role.clone();
771+
let role = config.role;
771772
let force_authoring = config.force_authoring;
772773
let backoff_authoring_blocks = if !force_authoring_backoff &&
773774
(config.chain_spec.is_polkadot() || config.chain_spec.is_kusama())

polkadot/node/test/service/src/lib.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use substrate_test_client::{
6868
pub type Client = FullClient;
6969

7070
pub use polkadot_service::{FullBackend, GetLastTimestamp};
71+
use sc_service::config::{ExecutorConfiguration, RpcConfiguration};
7172

7273
/// Create a new full node.
7374
#[sc_tracing::logging::prefix_logs_with(config.network.node_name.as_str())]
@@ -200,35 +201,37 @@ pub fn node_config(
200201
state_pruning: Default::default(),
201202
blocks_pruning: BlocksPruning::KeepFinalized,
202203
chain_spec: Box::new(spec),
203-
wasm_method: WasmExecutionMethod::Compiled {
204-
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
204+
executor: ExecutorConfiguration {
205+
wasm_method: WasmExecutionMethod::Compiled {
206+
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
207+
},
208+
..ExecutorConfiguration::default()
205209
},
206210
wasm_runtime_overrides: Default::default(),
207-
rpc_addr: Default::default(),
208-
rpc_max_request_size: Default::default(),
209-
rpc_max_response_size: Default::default(),
210-
rpc_max_connections: Default::default(),
211-
rpc_cors: None,
212-
rpc_methods: Default::default(),
213-
rpc_id_provider: None,
214-
rpc_max_subs_per_conn: Default::default(),
215-
rpc_port: 9944,
216-
rpc_message_buffer_capacity: Default::default(),
217-
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
218-
rpc_rate_limit: None,
219-
rpc_rate_limit_whitelisted_ips: Default::default(),
220-
rpc_rate_limit_trust_proxy_headers: Default::default(),
211+
rpc: RpcConfiguration {
212+
addr: Default::default(),
213+
max_request_size: Default::default(),
214+
max_response_size: Default::default(),
215+
max_connections: Default::default(),
216+
cors: None,
217+
methods: Default::default(),
218+
id_provider: None,
219+
max_subs_per_conn: Default::default(),
220+
port: 9944,
221+
message_buffer_capacity: Default::default(),
222+
batch_config: RpcBatchRequestConfig::Unlimited,
223+
rate_limit: None,
224+
rate_limit_whitelisted_ips: Default::default(),
225+
rate_limit_trust_proxy_headers: Default::default(),
226+
},
221227
prometheus_config: None,
222228
telemetry_endpoints: None,
223-
default_heap_pages: None,
224229
offchain_worker: Default::default(),
225230
force_authoring: false,
226231
disable_grandpa: false,
227232
dev_key_seed: Some(key_seed),
228233
tracing_targets: None,
229234
tracing_receiver: Default::default(),
230-
max_runtime_instances: 8,
231-
runtime_cache_size: 2,
232235
announce_block: true,
233236
data_path: root,
234237
base_path,

prdoc/pr_5364.prdoc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
title: Improve `sc-service` API
2+
3+
doc:
4+
- audience: Node Dev
5+
description: |
6+
This improves `sc-service` API by not requiring the whole `&Configuration`, using specific configuration options
7+
instead. `RpcConfiguration` and `ExecutorConfiguration` were also extracted from `Configuration` to group all RPC
8+
and executor options together.
9+
If `sc-service` is used as a library with lower-level APIs, `Configuration` can now be avoided in most cases.
10+
11+
This mainly impacts you on your node implementation. There you need to change this:
12+
```
13+
with_execution_method(config.wasm_method)
14+
```
15+
16+
to this:
17+
```
18+
with_execution_method(config.executor.wasm_method)
19+
```
20+
21+
There are similar changes required as well, but all are around the initialization of the wasm executor.
22+
23+
crates:
24+
- name: sc-service
25+
bump: major
26+
- name: sc-network-common
27+
bump: patch
28+
- name: sc-cli
29+
bump: major
30+
- name: polkadot-service
31+
bump: patch
32+
- name: cumulus-relay-chain-minimal-node
33+
bump: none
34+
- name: polkadot-parachain-bin
35+
bump: major
36+
- name: polkadot-parachain-lib
37+
bump: major
38+
- name: staging-node-inspect
39+
bump: major

substrate/bin/node/cli/benches/block_production.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughpu
2222

2323
use kitchensink_runtime::{constants::currency::*, BalancesCall};
2424
use node_cli::service::{create_extrinsic, FullClient};
25+
use polkadot_sdk::sc_service::config::{ExecutorConfiguration, RpcConfiguration};
2526
use sc_block_builder::{BlockBuilderBuilder, BuiltBlock};
2627
use sc_consensus::{
2728
block_import::{BlockImportParams, ForkChoiceStrategy},
@@ -73,34 +74,36 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
7374
state_pruning: Some(PruningMode::ArchiveAll),
7475
blocks_pruning: BlocksPruning::KeepAll,
7576
chain_spec: spec,
76-
wasm_method: WasmExecutionMethod::Compiled {
77-
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
77+
executor: ExecutorConfiguration {
78+
wasm_method: WasmExecutionMethod::Compiled {
79+
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
80+
},
81+
..ExecutorConfiguration::default()
82+
},
83+
rpc: RpcConfiguration {
84+
addr: None,
85+
max_connections: Default::default(),
86+
cors: None,
87+
methods: Default::default(),
88+
max_request_size: Default::default(),
89+
max_response_size: Default::default(),
90+
id_provider: Default::default(),
91+
max_subs_per_conn: Default::default(),
92+
port: 9944,
93+
message_buffer_capacity: Default::default(),
94+
batch_config: RpcBatchRequestConfig::Unlimited,
95+
rate_limit: None,
96+
rate_limit_whitelisted_ips: Default::default(),
97+
rate_limit_trust_proxy_headers: Default::default(),
7898
},
79-
rpc_addr: None,
80-
rpc_max_connections: Default::default(),
81-
rpc_cors: None,
82-
rpc_methods: Default::default(),
83-
rpc_max_request_size: Default::default(),
84-
rpc_max_response_size: Default::default(),
85-
rpc_id_provider: Default::default(),
86-
rpc_max_subs_per_conn: Default::default(),
87-
rpc_port: 9944,
88-
rpc_message_buffer_capacity: Default::default(),
89-
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
90-
rpc_rate_limit: None,
91-
rpc_rate_limit_whitelisted_ips: Default::default(),
92-
rpc_rate_limit_trust_proxy_headers: Default::default(),
9399
prometheus_config: None,
94100
telemetry_endpoints: None,
95-
default_heap_pages: None,
96101
offchain_worker: OffchainWorkerConfig { enabled: true, indexing_enabled: false },
97102
force_authoring: false,
98103
disable_grandpa: false,
99104
dev_key_seed: Some(Sr25519Keyring::Alice.to_seed()),
100105
tracing_targets: None,
101106
tracing_receiver: Default::default(),
102-
max_runtime_instances: 8,
103-
runtime_cache_size: 2,
104107
announce_block: true,
105108
data_path: base_path.path().into(),
106109
base_path,

substrate/bin/node/cli/benches/transaction_pool.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use futures::{future, StreamExt};
2424
use kitchensink_runtime::{constants::currency::*, BalancesCall, SudoCall};
2525
use node_cli::service::{create_extrinsic, fetch_nonce, FullClient, TransactionPool};
2626
use node_primitives::AccountId;
27+
use polkadot_sdk::sc_service::config::{ExecutorConfiguration, RpcConfiguration};
2728
use sc_service::{
2829
config::{
2930
BlocksPruning, DatabaseSource, KeystoreConfig, NetworkConfiguration, OffchainWorkerConfig,
@@ -70,32 +71,31 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
7071
state_pruning: Some(PruningMode::ArchiveAll),
7172
blocks_pruning: BlocksPruning::KeepAll,
7273
chain_spec: spec,
73-
wasm_method: Default::default(),
74-
rpc_addr: None,
75-
rpc_max_connections: Default::default(),
76-
rpc_cors: None,
77-
rpc_methods: Default::default(),
78-
rpc_max_request_size: Default::default(),
79-
rpc_max_response_size: Default::default(),
80-
rpc_id_provider: Default::default(),
81-
rpc_max_subs_per_conn: Default::default(),
82-
rpc_port: 9944,
83-
rpc_message_buffer_capacity: Default::default(),
84-
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
85-
rpc_rate_limit: None,
86-
rpc_rate_limit_whitelisted_ips: Default::default(),
87-
rpc_rate_limit_trust_proxy_headers: Default::default(),
74+
executor: ExecutorConfiguration::default(),
75+
rpc: RpcConfiguration {
76+
addr: None,
77+
max_connections: Default::default(),
78+
cors: None,
79+
methods: Default::default(),
80+
max_request_size: Default::default(),
81+
max_response_size: Default::default(),
82+
id_provider: Default::default(),
83+
max_subs_per_conn: Default::default(),
84+
port: 9944,
85+
message_buffer_capacity: Default::default(),
86+
batch_config: RpcBatchRequestConfig::Unlimited,
87+
rate_limit: None,
88+
rate_limit_whitelisted_ips: Default::default(),
89+
rate_limit_trust_proxy_headers: Default::default(),
90+
},
8891
prometheus_config: None,
8992
telemetry_endpoints: None,
90-
default_heap_pages: None,
9193
offchain_worker: OffchainWorkerConfig { enabled: true, indexing_enabled: false },
9294
force_authoring: false,
9395
disable_grandpa: false,
9496
dev_key_seed: Some(Sr25519Keyring::Alice.to_seed()),
9597
tracing_targets: None,
9698
tracing_receiver: Default::default(),
97-
max_runtime_instances: 8,
98-
runtime_cache_size: 2,
9999
announce_block: true,
100100
data_path: base_path.path().into(),
101101
base_path,

substrate/bin/node/cli/src/service.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub fn new_partial(
209209
})
210210
.transpose()?;
211211

212-
let executor = sc_service::new_wasm_executor(&config);
212+
let executor = sc_service::new_wasm_executor(&config.executor);
213213

214214
let (client, backend, keystore_container, task_manager) =
215215
sc_service::new_full_parts::<Block, RuntimeApi, _>(
@@ -406,7 +406,7 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
406406
),
407407
) -> Result<NewFullBase, ServiceError> {
408408
let is_offchain_indexing_enabled = config.offchain_worker.indexing_enabled;
409-
let role = config.role.clone();
409+
let role = config.role;
410410
let force_authoring = config.force_authoring;
411411
let backoff_authoring_blocks =
412412
Some(sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging::default());
@@ -719,7 +719,7 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
719719
name: Some(name),
720720
observer_enabled: false,
721721
keystore,
722-
local_role: role.clone(),
722+
local_role: role,
723723
telemetry: telemetry.as_ref().map(|x| x.handle()),
724724
protocol_name: grandpa_protocol_name,
725725
};

0 commit comments

Comments
 (0)