From 3b8bd6318c70f1d9acef6f86e54a749c0ae1437c Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Tue, 6 Aug 2024 03:58:50 +0300 Subject: [PATCH 1/3] Copy `spawn_tasks` function from `sc-service` without changes --- crates/subspace-service/src/spawn_tasks.rs | 194 +++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 crates/subspace-service/src/spawn_tasks.rs diff --git a/crates/subspace-service/src/spawn_tasks.rs b/crates/subspace-service/src/spawn_tasks.rs new file mode 100644 index 00000000000..8633d14788b --- /dev/null +++ b/crates/subspace-service/src/spawn_tasks.rs @@ -0,0 +1,194 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use prometheus_endpoint::Registry; +use sc_client_api::{ + BlockBackend, BlockchainEvents, ExecutorProvider, ProofProvider, StorageProvider, UsageProvider, +}; +use sc_rpc_api::DenyUnsafe; +use sc_service::config::PrometheusConfig; +use sc_service::{ + gen_rpc_module, init_telemetry, propagate_transaction_notifications, start_rpc_servers, + Configuration, Error, RpcHandlers, SpawnTasksParams, +}; +use sc_telemetry::TelemetryHandle; +use sc_transaction_pool_api::MaintainedTransactionPool; +use sp_api::__private::BlockT; +use sp_api::{CallApiAt, ProvideRuntimeApi}; +use sp_blockchain::{HeaderBackend, HeaderMetadata}; +use sp_consensus::block_validation::Chain; +use sp_runtime::traits::BlockIdTo; +use std::sync::Arc; +use tracing::info; + +/// Spawn the tasks that are required to run a node. +pub fn spawn_tasks( + params: SpawnTasksParams, +) -> Result +where + TCl: ProvideRuntimeApi + + HeaderMetadata + + Chain + + BlockBackend + + BlockIdTo + + ProofProvider + + HeaderBackend + + BlockchainEvents + + ExecutorProvider + + UsageProvider + + StorageProvider + + CallApiAt + + Send + + 'static, + >::Api: sp_api::Metadata + + sp_transaction_pool::runtime_api::TaggedTransactionQueue + + sp_session::SessionKeys + + sp_api::ApiExt, + TBl: BlockT, + TBl::Hash: Unpin, + TBl::Header: Unpin, + TBackend: 'static + sc_client_api::backend::Backend + Send, + TExPool: MaintainedTransactionPool::Hash> + 'static, +{ + let SpawnTasksParams { + mut config, + task_manager, + client, + backend, + keystore, + transaction_pool, + rpc_builder, + network, + system_rpc_tx, + tx_handler_controller, + sync_service, + telemetry, + } = params; + + let chain_info = client.usage_info().chain; + + sp_session::generate_initial_session_keys( + client.clone(), + chain_info.best_hash, + config + .dev_key_seed + .clone() + .map(|s| vec![s]) + .unwrap_or_default(), + keystore.clone(), + ) + .map_err(|e| Error::Application(Box::new(e)))?; + + let sysinfo = sc_sysinfo::gather_sysinfo(); + sc_sysinfo::print_sysinfo(&sysinfo); + + let telemetry = telemetry + .map(|telemetry| { + init_telemetry( + &mut config, + network.clone(), + client.clone(), + telemetry, + Some(sysinfo), + ) + }) + .transpose()?; + + info!("📦 Highest known block at #{}", chain_info.best_number); + + let spawn_handle = task_manager.spawn_handle(); + + // Inform the tx pool about imported and finalized blocks. + spawn_handle.spawn( + "txpool-notifications", + Some("transaction-pool"), + sc_transaction_pool::notification_future(client.clone(), transaction_pool.clone()), + ); + + spawn_handle.spawn( + "on-transaction-imported", + Some("transaction-pool"), + propagate_transaction_notifications( + transaction_pool.clone(), + tx_handler_controller, + telemetry.clone(), + ), + ); + + // Prometheus metrics. + let metrics_service = + if let Some(PrometheusConfig { port, registry }) = config.prometheus_config.clone() { + // Set static metrics. + let metrics = MetricsService::with_prometheus(telemetry, ®istry, &config)?; + spawn_handle.spawn( + "prometheus-endpoint", + None, + prometheus_endpoint::init_prometheus(port, registry).map(drop), + ); + + metrics + } else { + MetricsService::new(telemetry) + }; + + // Periodically updated metrics and telemetry updates. + spawn_handle.spawn( + "telemetry-periodic-send", + None, + metrics_service.run( + client.clone(), + transaction_pool.clone(), + network.clone(), + sync_service.clone(), + ), + ); + + let rpc_id_provider = config.rpc_id_provider.take(); + + // jsonrpsee RPC + let gen_rpc_module = |deny_unsafe: DenyUnsafe| { + gen_rpc_module( + deny_unsafe, + task_manager.spawn_handle(), + client.clone(), + transaction_pool.clone(), + keystore.clone(), + system_rpc_tx.clone(), + &config, + backend.clone(), + &*rpc_builder, + ) + }; + + let rpc = start_rpc_servers(&config, gen_rpc_module, rpc_id_provider)?; + let rpc_handlers = RpcHandlers(Arc::new(gen_rpc_module(sc_rpc::DenyUnsafe::No)?.into())); + + // Spawn informant task + spawn_handle.spawn( + "informant", + None, + sc_informant::build( + client.clone(), + network, + sync_service.clone(), + config.informant_output_format, + ), + ); + + task_manager.keep_alive((config.base_path, rpc)); + + Ok(rpc_handlers) +} From ca3a743c13cb907c017f1c856109f98ac6ddaf1d Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 14 Aug 2024 17:26:08 +0300 Subject: [PATCH 2/3] Simplify our version of `spawn_tasks`, make RPC server optional --- Cargo.lock | 1 + .../src/commands/run/consensus.rs | 2 +- .../subspace-node/src/commands/run/domain.rs | 2 +- crates/subspace-service/Cargo.toml | 1 + crates/subspace-service/src/config.rs | 4 +- crates/subspace-service/src/lib.rs | 3 +- crates/subspace-service/src/spawn_tasks.rs | 51 +++++-------------- domains/service/src/config.rs | 4 +- 8 files changed, 22 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db7271fad06..c58f0ac5a84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12962,6 +12962,7 @@ dependencies = [ "sc-rpc-spec-v2", "sc-service", "sc-subspace-block-relay", + "sc-sysinfo", "sc-telemetry", "sc-tracing", "sc-transaction-pool", diff --git a/crates/subspace-node/src/commands/run/consensus.rs b/crates/subspace-node/src/commands/run/consensus.rs index 513e2396995..8e06688fe4b 100644 --- a/crates/subspace-node/src/commands/run/consensus.rs +++ b/crates/subspace-node/src/commands/run/consensus.rs @@ -583,7 +583,7 @@ pub(super) fn create_consensus_chain_configuration( state_pruning: pruning_params.state_pruning(), blocks_pruning: pruning_params.blocks_pruning(), rpc_options: SubstrateRpcConfiguration { - listen_on: rpc_options.rpc_listen_on, + listen_on: Some(rpc_options.rpc_listen_on), max_connections: rpc_options.rpc_max_connections, cors: rpc_cors.into(), methods: match rpc_options.rpc_methods { diff --git a/crates/subspace-node/src/commands/run/domain.rs b/crates/subspace-node/src/commands/run/domain.rs index a92f7be5744..c22aaff49ae 100644 --- a/crates/subspace-node/src/commands/run/domain.rs +++ b/crates/subspace-node/src/commands/run/domain.rs @@ -332,7 +332,7 @@ pub(super) fn create_domain_configuration( state_pruning: pruning_params.state_pruning()?, blocks_pruning: pruning_params.blocks_pruning()?, rpc_options: SubstrateRpcConfiguration { - listen_on: rpc_options.rpc_listen_on, + listen_on: Some(rpc_options.rpc_listen_on), max_connections: rpc_options.rpc_max_connections, cors: rpc_cors.into(), methods: match rpc_options.rpc_methods { diff --git a/crates/subspace-service/Cargo.toml b/crates/subspace-service/Cargo.toml index bbe1e1f6b15..9e367208825 100644 --- a/crates/subspace-service/Cargo.toml +++ b/crates/subspace-service/Cargo.toml @@ -52,6 +52,7 @@ sc-rpc = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781 sc-rpc-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } sc-rpc-spec-v2 = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } sc-service = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631", default-features = false } +sc-sysinfo = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631", default-features = false } sc-subspace-block-relay = { version = "0.1.0", path = "../sc-subspace-block-relay" } sc-telemetry = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } sc-tracing = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" } diff --git a/crates/subspace-service/src/config.rs b/crates/subspace-service/src/config.rs index b98aaf5e25c..b5530bf45e5 100644 --- a/crates/subspace-service/src/config.rs +++ b/crates/subspace-service/src/config.rs @@ -35,7 +35,7 @@ pub const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 15; #[derive(Debug)] pub struct SubstrateRpcConfiguration { /// IP and port (TCP) on which to listen for RPC requests - pub listen_on: SocketAddr, + pub listen_on: Option, /// Maximum number of connections for JSON-RPC server pub max_connections: u32, /// CORS settings for HTTP & WS servers. `None` if all origins are allowed @@ -188,7 +188,7 @@ impl From for Configuration { blocks_pruning: configuration.blocks_pruning, wasm_method: Default::default(), wasm_runtime_overrides: None, - rpc_addr: Some(configuration.rpc_options.listen_on), + rpc_addr: configuration.rpc_options.listen_on, rpc_methods: configuration.rpc_options.methods, rpc_max_connections: configuration.rpc_options.max_connections, rpc_cors: configuration.rpc_options.cors, diff --git a/crates/subspace-service/src/lib.rs b/crates/subspace-service/src/lib.rs index 14ec425d9d5..0eeda11c8ed 100644 --- a/crates/subspace-service/src/lib.rs +++ b/crates/subspace-service/src/lib.rs @@ -31,6 +31,7 @@ pub mod dsn; mod metrics; pub(crate) mod mmr; pub mod rpc; +mod spawn_tasks; pub mod sync_from_dsn; pub mod transaction_pool; @@ -1164,7 +1165,7 @@ where // We replace the Substrate implementation of metrics server with our own. config.base.prometheus_config.take(); - let rpc_handlers = sc_service::spawn_tasks(SpawnTasksParams { + let rpc_handlers = spawn_tasks::spawn_tasks(SpawnTasksParams { network: network_service.clone(), client: client.clone(), keystore: keystore_container.keystore(), diff --git a/crates/subspace-service/src/spawn_tasks.rs b/crates/subspace-service/src/spawn_tasks.rs index 8633d14788b..4ac8feb2ca2 100644 --- a/crates/subspace-service/src/spawn_tasks.rs +++ b/crates/subspace-service/src/spawn_tasks.rs @@ -14,28 +14,24 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use prometheus_endpoint::Registry; use sc_client_api::{ BlockBackend, BlockchainEvents, ExecutorProvider, ProofProvider, StorageProvider, UsageProvider, }; use sc_rpc_api::DenyUnsafe; -use sc_service::config::PrometheusConfig; use sc_service::{ - gen_rpc_module, init_telemetry, propagate_transaction_notifications, start_rpc_servers, - Configuration, Error, RpcHandlers, SpawnTasksParams, + gen_rpc_module, init_telemetry, propagate_transaction_notifications, start_rpc_servers, Error, + MetricsService, RpcHandlers, SpawnTasksParams, }; -use sc_telemetry::TelemetryHandle; use sc_transaction_pool_api::MaintainedTransactionPool; -use sp_api::__private::BlockT; use sp_api::{CallApiAt, ProvideRuntimeApi}; use sp_blockchain::{HeaderBackend, HeaderMetadata}; use sp_consensus::block_validation::Chain; -use sp_runtime::traits::BlockIdTo; +use sp_runtime::traits::{Block as BlockT, BlockIdTo}; use std::sync::Arc; use tracing::info; /// Spawn the tasks that are required to run a node. -pub fn spawn_tasks( +pub(super) fn spawn_tasks( params: SpawnTasksParams, ) -> Result where @@ -64,6 +60,8 @@ where TExPool: MaintainedTransactionPool::Hash> + 'static, { let SpawnTasksParams { + // TODO: Stop using `Configuration` once + // https://github.com/paritytech/polkadot-sdk/pull/5364 is in our fork mut config, task_manager, client, @@ -80,18 +78,6 @@ where let chain_info = client.usage_info().chain; - sp_session::generate_initial_session_keys( - client.clone(), - chain_info.best_hash, - config - .dev_key_seed - .clone() - .map(|s| vec![s]) - .unwrap_or_default(), - keystore.clone(), - ) - .map_err(|e| Error::Application(Box::new(e)))?; - let sysinfo = sc_sysinfo::gather_sysinfo(); sc_sysinfo::print_sysinfo(&sysinfo); @@ -128,27 +114,11 @@ where ), ); - // Prometheus metrics. - let metrics_service = - if let Some(PrometheusConfig { port, registry }) = config.prometheus_config.clone() { - // Set static metrics. - let metrics = MetricsService::with_prometheus(telemetry, ®istry, &config)?; - spawn_handle.spawn( - "prometheus-endpoint", - None, - prometheus_endpoint::init_prometheus(port, registry).map(drop), - ); - - metrics - } else { - MetricsService::new(telemetry) - }; - // Periodically updated metrics and telemetry updates. spawn_handle.spawn( "telemetry-periodic-send", None, - metrics_service.run( + MetricsService::new(telemetry).run( client.clone(), transaction_pool.clone(), network.clone(), @@ -173,8 +143,11 @@ where ) }; - let rpc = start_rpc_servers(&config, gen_rpc_module, rpc_id_provider)?; - let rpc_handlers = RpcHandlers(Arc::new(gen_rpc_module(sc_rpc::DenyUnsafe::No)?.into())); + let rpc = config + .rpc_addr + .map(|_| start_rpc_servers(&config, gen_rpc_module, rpc_id_provider)) + .transpose()?; + let rpc_handlers = RpcHandlers::new(Arc::new(gen_rpc_module(DenyUnsafe::No)?)); // Spawn informant task spawn_handle.spawn( diff --git a/domains/service/src/config.rs b/domains/service/src/config.rs index 91be6251ca2..3f00477d4e4 100644 --- a/domains/service/src/config.rs +++ b/domains/service/src/config.rs @@ -27,7 +27,7 @@ pub const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 15; #[derive(Debug)] pub struct SubstrateRpcConfiguration { /// IP and port (TCP) on which to listen for RPC requests - pub listen_on: SocketAddr, + pub listen_on: Option, /// Maximum number of connections for JSON-RPC server pub max_connections: u32, /// CORS settings for HTTP & WS servers. `None` if all origins are allowed @@ -173,7 +173,7 @@ impl From for Configuration { blocks_pruning: configuration.blocks_pruning, wasm_method: Default::default(), wasm_runtime_overrides: None, - rpc_addr: Some(configuration.rpc_options.listen_on), + rpc_addr: configuration.rpc_options.listen_on, rpc_methods: configuration.rpc_options.methods, rpc_max_connections: configuration.rpc_options.max_connections, rpc_cors: configuration.rpc_options.cors, From ff568cc1f073c8c912c3172799e15432c649ed4e Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Thu, 15 Aug 2024 14:23:59 +0300 Subject: [PATCH 3/3] Rename module --- crates/subspace-service/src/lib.rs | 4 ++-- .../subspace-service/src/{spawn_tasks.rs => task_spawner.rs} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename crates/subspace-service/src/{spawn_tasks.rs => task_spawner.rs} (100%) diff --git a/crates/subspace-service/src/lib.rs b/crates/subspace-service/src/lib.rs index 0eeda11c8ed..4d9a4062ec1 100644 --- a/crates/subspace-service/src/lib.rs +++ b/crates/subspace-service/src/lib.rs @@ -31,8 +31,8 @@ pub mod dsn; mod metrics; pub(crate) mod mmr; pub mod rpc; -mod spawn_tasks; pub mod sync_from_dsn; +mod task_spawner; pub mod transaction_pool; use crate::config::{ChainSyncMode, SubspaceConfiguration, SubspaceNetworking}; @@ -1165,7 +1165,7 @@ where // We replace the Substrate implementation of metrics server with our own. config.base.prometheus_config.take(); - let rpc_handlers = spawn_tasks::spawn_tasks(SpawnTasksParams { + let rpc_handlers = task_spawner::spawn_tasks(SpawnTasksParams { network: network_service.clone(), client: client.clone(), keystore: keystore_container.keystore(), diff --git a/crates/subspace-service/src/spawn_tasks.rs b/crates/subspace-service/src/task_spawner.rs similarity index 100% rename from crates/subspace-service/src/spawn_tasks.rs rename to crates/subspace-service/src/task_spawner.rs