From d60d1103d139400a186c84477c9daef59a4d7f75 Mon Sep 17 00:00:00 2001 From: Ales Tsurko Date: Tue, 20 Aug 2024 13:35:58 +0300 Subject: [PATCH] Apply discussed changes --- node/src/cli/validator.rs | 52 +++++++++++++++++---------------------- node/src/command.rs | 4 +-- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/node/src/cli/validator.rs b/node/src/cli/validator.rs index 1de1814..22dd990 100644 --- a/node/src/cli/validator.rs +++ b/node/src/cli/validator.rs @@ -17,7 +17,7 @@ use crate::service::FullClient; pub struct ValidatorCmd { #[allow(missing_docs)] #[command(subcommand)] - subcommand: Option, + subcommand: Option, #[allow(missing_docs)] #[clap(flatten)] @@ -40,7 +40,7 @@ impl CliConfiguration for ValidatorCmd { } #[derive(Debug, Subcommand)] -pub enum ValidateSubcommands { +pub enum ValidatorSubcommands { /// Generate session keys and insert them into the keystore. GenerateSessionKeys(GenerateSessionKeysCmd), @@ -51,7 +51,7 @@ pub enum ValidateSubcommands { InsertKey(InsertKeyCmd), } -impl ValidateSubcommands { +impl ValidatorSubcommands { /// Runs the command. pub fn run(&self, cli: &Cli, client: &FullClient) -> Result<(), Error> { match self { @@ -118,13 +118,13 @@ pub struct InsertKeyCmd { #[arg(long)] pub key_type: String, - /// SURI - #[arg(long, default_value_t)] + /// Secret URI + #[arg(long)] pub suri: String, /// Public key #[arg(long)] - pub public: Bytes, + pub public_key: Bytes, } impl InsertKeyCmd { @@ -133,7 +133,7 @@ impl InsertKeyCmd { let keystore = init_keystore(cli, &self.shared_params, &self.keystore_params)?; let key_type = self.key_type.as_str().try_into().map_err(|_| Error::KeyTypeInvalid)?; keystore - .insert(key_type, &self.suri, &self.public[..]) + .insert(key_type, &self.suri, &self.public_key[..]) .map_err(|_| Error::KeystoreOperation)?; Ok(()) @@ -187,34 +187,28 @@ pub struct DecodeSessionKeysCmd { impl DecodeSessionKeysCmd { /// Run the command pub fn run(&self) -> Result<(), Error> { - match decode_readable(&self.keys)? { - Some(decoded) => { - for key_line in decoded { - println!("{}: {}", key_line.0, key_line.1); - } - }, - None => eprintln!("Error decoding session keys"), + for key_line in decode_readable(&self.keys)? { + println!("{}: {}", key_line.0, key_line.1); } - Ok(()) } } -fn decode_readable(keys: &str) -> Result>, Error> { +fn decode_readable(keys: &str) -> Result, Error> { let bytes: Vec = sp_core::bytes::from_hex(keys) .map_err(|convert_err| Error::Application(Box::new(convert_err).into()))?; + let decoded = opaque::SessionKeys::decode_into_raw_public_keys(&bytes) + .ok_or(std::io::Error::new(std::io::ErrorKind::Other, "Error decoding session keys"))?; - Ok(opaque::SessionKeys::decode_into_raw_public_keys(&bytes).map(|decoded| { - decoded - .into_iter() - .map(|(value, key_id)| { - ( - String::from_utf8(key_id.0.to_vec()).expect("KeyTypeId string is valid"), - sp_core::bytes::to_hex(&value, true), - ) - }) - .collect() - })) + Ok(decoded + .into_iter() + .map(|(value, key_id)| { + ( + String::from_utf8(key_id.0.to_vec()).expect("KeyTypeId string is valid"), + sp_core::bytes::to_hex(&value, true), + ) + }) + .collect()) } impl CliConfiguration for DecodeSessionKeysCmd { @@ -233,7 +227,7 @@ mod tests { assert_eq!( decode_readable(keys).unwrap(), - Some(vec![ + vec![ ( "babe".to_string(), "0xeafcb752d8b82fc872f3b4dcb6c55104b80de3b49796a1e61b9ef310eb5da42d" @@ -249,7 +243,7 @@ mod tests { "0x40b743a501c25bb8fa033dde00de6c73ebf8c62421d4d1bd67780e8098e8aa23" .to_string() ), - ]) + ] ) } } diff --git a/node/src/command.rs b/node/src/command.rs index b8bc5dd..6733cee 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -260,9 +260,9 @@ pub fn run() -> sc_cli::Result<()> { }, Some(Subcommand::Validator(cmd)) => { let runner = cli.create_runner(cmd)?; - log::set_max_level(log::LevelFilter::Off); + log::set_max_level(log::LevelFilter::Error); runner.sync_run(|mut config| { - let (client, _, _, _, _) = service::new_chain_ops(&mut config, &cli.eth)?; + let (client, ..) = service::new_chain_ops(&mut config, &cli.eth)?; cmd.run(&cli, &client) }) },