@@ -2,7 +2,7 @@ use alloy_consensus::TxEip1559;
22use alloy_eips:: { Encodable2718 , eip7623:: TOTAL_COST_FLOOR_PER_TOKEN } ;
33use alloy_evm:: Database ;
44use alloy_primitives:: {
5- Address , TxKind ,
5+ Address , B256 , Log , TxKind ,
66 map:: foldhash:: { HashSet , HashSetExt } ,
77} ;
88use core:: fmt:: Debug ;
@@ -30,7 +30,22 @@ use crate::{
3030pub struct BuilderTransactionCtx {
3131 pub gas_used : u64 ,
3232 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+ }
3449}
3550
3651/// Possible error variants during construction of builder txs.
@@ -80,7 +95,6 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
8095 info : & mut ExecutionInfo < Extra > ,
8196 ctx : & OpPayloadBuilderCtx < ExtraCtx > ,
8297 db : & mut State < impl Database > ,
83- top_of_block : bool ,
8498 ) -> Result < Vec < BuilderTransactionCtx > , BuilderTransactionError > ;
8599
86100 fn add_builder_txs < Extra : Debug + Default > (
@@ -98,30 +112,26 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
98112
99113 let mut invalid: HashSet < Address > = HashSet :: new ( ) ;
100114
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 ( ) ) ?;
108117 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" ) ;
115125 continue ;
116126 }
117127
118128 let ResultAndState { result, state } = evm
119- . transact ( & signed_tx)
129+ . transact ( & builder_tx . signed_tx )
120130 . map_err ( |err| BuilderTransactionError :: EvmExecutionError ( Box :: new ( err) ) ) ?;
121131
122132 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 ( ) ) ;
125135 continue ;
126136 }
127137
@@ -130,7 +140,7 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
130140 info. cumulative_gas_used += gas_used;
131141
132142 let ctx = ReceiptBuilderCtx {
133- tx : signed_tx. inner ( ) ,
143+ tx : builder_tx . signed_tx . inner ( ) ,
134144 evm : & evm,
135145 result,
136146 state : & state,
@@ -142,9 +152,9 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
142152 evm. db_mut ( ) . commit ( state) ;
143153
144154 // 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 ( ) ) ;
146156 info. executed_transactions
147- . push ( signed_tx. clone ( ) . into_inner ( ) ) ;
157+ . push ( builder_tx . signed_tx . clone ( ) . into_inner ( ) ) ;
148158 }
149159
150160 // Release the db reference by dropping evm
@@ -172,12 +182,8 @@ pub trait BuilderTransactions<ExtraCtx: Debug + Default = ()>: Debug {
172182 . evm_with_env ( & mut simulation_state, ctx. evm_env . clone ( ) ) ;
173183
174184 for builder_tx in builder_txs {
175- let signed_tx = match builder_tx. signed_tx . clone ( ) {
176- Some ( tx) => tx,
177- None => continue ,
178- } ;
179185 let ResultAndState { state, .. } = evm
180- . transact ( & signed_tx)
186+ . transact ( & builder_tx . signed_tx )
181187 . map_err ( |err| BuilderTransactionError :: EvmExecutionError ( Box :: new ( err) ) ) ?;
182188
183189 evm. db_mut ( ) . commit ( state) ;
@@ -214,7 +220,8 @@ impl BuilderTxBase {
214220 Ok ( Some ( BuilderTransactionCtx {
215221 gas_used,
216222 da_size,
217- signed_tx : Some ( signed_tx) ,
223+ signed_tx,
224+ is_top_of_block : false ,
218225 } ) )
219226 }
220227 None => Ok ( None ) ,
@@ -276,11 +283,20 @@ impl BuilderTxBase {
276283 }
277284}
278285
279- pub ( super ) fn get_nonce (
286+ pub ( crate ) fn get_nonce (
280287 db : & mut State < impl Database > ,
281288 address : Address ,
282289) -> Result < u64 , BuilderTransactionError > {
283290 db. load_cache_account ( address)
284291 . map ( |acc| acc. account_info ( ) . unwrap_or_default ( ) . nonce )
285292 . map_err ( |_| BuilderTransactionError :: AccountLoadFailed ( address) )
286293}
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