diff --git a/bins/revme/src/cmd/blockchaintest/post_block.rs b/bins/revme/src/cmd/blockchaintest/post_block.rs index f768e389e1..62617acc05 100644 --- a/bins/revme/src/cmd/blockchaintest/post_block.rs +++ b/bins/revme/src/cmd/blockchaintest/post_block.rs @@ -1,6 +1,6 @@ use revm::{ context::{Block, ContextTr}, - database::State, + database::{DatabaseCommitExt as _, State}, handler::EvmTr, primitives::{hardfork::SpecId, ONE_ETHER, ONE_GWEI}, Database, SystemCallCommitEvm, diff --git a/crates/database/interface/src/lib.rs b/crates/database/interface/src/lib.rs index 76e816b3e2..fc6cb06892 100644 --- a/crates/database/interface/src/lib.rs +++ b/crates/database/interface/src/lib.rs @@ -172,3 +172,37 @@ impl DatabaseRef for WrapDatabaseRef { self.0.block_hash_ref(number) } } + +impl DatabaseCommitExt for T { + // default implementation +} + +/// EVM database commit interface. +pub trait DatabaseCommitExt: Database + DatabaseCommit { + /// Iterates over received balances and increment all account balances. + /// + /// Update will create transitions for all accounts that are updated. + fn increment_balances( + &mut self, + balances: impl IntoIterator, + ) -> Result<(), Self::Error> { + // Make transition and update cache state + let balances = balances.into_iter(); + let mut transitions: HashMap = HashMap::default(); + transitions.reserve(balances.size_hint().0); + for (address, balance) in balances { + let mut original_account = match self.basic(address)? { + Some(acc_info) => Account::from(acc_info), + None => Account::new_not_existing(0), + }; + original_account.info.balance = original_account + .info + .balance + .saturating_add(U256::from(balance)); + original_account.mark_touch(); + transitions.insert(address, original_account); + } + self.commit(transitions); + Ok(()) + } +} diff --git a/crates/database/src/states/state.rs b/crates/database/src/states/state.rs index 0b90b8a807..a6eaa98138 100644 --- a/crates/database/src/states/state.rs +++ b/crates/database/src/states/state.rs @@ -81,39 +81,6 @@ impl State { self.bundle_state.size_hint() } - /// Iterates over received balances and increment all account balances. - /// - /// **Note**: If account is not found inside cache state it will be loaded from database. - /// - /// Update will create transitions for all accounts that are updated. - /// - /// If using this to implement withdrawals, zero balances must be filtered out before calling this function. - pub fn increment_balances( - &mut self, - balances: impl IntoIterator, - ) -> Result<(), DB::Error> { - // Make transition and update cache state - let balances = balances.into_iter(); - let mut transitions = Vec::with_capacity(balances.size_hint().0); - for (address, balance) in balances { - if balance == 0 { - continue; - } - let original_account = self.load_cache_account(address)?; - transitions.push(( - address, - original_account - .increment_balance(balance) - .expect("Balance is not zero"), - )) - } - // Append transition - if let Some(s) = self.transition_state.as_mut() { - s.add_transitions(transitions) - } - Ok(()) - } - /// Drains balances from given account and return those values. /// /// It is used for DAO hardfork state change to move values from given accounts.