Skip to content

Commit da65410

Browse files
nazar-pcniklasad1
andauthored
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 5291412 commit da65410

File tree

28 files changed

+436
-327
lines changed

28 files changed

+436
-327
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
@@ -322,10 +322,9 @@ impl<Config: CliConfig> CliConfiguration<Self> for RelayChainCli<Config> {
322322
_support_url: &String,
323323
_impl_version: &String,
324324
_logger_hook: F,
325-
_config: &sc_service::Configuration,
326325
) -> sc_cli::Result<()>
327326
where
328-
F: FnOnce(&mut sc_cli::LoggerBuilder, &sc_service::Configuration),
327+
F: FnOnce(&mut sc_cli::LoggerBuilder),
329328
{
330329
unreachable!("PolkadotCli is never initialized; qed");
331330
}

cumulus/polkadot-parachain/polkadot-parachain-lib/src/common/spec.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,15 @@ pub(crate) trait NodeSpec {
127127
})
128128
.transpose()?;
129129

130-
let heap_pages = config.default_heap_pages.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |h| {
131-
HeapAllocStrategy::Static { extra_pages: h as _ }
132-
});
130+
let heap_pages =
131+
config.executor.default_heap_pages.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |h| {
132+
HeapAllocStrategy::Static { extra_pages: h as _ }
133+
});
133134

134135
let executor = sc_executor::WasmExecutor::<ParachainHostFunctions>::builder()
135-
.with_execution_method(config.wasm_method)
136-
.with_max_runtime_instances(config.max_runtime_instances)
137-
.with_runtime_cache_size(config.runtime_cache_size)
136+
.with_execution_method(config.executor.wasm_method)
137+
.with_max_runtime_instances(config.executor.max_runtime_instances)
138+
.with_runtime_cache_size(config.executor.runtime_cache_size)
138139
.with_onchain_heap_alloc_strategy(heap_pages)
139140
.with_offchain_heap_alloc_strategy(heap_pages)
140141
.build();

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
}

cumulus/test/service/src/lib.rs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ use sc_network::{
7878
};
7979
use sc_service::{
8080
config::{
81-
BlocksPruning, DatabaseSource, KeystoreConfig, MultiaddrWithPeerId, NetworkConfiguration,
82-
OffchainWorkerConfig, PruningMode, RpcBatchRequestConfig, RpcEndpoint, WasmExecutionMethod,
81+
BlocksPruning, DatabaseSource, ExecutorConfiguration, KeystoreConfig, MultiaddrWithPeerId,
82+
NetworkConfiguration, OffchainWorkerConfig, PruningMode, RpcBatchRequestConfig,
83+
RpcConfiguration, RpcEndpoint, WasmExecutionMethod,
8384
},
8485
BasePath, ChainSpec as ChainSpecService, Configuration, Error as ServiceError,
8586
PartialComponents, Role, RpcHandlers, TFullBackend, TFullClient, TaskManager,
@@ -194,15 +195,16 @@ pub fn new_partial(
194195
enable_import_proof_record: bool,
195196
) -> Result<Service, sc_service::Error> {
196197
let heap_pages = config
198+
.executor
197199
.default_heap_pages
198200
.map_or(DEFAULT_HEAP_ALLOC_STRATEGY, |h| HeapAllocStrategy::Static { extra_pages: h as _ });
199201

200202
let executor = WasmExecutor::builder()
201-
.with_execution_method(config.wasm_method)
203+
.with_execution_method(config.executor.wasm_method)
202204
.with_onchain_heap_alloc_strategy(heap_pages)
203205
.with_offchain_heap_alloc_strategy(heap_pages)
204-
.with_max_runtime_instances(config.max_runtime_instances)
205-
.with_runtime_cache_size(config.runtime_cache_size)
206+
.with_max_runtime_instances(config.executor.max_runtime_instances)
207+
.with_runtime_cache_size(config.executor.runtime_cache_size)
206208
.build();
207209

208210
let (client, backend, keystore_container, task_manager) =
@@ -863,38 +865,41 @@ pub fn node_config(
863865
state_pruning: Some(PruningMode::ArchiveAll),
864866
blocks_pruning: BlocksPruning::KeepAll,
865867
chain_spec: spec,
866-
wasm_method: WasmExecutionMethod::Compiled {
867-
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::PoolingCopyOnWrite,
868+
executor: ExecutorConfiguration {
869+
wasm_method: WasmExecutionMethod::Compiled {
870+
instantiation_strategy:
871+
sc_executor_wasmtime::InstantiationStrategy::PoolingCopyOnWrite,
872+
},
873+
..ExecutorConfiguration::default()
874+
},
875+
rpc: RpcConfiguration {
876+
addr: None,
877+
max_connections: Default::default(),
878+
cors: None,
879+
methods: Default::default(),
880+
max_request_size: Default::default(),
881+
max_response_size: Default::default(),
882+
id_provider: None,
883+
max_subs_per_conn: Default::default(),
884+
port: 9945,
885+
message_buffer_capacity: Default::default(),
886+
batch_config: RpcBatchRequestConfig::Unlimited,
887+
rate_limit: None,
888+
rate_limit_whitelisted_ips: Default::default(),
889+
rate_limit_trust_proxy_headers: Default::default(),
868890
},
869-
rpc_addr: None,
870-
rpc_max_connections: Default::default(),
871-
rpc_cors: None,
872-
rpc_methods: Default::default(),
873-
rpc_max_request_size: Default::default(),
874-
rpc_max_response_size: Default::default(),
875-
rpc_id_provider: None,
876-
rpc_max_subs_per_conn: Default::default(),
877-
rpc_port: 9945,
878-
rpc_message_buffer_capacity: Default::default(),
879-
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
880-
rpc_rate_limit: None,
881-
rpc_rate_limit_whitelisted_ips: Default::default(),
882-
rpc_rate_limit_trust_proxy_headers: Default::default(),
883891
prometheus_config: None,
884892
telemetry_endpoints: None,
885-
default_heap_pages: None,
886893
offchain_worker: OffchainWorkerConfig { enabled: true, indexing_enabled: false },
887894
force_authoring: false,
888895
disable_grandpa: false,
889896
dev_key_seed: Some(key_seed),
890897
tracing_targets: None,
891898
tracing_receiver: Default::default(),
892-
max_runtime_instances: 8,
893899
announce_block: true,
894900
data_path: root,
895901
base_path,
896902
wasm_runtime_overrides: None,
897-
runtime_cache_size: 2,
898903
})
899904
}
900905

@@ -1006,19 +1011,19 @@ pub fn run_relay_chain_validator_node(
10061011
);
10071012

10081013
if let Some(port) = port {
1009-
config.rpc_addr = Some(vec![RpcEndpoint {
1010-
batch_config: config.rpc_batch_config,
1011-
cors: config.rpc_cors.clone(),
1014+
config.rpc.addr = Some(vec![RpcEndpoint {
1015+
batch_config: config.rpc.batch_config,
1016+
cors: config.rpc.cors.clone(),
10121017
listen_addr: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port)),
1013-
max_connections: config.rpc_max_connections,
1014-
max_payload_in_mb: config.rpc_max_request_size,
1015-
max_payload_out_mb: config.rpc_max_response_size,
1016-
max_subscriptions_per_connection: config.rpc_max_subs_per_conn,
1017-
max_buffer_capacity_per_connection: config.rpc_message_buffer_capacity,
1018-
rpc_methods: config.rpc_methods,
1019-
rate_limit: config.rpc_rate_limit,
1020-
rate_limit_trust_proxy_headers: config.rpc_rate_limit_trust_proxy_headers,
1021-
rate_limit_whitelisted_ips: config.rpc_rate_limit_whitelisted_ips.clone(),
1018+
max_connections: config.rpc.max_connections,
1019+
max_payload_in_mb: config.rpc.max_request_size,
1020+
max_payload_out_mb: config.rpc.max_response_size,
1021+
max_subscriptions_per_connection: config.rpc.max_subs_per_conn,
1022+
max_buffer_capacity_per_connection: config.rpc.message_buffer_capacity,
1023+
rpc_methods: config.rpc.methods,
1024+
rate_limit: config.rpc.rate_limit,
1025+
rate_limit_trust_proxy_headers: config.rpc.rate_limit_trust_proxy_headers,
1026+
rate_limit_whitelisted_ips: config.rpc.rate_limit_whitelisted_ips.clone(),
10221027
retry_random_port: true,
10231028
is_optional: false,
10241029
}]);

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) =
@@ -764,7 +765,7 @@ pub fn new_full<
764765
use sc_network_sync::WarpSyncConfig;
765766

766767
let is_offchain_indexing_enabled = config.offchain_worker.indexing_enabled;
767-
let role = config.role.clone();
768+
let role = config.role;
768769
let force_authoring = config.force_authoring;
769770
let backoff_authoring_blocks = if !force_authoring_backoff &&
770771
(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,

0 commit comments

Comments
 (0)