From 5ce1873a839521c0d84f1ce10b155e56606c2178 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Apr 2023 12:17:05 +0200 Subject: [PATCH] feat: extract increment_account_balance (#2201) --- crates/revm/src/executor.rs | 79 ++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/crates/revm/src/executor.rs b/crates/revm/src/executor.rs index eb7634c14a..24e0e24613 100644 --- a/crates/revm/src/executor.rs +++ b/crates/revm/src/executor.rs @@ -159,38 +159,8 @@ where increment: U256, post_state: &mut PostState, ) -> Result<(), Error> { - let db = self.db(); - let beneficiary = db.load_account(address).map_err(|_| Error::ProviderError)?; - let old = to_reth_acc(&beneficiary.info); - // Increment beneficiary balance by mutating db entry in place. - beneficiary.info.balance += increment; - let new = to_reth_acc(&beneficiary.info); - match beneficiary.account_state { - AccountState::NotExisting => { - // if account was not existing that means that storage is not - // present. - beneficiary.account_state = AccountState::StorageCleared; - - // if account was not present append `Created` changeset - post_state.create_account( - address, - Account { nonce: 0, balance: new.balance, bytecode_hash: None }, - ) - } - - AccountState::StorageCleared | AccountState::Touched | AccountState::None => { - // If account is None that means that EVM didn't touch it. - // we are changing the state to Touched as account can have - // storage in db. - if beneficiary.account_state == AccountState::None { - beneficiary.account_state = AccountState::Touched; - } - // if account was present, append changed changeset. - post_state.change_account(address, old, new); - } - } - - Ok(()) + increment_account_balance(self.db(), post_state, address, increment) + .map_err(|_| Error::ProviderError) } /// Runs a single transaction in the configured environment and proceeds @@ -351,6 +321,51 @@ where } } +/// Increment the balance for the given account in the [PostState]. +/// +/// Returns an error if the database encountered an error while loading the account. +pub fn increment_account_balance( + db: &mut CacheDB, + post_state: &mut PostState, + address: Address, + increment: U256, +) -> Result<(), ::Error> +where + DB: DatabaseRef, +{ + let beneficiary = db.load_account(address)?; + let old = to_reth_acc(&beneficiary.info); + // Increment beneficiary balance by mutating db entry in place. + beneficiary.info.balance += increment; + let new = to_reth_acc(&beneficiary.info); + match beneficiary.account_state { + AccountState::NotExisting => { + // if account was not existing that means that storage is not + // present. + beneficiary.account_state = AccountState::StorageCleared; + + // if account was not present append `Created` changeset + post_state.create_account( + address, + Account { nonce: 0, balance: new.balance, bytecode_hash: None }, + ) + } + + AccountState::StorageCleared | AccountState::Touched | AccountState::None => { + // If account is None that means that EVM didn't touch it. + // we are changing the state to Touched as account can have + // storage in db. + if beneficiary.account_state == AccountState::None { + beneficiary.account_state = AccountState::Touched; + } + // if account was present, append changed changeset. + post_state.change_account(address, old, new); + } + } + + Ok(()) +} + /// Commit change to the _run-time_ database [CacheDB], and update the given [PostState] with the /// changes made in the transaction, which can be persisted to the database. ///