add method to determine validator status

This commit is contained in:
Alex Stokes 2018-12-11 15:16:25 -08:00
parent 86e5ce9ed8
commit d3681e876a
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086
2 changed files with 11 additions and 6 deletions

View File

@ -42,6 +42,10 @@ impl ValidatorRecord {
};
(s, keypair)
}
pub fn status_is(&self, status: ValidatorStatus) -> bool {
self.status == status as u8
}
}
#[cfg(test)]

View File

@ -2,28 +2,29 @@ extern crate types;
use types::{ValidatorRecord, ValidatorStatus};
pub fn validator_is_active(v: &ValidatorRecord) -> bool {
v.status == ValidatorStatus::Active as u8
}
/// Returns the indicies of each active validator in a given vec of validators.
pub fn active_validator_indices(validators: &[ValidatorRecord]) -> Vec<usize> {
validators
.iter()
.enumerate()
.filter_map(|(i, validator)| {
if validator_is_active(&validator) {
if validator.status_is(ValidatorStatus::Active) {
Some(i)
} else {
None
}
}).collect()
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
pub fn validator_is_active(v: &ValidatorRecord) -> bool {
v.status_is(ValidatorStatus::Active)
}
#[test]
fn test_active_validator() {
let mut validators = vec![];