Skip to content

Commit

Permalink
#393: more of vote + shorten code
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-kuprianov committed Jul 20, 2020
1 parent 280e822 commit ede2e45
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 43 deletions.
10 changes: 6 additions & 4 deletions mbt-utils/bin/tendermint-testgen.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use gumdrop::Options;

use tendermint_testgen::commit::Commit;
use tendermint_testgen::header::Header;
use tendermint_testgen::generator::Generator;
use tendermint_testgen::validator::Validator;
use tendermint_testgen::{
Generator, Validator, Header, Vote, Commit
};
use simple_error::SimpleError;
use tendermint_testgen::helpers::read_stdin;

Expand Down Expand Up @@ -66,6 +65,8 @@ enum Command {
Validator(Validator),
#[options(help = "produce header from validator array and other parameters")]
Header(Header),
#[options(help = "produce vote from validator and other parameters")]
Vote(Vote),
#[options(help = "produce commit from validator array and other parameters")]
Commit(Commit),
}
Expand Down Expand Up @@ -124,6 +125,7 @@ fn main() {
}
Some(Command::Validator(cli)) => run_command(cli, opts.stdin),
Some(Command::Header(cli)) => run_command(cli, opts.stdin),
Some(Command::Vote(cli)) => eprintln!("{:?}", cli),
Some(Command::Commit(cli)) => run_command(cli, opts.stdin),
}
}
9 changes: 2 additions & 7 deletions mbt-utils/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ use tendermint::signature::Signature;
use tendermint::vote::{Type, Vote};
use tendermint::{amino_types, block, lite, vote};

use crate::header::Header;
use crate::helpers::*;
use crate::generator::Generator;
use crate::validator::Validator;
use crate::{Generator, Validator, Header};

#[derive(Debug, Options, Deserialize)]
pub struct Commit {
Expand All @@ -40,10 +38,7 @@ impl std::str::FromStr for Commit {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let commit = match parse_as::<Commit>(s) {
Ok(input) => input,
Err(_) => Commit {
header: Some(parse_as::<Header>(s)?),
round: None,
},
Err(_) => Commit::new(&parse_as::<Header>(s)?)
};
Ok(commit)
}
Expand Down
11 changes: 2 additions & 9 deletions mbt-utils/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use tendermint::lite::ValidatorSet;
use tendermint::{block, chain, validator, Time};

use crate::helpers::*;
use crate::generator::Generator;
use crate::validator::{generate_validators, Validator};
use crate::{Generator, Validator, validator::generate_validators};

#[derive(Debug, Options, Deserialize, Clone)]
pub struct Header {
Expand Down Expand Up @@ -53,13 +52,7 @@ impl std::str::FromStr for Header {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let header = match parse_as::<Header>(s) {
Ok(input) => input,
Err(_) => Header {
validators: Some(parse_as::<Vec<Validator>>(s)?),
next_validators: None,
chain_id: None,
height: None,
time: None,
},
Err(_) => Header::new(&parse_as::<Vec<Validator>>(s)?)
};
Ok(header)
}
Expand Down
8 changes: 7 additions & 1 deletion mbt-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ pub mod validator;
pub mod header;
pub mod vote;
pub mod commit;
pub mod consensus;
pub mod consensus;

pub use generator::Generator;
pub use validator::Validator;
pub use header::Header;
pub use vote::Vote;
pub use commit::Commit;
17 changes: 6 additions & 11 deletions mbt-utils/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tendermint::validator::{Info, ProposerPriority};
use tendermint::vote::Power;

use crate::helpers::*;
use crate::generator::Generator;
use crate::Generator;

#[derive(Debug, Options, Deserialize, Clone)]
pub struct Validator {
Expand Down Expand Up @@ -45,8 +45,11 @@ impl Validator {
bail!("validator identifier is missing")
}
let mut bytes = self.id.clone().unwrap().into_bytes();
if bytes.is_empty() {
bail!("empty validator identifier")
}
if bytes.len() > 32 {
bail!("identifier is too long")
bail!("validator identifier is too long")
}
bytes.extend(vec![0u8; 32 - bytes.len()].iter());
let seed = require_with!(
Expand All @@ -62,15 +65,7 @@ impl std::str::FromStr for Validator {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let validator = match parse_as::<Validator>(s) {
Ok(input) => input,
Err(_) => Validator {
id: if s.is_empty() {
bail!("failed to parse validator")
} else {
Some(s.to_string())
},
voting_power: None,
proposer_priority: None,
}
Err(_) => Validator::new(s)
};
Ok(validator)
}
Expand Down
34 changes: 23 additions & 11 deletions mbt-utils/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,53 @@ use gumdrop::Options;
use serde::Deserialize;
use simple_error::*;
use crate::helpers::*;
use crate::validator::Validator;
use tendermint::{Time};
use crate::{Validator, Header};

#[derive(Debug, Options, Deserialize, Clone)]
pub struct Vote {
#[options(help = "validator of this vote (required; can be passed via STDIN)",
parse(try_from_str = "parse_as::<Validator>"))]
pub validator: Option<Validator>,
#[options(help = "vote type; if set -- this is 'precommit' vote, otherwise 'prevote' (default)")]
pub precommit: Option<bool>,
#[options(help = "block height (default: taken from block)")]
#[options(help = "validator index (default: from commit header)")]
pub index: Option<u64>,
#[options(help = "header to sign (default: commit header)")]
pub header: Option<Header>,
#[options(help = "vote type; 'precommit' if set, otherwise 'prevote' (default)")]
pub precommit: Option<()>,
#[options(help = "block height (default: from commit header)")]
pub height: Option<u64>,
#[options(help = "time (default: from commit header)")]
pub time: Option<Time>,
#[options(help = "commit round (default: from commit)")]
pub round: Option<u64>,
}

impl Vote {
pub fn new(validator: &Validator) -> Self {
Vote {
validator: Some(validator.clone()),
index: None,
header: None,
precommit: None,
height: None
height: None,
time: None,
round: None
}
}
set_option!(precommit, bool);
set_option!(index, u64);
set_option!(precommit, ());
set_option!(height, u64);
set_option!(time, Time);
set_option!(round, u64);
}

impl std::str::FromStr for Vote {
type Err = SimpleError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let vote = match parse_as::<Vote>(s) {
Ok(input) => input,
Err(_) => Vote {
validator: Some(parse_as::<Validator>(s)?),
precommit: None,
height: None
}
Err(_) => Vote::new(&parse_as::<Validator>(s)?)
};
Ok(vote)
}
Expand Down

0 comments on commit ede2e45

Please sign in to comment.