Skip to content

Commit

Permalink
Improve service building API by requiring specific configuration opti…
Browse files Browse the repository at this point in the history
…ons, introduce `sc_service::config::RpcConfiguration` to replace a bunch of flat fields
  • Loading branch information
nazar-pc committed Aug 14, 2024
1 parent e4f8a6d commit ff8a168
Show file tree
Hide file tree
Showing 18 changed files with 265 additions and 208 deletions.
2 changes: 1 addition & 1 deletion cumulus/client/relay-chain-minimal-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async fn new_minimal_relay_chain<Block: BlockT, Network: NetworkBackend<RelayBlo
collator_pair: CollatorPair,
relay_chain_rpc_client: Arc<BlockChainRpcClient>,
) -> Result<NewMinimalNode, RelayChainError> {
let role = config.role.clone();
let role = config.role;
let mut net_config = sc_network::config::FullNetworkConfiguration::<_, _, Network>::new(
&config.network,
config.prometheus_config.as_ref().map(|cfg| cfg.registry.clone()),
Expand Down
2 changes: 1 addition & 1 deletion cumulus/client/relay-chain-minimal-node/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub(crate) fn build_collator_network<Network: NetworkBackend<Block, Hash>>(
spawn_handle.spawn("peer-store", Some("networking"), peer_store.run());

let network_params = sc_network::config::Params::<Block, Hash, Network> {
role: config.role.clone(),
role: config.role,
executor: {
let spawn_handle = Clone::clone(&spawn_handle);
Box::new(move |fut| {
Expand Down
35 changes: 19 additions & 16 deletions cumulus/test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ use sc_network::{
use sc_service::{
config::{
BlocksPruning, DatabaseSource, KeystoreConfig, MultiaddrWithPeerId, NetworkConfiguration,
OffchainWorkerConfig, PruningMode, RpcBatchRequestConfig, WasmExecutionMethod,
OffchainWorkerConfig, PruningMode, RpcBatchRequestConfig, RpcConfiguration,
WasmExecutionMethod,
},
BasePath, ChainSpec as ChainSpecService, Configuration, Error as ServiceError,
PartialComponents, Role, RpcHandlers, TFullBackend, TFullClient, TaskManager,
Expand Down Expand Up @@ -866,20 +867,22 @@ pub fn node_config(
wasm_method: WasmExecutionMethod::Compiled {
instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::PoolingCopyOnWrite,
},
rpc_addr: None,
rpc_max_connections: Default::default(),
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_request_size: Default::default(),
rpc_max_response_size: Default::default(),
rpc_id_provider: None,
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9945,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
rpc_rate_limit: None,
rpc_rate_limit_whitelisted_ips: Default::default(),
rpc_rate_limit_trust_proxy_headers: Default::default(),
rpc: RpcConfiguration {
addr: None,
max_connections: Default::default(),
cors: None,
methods: Default::default(),
max_request_size: Default::default(),
max_response_size: Default::default(),
id_provider: None,
max_subs_per_conn: Default::default(),
port: 9945,
message_buffer_capacity: Default::default(),
batch_config: RpcBatchRequestConfig::Unlimited,
rate_limit: None,
rate_limit_whitelisted_ips: Default::default(),
rate_limit_trust_proxy_headers: Default::default(),
},
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down Expand Up @@ -1006,7 +1009,7 @@ pub fn run_relay_chain_validator_node(
);

if let Some(port) = port {
config.rpc_addr = Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port));
config.rpc.addr = Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port));
}

let mut workers_path = std::env::current_exe().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ pub fn new_full<
use sc_network_sync::WarpSyncParams;

let is_offchain_indexing_enabled = config.offchain_worker.indexing_enabled;
let role = config.role.clone();
let role = config.role;
let force_authoring = config.force_authoring;
let backoff_authoring_blocks = if !force_authoring_backoff &&
(config.chain_spec.is_polkadot() || config.chain_spec.is_kusama())
Expand Down
31 changes: 17 additions & 14 deletions polkadot/node/test/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use substrate_test_client::{
pub type Client = FullClient;

pub use polkadot_service::{FullBackend, GetLastTimestamp};
use sc_service::config::RpcConfiguration;

/// Create a new full node.
#[sc_tracing::logging::prefix_logs_with(config.network.node_name.as_str())]
Expand Down Expand Up @@ -204,20 +205,22 @@ pub fn node_config(
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
},
wasm_runtime_overrides: Default::default(),
rpc_addr: Default::default(),
rpc_max_request_size: Default::default(),
rpc_max_response_size: Default::default(),
rpc_max_connections: Default::default(),
rpc_cors: None,
rpc_methods: Default::default(),
rpc_id_provider: None,
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
rpc_rate_limit: None,
rpc_rate_limit_whitelisted_ips: Default::default(),
rpc_rate_limit_trust_proxy_headers: Default::default(),
rpc: RpcConfiguration {
addr: Default::default(),
max_request_size: Default::default(),
max_response_size: Default::default(),
max_connections: Default::default(),
cors: None,
methods: Default::default(),
id_provider: None,
max_subs_per_conn: Default::default(),
port: 9944,
message_buffer_capacity: Default::default(),
batch_config: RpcBatchRequestConfig::Unlimited,
rate_limit: None,
rate_limit_whitelisted_ips: Default::default(),
rate_limit_trust_proxy_headers: Default::default(),
},
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
31 changes: 17 additions & 14 deletions substrate/bin/node/cli/benches/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughpu

use kitchensink_runtime::{constants::currency::*, BalancesCall};
use node_cli::service::{create_extrinsic, FullClient};
use polkadot_sdk::sc_service::config::RpcConfiguration;
use sc_block_builder::{BlockBuilderBuilder, BuiltBlock};
use sc_consensus::{
block_import::{BlockImportParams, ForkChoiceStrategy},
Expand Down Expand Up @@ -76,20 +77,22 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
wasm_method: WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
},
rpc_addr: None,
rpc_max_connections: Default::default(),
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_request_size: Default::default(),
rpc_max_response_size: Default::default(),
rpc_id_provider: Default::default(),
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
rpc_rate_limit: None,
rpc_rate_limit_whitelisted_ips: Default::default(),
rpc_rate_limit_trust_proxy_headers: Default::default(),
rpc: RpcConfiguration {
addr: None,
max_connections: Default::default(),
cors: None,
methods: Default::default(),
max_request_size: Default::default(),
max_response_size: Default::default(),
id_provider: Default::default(),
max_subs_per_conn: Default::default(),
port: 9944,
message_buffer_capacity: Default::default(),
batch_config: RpcBatchRequestConfig::Unlimited,
rate_limit: None,
rate_limit_whitelisted_ips: Default::default(),
rate_limit_trust_proxy_headers: Default::default(),
},
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
31 changes: 17 additions & 14 deletions substrate/bin/node/cli/benches/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use futures::{future, StreamExt};
use kitchensink_runtime::{constants::currency::*, BalancesCall, SudoCall};
use node_cli::service::{create_extrinsic, fetch_nonce, FullClient, TransactionPool};
use node_primitives::AccountId;
use polkadot_sdk::sc_service::config::RpcConfiguration;
use sc_service::{
config::{
BlocksPruning, DatabaseSource, KeystoreConfig, NetworkConfiguration, OffchainWorkerConfig,
Expand Down Expand Up @@ -71,20 +72,22 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
blocks_pruning: BlocksPruning::KeepAll,
chain_spec: spec,
wasm_method: Default::default(),
rpc_addr: None,
rpc_max_connections: Default::default(),
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_request_size: Default::default(),
rpc_max_response_size: Default::default(),
rpc_id_provider: Default::default(),
rpc_max_subs_per_conn: Default::default(),
rpc_port: 9944,
rpc_message_buffer_capacity: Default::default(),
rpc_batch_config: RpcBatchRequestConfig::Unlimited,
rpc_rate_limit: None,
rpc_rate_limit_whitelisted_ips: Default::default(),
rpc_rate_limit_trust_proxy_headers: Default::default(),
rpc: RpcConfiguration {
addr: None,
max_connections: Default::default(),
cors: None,
methods: Default::default(),
max_request_size: Default::default(),
max_response_size: Default::default(),
id_provider: Default::default(),
max_subs_per_conn: Default::default(),
port: 9944,
message_buffer_capacity: Default::default(),
batch_config: RpcBatchRequestConfig::Unlimited,
rate_limit: None,
rate_limit_whitelisted_ips: Default::default(),
rate_limit_trust_proxy_headers: Default::default(),
},
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
4 changes: 2 additions & 2 deletions substrate/bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
),
) -> Result<NewFullBase, ServiceError> {
let is_offchain_indexing_enabled = config.offchain_worker.indexing_enabled;
let role = config.role.clone();
let role = config.role;
let force_authoring = config.force_authoring;
let backoff_authoring_blocks =
Some(sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging::default());
Expand Down Expand Up @@ -719,7 +719,7 @@ pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
name: Some(name),
observer_enabled: false,
keystore,
local_role: role.clone(),
local_role: role,
telemetry: telemetry.as_ref().map(|x| x.handle()),
protocol_name: grandpa_protocol_name,
};
Expand Down
34 changes: 18 additions & 16 deletions substrate/client/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use sc_service::{
config::{
BasePath, Configuration, DatabaseSource, IpNetwork, KeystoreConfig, NetworkConfiguration,
NodeKeyConfig, OffchainWorkerConfig, PrometheusConfig, PruningMode, Role,
RpcBatchRequestConfig, RpcMethods, TelemetryEndpoints, TransactionPoolOptions,
WasmExecutionMethod,
RpcBatchRequestConfig, RpcConfiguration, RpcMethods, TelemetryEndpoints,
TransactionPoolOptions, WasmExecutionMethod,
},
BlocksPruning, ChainSpec, TracingReceiver,
};
Expand Down Expand Up @@ -527,20 +527,22 @@ pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
blocks_pruning: self.blocks_pruning()?,
wasm_method: self.wasm_method()?,
wasm_runtime_overrides: self.wasm_runtime_overrides(),
rpc_addr: self.rpc_addr(DCV::rpc_listen_port())?,
rpc_methods: self.rpc_methods()?,
rpc_max_connections: self.rpc_max_connections()?,
rpc_cors: self.rpc_cors(is_dev)?,
rpc_max_request_size: self.rpc_max_request_size()?,
rpc_max_response_size: self.rpc_max_response_size()?,
rpc_id_provider: None,
rpc_max_subs_per_conn: self.rpc_max_subscriptions_per_connection()?,
rpc_port: DCV::rpc_listen_port(),
rpc_message_buffer_capacity: self.rpc_buffer_capacity_per_connection()?,
rpc_batch_config: self.rpc_batch_config()?,
rpc_rate_limit: self.rpc_rate_limit()?,
rpc_rate_limit_whitelisted_ips: self.rpc_rate_limit_whitelisted_ips()?,
rpc_rate_limit_trust_proxy_headers: self.rpc_rate_limit_trust_proxy_headers()?,
rpc: RpcConfiguration {
addr: self.rpc_addr(DCV::rpc_listen_port())?,
methods: self.rpc_methods()?,
max_connections: self.rpc_max_connections()?,
cors: self.rpc_cors(is_dev)?,
max_request_size: self.rpc_max_request_size()?,
max_response_size: self.rpc_max_response_size()?,
id_provider: None,
max_subs_per_conn: self.rpc_max_subscriptions_per_connection()?,
port: DCV::rpc_listen_port(),
message_buffer_capacity: self.rpc_buffer_capacity_per_connection()?,
batch_config: self.rpc_batch_config()?,
rate_limit: self.rpc_rate_limit()?,
rate_limit_whitelisted_ips: self.rpc_rate_limit_whitelisted_ips()?,
rate_limit_trust_proxy_headers: self.rpc_rate_limit_trust_proxy_headers()?,
},
prometheus_config: self
.prometheus_config(DCV::prometheus_listen_port(), &chain_spec)?,
telemetry_endpoints,
Expand Down
5 changes: 2 additions & 3 deletions substrate/client/cli/src/params/node_key_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ impl NodeKeyParams {
.clone()
.unwrap_or_else(|| net_config_dir.join(NODE_KEY_ED25519_FILE));
if !self.unsafe_force_node_key_generation &&
role.is_authority() && !is_dev &&
!key_path.exists()
role.is_authority() &&
!is_dev && !key_path.exists()
{
return Err(Error::NetworkKeyNotFound(key_path))
}
Expand Down Expand Up @@ -237,7 +237,6 @@ mod tests {
|params| {
let dir = PathBuf::from(net_config_dir.clone());
let typ = params.node_key_type;
let role = role.clone();
params.node_key(net_config_dir, role, is_dev).and_then(move |c| match c {
NodeKeyConfig::Ed25519(sc_network::config::Secret::File(ref f))
if typ == NodeKeyType::Ed25519 &&
Expand Down
32 changes: 17 additions & 15 deletions substrate/client/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub fn print_node_infos<C: SubstrateCli>(config: &Configuration) {
mod tests {
use super::*;
use sc_network::config::NetworkConfiguration;
use sc_service::{Arc, ChainType, GenericChainSpec, NoExtension};
use sc_service::{config::RpcConfiguration, Arc, ChainType, GenericChainSpec, NoExtension};
use std::{
path::PathBuf,
sync::atomic::{AtomicU64, Ordering},
Expand Down Expand Up @@ -264,20 +264,22 @@ mod tests {
),
wasm_method: Default::default(),
wasm_runtime_overrides: None,
rpc_addr: None,
rpc_max_connections: Default::default(),
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_request_size: Default::default(),
rpc_max_response_size: Default::default(),
rpc_id_provider: Default::default(),
rpc_max_subs_per_conn: Default::default(),
rpc_message_buffer_capacity: Default::default(),
rpc_port: 9944,
rpc_batch_config: sc_service::config::RpcBatchRequestConfig::Unlimited,
rpc_rate_limit: None,
rpc_rate_limit_whitelisted_ips: Default::default(),
rpc_rate_limit_trust_proxy_headers: Default::default(),
rpc: RpcConfiguration {
addr: None,
max_connections: Default::default(),
cors: None,
methods: Default::default(),
max_request_size: Default::default(),
max_response_size: Default::default(),
id_provider: Default::default(),
max_subs_per_conn: Default::default(),
message_buffer_capacity: Default::default(),
port: 9944,
batch_config: sc_service::config::RpcBatchRequestConfig::Unlimited,
rate_limit: None,
rate_limit_whitelisted_ips: Default::default(),
rate_limit_trust_proxy_headers: Default::default(),
},
prometheus_config: None,
telemetry_endpoints: None,
default_heap_pages: None,
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/common/src/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl From<Roles> for ObservedRole {
}

/// Role of the local node.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum Role {
/// Regular full node.
Full,
Expand Down
Loading

0 comments on commit ff8a168

Please sign in to comment.