reth_payload_primitives/
traits.rs

1use alloy_eips::{
2    eip4895::{Withdrawal, Withdrawals},
3    eip7685::Requests,
4};
5use alloy_primitives::{Address, B256, U256};
6use alloy_rpc_types_engine::{PayloadAttributes as EthPayloadAttributes, PayloadId};
7use reth_chain_state::ExecutedBlock;
8use reth_primitives::{EthPrimitives, NodePrimitives, SealedBlock};
9
10/// Represents a built payload type that contains a built [`SealedBlock`] and can be converted into
11/// engine API execution payloads.
12pub trait BuiltPayload<N: NodePrimitives = EthPrimitives>: Send + Sync + std::fmt::Debug {
13    /// Returns the built block (sealed)
14    fn block(&self) -> &SealedBlock<N::BlockHeader, N::BlockBody>;
15
16    /// Returns the fees collected for the built block
17    fn fees(&self) -> U256;
18
19    /// Returns the entire execution data for the built block, if available.
20    fn executed_block(&self) -> Option<ExecutedBlock<N>> {
21        None
22    }
23
24    /// Returns the EIP-7865 requests for the payload if any.
25    fn requests(&self) -> Option<Requests>;
26}
27
28/// This can be implemented by types that describe a currently running payload job.
29///
30/// This is used as a conversion type, transforming a payload attributes type that the engine API
31/// receives, into a type that the payload builder can use.
32pub trait PayloadBuilderAttributes: Send + Sync + std::fmt::Debug {
33    /// The payload attributes that can be used to construct this type. Used as the argument in
34    /// [`PayloadBuilderAttributes::try_new`].
35    type RpcPayloadAttributes;
36    /// The error type used in [`PayloadBuilderAttributes::try_new`].
37    type Error: core::error::Error;
38
39    /// Creates a new payload builder for the given parent block and the attributes.
40    ///
41    /// Derives the unique [`PayloadId`] for the given parent, attributes and version.
42    fn try_new(
43        parent: B256,
44        rpc_payload_attributes: Self::RpcPayloadAttributes,
45        version: u8,
46    ) -> Result<Self, Self::Error>
47    where
48        Self: Sized;
49
50    /// Returns the [`PayloadId`] for the running payload job.
51    fn payload_id(&self) -> PayloadId;
52
53    /// Returns the parent block hash for the running payload job.
54    fn parent(&self) -> B256;
55
56    /// Returns the timestamp for the running payload job.
57    fn timestamp(&self) -> u64;
58
59    /// Returns the parent beacon block root for the running payload job, if it exists.
60    fn parent_beacon_block_root(&self) -> Option<B256>;
61
62    /// Returns the suggested fee recipient for the running payload job.
63    fn suggested_fee_recipient(&self) -> Address;
64
65    /// Returns the prevrandao field for the running payload job.
66    fn prev_randao(&self) -> B256;
67
68    /// Returns the withdrawals for the running payload job.
69    fn withdrawals(&self) -> &Withdrawals;
70}
71
72/// The execution payload attribute type the CL node emits via the engine API.
73/// This trait should be implemented by types that could be used to spawn a payload job.
74///
75/// This type is emitted as part of the forkchoiceUpdated call
76pub trait PayloadAttributes:
77    serde::de::DeserializeOwned + serde::Serialize + std::fmt::Debug + Clone + Send + Sync + 'static
78{
79    /// Returns the timestamp to be used in the payload job.
80    fn timestamp(&self) -> u64;
81
82    /// Returns the withdrawals for the given payload attributes.
83    fn withdrawals(&self) -> Option<&Vec<Withdrawal>>;
84
85    /// Return the parent beacon block root for the payload attributes.
86    fn parent_beacon_block_root(&self) -> Option<B256>;
87}
88
89impl PayloadAttributes for EthPayloadAttributes {
90    fn timestamp(&self) -> u64 {
91        self.timestamp
92    }
93
94    fn withdrawals(&self) -> Option<&Vec<Withdrawal>> {
95        self.withdrawals.as_ref()
96    }
97
98    fn parent_beacon_block_root(&self) -> Option<B256> {
99        self.parent_beacon_block_root
100    }
101}
102
103#[cfg(feature = "op")]
104impl PayloadAttributes for op_alloy_rpc_types_engine::OpPayloadAttributes {
105    fn timestamp(&self) -> u64 {
106        self.payload_attributes.timestamp
107    }
108
109    fn withdrawals(&self) -> Option<&Vec<Withdrawal>> {
110        self.payload_attributes.withdrawals.as_ref()
111    }
112
113    fn parent_beacon_block_root(&self) -> Option<B256> {
114        self.payload_attributes.parent_beacon_block_root
115    }
116}
117
118/// A builder that can return the current payload attribute.
119pub trait PayloadAttributesBuilder<Attributes>: Send + Sync + 'static {
120    /// Return a new payload attribute from the builder.
121    fn build(&self, timestamp: u64) -> Attributes;
122}