// This file is part of Substrate.
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//! Configuration trait for a CLI based on substrate
use crate::arg_enums::Database;
use crate::error::Result;
use crate::{
init_logger, DatabaseParams, ImportParams, KeystoreParams, NetworkParams, NodeKeyParams,
OffchainWorkerParams, PruningParams, SharedParams, SubstrateCli,
};
use names::{Generator, Name};
use sc_client_api::execution_extensions::ExecutionStrategies;
use sc_service::config::{
BasePath, Configuration, DatabaseConfig, ExtTransport, KeystoreConfig, NetworkConfiguration,
NodeKeyConfig, OffchainWorkerConfig, PrometheusConfig, PruningMode, Role, RpcMethods,
TaskExecutor, TelemetryEndpoints, TransactionPoolOptions, WasmExecutionMethod,
};
use sc_service::{ChainSpec, TracingReceiver};
use std::net::SocketAddr;
use std::path::PathBuf;
/// The maximum number of characters for a node name.
pub(crate) const NODE_NAME_MAX_LENGTH: usize = 64;
/// default sub directory to store network config
pub(crate) const DEFAULT_NETWORK_CONFIG_PATH: &'static str = "network";
/// Default configuration values used by Substrate
///
/// These values will be used by [`CliConfiguritation`] to set
/// default values for e.g. the listen port or the RPC port.
pub trait DefaultConfigurationValues {
/// The port Substrate should listen on for p2p connections.
///
/// By default this is `30333`.
fn p2p_listen_port() -> u16 {
30333
}
/// The port Substrate should listen on for websocket connections.
///
/// By default this is `9944`.
fn rpc_ws_listen_port() -> u16 {
9944
}
/// The port Substrate should listen on for http connections.
///
/// By default this is `9933`.
fn rpc_http_listen_port() -> u16 {
9933
}
/// The port Substrate should listen on for prometheus connections.
///
/// By default this is `9615`.
fn prometheus_listen_port() -> u16 {
9615
}
}
impl DefaultConfigurationValues for () {}
/// A trait that allows converting an object to a Configuration
pub trait CliConfiguration: Sized {
/// Get the SharedParams for this object
fn shared_params(&self) -> &SharedParams;
/// Get the ImportParams for this object
fn import_params(&self) -> Option<&ImportParams> {
None
}
/// Get the PruningParams for this object
fn pruning_params(&self) -> Option<&PruningParams> {
self.import_params().map(|x| &x.pruning_params)
}
/// Get the KeystoreParams for this object
fn keystore_params(&self) -> Option<&KeystoreParams> {
None
}
/// Get the NetworkParams for this object
fn network_params(&self) -> Option<&NetworkParams> {
None
}
/// Get a reference to `OffchainWorkerParams` for this object.
fn offchain_worker_params(&self) -> Option<&OffchainWorkerParams> {
None
}
/// Get the NodeKeyParams for this object
fn node_key_params(&self) -> Option<&NodeKeyParams> {
self.network_params().map(|x| &x.node_key_params)
}
/// Get the DatabaseParams for this object
fn database_params(&self) -> Option<&DatabaseParams> {
self.import_params().map(|x| &x.database_params)
}
/// Get the base path of the configuration (if any)
///
/// By default this is retrieved from `SharedParams`.
fn base_path(&self) -> Result