Implements is_active_validator helper

This commit is contained in:
Alex Stokes 2019-01-10 11:33:45 -06:00
parent 4d3d351b67
commit 42d950eb88
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086

View File

@ -42,6 +42,12 @@ pub struct ValidatorRecord {
pub penultimate_custody_reseed_slot: u64, pub penultimate_custody_reseed_slot: u64,
} }
impl ValidatorRecord {
pub fn is_active_at(&self, slot: u64) -> bool {
self.activation_slot <= slot && slot < self.exit_slot
}
}
impl default::Default for ValidatorRecord { impl default::Default for ValidatorRecord {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -200,4 +206,26 @@ mod tests {
assert_eq!(original, decoded); assert_eq!(original, decoded);
} }
#[test]
fn test_validator_can_be_active() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let mut validator = ValidatorRecord::random_for_test(&mut rng);
let activation_slot = u64::random_for_test(&mut rng);
let exit_slot = activation_slot + 234;
validator.activation_slot = activation_slot;
validator.exit_slot = exit_slot;
for slot in (activation_slot - 100)..(exit_slot + 100) {
if slot < activation_slot {
assert!(!validator.is_active_at(slot));
} else if slot >= exit_slot {
assert!(!validator.is_active_at(slot));
} else {
assert!(validator.is_active_at(slot));
}
}
}
} }