Updates ValidatorRecord
to match the latest spec
This commit is contained in:
parent
f74bf597dd
commit
6928301b91
@ -50,7 +50,7 @@ pub use crate::proposer_slashing::ProposerSlashing;
|
|||||||
pub use crate::shard_committee::ShardCommittee;
|
pub use crate::shard_committee::ShardCommittee;
|
||||||
pub use crate::slashable_vote_data::SlashableVoteData;
|
pub use crate::slashable_vote_data::SlashableVoteData;
|
||||||
pub use crate::special_record::{SpecialRecord, SpecialRecordKind};
|
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 Hash256 = H256;
|
||||||
pub type Address = H160;
|
pub type Address = H160;
|
||||||
|
@ -5,25 +5,20 @@ use rand::RngCore;
|
|||||||
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
use ssz::{Decodable, DecodeError, Encodable, SszStream};
|
||||||
use std::convert;
|
use std::convert;
|
||||||
|
|
||||||
|
const STATUS_FLAG_INITIATED_EXIT: u8 = 1;
|
||||||
|
const STATUS_FLAG_WITHDRAWABLE: u8 = 2;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
pub enum ValidatorStatus {
|
pub enum StatusFlags {
|
||||||
PendingActivation,
|
InitiatedExit,
|
||||||
Active,
|
Withdrawable,
|
||||||
PendingExit,
|
|
||||||
PendingWithdraw,
|
|
||||||
Withdrawn,
|
|
||||||
Penalized,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl convert::From<u8> for ValidatorStatus {
|
impl convert::From<u8> for StatusFlags {
|
||||||
fn from(status: u8) -> Self {
|
fn from(status_flag: u8) -> Self {
|
||||||
match status {
|
match status_flag {
|
||||||
0 => ValidatorStatus::PendingActivation,
|
STATUS_FLAG_INITIATED_EXIT => StatusFlags::InitiatedExit,
|
||||||
1 => ValidatorStatus::Active,
|
STATUS_FLAG_WITHDRAWABLE => StatusFlags::Withdrawable,
|
||||||
2 => ValidatorStatus::PendingExit,
|
|
||||||
3 => ValidatorStatus::PendingWithdraw,
|
|
||||||
5 => ValidatorStatus::Withdrawn,
|
|
||||||
127 => ValidatorStatus::Penalized,
|
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,59 +35,38 @@ pub struct ValidatorRecord {
|
|||||||
pub withdrawal_slot: u64,
|
pub withdrawal_slot: u64,
|
||||||
pub penalized_slot: u64,
|
pub penalized_slot: u64,
|
||||||
pub exit_count: u64,
|
pub exit_count: u64,
|
||||||
pub status: ValidatorStatus,
|
pub status_flags: StatusFlags,
|
||||||
pub custody_commitment: Hash256,
|
pub custody_commitment: Hash256,
|
||||||
pub latest_custody_reseed_slot: u64,
|
pub latest_custody_reseed_slot: u64,
|
||||||
pub penultimate_custody_reseed_slot: u64,
|
pub penultimate_custody_reseed_slot: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ValidatorRecord {
|
impl Encodable for StatusFlags {
|
||||||
pub fn status_is(&self, status: ValidatorStatus) -> bool {
|
|
||||||
self.status == status
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encodable for ValidatorStatus {
|
|
||||||
fn ssz_append(&self, s: &mut SszStream) {
|
fn ssz_append(&self, s: &mut SszStream) {
|
||||||
let byte: u8 = match self {
|
let byte: u8 = match self {
|
||||||
ValidatorStatus::PendingActivation => 0,
|
StatusFlags::InitiatedExit => STATUS_FLAG_INITIATED_EXIT,
|
||||||
ValidatorStatus::Active => 1,
|
StatusFlags::Withdrawable => STATUS_FLAG_WITHDRAWABLE,
|
||||||
ValidatorStatus::PendingExit => 2,
|
|
||||||
ValidatorStatus::PendingWithdraw => 3,
|
|
||||||
ValidatorStatus::Withdrawn => 5,
|
|
||||||
ValidatorStatus::Penalized => 127,
|
|
||||||
};
|
};
|
||||||
s.append(&byte);
|
s.append(&byte);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Decodable for ValidatorStatus {
|
impl Decodable for StatusFlags {
|
||||||
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
|
||||||
let (byte, i) = u8::ssz_decode(bytes, i)?;
|
let (byte, i) = u8::ssz_decode(bytes, i)?;
|
||||||
let status = match byte {
|
let status = match byte {
|
||||||
0 => ValidatorStatus::PendingActivation,
|
1 => StatusFlags::InitiatedExit,
|
||||||
1 => ValidatorStatus::Active,
|
2 => StatusFlags::Withdrawable,
|
||||||
2 => ValidatorStatus::PendingExit,
|
|
||||||
3 => ValidatorStatus::PendingWithdraw,
|
|
||||||
5 => ValidatorStatus::Withdrawn,
|
|
||||||
127 => ValidatorStatus::Penalized,
|
|
||||||
_ => return Err(DecodeError::Invalid),
|
_ => return Err(DecodeError::Invalid),
|
||||||
};
|
};
|
||||||
Ok((status, i))
|
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 {
|
fn random_for_test(rng: &mut T) -> Self {
|
||||||
let options = vec![
|
let options = vec![StatusFlags::InitiatedExit, StatusFlags::Withdrawable];
|
||||||
ValidatorStatus::PendingActivation,
|
options[(rng.next_u32() as usize) % options.len()].clone()
|
||||||
ValidatorStatus::Active,
|
|
||||||
ValidatorStatus::PendingExit,
|
|
||||||
ValidatorStatus::PendingWithdraw,
|
|
||||||
ValidatorStatus::Withdrawn,
|
|
||||||
ValidatorStatus::Penalized,
|
|
||||||
];
|
|
||||||
options[(rng.next_u32() as usize) % options.len()]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,9 +76,12 @@ impl Encodable for ValidatorRecord {
|
|||||||
s.append(&self.withdrawal_credentials);
|
s.append(&self.withdrawal_credentials);
|
||||||
s.append(&self.randao_commitment);
|
s.append(&self.randao_commitment);
|
||||||
s.append(&self.randao_layers);
|
s.append(&self.randao_layers);
|
||||||
s.append(&self.status);
|
s.append(&self.activation_slot);
|
||||||
s.append(&self.latest_status_change_slot);
|
s.append(&self.exit_slot);
|
||||||
|
s.append(&self.withdrawal_slot);
|
||||||
|
s.append(&self.penalized_slot);
|
||||||
s.append(&self.exit_count);
|
s.append(&self.exit_count);
|
||||||
|
s.append(&self.status_flags);
|
||||||
s.append(&self.custody_commitment);
|
s.append(&self.custody_commitment);
|
||||||
s.append(&self.latest_custody_reseed_slot);
|
s.append(&self.latest_custody_reseed_slot);
|
||||||
s.append(&self.penultimate_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 (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
let (randao_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (randao_layers, i) = <_>::ssz_decode(bytes, i)?;
|
let (randao_layers, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (status, i) = <_>::ssz_decode(bytes, i)?;
|
let (activation_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (latest_status_change_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 (exit_count, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
|
let (status_flags, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
let (custody_commitment, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
let (latest_custody_reseed_slot, 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)?;
|
let (penultimate_custody_reseed_slot, i) = <_>::ssz_decode(bytes, i)?;
|
||||||
@ -130,9 +110,12 @@ impl Decodable for ValidatorRecord {
|
|||||||
withdrawal_credentials,
|
withdrawal_credentials,
|
||||||
randao_commitment,
|
randao_commitment,
|
||||||
randao_layers,
|
randao_layers,
|
||||||
status,
|
activation_slot,
|
||||||
latest_status_change_slot,
|
exit_slot,
|
||||||
|
withdrawal_slot,
|
||||||
|
penalized_slot,
|
||||||
exit_count,
|
exit_count,
|
||||||
|
status_flags,
|
||||||
custody_commitment,
|
custody_commitment,
|
||||||
latest_custody_reseed_slot,
|
latest_custody_reseed_slot,
|
||||||
penultimate_custody_reseed_slot,
|
penultimate_custody_reseed_slot,
|
||||||
@ -149,9 +132,12 @@ impl<T: RngCore> TestRandom<T> for ValidatorRecord {
|
|||||||
withdrawal_credentials: <_>::random_for_test(rng),
|
withdrawal_credentials: <_>::random_for_test(rng),
|
||||||
randao_commitment: <_>::random_for_test(rng),
|
randao_commitment: <_>::random_for_test(rng),
|
||||||
randao_layers: <_>::random_for_test(rng),
|
randao_layers: <_>::random_for_test(rng),
|
||||||
status: <_>::random_for_test(rng),
|
activation_slot: <_>::random_for_test(rng),
|
||||||
latest_status_change_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),
|
exit_count: <_>::random_for_test(rng),
|
||||||
|
status_flags: <_>::random_for_test(rng),
|
||||||
custody_commitment: <_>::random_for_test(rng),
|
custody_commitment: <_>::random_for_test(rng),
|
||||||
latest_custody_reseed_slot: <_>::random_for_test(rng),
|
latest_custody_reseed_slot: <_>::random_for_test(rng),
|
||||||
penultimate_custody_reseed_slot: <_>::random_for_test(rng),
|
penultimate_custody_reseed_slot: <_>::random_for_test(rng),
|
||||||
@ -179,7 +165,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
pub fn test_validator_status_ssz_round_trip() {
|
pub fn test_validator_status_ssz_round_trip() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
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 bytes = ssz_encode(&original);
|
||||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
||||||
|
Loading…
Reference in New Issue
Block a user