diff --git a/crates/eips/src/eip7685.rs b/crates/eips/src/eip7685.rs index 0ffe72e1b90..6b33a048411 100644 --- a/crates/eips/src/eip7685.rs +++ b/crates/eips/src/eip7685.rs @@ -77,6 +77,35 @@ impl Requests { } } +/// A list of requests or a precomputed requests hash. +/// +/// For testing purposes, the `Hash` variant stores a precomputed requests hash. This can be useful +/// when the exact contents of the requests are unnecessary, and only a consistent hash value is +/// needed to simulate the presence of requests without holding actual data. + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub enum RequestsOrHash { + /// Stores a list of requests, allowing for dynamic requests hash calculation. + Requests(Requests), + /// Stores a precomputed requests hash, used primarily for testing. + Hash(B256), +} + +impl RequestsOrHash { + /// Returns the requests hash for the enum instance. + /// + /// - If the instance contains a list of requests, this function calculates the hash using + /// `requests_hash` of the [`Requests`] struct. + /// - If it contains a precomputed hash, it returns that hash directly. + #[cfg(feature = "sha2")] + pub fn requests_hash(&self) -> B256 { + match self { + Self::Requests(requests) => requests.requests_hash(), + Self::Hash(precomputed_hash) => *precomputed_hash, + } + } +} #[cfg(test)] mod tests { use super::*; diff --git a/crates/rpc-types-engine/src/sidecar.rs b/crates/rpc-types-engine/src/sidecar.rs index a640508bcd8..387579b801c 100644 --- a/crates/rpc-types-engine/src/sidecar.rs +++ b/crates/rpc-types-engine/src/sidecar.rs @@ -2,7 +2,7 @@ use crate::{CancunPayloadFields, MaybeCancunPayloadFields}; use alloc::vec::Vec; -use alloy_eips::eip7685::Requests; +use alloy_eips::eip7685::{Requests, RequestsOrHash}; use alloy_primitives::B256; /// Container type for all available additional `newPayload` request parameters that are not present @@ -15,7 +15,7 @@ pub struct ExecutionPayloadSidecar { cancun: MaybeCancunPayloadFields, /// The EIP-7685 requests provided as additional request params to `engine_newPayloadV4` that /// are not present in the `ExecutionPayload`. - prague: Option, + prague: Option, } impl ExecutionPayloadSidecar { @@ -30,7 +30,7 @@ impl ExecutionPayloadSidecar { } /// Creates a new instance post prague for `engine_newPayloadV4` - pub fn v4(cancun: CancunPayloadFields, requests: Requests) -> Self { + pub fn v4(cancun: CancunPayloadFields, requests: RequestsOrHash) -> Self { Self { cancun: cancun.into(), prague: Some(requests) } } @@ -51,6 +51,19 @@ impl ExecutionPayloadSidecar { /// Returns the EIP-7685 requests pub const fn requests(&self) -> Option<&Requests> { - self.prague.as_ref() + if let Some(RequestsOrHash::Requests(ref requests)) = self.prague { + Some(requests) + } else { + None + } + } + + /// Calculates or retrieves the requests hash. + /// + /// - If the `prague` field contains a list of requests, it calculates the requests hash + /// dynamically. + /// - If it contains a precomputed hash (used for testing), it returns that hash directly. + pub fn requests_hash(&self) -> Option { + self.prague.as_ref().map(|hash| hash.requests_hash()) } }