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};
1112/// 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
17type Primitives: NodePrimitives;
1819/// Returns the built block (sealed)
20fn block(&self) -> &SealedBlock<<Self::Primitives as NodePrimitives>::Block>;
2122/// Returns the fees collected for the built block
23fn fees(&self) -> U256;
2425/// Returns the entire execution data for the built block, if available.
26fn executed_block(&self) -> Option<ExecutedBlockWithTrieUpdates<Self::Primitives>> {
27None28 }
2930/// Returns the EIP-7685 requests for the payload if any.
31fn requests(&self) -> Option<Requests>;
32}
3334/// 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`].
41type RpcPayloadAttributes;
42/// The error type used in [`PayloadBuilderAttributes::try_new`].
43type Error: core::error::Error;
4445/// 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.
48fn try_new(
49 parent: B256,
50 rpc_payload_attributes: Self::RpcPayloadAttributes,
51 version: u8,
52 ) -> Result<Self, Self::Error>
53where
54Self: Sized;
5556/// Returns the [`PayloadId`] for the running payload job.
57fn payload_id(&self) -> PayloadId;
5859/// Returns the parent block hash for the running payload job.
60fn parent(&self) -> B256;
6162/// Returns the timestamp for the running payload job.
63fn timestamp(&self) -> u64;
6465/// Returns the parent beacon block root for the running payload job, if it exists.
66fn parent_beacon_block_root(&self) -> Option<B256>;
6768/// Returns the suggested fee recipient for the running payload job.
69fn suggested_fee_recipient(&self) -> Address;
7071/// Returns the prevrandao field for the running payload job.
72fn prev_randao(&self) -> B256;
7374/// Returns the withdrawals for the running payload job.
75fn withdrawals(&self) -> &Withdrawals;
76}
7778/// 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:
83serde::de::DeserializeOwned + serde::Serialize + fmt::Debug + Clone + Send + Sync + 'static
84{
85/// Returns the timestamp to be used in the payload job.
86fn timestamp(&self) -> u64;
8788/// Returns the withdrawals for the given payload attributes.
89fn withdrawals(&self) -> Option<&Vec<Withdrawal>>;
9091/// Return the parent beacon block root for the payload attributes.
92fn parent_beacon_block_root(&self) -> Option<B256>;
93}
9495impl PayloadAttributesfor EthPayloadAttributes {
96fn timestamp(&self) -> u64 {
97self.timestamp
98 }
99100fn withdrawals(&self) -> Option<&Vec<Withdrawal>> {
101self.withdrawals.as_ref()
102 }
103104fn parent_beacon_block_root(&self) -> Option<B256> {
105self.parent_beacon_block_root
106 }
107}
108109#[cfg(feature = "op")]
110impl PayloadAttributesfor op_alloy_rpc_types_engine::OpPayloadAttributes {
111fn timestamp(&self) -> u64 {
112self.payload_attributes.timestamp
113 }
114115fn withdrawals(&self) -> Option<&Vec<Withdrawal>> {
116self.payload_attributes.withdrawals.as_ref()
117 }
118119fn parent_beacon_block_root(&self) -> Option<B256> {
120self.payload_attributes.parent_beacon_block_root
121 }
122}
123124/// 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.
127fn build(&self, timestamp: u64) -> Attributes;
128}