reth_payload_primitives/
payload.rs

1use crate::{MessageValidationKind, PayloadAttributes};
2use alloy_eips::eip4895::Withdrawal;
3use alloy_primitives::B256;
4use alloy_rpc_types_engine::ExecutionPayload;
5
6/// Either an [`ExecutionPayload`] or a types that implements the [`PayloadAttributes`] trait.
7///
8/// This is a helper type to unify pre-validation of version specific fields of the engine API.
9#[derive(Debug)]
10pub enum PayloadOrAttributes<'a, Attributes> {
11    /// An [`ExecutionPayload`] and optional parent beacon block root.
12    ExecutionPayload {
13        /// The inner execution payload
14        payload: &'a ExecutionPayload,
15        /// The parent beacon block root
16        parent_beacon_block_root: Option<B256>,
17    },
18    /// A payload attributes type.
19    PayloadAttributes(&'a Attributes),
20}
21
22impl<'a, Attributes> PayloadOrAttributes<'a, Attributes> {
23    /// Construct a [`PayloadOrAttributes`] from an [`ExecutionPayload`] and optional parent beacon
24    /// block root.
25    pub const fn from_execution_payload(
26        payload: &'a ExecutionPayload,
27        parent_beacon_block_root: Option<B256>,
28    ) -> Self {
29        Self::ExecutionPayload { payload, parent_beacon_block_root }
30    }
31
32    /// Construct a [`PayloadOrAttributes::PayloadAttributes`] variant
33    pub const fn from_attributes(attributes: &'a Attributes) -> Self {
34        Self::PayloadAttributes(attributes)
35    }
36}
37
38impl<Attributes> PayloadOrAttributes<'_, Attributes>
39where
40    Attributes: PayloadAttributes,
41{
42    /// Return the withdrawals for the payload or attributes.
43    pub fn withdrawals(&self) -> Option<&Vec<Withdrawal>> {
44        match self {
45            Self::ExecutionPayload { payload, .. } => payload.withdrawals(),
46            Self::PayloadAttributes(attributes) => attributes.withdrawals(),
47        }
48    }
49
50    /// Return the timestamp for the payload or attributes.
51    pub fn timestamp(&self) -> u64 {
52        match self {
53            Self::ExecutionPayload { payload, .. } => payload.timestamp(),
54            Self::PayloadAttributes(attributes) => attributes.timestamp(),
55        }
56    }
57
58    /// Return the parent beacon block root for the payload or attributes.
59    pub fn parent_beacon_block_root(&self) -> Option<B256> {
60        match self {
61            Self::ExecutionPayload { parent_beacon_block_root, .. } => *parent_beacon_block_root,
62            Self::PayloadAttributes(attributes) => attributes.parent_beacon_block_root(),
63        }
64    }
65
66    /// Return a [`MessageValidationKind`] for the payload or attributes.
67    pub const fn message_validation_kind(&self) -> MessageValidationKind {
68        match self {
69            Self::ExecutionPayload { .. } => MessageValidationKind::Payload,
70            Self::PayloadAttributes(_) => MessageValidationKind::PayloadAttributes,
71        }
72    }
73}
74
75impl<'a, AttributesType> From<&'a AttributesType> for PayloadOrAttributes<'a, AttributesType>
76where
77    AttributesType: PayloadAttributes,
78{
79    fn from(attributes: &'a AttributesType) -> Self {
80        Self::PayloadAttributes(attributes)
81    }
82}