Skip to content

Commit

Permalink
Add a few methods to tests and deduplicate code for fn hash of
Browse files Browse the repository at this point in the history
lite::ValidatorSet:: & Set

- tests for  lite::ValidatorSetLookup lite::ValidatorSet impl
- delete `fn hash(self) -> merkle::Hash` from Set and only keep the impl
  of lite::ValidatorSet
  • Loading branch information
liamsi committed Nov 13, 2019
1 parent c8d17a9 commit 23042bb
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions tendermint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,19 @@ impl Set {
vals.sort_by(|v1, v2| v1.address.partial_cmp(&v2.address).unwrap());
Set { validators: vals }
}

/// Compute the Merkle root of the validator set
pub fn hash(self) -> merkle::Hash {
let validator_bytes: Vec<Vec<u8>> = self
.validators
.into_iter()
.map(|validator| validator.hash_bytes())
.collect();
merkle::simple_hash_from_byte_slices(validator_bytes)
}
}

impl lite::ValidatorSet for Set {
type Validator = Info;

/// Compute the Merkle root of the validator set
fn hash(&self) -> Hash {
// TODO almost the same as above's pub fn hash(self) -> merkle::Hash
// deduplicate
let validator_bytes: &Vec<Vec<u8>> = &self
let validator_bytes: Vec<Vec<u8>> = self
.validators
.iter()
.map(|validator| validator.hash_bytes())
.collect();
Hash::Sha256(merkle::simple_hash_from_byte_slices(
validator_bytes.to_vec(),
))
Hash::Sha256(merkle::simple_hash_from_byte_slices(validator_bytes))
}

fn total_power(&self) -> u64 {
Expand All @@ -72,7 +59,7 @@ impl lite::ValidatorSetLookup for Set {
}

/// Validator information
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
pub struct Info {
/// Validator account address
pub address: account::Id,
Expand Down Expand Up @@ -231,6 +218,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::lite::{ValidatorSet, ValidatorSetLookup};
use subtle_encoding::hex;

// make a validator from a hex ed25519 pubkey and a voting power
Expand Down Expand Up @@ -259,6 +247,21 @@ mod tests {

let val_set = Set::new(vec![v1, v2, v3]);
let hash = val_set.hash();
assert_eq!(hash_expect, &hash);
assert_eq!(hash_expect, &hash.as_bytes().unwrap().to_vec());

let not_in_set = make_validator(
"EB6B732C5BD86B5FA3F3BC3DB688DA0ED182A7411F81C2D405506B298FC19E52",
1,
);
assert_eq!(val_set.validator(v1.address).unwrap(), v1);
assert_eq!(val_set.validator(v2.address).unwrap(), v2);
assert_eq!(val_set.validator(v3.address).unwrap(), v3);
assert_eq!(val_set.validator(not_in_set.address), None);
assert_eq!(
val_set.total_power(),
148_151_478_422_287_875 + 158_095_448_483_785_107 + 770_561_664_770_006_272
);

assert_eq!(val_set.into_vec(), vec![v1, v2, v3]);
}
}

0 comments on commit 23042bb

Please sign in to comment.