use crate::{
arg_enums::Database, error::Result, DatabaseParams, ImportParams, KeystoreParams,
NetworkParams, NodeKeyParams, OffchainWorkerParams, PruningParams, RpcEndpoint, SharedParams,
SubstrateCli,
};
use log::warn;
use names::{Generator, Name};
use sc_service::{
config::{
BasePath, Configuration, DatabaseSource, ExecutorConfiguration, IpNetwork, KeystoreConfig,
NetworkConfiguration, NodeKeyConfig, OffchainWorkerConfig, PrometheusConfig, PruningMode,
Role, RpcBatchRequestConfig, RpcConfiguration, RpcMethods, TelemetryEndpoints,
TransactionPoolOptions, WasmExecutionMethod,
},
BlocksPruning, ChainSpec, TracingReceiver,
};
use sc_tracing::logging::LoggerBuilder;
use std::{num::NonZeroU32, path::PathBuf};
pub(crate) const NODE_NAME_MAX_LENGTH: usize = 64;
pub(crate) const DEFAULT_NETWORK_CONFIG_PATH: &str = "network";
const RECOMMENDED_OPEN_FILE_DESCRIPTOR_LIMIT: u64 = 10_000;
pub const RPC_DEFAULT_PORT: u16 = 9944;
pub const RPC_DEFAULT_MAX_SUBS_PER_CONN: u32 = 1024;
pub const RPC_DEFAULT_MAX_REQUEST_SIZE_MB: u32 = 15;
pub const RPC_DEFAULT_MAX_RESPONSE_SIZE_MB: u32 = 15;
pub const RPC_DEFAULT_MAX_CONNECTIONS: u32 = 100;
pub const RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN: u32 = 64;
pub trait DefaultConfigurationValues {
fn p2p_listen_port() -> u16 {
30333
}
fn rpc_listen_port() -> u16 {
RPC_DEFAULT_PORT
}
fn prometheus_listen_port() -> u16 {
9615
}
}
impl DefaultConfigurationValues for () {}
pub trait CliConfiguration<DCV: DefaultConfigurationValues = ()>: Sized {
fn shared_params(&self) -> &SharedParams;
fn import_params(&self) -> Option<&ImportParams> {
None
}
fn pruning_params(&self) -> Option<&PruningParams> {
self.import_params().map(|x| &x.pruning_params)
}
fn keystore_params(&self) -> Option<&KeystoreParams> {
None
}
fn network_params(&self) -> Option<&NetworkParams> {
None
}
fn offchain_worker_params(&self) -> Option<&OffchainWorkerParams> {
None
}
fn node_key_params(&self) -> Option<&NodeKeyParams> {
self.network_params().map(|x| &x.node_key_params)
}
fn database_params(&self) -> Option<&DatabaseParams> {
self.import_params().map(|x| &x.database_params)
}
fn base_path(&self) -> Result<Option<BasePath>> {
self.shared_params().base_path()
}
fn is_dev(&self) -> Result<bool> {
Ok(self.shared_params().is_dev())
}
fn role(&self, _is_dev: bool) -> Result<Role> {
Ok(Role::Full)
}
fn transaction_pool(&self, _is_dev: bool) -> Result<TransactionPoolOptions> {
Ok(Default::default())
}
fn network_config(
&self,
chain_spec: &Box<dyn ChainSpec>,
is_dev: bool,
is_validator: bool,
net_config_dir: PathBuf,
client_id: &str,
node_name: &str,
node_key: NodeKeyConfig,
default_listen_port: u16,
) -> Result<NetworkConfiguration> {
let network_config = if let Some(network_params) = self.network_params() {
network_params.network_config(
chain_spec,
is_dev,
is_validator,
Some(net_config_dir),
client_id,
node_name,
node_key,
default_listen_port,
)
} else {
NetworkConfiguration::new(node_name, client_id, node_key, Some(net_config_dir))
};
Ok(network_config)
}
fn keystore_config(&self, config_dir: &PathBuf) -> Result<KeystoreConfig> {
self.keystore_params()
.map(|x| x.keystore_config(config_dir))
.unwrap_or_else(|| Ok(KeystoreConfig::InMemory))
}
fn database_cache_size(&self) -> Result<Option<usize>> {
Ok(self.database_params().map(|x| x.database_cache_size()).unwrap_or_default())
}
fn database(&self) -> Result<Option<Database>> {
Ok(self.database_params().and_then(|x| x.database()))
}
fn database_config(
&self,
base_path: &PathBuf,
cache_size: usize,
database: Database,
) -> Result<DatabaseSource> {
let role_dir = "full";
let rocksdb_path = base_path.join("db").join(role_dir);
let paritydb_path = base_path.join("paritydb").join(role_dir);
Ok(match database {
#[cfg(feature = "rocksdb")]
Database::RocksDb => DatabaseSource::RocksDb { path: rocksdb_path, cache_size },
Database::ParityDb => DatabaseSource::ParityDb { path: paritydb_path },
Database::ParityDbDeprecated => {
eprintln!(
"WARNING: \"paritydb-experimental\" database setting is deprecated and will be removed in future releases. \
Please update your setup to use the new value: \"paritydb\"."
);
DatabaseSource::ParityDb { path: paritydb_path }
},
Database::Auto => DatabaseSource::Auto { paritydb_path, rocksdb_path, cache_size },
})
}
fn trie_cache_maximum_size(&self) -> Result<Option<usize>> {
Ok(self.import_params().map(|x| x.trie_cache_maximum_size()).unwrap_or_default())
}
fn state_pruning(&self) -> Result<Option<PruningMode>> {
self.pruning_params()
.map(|x| x.state_pruning())
.unwrap_or_else(|| Ok(Default::default()))
}
fn blocks_pruning(&self) -> Result<BlocksPruning> {
self.pruning_params()
.map(|x| x.blocks_pruning())
.unwrap_or_else(|| Ok(BlocksPruning::KeepFinalized))
}
fn chain_id(&self, is_dev: bool) -> Result<String> {
Ok(self.shared_params().chain_id(is_dev))
}
fn node_name(&self) -> Result<String> {
Ok(generate_node_name())
}
fn wasm_method(&self) -> Result<WasmExecutionMethod> {
Ok(self.import_params().map(|x| x.wasm_method()).unwrap_or_default())
}
fn wasm_runtime_overrides(&self) -> Option<PathBuf> {
self.import_params().map(|x| x.wasm_runtime_overrides()).unwrap_or_default()
}
fn rpc_addr(&self, _default_listen_port: u16) -> Result<Option<Vec<RpcEndpoint>>> {
Ok(None)
}
fn rpc_methods(&self) -> Result<RpcMethods> {
Ok(Default::default())
}
fn rpc_max_connections(&self) -> Result<u32> {
Ok(RPC_DEFAULT_MAX_CONNECTIONS)
}
fn rpc_cors(&self, _is_dev: bool) -> Result<Option<Vec<String>>> {
Ok(Some(Vec::new()))
}
fn rpc_max_request_size(&self) -> Result<u32> {
Ok(RPC_DEFAULT_MAX_REQUEST_SIZE_MB)
}
fn rpc_max_response_size(&self) -> Result<u32> {
Ok(RPC_DEFAULT_MAX_RESPONSE_SIZE_MB)
}
fn rpc_max_subscriptions_per_connection(&self) -> Result<u32> {
Ok(RPC_DEFAULT_MAX_SUBS_PER_CONN)
}
fn rpc_buffer_capacity_per_connection(&self) -> Result<u32> {
Ok(RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN)
}
fn rpc_batch_config(&self) -> Result<RpcBatchRequestConfig> {
Ok(RpcBatchRequestConfig::Unlimited)
}
fn rpc_rate_limit(&self) -> Result<Option<NonZeroU32>> {
Ok(None)
}
fn rpc_rate_limit_whitelisted_ips(&self) -> Result<Vec<IpNetwork>> {
Ok(vec![])
}
fn rpc_rate_limit_trust_proxy_headers(&self) -> Result<bool> {
Ok(false)
}
fn prometheus_config(
&self,
_default_listen_port: u16,
_chain_spec: &Box<dyn ChainSpec>,
) -> Result<Option<PrometheusConfig>> {
Ok(None)
}
fn telemetry_endpoints(
&self,
chain_spec: &Box<dyn ChainSpec>,
) -> Result<Option<TelemetryEndpoints>> {
Ok(chain_spec.telemetry_endpoints().clone())
}
fn default_heap_pages(&self) -> Result<Option<u64>> {
Ok(None)
}
fn offchain_worker(&self, role: &Role) -> Result<OffchainWorkerConfig> {
self.offchain_worker_params()
.map(|x| x.offchain_worker(role))
.unwrap_or_else(|| Ok(OffchainWorkerConfig::default()))
}
fn force_authoring(&self) -> Result<bool> {
Ok(Default::default())
}
fn disable_grandpa(&self) -> Result<bool> {
Ok(Default::default())
}
fn dev_key_seed(&self, _is_dev: bool) -> Result<Option<String>> {
Ok(Default::default())
}
fn tracing_targets(&self) -> Result<Option<String>> {
Ok(self.shared_params().tracing_targets())
}
fn tracing_receiver(&self) -> Result<TracingReceiver> {
Ok(self.shared_params().tracing_receiver())
}
fn node_key(&self, net_config_dir: &PathBuf) -> Result<NodeKeyConfig> {
let is_dev = self.is_dev()?;
let role = self.role(is_dev)?;
self.node_key_params()
.map(|x| x.node_key(net_config_dir, role, is_dev))
.unwrap_or_else(|| Ok(Default::default()))
}
fn max_runtime_instances(&self) -> Result<Option<usize>> {
Ok(Default::default())
}
fn runtime_cache_size(&self) -> Result<u8> {
Ok(2)
}
fn announce_block(&self) -> Result<bool> {
Ok(true)
}
fn create_configuration<C: SubstrateCli>(
&self,
cli: &C,
tokio_handle: tokio::runtime::Handle,
) -> Result<Configuration> {
let is_dev = self.is_dev()?;
let chain_id = self.chain_id(is_dev)?;
let chain_spec = cli.load_spec(&chain_id)?;
let base_path = base_path_or_default(self.base_path()?, &C::executable_name());
let config_dir = build_config_dir(&base_path, chain_spec.id());
let net_config_dir = build_net_config_dir(&config_dir);
let client_id = C::client_id();
let database_cache_size = self.database_cache_size()?.unwrap_or(1024);
let database = self.database()?.unwrap_or(
#[cfg(feature = "rocksdb")]
{
Database::RocksDb
},
#[cfg(not(feature = "rocksdb"))]
{
Database::ParityDb
},
);
let node_key = self.node_key(&net_config_dir)?;
let role = self.role(is_dev)?;
let max_runtime_instances = self.max_runtime_instances()?.unwrap_or(8);
let is_validator = role.is_authority();
let keystore = self.keystore_config(&config_dir)?;
let telemetry_endpoints = self.telemetry_endpoints(&chain_spec)?;
let runtime_cache_size = self.runtime_cache_size()?;
let rpc_addrs: Option<Vec<sc_service::config::RpcEndpoint>> = self
.rpc_addr(DCV::rpc_listen_port())?
.map(|addrs| addrs.into_iter().map(Into::into).collect());
Ok(Configuration {
impl_name: C::impl_name(),
impl_version: C::impl_version(),
tokio_handle,
transaction_pool: self.transaction_pool(is_dev)?,
network: self.network_config(
&chain_spec,
is_dev,
is_validator,
net_config_dir,
client_id.as_str(),
self.node_name()?.as_str(),
node_key,
DCV::p2p_listen_port(),
)?,
keystore,
database: self.database_config(&config_dir, database_cache_size, database)?,
data_path: config_dir,
trie_cache_maximum_size: self.trie_cache_maximum_size()?,
state_pruning: self.state_pruning()?,
blocks_pruning: self.blocks_pruning()?,
executor: ExecutorConfiguration {
wasm_method: self.wasm_method()?,
default_heap_pages: self.default_heap_pages()?,
max_runtime_instances,
runtime_cache_size,
},
wasm_runtime_overrides: self.wasm_runtime_overrides(),
rpc: RpcConfiguration {
addr: rpc_addrs,
methods: self.rpc_methods()?,
max_connections: self.rpc_max_connections()?,
cors: self.rpc_cors(is_dev)?,
max_request_size: self.rpc_max_request_size()?,
max_response_size: self.rpc_max_response_size()?,
id_provider: None,
max_subs_per_conn: self.rpc_max_subscriptions_per_connection()?,
port: DCV::rpc_listen_port(),
message_buffer_capacity: self.rpc_buffer_capacity_per_connection()?,
batch_config: self.rpc_batch_config()?,
rate_limit: self.rpc_rate_limit()?,
rate_limit_whitelisted_ips: self.rpc_rate_limit_whitelisted_ips()?,
rate_limit_trust_proxy_headers: self.rpc_rate_limit_trust_proxy_headers()?,
},
prometheus_config: self
.prometheus_config(DCV::prometheus_listen_port(), &chain_spec)?,
telemetry_endpoints,
offchain_worker: self.offchain_worker(&role)?,
force_authoring: self.force_authoring()?,
disable_grandpa: self.disable_grandpa()?,
dev_key_seed: self.dev_key_seed(is_dev)?,
tracing_targets: self.tracing_targets()?,
tracing_receiver: self.tracing_receiver()?,
chain_spec,
announce_block: self.announce_block()?,
role,
base_path,
})
}
fn log_filters(&self) -> Result<String> {
Ok(self.shared_params().log_filters().join(","))
}
fn detailed_log_output(&self) -> Result<bool> {
Ok(self.shared_params().detailed_log_output())
}
fn enable_log_reloading(&self) -> Result<bool> {
Ok(self.shared_params().enable_log_reloading())
}
fn disable_log_color(&self) -> Result<bool> {
Ok(self.shared_params().disable_log_color())
}
fn init<F>(&self, support_url: &String, impl_version: &String, logger_hook: F) -> Result<()>
where
F: FnOnce(&mut LoggerBuilder),
{
sp_panic_handler::set(support_url, impl_version);
let mut logger = LoggerBuilder::new(self.log_filters()?);
logger
.with_log_reloading(self.enable_log_reloading()?)
.with_detailed_output(self.detailed_log_output()?);
if let Some(tracing_targets) = self.tracing_targets()? {
let tracing_receiver = self.tracing_receiver()?;
logger.with_profiling(tracing_receiver, tracing_targets);
}
if self.disable_log_color()? {
logger.with_colors(false);
}
logger_hook(&mut logger);
logger.init()?;
match fdlimit::raise_fd_limit() {
Ok(fdlimit::Outcome::LimitRaised { to, .. }) =>
if to < RECOMMENDED_OPEN_FILE_DESCRIPTOR_LIMIT {
warn!(
"Low open file descriptor limit configured for the process. \
Current value: {:?}, recommended value: {:?}.",
to, RECOMMENDED_OPEN_FILE_DESCRIPTOR_LIMIT,
);
},
Ok(fdlimit::Outcome::Unsupported) => {
},
Err(error) => {
warn!(
"Failed to configure file descriptor limit for the process: \
{}, recommended value: {:?}.",
error, RECOMMENDED_OPEN_FILE_DESCRIPTOR_LIMIT,
);
},
}
Ok(())
}
}
pub fn generate_node_name() -> String {
loop {
let node_name = Generator::with_naming(Name::Numbered)
.next()
.expect("RNG is available on all supported platforms; qed");
let count = node_name.chars().count();
if count < NODE_NAME_MAX_LENGTH {
return node_name
}
}
}
pub(crate) fn base_path_or_default(
base_path: Option<BasePath>,
executable_name: &String,
) -> BasePath {
base_path.unwrap_or_else(|| BasePath::from_project("", "", executable_name))
}
pub(crate) fn build_config_dir(base_path: &BasePath, chain_spec_id: &str) -> PathBuf {
base_path.config_dir(chain_spec_id)
}
pub(crate) fn build_net_config_dir(config_dir: &PathBuf) -> PathBuf {
config_dir.join(DEFAULT_NETWORK_CONFIG_PATH)
}
pub(crate) fn build_network_key_dir_or_default(
base_path: Option<BasePath>,
chain_spec_id: &str,
executable_name: &String,
) -> PathBuf {
let config_dir =
build_config_dir(&base_path_or_default(base_path, executable_name), chain_spec_id);
build_net_config_dir(&config_dir)
}