reth_basic_payload_builder/
stack.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use crate::{
    BuildArguments, BuildOutcome, PayloadBuilder, PayloadBuilderAttributes, PayloadBuilderError,
    PayloadConfig,
};

use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::{Address, B256, U256};
use reth_payload_builder::PayloadId;
use reth_payload_primitives::BuiltPayload;
use reth_primitives::SealedBlock;

use alloy_eips::eip7685::Requests;
use std::{error::Error, fmt};

/// hand rolled Either enum to handle two builder types
#[derive(Debug, Clone)]
pub enum Either<L, R> {
    /// left variant
    Left(L),
    /// right variant
    Right(R),
}

impl<L, R> fmt::Display for Either<L, R>
where
    L: fmt::Display,
    R: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Left(l) => write!(f, "Left: {}", l),
            Self::Right(r) => write!(f, "Right: {}", r),
        }
    }
}

impl<L, R> Error for Either<L, R>
where
    L: Error + 'static,
    R: Error + 'static,
{
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Left(l) => Some(l),
            Self::Right(r) => Some(r),
        }
    }
}

impl<L, R> PayloadBuilderAttributes for Either<L, R>
where
    L: PayloadBuilderAttributes,
    R: PayloadBuilderAttributes,
    L::Error: Error + 'static,
    R::Error: Error + 'static,
{
    type RpcPayloadAttributes = Either<L::RpcPayloadAttributes, R::RpcPayloadAttributes>;
    type Error = Either<L::Error, R::Error>;

    fn try_new(
        parent: B256,
        rpc_payload_attributes: Self::RpcPayloadAttributes,
        version: u8,
    ) -> Result<Self, Self::Error> {
        match rpc_payload_attributes {
            Either::Left(attr) => {
                L::try_new(parent, attr, version).map(Either::Left).map_err(Either::Left)
            }
            Either::Right(attr) => {
                R::try_new(parent, attr, version).map(Either::Right).map_err(Either::Right)
            }
        }
    }

    fn payload_id(&self) -> PayloadId {
        match self {
            Self::Left(l) => l.payload_id(),
            Self::Right(r) => r.payload_id(),
        }
    }

    fn parent(&self) -> B256 {
        match self {
            Self::Left(l) => l.parent(),
            Self::Right(r) => r.parent(),
        }
    }

    fn timestamp(&self) -> u64 {
        match self {
            Self::Left(l) => l.timestamp(),
            Self::Right(r) => r.timestamp(),
        }
    }

    fn parent_beacon_block_root(&self) -> Option<B256> {
        match self {
            Self::Left(l) => l.parent_beacon_block_root(),
            Self::Right(r) => r.parent_beacon_block_root(),
        }
    }

    fn suggested_fee_recipient(&self) -> Address {
        match self {
            Self::Left(l) => l.suggested_fee_recipient(),
            Self::Right(r) => r.suggested_fee_recipient(),
        }
    }

    fn prev_randao(&self) -> B256 {
        match self {
            Self::Left(l) => l.prev_randao(),
            Self::Right(r) => r.prev_randao(),
        }
    }

    fn withdrawals(&self) -> &Withdrawals {
        match self {
            Self::Left(l) => l.withdrawals(),
            Self::Right(r) => r.withdrawals(),
        }
    }
}

/// this structure enables the chaining of multiple `PayloadBuilder` implementations,
/// creating a hierarchical fallback system. It's designed to be nestable, allowing
/// for complex builder arrangements like `Stack<Stack<A, B>, C>` with different
#[derive(Debug)]
pub struct PayloadBuilderStack<L, R> {
    left: L,
    right: R,
}

impl<L, R> PayloadBuilderStack<L, R> {
    /// Creates a new `PayloadBuilderStack` with the given left and right builders.
    pub const fn new(left: L, right: R) -> Self {
        Self { left, right }
    }
}

impl<L, R> Clone for PayloadBuilderStack<L, R>
where
    L: Clone,
    R: Clone,
{
    fn clone(&self) -> Self {
        Self::new(self.left.clone(), self.right.clone())
    }
}

impl<L, R> BuiltPayload for Either<L, R>
where
    L: BuiltPayload,
    R: BuiltPayload,
{
    fn block(&self) -> &SealedBlock {
        match self {
            Self::Left(l) => l.block(),
            Self::Right(r) => r.block(),
        }
    }

    fn fees(&self) -> U256 {
        match self {
            Self::Left(l) => l.fees(),
            Self::Right(r) => r.fees(),
        }
    }

    fn requests(&self) -> Option<Requests> {
        match self {
            Self::Left(l) => l.requests(),
            Self::Right(r) => r.requests(),
        }
    }
}

impl<L, R, Pool, Client> PayloadBuilder<Pool, Client> for PayloadBuilderStack<L, R>
where
    L: PayloadBuilder<Pool, Client> + Unpin + 'static,
    R: PayloadBuilder<Pool, Client> + Unpin + 'static,
    Client: Clone,
    Pool: Clone,
    L::Attributes: Unpin + Clone,
    R::Attributes: Unpin + Clone,
    L::BuiltPayload: Unpin + Clone,
    R::BuiltPayload: Unpin + Clone,
    <<L as PayloadBuilder<Pool, Client>>::Attributes as PayloadBuilderAttributes>::Error: 'static,
    <<R as PayloadBuilder<Pool, Client>>::Attributes as PayloadBuilderAttributes>::Error: 'static,
{
    type Attributes = Either<L::Attributes, R::Attributes>;
    type BuiltPayload = Either<L::BuiltPayload, R::BuiltPayload>;

    fn try_build(
        &self,
        args: BuildArguments<Pool, Client, Self::Attributes, Self::BuiltPayload>,
    ) -> Result<BuildOutcome<Self::BuiltPayload>, PayloadBuilderError> {
        match args.config.attributes {
            Either::Left(ref left_attr) => {
                let left_args: BuildArguments<Pool, Client, L::Attributes, L::BuiltPayload> =
                    BuildArguments {
                        client: args.client.clone(),
                        pool: args.pool.clone(),
                        cached_reads: args.cached_reads.clone(),
                        config: PayloadConfig {
                            parent_header: args.config.parent_header.clone(),
                            attributes: left_attr.clone(),
                        },
                        cancel: args.cancel.clone(),
                        best_payload: args.best_payload.clone().and_then(|payload| {
                            if let Either::Left(p) = payload {
                                Some(p)
                            } else {
                                None
                            }
                        }),
                    };

                self.left.try_build(left_args).map(|out| out.map_payload(Either::Left))
            }
            Either::Right(ref right_attr) => {
                let right_args = BuildArguments {
                    client: args.client.clone(),
                    pool: args.pool.clone(),
                    cached_reads: args.cached_reads.clone(),
                    config: PayloadConfig {
                        parent_header: args.config.parent_header.clone(),
                        attributes: right_attr.clone(),
                    },
                    cancel: args.cancel.clone(),
                    best_payload: args.best_payload.clone().and_then(|payload| {
                        if let Either::Right(p) = payload {
                            Some(p)
                        } else {
                            None
                        }
                    }),
                };

                self.right.try_build(right_args).map(|out| out.map_payload(Either::Right))
            }
        }
    }

    fn build_empty_payload(
        &self,
        client: &Client,
        config: PayloadConfig<Self::Attributes>,
    ) -> Result<Self::BuiltPayload, PayloadBuilderError> {
        match config.attributes {
            Either::Left(left_attr) => {
                let left_config = PayloadConfig {
                    parent_header: config.parent_header.clone(),
                    attributes: left_attr,
                };
                self.left.build_empty_payload(client, left_config).map(Either::Left)
            }
            Either::Right(right_attr) => {
                let right_config = PayloadConfig {
                    parent_header: config.parent_header.clone(),
                    attributes: right_attr,
                };
                self.right.build_empty_payload(client, right_config).map(Either::Right)
            }
        }
    }
}