mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 05:47:58 +00:00
ChainSpec trait (#5185)
* ChainSpec trait * Apply suggestions from code review Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Added docs * Fixed build * Fixed build Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
use structopt::StructOpt;
|
||||
use log::info;
|
||||
use sc_network::config::build_multiaddr;
|
||||
use sc_service::{Configuration, ChainSpecExtension, RuntimeGenesis, ChainSpec};
|
||||
use sc_service::{Configuration, ChainSpec};
|
||||
|
||||
use crate::error;
|
||||
use crate::VersionInfo;
|
||||
@@ -49,16 +49,12 @@ pub struct BuildSpecCmd {
|
||||
|
||||
impl BuildSpecCmd {
|
||||
/// Run the build-spec command
|
||||
pub fn run<G, E>(
|
||||
pub fn run(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
{
|
||||
config: Configuration,
|
||||
) -> error::Result<()> {
|
||||
info!("Building chain spec");
|
||||
let mut spec = config.expect_chain_spec().clone();
|
||||
let mut spec = config.chain_spec.expect("`chain_spec` is set to `Some` in `update_config`");
|
||||
let raw_output = self.raw;
|
||||
|
||||
if spec.boot_nodes().is_empty() && !self.disable_default_bootnode {
|
||||
@@ -72,7 +68,7 @@ impl BuildSpecCmd {
|
||||
spec.add_boot_node(addr)
|
||||
}
|
||||
|
||||
let json = sc_service::chain_ops::build_spec(spec, raw_output)?;
|
||||
let json = sc_service::chain_ops::build_spec(&*spec, raw_output)?;
|
||||
|
||||
print!("{}", json);
|
||||
|
||||
@@ -80,15 +76,13 @@ impl BuildSpecCmd {
|
||||
}
|
||||
|
||||
/// Update and prepare a `Configuration` with command line parameters
|
||||
pub fn update_config<G, E, F>(
|
||||
pub fn update_config<F>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
mut config: &mut Configuration,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()> where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
F: FnOnce(&str) -> Result<Box<dyn ChainSpec>, String>,
|
||||
{
|
||||
self.shared_params.update_config(&mut config, spec_factory, version)?;
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ use std::fmt::Debug;
|
||||
use std::str::FromStr;
|
||||
use structopt::StructOpt;
|
||||
use sc_service::{
|
||||
Configuration, ChainSpecExtension, RuntimeGenesis, ServiceBuilderCommand, Roles, ChainSpec,
|
||||
Configuration, ServiceBuilderCommand, Roles, ChainSpec,
|
||||
};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
|
||||
use sp_runtime::generic::BlockId;
|
||||
@@ -53,15 +53,13 @@ pub struct CheckBlockCmd {
|
||||
|
||||
impl CheckBlockCmd {
|
||||
/// Run the check-block command
|
||||
pub fn run<G, E, B, BC, BB>(
|
||||
pub fn run<B, BC, BB>(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
config: Configuration,
|
||||
builder: B,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
B: FnOnce(Configuration<G, E>) -> Result<BC, sc_service::error::Error>,
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
|
||||
BC: ServiceBuilderCommand<Block = BB> + Unpin,
|
||||
BB: sp_runtime::traits::Block + Debug,
|
||||
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
|
||||
@@ -86,15 +84,13 @@ impl CheckBlockCmd {
|
||||
}
|
||||
|
||||
/// Update and prepare a `Configuration` with command line parameters
|
||||
pub fn update_config<G, E, F>(
|
||||
pub fn update_config<F>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
mut config: &mut Configuration,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()> where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
F: FnOnce(&str) -> Result<Box<dyn ChainSpec>, String>,
|
||||
{
|
||||
self.shared_params.update_config(&mut config, spec_factory, version)?;
|
||||
self.import_params.update_config(&mut config, Roles::FULL, self.shared_params.dev)?;
|
||||
|
||||
@@ -21,7 +21,7 @@ use std::fmt::Debug;
|
||||
use log::info;
|
||||
use structopt::StructOpt;
|
||||
use sc_service::{
|
||||
Configuration, ChainSpecExtension, RuntimeGenesis, ServiceBuilderCommand, ChainSpec,
|
||||
Configuration, ServiceBuilderCommand, ChainSpec,
|
||||
config::DatabaseConfig, Roles,
|
||||
};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
|
||||
@@ -65,15 +65,13 @@ pub struct ExportBlocksCmd {
|
||||
|
||||
impl ExportBlocksCmd {
|
||||
/// Run the export-blocks command
|
||||
pub fn run<G, E, B, BC, BB>(
|
||||
pub fn run<B, BC, BB>(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
config: Configuration,
|
||||
builder: B,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
B: FnOnce(Configuration<G, E>) -> Result<BC, sc_service::error::Error>,
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
|
||||
BC: ServiceBuilderCommand<Block = BB> + Unpin,
|
||||
BB: sp_runtime::traits::Block + Debug,
|
||||
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
|
||||
@@ -98,15 +96,13 @@ impl ExportBlocksCmd {
|
||||
}
|
||||
|
||||
/// Update and prepare a `Configuration` with command line parameters
|
||||
pub fn update_config<G, E, F>(
|
||||
pub fn update_config<F>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
mut config: &mut Configuration,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()> where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
F: FnOnce(&str) -> Result<Box<dyn ChainSpec>, String>,
|
||||
{
|
||||
self.shared_params.update_config(&mut config, spec_factory, version)?;
|
||||
self.pruning_params.update_config(&mut config, Roles::FULL, true)?;
|
||||
|
||||
@@ -20,7 +20,7 @@ use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
use sc_service::{
|
||||
Configuration, ChainSpecExtension, RuntimeGenesis, ServiceBuilderCommand, ChainSpec, Roles,
|
||||
Configuration, ServiceBuilderCommand, ChainSpec, Roles,
|
||||
};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
|
||||
|
||||
@@ -59,15 +59,13 @@ impl<T: Read + Seek> ReadPlusSeek for T {}
|
||||
|
||||
impl ImportBlocksCmd {
|
||||
/// Run the import-blocks command
|
||||
pub fn run<G, E, B, BC, BB>(
|
||||
pub fn run<B, BC, BB>(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
config: Configuration,
|
||||
builder: B,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
B: FnOnce(Configuration<G, E>) -> Result<BC, sc_service::error::Error>,
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
|
||||
BC: ServiceBuilderCommand<Block = BB> + Unpin,
|
||||
BB: sp_runtime::traits::Block + Debug,
|
||||
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
|
||||
@@ -88,15 +86,13 @@ impl ImportBlocksCmd {
|
||||
}
|
||||
|
||||
/// Update and prepare a `Configuration` with command line parameters
|
||||
pub fn update_config<G, E, F>(
|
||||
pub fn update_config<F>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
mut config: &mut Configuration,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()> where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
F: FnOnce(&str) -> Result<Box<dyn ChainSpec>, String>,
|
||||
{
|
||||
self.shared_params.update_config(&mut config, spec_factory, version)?;
|
||||
self.import_params.update_config(&mut config, Roles::FULL, self.shared_params.dev)?;
|
||||
|
||||
@@ -25,9 +25,7 @@ mod purge_chain_cmd;
|
||||
use std::fmt::Debug;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use sc_service::{
|
||||
Configuration, ChainSpecExtension, RuntimeGenesis, ServiceBuilderCommand, ChainSpec,
|
||||
};
|
||||
use sc_service::{ Configuration, ServiceBuilderCommand, ChainSpec };
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
|
||||
|
||||
use crate::error;
|
||||
@@ -87,15 +85,13 @@ impl Subcommand {
|
||||
}
|
||||
|
||||
/// Run any `CoreParams` command
|
||||
pub fn run<G, E, B, BC, BB>(
|
||||
pub fn run<B, BC, BB>(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
config: Configuration,
|
||||
builder: B,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
B: FnOnce(Configuration<G, E>) -> Result<BC, sc_service::error::Error>,
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
|
||||
BC: ServiceBuilderCommand<Block = BB> + Unpin,
|
||||
BB: sp_runtime::traits::Block + Debug,
|
||||
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
|
||||
@@ -112,15 +108,13 @@ impl Subcommand {
|
||||
}
|
||||
|
||||
/// Update and prepare a `Configuration` with command line parameters
|
||||
pub fn update_config<G, E, F>(
|
||||
pub fn update_config<F>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
mut config: &mut Configuration,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()> where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
F: FnOnce(&str) -> Result<Box<dyn ChainSpec>, String>,
|
||||
{
|
||||
match self {
|
||||
Subcommand::BuildSpec(cmd) => cmd.update_config(&mut config, spec_factory, version),
|
||||
|
||||
@@ -18,10 +18,7 @@ use std::fmt::Debug;
|
||||
use std::io::{Write, self};
|
||||
use std::fs;
|
||||
use structopt::StructOpt;
|
||||
use sc_service::{
|
||||
Configuration, ChainSpecExtension, RuntimeGenesis, ChainSpec,
|
||||
config::{DatabaseConfig},
|
||||
};
|
||||
use sc_service::{ Configuration, ChainSpec, config::{DatabaseConfig} };
|
||||
|
||||
use crate::error;
|
||||
use crate::VersionInfo;
|
||||
@@ -41,14 +38,10 @@ pub struct PurgeChainCmd {
|
||||
|
||||
impl PurgeChainCmd {
|
||||
/// Run the purge command
|
||||
pub fn run<G, E>(
|
||||
pub fn run(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
{
|
||||
config: Configuration,
|
||||
) -> error::Result<()> {
|
||||
let db_path = match config.expect_database() {
|
||||
DatabaseConfig::Path { path, .. } => path,
|
||||
_ => {
|
||||
@@ -88,15 +81,13 @@ impl PurgeChainCmd {
|
||||
}
|
||||
|
||||
/// Update and prepare a `Configuration` with command line parameters
|
||||
pub fn update_config<G, E, F>(
|
||||
pub fn update_config<F>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
mut config: &mut Configuration,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()> where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
F: FnOnce(&str) -> Result<Box<dyn ChainSpec>, String>,
|
||||
{
|
||||
self.shared_params.update_config(&mut config, spec_factory, version)?;
|
||||
config.use_in_memory_keystore()?;
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
use std::fmt::Debug;
|
||||
use structopt::StructOpt;
|
||||
use sc_service::{
|
||||
Configuration, ChainSpecExtension, RuntimeGenesis, ServiceBuilderCommand, ChainSpec, Roles,
|
||||
Configuration, ServiceBuilderCommand, ChainSpec, Roles,
|
||||
};
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
|
||||
|
||||
@@ -43,15 +43,13 @@ pub struct RevertCmd {
|
||||
|
||||
impl RevertCmd {
|
||||
/// Run the revert command
|
||||
pub fn run<G, E, B, BC, BB>(
|
||||
pub fn run<B, BC, BB>(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
config: Configuration,
|
||||
builder: B,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
B: FnOnce(Configuration<G, E>) -> Result<BC, sc_service::error::Error>,
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
B: FnOnce(Configuration) -> Result<BC, sc_service::error::Error>,
|
||||
BC: ServiceBuilderCommand<Block = BB> + Unpin,
|
||||
BB: sp_runtime::traits::Block + Debug,
|
||||
<<<BB as BlockT>::Header as HeaderT>::Number as std::str::FromStr>::Err: std::fmt::Debug,
|
||||
@@ -64,15 +62,13 @@ impl RevertCmd {
|
||||
}
|
||||
|
||||
/// Update and prepare a `Configuration` with command line parameters
|
||||
pub fn update_config<G, E, F>(
|
||||
pub fn update_config<F>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
mut config: &mut Configuration,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()> where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
F: FnOnce(&str) -> Result<Box<dyn ChainSpec>, String>,
|
||||
{
|
||||
self.shared_params.update_config(&mut config, spec_factory, version)?;
|
||||
self.pruning_params.update_config(&mut config, Roles::FULL, true)?;
|
||||
|
||||
@@ -23,7 +23,7 @@ use names::{Generator, Name};
|
||||
use regex::Regex;
|
||||
use chrono::prelude::*;
|
||||
use sc_service::{
|
||||
AbstractService, Configuration, ChainSpecExtension, RuntimeGenesis, ChainSpec, Roles,
|
||||
AbstractService, Configuration, ChainSpec, Roles,
|
||||
config::{KeystoreConfig, PrometheusConfig},
|
||||
};
|
||||
use sc_telemetry::TelemetryEndpoints;
|
||||
@@ -291,16 +291,14 @@ impl RunCmd {
|
||||
}
|
||||
|
||||
/// Update and prepare a `Configuration` with command line parameters of `RunCmd` and `VersionInfo`
|
||||
pub fn update_config<G, E, F>(
|
||||
pub fn update_config<F>(
|
||||
&self,
|
||||
mut config: &mut Configuration<G, E>,
|
||||
mut config: &mut Configuration,
|
||||
spec_factory: F,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
F: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
|
||||
F: FnOnce(&str) -> Result<Box<dyn ChainSpec>, String>,
|
||||
{
|
||||
self.shared_params.update_config(&mut config, spec_factory, version)?;
|
||||
|
||||
@@ -447,18 +445,16 @@ impl RunCmd {
|
||||
}
|
||||
|
||||
/// Run the command that runs the node
|
||||
pub fn run<G, E, FNL, FNF, SL, SF>(
|
||||
pub fn run<FNL, FNF, SL, SF>(
|
||||
self,
|
||||
config: Configuration<G, E>,
|
||||
config: Configuration,
|
||||
new_light: FNL,
|
||||
new_full: FNF,
|
||||
version: &VersionInfo,
|
||||
) -> error::Result<()>
|
||||
where
|
||||
G: RuntimeGenesis,
|
||||
E: ChainSpecExtension,
|
||||
FNL: FnOnce(Configuration<G, E>) -> Result<SL, sc_service::error::Error>,
|
||||
FNF: FnOnce(Configuration<G, E>) -> Result<SF, sc_service::error::Error>,
|
||||
FNL: FnOnce(Configuration) -> Result<SL, sc_service::error::Error>,
|
||||
FNF: FnOnce(Configuration) -> Result<SF, sc_service::error::Error>,
|
||||
SL: AbstractService + Unpin,
|
||||
SF: AbstractService + Unpin,
|
||||
{
|
||||
@@ -625,7 +621,7 @@ fn parse_cors(s: &str) -> Result<Cors, Box<dyn std::error::Error>> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sc_service::config::DatabaseConfig;
|
||||
use sc_service::{GenericChainSpec, config::DatabaseConfig};
|
||||
|
||||
const TEST_VERSION_INFO: &'static VersionInfo = &VersionInfo {
|
||||
name: "node-test",
|
||||
@@ -655,7 +651,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn keystore_path_is_generated_correctly() {
|
||||
let chain_spec = ChainSpec::from_genesis(
|
||||
let chain_spec = GenericChainSpec::from_genesis(
|
||||
"test",
|
||||
"test-id",
|
||||
|| (),
|
||||
@@ -673,9 +669,9 @@ mod tests {
|
||||
|
||||
let mut config = Configuration::default();
|
||||
config.config_dir = Some(PathBuf::from("/test/path"));
|
||||
config.chain_spec = Some(chain_spec.clone());
|
||||
config.chain_spec = Some(Box::new(chain_spec.clone()));
|
||||
let chain_spec = chain_spec.clone();
|
||||
cli.update_config(&mut config, move |_| Ok(Some(chain_spec)), TEST_VERSION_INFO).unwrap();
|
||||
cli.update_config(&mut config, move |_| Ok(Box::new(chain_spec)), TEST_VERSION_INFO).unwrap();
|
||||
|
||||
let expected_path = match keystore_path {
|
||||
Some(path) => PathBuf::from(path),
|
||||
@@ -688,7 +684,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn ensure_load_spec_provide_defaults() {
|
||||
let chain_spec = ChainSpec::from_genesis(
|
||||
let chain_spec = GenericChainSpec::from_genesis(
|
||||
"test",
|
||||
"test-id",
|
||||
|| (),
|
||||
@@ -703,7 +699,7 @@ mod tests {
|
||||
let cli = RunCmd::from_iter(args);
|
||||
|
||||
let mut config = Configuration::from_version(TEST_VERSION_INFO);
|
||||
cli.update_config(&mut config, |_| Ok(Some(chain_spec)), TEST_VERSION_INFO).unwrap();
|
||||
cli.update_config(&mut config, |_| Ok(Box::new(chain_spec)), TEST_VERSION_INFO).unwrap();
|
||||
|
||||
assert!(config.chain_spec.is_some());
|
||||
assert!(!config.network.boot_nodes.is_empty());
|
||||
@@ -712,7 +708,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn ensure_update_config_for_running_node_provides_defaults() {
|
||||
let chain_spec = ChainSpec::from_genesis(
|
||||
let chain_spec = GenericChainSpec::from_genesis(
|
||||
"test",
|
||||
"test-id",
|
||||
|| (),
|
||||
@@ -728,7 +724,7 @@ mod tests {
|
||||
|
||||
let mut config = Configuration::from_version(TEST_VERSION_INFO);
|
||||
cli.init(&TEST_VERSION_INFO).unwrap();
|
||||
cli.update_config(&mut config, |_| Ok(Some(chain_spec)), TEST_VERSION_INFO).unwrap();
|
||||
cli.update_config(&mut config, |_| Ok(Box::new(chain_spec)), TEST_VERSION_INFO).unwrap();
|
||||
|
||||
assert!(config.config_dir.is_some());
|
||||
assert!(config.database.is_some());
|
||||
|
||||
Reference in New Issue
Block a user