Skip to content

Commit

Permalink
feat: add additional conversion fn (#1881)
Browse files Browse the repository at this point in the history
* feat: add additional conversion fn

* add v3 conversion
  • Loading branch information
mattsse authored Jan 3, 2025
1 parent 6e49ed0 commit ccf0fd3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions crates/rpc-types-engine/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,34 @@ pub struct ExecutionPayloadV2 {
}

impl ExecutionPayloadV2 {
/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV2`].
///
/// See also [`ExecutionPayloadV1::from_block_unchecked`].
///
/// If the block does not have any withdrawals, an empty vector is used.
///
/// Note: This re-calculates the block hash.
pub fn from_block_slow<T: Encodable2718>(block: &Block<T>) -> Self {
Self::from_block_unchecked(block.hash_slow(), block)
}

/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV2`] using the given block hash.
///
/// See also [`ExecutionPayloadV1::from_block_unchecked`].
///
/// If the block does not have any withdrawals, an empty vector is used.
pub fn from_block_unchecked<T: Encodable2718>(block_hash: B256, block: &Block<T>) -> Self {
Self {
withdrawals: block
.body
.withdrawals
.clone()
.map(Withdrawals::into_inner)
.unwrap_or_default(),
payload_inner: ExecutionPayloadV1::from_block_unchecked(block_hash, block),
}
}

/// Returns the timestamp for the execution payload.
pub const fn timestamp(&self) -> u64 {
self.payload_inner.timestamp
Expand Down Expand Up @@ -509,6 +537,26 @@ pub struct ExecutionPayloadV3 {
}

impl ExecutionPayloadV3 {
/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV3`].
///
/// See also [`ExecutionPayloadV2::from_block_unchecked`].
///
/// Note: This re-calculates the block hash.
pub fn from_block_slow<T: Encodable2718>(block: &Block<T>) -> Self {
Self::from_block_unchecked(block.hash_slow(), block)
}

/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV3`] using the given block hash.
///
/// See also [`ExecutionPayloadV2::from_block_unchecked`].
pub fn from_block_unchecked<T: Encodable2718>(block_hash: B256, block: &Block<T>) -> Self {
Self {
blob_gas_used: block.blob_gas_used.unwrap_or_default(),
excess_blob_gas: block.excess_blob_gas.unwrap_or_default(),
payload_inner: ExecutionPayloadV2::from_block_unchecked(block_hash, block),
}
}

/// Returns the withdrawals for the payload.
pub const fn withdrawals(&self) -> &Vec<Withdrawal> {
&self.payload_inner.withdrawals
Expand Down

0 comments on commit ccf0fd3

Please sign in to comment.