Skip to content

Commit

Permalink
add ConfigParams deserialization from string
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Oct 26, 2021
1 parent 1c86ed4 commit a53d8c1
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions jcli/src/jcli_lib/vote/update_proposal.rs
Expand Up @@ -3,7 +3,10 @@ use crate::{
rest::{v0::message::post_fragment, RestArgs},
utils::key_parser::read_secret_key,
};
use chain_core::property::Serialize;
use chain_core::{
mempack::ReadError,
property::{Deserialize, Serialize},
};
use chain_impl_mockchain::{
fragment::{ConfigParams, Fragment},
key::{BftLeaderId, EitherEd25519SecretKey},
Expand All @@ -18,7 +21,7 @@ use structopt::StructOpt;
#[structopt(rename_all = "kebab-case")]
pub struct UpdateProposal {
/// the config update
#[structopt(name = "CONFIG_UPDATE")]
#[structopt(name = "CONFIG_UPDATE", parse(try_from_str = parse_hex))]
config: ConfigParams,

/// the file path to the file to read the signing key from.
Expand All @@ -30,11 +33,18 @@ pub struct UpdateProposal {
rest_args: RestArgs,
}

fn parse_hex(str: &str) -> Result<ConfigParams, ReadError> {
let bytes = hex::decode(str).map_err(|err| ReadError::InvalidData(err.to_string()))?;

ConfigParams::deserialize(bytes.as_slice())
.map_err(|err| ReadError::InvalidData(err.to_string()))
}

impl UpdateProposal {
pub fn exec(self) -> Result<(), Error> {
let secret_key = read_secret_key(self.secret)?;

let fragment = build_fragment(secret_key);
let fragment = build_fragment(secret_key, self.config);

let fragment_id = post_fragment(self.rest_args, fragment)?;
println!("Posted fragment id: {}", fragment_id);
Expand All @@ -43,10 +53,10 @@ impl UpdateProposal {
}
}

fn build_fragment(secret_key: EitherEd25519SecretKey) -> Fragment {
fn build_fragment(secret_key: EitherEd25519SecretKey, config: ConfigParams) -> Fragment {
let proposer_id = secret_key.to_public();

let update_proposal = UpdateProposalLib::new(ConfigParams::new());
let update_proposal = UpdateProposalLib::new(config);

let bytes = update_proposal.serialize_as_vec().unwrap();

Expand Down

0 comments on commit a53d8c1

Please sign in to comment.