alloy_rpc_types_engine/
cancun.rs

1//! Contains types related to the Cancun hardfork that will be used by RPC to communicate with the
2//! beacon consensus engine.
3
4use alloc::vec::Vec;
5use alloy_primitives::B256;
6
7/// Fields introduced in `engine_newPayloadV3` that are not present in the `ExecutionPayload` RPC
8/// object.
9///
10/// See also:
11/// <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#request>
12#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
15pub struct CancunPayloadFields {
16    /// The parent beacon block root.
17    pub parent_beacon_block_root: B256,
18
19    /// The expected blob versioned hashes.
20    pub versioned_hashes: Vec<B256>,
21}
22
23impl CancunPayloadFields {
24    /// Returns a new [`CancunPayloadFields`] instance.
25    pub const fn new(parent_beacon_block_root: B256, versioned_hashes: Vec<B256>) -> Self {
26        Self { parent_beacon_block_root, versioned_hashes }
27    }
28}
29
30/// A container type for [CancunPayloadFields] that may or may not be present.
31#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
34pub struct MaybeCancunPayloadFields {
35    fields: Option<CancunPayloadFields>,
36}
37
38impl MaybeCancunPayloadFields {
39    /// Returns a new `MaybeCancunPayloadFields` with no cancun fields.
40    pub const fn none() -> Self {
41        Self { fields: None }
42    }
43
44    /// Consumes `self` and returns the contained [`CancunPayloadFields`], if present.
45    pub fn into_inner(self) -> Option<CancunPayloadFields> {
46        self.fields
47    }
48
49    /// Returns the parent beacon block root, if any.
50    pub fn parent_beacon_block_root(&self) -> Option<B256> {
51        self.fields.as_ref().map(|fields| fields.parent_beacon_block_root)
52    }
53
54    /// Returns the blob versioned hashes, if any.
55    pub fn versioned_hashes(&self) -> Option<&Vec<B256>> {
56        self.fields.as_ref().map(|fields| &fields.versioned_hashes)
57    }
58
59    /// Returns a reference to the inner fields.
60    pub const fn as_ref(&self) -> Option<&CancunPayloadFields> {
61        self.fields.as_ref()
62    }
63}
64
65impl From<CancunPayloadFields> for MaybeCancunPayloadFields {
66    #[inline]
67    fn from(fields: CancunPayloadFields) -> Self {
68        Self { fields: Some(fields) }
69    }
70}
71
72impl From<Option<CancunPayloadFields>> for MaybeCancunPayloadFields {
73    #[inline]
74    fn from(fields: Option<CancunPayloadFields>) -> Self {
75        Self { fields }
76    }
77}