From 058e98ef34d5fdba09922527eb0c395a23872c9e Mon Sep 17 00:00:00 2001
From: Nikolay Volf <nikvolf@gmail.com>
Date: Thu, 16 Jan 2020 12:08:05 +0300
Subject: [PATCH] use default in-memory for commands that don't use keystore
 (#4634)

---
 substrate/bin/node/cli/src/cli.rs |  1 -
 substrate/client/cli/src/lib.rs   | 21 ++++++++++++++++++---
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/substrate/bin/node/cli/src/cli.rs b/substrate/bin/node/cli/src/cli.rs
index 7a4321802cf..4442bb1b037 100644
--- a/substrate/bin/node/cli/src/cli.rs
+++ b/substrate/bin/node/cli/src/cli.rs
@@ -139,7 +139,6 @@ pub fn run<I, T, E>(args: I, exit: E, version: sc_cli::VersionInfo) -> error::Re
 				&cli_args.shared_params,
 				&version,
 			)?;
-
 			sc_cli::fill_import_params(&mut config, &cli_args.import_params, ServiceRoles::FULL)?;
 
 			match ChainSpec::from(config.chain_spec.id()) {
diff --git a/substrate/client/cli/src/lib.rs b/substrate/client/cli/src/lib.rs
index fe47eb84979..1e8d40cb0f8 100644
--- a/substrate/client/cli/src/lib.rs
+++ b/substrate/client/cli/src/lib.rs
@@ -401,7 +401,8 @@ impl<'a> ParseAndPrepareExport<'a> {
 		E: ChainSpecExtension,
 		Exit: IntoExit
 	{
-		let 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)?;
+		fill_config_keystore_in_memory(&mut config)?;
 
 		if let DatabaseConfig::Path { ref path, .. } = &config.database {
 			info!("DB path: {}", path.display());
@@ -523,6 +524,7 @@ impl<'a> CheckBlock<'a> {
 	{
 		let mut config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?;
 		fill_import_params(&mut config, &self.params.import_params, sc_service::Roles::FULL)?;
+		fill_config_keystore_in_memory(&mut config)?;
 
 		let input = if self.params.input.starts_with("0x") { &self.params.input[2..] } else { &self.params.input[..] };
 		let block_id = match FromStr::from_str(input) {
@@ -559,9 +561,10 @@ impl<'a> ParseAndPreparePurge<'a> {
 		G: RuntimeGenesis,
 		E: ChainSpecExtension,
 	{
-		let config = create_config_with_db_path::<(), _, _, _>(
+		let mut config = create_config_with_db_path::<(), _, _, _>(
 			spec_factory, &self.params.shared_params, self.version
 		)?;
+		fill_config_keystore_in_memory(&mut config)?;
 		let db_path = match config.database {
 			DatabaseConfig::Path { path, .. } => path,
 			_ => {
@@ -623,9 +626,11 @@ impl<'a> ParseAndPrepareRevert<'a> {
 		G: RuntimeGenesis,
 		E: ChainSpecExtension,
 	{
-		let config = create_config_with_db_path(
+		let mut config = create_config_with_db_path(
 			spec_factory, &self.params.shared_params, self.version
 		)?;
+		fill_config_keystore_in_memory(&mut config)?;
+
 		let blocks = self.params.num.parse()?;
 		builder(config)?.revert_chain(blocks)?;
 		Ok(())
@@ -749,6 +754,16 @@ fn input_keystore_password() -> Result<String, String> {
 		.map_err(|e| format!("{:?}", e))
 }
 
+/// Use in memory keystore config when it is not required at all.
+fn fill_config_keystore_in_memory<C, G, E>(config: &mut sc_service::Configuration<C, G, E>)
+	-> Result<(), String>
+{
+	match &mut config.keystore {
+		cfg @ KeystoreConfig::None => { *cfg = KeystoreConfig::InMemory; Ok(()) },
+		_ => Err("Keystore config specified when it should not be!".into()),
+	}
+}
+
 /// Fill the password field of the given config instance.
 fn fill_config_keystore_password_and_path<C, G, E>(
 	config: &mut sc_service::Configuration<C, G, E>,
-- 
GitLab