-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpayload_service.rs
More file actions
271 lines (240 loc) · 9.13 KB
/
payload_service.rs
File metadata and controls
271 lines (240 loc) · 9.13 KB
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
268
269
270
271
use std::sync::Arc;
use alloy_primitives::{Address, U256};
use evolve_ev_reth::EvolvePayloadAttributes;
use eyre::WrapErr;
use reth_basic_payload_builder::{
BuildArguments, BuildOutcome, HeaderForPayload, MissingPayloadBehaviour, PayloadBuilder,
PayloadConfig,
};
use reth_ethereum::{
chainspec::{ChainSpec, ChainSpecProvider},
node::{
api::{payload::PayloadBuilderAttributes, FullNodeTypes, NodeTypes},
builder::{components::PayloadBuilderBuilder, BuilderContext},
},
pool::{PoolTransaction, TransactionPool},
primitives::Header,
TransactionSigned,
};
use reth_payload_builder::{EthBuiltPayload, PayloadBuilderError};
use reth_provider::HeaderProvider;
use reth_revm::cached::CachedReads;
use tokio::runtime::Handle;
use tracing::info;
use crate::{
attributes::EvolveEnginePayloadBuilderAttributes, builder::EvolvePayloadBuilder,
config::EvolvePayloadBuilderConfig, executor::EvolveEvmConfig, node::EvolveEngineTypes,
};
use evolve_ev_reth::config::set_current_block_gas_limit;
/// Evolve payload service builder that integrates with the evolve payload builder.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct EvolvePayloadBuilderBuilder {
config: EvolvePayloadBuilderConfig,
}
impl EvolvePayloadBuilderBuilder {
/// Create a new builder with evolve args.
pub fn new() -> Self {
let config = EvolvePayloadBuilderConfig::new();
info!("Created Evolve payload builder with config: {:?}", config);
Self { config }
}
}
impl Default for EvolvePayloadBuilderBuilder {
fn default() -> Self {
Self::new()
}
}
/// The evolve engine payload builder that integrates with the evolve payload builder.
#[derive(Debug, Clone)]
pub struct EvolveEnginePayloadBuilder<Client>
where
Client: Clone,
{
pub(crate) evolve_builder: Arc<EvolvePayloadBuilder<Client>>,
pub(crate) config: EvolvePayloadBuilderConfig,
}
impl<Node, Pool> PayloadBuilderBuilder<Node, Pool, EvolveEvmConfig> for EvolvePayloadBuilderBuilder
where
Node: FullNodeTypes<
Types: NodeTypes<
Payload = EvolveEngineTypes,
ChainSpec = ChainSpec,
Primitives = reth_ethereum::EthPrimitives,
>,
>,
Pool: TransactionPool<Transaction: PoolTransaction<Consensus = TransactionSigned>>
+ Unpin
+ 'static,
{
type PayloadBuilder = EvolveEnginePayloadBuilder<Node::Provider>;
async fn build_payload_builder(
self,
ctx: &BuilderContext<Node>,
_pool: Pool,
evm_config: EvolveEvmConfig,
) -> eyre::Result<Self::PayloadBuilder> {
let chain_spec = ctx.chain_spec();
let mut config = EvolvePayloadBuilderConfig::from_chain_spec(&chain_spec)
.wrap_err("failed to load evolve config from chain spec")?;
if self.config.base_fee_sink.is_some() {
config.base_fee_sink = self.config.base_fee_sink;
}
config.validate()?;
let evolve_builder = Arc::new(EvolvePayloadBuilder::new(
Arc::new(ctx.provider().clone()),
evm_config,
config.clone(),
));
Ok(EvolveEnginePayloadBuilder {
evolve_builder,
config,
})
}
}
impl<Client> PayloadBuilder for EvolveEnginePayloadBuilder<Client>
where
Client: reth_ethereum::provider::StateProviderFactory
+ ChainSpecProvider<ChainSpec = ChainSpec>
+ HeaderProvider<Header = Header>
+ Clone
+ Send
+ Sync
+ 'static,
{
type Attributes = EvolveEnginePayloadBuilderAttributes;
type BuiltPayload = EthBuiltPayload;
fn try_build(
&self,
args: BuildArguments<Self::Attributes, Self::BuiltPayload>,
) -> Result<BuildOutcome<Self::BuiltPayload>, PayloadBuilderError> {
let BuildArguments {
cached_reads: _,
config,
cancel: _,
best_payload: _,
} = args;
let PayloadConfig {
parent_header,
attributes,
} = config;
info!(
"Evolve engine payload builder: building payload with {} transactions",
attributes.transactions.len()
);
// Convert Engine API attributes to Evolve payload attributes.
// If no gas_limit provided, default to the parent header's gas limit (genesis for first block).
let parent_header_ref = parent_header.as_ref();
let effective_gas_limit = attributes.gas_limit.unwrap_or(parent_header_ref.gas_limit);
// Publish effective gas limit for RPC alignment.
set_current_block_gas_limit(effective_gas_limit);
let mut fee_recipient = attributes.suggested_fee_recipient();
if fee_recipient == Address::ZERO {
if let Some(sink) = self.config.base_fee_sink {
info!(
target: "ev-reth",
fee_sink = ?sink,
"Suggested fee recipient missing; defaulting to base-fee sink"
);
fee_recipient = sink;
}
}
let evolve_attrs = EvolvePayloadAttributes::new(
attributes.transactions.clone(),
Some(effective_gas_limit),
attributes.timestamp(),
attributes.prev_randao(),
fee_recipient,
attributes.parent(),
parent_header_ref.number + 1,
);
// Build the payload using the evolve payload builder - use spawn_blocking for async work.
let evolve_builder = self.evolve_builder.clone();
let parent_header_for_builder = parent_header_ref.clone();
let sealed_block = tokio::task::block_in_place(move || {
Handle::current().block_on(
evolve_builder.build_payload(evolve_attrs, parent_header_for_builder),
)
})
.map_err(PayloadBuilderError::other)?;
info!(
"Evolve engine payload builder: built block with {} transactions, gas used: {}",
sealed_block.transaction_count(),
sealed_block.gas_used
);
// Convert to EthBuiltPayload.
let gas_used = sealed_block.gas_used;
let built_payload = EthBuiltPayload::new(
attributes.payload_id(), // Use the proper payload ID from attributes.
Arc::new(sealed_block),
U256::from(gas_used), // Block gas used.
None, // No blob sidecar for evolve.
);
Ok(BuildOutcome::Better {
payload: built_payload,
cached_reads: CachedReads::default(),
})
}
fn build_empty_payload(
&self,
config: PayloadConfig<Self::Attributes, HeaderForPayload<Self::BuiltPayload>>,
) -> Result<Self::BuiltPayload, PayloadBuilderError> {
let PayloadConfig {
parent_header,
attributes,
} = config;
info!("Evolve engine payload builder: building empty payload");
// Create empty evolve attributes (no transactions).
// If no gas_limit provided, default to the parent header's gas limit (genesis for first block).
let parent_header_ref = parent_header.as_ref();
let effective_gas_limit = attributes.gas_limit.unwrap_or(parent_header_ref.gas_limit);
// Publish effective gas limit for RPC alignment.
set_current_block_gas_limit(effective_gas_limit);
let mut fee_recipient = attributes.suggested_fee_recipient();
if fee_recipient == Address::ZERO {
if let Some(sink) = self.config.base_fee_sink {
info!(
target: "ev-reth",
fee_sink = ?sink,
"Suggested fee recipient missing; defaulting to base-fee sink"
);
fee_recipient = sink;
}
}
let evolve_attrs = EvolvePayloadAttributes::new(
vec![],
Some(effective_gas_limit),
attributes.timestamp(),
attributes.prev_randao(),
fee_recipient,
attributes.parent(),
parent_header_ref.number + 1,
);
// Build empty payload - use spawn_blocking for async work.
let evolve_builder = self.evolve_builder.clone();
let parent_header_for_builder = parent_header_ref.clone();
let sealed_block = tokio::task::block_in_place(move || {
Handle::current().block_on(
evolve_builder.build_payload(evolve_attrs, parent_header_for_builder),
)
})
.map_err(PayloadBuilderError::other)?;
let gas_used = sealed_block.gas_used;
Ok(EthBuiltPayload::new(
attributes.payload_id(),
Arc::new(sealed_block),
U256::from(gas_used),
None,
))
}
/// Determines how to handle a request for a payload that is currently being built.
///
/// This will always await the in-progress job, preventing a race with a new build.
/// This is the recommended behavior to prevent redundant payload builds.
fn on_missing_payload(
&self,
_args: BuildArguments<Self::Attributes, Self::BuiltPayload>,
) -> MissingPayloadBehaviour<Self::BuiltPayload> {
MissingPayloadBehaviour::AwaitInProgress
}
}