Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 21, 2023
1 parent 5534ae9 commit d7fcb01
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Code/test/src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl malachite_common::Proposal<TestContext> for Proposal {
}

fn value(&self) -> Value {
self.value.clone()
self.value
}

fn pol_round(&self) -> Round {
Expand Down
25 changes: 1 addition & 24 deletions Code/test/src/validator_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,20 @@ pub struct ValidatorSet {

impl ValidatorSet {
pub fn new(validators: impl IntoIterator<Item = Validator>) -> Self {
let mut validators: Vec<_> = validators.into_iter().collect();
let validators: Vec<_> = validators.into_iter().collect();
assert!(!validators.is_empty());

Self::sort_validators(&mut validators);

Self { validators }
}

/// The total voting power of the validator set
pub fn total_voting_power(&self) -> VotingPower {
// TODO: Cache this?
self.validators.iter().map(|v| v.voting_power).sum()
}

/// Add a validator to the set
pub fn add(&mut self, validator: Validator) {
self.validators.push(validator);

ValidatorSet::sort_validators(&mut self.validators);
}

/// Update the voting power of the given validator
Expand All @@ -106,35 +101,17 @@ impl ValidatorSet {
{
v.voting_power = val.voting_power;
}

Self::sort_validators(&mut self.validators);
}

/// Remove a validator from the set
pub fn remove(&mut self, address: &Address) {
self.validators.retain(|v| &v.address != address);

Self::sort_validators(&mut self.validators); // TODO: Not needed
}

/// Get a validator by its address
pub fn get_by_address(&self, address: &Address) -> Option<&Validator> {
self.validators.iter().find(|v| &v.address == address)
}

/// In place sort and deduplication of a list of validators
fn sort_validators(vals: &mut Vec<Validator>) {
// Sort the validators according to the current Tendermint requirements
use core::cmp::Reverse;

vals.sort_unstable_by(|v1, v2| {
let a = (Reverse(v1.voting_power), &v1.address);
let b = (Reverse(v2.voting_power), &v2.address);
a.cmp(&b)
});

vals.dedup();
}
}

impl malachite_common::ValidatorSet<TestContext> for ValidatorSet {
Expand Down

0 comments on commit d7fcb01

Please sign in to comment.