Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close #409: sort validators by address on deserialization #410

Merged
merged 4 commits into from
Jul 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions tendermint/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ impl Set {
/// Create a new validator set.
/// vals is mutable so it can be sorted by address.
pub fn new(mut vals: Vec<Info>) -> Set {
vals.sort_by(|v1, v2| v1.address.partial_cmp(&v2.address).unwrap());
Self::sort_validators(&mut vals);
Set { validators: vals }
}

/// Get Info of the underlying validators.
pub fn validators(&self) -> &Vec<Info> {
&self.validators
}

/// Sort the validators according to the current Tendermint requirements
/// (v. 0.33 -> by validator address, ascending)
fn sort_validators(vals: &mut Vec<Info>) {
vals.sort_by_key(|v| v.address);
}
}

impl Set {
Expand All @@ -49,7 +55,10 @@ fn parse_vals<'de, D>(d: D) -> Result<Vec<Info>, D::Error>
where
D: Deserializer<'de>,
{
Deserialize::deserialize(d).map(|x: Option<_>| x.unwrap_or_default())
let mut vals: Vec<Info> =
Deserialize::deserialize(d).map(|x: Option<_>| x.unwrap_or_default())?;
Set::sort_validators(&mut vals);
Ok(vals)
}

/// Validator information
Expand Down