diff --git a/tendermint/src/validator.rs b/tendermint/src/validator.rs index d3af30bb3..8fbb79f1f 100644 --- a/tendermint/src/validator.rs +++ b/tendermint/src/validator.rs @@ -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> = 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> = &self + let validator_bytes: Vec> = 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 { @@ -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, @@ -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 @@ -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]); } }