mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 10:27:59 +00:00
Add tests & Service's Configuration has optional fields that shouldn't be optional (#4842)
Related to #4776 Related to https://github.com/paritytech/polkadot/pull/832 To summarize the changes: 1. I did not manage to validate with types the service's Configuration. But I did reduce the possibility of errors by moving all the "fill" functions to their respective structopts 2. I split params.rs to multiple modules: one module params for just CLI parameters and one module commands for CLI subcommands (and RunCmd). Every command and params are in their own file so things are grouped better together and easier to remove 3. I removed the run and run_subcommand helpers as they are not helping much anymore. Running a command is always a set of 3 commands: 1. init 2. update config 3. run. This still allow the user to change the config before arguments get parsed or right after. 4. I added tests for all subcommands. 5. [deleted] Overall the aim is to improve the situation with the Configuration and the optional parameters, add tests, make the API more consistent and simpler.
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use structopt::StructOpt;
|
||||
use sc_service::{
|
||||
Configuration, RuntimeGenesis,
|
||||
config::DatabaseConfig, PruningMode,
|
||||
};
|
||||
|
||||
use crate::error;
|
||||
use crate::arg_enums::{
|
||||
WasmExecutionMethod, TracingReceiver, ExecutionStrategy, DEFAULT_EXECUTION_BLOCK_CONSTRUCTION,
|
||||
DEFAULT_EXECUTION_IMPORT_BLOCK, DEFAULT_EXECUTION_OFFCHAIN_WORKER, DEFAULT_EXECUTION_OTHER,
|
||||
DEFAULT_EXECUTION_SYNCING
|
||||
};
|
||||
|
||||
/// Parameters for block import.
|
||||
#[derive(Debug, StructOpt, Clone)]
|
||||
pub struct ImportParams {
|
||||
/// Specify the state pruning mode, a number of blocks to keep or 'archive'.
|
||||
///
|
||||
/// Default is to keep all block states if the node is running as a
|
||||
/// validator (i.e. 'archive'), otherwise state is only kept for the last
|
||||
/// 256 blocks.
|
||||
#[structopt(long = "pruning", value_name = "PRUNING_MODE")]
|
||||
pub pruning: Option<String>,
|
||||
|
||||
/// Force start with unsafe pruning settings.
|
||||
///
|
||||
/// When running as a validator it is highly recommended to disable state
|
||||
/// pruning (i.e. 'archive') which is the default. The node will refuse to
|
||||
/// start as a validator if pruning is enabled unless this option is set.
|
||||
#[structopt(long = "unsafe-pruning")]
|
||||
pub unsafe_pruning: bool,
|
||||
|
||||
/// Method for executing Wasm runtime code.
|
||||
#[structopt(
|
||||
long = "wasm-execution",
|
||||
value_name = "METHOD",
|
||||
possible_values = &WasmExecutionMethod::enabled_variants(),
|
||||
case_insensitive = true,
|
||||
default_value = "Interpreted"
|
||||
)]
|
||||
pub wasm_method: WasmExecutionMethod,
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[structopt(flatten)]
|
||||
pub execution_strategies: ExecutionStrategies,
|
||||
|
||||
/// Limit the memory the database cache can use.
|
||||
#[structopt(long = "db-cache", value_name = "MiB", default_value = "1024")]
|
||||
pub database_cache_size: u32,
|
||||
|
||||
/// Specify the state cache size.
|
||||
#[structopt(long = "state-cache-size", value_name = "Bytes", default_value = "67108864")]
|
||||
pub state_cache_size: usize,
|
||||
|
||||
/// Comma separated list of targets for tracing
|
||||
#[structopt(long = "tracing-targets", value_name = "TARGETS")]
|
||||
pub tracing_targets: Option<String>,
|
||||
|
||||
/// Receiver to process tracing messages
|
||||
#[structopt(
|
||||
long = "tracing-receiver",
|
||||
value_name = "RECEIVER",
|
||||
possible_values = &TracingReceiver::variants(),
|
||||
case_insensitive = true,
|
||||
default_value = "Log"
|
||||
)]
|
||||
pub tracing_receiver: TracingReceiver,
|
||||
}
|
||||
|
||||
impl ImportParams {
|
||||
/// Put block import CLI params into `config` object.
|
||||
pub fn update_config<G, E>(
|
||||
&self,
|
||||
config: &mut Configuration<G, E>,
|
||||
role: sc_service::Roles,
|
||||
is_dev: bool,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
G: RuntimeGenesis,
|
||||
{
|
||||
use sc_client_api::execution_extensions::ExecutionStrategies;
|
||||
|
||||
if let Some(DatabaseConfig::Path { ref mut cache_size, .. }) = config.database {
|
||||
*cache_size = Some(self.database_cache_size);
|
||||
}
|
||||
|
||||
config.state_cache_size = self.state_cache_size;
|
||||
|
||||
// by default we disable pruning if the node is an authority (i.e.
|
||||
// `ArchiveAll`), otherwise we keep state for the last 256 blocks. if the
|
||||
// node is an authority and pruning is enabled explicitly, then we error
|
||||
// unless `unsafe_pruning` is set.
|
||||
config.pruning = match &self.pruning {
|
||||
Some(ref s) if s == "archive" => PruningMode::ArchiveAll,
|
||||
None if role == sc_service::Roles::AUTHORITY => PruningMode::ArchiveAll,
|
||||
None => PruningMode::default(),
|
||||
Some(s) => {
|
||||
if role == sc_service::Roles::AUTHORITY && !self.unsafe_pruning {
|
||||
return Err(error::Error::Input(
|
||||
"Validators should run with state pruning disabled (i.e. archive). \
|
||||
You can ignore this check with `--unsafe-pruning`.".to_string()
|
||||
));
|
||||
}
|
||||
|
||||
PruningMode::keep_blocks(s.parse()
|
||||
.map_err(|_| error::Error::Input("Invalid pruning mode specified".to_string()))?
|
||||
)
|
||||
},
|
||||
};
|
||||
|
||||
config.wasm_method = self.wasm_method.into();
|
||||
|
||||
let exec = &self.execution_strategies;
|
||||
let exec_all_or = |strat: ExecutionStrategy, default: ExecutionStrategy| {
|
||||
exec.execution.unwrap_or(if strat == default && is_dev {
|
||||
ExecutionStrategy::Native
|
||||
} else {
|
||||
strat
|
||||
}).into()
|
||||
};
|
||||
|
||||
config.execution_strategies = ExecutionStrategies {
|
||||
syncing: exec_all_or(exec.execution_syncing, DEFAULT_EXECUTION_SYNCING),
|
||||
importing: exec_all_or(exec.execution_import_block, DEFAULT_EXECUTION_IMPORT_BLOCK),
|
||||
block_construction:
|
||||
exec_all_or(exec.execution_block_construction, DEFAULT_EXECUTION_BLOCK_CONSTRUCTION),
|
||||
offchain_worker:
|
||||
exec_all_or(exec.execution_offchain_worker, DEFAULT_EXECUTION_OFFCHAIN_WORKER),
|
||||
other: exec_all_or(exec.execution_other, DEFAULT_EXECUTION_OTHER),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Execution strategies parameters.
|
||||
#[derive(Debug, StructOpt, Clone)]
|
||||
pub struct ExecutionStrategies {
|
||||
/// The means of execution used when calling into the runtime while syncing blocks.
|
||||
#[structopt(
|
||||
long = "execution-syncing",
|
||||
value_name = "STRATEGY",
|
||||
possible_values = &ExecutionStrategy::variants(),
|
||||
case_insensitive = true,
|
||||
default_value = DEFAULT_EXECUTION_SYNCING.as_str(),
|
||||
)]
|
||||
pub execution_syncing: ExecutionStrategy,
|
||||
|
||||
/// The means of execution used when calling into the runtime while importing blocks.
|
||||
#[structopt(
|
||||
long = "execution-import-block",
|
||||
value_name = "STRATEGY",
|
||||
possible_values = &ExecutionStrategy::variants(),
|
||||
case_insensitive = true,
|
||||
default_value = DEFAULT_EXECUTION_IMPORT_BLOCK.as_str(),
|
||||
)]
|
||||
pub execution_import_block: ExecutionStrategy,
|
||||
|
||||
/// The means of execution used when calling into the runtime while constructing blocks.
|
||||
#[structopt(
|
||||
long = "execution-block-construction",
|
||||
value_name = "STRATEGY",
|
||||
possible_values = &ExecutionStrategy::variants(),
|
||||
case_insensitive = true,
|
||||
default_value = DEFAULT_EXECUTION_BLOCK_CONSTRUCTION.as_str(),
|
||||
)]
|
||||
pub execution_block_construction: ExecutionStrategy,
|
||||
|
||||
/// The means of execution used when calling into the runtime while using an off-chain worker.
|
||||
#[structopt(
|
||||
long = "execution-offchain-worker",
|
||||
value_name = "STRATEGY",
|
||||
possible_values = &ExecutionStrategy::variants(),
|
||||
case_insensitive = true,
|
||||
default_value = DEFAULT_EXECUTION_OFFCHAIN_WORKER.as_str(),
|
||||
)]
|
||||
pub execution_offchain_worker: ExecutionStrategy,
|
||||
|
||||
/// The means of execution used when calling into the runtime while not syncing, importing or constructing blocks.
|
||||
#[structopt(
|
||||
long = "execution-other",
|
||||
value_name = "STRATEGY",
|
||||
possible_values = &ExecutionStrategy::variants(),
|
||||
case_insensitive = true,
|
||||
default_value = DEFAULT_EXECUTION_OTHER.as_str(),
|
||||
)]
|
||||
pub execution_other: ExecutionStrategy,
|
||||
|
||||
/// The execution strategy that should be used by all execution contexts.
|
||||
#[structopt(
|
||||
long = "execution",
|
||||
value_name = "STRATEGY",
|
||||
possible_values = &ExecutionStrategy::variants(),
|
||||
case_insensitive = true,
|
||||
conflicts_with_all = &[
|
||||
"execution-other",
|
||||
"execution-offchain-worker",
|
||||
"execution-block-construction",
|
||||
"execution-import-block",
|
||||
"execution-syncing",
|
||||
]
|
||||
)]
|
||||
pub execution: Option<ExecutionStrategy>,
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
mod import_params;
|
||||
mod transaction_pool_params;
|
||||
mod shared_params;
|
||||
mod node_key_params;
|
||||
mod network_configuration_params;
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub use crate::params::import_params::*;
|
||||
pub use crate::params::transaction_pool_params::*;
|
||||
pub use crate::params::shared_params::*;
|
||||
pub use crate::params::node_key_params::*;
|
||||
pub use crate::params::network_configuration_params::*;
|
||||
|
||||
/// Wrapper type of `String` that holds an unsigned integer of arbitrary size, formatted as a decimal.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BlockNumber(String);
|
||||
|
||||
impl FromStr for BlockNumber {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(block_number: &str) -> Result<Self, Self::Err> {
|
||||
if block_number.chars().any(|d| !d.is_digit(10)) {
|
||||
Err(format!(
|
||||
"Invalid block number: {}, expected decimal formatted unsigned integer",
|
||||
block_number,
|
||||
))
|
||||
} else {
|
||||
Ok(Self(block_number.to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockNumber {
|
||||
/// Wrapper on top of `std::str::parse<N>` but with `Error` as a `String`
|
||||
///
|
||||
/// See `https://doc.rust-lang.org/std/primitive.str.html#method.parse` for more elaborate
|
||||
/// documentation.
|
||||
pub fn parse<N>(&self) -> Result<N, String>
|
||||
where
|
||||
N: FromStr,
|
||||
N::Err: std::fmt::Debug,
|
||||
{
|
||||
self.0
|
||||
.parse()
|
||||
.map_err(|e| format!("BlockNumber: {} parsing failed because of {:?}", self.0, e))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::iter;
|
||||
use std::net::Ipv4Addr;
|
||||
use structopt::StructOpt;
|
||||
use sc_network::{
|
||||
config::{NonReservedPeerMode, TransportConfig}, multiaddr::Protocol,
|
||||
};
|
||||
use sc_service::{Configuration, RuntimeGenesis};
|
||||
|
||||
use crate::error;
|
||||
use crate::params::node_key_params::NodeKeyParams;
|
||||
|
||||
/// Parameters used to create the network configuration.
|
||||
#[derive(Debug, StructOpt, Clone)]
|
||||
pub struct NetworkConfigurationParams {
|
||||
/// Specify a list of bootnodes.
|
||||
#[structopt(long = "bootnodes", value_name = "URL")]
|
||||
pub bootnodes: Vec<String>,
|
||||
|
||||
/// Specify a list of reserved node addresses.
|
||||
#[structopt(long = "reserved-nodes", value_name = "URL")]
|
||||
pub reserved_nodes: Vec<String>,
|
||||
|
||||
/// Whether to only allow connections to/from reserved nodes.
|
||||
///
|
||||
/// If you are a validator your node might still connect to other validator
|
||||
/// nodes regardless of whether they are defined as reserved nodes.
|
||||
#[structopt(long = "reserved-only")]
|
||||
pub reserved_only: bool,
|
||||
|
||||
/// Specify a list of sentry node public addresses.
|
||||
#[structopt(
|
||||
long = "sentry-nodes",
|
||||
value_name = "URL",
|
||||
conflicts_with_all = &[ "sentry" ]
|
||||
)]
|
||||
pub sentry_nodes: Vec<String>,
|
||||
|
||||
/// Listen on this multiaddress.
|
||||
#[structopt(long = "listen-addr", value_name = "LISTEN_ADDR")]
|
||||
pub listen_addr: Vec<String>,
|
||||
|
||||
/// Specify p2p protocol TCP port.
|
||||
///
|
||||
/// Only used if --listen-addr is not specified.
|
||||
#[structopt(long = "port", value_name = "PORT")]
|
||||
pub port: Option<u16>,
|
||||
|
||||
/// Forbid connecting to private IPv4 addresses (as specified in
|
||||
/// [RFC1918](https://tools.ietf.org/html/rfc1918)), unless the address was passed with
|
||||
/// `--reserved-nodes` or `--bootnodes`.
|
||||
#[structopt(long = "no-private-ipv4")]
|
||||
pub no_private_ipv4: bool,
|
||||
|
||||
/// Specify the number of outgoing connections we're trying to maintain.
|
||||
#[structopt(long = "out-peers", value_name = "COUNT", default_value = "25")]
|
||||
pub out_peers: u32,
|
||||
|
||||
/// Specify the maximum number of incoming connections we're accepting.
|
||||
#[structopt(long = "in-peers", value_name = "COUNT", default_value = "25")]
|
||||
pub in_peers: u32,
|
||||
|
||||
/// Disable mDNS discovery.
|
||||
///
|
||||
/// By default, the network will use mDNS to discover other nodes on the
|
||||
/// local network. This disables it. Automatically implied when using --dev.
|
||||
#[structopt(long = "no-mdns")]
|
||||
pub no_mdns: bool,
|
||||
|
||||
/// Maximum number of peers to ask the same blocks in parallel.
|
||||
///
|
||||
/// This allows downlading announced blocks from multiple peers. Decrease to save
|
||||
/// traffic and risk increased latency.
|
||||
#[structopt(long = "max-parallel-downloads", value_name = "COUNT", default_value = "5")]
|
||||
pub max_parallel_downloads: u32,
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[structopt(flatten)]
|
||||
pub node_key_params: NodeKeyParams,
|
||||
|
||||
/// Experimental feature flag.
|
||||
#[structopt(long = "use-yamux-flow-control")]
|
||||
pub use_yamux_flow_control: bool,
|
||||
}
|
||||
|
||||
impl NetworkConfigurationParams {
|
||||
/// Fill the given `NetworkConfiguration` by looking at the cli parameters.
|
||||
pub fn update_config<G, E>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
config_path: PathBuf,
|
||||
client_id: String,
|
||||
is_dev: bool,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
G: RuntimeGenesis,
|
||||
{
|
||||
config.network.boot_nodes.extend(self.bootnodes.clone());
|
||||
config.network.config_path = Some(config_path.clone());
|
||||
config.network.net_config_path = Some(config_path.clone());
|
||||
|
||||
config.network.reserved_nodes.extend(self.reserved_nodes.clone());
|
||||
if self.reserved_only {
|
||||
config.network.non_reserved_mode = NonReservedPeerMode::Deny;
|
||||
}
|
||||
|
||||
config.network.sentry_nodes.extend(self.sentry_nodes.clone());
|
||||
|
||||
for addr in self.listen_addr.iter() {
|
||||
let addr = addr.parse().ok().ok_or(error::Error::InvalidListenMultiaddress)?;
|
||||
config.network.listen_addresses.push(addr);
|
||||
}
|
||||
|
||||
if config.network.listen_addresses.is_empty() {
|
||||
let port = match self.port {
|
||||
Some(port) => port,
|
||||
None => 30333,
|
||||
};
|
||||
|
||||
config.network.listen_addresses = vec![
|
||||
iter::once(Protocol::Ip4(Ipv4Addr::new(0, 0, 0, 0)))
|
||||
.chain(iter::once(Protocol::Tcp(port)))
|
||||
.collect()
|
||||
];
|
||||
}
|
||||
|
||||
config.network.client_version = client_id;
|
||||
self.node_key_params.update_config(&mut config, Some(&config_path))?;
|
||||
|
||||
config.network.in_peers = self.in_peers;
|
||||
config.network.out_peers = self.out_peers;
|
||||
|
||||
config.network.transport = TransportConfig::Normal {
|
||||
enable_mdns: !is_dev && !self.no_mdns,
|
||||
allow_private_ipv4: !self.no_private_ipv4,
|
||||
wasm_external_transport: None,
|
||||
use_yamux_flow_control: self.use_yamux_flow_control,
|
||||
};
|
||||
|
||||
config.network.max_parallel_downloads = self.max_parallel_downloads;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{path::PathBuf, str::FromStr};
|
||||
use structopt::StructOpt;
|
||||
use sc_service::{Configuration, RuntimeGenesis};
|
||||
use sc_network::config::NodeKeyConfig;
|
||||
use sp_core::H256;
|
||||
|
||||
use crate::error;
|
||||
use crate::arg_enums::NodeKeyType;
|
||||
|
||||
/// The file name of the node's Ed25519 secret key inside the chain-specific
|
||||
/// network config directory, if neither `--node-key` nor `--node-key-file`
|
||||
/// is specified in combination with `--node-key-type=ed25519`.
|
||||
const NODE_KEY_ED25519_FILE: &str = "secret_ed25519";
|
||||
|
||||
/// Parameters used to create the `NodeKeyConfig`, which determines the keypair
|
||||
/// used for libp2p networking.
|
||||
#[derive(Debug, StructOpt, Clone)]
|
||||
pub struct NodeKeyParams {
|
||||
/// The secret key to use for libp2p networking.
|
||||
///
|
||||
/// The value is a string that is parsed according to the choice of
|
||||
/// `--node-key-type` as follows:
|
||||
///
|
||||
/// `ed25519`:
|
||||
/// The value is parsed as a hex-encoded Ed25519 32 bytes secret key,
|
||||
/// i.e. 64 hex characters.
|
||||
///
|
||||
/// The value of this option takes precedence over `--node-key-file`.
|
||||
///
|
||||
/// WARNING: Secrets provided as command-line arguments are easily exposed.
|
||||
/// Use of this option should be limited to development and testing. To use
|
||||
/// an externally managed secret key, use `--node-key-file` instead.
|
||||
#[structopt(long = "node-key", value_name = "KEY")]
|
||||
pub node_key: Option<String>,
|
||||
|
||||
/// The type of secret key to use for libp2p networking.
|
||||
///
|
||||
/// The secret key of the node is obtained as follows:
|
||||
///
|
||||
/// * If the `--node-key` option is given, the value is parsed as a secret key
|
||||
/// according to the type. See the documentation for `--node-key`.
|
||||
///
|
||||
/// * If the `--node-key-file` option is given, the secret key is read from the
|
||||
/// specified file. See the documentation for `--node-key-file`.
|
||||
///
|
||||
/// * Otherwise, the secret key is read from a file with a predetermined,
|
||||
/// type-specific name from the chain-specific network config directory
|
||||
/// inside the base directory specified by `--base-dir`. If this file does
|
||||
/// not exist, it is created with a newly generated secret key of the
|
||||
/// chosen type.
|
||||
///
|
||||
/// The node's secret key determines the corresponding public key and hence the
|
||||
/// node's peer ID in the context of libp2p.
|
||||
#[structopt(
|
||||
long = "node-key-type",
|
||||
value_name = "TYPE",
|
||||
possible_values = &NodeKeyType::variants(),
|
||||
case_insensitive = true,
|
||||
default_value = "Ed25519"
|
||||
)]
|
||||
pub node_key_type: NodeKeyType,
|
||||
|
||||
/// The file from which to read the node's secret key to use for libp2p networking.
|
||||
///
|
||||
/// The contents of the file are parsed according to the choice of `--node-key-type`
|
||||
/// as follows:
|
||||
///
|
||||
/// `ed25519`:
|
||||
/// The file must contain an unencoded 32 bytes Ed25519 secret key.
|
||||
///
|
||||
/// If the file does not exist, it is created with a newly generated secret key of
|
||||
/// the chosen type.
|
||||
#[structopt(long = "node-key-file", value_name = "FILE")]
|
||||
pub node_key_file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl NodeKeyParams {
|
||||
/// Create a `NodeKeyConfig` from the given `NodeKeyParams` in the context
|
||||
/// of an optional network config storage directory.
|
||||
pub fn update_config<'a, G, E>(
|
||||
&self,
|
||||
mut config: &'a mut Configuration<G, E>,
|
||||
net_config_path: Option<&PathBuf>,
|
||||
) -> error::Result<&'a NodeKeyConfig>
|
||||
where
|
||||
G: RuntimeGenesis,
|
||||
{
|
||||
config.network.node_key = match self.node_key_type {
|
||||
NodeKeyType::Ed25519 => {
|
||||
let secret = if let Some(node_key) = self.node_key.as_ref() {
|
||||
parse_ed25519_secret(node_key)?
|
||||
} else {
|
||||
let path = self.node_key_file.clone()
|
||||
.or_else(|| net_config_path.map(|d| d.join(NODE_KEY_ED25519_FILE)));
|
||||
|
||||
if let Some(path) = path {
|
||||
sc_network::config::Secret::File(path)
|
||||
} else {
|
||||
sc_network::config::Secret::New
|
||||
}
|
||||
};
|
||||
|
||||
NodeKeyConfig::Ed25519(secret)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(&config.network.node_key)
|
||||
}
|
||||
}
|
||||
|
||||
/// Create an error caused by an invalid node key argument.
|
||||
fn invalid_node_key(e: impl std::fmt::Display) -> error::Error {
|
||||
error::Error::Input(format!("Invalid node key: {}", e))
|
||||
}
|
||||
|
||||
/// Parse a Ed25519 secret key from a hex string into a `sc_network::Secret`.
|
||||
fn parse_ed25519_secret(hex: &str) -> error::Result<sc_network::config::Ed25519Secret> {
|
||||
H256::from_str(&hex).map_err(invalid_node_key).and_then(|bytes|
|
||||
sc_network::config::identity::ed25519::SecretKey::from_bytes(bytes)
|
||||
.map(sc_network::config::Secret::Input)
|
||||
.map_err(invalid_node_key))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use sc_network::config::identity::ed25519;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_node_key_config_input() {
|
||||
fn secret_input(net_config_dir: Option<&PathBuf>) -> error::Result<()> {
|
||||
NodeKeyType::variants().iter().try_for_each(|t| {
|
||||
let mut config = Configuration::<(), ()>::default();
|
||||
let node_key_type = NodeKeyType::from_str(t).unwrap();
|
||||
let sk = match node_key_type {
|
||||
NodeKeyType::Ed25519 => ed25519::SecretKey::generate().as_ref().to_vec()
|
||||
};
|
||||
let params = NodeKeyParams {
|
||||
node_key_type,
|
||||
node_key: Some(format!("{:x}", H256::from_slice(sk.as_ref()))),
|
||||
node_key_file: None
|
||||
};
|
||||
params.update_config(&mut config, net_config_dir).and_then(|c| match c {
|
||||
NodeKeyConfig::Ed25519(sc_network::config::Secret::Input(ref ski))
|
||||
if node_key_type == NodeKeyType::Ed25519 &&
|
||||
&sk[..] == ski.as_ref() => Ok(()),
|
||||
_ => Err(error::Error::Input("Unexpected node key config".into()))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
assert!(secret_input(None).is_ok());
|
||||
assert!(secret_input(Some(&PathBuf::from_str("x").unwrap())).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_node_key_config_file() {
|
||||
fn secret_file(net_config_dir: Option<&PathBuf>) -> error::Result<()> {
|
||||
NodeKeyType::variants().iter().try_for_each(|t| {
|
||||
let mut config = Configuration::<(), ()>::default();
|
||||
let node_key_type = NodeKeyType::from_str(t).unwrap();
|
||||
let tmp = tempfile::Builder::new().prefix("alice").tempdir()?;
|
||||
let file = tmp.path().join(format!("{}_mysecret", t)).to_path_buf();
|
||||
let params = NodeKeyParams {
|
||||
node_key_type,
|
||||
node_key: None,
|
||||
node_key_file: Some(file.clone())
|
||||
};
|
||||
params.update_config(&mut config, net_config_dir).and_then(|c| match c {
|
||||
NodeKeyConfig::Ed25519(sc_network::config::Secret::File(ref f))
|
||||
if node_key_type == NodeKeyType::Ed25519 && f == &file => Ok(()),
|
||||
_ => Err(error::Error::Input("Unexpected node key config".into()))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
assert!(secret_file(None).is_ok());
|
||||
assert!(secret_file(Some(&PathBuf::from_str("x").unwrap())).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_node_key_config_default() {
|
||||
fn with_def_params<F>(f: F) -> error::Result<()>
|
||||
where
|
||||
F: Fn(NodeKeyParams) -> error::Result<()>
|
||||
{
|
||||
NodeKeyType::variants().iter().try_for_each(|t| {
|
||||
let node_key_type = NodeKeyType::from_str(t).unwrap();
|
||||
f(NodeKeyParams {
|
||||
node_key_type,
|
||||
node_key: None,
|
||||
node_key_file: None
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn no_config_dir() -> error::Result<()> {
|
||||
with_def_params(|params| {
|
||||
let mut config = Configuration::<(), ()>::default();
|
||||
let typ = params.node_key_type;
|
||||
params.update_config(&mut config, None)
|
||||
.and_then(|c| match c {
|
||||
NodeKeyConfig::Ed25519(sc_network::config::Secret::New)
|
||||
if typ == NodeKeyType::Ed25519 => Ok(()),
|
||||
_ => Err(error::Error::Input("Unexpected node key config".into()))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn some_config_dir(net_config_dir: &PathBuf) -> error::Result<()> {
|
||||
with_def_params(|params| {
|
||||
let mut config = Configuration::<(), ()>::default();
|
||||
let dir = PathBuf::from(net_config_dir.clone());
|
||||
let typ = params.node_key_type;
|
||||
params.update_config(&mut config, Some(net_config_dir))
|
||||
.and_then(move |c| match c {
|
||||
NodeKeyConfig::Ed25519(sc_network::config::Secret::File(ref f))
|
||||
if typ == NodeKeyType::Ed25519 &&
|
||||
f == &dir.join(NODE_KEY_ED25519_FILE) => Ok(()),
|
||||
_ => Err(error::Error::Input("Unexpected node key config".into()))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
assert!(no_config_dir().is_ok());
|
||||
assert!(some_config_dir(&PathBuf::from_str("x").unwrap()).is_ok());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
use app_dirs::{AppInfo, AppDataType};
|
||||
use sc_service::{
|
||||
Configuration, ChainSpecExtension, RuntimeGenesis,
|
||||
config::DatabaseConfig, ChainSpec,
|
||||
};
|
||||
|
||||
use crate::VersionInfo;
|
||||
use crate::error;
|
||||
|
||||
/// default sub directory to store database
|
||||
const DEFAULT_DB_CONFIG_PATH : &'static str = "db";
|
||||
|
||||
/// Shared parameters used by all `CoreParams`.
|
||||
#[derive(Debug, StructOpt, Clone)]
|
||||
pub struct SharedParams {
|
||||
/// Specify the chain specification (one of dev, local or staging).
|
||||
#[structopt(long = "chain", value_name = "CHAIN_SPEC")]
|
||||
pub chain: Option<String>,
|
||||
|
||||
/// Specify the development chain.
|
||||
#[structopt(long = "dev")]
|
||||
pub dev: bool,
|
||||
|
||||
/// Specify custom base path.
|
||||
#[structopt(long = "base-path", short = "d", value_name = "PATH", parse(from_os_str))]
|
||||
pub base_path: Option<PathBuf>,
|
||||
|
||||
/// Sets a custom logging filter.
|
||||
#[structopt(short = "l", long = "log", value_name = "LOG_PATTERN")]
|
||||
pub log: Option<String>,
|
||||
}
|
||||
|
||||
impl SharedParams {
|
||||
/// Load spec to `Configuration` from `SharedParams` and spec factory.
|
||||
pub fn update_config<'a, G, E, F>(
|
||||
&self,
|
||||
mut config: &'a mut Configuration<G, E>,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<&'a ChainSpec<G, E>> where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
{
|
||||
let chain_key = match self.chain {
|
||||
Some(ref chain) => chain.clone(),
|
||||
None => if self.dev { "dev".into() } else { "".into() }
|
||||
};
|
||||
let spec = match spec_factory(&chain_key)? {
|
||||
Some(spec) => spec,
|
||||
None => ChainSpec::from_json_file(PathBuf::from(chain_key))?
|
||||
};
|
||||
|
||||
config.network.boot_nodes = spec.boot_nodes().to_vec();
|
||||
config.telemetry_endpoints = spec.telemetry_endpoints().clone();
|
||||
|
||||
config.chain_spec = Some(spec);
|
||||
|
||||
if config.config_dir.is_none() {
|
||||
config.config_dir = Some(base_path(self, version));
|
||||
}
|
||||
|
||||
if config.database.is_none() {
|
||||
config.database = Some(DatabaseConfig::Path {
|
||||
path: config
|
||||
.in_chain_config_dir(DEFAULT_DB_CONFIG_PATH)
|
||||
.expect("We provided a base_path/config_dir."),
|
||||
cache_size: None,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(config.chain_spec.as_ref().unwrap())
|
||||
}
|
||||
|
||||
/// Initialize substrate. This must be done only once.
|
||||
///
|
||||
/// This method:
|
||||
///
|
||||
/// 1. Set the panic handler
|
||||
/// 2. Raise the FD limit
|
||||
/// 3. Initialize the logger
|
||||
pub fn init(&self, version: &VersionInfo) -> error::Result<()> {
|
||||
crate::init(self.log.as_ref().map(|v| v.as_ref()).unwrap_or(""), version)
|
||||
}
|
||||
}
|
||||
|
||||
fn base_path(cli: &SharedParams, version: &VersionInfo) -> PathBuf {
|
||||
cli.base_path.clone()
|
||||
.unwrap_or_else(||
|
||||
app_dirs::get_app_root(
|
||||
AppDataType::UserData,
|
||||
&AppInfo {
|
||||
name: version.executable_name,
|
||||
author: version.author
|
||||
}
|
||||
).expect("app directories exist on all supported platforms; qed")
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate 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.
|
||||
|
||||
// Substrate 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 Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use structopt::StructOpt;
|
||||
use sc_service::Configuration;
|
||||
use crate::error;
|
||||
|
||||
/// Parameters used to create the pool configuration.
|
||||
#[derive(Debug, StructOpt, Clone)]
|
||||
pub struct TransactionPoolParams {
|
||||
/// Maximum number of transactions in the transaction pool.
|
||||
#[structopt(long = "pool-limit", value_name = "COUNT", default_value = "8192")]
|
||||
pub pool_limit: usize,
|
||||
/// Maximum number of kilobytes of all transactions stored in the pool.
|
||||
#[structopt(long = "pool-kbytes", value_name = "COUNT", default_value = "20480")]
|
||||
pub pool_kbytes: usize,
|
||||
}
|
||||
|
||||
impl TransactionPoolParams {
|
||||
/// Fill the given `PoolConfiguration` by looking at the cli parameters.
|
||||
pub fn update_config<G, E>(
|
||||
&self,
|
||||
config: &mut Configuration<G, E>,
|
||||
) -> error::Result<()> {
|
||||
// ready queue
|
||||
config.transaction_pool.ready.count = self.pool_limit;
|
||||
config.transaction_pool.ready.total_bytes = self.pool_kbytes * 1024;
|
||||
|
||||
// future queue
|
||||
let factor = 10;
|
||||
config.transaction_pool.future.count = self.pool_limit / factor;
|
||||
config.transaction_pool.future.total_bytes = self.pool_kbytes * 1024 / factor;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user