Skip to content

Commit

Permalink
Apply discussed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-tsurko committed Aug 20, 2024
1 parent fc2fbfc commit d60d110
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
52 changes: 23 additions & 29 deletions node/src/cli/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::service::FullClient;
pub struct ValidatorCmd {
#[allow(missing_docs)]
#[command(subcommand)]
subcommand: Option<ValidateSubcommands>,
subcommand: Option<ValidatorSubcommands>,

#[allow(missing_docs)]
#[clap(flatten)]
Expand All @@ -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),

Expand All @@ -51,7 +51,7 @@ pub enum ValidateSubcommands {
InsertKey(InsertKeyCmd),
}

impl ValidateSubcommands {
impl ValidatorSubcommands {
/// Runs the command.
pub fn run<Cli: SubstrateCli>(&self, cli: &Cli, client: &FullClient) -> Result<(), Error> {
match self {
Expand Down Expand Up @@ -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 {
Expand All @@ -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(())
Expand Down Expand Up @@ -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<Option<Vec<(String, String)>>, Error> {
fn decode_readable(keys: &str) -> Result<Vec<(String, String)>, Error> {
let bytes: Vec<u8> = 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 {
Expand All @@ -233,7 +227,7 @@ mod tests {

assert_eq!(
decode_readable(keys).unwrap(),
Some(vec![
vec![
(
"babe".to_string(),
"0xeafcb752d8b82fc872f3b4dcb6c55104b80de3b49796a1e61b9ef310eb5da42d"
Expand All @@ -249,7 +243,7 @@ mod tests {
"0x40b743a501c25bb8fa033dde00de6c73ebf8c62421d4d1bd67780e8098e8aa23"
.to_string()
),
])
]
)
}
}
4 changes: 2 additions & 2 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
},
Expand Down

0 comments on commit d60d110

Please sign in to comment.