mirror of
https://gitlab.com/pulsechaincom/lighthouse-pulse.git
synced 2025-01-06 19:12:20 +00:00
Simplify how the StatusFlags
are handled wrt serialization
This commit is contained in:
parent
c9a9f0e305
commit
1f32da81e7
@ -13,13 +13,31 @@ pub enum StatusFlags {
|
|||||||
Withdrawable,
|
Withdrawable,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<u8> for StatusFlags {
|
struct StatusFlagsDecodeError;
|
||||||
fn from(status_flag: u8) -> Self {
|
|
||||||
match status_flag {
|
impl From<StatusFlagsDecodeError> for DecodeError {
|
||||||
STATUS_FLAG_INITIATED_EXIT => StatusFlags::InitiatedExit,
|
fn from(_: StatusFlagsDecodeError) -> DecodeError {
|
||||||
STATUS_FLAG_WITHDRAWABLE => StatusFlags::Withdrawable,
|
DecodeError::Invalid
|
||||||
_ => unreachable!(),
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn status_flag_to_byte(flag: Option<StatusFlags>) -> u8 {
|
||||||
|
if let Some(flag) = flag {
|
||||||
|
match flag {
|
||||||
|
StatusFlags::InitiatedExit => STATUS_FLAG_INITIATED_EXIT,
|
||||||
|
StatusFlags::Withdrawable => STATUS_FLAG_WITHDRAWABLE,
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn status_flag_from_byte(flag: u8) -> Result<Option<StatusFlags>, StatusFlagsDecodeError> {
|
||||||
|
match flag {
|
||||||
|
0 => Ok(None),
|
||||||
|
1 => Ok(Some(StatusFlags::InitiatedExit)),
|
||||||
|
2 => Ok(Some(StatusFlags::Withdrawable)),
|
||||||
|
_ => Err(StatusFlagsDecodeError),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,28 +84,6 @@ impl Default for ValidatorRecord {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for StatusFlags {
|
|
||||||
fn ssz_append(&self, s: &mut SszStream) {
|
|
||||||
let byte: u8 = match self {
|
|
||||||
StatusFlags::InitiatedExit => STATUS_FLAG_INITIATED_EXIT,
|
|
||||||
StatusFlags::Withdrawable => STATUS_FLAG_WITHDRAWABLE,
|
|
||||||
};
|
|
||||||
s.append(&byte);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
1 => StatusFlags::InitiatedExit,
|
|
||||||
2 => StatusFlags::Withdrawable,
|
|
||||||
_ => return Err(DecodeError::Invalid),
|
|
||||||
};
|
|
||||||
Ok((status, i))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: RngCore> TestRandom<T> for StatusFlags {
|
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![StatusFlags::InitiatedExit, StatusFlags::Withdrawable];
|
let options = vec![StatusFlags::InitiatedExit, StatusFlags::Withdrawable];
|
||||||
@ -106,11 +102,7 @@ impl Encodable for ValidatorRecord {
|
|||||||
s.append(&self.withdrawal_slot);
|
s.append(&self.withdrawal_slot);
|
||||||
s.append(&self.penalized_slot);
|
s.append(&self.penalized_slot);
|
||||||
s.append(&self.exit_count);
|
s.append(&self.exit_count);
|
||||||
if let Some(status_flags) = self.status_flags {
|
s.append(&status_flag_to_byte(self.status_flags));
|
||||||
s.append(&status_flags);
|
|
||||||
} else {
|
|
||||||
s.append(&(0 as u8));
|
|
||||||
}
|
|
||||||
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);
|
||||||
@ -133,11 +125,7 @@ impl Decodable for ValidatorRecord {
|
|||||||
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)?;
|
||||||
|
|
||||||
let status_flags = if status_flags_byte == 0u8 {
|
let status_flags = status_flag_from_byte(status_flags_byte)?;
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(StatusFlags::from(status_flags_byte))
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Self {
|
Self {
|
||||||
@ -194,17 +182,6 @@ mod tests {
|
|||||||
assert_eq!(original, decoded);
|
assert_eq!(original, decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
pub fn test_validator_status_ssz_round_trip() {
|
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
|
||||||
let original = StatusFlags::random_for_test(&mut rng);
|
|
||||||
|
|
||||||
let bytes = ssz_encode(&original);
|
|
||||||
let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(original, decoded);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_validator_can_be_active() {
|
fn test_validator_can_be_active() {
|
||||||
let mut rng = XorShiftRng::from_seed([42; 16]);
|
let mut rng = XorShiftRng::from_seed([42; 16]);
|
||||||
|
Loading…
Reference in New Issue
Block a user