feat: extract increment_account_balance (#2201)

This commit is contained in:
Matthias Seitz
2023-04-12 12:17:05 +02:00
committed by GitHub
parent e3f4780c6c
commit 5ce1873a83

View File

@@ -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>(
db: &mut CacheDB<DB>,
post_state: &mut PostState,
address: Address,
increment: U256,
) -> Result<(), <DB as DatabaseRef>::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.
///