reth_payload_primitives/
traits.rs

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