Commit 711d47ab authored by Cecile Tonglet's avatar Cecile Tonglet
Browse files

Implement force_kusama parameter

parent c67277ab
Pipeline #84485 canceled with stage
in 3 minutes and 37 seconds
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Predefined chains.
use service;
/// The chain specification (this should eventually be replaced by a more general JSON-based chain
/// specification).
#[derive(Clone, Debug)]
pub enum ChainSpec {
/// Whatever the current polkadot runtime is, with just Alice as an auth.
PolkadotDevelopment,
/// Whatever the current pokadot runtime is, with simple Alice/Bob auths.
PolkadotLocalTestnet,
/// The Kusama network.
Kusama,
/// Whatever the current kusama runtime is, with just Alice as an auth.
KusamaDevelopment,
/// The Westend network,
Westend,
/// Whatever the current polkadot runtime is with the "global testnet" defaults.
PolkadotStagingTestnet,
/// Whatever the current kusama runtime is with the "global testnet" defaults.
KusamaStagingTestnet,
/// Whatever the current kusama runtime is, with simple Alice/Bob auths.
KusamaLocalTestnet,
}
impl Default for ChainSpec {
fn default() -> Self {
ChainSpec::Kusama
}
}
/// Get a chain config from a spec setting.
impl ChainSpec {
pub(crate) fn load(self) -> Result<Box<dyn service::ChainSpec>, String> {
Ok(match self {
ChainSpec::PolkadotDevelopment => Box::new(service::chain_spec::polkadot_development_config()),
ChainSpec::PolkadotLocalTestnet => Box::new(service::chain_spec::polkadot_local_testnet_config()),
ChainSpec::PolkadotStagingTestnet => Box::new(service::chain_spec::polkadot_staging_testnet_config()),
ChainSpec::KusamaDevelopment =>Box::new(service::chain_spec::kusama_development_config()),
ChainSpec::KusamaLocalTestnet => Box::new(service::chain_spec::kusama_local_testnet_config()),
ChainSpec::KusamaStagingTestnet => Box::new(service::chain_spec::kusama_staging_testnet_config()),
ChainSpec::Westend => Box::new(service::chain_spec::westend_config()?),
ChainSpec::Kusama => Box::new(service::chain_spec::kusama_config()?),
})
}
pub(crate) fn from(s: &str) -> Option<Self> {
match s {
"polkadot-dev" | "dev" => Some(ChainSpec::PolkadotDevelopment),
"polkadot-local" => Some(ChainSpec::PolkadotLocalTestnet),
"polkadot-staging" => Some(ChainSpec::PolkadotStagingTestnet),
"kusama-dev" => Some(ChainSpec::KusamaDevelopment),
"kusama-local" => Some(ChainSpec::KusamaLocalTestnet),
"kusama-staging" => Some(ChainSpec::KusamaStagingTestnet),
"kusama" => Some(ChainSpec::Kusama),
"westend" => Some(ChainSpec::Westend),
"" => Some(ChainSpec::default()),
_ => None,
}
}
}
......@@ -18,12 +18,32 @@
use structopt::StructOpt;
#[allow(missing_docs)]
#[derive(Debug, StructOpt, Clone)]
pub struct ForceKusama {
/// Force using Kusama native runtime.
#[structopt(long = "force-kusama")]
pub force_kusama: bool,
}
#[allow(missing_docs)]
#[derive(Debug, StructOpt, Clone)]
pub struct BaseSubcommand {
#[allow(missing_docs)]
#[structopt(flatten)]
pub subcommand: sc_cli::Subcommand,
#[allow(missing_docs)]
#[structopt(flatten)]
pub force_kusama: ForceKusama,
}
#[allow(missing_docs)]
#[derive(Debug, StructOpt, Clone)]
pub enum Subcommand {
#[allow(missing_docs)]
#[structopt(flatten)]
Base(sc_cli::Subcommand),
Base(BaseSubcommand),
#[allow(missing_docs)]
#[structopt(name = "validation-worker", setting = structopt::clap::AppSettings::Hidden)]
......@@ -51,18 +71,13 @@ pub struct RunCmd {
#[structopt(flatten)]
pub base: sc_cli::RunCmd,
/// Force using Kusama native runtime.
#[structopt(long = "force-kusama")]
pub force_kusama: bool,
#[allow(missing_docs)]
#[structopt(flatten)]
pub force_kusama: ForceKusama,
}
#[allow(missing_docs)]
#[derive(Debug, StructOpt, Clone)]
#[structopt(settings = &[
structopt::clap::AppSettings::GlobalVersion,
structopt::clap::AppSettings::ArgsNegateSubcommands,
structopt::clap::AppSettings::SubcommandsNegateReqs,
])]
pub struct Cli {
#[allow(missing_docs)]
#[structopt(subcommand)]
......
......@@ -14,14 +14,20 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use std::path::PathBuf;
use std::net::SocketAddr;
use log::info;
use sp_runtime::traits::BlakeTwo256;
use service::{IsKusama, Block, self, RuntimeApiCollection, TFullClient};
use sc_service::{
config::{
DatabaseConfig, ExecutionStrategies, NodeKeyConfig, WasmExecutionMethod, PrometheusConfig, TelemetryEndpoints,
}, PruningMode, Roles, TracingReceiver, TransactionPoolOptions,
};
use sp_api::ConstructRuntimeApi;
use sc_cli::{spec_factory, SubstrateCLI};
use sc_cli::{spec_factory, SubstrateCLI, CliConfiguration, Result, substrate_cli_params};
use sc_executor::NativeExecutionDispatch;
use crate::cli::{Cli, Subcommand};
use crate::ChainSpec;
use crate::cli::{Cli, Subcommand, BaseSubcommand, RunCmd};
#[spec_factory(
impl_name = "parity-polkadot",
......@@ -29,16 +35,28 @@ use crate::ChainSpec;
copyright_start_year = 2017,
executable_name = "polkadot",
)]
fn spec_factory(id: &str) -> Result<Box<dyn sc_service::ChainSpec>, String> {
Ok(match ChainSpec::from(id) {
Some(spec) => spec.load()?,
//None if force_kusama => Box::new(service::KusamaChainSpec::from_json_file(std::path::PathBuf::from(id))?), // TODO
None => Box::new(service::PolkadotChainSpec::from_json_file(std::path::PathBuf::from(id))?),
fn spec_factory(_id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
unreachable!()
}
// TODO: merge this load_spec to spec_factory somehow
fn load_spec(s: &str, is_kusama: bool) -> std::result::Result<Box<dyn service::ChainSpec>, String> {
Ok(match s {
"polkadot-dev" | "dev" => Box::new(service::chain_spec::polkadot_development_config()),
"polkadot-local" => Box::new(service::chain_spec::polkadot_local_testnet_config()),
"polkadot-staging" => Box::new(service::chain_spec::polkadot_staging_testnet_config()),
"kusama-dev" => Box::new(service::chain_spec::kusama_development_config()),
"kusama-local" => Box::new(service::chain_spec::kusama_local_testnet_config()),
"kusama-staging" => Box::new(service::chain_spec::kusama_staging_testnet_config()),
"westend" => Box::new(service::chain_spec::westend_config()?),
"kusama" | "" => Box::new(service::chain_spec::kusama_config()?),
path if is_kusama => Box::new(service::KusamaChainSpec::from_json_file(std::path::PathBuf::from(path))?),
path => Box::new(service::PolkadotChainSpec::from_json_file(std::path::PathBuf::from(path))?),
})
}
/// Parses polkadot specific CLI arguments and run the service.
pub fn run() -> sc_cli::Result<()> {
pub fn run() -> Result<()> {
let opt = Cli::from_args();
// let force_kusama = opt.run.force_kusama; // TODO
......@@ -53,7 +71,7 @@ pub fn run() -> sc_cli::Result<()> {
match opt.subcommand {
None => {
let runtime = Cli::create_runtime(&opt.run.base)?;
let runtime = Cli::create_runtime(&opt.run)?;
let config = runtime.config();
let is_kusama = config.chain_spec.is_kusama();
......@@ -85,7 +103,7 @@ pub fn run() -> sc_cli::Result<()> {
let is_kusama = runtime.config().chain_spec.is_kusama();
if is_kusama {
runtime.run_subcommand(subcommand, |config|
runtime.run_subcommand(subcommand.subcommand, |config|
service::new_chain_ops::<
service::kusama_runtime::RuntimeApi,
service::KusamaExecutor,
......@@ -93,7 +111,7 @@ pub fn run() -> sc_cli::Result<()> {
>(config)
)
} else {
runtime.run_subcommand(subcommand, |config|
runtime.run_subcommand(subcommand.subcommand, |config|
service::new_chain_ops::<
service::polkadot_runtime::RuntimeApi,
service::PolkadotExecutor,
......@@ -181,3 +199,106 @@ type TLightClient<Runtime, Dispatch> = sc_client::Client<
Block,
Runtime
>;
// TODO: reduce boilerplate?
impl CliConfiguration for BaseSubcommand {
fn base_path(&self) -> sc_cli::Result<Option<&PathBuf>> { self.subcommand.base_path() }
fn is_dev(&self) -> Result<bool> { self.subcommand.is_dev() }
fn database_config(&self, base_path: &PathBuf, cache_size: Option<usize>) -> Result<DatabaseConfig> {
self.subcommand.database_config(base_path, cache_size)
}
// TODO: only relevant override
fn chain_spec<C: SubstrateCLI>(&self) -> Result<Box<dyn sc_service::ChainSpec>> {
let id = match self.subcommand.get_shared_params().chain {
Some(ref chain) => chain.clone(),
None => {
if self.subcommand.get_shared_params().dev {
"dev".into()
} else {
"".into()
}
}
};
Ok(load_spec(id.as_str(), self.force_kusama.force_kusama)?)
}
fn init<C: SubstrateCLI>(&self) -> Result<()> { self.subcommand.init::<C>() }
fn pruning(&self, is_dev: bool, roles: Roles) -> Result<PruningMode> { self.subcommand.pruning(is_dev, roles) }
fn tracing_receiver(&self) -> Result<TracingReceiver> { self.subcommand.tracing_receiver() }
fn tracing_targets(&self) -> Result<Option<String>> { self.subcommand.tracing_targets() }
fn state_cache_size(&self) -> Result<usize> { self.subcommand.state_cache_size() }
fn wasm_method(&self) -> Result<WasmExecutionMethod> { self.subcommand.wasm_method() }
fn execution_strategies(&self, is_dev: bool) -> Result<ExecutionStrategies> {
self.subcommand.execution_strategies(is_dev)
}
fn database_cache_size(&self) -> Result<Option<usize>> { self.subcommand.database_cache_size() }
fn node_key(&self, net_config_dir: &PathBuf) -> Result<NodeKeyConfig> { self.subcommand.node_key(net_config_dir) }
}
// TODO: reduce boilerplate?
#[substrate_cli_params(
shared_params = base.shared_params, import_params = base.import_params, network_params = base.network_config,
keystore_params = base.keystore_params,
)]
impl CliConfiguration for RunCmd {
// TODO: only relevant override
fn chain_spec<C: SubstrateCLI>(&self) -> Result<Box<dyn sc_service::ChainSpec>> {
let id = match self.base.shared_params.chain {
Some(ref chain) => chain.clone(),
None => {
if self.base.shared_params.dev {
"dev".into()
} else {
"".into()
}
}
};
Ok(load_spec(id.as_str(), self.force_kusama.force_kusama)?)
}
fn node_name(&self) -> Result<String> { self.base.node_name() }
fn dev_key_seed(&self, is_dev: bool) -> Result<Option<String>> { self.base.dev_key_seed(is_dev) }
fn telemetry_endpoints(
&self,
chain_spec: &Box<dyn sc_service::ChainSpec>,
) -> Result<Option<TelemetryEndpoints>> { self.base.telemetry_endpoints(chain_spec) }
fn sentry_mode(&self) -> Result<bool> { self.base.sentry_mode() }
fn roles(&self, is_dev: bool) -> Result<Roles> { self.base.roles(is_dev) }
fn force_authoring(&self) -> Result<bool> { self.base.force_authoring() }
fn prometheus_config(&self) -> Result<Option<PrometheusConfig>> { self.base.prometheus_config() }
fn disable_grandpa(&self) -> Result<bool> { self.base.disable_grandpa() }
fn rpc_ws_max_connections(&self) -> Result<Option<usize>> { self.base.rpc_ws_max_connections() }
fn rpc_cors(&self, is_dev: bool) -> Result<Option<Vec<String>>> { self.base.rpc_cors(is_dev) }
fn rpc_http(&self) -> Result<Option<SocketAddr>> { self.base.rpc_http() }
fn rpc_ws(&self) -> Result<Option<SocketAddr>> { self.base.rpc_ws() }
fn offchain_worker(&self, roles: Roles) -> Result<bool> { self.base.offchain_worker(roles) }
fn transaction_pool(&self) -> Result<TransactionPoolOptions> { self.base.transaction_pool() }
fn max_runtime_instances(&self) -> Result<Option<usize>> { self.base.max_runtime_instances() }
}
......@@ -19,7 +19,6 @@
#![warn(missing_docs)]
#![warn(unused_extern_crates)]
mod chain_spec;
#[cfg(feature = "browser")]
mod browser;
#[cfg(feature = "cli")]
......@@ -38,7 +37,5 @@ pub use cli::*;
#[cfg(feature = "cli")]
pub use command::*;
pub use chain_spec::*;
#[cfg(feature = "cli")]
pub use sc_cli::{Error, Result};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment