Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom spawn_tasks #2973

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/subspace-node/src/commands/run/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-node/src/commands/run/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketAddr>,
/// Maximum number of connections for JSON-RPC server
pub max_connections: u32,
/// CORS settings for HTTP & WS servers. `None` if all origins are allowed
Expand Down Expand Up @@ -188,7 +188,7 @@ impl From<SubstrateConfiguration> 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,
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod dsn;
mod metrics;
pub(crate) mod mmr;
pub mod rpc;
mod spawn_tasks;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider changing to task_spawner: spawn_tasks::spawn_tasks looks awkward.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed, though it is really a bad name in Substrate, I wouldn't call that function spawn_tasks in the first place.

pub mod sync_from_dsn;
pub mod transaction_pool;

Expand Down Expand Up @@ -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(),
Expand Down
51 changes: 12 additions & 39 deletions crates/subspace-service/src/spawn_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,24 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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<TBl, TBackend, TExPool, TRpc, TCl>(
pub(super) fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
params: SpawnTasksParams<TBl, TCl, TExPool, TRpc, TBackend>,
) -> Result<RpcHandlers, Error>
where
Expand Down Expand Up @@ -64,6 +60,8 @@ where
TExPool: MaintainedTransactionPool<Block = TBl, Hash = <TBl as BlockT>::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,
Expand All @@ -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);

Expand Down Expand Up @@ -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, &registry, &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(),
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions domains/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketAddr>,
/// Maximum number of connections for JSON-RPC server
pub max_connections: u32,
/// CORS settings for HTTP & WS servers. `None` if all origins are allowed
Expand Down Expand Up @@ -173,7 +173,7 @@ impl From<SubstrateConfiguration> 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,
Expand Down
Loading