Skip to content

Commit

Permalink
#414: testgen: add absent votes in the commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-kuprianov committed Aug 10, 2020
1 parent 368975c commit da11fc1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
26 changes: 21 additions & 5 deletions testgen/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use gumdrop::Options;
use serde::Deserialize;
use simple_error::*;
use tendermint::{block, lite};
use std::collections::BTreeSet;
use std::iter::FromIterator;

use crate::{helpers::*, Generator, Header, Validator, Vote};
use crate::validator::sort_validators;

#[derive(Debug, Options, Deserialize, Clone)]
pub struct Commit {
Expand Down Expand Up @@ -104,13 +107,18 @@ impl Generator<block::Commit> for Commit {
None => bail!("failed to generate commit: header is missing"),
Some(h) => h,
};
let block_header = header.generate()?;
let block_id = block::Id::new(lite::Header::hash(&block_header), None);
let votes = match &self.votes {
None => self.clone().generate_default_votes().votes.unwrap(),
Some(vs) => vs.to_vec(),
};
let block_header = header.generate()?;
let block_id = block::Id::new(lite::Header::hash(&block_header), None);

let all_vals = header.validators.as_ref().unwrap();
let mut all_vals: BTreeSet<&Validator> = BTreeSet::from_iter(all_vals);
let votes_vals: Vec<Validator> = votes.iter().map(|v| v.validator.clone().unwrap()).collect();
all_vals.append(&mut BTreeSet::from_iter(&votes_vals));
let all_vals: Vec<Validator> = Vec::from_iter(all_vals.iter().map(|&x|x.clone()));
let all_vals = sort_validators(&all_vals);
let vote_to_sig = |v: &Vote| -> Result<block::CommitSig, SimpleError> {
let vote = v.generate()?;
Ok(block::CommitSig::BlockIDFlagCommit {
Expand All @@ -119,9 +127,17 @@ impl Generator<block::Commit> for Commit {
signature: vote.signature,
})
};
let sigs = votes
let val_to_sig = |val: &Validator| -> Result<block::CommitSig, SimpleError> {
if let Some(vote) = votes.iter().find(|&vote| vote.validator.as_ref().unwrap() == val) {
vote_to_sig(vote)
}
else {
Ok(block::CommitSig::BlockIDFlagAbsent)
}
};
let sigs = all_vals
.iter()
.map(vote_to_sig)
.map(val_to_sig)
.collect::<Result<Vec<block::CommitSig>, SimpleError>>()?;
let commit = block::Commit {
height: block_header.height,
Expand Down
23 changes: 22 additions & 1 deletion testgen/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use signatory::{ed25519, public_key::PublicKeyed};
use signatory_dalek::{Ed25519Signer, Ed25519Verifier};
use simple_error::*;
use tendermint::{account, public_key::PublicKey, validator, vote};
use tendermint::consensus::state::Ordering;

#[derive(Debug, Options, Deserialize, Clone)]
pub struct Validator {
Expand Down Expand Up @@ -79,6 +80,18 @@ impl std::cmp::PartialEq for Validator {
}
impl std::cmp::Eq for Validator {}

impl std::cmp::Ord for Validator {
fn cmp(&self, other: &Self) -> Ordering {
self.generate().unwrap().address.cmp(&other.generate().unwrap().address)
}
}

impl std::cmp::PartialOrd for Validator {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Generator<validator::Info> for Validator {
fn merge_with_default(self, default: Self) -> Self {
Validator {
Expand Down Expand Up @@ -106,12 +119,20 @@ impl Generator<validator::Info> for Validator {

/// A helper function to generate multiple validators at once.
pub fn generate_validators(vals: &[Validator]) -> Result<Vec<validator::Info>, SimpleError> {
Ok(vals
let sorted = sort_validators(vals);
Ok(sorted
.iter()
.map(|v| v.generate())
.collect::<Result<Vec<validator::Info>, SimpleError>>()?)
}

/// A helper function to sort validators according to the Tendermint specs.
pub fn sort_validators(vals: &[Validator]) -> Vec<Validator> {
let mut sorted = vals.clone().to_vec();
sorted.sort_by_key(|v|v.generate().unwrap().address);
sorted.to_vec()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit da11fc1

Please sign in to comment.