Skip to content
This repository was archived by the owner on Nov 1, 2023. It is now read-only.

Commit 5c5eb70

Browse files
committed
enable custom subid gen through spawn_tasks
1 parent 0a61c83 commit 5c5eb70

File tree

8 files changed

+36
-13
lines changed

8 files changed

+36
-13
lines changed

bin/node-template/node/src/service.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.
22
3+
use jsonrpsee::ws_server::RandomStringIdProvider;
34
use node_template_runtime::{self, opaque::Block, RuntimeApi};
45
use sc_client_api::{BlockBackend, ExecutorProvider};
56
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
@@ -244,6 +245,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
244245
system_rpc_tx,
245246
config,
246247
telemetry: telemetry.as_mut(),
248+
rpc_id_provider: RandomStringIdProvider::new(16),
247249
})?;
248250

249251
if role.is_authority() {

bin/node/cli/benches/block_production.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
2020

21+
use jsonrpsee::ws_server::RandomStringIdProvider;
2122
use node_cli::service::{create_extrinsic, FullClient};
2223
use node_runtime::{constants::currency::*, BalancesCall};
2324
use sc_block_builder::{BlockBuilderProvider, BuiltBlock, RecordProof};
@@ -111,7 +112,8 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
111112
wasm_runtime_overrides: None,
112113
};
113114

114-
node_cli::service::new_full_base(config, |_, _| ()).expect("creating a full node doesn't fail")
115+
node_cli::service::new_full_base(config, RandomStringIdProvider::new(16), |_, _| ())
116+
.expect("creating a full node doesn't fail")
115117
}
116118

117119
fn extrinsic_set_time(now: u64) -> OpaqueExtrinsic {

bin/node/cli/benches/transaction_pool.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
2020
use futures::{future, StreamExt};
21+
use jsonrpsee::ws_server::RandomStringIdProvider;
2122
use node_cli::service::{create_extrinsic, fetch_nonce, FullClient, TransactionPool};
2223
use node_primitives::AccountId;
2324
use node_runtime::{constants::currency::*, BalancesCall, SudoCall};
@@ -103,7 +104,8 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
103104
wasm_runtime_overrides: None,
104105
};
105106

106-
node_cli::service::new_full_base(config, |_, _| ()).expect("Creates node")
107+
node_cli::service::new_full_base(config, RandomStringIdProvider::new(16), |_, _| ())
108+
.expect("Creates node")
107109
}
108110

109111
fn create_accounts(num: usize) -> Vec<sr25519::Pair> {

bin/node/cli/src/chain_spec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ pub fn local_testnet_config() -> ChainSpec {
421421
pub(crate) mod tests {
422422
use super::*;
423423
use crate::service::{new_full_base, NewFullBase};
424+
use jsonrpsee::ws_server::RandomStringIdProvider;
424425
use sc_service_test;
425426
use sp_runtime::BuildStorage;
426427

@@ -472,7 +473,7 @@ pub(crate) mod tests {
472473

473474
sc_service_test::connectivity(integration_test_config_with_two_authorities(), |config| {
474475
let NewFullBase { task_manager, client, network, transaction_pool, .. } =
475-
new_full_base(config, |_, _| ())?;
476+
new_full_base(config, RandomStringIdProvider::new(16), |_, _| ())?;
476477
Ok(sc_service_test::TestNetComponents::new(
477478
task_manager,
478479
client,

bin/node/cli/src/service.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
use codec::Encode;
2424
use frame_system_rpc_runtime_api::AccountNonceApi;
2525
use futures::prelude::*;
26+
use jsonrpsee::ws_server::RandomStringIdProvider;
2627
use node_executor::ExecutorDispatch;
2728
use node_primitives::Block;
2829
use node_runtime::RuntimeApi;
2930
use sc_client_api::{BlockBackend, ExecutorProvider};
3031
use sc_consensus_babe::{self, SlotProportion};
3132
use sc_executor::NativeElseWasmExecutor;
3233
use sc_network::{Event, NetworkService};
33-
use sc_service::{config::Configuration, error::Error as ServiceError, RpcHandlers, TaskManager};
34+
use sc_service::{
35+
config::Configuration, error::Error as ServiceError, RpcHandlers, RpcIdProvider, TaskManager,
36+
};
3437
use sc_telemetry::{Telemetry, TelemetryWorker};
3538
use sp_api::ProvideRuntimeApi;
3639
use sp_core::crypto::Pair;
@@ -308,6 +311,7 @@ pub struct NewFullBase {
308311
/// Creates a full service from the configuration.
309312
pub fn new_full_base(
310313
mut config: Configuration,
314+
rpc_id_provider: impl RpcIdProvider + 'static,
311315
with_startup_data: impl FnOnce(
312316
&sc_consensus_babe::BabeBlockImport<Block, FullClient, FullGrandpaBlockImport>,
313317
&sc_consensus_babe::BabeLink<Block>,
@@ -380,6 +384,7 @@ pub fn new_full_base(
380384
task_manager: &mut task_manager,
381385
system_rpc_tx,
382386
telemetry: telemetry.as_mut(),
387+
rpc_id_provider,
383388
})?;
384389

385390
let (block_import, grandpa_link, babe_link) = import_setup;
@@ -530,13 +535,15 @@ pub fn new_full_base(
530535

531536
/// Builds a new service for a full client.
532537
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
533-
new_full_base(config, |_, _| ()).map(|NewFullBase { task_manager, .. }| task_manager)
538+
new_full_base(config, RandomStringIdProvider::new(16), |_, _| ())
539+
.map(|NewFullBase { task_manager, .. }| task_manager)
534540
}
535541

536542
#[cfg(test)]
537543
mod tests {
538544
use crate::service::{new_full_base, NewFullBase};
539545
use codec::Encode;
546+
use jsonrpsee::ws_server::RandomStringIdProvider;
540547
use node_primitives::{Block, DigestItem, Signature};
541548
use node_runtime::{
542549
constants::{currency::CENTS, time::SLOT_DURATION},
@@ -597,6 +604,7 @@ mod tests {
597604
let NewFullBase { task_manager, client, network, transaction_pool, .. } =
598605
new_full_base(
599606
config,
607+
RandomStringIdProvider::new(16),
600608
|block_import: &sc_consensus_babe::BabeBlockImport<Block, _, _>,
601609
babe_link: &sc_consensus_babe::BabeLink<Block>| {
602610
setup_handles = Some((block_import.clone(), babe_link.clone()));
@@ -771,7 +779,7 @@ mod tests {
771779
crate::chain_spec::tests::integration_test_config_with_two_authorities(),
772780
|config| {
773781
let NewFullBase { task_manager, client, network, transaction_pool, .. } =
774-
new_full_base(config, |_, _| ())?;
782+
new_full_base(config, RandomStringIdProvider::new(16), |_, _| ())?;
775783
Ok(sc_service_test::TestNetComponents::new(
776784
task_manager,
777785
client,

client/rpc-servers/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
use jsonrpsee::{
2424
http_server::{AccessControlBuilder, HttpServerBuilder, HttpServerHandle},
25-
ws_server::{RandomStringIdProvider, WsServerBuilder, WsServerHandle},
25+
ws_server::{IdProvider, WsServerBuilder, WsServerHandle},
2626
RpcModule,
2727
};
2828
use std::net::SocketAddr;
@@ -95,6 +95,7 @@ pub fn start_ws<M: Send + Sync + 'static>(
9595
metrics: Option<RpcMetrics>,
9696
rpc_api: RpcModule<M>,
9797
rt: tokio::runtime::Handle,
98+
id_provider: impl IdProvider + 'static,
9899
) -> Result<WsServerHandle, anyhow::Error> {
99100
let max_request_body_size = max_payload_mb
100101
.map(|mb| mb.saturating_mul(MEGABYTE))
@@ -104,7 +105,7 @@ pub fn start_ws<M: Send + Sync + 'static>(
104105
let mut builder = WsServerBuilder::new()
105106
.max_request_body_size(max_request_body_size as u32)
106107
.max_connections(max_connections as u64)
107-
.set_id_provider(RandomStringIdProvider::new(16))
108+
.set_id_provider(id_provider)
108109
.custom_tokio_runtime(rt.clone());
109110

110111
if let Some(cors) = cors {

client/service/src/builder.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
start_rpc_servers, RpcHandlers, SpawnTaskHandle, TaskManager, TransactionPoolAdapter,
2626
};
2727
use futures::{channel::oneshot, future::ready, FutureExt, StreamExt};
28-
use jsonrpsee::RpcModule;
28+
use jsonrpsee::{core::traits::IdProvider, RpcModule};
2929
use log::info;
3030
use prometheus_endpoint::Registry;
3131
use sc_chain_spec::get_extension;
@@ -322,7 +322,7 @@ where
322322
}
323323

324324
/// Parameters to pass into `build`.
325-
pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> {
325+
pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend, TRpcId> {
326326
/// The service configuration.
327327
pub config: Configuration,
328328
/// A shared client returned by `new_full_parts`.
@@ -344,6 +344,8 @@ pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> {
344344
pub system_rpc_tx: TracingUnboundedSender<sc_rpc::system::Request<TBl>>,
345345
/// Telemetry instance for this node.
346346
pub telemetry: Option<&'a mut Telemetry>,
347+
/// Custom subscription generator for JSON-RPC subscriptions.
348+
pub rpc_id_provider: TRpcId,
347349
}
348350

349351
/// Build a shared offchain workers instance.
@@ -379,8 +381,8 @@ where
379381
}
380382

381383
/// Spawn the tasks that are required to run a node.
382-
pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
383-
params: SpawnTasksParams<TBl, TCl, TExPool, TRpc, TBackend>,
384+
pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl, TRpcId>(
385+
params: SpawnTasksParams<TBl, TCl, TExPool, TRpc, TBackend, TRpcId>,
384386
) -> Result<RpcHandlers, Error>
385387
where
386388
TCl: ProvideRuntimeApi<TBl>
@@ -409,6 +411,7 @@ where
409411
TExPool: MaintainedTransactionPool<Block = TBl, Hash = <TBl as BlockT>::Hash>
410412
+ parity_util_mem::MallocSizeOf
411413
+ 'static,
414+
TRpcId: IdProvider + 'static,
412415
{
413416
let SpawnTasksParams {
414417
mut config,
@@ -421,6 +424,7 @@ where
421424
network,
422425
system_rpc_tx,
423426
telemetry,
427+
rpc_id_provider,
424428
} = params;
425429

426430
let chain_info = client.usage_info().chain;
@@ -491,7 +495,7 @@ where
491495
)
492496
};
493497

494-
let rpc = start_rpc_servers(&config, gen_rpc_module)?;
498+
let rpc = start_rpc_servers(&config, gen_rpc_module, rpc_id_provider)?;
495499
let rpc_handlers = RpcHandlers(Arc::new(gen_rpc_module(sc_rpc::DenyUnsafe::No)?.into()));
496500

497501
// Spawn informant task

client/service/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub use sc_chain_spec::{
6666
Properties, RuntimeGenesis,
6767
};
6868

69+
pub use jsonrpsee::core::traits::IdProvider as RpcIdProvider;
6970
pub use sc_consensus::ImportQueue;
7071
pub use sc_executor::NativeExecutionDispatch;
7172
#[doc(hidden)]
@@ -314,6 +315,7 @@ mod waiting {
314315
fn start_rpc_servers<R>(
315316
config: &Configuration,
316317
gen_rpc_module: R,
318+
rpc_id_provider: impl RpcIdProvider + 'static,
317319
) -> Result<Box<dyn std::any::Any + Send + Sync>, error::Error>
318320
where
319321
R: Fn(sc_rpc::DenyUnsafe) -> Result<RpcModule<()>, Error>,
@@ -360,6 +362,7 @@ where
360362
metrics,
361363
gen_rpc_module(deny_unsafe(http_addr, &config.rpc_methods))?,
362364
config.tokio_handle.clone(),
365+
rpc_id_provider,
363366
)
364367
.map_err(|e| Error::Application(e.into()))?;
365368

0 commit comments

Comments
 (0)