Skip to content

Commit

Permalink
touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Nov 27, 2024
1 parent 6497e05 commit db00324
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
28 changes: 24 additions & 4 deletions crates/eips/src/eip7685.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ impl Requests {
///
/// 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)]
/// needed to simulate the presence of requests without holding actual data.
#[derive(Debug, Clone, PartialEq, Eq, Hash, derive_more::From)]
#[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.
/// Stores a precomputed requests hash, used primarily for testing or mocking because the
/// header only contains the hash.
Hash(B256),
}

Expand All @@ -103,7 +103,27 @@ impl RequestsOrHash {
Self::Hash(precomputed_hash) => *precomputed_hash,
}
}

/// Returns an instance with the [`EMPTY_REQUESTS_HASH`].
pub const fn empty() -> Self {
Self::Hash(EMPTY_REQUESTS_HASH)
}

/// Returns the requests, if any.
pub const fn requests(&self) -> Option<&Requests> {
match self {
Self::Requests(requests) => Some(requests),
Self::Hash(_) => None,
}
}
}

impl Default for RequestsOrHash {
fn default() -> Self {
Self::Requests(Requests::default())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 7 additions & 1 deletion crates/rpc-types-engine/src/cancun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! beacon consensus engine.
use alloc::vec::Vec;

use alloy_primitives::B256;

/// Fields introduced in `engine_newPayloadV3` that are not present in the `ExecutionPayload` RPC
Expand All @@ -20,6 +19,13 @@ pub struct CancunPayloadFields {
pub versioned_hashes: Vec<B256>,
}

impl CancunPayloadFields {
/// Returns a new [`CancunPayloadFields`] instance.
pub const fn new(parent_beacon_block_root: B256, versioned_hashes: Vec<B256>) -> Self {
Self { parent_beacon_block_root, versioned_hashes }
}
}

/// A container type for [CancunPayloadFields] that may or may not be present.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down
23 changes: 20 additions & 3 deletions crates/rpc-types-engine/src/prague.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
//! Contains types related to the Prague hardfork that will be used by RPC to communicate with the
//! beacon consensus engine.
use alloy_eips::eip7685::Requests;
use alloy_eips::eip7685::{Requests, RequestsOrHash};
use alloy_primitives::B256;

/// Fields introduced in `engine_newPayloadV4` that are not present in the `ExecutionPayload` RPC
/// object.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PraguePayloadFields {
/// EIP-7685 requests.
pub requests: Requests,
pub requests: RequestsOrHash,
/// EIP-7742 target number of blobs in the block.
pub target_blobs_per_block: u64,
}

impl PraguePayloadFields {
/// Returns a new [`PraguePayloadFields`] instance.
pub fn new(requests: impl Into<RequestsOrHash>, target_blobs_per_block: u64) -> Self {
Self { requests: requests.into(), target_blobs_per_block }
}
}

/// A container type for [PraguePayloadFields] that may or may not be present.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -34,14 +42,23 @@ impl MaybePraguePayloadFields {

/// Returns the requests, if any.
pub fn requests(&self) -> Option<&Requests> {
self.fields.as_ref().map(|fields| &fields.requests)
self.fields.as_ref().and_then(|fields| fields.requests.requests())
}

/// Returns the target blobs per block, if any.
pub fn target_blobs_per_block(&self) -> Option<u64> {
self.fields.as_ref().map(|fields| fields.target_blobs_per_block)
}

/// 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<B256> {
self.fields.as_ref().map(|fields| fields.requests.requests_hash())
}

/// Returns a reference to the inner fields.
pub const fn as_ref(&self) -> Option<&PraguePayloadFields> {
self.fields.as_ref()
Expand Down
15 changes: 7 additions & 8 deletions crates/rpc-types-engine/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
CancunPayloadFields, MaybeCancunPayloadFields, MaybePraguePayloadFields, PraguePayloadFields,
};
use alloc::vec::Vec;
use alloy_eips::eip7685::{Requests, RequestsOrHash};
use alloy_eips::eip7685::Requests;
use alloy_primitives::B256;

/// Container type for all available additional `newPayload` request parameters that are not present
Expand Down Expand Up @@ -57,12 +57,11 @@ impl ExecutionPayloadSidecar {
}

/// Returns the EIP-7685 requests
pub const fn requests(&self) -> Option<&Requests> {
if let Some(RequestsOrHash::Requests(ref requests)) = self.prague {
Some(requests)
} else {
None
}
///
/// Note: if the [`PraguePayloadFields`] only contains the requests hash this will return
/// `None`.
pub fn requests(&self) -> Option<&Requests> {
self.prague.requests()
}

/// Calculates or retrieves the requests hash.
Expand All @@ -71,7 +70,7 @@ impl ExecutionPayloadSidecar {
/// dynamically.
/// - If it contains a precomputed hash (used for testing), it returns that hash directly.
pub fn requests_hash(&self) -> Option<B256> {
self.prague.as_ref().map(|hash| hash.requests_hash())
self.prague.requests_hash()
}

/// Returns the target blobs per block
Expand Down

0 comments on commit db00324

Please sign in to comment.