Updates ValidatorRecord to match the latest spec

This commit is contained in:
Alex Stokes 2019-01-08 16:55:04 -06:00
parent f74bf597dd
commit 6928301b91
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086
2 changed files with 43 additions and 57 deletions

View File

@ -50,7 +50,7 @@ pub use crate::proposer_slashing::ProposerSlashing;
pub use crate::shard_committee::ShardCommittee;
pub use crate::slashable_vote_data::SlashableVoteData;
pub use crate::special_record::{SpecialRecord, SpecialRecordKind};
pub use crate::validator_record::{ValidatorRecord, ValidatorStatus};
pub use crate::validator_record::{StatusFlags as ValidatorStatusFlags, ValidatorRecord};
pub type Hash256 = H256;
pub type Address = H160;

View File

@ -5,25 +5,20 @@ use rand::RngCore;
use ssz::{Decodable, DecodeError, Encodable, SszStream};
use std::convert;
const STATUS_FLAG_INITIATED_EXIT: u8 = 1;
const STATUS_FLAG_WITHDRAWABLE: u8 = 2;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ValidatorStatus {
PendingActivation,
Active,
PendingExit,
PendingWithdraw,
Withdrawn,
Penalized,
pub enum StatusFlags {
InitiatedExit,
Withdrawable,
}
impl convert::From<u8> for ValidatorStatus {
fn from(status: u8) -> Self {
match status {
0 => ValidatorStatus::PendingActivation,
1 => ValidatorStatus::Active,
2 => ValidatorStatus::PendingExit,
3 => ValidatorStatus::PendingWithdraw,
5 => ValidatorStatus::Withdrawn,
127 => ValidatorStatus::Penalized,
impl convert::From<u8> for StatusFlags {
fn from(status_flag: u8) -> Self {
match status_flag {
STATUS_FLAG_INITIATED_EXIT => StatusFlags::InitiatedExit,
STATUS_FLAG_WITHDRAWABLE => StatusFlags::Withdrawable,
_ => unreachable!(),
}
}
@ -40,59 +35,38 @@ pub struct ValidatorRecord {
pub withdrawal_slot: u64,
pub penalized_slot: u64,
pub exit_count: u64,
pub status: ValidatorStatus,
pub status_flags: StatusFlags,
pub custody_commitment: Hash256,
pub latest_custody_reseed_slot: u64,
pub penultimate_custody_reseed_slot: u64,
}
impl ValidatorRecord {
pub fn status_is(&self, status: ValidatorStatus) -> bool {
self.status == status
}
}
impl Encodable for ValidatorStatus {
impl Encodable for StatusFlags {
fn ssz_append(&self, s: &mut SszStream) {
let byte: u8 = match self {
ValidatorStatus::PendingActivation => 0,
ValidatorStatus::Active => 1,
ValidatorStatus::PendingExit => 2,
ValidatorStatus::PendingWithdraw => 3,
ValidatorStatus::Withdrawn => 5,
ValidatorStatus::Penalized => 127,
StatusFlags::InitiatedExit => STATUS_FLAG_INITIATED_EXIT,
StatusFlags::Withdrawable => STATUS_FLAG_WITHDRAWABLE,
};
s.append(&byte);
}
}
impl Decodable for ValidatorStatus {
impl Decodable for StatusFlags {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (byte, i) = u8::ssz_decode(bytes, i)?;
let status = match byte {
0 => ValidatorStatus::PendingActivation,
1 => ValidatorStatus::Active,
2 => ValidatorStatus::PendingExit,
3 => ValidatorStatus::PendingWithdraw,
5 => ValidatorStatus::Withdrawn,
127 => ValidatorStatus::Penalized,
1 => StatusFlags::InitiatedExit,
2 => StatusFlags::Withdrawable,
_ => return Err(DecodeError::Invalid),
};
Ok((status, i))
}
}
impl<T: RngCore> TestRandom<T> for ValidatorStatus {
impl<T: RngCore> TestRandom<T> for StatusFlags {
fn random_for_test(rng: &mut T) -> Self {
let options = vec![
ValidatorStatus::PendingActivation,
ValidatorStatus::Active,
ValidatorStatus::PendingExit,
ValidatorStatus::PendingWithdraw,
ValidatorStatus::Withdrawn,
ValidatorStatus::Penalized,
];
options[(rng.next_u32() as usize) % options.len()]
let options = vec![StatusFlags::InitiatedExit, StatusFlags::Withdrawable];
options[(rng.next_u32() as usize) % options.len()].clone()
}
}
@ -102,9 +76,12 @@ impl Encodable for ValidatorRecord {
s.append(&self.withdrawal_credentials);
s.append(&self.randao_commitment);
s.append(&self.randao_layers);
s.append(&self.status);
s.append(&self.latest_status_change_slot);
s.append(&self.activation_slot);
s.append(&self.exit_slot);
s.append(&self.withdrawal_slot);
s.append(&self.penalized_slot);
s.append(&self.exit_count);
s.append(&self.status_flags);
s.append(&self.custody_commitment);
s.append(&self.latest_custody_reseed_slot);
s.append(&self.penultimate_custody_reseed_slot);
@ -117,9 +94,12 @@ impl Decodable for ValidatorRecord {
let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?;
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
let (randao_layers, i) = <_>::ssz_decode(bytes, i)?;
let (status, i) = <_>::ssz_decode(bytes, i)?;
let (latest_status_change_slot, i) = <_>::ssz_decode(bytes, i)?;
let (activation_slot, i) = <_>::ssz_decode(bytes, i)?;
let (exit_slot, i) = <_>::ssz_decode(bytes, i)?;
let (withdrawal_slot, i) = <_>::ssz_decode(bytes, i)?;
let (penalized_slot, i) = <_>::ssz_decode(bytes, i)?;
let (exit_count, i) = <_>::ssz_decode(bytes, i)?;
let (status_flags, i) = <_>::ssz_decode(bytes, i)?;
let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?;
let (latest_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?;
let (penultimate_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?;
@ -130,9 +110,12 @@ impl Decodable for ValidatorRecord {
withdrawal_credentials,
randao_commitment,
randao_layers,
status,
latest_status_change_slot,
activation_slot,
exit_slot,
withdrawal_slot,
penalized_slot,
exit_count,
status_flags,
custody_commitment,
latest_custody_reseed_slot,
penultimate_custody_reseed_slot,
@ -149,9 +132,12 @@ impl<T: RngCore> TestRandom<T> for ValidatorRecord {
withdrawal_credentials: <_>::random_for_test(rng),
randao_commitment: <_>::random_for_test(rng),
randao_layers: <_>::random_for_test(rng),
status: <_>::random_for_test(rng),
latest_status_change_slot: <_>::random_for_test(rng),
activation_slot: <_>::random_for_test(rng),
exit_slot: <_>::random_for_test(rng),
withdrawal_slot: <_>::random_for_test(rng),
penalized_slot: <_>::random_for_test(rng),
exit_count: <_>::random_for_test(rng),
status_flags: <_>::random_for_test(rng),
custody_commitment: <_>::random_for_test(rng),
latest_custody_reseed_slot: <_>::random_for_test(rng),
penultimate_custody_reseed_slot: <_>::random_for_test(rng),
@ -179,7 +165,7 @@ mod tests {
#[test]
pub fn test_validator_status_ssz_round_trip() {
let mut rng = XorShiftRng::from_seed([42; 16]);
let original = ValidatorStatus::random_for_test(&mut rng);
let original = StatusFlags::random_for_test(&mut rng);
let bytes = ssz_encode(&original);
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();