Skip to content

Commit

Permalink
feat: add tryfrom payloadv2 + v3 for block (#1853)
Browse files Browse the repository at this point in the history
* feat: add tryfrom payloadv2 for block

* feat: add tryfrom payloadv3 for block

* docs
  • Loading branch information
mattsse authored Dec 27, 2024
1 parent bbab94f commit ccfb974
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions crates/rpc-types-engine/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,29 @@ impl ExecutionPayloadV2 {
pub const fn timestamp(&self) -> u64 {
self.payload_inner.timestamp
}

/// Converts [`ExecutionPayloadV2`] to [`Block`].
///
/// This performs the same conversion as the underlying V1 payload, but calculates the
/// withdrawals root and adds withdrawals.
///
/// See also [`ExecutionPayloadV1::try_into_block`].
pub fn try_into_block<T: Decodable2718>(self) -> Result<Block<T>, PayloadError> {
let mut base_sealed_block = self.payload_inner.try_into_block()?;
let withdrawals_root =
alloy_consensus::proofs::calculate_withdrawals_root(&self.withdrawals);
base_sealed_block.body.withdrawals = Some(self.withdrawals.into());
base_sealed_block.header.withdrawals_root = Some(withdrawals_root);
Ok(base_sealed_block)
}
}

impl<T: Decodable2718> TryFrom<ExecutionPayloadV2> for Block<T> {
type Error = PayloadError;

fn try_from(value: ExecutionPayloadV2) -> Result<Self, Self::Error> {
value.try_into_block()
}
}

#[cfg(feature = "ssz")]
Expand Down Expand Up @@ -422,6 +445,29 @@ impl ExecutionPayloadV3 {
pub const fn timestamp(&self) -> u64 {
self.payload_inner.payload_inner.timestamp
}

/// Converts [`ExecutionPayloadV3`] to [`Block`].
///
/// This performs the same conversion as the underlying V2 payload, but inserts the blob gas
/// used and excess blob gas.
///
/// See also [`ExecutionPayloadV2::try_into_block`].
pub fn try_into_block<T: Decodable2718>(self) -> Result<Block<T>, PayloadError> {
let mut base_block = self.payload_inner.try_into_block()?;

base_block.header.blob_gas_used = Some(self.blob_gas_used);
base_block.header.excess_blob_gas = Some(self.excess_blob_gas);

Ok(base_block)
}
}

impl<T: Decodable2718> TryFrom<ExecutionPayloadV3> for Block<T> {
type Error = PayloadError;

fn try_from(value: ExecutionPayloadV3) -> Result<Self, Self::Error> {
value.try_into_block()
}
}

#[cfg(feature = "ssz")]
Expand Down

0 comments on commit ccfb974

Please sign in to comment.