Skip to content
Snippets Groups Projects
Commit 60feeb7a authored by Cecile Tonglet's avatar Cecile Tonglet Committed by Gavin Wood
Browse files

Getting configuration from commands (#4643)

* Expose a method that allows converting RunCmd to Configuration

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP
parent 058e98ef
No related merge requests found
...@@ -138,6 +138,7 @@ pub fn run<I, T, E>(args: I, exit: E, version: sc_cli::VersionInfo) -> error::Re ...@@ -138,6 +138,7 @@ pub fn run<I, T, E>(args: I, exit: E, version: sc_cli::VersionInfo) -> error::Re
load_spec, load_spec,
&cli_args.shared_params, &cli_args.shared_params,
&version, &version,
None,
)?; )?;
sc_cli::fill_import_params(&mut config, &cli_args.import_params, ServiceRoles::FULL)?; sc_cli::fill_import_params(&mut config, &cli_args.import_params, ServiceRoles::FULL)?;
......
...@@ -142,8 +142,13 @@ pub fn load_spec<F, G, E>(cli: &SharedParams, factory: F) -> error::Result<Chain ...@@ -142,8 +142,13 @@ pub fn load_spec<F, G, E>(cli: &SharedParams, factory: F) -> error::Result<Chain
Ok(spec) Ok(spec)
} }
fn base_path(cli: &SharedParams, version: &VersionInfo) -> PathBuf { fn base_path(
cli: &SharedParams,
version: &VersionInfo,
default_base_path: Option<PathBuf>,
) -> PathBuf {
cli.base_path.clone() cli.base_path.clone()
.or(default_base_path)
.unwrap_or_else(|| .unwrap_or_else(||
app_dirs::get_app_root( app_dirs::get_app_root(
AppDataType::UserData, AppDataType::UserData,
...@@ -295,6 +300,78 @@ impl<'a, CC, RP> ParseAndPrepare<'a, CC, RP> where CC: GetSharedParams { ...@@ -295,6 +300,78 @@ impl<'a, CC, RP> ParseAndPrepare<'a, CC, RP> where CC: GetSharedParams {
} }
} }
impl<'a, CC, RP> ParseAndPrepare<'a, CC, RP> {
/// Convert ParseAndPrepare to Configuration
pub fn into_configuration<C, G, E, S>(
self,
spec_factory: S,
default_base_path: Option<PathBuf>,
) -> error::Result<Option<Configuration<C, G, E>>>
where
C: Default,
G: RuntimeGenesis,
E: ChainSpecExtension,
S: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
{
match self {
ParseAndPrepare::Run(c) =>
Some(create_run_node_config(
c.params.left,
spec_factory,
c.impl_name,
c.version,
default_base_path,
)).transpose(),
ParseAndPrepare::BuildSpec(c) => {
let spec = load_spec(&c.params.shared_params, spec_factory)?;
Some(create_build_spec_config(
&spec,
&c.params.shared_params,
c.version,
default_base_path,
)).transpose()
},
ParseAndPrepare::ExportBlocks(c) =>
Some(create_config_with_db_path(
spec_factory,
&c.params.shared_params,
c.version,
default_base_path,
)).transpose(),
ParseAndPrepare::ImportBlocks(c) =>
Some(create_config_with_db_path(
spec_factory,
&c.params.shared_params,
c.version,
default_base_path,
)).transpose(),
ParseAndPrepare::CheckBlock(c) =>
Some(create_config_with_db_path(
spec_factory,
&c.params.shared_params,
c.version,
default_base_path,
)).transpose(),
ParseAndPrepare::PurgeChain(c) =>
Some(create_config_with_db_path(
spec_factory,
&c.params.shared_params,
c.version,
default_base_path,
)).transpose(),
ParseAndPrepare::RevertChain(c) =>
Some(create_config_with_db_path(
spec_factory,
&c.params.shared_params,
c.version,
default_base_path,
)).transpose(),
ParseAndPrepare::CustomCommand(_) => Ok(None),
}
}
}
/// Command ready to run the main client. /// Command ready to run the main client.
pub struct ParseAndPrepareRun<'a, RP> { pub struct ParseAndPrepareRun<'a, RP> {
params: MergeParameters<RunCmd, RP>, params: MergeParameters<RunCmd, RP>,
...@@ -321,7 +398,11 @@ impl<'a, RP> ParseAndPrepareRun<'a, RP> { ...@@ -321,7 +398,11 @@ impl<'a, RP> ParseAndPrepareRun<'a, RP> {
RS: FnOnce(Exit, RunCmd, RP, Configuration<C, G, CE>) -> Result<(), E> RS: FnOnce(Exit, RunCmd, RP, Configuration<C, G, CE>) -> Result<(), E>
{ {
let config = create_run_node_config( let config = create_run_node_config(
self.params.left.clone(), spec_factory, self.impl_name, self.version, self.params.left.clone(),
spec_factory,
self.impl_name,
self.version,
None,
)?; )?;
run_service(exit, self.params.left, self.params.right, config).map_err(Into::into) run_service(exit, self.params.left, self.params.right, config).map_err(Into::into)
...@@ -350,11 +431,12 @@ impl<'a> ParseAndPrepareBuildSpec<'a> { ...@@ -350,11 +431,12 @@ impl<'a> ParseAndPrepareBuildSpec<'a> {
let mut spec = load_spec(&self.params.shared_params, spec_factory)?; let mut spec = load_spec(&self.params.shared_params, spec_factory)?;
if spec.boot_nodes().is_empty() && !self.params.disable_default_bootnode { if spec.boot_nodes().is_empty() && !self.params.disable_default_bootnode {
let base_path = base_path(&self.params.shared_params, self.version); let cfg = create_build_spec_config::<C, _, _>(
let cfg = sc_service::Configuration::<C,_,_>::default_with_spec_and_base_path( &spec,
spec.clone(), &self.params.shared_params,
Some(base_path), self.version,
); None,
)?;
let node_key = node_key_config( let node_key = node_key_config(
self.params.node_key_params, self.params.node_key_params,
&Some(cfg.in_chain_config_dir(DEFAULT_NETWORK_CONFIG_PATH).expect("We provided a base_path")) &Some(cfg.in_chain_config_dir(DEFAULT_NETWORK_CONFIG_PATH).expect("We provided a base_path"))
...@@ -401,7 +483,12 @@ impl<'a> ParseAndPrepareExport<'a> { ...@@ -401,7 +483,12 @@ impl<'a> ParseAndPrepareExport<'a> {
E: ChainSpecExtension, E: ChainSpecExtension,
Exit: IntoExit Exit: IntoExit
{ {
let mut config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?; let mut config = create_config_with_db_path(
spec_factory,
&self.params.shared_params,
self.version,
None,
)?;
fill_config_keystore_in_memory(&mut config)?; fill_config_keystore_in_memory(&mut config)?;
if let DatabaseConfig::Path { ref path, .. } = &config.database { if let DatabaseConfig::Path { ref path, .. } = &config.database {
...@@ -463,7 +550,12 @@ impl<'a> ParseAndPrepareImport<'a> { ...@@ -463,7 +550,12 @@ impl<'a> ParseAndPrepareImport<'a> {
E: ChainSpecExtension, E: ChainSpecExtension,
Exit: IntoExit Exit: IntoExit
{ {
let mut config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?; let mut config = create_config_with_db_path(
spec_factory,
&self.params.shared_params,
self.version,
None,
)?;
fill_import_params(&mut config, &self.params.import_params, sc_service::Roles::FULL)?; fill_import_params(&mut config, &self.params.import_params, sc_service::Roles::FULL)?;
let file: Box<dyn ReadPlusSeek + Send> = match self.params.input { let file: Box<dyn ReadPlusSeek + Send> = match self.params.input {
...@@ -522,7 +614,12 @@ impl<'a> CheckBlock<'a> { ...@@ -522,7 +614,12 @@ impl<'a> CheckBlock<'a> {
E: ChainSpecExtension, E: ChainSpecExtension,
Exit: IntoExit Exit: IntoExit
{ {
let mut config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?; let mut config = create_config_with_db_path(
spec_factory,
&self.params.shared_params,
self.version,
None,
)?;
fill_import_params(&mut config, &self.params.import_params, sc_service::Roles::FULL)?; fill_import_params(&mut config, &self.params.import_params, sc_service::Roles::FULL)?;
fill_config_keystore_in_memory(&mut config)?; fill_config_keystore_in_memory(&mut config)?;
...@@ -562,7 +659,10 @@ impl<'a> ParseAndPreparePurge<'a> { ...@@ -562,7 +659,10 @@ impl<'a> ParseAndPreparePurge<'a> {
E: ChainSpecExtension, E: ChainSpecExtension,
{ {
let mut config = create_config_with_db_path::<(), _, _, _>( let mut config = create_config_with_db_path::<(), _, _, _>(
spec_factory, &self.params.shared_params, self.version spec_factory,
&self.params.shared_params,
self.version,
None,
)?; )?;
fill_config_keystore_in_memory(&mut config)?; fill_config_keystore_in_memory(&mut config)?;
let db_path = match config.database { let db_path = match config.database {
...@@ -627,7 +727,10 @@ impl<'a> ParseAndPrepareRevert<'a> { ...@@ -627,7 +727,10 @@ impl<'a> ParseAndPrepareRevert<'a> {
E: ChainSpecExtension, E: ChainSpecExtension,
{ {
let mut config = create_config_with_db_path( let mut config = create_config_with_db_path(
spec_factory, &self.params.shared_params, self.version spec_factory,
&self.params.shared_params,
self.version,
None,
)?; )?;
fill_config_keystore_in_memory(&mut config)?; fill_config_keystore_in_memory(&mut config)?;
...@@ -852,7 +955,11 @@ pub fn fill_import_params<C, G, E>( ...@@ -852,7 +955,11 @@ pub fn fill_import_params<C, G, E>(
} }
fn create_run_node_config<C, G, E, S>( fn create_run_node_config<C, G, E, S>(
cli: RunCmd, spec_factory: S, impl_name: &'static str, version: &VersionInfo, cli: RunCmd,
spec_factory: S,
impl_name: &'static str,
version: &VersionInfo,
default_base_path: Option<PathBuf>,
) -> error::Result<Configuration<C, G, E>> ) -> error::Result<Configuration<C, G, E>>
where where
C: Default, C: Default,
...@@ -860,7 +967,12 @@ where ...@@ -860,7 +967,12 @@ where
E: ChainSpecExtension, E: ChainSpecExtension,
S: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>, S: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
{ {
let mut config = create_config_with_db_path(spec_factory, &cli.shared_params, &version)?; let mut config = create_config_with_db_path(
spec_factory,
&cli.shared_params,
&version,
default_base_path,
)?;
fill_config_keystore_password_and_path(&mut config, &cli)?; fill_config_keystore_password_and_path(&mut config, &cli)?;
...@@ -994,7 +1106,10 @@ fn interface_str( ...@@ -994,7 +1106,10 @@ fn interface_str(
/// Creates a configuration including the database path. /// Creates a configuration including the database path.
pub fn create_config_with_db_path<C, G, E, S>( pub fn create_config_with_db_path<C, G, E, S>(
spec_factory: S, cli: &SharedParams, version: &VersionInfo, spec_factory: S,
cli: &SharedParams,
version: &VersionInfo,
default_base_path: Option<PathBuf>,
) -> error::Result<Configuration<C, G, E>> ) -> error::Result<Configuration<C, G, E>>
where where
C: Default, C: Default,
...@@ -1003,7 +1118,7 @@ where ...@@ -1003,7 +1118,7 @@ where
S: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>, S: FnOnce(&str) -> Result<Option<ChainSpec<G, E>>, String>,
{ {
let spec = load_spec(cli, spec_factory)?; let spec = load_spec(cli, spec_factory)?;
let base_path = base_path(cli, version); let base_path = base_path(cli, version, default_base_path);
let mut config = sc_service::Configuration::default_with_spec_and_base_path( let mut config = sc_service::Configuration::default_with_spec_and_base_path(
spec.clone(), spec.clone(),
...@@ -1018,6 +1133,27 @@ where ...@@ -1018,6 +1133,27 @@ where
Ok(config) Ok(config)
} }
/// Creates a configuration including the base path and the shared params
fn create_build_spec_config<C, G, E>(
spec: &ChainSpec<G, E>,
cli: &SharedParams,
version: &VersionInfo,
default_base_path: Option<PathBuf>,
) -> error::Result<Configuration<C, G, E>>
where
C: Default,
G: RuntimeGenesis,
E: ChainSpecExtension,
{
let base_path = base_path(&cli, version, default_base_path);
let cfg = sc_service::Configuration::<C,_,_>::default_with_spec_and_base_path(
spec.clone(),
Some(base_path),
);
Ok(cfg)
}
/// Internal trait used to cast to a dynamic type that implements Read and Seek. /// Internal trait used to cast to a dynamic type that implements Read and Seek.
trait ReadPlusSeek: Read + Seek {} trait ReadPlusSeek: Read + Seek {}
...@@ -1255,6 +1391,7 @@ mod tests { ...@@ -1255,6 +1391,7 @@ mod tests {
|_| Ok(Some(chain_spec.clone())), |_| Ok(Some(chain_spec.clone())),
"test", "test",
&version_info, &version_info,
None,
).unwrap(); ).unwrap();
let expected_path = match keystore_path { let expected_path = match keystore_path {
...@@ -1265,4 +1402,44 @@ mod tests { ...@@ -1265,4 +1402,44 @@ mod tests {
assert_eq!(expected_path, node_config.keystore.path().unwrap().to_owned()); assert_eq!(expected_path, node_config.keystore.path().unwrap().to_owned());
} }
} }
#[test]
fn parse_and_prepare_into_configuration() {
let chain_spec = ChainSpec::from_genesis(
"test",
"test-id",
|| (),
Vec::new(),
None,
None,
None,
None,
);
let version = VersionInfo {
name: "test",
version: "42",
commit: "234234",
executable_name: "test",
description: "cool test",
author: "universe",
support_url: "com",
};
let spec_factory = |_: &str| Ok(Some(chain_spec.clone()));
let args = vec!["substrate", "--dev", "--state-cache-size=42"];
let pnp = parse_and_prepare::<NoCustom, NoCustom, _>(&version, "test", args);
let config = pnp.into_configuration::<(), _, _, _>(spec_factory, None).unwrap().unwrap();
assert_eq!(config.roles, sc_service::Roles::AUTHORITY);
assert_eq!(config.state_cache_size, 42);
let args = vec!["substrate", "import-blocks", "--dev"];
let pnp = parse_and_prepare::<NoCustom, NoCustom, _>(&version, "test", args);
let config = pnp.into_configuration::<(), _, _, _>(spec_factory, None).unwrap().unwrap();
assert_eq!(config.roles, sc_service::Roles::FULL);
let args = vec!["substrate", "--base-path=/foo"];
let pnp = parse_and_prepare::<NoCustom, NoCustom, _>(&version, "test", args);
let config = pnp.into_configuration::<(), _, _, _>(spec_factory, Some("/bar".into())).unwrap().unwrap();
assert_eq!(config.config_dir, Some("/foo".into()));
}
} }
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