Skip to content

Commit

Permalink
feat: add block to payloadv1 (#1875)
Browse files Browse the repository at this point in the history
* feat: add block to payloadv1

* take by ref
  • Loading branch information
mattsse authored Dec 31, 2024
1 parent 760a26b commit 125bd7d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/rpc-types-engine/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,35 @@ impl ExecutionPayloadV1 {

Ok(Block { header, body: BlockBody { transactions, ommers: vec![], withdrawals: None } })
}

/// Converts [`alloy_consensus::Block`] to [`ExecutionPayloadV1`].
///
/// 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 [`ExecutionPayloadV1`] using the given block hash.
pub fn from_block_unchecked<T: Encodable2718>(block_hash: B256, block: &Block<T>) -> Self {
let transactions =
block.body.transactions().map(|tx| tx.encoded_2718().into()).collect::<Vec<_>>();
Self {
parent_hash: block.parent_hash,
fee_recipient: block.beneficiary,
state_root: block.state_root,
receipts_root: block.receipts_root,
logs_bloom: block.logs_bloom,
prev_randao: block.mix_hash,
block_number: block.number,
gas_limit: block.gas_limit,
gas_used: block.gas_used,
timestamp: block.timestamp,
base_fee_per_gas: U256::from(block.base_fee_per_gas.unwrap_or_default()),
extra_data: block.header.extra_data.clone(),
block_hash,
transactions,
}
}
}

impl<T: Decodable2718> TryFrom<ExecutionPayloadV1> for Block<T> {
Expand Down

0 comments on commit 125bd7d

Please sign in to comment.