Skip to content

Commit

Permalink
sidecar
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Oct 31, 2024
1 parent a44eeef commit b283c74
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
2 changes: 2 additions & 0 deletions crates/rpc-types-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ extern crate alloc;

mod cancun;
pub use cancun::*;
mod prague;
pub use prague::*;
mod sidecar;
pub use sidecar::*;

Expand Down
63 changes: 63 additions & 0 deletions crates/rpc-types-engine/src/prague.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! 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;

/// 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,
/// Target number of blobs in the block.
pub target_blobs_per_block: u64,
}

/// 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))]
pub struct MaybePraguePayloadFields {
fields: Option<PraguePayloadFields>,
}

impl MaybePraguePayloadFields {
/// Returns a new [`MaybePraguePayloadFields`] with no prague fields.
pub const fn none() -> Self {
Self { fields: None }
}

/// Returns a new [`MaybePraguePayloadFields`] with the given prague fields.
pub fn into_inner(self) -> Option<PraguePayloadFields> {
self.fields
}

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

/// Returns a reference to the inner fields.
pub const fn as_ref(&self) -> Option<&PraguePayloadFields> {
self.fields.as_ref()
}
}

impl From<PraguePayloadFields> for MaybePraguePayloadFields {
#[inline]
fn from(fields: PraguePayloadFields) -> Self {
Self { fields: Some(fields) }
}
}

impl From<Option<PraguePayloadFields>> for MaybePraguePayloadFields {
#[inline]
fn from(fields: Option<PraguePayloadFields>) -> Self {
Self { fields }
}
}
28 changes: 20 additions & 8 deletions crates/rpc-types-engine/src/sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Contains helpers for dealing with additional parameters of `newPayload` requests.

use crate::{CancunPayloadFields, MaybeCancunPayloadFields};
use crate::{
CancunPayloadFields, MaybeCancunPayloadFields, MaybePraguePayloadFields, PraguePayloadFields,
};
use alloc::vec::Vec;
use alloy_eips::eip7685::Requests;
use alloy_primitives::B256;
Expand All @@ -15,30 +17,35 @@ 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<Requests>,
prague: MaybePraguePayloadFields,
}

impl ExecutionPayloadSidecar {
/// Returns a new empty instance (pre-cancun, v1, v2)
pub const fn none() -> Self {
Self { cancun: MaybeCancunPayloadFields::none(), prague: None }
Self { cancun: MaybeCancunPayloadFields::none(), prague: MaybePraguePayloadFields::none() }
}

/// Creates a new instance for cancun with the cancun fields for `engine_newPayloadV3`
pub fn v3(cancun: CancunPayloadFields) -> Self {
Self { cancun: cancun.into(), prague: None }
Self { cancun: cancun.into(), prague: MaybePraguePayloadFields::none() }
}

/// Creates a new instance post prague for `engine_newPayloadV4`
pub fn v4(cancun: CancunPayloadFields, requests: Requests) -> Self {
Self { cancun: cancun.into(), prague: Some(requests) }
pub fn v4(cancun: CancunPayloadFields, prague: PraguePayloadFields) -> Self {
Self { cancun: cancun.into(), prague: prague.into() }
}

/// Returns a reference to the [`CancunPayloadFields`].
pub const fn cancun(&self) -> Option<&CancunPayloadFields> {
self.cancun.as_ref()
}

/// Returns a reference to the [`PraguePayloadFields`].
pub const fn prague(&self) -> Option<&PraguePayloadFields> {
self.prague.as_ref()
}

/// Returns the parent beacon block root, if any.
pub fn parent_beacon_block_root(&self) -> Option<B256> {
self.cancun.parent_beacon_block_root()
Expand All @@ -50,7 +57,12 @@ impl ExecutionPayloadSidecar {
}

/// Returns the EIP-7685 requests
pub const fn requests(&self) -> Option<&Requests> {
self.prague.as_ref()
pub fn requests(&self) -> Option<&Requests> {
self.prague.requests()
}

/// Returns the target blobs per block
pub fn target_blobs_per_block(&self) -> Option<u64> {
self.prague.target_blobs_per_block()
}
}

0 comments on commit b283c74

Please sign in to comment.