alloy_rpc_types_engine/
cancun.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Contains types related to the Cancun hardfork that will be used by RPC to communicate with the
//! beacon consensus engine.

use alloc::vec::Vec;

use alloy_primitives::B256;

/// Fields introduced in `engine_newPayloadV3` that are not present in the `ExecutionPayload` RPC
/// object.
///
/// See also:
/// <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#request>
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CancunPayloadFields {
    /// The parent beacon block root.
    pub parent_beacon_block_root: B256,

    /// The expected blob versioned hashes.
    pub versioned_hashes: Vec<B256>,
}

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

impl MaybeCancunPayloadFields {
    /// Returns a new `MaybeCancunPayloadFields` with no cancun fields.
    pub const fn none() -> Self {
        Self { fields: None }
    }

    /// Returns a new `MaybeCancunPayloadFields` with the given cancun fields.
    pub fn into_inner(self) -> Option<CancunPayloadFields> {
        self.fields
    }

    /// Returns the parent beacon block root, if any.
    pub fn parent_beacon_block_root(&self) -> Option<B256> {
        self.fields.as_ref().map(|fields| fields.parent_beacon_block_root)
    }

    /// Returns the blob versioned hashes, if any.
    pub fn versioned_hashes(&self) -> Option<&Vec<B256>> {
        self.fields.as_ref().map(|fields| &fields.versioned_hashes)
    }

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

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

impl From<Option<CancunPayloadFields>> for MaybeCancunPayloadFields {
    #[inline]
    fn from(fields: Option<CancunPayloadFields>) -> Self {
        Self { fields }
    }
}