Skip to content

Commit

Permalink
Reorganize deneb primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jan 11, 2024
1 parent 99f5a6d commit 3580c4d
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 154 deletions.
6 changes: 2 additions & 4 deletions parachain/pallets/ethereum-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,10 @@ pub mod pallet {
);

// Checks that we don't skip execution headers, they need to be imported sequentially.
let compact_execution_header: CompactExecutionHeader =
update.execution_header.clone().into();
let latest_execution_state: ExecutionHeaderState = Self::latest_execution_state();
ensure!(
latest_execution_state.block_number == 0 ||
compact_execution_header.block_number ==
update.execution_header.block_number() ==
latest_execution_state.block_number + 1,
Error::<T>::ExecutionHeaderSkippedBlock
);
Expand Down Expand Up @@ -606,7 +604,7 @@ pub mod pallet {

Self::store_execution_header(
update.execution_header.block_hash(),
compact_execution_header,
update.execution_header.clone().into(),
update.header.slot,
block_root,
);
Expand Down
56 changes: 0 additions & 56 deletions parachain/primitives/beacon/src/deneb.rs

This file was deleted.

4 changes: 0 additions & 4 deletions parachain/primitives/beacon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
pub mod bits;
pub mod bls;
pub mod config;
pub mod deneb;
pub mod merkle_proof;
pub mod receipt;
pub mod ssz;
pub mod ssz_deneb;
pub mod types;
pub mod updates;

Expand All @@ -30,7 +28,5 @@ pub use bls::{
prepare_aggregate_signature, prepare_g1_pubkeys, AggregatePublicKey, AggregateSignature,
BlsError, PublicKeyPrepared, SignaturePrepared,
};
pub use deneb::ExecutionPayloadHeaderDeneb;
pub use merkle_proof::verify_merkle_branch;
pub use receipt::verify_receipt_proof;
pub use ssz_deneb::SSZExecutionPayloadHeaderDeneb;
83 changes: 83 additions & 0 deletions parachain/primitives/beacon/src/ssz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,86 @@ pub fn hash_tree_root<T: SimpleSerialize>(mut object: T) -> Result<H256, SimpleS
Err(err) => Err(err.into()),
}
}

pub mod deneb {
use crate::{
config::{EXTRA_DATA_SIZE, FEE_RECIPIENT_SIZE, LOGS_BLOOM_SIZE},
ssz::hash_tree_root,
types::deneb::ExecutionPayloadHeader,
};
use byte_slice_cast::AsByteSlice;
use sp_core::H256;
use ssz_rs::{
prelude::{List, Vector},
Deserialize, DeserializeError, SimpleSerializeError, Sized, U256,
};
use ssz_rs_derive::SimpleSerialize as SimpleSerializeDerive;

This comment has been minimized.

Copy link
@vgeddes

vgeddes Jan 16, 2024

Collaborator

can also just use use super::*; instead of all these imports.


#[derive(Default, SimpleSerializeDerive, Clone, Debug)]
pub struct SSZExecutionPayloadHeader {
pub parent_hash: [u8; 32],
pub fee_recipient: Vector<u8, FEE_RECIPIENT_SIZE>,
pub state_root: [u8; 32],
pub receipts_root: [u8; 32],
pub logs_bloom: Vector<u8, LOGS_BLOOM_SIZE>,
pub prev_randao: [u8; 32],
pub block_number: u64,
pub gas_limit: u64,
pub gas_used: u64,
pub timestamp: u64,
pub extra_data: List<u8, EXTRA_DATA_SIZE>,
pub base_fee_per_gas: U256,
pub block_hash: [u8; 32],
pub transactions_root: [u8; 32],
pub withdrawals_root: [u8; 32],
pub blob_gas_used: u64,
pub excess_blob_gas: u64,
}

impl TryFrom<ExecutionPayloadHeader> for SSZExecutionPayloadHeader {
type Error = SimpleSerializeError;

fn try_from(payload: ExecutionPayloadHeader) -> Result<Self, Self::Error> {
Ok(SSZExecutionPayloadHeader {
parent_hash: payload.parent_hash.to_fixed_bytes(),
fee_recipient: Vector::<u8, FEE_RECIPIENT_SIZE>::try_from(
payload.fee_recipient.to_fixed_bytes().to_vec(),
)
.expect("checked statically; qed"),
state_root: payload.state_root.to_fixed_bytes(),
receipts_root: payload.receipts_root.to_fixed_bytes(),
// Logs bloom bytes size is not constrained, so here we do need to check the
// try_from error
logs_bloom: Vector::<u8, LOGS_BLOOM_SIZE>::try_from(payload.logs_bloom)
.map_err(|(_, err)| err)?,
prev_randao: payload.prev_randao.to_fixed_bytes(),
block_number: payload.block_number,
gas_limit: payload.gas_limit,
gas_used: payload.gas_used,
timestamp: payload.timestamp,
// Extra data bytes size is not constrained, so here we do need to check the
// try_from error
extra_data: List::<u8, EXTRA_DATA_SIZE>::try_from(payload.extra_data)
.map_err(|(_, err)| err)?,
base_fee_per_gas: U256::from_bytes_le(
payload
.base_fee_per_gas
.as_byte_slice()
.try_into()
.expect("checked in prep; qed"),
),
block_hash: payload.block_hash.to_fixed_bytes(),
transactions_root: payload.transactions_root.to_fixed_bytes(),
withdrawals_root: payload.withdrawals_root.to_fixed_bytes(),
blob_gas_used: payload.blob_gas_used,
excess_blob_gas: payload.excess_blob_gas,
})
}
}

impl ExecutionPayloadHeader {
pub fn hash_tree_root(&self) -> Result<H256, SimpleSerializeError> {
hash_tree_root::<SSZExecutionPayloadHeader>(self.clone().try_into()?)
}
}
}
83 changes: 0 additions & 83 deletions parachain/primitives/beacon/src/ssz_deneb.rs

This file was deleted.

83 changes: 76 additions & 7 deletions parachain/primitives/beacon/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ use ssz_rs::SimpleSerializeError;

pub use crate::bits::decompress_sync_committee_bits;

use crate::{
bls::{prepare_g1_pubkeys, prepare_milagro_pubkey, BlsError},
ssz_deneb::SSZExecutionPayloadHeaderDeneb,
ExecutionPayloadHeaderDeneb,
};
use crate::bls::{prepare_g1_pubkeys, prepare_milagro_pubkey, BlsError};
use milagro_bls::PublicKey as PublicKeyPrepared;

pub type ValidatorIndex = u64;
Expand Down Expand Up @@ -401,7 +397,7 @@ pub struct CompactBeaconState {
#[codec(mel_bound())]
pub enum VersionedExecutionPayloadHeader {
Capella(ExecutionPayloadHeader),
Deneb(ExecutionPayloadHeaderDeneb),
Deneb(deneb::ExecutionPayloadHeader),
}

/// Convert VersionedExecutionPayloadHeader to CompactExecutionHeader
Expand All @@ -424,7 +420,7 @@ impl VersionedExecutionPayloadHeader {
execution_payload_header.clone().try_into()?,
),
VersionedExecutionPayloadHeader::Deneb(execution_payload_header) =>
hash_tree_root::<SSZExecutionPayloadHeaderDeneb>(
hash_tree_root::<crate::ssz::deneb::SSZExecutionPayloadHeader>(
execution_payload_header.clone().try_into()?,
),
}
Expand All @@ -438,6 +434,15 @@ impl VersionedExecutionPayloadHeader {
execution_payload_header.block_hash,
}
}

pub fn block_number(&self) -> u64 {
match self {
VersionedExecutionPayloadHeader::Capella(execution_payload_header) =>
execution_payload_header.block_number,
VersionedExecutionPayloadHeader::Deneb(execution_payload_header) =>
execution_payload_header.block_number,
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -564,3 +569,67 @@ pub enum Mode {
Active,
Blocked,
}

pub mod deneb {
use crate::CompactExecutionHeader;
use codec::{Decode, Encode};
use frame_support::{CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_core::{H160, H256, U256};

/// ExecutionPayloadHeader
/// https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/beacon-chain.md#executionpayloadheader
#[derive(
Default, Encode, Decode, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo,
)]
#[cfg_attr(
feature = "std",
derive(Serialize, Deserialize),
serde(deny_unknown_fields, bound(serialize = ""), bound(deserialize = ""))
)]
#[codec(mel_bound())]
pub struct ExecutionPayloadHeader {
pub parent_hash: H256,
pub fee_recipient: H160,
pub state_root: H256,
pub receipts_root: H256,
#[cfg_attr(
feature = "std",
serde(deserialize_with = "crate::serde_utils::from_hex_to_bytes")
)]
pub logs_bloom: Vec<u8>,
pub prev_randao: H256,
pub block_number: u64,
pub gas_limit: u64,
pub gas_used: u64,
pub timestamp: u64,
#[cfg_attr(
feature = "std",
serde(deserialize_with = "crate::serde_utils::from_hex_to_bytes")
)]
pub extra_data: Vec<u8>,
#[cfg_attr(
feature = "std",
serde(deserialize_with = "crate::serde_utils::from_int_to_u256")
)]
pub base_fee_per_gas: U256,
pub block_hash: H256,
pub transactions_root: H256,
pub withdrawals_root: H256,
pub blob_gas_used: u64, // [New in Deneb:EIP4844]
pub excess_blob_gas: u64, // [New in Deneb:EIP4844]
}

impl From<ExecutionPayloadHeader> for CompactExecutionHeader {
fn from(execution_payload: ExecutionPayloadHeader) -> Self {
Self {
parent_hash: execution_payload.parent_hash,
block_number: execution_payload.block_number,
state_root: execution_payload.state_root,
receipts_root: execution_payload.receipts_root,
}
}
}
}

0 comments on commit 3580c4d

Please sign in to comment.