Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eips): Make prague field an enum #1574

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions crates/eips/src/eip7685.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,55 @@ 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, 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 or mocking because the
/// header only contains the hash.
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,
}
}

/// 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
12 changes: 12 additions & 0 deletions crates/rpc-types-engine/src/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,22 @@ impl ExecutionPayloadSidecar {
}

/// Returns the EIP-7685 requests
///
/// 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.
///
/// - 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.prague.requests_hash()
}

DoTheBestToGetTheBest marked this conversation as resolved.
Show resolved Hide resolved
/// Returns the target blobs per block
pub fn target_blobs_per_block(&self) -> Option<u64> {
self.prague.target_blobs_per_block()
Expand Down