mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-29 02:33:48 +00:00
Electra attestation changes rm decode impl (#5856)
* Remove Crappy Decode impl for Attestation * Remove Inefficient Attestation Decode impl * Implement Schema Upgrade / Downgrade * Update beacon_node/beacon_chain/src/schema_change/migration_schema_v20.rs Co-authored-by: Michael Sproul <micsproul@gmail.com> --------- Co-authored-by: Michael Sproul <micsproul@gmail.com>
This commit is contained in:
@@ -4,7 +4,6 @@ use derivative::Derivative;
|
||||
use rand::RngCore;
|
||||
use safe_arith::ArithError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ssz::Decode;
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use ssz_types::BitVector;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@@ -72,26 +71,6 @@ pub struct Attestation<E: EthSpec> {
|
||||
pub signature: AggregateSignature,
|
||||
}
|
||||
|
||||
impl<E: EthSpec> Decode for Attestation<E> {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
|
||||
if let Ok(result) = AttestationBase::from_ssz_bytes(bytes) {
|
||||
return Ok(Attestation::Base(result));
|
||||
}
|
||||
|
||||
if let Ok(result) = AttestationElectra::from_ssz_bytes(bytes) {
|
||||
return Ok(Attestation::Electra(result));
|
||||
}
|
||||
|
||||
Err(ssz::DecodeError::BytesInvalid(String::from(
|
||||
"bytes not valid for any fork variant",
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(electra): think about how to handle fork variants here
|
||||
impl<E: EthSpec> TestRandom for Attestation<E> {
|
||||
fn random_for_test(rng: &mut impl RngCore) -> Self {
|
||||
@@ -417,6 +396,65 @@ impl<'a, E: EthSpec> SlotData for AttestationRef<'a, E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Encode, Decode, PartialEq)]
|
||||
#[ssz(enum_behaviour = "union")]
|
||||
pub enum AttestationOnDisk<E: EthSpec> {
|
||||
Base(AttestationBase<E>),
|
||||
Electra(AttestationElectra<E>),
|
||||
}
|
||||
|
||||
impl<E: EthSpec> AttestationOnDisk<E> {
|
||||
pub fn to_ref(&self) -> AttestationRefOnDisk<E> {
|
||||
match self {
|
||||
AttestationOnDisk::Base(att) => AttestationRefOnDisk::Base(att),
|
||||
AttestationOnDisk::Electra(att) => AttestationRefOnDisk::Electra(att),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Encode)]
|
||||
#[ssz(enum_behaviour = "union")]
|
||||
pub enum AttestationRefOnDisk<'a, E: EthSpec> {
|
||||
Base(&'a AttestationBase<E>),
|
||||
Electra(&'a AttestationElectra<E>),
|
||||
}
|
||||
|
||||
impl<E: EthSpec> From<Attestation<E>> for AttestationOnDisk<E> {
|
||||
fn from(attestation: Attestation<E>) -> Self {
|
||||
match attestation {
|
||||
Attestation::Base(attestation) => Self::Base(attestation),
|
||||
Attestation::Electra(attestation) => Self::Electra(attestation),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: EthSpec> From<AttestationOnDisk<E>> for Attestation<E> {
|
||||
fn from(attestation: AttestationOnDisk<E>) -> Self {
|
||||
match attestation {
|
||||
AttestationOnDisk::Base(attestation) => Self::Base(attestation),
|
||||
AttestationOnDisk::Electra(attestation) => Self::Electra(attestation),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E: EthSpec> From<AttestationRef<'a, E>> for AttestationRefOnDisk<'a, E> {
|
||||
fn from(attestation: AttestationRef<'a, E>) -> Self {
|
||||
match attestation {
|
||||
AttestationRef::Base(attestation) => Self::Base(attestation),
|
||||
AttestationRef::Electra(attestation) => Self::Electra(attestation),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E: EthSpec> From<AttestationRefOnDisk<'a, E>> for AttestationRef<'a, E> {
|
||||
fn from(attestation: AttestationRefOnDisk<'a, E>) -> Self {
|
||||
match attestation {
|
||||
AttestationRefOnDisk::Base(attestation) => Self::Base(attestation),
|
||||
AttestationRefOnDisk::Electra(attestation) => Self::Electra(attestation),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user