Update Validator to 0.5.0, add tests

This commit is contained in:
Paul Hauner 2019-03-15 15:48:33 +11:00
parent d84850b892
commit 4ffdfbc993
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6

View File

@ -6,7 +6,7 @@ use test_random_derive::TestRandom;
/// Information about a `BeaconChain` validator. /// Information about a `BeaconChain` validator.
/// ///
/// Spec v0.4.0 /// Spec v0.5.0
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TestRandom, TreeHash)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TestRandom, TreeHash)]
pub struct Validator { pub struct Validator {
pub pubkey: PublicKey, pub pubkey: PublicKey,
@ -53,29 +53,60 @@ impl Default for Validator {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
#[test] #[test]
fn test_validator_can_be_active() { fn default() {
let mut rng = XorShiftRng::from_seed([42; 16]); let v = Validator::default();
let mut validator = Validator::random_for_test(&mut rng);
let activation_epoch = u64::random_for_test(&mut rng); let epoch = Epoch::new(0);
let exit_epoch = activation_epoch + 234;
validator.activation_epoch = Epoch::from(activation_epoch); assert_eq!(v.is_active_at(epoch), false);
validator.exit_epoch = Epoch::from(exit_epoch); assert_eq!(v.is_exited_at(epoch), false);
assert_eq!(v.is_withdrawable_at(epoch), false);
assert_eq!(v.initiated_exit, false);
assert_eq!(v.slashed, false);
}
for slot in (activation_epoch - 100)..(exit_epoch + 100) { #[test]
let slot = Epoch::from(slot); fn is_active_at() {
if slot < activation_epoch { let epoch = Epoch::new(10);
assert!(!validator.is_active_at(slot));
} else if slot >= exit_epoch { let v = Validator {
assert!(!validator.is_active_at(slot)); activation_epoch: epoch,
} else { ..Validator::default()
assert!(validator.is_active_at(slot)); };
}
} assert_eq!(v.is_active_at(epoch - 1), false);
assert_eq!(v.is_active_at(epoch), true);
assert_eq!(v.is_active_at(epoch + 1), true);
}
#[test]
fn is_exited_at() {
let epoch = Epoch::new(10);
let v = Validator {
exit_epoch: epoch,
..Validator::default()
};
assert_eq!(v.is_exited_at(epoch - 1), false);
assert_eq!(v.is_exited_at(epoch), true);
assert_eq!(v.is_exited_at(epoch + 1), true);
}
#[test]
fn is_withdrawable_at() {
let epoch = Epoch::new(10);
let v = Validator {
withdrawable_epoch: epoch,
..Validator::default()
};
assert_eq!(v.is_withdrawable_at(epoch - 1), false);
assert_eq!(v.is_withdrawable_at(epoch), true);
assert_eq!(v.is_withdrawable_at(epoch + 1), true);
} }
ssz_tests!(Validator); ssz_tests!(Validator);