reth_payload_primitives/
traits.rs1use crate::PayloadBuilderError;
4use alloc::{boxed::Box, vec::Vec};
5use alloy_eips::{
6 eip4895::{Withdrawal, Withdrawals},
7 eip7685::Requests,
8};
9use alloy_primitives::{Address, B256, U256};
10use alloy_rpc_types_engine::{PayloadAttributes as EthPayloadAttributes, PayloadId};
11use core::fmt;
12use reth_chain_state::ExecutedBlockWithTrieUpdates;
13use reth_primitives_traits::{NodePrimitives, SealedBlock, SealedHeader};
14
15#[auto_impl::auto_impl(&, Arc)]
20pub trait BuiltPayload: Send + Sync + fmt::Debug {
21 type Primitives: NodePrimitives;
23
24 fn block(&self) -> &SealedBlock<<Self::Primitives as NodePrimitives>::Block>;
26
27 fn fees(&self) -> U256;
29
30 fn executed_block(&self) -> Option<ExecutedBlockWithTrieUpdates<Self::Primitives>> {
34 None
35 }
36
37 fn requests(&self) -> Option<Requests>;
42}
43
44pub trait PayloadBuilderAttributes: Send + Sync + Unpin + fmt::Debug + 'static {
49 type RpcPayloadAttributes: Send + Sync + 'static;
51 type Error: core::error::Error + Send + Sync + 'static;
53
54 fn try_new(
59 parent: B256,
60 rpc_payload_attributes: Self::RpcPayloadAttributes,
61 version: u8,
62 ) -> Result<Self, Self::Error>
63 where
64 Self: Sized;
65
66 fn payload_id(&self) -> PayloadId;
68
69 fn parent(&self) -> B256;
71
72 fn timestamp(&self) -> u64;
74
75 fn parent_beacon_block_root(&self) -> Option<B256>;
79
80 fn suggested_fee_recipient(&self) -> Address;
82
83 fn prev_randao(&self) -> B256;
85
86 fn withdrawals(&self) -> &Withdrawals;
88}
89
90pub trait PayloadAttributes:
95 serde::de::DeserializeOwned + serde::Serialize + fmt::Debug + Clone + Send + Sync + 'static
96{
97 fn timestamp(&self) -> u64;
99
100 fn withdrawals(&self) -> Option<&Vec<Withdrawal>>;
104
105 fn parent_beacon_block_root(&self) -> Option<B256>;
109}
110
111impl PayloadAttributes for EthPayloadAttributes {
112 fn timestamp(&self) -> u64 {
113 self.timestamp
114 }
115
116 fn withdrawals(&self) -> Option<&Vec<Withdrawal>> {
117 self.withdrawals.as_ref()
118 }
119
120 fn parent_beacon_block_root(&self) -> Option<B256> {
121 self.parent_beacon_block_root
122 }
123}
124
125#[cfg(feature = "op")]
126impl PayloadAttributes for op_alloy_rpc_types_engine::OpPayloadAttributes {
127 fn timestamp(&self) -> u64 {
128 self.payload_attributes.timestamp
129 }
130
131 fn withdrawals(&self) -> Option<&Vec<Withdrawal>> {
132 self.payload_attributes.withdrawals.as_ref()
133 }
134
135 fn parent_beacon_block_root(&self) -> Option<B256> {
136 self.payload_attributes.parent_beacon_block_root
137 }
138}
139
140pub trait PayloadAttributesBuilder<Attributes>: Send + Sync + 'static {
145 fn build(&self, timestamp: u64) -> Attributes;
147}
148
149impl<Attributes, F> PayloadAttributesBuilder<Attributes> for F
150where
151 F: Fn(u64) -> Attributes + Send + Sync + 'static,
152{
153 fn build(&self, timestamp: u64) -> Attributes {
154 self(timestamp)
155 }
156}
157
158impl<Attributes, L, R> PayloadAttributesBuilder<Attributes> for either::Either<L, R>
159where
160 L: PayloadAttributesBuilder<Attributes>,
161 R: PayloadAttributesBuilder<Attributes>,
162{
163 fn build(&self, timestamp: u64) -> Attributes {
164 match self {
165 Self::Left(l) => l.build(timestamp),
166 Self::Right(r) => r.build(timestamp),
167 }
168 }
169}
170
171impl<Attributes> PayloadAttributesBuilder<Attributes>
172 for Box<dyn PayloadAttributesBuilder<Attributes>>
173where
174 Attributes: 'static,
175{
176 fn build(&self, timestamp: u64) -> Attributes {
177 self.as_ref().build(timestamp)
178 }
179}
180
181pub trait BuildNextEnv<Attributes, Header, Ctx>: Sized {
185 fn build_next_env(
187 attributes: &Attributes,
188 parent: &SealedHeader<Header>,
189 ctx: &Ctx,
190 ) -> Result<Self, PayloadBuilderError>;
191}