Skip to content

Commit

Permalink
rpc server: listen to ipv6 socket if available and `--experimental-…
Browse files Browse the repository at this point in the history
…rpc-endpoint` CLI option (#4792)

Close #3488,
#4331

This changes/adds the following:

1. The default setting is that substrate starts a rpc server that
listens to localhost both Ipv4 and Ipv6 on the same port. Ipv6 is
allowed to fail because some platforms may not support it
2. A new RPC CLI option `--experimental-rpc-endpoint` which allow to
configure arbitrary listen addresses including the port, if this is
enabled no other interfaces are enabled.
3. If the local addr is not found for any of the sockets the server is
not started throws an error.
4. Remove the deny_unsafe from the RPC implementations instead this is
an extension to allow different polices for different interfaces/sockets
such one may enable unsafe on local interface and safe on only the
external interface.

So for instance in this PR it's now possible to start up three RPC
endpoints as follows:
```
$ polkadot --experimental-rpc-endpoint "listen-addr=127.0.0.1:9944,rpc-methods=unsafe" --experimental-rpc-endpoint "listen-addr=0.0.0.0:9945,rpc-methods=safe,rate-limit=100" --experimental-rpc-endpoint "listen-addr=[::1]:9944,optional=true"
```

#### Needs to be addressed

~1. Support binding to a random port if it's fails with the default
stuff for backward compatible reasons~
~2. How to sync that the rpc CLI params and that the rpc-listen-addr
align, hard to maintain...~
~3. Add similar warning prints for exposing unsafe methods on external
interfaces..~
~4. Inline todos + the hacky String conversion from rpc params.~

#### Cons with this PR

Manual strings parsing impl more error-prone than relying on clap....

//cc @jsdw @BulatSaif @PierreBesson @bkchr

---------

Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
  • Loading branch information
niklasad1 and skunert authored Aug 28, 2024
1 parent f0fd083 commit 09254eb
Show file tree
Hide file tree
Showing 62 changed files with 1,472 additions and 647 deletions.
76 changes: 33 additions & 43 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,8 @@ isahc = { version = "1.2" }
itertools = { version = "0.11" }
jobserver = { version = "0.1.26" }
jsonpath_lib = { version = "0.3" }
jsonrpsee = { version = "0.23.2" }
jsonrpsee-core = { version = "0.23.2" }
jsonrpsee = { version = "0.24.3" }
jsonrpsee-core = { version = "0.24.3" }
k256 = { version = "0.13.3", default-features = false }
kitchensink-runtime = { path = "substrate/bin/node/runtime" }
kvdb = { version = "0.13.0" }
Expand Down
4 changes: 2 additions & 2 deletions cumulus/client/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
use std::{
fs,
io::{self, Write},
net::SocketAddr,
path::PathBuf,
sync::Arc,
};

use codec::Encode;
use sc_chain_spec::ChainSpec;
use sc_cli::RpcEndpoint;
use sc_client_api::HeaderBackend;
use sc_service::{
config::{PrometheusConfig, RpcBatchRequestConfig, TelemetryEndpoints},
Expand Down Expand Up @@ -423,7 +423,7 @@ impl sc_cli::CliConfiguration for NormalizedRunCmd {
self.base.rpc_cors(is_dev)
}

fn rpc_addr(&self, default_listen_port: u16) -> sc_cli::Result<Option<SocketAddr>> {
fn rpc_addr(&self, default_listen_port: u16) -> sc_cli::Result<Option<Vec<RpcEndpoint>>> {
self.base.rpc_addr(default_listen_port)
}

Expand Down
6 changes: 3 additions & 3 deletions cumulus/polkadot-parachain/polkadot-parachain-lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ use clap::{Command, CommandFactory, FromArgMatches};
use sc_chain_spec::ChainSpec;
use sc_cli::{
CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams, NetworkParams,
SharedParams, SubstrateCli,
RpcEndpoint, SharedParams, SubstrateCli,
};
use sc_service::{config::PrometheusConfig, BasePath};
use std::{fmt::Debug, marker::PhantomData, net::SocketAddr, path::PathBuf};
use std::{fmt::Debug, marker::PhantomData, path::PathBuf};

/// Trait that can be used to customize some of the customer-facing info related to the node binary
/// that is being built using this library.
Expand Down Expand Up @@ -300,7 +300,7 @@ impl<Config: CliConfig> CliConfiguration<Self> for RelayChainCli<Config> {
.or_else(|| self.base_path.clone().map(Into::into)))
}

fn rpc_addr(&self, default_listen_port: u16) -> sc_cli::Result<Option<SocketAddr>> {
fn rpc_addr(&self, default_listen_port: u16) -> sc_cli::Result<Option<Vec<RpcEndpoint>>> {
self.base.base.rpc_addr(default_listen_port)
}

Expand Down
14 changes: 4 additions & 10 deletions cumulus/polkadot-parachain/polkadot-parachain-lib/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ use crate::{
};
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use parachains_common::{AccountId, Balance, Block, Nonce};
use sc_rpc::{
dev::{Dev, DevApiServer},
DenyUnsafe,
};
use sc_rpc::dev::{Dev, DevApiServer};
use std::{marker::PhantomData, sync::Arc};
use substrate_frame_rpc_system::{System, SystemApiServer};
use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer};
Expand All @@ -37,7 +34,6 @@ pub type RpcExtension = jsonrpsee::RpcModule<()>;

pub(crate) trait BuildRpcExtensions<Client, Backend, Pool> {
fn build_rpc_extensions(
deny_unsafe: DenyUnsafe,
client: Arc<Client>,
backend: Arc<Backend>,
pool: Arc<Pool>,
Expand All @@ -56,7 +52,6 @@ where
RuntimeApi: ConstructNodeRuntimeApi<Block, ParachainClient<RuntimeApi>> + Send + Sync + 'static,
{
fn build_rpc_extensions(
_deny_unsafe: DenyUnsafe,
_client: Arc<ParachainClient<RuntimeApi>>,
_backend: Arc<ParachainBackend>,
_pool: Arc<sc_transaction_pool::FullPool<Block, ParachainClient<RuntimeApi>>>,
Expand All @@ -79,18 +74,17 @@ where
+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
{
fn build_rpc_extensions(
deny_unsafe: DenyUnsafe,
client: Arc<ParachainClient<RuntimeApi>>,
backend: Arc<ParachainBackend>,
pool: Arc<sc_transaction_pool::FullPool<Block, ParachainClient<RuntimeApi>>>,
) -> sc_service::error::Result<RpcExtension> {
let build = || -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>> {
let mut module = RpcExtension::new(());

module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?;
module.merge(System::new(client.clone(), pool).into_rpc())?;
module.merge(TransactionPayment::new(client.clone()).into_rpc())?;
module.merge(StateMigration::new(client.clone(), backend, deny_unsafe).into_rpc())?;
module.merge(Dev::new(client, deny_unsafe).into_rpc())?;
module.merge(StateMigration::new(client.clone(), backend).into_rpc())?;
module.merge(Dev::new(client).into_rpc())?;

Ok(module)
};
Expand Down
Loading

0 comments on commit 09254eb

Please sign in to comment.