@@ -2,8 +2,8 @@ use alloy_consensus::TxEip1559;
2
2
use alloy_eips:: Encodable2718 ;
3
3
use alloy_evm:: { Database , Evm } ;
4
4
use alloy_op_evm:: OpEvm ;
5
- use alloy_primitives:: { Address , Bytes , TxKind , U256 } ;
6
- use alloy_sol_types:: { SolCall , SolError , sol} ;
5
+ use alloy_primitives:: { Address , B256 , TxKind } ;
6
+ use alloy_sol_types:: { Error , SolCall , SolEvent , SolInterface , sol} ;
7
7
use core:: fmt:: Debug ;
8
8
use op_alloy_consensus:: OpTypedTransaction ;
9
9
use op_revm:: OpHaltReason ;
@@ -21,7 +21,7 @@ use tracing::warn;
21
21
use crate :: {
22
22
builders:: {
23
23
BuilderTransactionCtx , BuilderTransactionError , BuilderTransactions ,
24
- builder_tx:: { BuilderTxBase , get_nonce} ,
24
+ builder_tx:: { BuilderTxBase , get_nonce, log_exists } ,
25
25
context:: OpPayloadBuilderCtx ,
26
26
flashblocks:: payload:: FlashblocksExtraCtx ,
27
27
} ,
@@ -33,62 +33,34 @@ use crate::{
33
33
sol ! (
34
34
// From https://github.com/Uniswap/flashblocks_number_contract/blob/main/src/FlashblockNumber.sol
35
35
#[ sol( rpc, abi) ]
36
+ #[ derive( Debug ) ]
36
37
interface IFlashblockNumber {
37
38
function incrementFlashblockNumber( ) external;
38
- }
39
39
40
- // @notice Emitted when flashblock index is incremented
41
- // @param newFlashblockIndex The new flashblock index (0-indexed within each L2 block)
42
- event FlashblockIncremented ( uint256 newFlashblockIndex) ;
40
+ // @notice Emitted when flashblock index is incremented
41
+ // @param newFlashblockIndex The new flashblock index (0-indexed within each L2 block)
42
+ event FlashblockIncremented ( uint256 newFlashblockIndex) ;
43
43
44
- /// -----------------------------------------------------------------------
45
- /// Errors
46
- /// -----------------------------------------------------------------------
47
- error NonBuilderAddress ( address addr) ;
48
- error MismatchedFlashblockNumber ( uint256 expectedFlashblockNumber, uint256 actualFlashblockNumber) ;
44
+ /// -----------------------------------------------------------------------
45
+ /// Errors
46
+ /// -----------------------------------------------------------------------
47
+ error NonBuilderAddress ( address addr) ;
48
+ error MismatchedFlashblockNumber ( uint256 expectedFlashblockNumber, uint256 actualFlashblockNumber) ;
49
+ }
49
50
) ;
50
51
51
52
#[ derive( Debug , thiserror:: Error ) ]
52
53
pub ( super ) enum FlashblockNumberError {
53
- #[ error( "non builder address : {0}" ) ]
54
- NonBuilderAddress ( Address ) ,
55
- #[ error( "mismatched flashblock number : expected {0}, actual {1 }" ) ]
56
- MismatchedFlashblockNumber ( U256 , U256 ) ,
57
- #[ error( "unknown revert: {0}" ) ]
58
- Unknown ( String ) ,
54
+ #[ error( "flashblocks number contract tx reverted : {0:? }" ) ]
55
+ Revert ( IFlashblockNumber :: IFlashblockNumberErrors ) ,
56
+ #[ error( "contract may be invalid, mismatch in log emitted : expected {0:? }" ) ]
57
+ LogMismatch ( B256 ) ,
58
+ #[ error( "unknown revert: {0} err: {1} " ) ]
59
+ Unknown ( String , Error ) ,
59
60
#[ error( "halt: {0:?}" ) ]
60
61
Halt ( OpHaltReason ) ,
61
62
}
62
63
63
- impl From < Bytes > for FlashblockNumberError {
64
- fn from ( value : Bytes ) -> Self {
65
- // Empty revert
66
- if value. is_empty ( ) {
67
- return FlashblockNumberError :: Unknown (
68
- "Transaction reverted without reason" . to_string ( ) ,
69
- ) ;
70
- }
71
-
72
- // Try to decode each custom error type
73
- if let Ok ( NonBuilderAddress { addr } ) = NonBuilderAddress :: abi_decode ( & value) {
74
- return FlashblockNumberError :: NonBuilderAddress ( addr) ;
75
- }
76
-
77
- if let Ok ( MismatchedFlashblockNumber {
78
- expectedFlashblockNumber,
79
- actualFlashblockNumber,
80
- } ) = MismatchedFlashblockNumber :: abi_decode ( & value)
81
- {
82
- return FlashblockNumberError :: MismatchedFlashblockNumber (
83
- expectedFlashblockNumber,
84
- actualFlashblockNumber,
85
- ) ;
86
- }
87
-
88
- FlashblockNumberError :: Unknown ( hex:: encode ( value) )
89
- }
90
- }
91
-
92
64
// This will be the end of block transaction of a regular block
93
65
#[ derive( Debug , Clone ) ]
94
66
pub ( super ) struct FlashblocksBuilderTx {
@@ -116,7 +88,6 @@ impl BuilderTransactions<FlashblocksExtraCtx> for FlashblocksBuilderTx {
116
88
info : & mut ExecutionInfo < Extra > ,
117
89
ctx : & OpPayloadBuilderCtx < FlashblocksExtraCtx > ,
118
90
db : & mut State < impl Database > ,
119
- top_of_block : bool ,
120
91
) -> Result < Vec < BuilderTransactionCtx > , BuilderTransactionError > {
121
92
let mut builder_txs = Vec :: < BuilderTransactionCtx > :: new ( ) ;
122
93
@@ -126,24 +97,14 @@ impl BuilderTransactions<FlashblocksExtraCtx> for FlashblocksBuilderTx {
126
97
}
127
98
128
99
if ctx. is_last_flashblock ( ) {
129
- let flashblocks_builder_tx = self . base_builder_tx . simulate_builder_tx ( ctx, db) ?;
130
- if let Some ( tx) = flashblocks_builder_tx. clone ( ) {
131
- if top_of_block {
132
- // don't commit the builder if top of block, we only return the gas used to reserve gas for the builder tx
133
- builder_txs. push ( BuilderTransactionCtx {
134
- gas_used : tx. gas_used ,
135
- da_size : tx. da_size ,
136
- signed_tx : None ,
137
- } ) ;
138
- } else {
139
- builder_txs. push ( tx) ;
140
- }
141
- }
100
+ let base_tx = self . base_builder_tx . simulate_builder_tx ( ctx, db) ?;
101
+ builder_txs. extend ( base_tx. clone ( ) ) ;
102
+
142
103
if let Some ( flashtestations_builder_tx) = & self . flashtestations_builder_tx {
143
104
// We only include flashtestations txs in the last flashblock
144
105
let mut simulation_state = self . simulate_builder_txs_state :: < FlashblocksExtraCtx > (
145
106
state_provider. clone ( ) ,
146
- flashblocks_builder_tx . iter ( ) . collect ( ) ,
107
+ base_tx . iter ( ) . collect ( ) ,
147
108
ctx,
148
109
db,
149
110
) ?;
@@ -152,7 +113,6 @@ impl BuilderTransactions<FlashblocksExtraCtx> for FlashblocksBuilderTx {
152
113
info,
153
114
ctx,
154
115
& mut simulation_state,
155
- top_of_block,
156
116
) ?;
157
117
builder_txs. extend ( flashtestations_builder_txs) ;
158
118
}
@@ -205,10 +165,27 @@ impl FlashblocksNumberBuilderTx {
205
165
} ;
206
166
207
167
match result {
208
- ExecutionResult :: Success { gas_used, .. } => Ok ( gas_used) ,
209
- ExecutionResult :: Revert { output, .. } => Err ( BuilderTransactionError :: Other (
210
- Box :: new ( FlashblockNumberError :: from ( output) ) ,
211
- ) ) ,
168
+ ExecutionResult :: Success { gas_used, logs, .. } => {
169
+ if log_exists (
170
+ & logs,
171
+ & IFlashblockNumber :: FlashblockIncremented :: SIGNATURE_HASH ,
172
+ ) {
173
+ Ok ( gas_used)
174
+ } else {
175
+ Err ( BuilderTransactionError :: Other ( Box :: new (
176
+ FlashblockNumberError :: LogMismatch (
177
+ IFlashblockNumber :: FlashblockIncremented :: SIGNATURE_HASH ,
178
+ ) ,
179
+ ) ) )
180
+ }
181
+ }
182
+ ExecutionResult :: Revert { output, .. } => {
183
+ Err ( BuilderTransactionError :: Other ( Box :: new (
184
+ IFlashblockNumber :: IFlashblockNumberErrors :: abi_decode ( & output)
185
+ . map ( FlashblockNumberError :: Revert )
186
+ . unwrap_or_else ( |e| FlashblockNumberError :: Unknown ( hex:: encode ( output) , e) ) ,
187
+ ) ) )
188
+ }
212
189
ExecutionResult :: Halt { reason, .. } => Err ( BuilderTransactionError :: Other ( Box :: new (
213
190
FlashblockNumberError :: Halt ( reason) ,
214
191
) ) ) ,
@@ -245,7 +222,6 @@ impl BuilderTransactions<FlashblocksExtraCtx> for FlashblocksNumberBuilderTx {
245
222
info : & mut ExecutionInfo < Extra > ,
246
223
ctx : & OpPayloadBuilderCtx < FlashblocksExtraCtx > ,
247
224
db : & mut State < impl Database > ,
248
- top_of_block : bool ,
249
225
) -> Result < Vec < BuilderTransactionCtx > , BuilderTransactionError > {
250
226
let mut builder_txs = Vec :: < BuilderTransactionCtx > :: new ( ) ;
251
227
let state = StateProviderDatabase :: new ( state_provider. clone ( ) ) ;
@@ -256,8 +232,8 @@ impl BuilderTransactions<FlashblocksExtraCtx> for FlashblocksNumberBuilderTx {
256
232
. build ( ) ;
257
233
258
234
if ctx. is_first_flashblock ( ) {
259
- let flashblocks_builder_tx = self . base_builder_tx . simulate_builder_tx ( ctx , db ) ? ;
260
- builder_txs. extend ( flashblocks_builder_tx . clone ( ) ) ;
235
+ // fallback block builder tx
236
+ builder_txs. extend ( self . base_builder_tx . simulate_builder_tx ( ctx , db ) ? ) ;
261
237
} else {
262
238
// we increment the flashblock number for the next flashblock so we don't increment in the last flashblock
263
239
if let Some ( signer) = & self . signer {
@@ -274,41 +250,28 @@ impl BuilderTransactions<FlashblocksExtraCtx> for FlashblocksNumberBuilderTx {
274
250
{
275
251
Ok ( gas_used) => {
276
252
// Due to EIP-150, 63/64 of available gas is forwarded to external calls so need to add a buffer
277
- let flashblocks_tx = self . signed_flashblock_number_tx (
253
+ let signed_tx = self . signed_flashblock_number_tx (
278
254
ctx,
279
255
gas_used * 64 / 63 ,
280
256
nonce,
281
257
signer,
282
258
) ?;
283
259
284
260
let da_size = op_alloy_flz:: tx_estimated_size_fjord_bytes (
285
- flashblocks_tx . encoded_2718 ( ) . as_slice ( ) ,
261
+ signed_tx . encoded_2718 ( ) . as_slice ( ) ,
286
262
) ;
287
263
Some ( BuilderTransactionCtx {
288
264
gas_used,
289
265
da_size,
290
- signed_tx : if top_of_block {
291
- Some ( flashblocks_tx)
292
- } else {
293
- None
294
- } , // number tx at top of flashblock
266
+ signed_tx,
267
+ is_top_of_block : true , // number tx at top of flashblock
295
268
} )
296
269
}
297
270
Err ( e) => {
298
271
warn ! ( target: "builder_tx" , error = ?e, "Flashblocks number contract tx simulation failed, defaulting to fallback builder tx" ) ;
299
- let builder_tx = self . base_builder_tx . simulate_builder_tx ( ctx, db) ?;
300
- if let Some ( tx) = & builder_tx
301
- && top_of_block
302
- {
303
- // don't commit the builder if top of block, we only return the gas used to reserve gas for the builder tx
304
- Some ( BuilderTransactionCtx {
305
- gas_used : tx. gas_used ,
306
- da_size : tx. da_size ,
307
- signed_tx : None ,
308
- } )
309
- } else {
310
- builder_tx
311
- }
272
+ self . base_builder_tx
273
+ . simulate_builder_tx ( ctx, db) ?
274
+ . map ( |tx| tx. set_top_of_block ( ) )
312
275
}
313
276
} ;
314
277
@@ -331,7 +294,6 @@ impl BuilderTransactions<FlashblocksExtraCtx> for FlashblocksNumberBuilderTx {
331
294
info,
332
295
ctx,
333
296
& mut simulation_state,
334
- top_of_block,
335
297
) ?;
336
298
builder_txs. extend ( flashtestations_builder_txs) ;
337
299
}
0 commit comments