Skip to content

Commit 72575a0

Browse files
authored
Merge pull request #2973 from autonomys/custom-spawn_tasks
Custom `spawn_tasks`
2 parents 92a51c1 + ff568cc commit 72575a0

File tree

8 files changed

+177
-7
lines changed

8 files changed

+177
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/subspace-node/src/commands/run/consensus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ pub(super) fn create_consensus_chain_configuration(
583583
state_pruning: pruning_params.state_pruning(),
584584
blocks_pruning: pruning_params.blocks_pruning(),
585585
rpc_options: SubstrateRpcConfiguration {
586-
listen_on: rpc_options.rpc_listen_on,
586+
listen_on: Some(rpc_options.rpc_listen_on),
587587
max_connections: rpc_options.rpc_max_connections,
588588
cors: rpc_cors.into(),
589589
methods: match rpc_options.rpc_methods {

crates/subspace-node/src/commands/run/domain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ pub(super) fn create_domain_configuration(
332332
state_pruning: pruning_params.state_pruning()?,
333333
blocks_pruning: pruning_params.blocks_pruning()?,
334334
rpc_options: SubstrateRpcConfiguration {
335-
listen_on: rpc_options.rpc_listen_on,
335+
listen_on: Some(rpc_options.rpc_listen_on),
336336
max_connections: rpc_options.rpc_max_connections,
337337
cors: rpc_cors.into(),
338338
methods: match rpc_options.rpc_methods {

crates/subspace-service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ sc-rpc = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781
5252
sc-rpc-api = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
5353
sc-rpc-spec-v2 = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
5454
sc-service = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631", default-features = false }
55+
sc-sysinfo = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631", default-features = false }
5556
sc-subspace-block-relay = { version = "0.1.0", path = "../sc-subspace-block-relay" }
5657
sc-telemetry = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
5758
sc-tracing = { git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }

crates/subspace-service/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 15;
3535
#[derive(Debug)]
3636
pub struct SubstrateRpcConfiguration {
3737
/// IP and port (TCP) on which to listen for RPC requests
38-
pub listen_on: SocketAddr,
38+
pub listen_on: Option<SocketAddr>,
3939
/// Maximum number of connections for JSON-RPC server
4040
pub max_connections: u32,
4141
/// CORS settings for HTTP & WS servers. `None` if all origins are allowed
@@ -181,7 +181,7 @@ impl From<SubstrateConfiguration> for Configuration {
181181
blocks_pruning: configuration.blocks_pruning,
182182
wasm_method: Default::default(),
183183
wasm_runtime_overrides: None,
184-
rpc_addr: Some(configuration.rpc_options.listen_on),
184+
rpc_addr: configuration.rpc_options.listen_on,
185185
rpc_methods: configuration.rpc_options.methods,
186186
rpc_max_connections: configuration.rpc_options.max_connections,
187187
rpc_cors: configuration.rpc_options.cors,

crates/subspace-service/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mod metrics;
3232
pub(crate) mod mmr;
3333
pub mod rpc;
3434
pub mod sync_from_dsn;
35+
mod task_spawner;
3536
pub mod transaction_pool;
3637

3738
use crate::config::{ChainSyncMode, SubspaceConfiguration, SubspaceNetworking};
@@ -1180,7 +1181,7 @@ where
11801181
// We replace the Substrate implementation of metrics server with our own.
11811182
config.base.prometheus_config.take();
11821183

1183-
let rpc_handlers = sc_service::spawn_tasks(SpawnTasksParams {
1184+
let rpc_handlers = task_spawner::spawn_tasks(SpawnTasksParams {
11841185
network: network_service.clone(),
11851186
client: client.clone(),
11861187
keystore: keystore_container.keystore(),
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright (C) Parity Technologies (UK) Ltd.
2+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3+
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
use sc_client_api::{
18+
BlockBackend, BlockchainEvents, ExecutorProvider, ProofProvider, StorageProvider, UsageProvider,
19+
};
20+
use sc_rpc_api::DenyUnsafe;
21+
use sc_service::{
22+
gen_rpc_module, init_telemetry, propagate_transaction_notifications, start_rpc_servers, Error,
23+
MetricsService, RpcHandlers, SpawnTasksParams,
24+
};
25+
use sc_transaction_pool_api::MaintainedTransactionPool;
26+
use sp_api::{CallApiAt, ProvideRuntimeApi};
27+
use sp_blockchain::{HeaderBackend, HeaderMetadata};
28+
use sp_consensus::block_validation::Chain;
29+
use sp_runtime::traits::{Block as BlockT, BlockIdTo};
30+
use std::sync::Arc;
31+
use tracing::info;
32+
33+
/// Spawn the tasks that are required to run a node.
34+
pub(super) fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
35+
params: SpawnTasksParams<TBl, TCl, TExPool, TRpc, TBackend>,
36+
) -> Result<RpcHandlers, Error>
37+
where
38+
TCl: ProvideRuntimeApi<TBl>
39+
+ HeaderMetadata<TBl, Error = sp_blockchain::Error>
40+
+ Chain<TBl>
41+
+ BlockBackend<TBl>
42+
+ BlockIdTo<TBl, Error = sp_blockchain::Error>
43+
+ ProofProvider<TBl>
44+
+ HeaderBackend<TBl>
45+
+ BlockchainEvents<TBl>
46+
+ ExecutorProvider<TBl>
47+
+ UsageProvider<TBl>
48+
+ StorageProvider<TBl, TBackend>
49+
+ CallApiAt<TBl>
50+
+ Send
51+
+ 'static,
52+
<TCl as ProvideRuntimeApi<TBl>>::Api: sp_api::Metadata<TBl>
53+
+ sp_transaction_pool::runtime_api::TaggedTransactionQueue<TBl>
54+
+ sp_session::SessionKeys<TBl>
55+
+ sp_api::ApiExt<TBl>,
56+
TBl: BlockT,
57+
TBl::Hash: Unpin,
58+
TBl::Header: Unpin,
59+
TBackend: 'static + sc_client_api::backend::Backend<TBl> + Send,
60+
TExPool: MaintainedTransactionPool<Block = TBl, Hash = <TBl as BlockT>::Hash> + 'static,
61+
{
62+
let SpawnTasksParams {
63+
// TODO: Stop using `Configuration` once
64+
// https://github.com/paritytech/polkadot-sdk/pull/5364 is in our fork
65+
mut config,
66+
task_manager,
67+
client,
68+
backend,
69+
keystore,
70+
transaction_pool,
71+
rpc_builder,
72+
network,
73+
system_rpc_tx,
74+
tx_handler_controller,
75+
sync_service,
76+
telemetry,
77+
} = params;
78+
79+
let chain_info = client.usage_info().chain;
80+
81+
let sysinfo = sc_sysinfo::gather_sysinfo();
82+
sc_sysinfo::print_sysinfo(&sysinfo);
83+
84+
let telemetry = telemetry
85+
.map(|telemetry| {
86+
init_telemetry(
87+
&mut config,
88+
network.clone(),
89+
client.clone(),
90+
telemetry,
91+
Some(sysinfo),
92+
)
93+
})
94+
.transpose()?;
95+
96+
info!("📦 Highest known block at #{}", chain_info.best_number);
97+
98+
let spawn_handle = task_manager.spawn_handle();
99+
100+
// Inform the tx pool about imported and finalized blocks.
101+
spawn_handle.spawn(
102+
"txpool-notifications",
103+
Some("transaction-pool"),
104+
sc_transaction_pool::notification_future(client.clone(), transaction_pool.clone()),
105+
);
106+
107+
spawn_handle.spawn(
108+
"on-transaction-imported",
109+
Some("transaction-pool"),
110+
propagate_transaction_notifications(
111+
transaction_pool.clone(),
112+
tx_handler_controller,
113+
telemetry.clone(),
114+
),
115+
);
116+
117+
// Periodically updated metrics and telemetry updates.
118+
spawn_handle.spawn(
119+
"telemetry-periodic-send",
120+
None,
121+
MetricsService::new(telemetry).run(
122+
client.clone(),
123+
transaction_pool.clone(),
124+
network.clone(),
125+
sync_service.clone(),
126+
),
127+
);
128+
129+
let rpc_id_provider = config.rpc_id_provider.take();
130+
131+
// jsonrpsee RPC
132+
let gen_rpc_module = |deny_unsafe: DenyUnsafe| {
133+
gen_rpc_module(
134+
deny_unsafe,
135+
task_manager.spawn_handle(),
136+
client.clone(),
137+
transaction_pool.clone(),
138+
keystore.clone(),
139+
system_rpc_tx.clone(),
140+
&config,
141+
backend.clone(),
142+
&*rpc_builder,
143+
)
144+
};
145+
146+
let rpc = config
147+
.rpc_addr
148+
.map(|_| start_rpc_servers(&config, gen_rpc_module, rpc_id_provider))
149+
.transpose()?;
150+
let rpc_handlers = RpcHandlers::new(Arc::new(gen_rpc_module(DenyUnsafe::No)?));
151+
152+
// Spawn informant task
153+
spawn_handle.spawn(
154+
"informant",
155+
None,
156+
sc_informant::build(
157+
client.clone(),
158+
network,
159+
sync_service.clone(),
160+
config.informant_output_format,
161+
),
162+
);
163+
164+
task_manager.keep_alive((config.base_path, rpc));
165+
166+
Ok(rpc_handlers)
167+
}

domains/service/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 15;
2727
#[derive(Debug)]
2828
pub struct SubstrateRpcConfiguration {
2929
/// IP and port (TCP) on which to listen for RPC requests
30-
pub listen_on: SocketAddr,
30+
pub listen_on: Option<SocketAddr>,
3131
/// Maximum number of connections for JSON-RPC server
3232
pub max_connections: u32,
3333
/// CORS settings for HTTP & WS servers. `None` if all origins are allowed
@@ -173,7 +173,7 @@ impl From<SubstrateConfiguration> for Configuration {
173173
blocks_pruning: configuration.blocks_pruning,
174174
wasm_method: Default::default(),
175175
wasm_runtime_overrides: None,
176-
rpc_addr: Some(configuration.rpc_options.listen_on),
176+
rpc_addr: configuration.rpc_options.listen_on,
177177
rpc_methods: configuration.rpc_options.methods,
178178
rpc_max_connections: configuration.rpc_options.max_connections,
179179
rpc_cors: configuration.rpc_options.cors,

0 commit comments

Comments
 (0)