From 42d950eb88f27605c505710a1ecea213ac8989af Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 10 Jan 2019 11:33:45 -0600 Subject: [PATCH] Implements `is_active_validator` helper --- beacon_chain/types/src/validator_record.rs | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 7077d87e2..0e1102a59 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -42,6 +42,12 @@ pub struct ValidatorRecord { 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 { fn default() -> Self { Self { @@ -200,4 +206,26 @@ mod tests { 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)); + } + } + } }