@@ -54,18 +54,21 @@ impl StateWithIncrements {
5454 pub fn resolve_state < DB : Database > ( self , db : & mut DB ) -> Result < EvmState , DB :: Error > {
5555 let mut state = self . loaded_state ;
5656 for ( addr, delta) in self . pending_balance_increments . iter ( ) {
57- match state. entry ( * addr) {
58- Entry :: Occupied ( mut entry) => {
59- entry. get_mut ( ) . info . balance = entry. get ( ) . info . balance . saturating_add ( * delta) ;
60- }
61- Entry :: Vacant ( entry) => {
62- let mut account = db. basic ( * addr) ?. unwrap_or_default ( ) ;
63- account. balance = account. balance . saturating_add ( * delta) ;
64- let mut account: Account = account. into ( ) ;
65- account. mark_touch ( ) ;
66- entry. insert ( account) ;
67- }
57+ let mut account = db. basic ( * addr) ?. unwrap_or_default ( ) ;
58+ // if loaded_state also has an entry and it doesn't match the account from the database, we have a problem
59+ // Addresses in pending_balance_increments must not be in loaded_state
60+ if let Some ( loaded_account) = state. get ( addr) {
61+ debug_assert_eq ! (
62+ loaded_account. info, account,
63+ "Address {} in pending_balance_increments must not be in loaded_state" ,
64+ addr
65+ ) ;
6866 }
67+
68+ account. balance = account. balance . saturating_add ( * delta) ;
69+ let mut account: Account = account. into ( ) ;
70+ account. mark_touch ( ) ;
71+ state. insert ( * addr, account) ;
6972 }
7073 Ok ( state)
7174 }
@@ -105,7 +108,7 @@ impl<
105108 pub fn execute_single_tx <
106109 ExecuteTxFn : ( Fn (
107110 & Recovered < op_alloy_consensus:: OpTxEnvelope > ,
108- & mut State < LazyDatabaseWrapper < VersionedDatabase < ' _ , DB > > > ,
111+ & mut LazyDatabaseWrapper < State < VersionedDatabase < ' _ , DB > > > ,
109112 & HashSet < EvmStateKey > ,
110113 Option < & TxExecutionResult > ,
111114 u64 ,
@@ -140,11 +143,11 @@ impl<
140143 Arc :: clone ( & self . shared_code_cache ) ,
141144 ) ;
142145
143- // Wrap with LazyDatabaseWrapper to track balance increments (fee payments)
144- let lazy_db = LazyDatabaseWrapper :: new ( versioned_db) ;
145-
146146 // Create State wrapper for EVM execution
147- let mut tx_state = State :: builder ( ) . with_database ( lazy_db) . build ( ) ;
147+ let tx_state = State :: builder ( ) . with_database ( versioned_db) . build ( ) ;
148+
149+ // Wrap with LazyDatabaseWrapper to track balance increments (fee payments)
150+ let mut tx_state = LazyDatabaseWrapper :: new ( tx_state) ;
148151
149152 // Detect conflicting keys for re-executions (incarnation > 0)
150153 // Conflicting keys are reads whose versions changed since the previous execution
@@ -204,10 +207,11 @@ impl<
204207 let mut write_set = WriteSet :: new ( ) ;
205208
206209 // Extract pending balance increments from LazyDatabaseWrapper
207- let pending_balance_increments = tx_state. database . pending_increments ( ) ;
210+ // These are balances that were incremented only and never read otherwise.
211+ let pending_balance_increments = tx_state. pending_increments ( ) ;
208212
209213 // Get read set and captured reads from inner VersionedDatabase
210- let versioned_db = tx_state. database . inner_mut ( ) ;
214+ let versioned_db = & mut tx_state. inner_mut ( ) . database ;
211215 let read_set = versioned_db. take_read_set ( ) ;
212216 let captured_reads = versioned_db. take_captured_reads ( ) ;
213217
@@ -335,7 +339,7 @@ impl<
335339 "Read aborted for transaction"
336340 ) ;
337341
338- let read_set = tx_state. database . inner_mut ( ) . take_read_set ( ) ;
342+ let read_set = tx_state. inner_mut ( ) . database . take_read_set ( ) ;
339343 return (
340344 read_set,
341345 Default :: default ( ) ,
@@ -367,7 +371,7 @@ impl<
367371 "Error executing transaction"
368372 ) ;
369373
370- let read_set = tx_state. database . inner_mut ( ) . take_read_set ( ) ;
374+ let read_set = tx_state. inner_mut ( ) . database . take_read_set ( ) ;
371375 return (
372376 read_set,
373377 Default :: default ( ) ,
@@ -391,7 +395,7 @@ impl<
391395 pub fn execute_transactions_parallel <
392396 ExecuteTxFn : ( Fn (
393397 & Recovered < op_alloy_consensus:: OpTxEnvelope > ,
394- & mut State < LazyDatabaseWrapper < VersionedDatabase < ' _ , DB > > > ,
398+ & mut LazyDatabaseWrapper < State < VersionedDatabase < ' _ , DB > > > ,
395399 & HashSet < EvmStateKey > ,
396400 Option < & TxExecutionResult > ,
397401 u64 ,
0 commit comments