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) (s, keypair)
} }
pub fn status_is(&self, status: ValidatorStatus) -> bool {
self.status == status as u8
}
} }
#[cfg(test)] #[cfg(test)]

View File

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