@@ -2,7 +2,7 @@ use alloy_consensus::TxEip1559;
2
2
use alloy_eips:: { Encodable2718 , eip7623:: TOTAL_COST_FLOOR_PER_TOKEN } ;
3
3
use alloy_evm:: Database ;
4
4
use alloy_primitives:: {
5
- Address , TxKind ,
5
+ Address , B256 , Log , TxKind ,
6
6
map:: foldhash:: { HashSet , HashSetExt } ,
7
7
} ;
8
8
use core:: fmt:: Debug ;
@@ -30,7 +30,22 @@ use crate::{
30
30
pub struct BuilderTransactionCtx {
31
31
pub gas_used : u64 ,
32
32
pub da_size : u64 ,
33
- pub signed_tx : Option < Recovered < OpTransactionSigned > > ,
33
+ pub signed_tx : Recovered < OpTransactionSigned > ,
34
+ // whether the transaction should be a top of block or
35
+ // bottom of block transaction
36
+ pub is_top_of_block : bool ,
37
+ }
38
+
39
+ impl BuilderTransactionCtx {
40
+ pub fn set_top_of_block ( mut self ) -> Self {
41
+ self . is_top_of_block = true ;
42
+ self
43
+ }
44
+
45
+ pub fn set_bottom_of_block ( mut self ) -> Self {
46
+ self . is_top_of_block = false ;
47
+ self
48
+ }
34
49
}
35
50
36
51
/// Possible error variants during construction of builder txs.
@@ -80,7 +95,6 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
80
95
info : & mut ExecutionInfo < Extra > ,
81
96
ctx : & OpPayloadBuilderCtx < ExtraCtx > ,
82
97
db : & mut State < impl Database > ,
83
- top_of_block : bool ,
84
98
) -> Result < Vec < BuilderTransactionCtx > , BuilderTransactionError > ;
85
99
86
100
fn add_builder_txs < Extra : Debug + Default > (
@@ -98,30 +112,26 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
98
112
99
113
let mut invalid: HashSet < Address > = HashSet :: new ( ) ;
100
114
101
- let builder_txs = self . simulate_builder_txs (
102
- state_provider,
103
- info,
104
- builder_ctx,
105
- evm. db_mut ( ) ,
106
- top_of_block,
107
- ) ?;
115
+ let builder_txs =
116
+ self . simulate_builder_txs ( state_provider, info, builder_ctx, evm. db_mut ( ) ) ?;
108
117
for builder_tx in builder_txs. iter ( ) {
109
- let signed_tx = match builder_tx. signed_tx . clone ( ) {
110
- Some ( tx) => tx,
111
- None => continue ,
112
- } ;
113
- if invalid. contains ( & signed_tx. signer ( ) ) {
114
- warn ! ( target: "payload_builder" , tx_hash = ?signed_tx. tx_hash( ) , "builder signer invalid as previous builder tx reverted" ) ;
118
+ if builder_tx. is_top_of_block != top_of_block {
119
+ // don't commit tx if the buidler tx is not being added in the intended
120
+ // position in the block
121
+ continue ;
122
+ }
123
+ if invalid. contains ( & builder_tx. signed_tx . signer ( ) ) {
124
+ warn ! ( target: "payload_builder" , tx_hash = ?builder_tx. signed_tx. tx_hash( ) , "builder signer invalid as previous builder tx reverted" ) ;
115
125
continue ;
116
126
}
117
127
118
128
let ResultAndState { result, state } = evm
119
- . transact ( & signed_tx)
129
+ . transact ( & builder_tx . signed_tx )
120
130
. map_err ( |err| BuilderTransactionError :: EvmExecutionError ( Box :: new ( err) ) ) ?;
121
131
122
132
if !result. is_success ( ) {
123
- warn ! ( target: "payload_builder" , tx_hash = ?signed_tx. tx_hash( ) , "builder tx reverted" ) ;
124
- invalid. insert ( signed_tx. signer ( ) ) ;
133
+ warn ! ( target: "payload_builder" , tx_hash = ?builder_tx . signed_tx. tx_hash( ) , "builder tx reverted" ) ;
134
+ invalid. insert ( builder_tx . signed_tx . signer ( ) ) ;
125
135
continue ;
126
136
}
127
137
@@ -130,7 +140,7 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
130
140
info. cumulative_gas_used += gas_used;
131
141
132
142
let ctx = ReceiptBuilderCtx {
133
- tx : signed_tx. inner ( ) ,
143
+ tx : builder_tx . signed_tx . inner ( ) ,
134
144
evm : & evm,
135
145
result,
136
146
state : & state,
@@ -142,9 +152,9 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
142
152
evm. db_mut ( ) . commit ( state) ;
143
153
144
154
// Append sender and transaction to the respective lists
145
- info. executed_senders . push ( signed_tx. signer ( ) ) ;
155
+ info. executed_senders . push ( builder_tx . signed_tx . signer ( ) ) ;
146
156
info. executed_transactions
147
- . push ( signed_tx. clone ( ) . into_inner ( ) ) ;
157
+ . push ( builder_tx . signed_tx . clone ( ) . into_inner ( ) ) ;
148
158
}
149
159
150
160
// Release the db reference by dropping evm
@@ -172,12 +182,8 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
172
182
. evm_with_env ( & mut simulation_state, ctx. evm_env . clone ( ) ) ;
173
183
174
184
for builder_tx in builder_txs {
175
- let signed_tx = match builder_tx. signed_tx . clone ( ) {
176
- Some ( tx) => tx,
177
- None => continue ,
178
- } ;
179
185
let ResultAndState { state, .. } = evm
180
- . transact ( & signed_tx)
186
+ . transact ( & builder_tx . signed_tx )
181
187
. map_err ( |err| BuilderTransactionError :: EvmExecutionError ( Box :: new ( err) ) ) ?;
182
188
183
189
evm. db_mut ( ) . commit ( state) ;
@@ -214,7 +220,8 @@ impl BuilderTxBase {
214
220
Ok ( Some ( BuilderTransactionCtx {
215
221
gas_used,
216
222
da_size,
217
- signed_tx : Some ( signed_tx) ,
223
+ signed_tx,
224
+ is_top_of_block : false ,
218
225
} ) )
219
226
}
220
227
None => Ok ( None ) ,
@@ -276,11 +283,20 @@ impl BuilderTxBase {
276
283
}
277
284
}
278
285
279
- pub ( super ) fn get_nonce (
286
+ pub ( crate ) fn get_nonce (
280
287
db : & mut State < impl Database > ,
281
288
address : Address ,
282
289
) -> Result < u64 , BuilderTransactionError > {
283
290
db. load_cache_account ( address)
284
291
. map ( |acc| acc. account_info ( ) . unwrap_or_default ( ) . nonce )
285
292
. map_err ( |_| BuilderTransactionError :: AccountLoadFailed ( address) )
286
293
}
294
+
295
+ pub ( crate ) fn log_exists ( logs : & [ Log ] , topic : & B256 ) -> bool {
296
+ for log in logs {
297
+ if log. topics ( ) . first ( ) == Some ( topic) {
298
+ return true ;
299
+ }
300
+ }
301
+ false
302
+ }
0 commit comments