mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-31 13:17:09 +00:00
Cleanup payload types (#3675)
* Add transparent support * Add `Config` struct * Deprecate `enum_behaviour` * Partially remove enum_behaviour from project * Revert "Partially remove enum_behaviour from project" This reverts commit46ffb7fe77. * Revert "Deprecate `enum_behaviour`" This reverts commit89b64a6f53. * Add `struct_behaviour` * Tidy * Move tests into `ssz_derive` * Bump ssz derive * Fix comment * newtype transaparent ssz * use ssz transparent and create macros for per fork implementations * use superstruct map macros Co-authored-by: Paul Hauner <paul@paulhauner.com>
This commit is contained in:
@@ -120,6 +120,7 @@ pub enum Error {
|
||||
ArithError(ArithError),
|
||||
MissingBeaconBlock(SignedBeaconBlockHash),
|
||||
MissingBeaconState(BeaconStateHash),
|
||||
PayloadConversionLogicFlaw,
|
||||
SyncCommitteeNotKnown {
|
||||
current_epoch: Epoch,
|
||||
epoch: Epoch,
|
||||
|
||||
@@ -121,8 +121,4 @@ impl<T: EthSpec> ExecutionPayload<T> {
|
||||
// Max size of variable length `withdrawals` field
|
||||
+ (T::max_withdrawals_per_payload() * <Withdrawal as Encode>::ssz_fixed_len())
|
||||
}
|
||||
|
||||
pub fn blob_txns_iter(&self) -> Iter<'_, Transaction<T::MaxBytesPerTransaction>> {
|
||||
self.transactions().iter()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,12 @@ pub struct ExecutionPayloadHeader<T: EthSpec> {
|
||||
pub withdrawals_root: Hash256,
|
||||
}
|
||||
|
||||
impl<T: EthSpec> ExecutionPayloadHeader<T> {
|
||||
pub fn transactions(&self) -> Option<&Transactions<T>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: EthSpec> ExecutionPayloadHeaderRef<'a, T> {
|
||||
// FIXME: maybe this could be a derived trait..
|
||||
pub fn is_default(self) -> bool {
|
||||
@@ -210,6 +216,34 @@ impl<T: EthSpec> From<ExecutionPayloadEip4844<T>> for ExecutionPayloadHeaderEip4
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EthSpec> From<ExecutionPayloadMerge<T>> for ExecutionPayloadHeader<T> {
|
||||
fn from(payload: ExecutionPayloadMerge<T>) -> Self {
|
||||
Self::Merge(ExecutionPayloadHeaderMerge::from(payload))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EthSpec> From<ExecutionPayloadCapella<T>> for ExecutionPayloadHeader<T> {
|
||||
fn from(payload: ExecutionPayloadCapella<T>) -> Self {
|
||||
Self::Capella(ExecutionPayloadHeaderCapella::from(payload))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EthSpec> From<ExecutionPayloadEip4844<T>> for ExecutionPayloadHeader<T> {
|
||||
fn from(payload: ExecutionPayloadEip4844<T>) -> Self {
|
||||
Self::Eip4844(ExecutionPayloadHeaderEip4844::from(payload))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EthSpec> From<ExecutionPayload<T>> for ExecutionPayloadHeader<T> {
|
||||
fn from(payload: ExecutionPayload<T>) -> Self {
|
||||
match payload {
|
||||
ExecutionPayload::Merge(payload) => Self::from(payload),
|
||||
ExecutionPayload::Capella(payload) => Self::from(payload),
|
||||
ExecutionPayload::Eip4844(payload) => Self::from(payload),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: EthSpec> TryFrom<ExecutionPayloadHeader<T>> for ExecutionPayloadHeaderMerge<T> {
|
||||
type Error = BeaconStateError;
|
||||
fn try_from(header: ExecutionPayloadHeader<T>) -> Result<Self, Self::Error> {
|
||||
|
||||
@@ -2,13 +2,14 @@ use crate::test_utils::TestRandom;
|
||||
use crate::*;
|
||||
use derivative::Derivative;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::{Decode, DecodeError, Encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use std::fmt;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use tree_hash::{PackedEncoding, TreeHash};
|
||||
|
||||
#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Derivative, Debug, Clone, Encode, Decode, Serialize, Deserialize)]
|
||||
#[derivative(PartialEq, Eq, Hash)]
|
||||
#[ssz(struct_behaviour = "transparent")]
|
||||
pub struct KzgCommitment(#[serde(with = "BigArray")] pub [u8; 48]);
|
||||
|
||||
impl Display for KzgCommitment {
|
||||
@@ -40,27 +41,3 @@ impl TestRandom for KzgCommitment {
|
||||
KzgCommitment(<[u8; 48] as TestRandom>::random_for_test(rng))
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for KzgCommitment {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<[u8; 48] as Decode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
<[u8; 48] as Decode>::from_ssz_bytes(bytes).map(KzgCommitment)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for KzgCommitment {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<[u8; 48] as Encode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
self.0.ssz_append(buf)
|
||||
}
|
||||
|
||||
fn ssz_bytes_len(&self) -> usize {
|
||||
self.0.ssz_bytes_len()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,15 @@ use crate::test_utils::{RngCore, TestRandom};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_big_array::BigArray;
|
||||
use ssz::{Decode, DecodeError, Encode};
|
||||
use ssz_derive::{Decode, Encode};
|
||||
use std::fmt;
|
||||
use tree_hash::{PackedEncoding, TreeHash};
|
||||
|
||||
const KZG_PROOF_BYTES_LEN: usize = 48;
|
||||
|
||||
#[derive(Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)]
|
||||
#[derive(Debug, PartialEq, Hash, Clone, Copy, Encode, Decode, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
#[ssz(struct_behaviour = "transparent")]
|
||||
pub struct KzgProof(#[serde(with = "BigArray")] pub [u8; KZG_PROOF_BYTES_LEN]);
|
||||
|
||||
impl fmt::Display for KzgProof {
|
||||
@@ -35,38 +37,6 @@ impl Into<[u8; KZG_PROOF_BYTES_LEN]> for KzgProof {
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for KzgProof {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<[u8; KZG_PROOF_BYTES_LEN] as Encode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_fixed_len() -> usize {
|
||||
<[u8; KZG_PROOF_BYTES_LEN] as Encode>::ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_bytes_len(&self) -> usize {
|
||||
self.0.ssz_bytes_len()
|
||||
}
|
||||
|
||||
fn ssz_append(&self, buf: &mut Vec<u8>) {
|
||||
self.0.ssz_append(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for KzgProof {
|
||||
fn is_ssz_fixed_len() -> bool {
|
||||
<[u8; KZG_PROOF_BYTES_LEN] as Decode>::is_ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn ssz_fixed_len() -> usize {
|
||||
<[u8; KZG_PROOF_BYTES_LEN] as Decode>::ssz_fixed_len()
|
||||
}
|
||||
|
||||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
|
||||
<[u8; KZG_PROOF_BYTES_LEN]>::from_ssz_bytes(bytes).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeHash for KzgProof {
|
||||
fn tree_hash_type() -> tree_hash::TreeHashType {
|
||||
<[u8; KZG_PROOF_BYTES_LEN]>::tree_hash_type()
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user