Implement Deref and DerefMut for StateProviderDatabase (#6538)

This commit is contained in:
Thomas Coratger
2024-02-15 21:09:13 +01:00
committed by GitHub
parent 9450319002
commit 4a8a68b8b9
2 changed files with 20 additions and 20 deletions

View File

@ -6,6 +6,7 @@ use revm::{
primitives::{AccountInfo, Bytecode},
Database, StateDBBox,
};
use std::ops::{Deref, DerefMut};
/// SubState of database. Uses revm internal cache with binding to reth StateProvider trait.
pub type SubState<DB> = CacheDB<StateProviderDatabase<DB>>;
@ -23,22 +24,26 @@ impl<DB: StateProvider> StateProviderDatabase<DB> {
Self(db)
}
/// Return inner state reference
pub fn state(&self) -> &DB {
&self.0
}
/// Return inner state mutable reference
pub fn state_mut(&mut self) -> &mut DB {
&mut self.0
}
/// Consume State and return inner StateProvider.
pub fn into_inner(self) -> DB {
self.0
}
}
impl<DB: StateProvider> Deref for StateProviderDatabase<DB> {
type Target = DB;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<DB: StateProvider> DerefMut for StateProviderDatabase<DB> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<DB: StateProvider> Database for StateProviderDatabase<DB> {
type Error = ProviderError;
@ -81,7 +86,7 @@ impl<DB: StateProvider> DatabaseRef for StateProviderDatabase<DB> {
/// Returns `Ok` with `Some(AccountInfo)` if the account exists,
/// `None` if it doesn't, or an error if encountered.
fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Ok(self.0.basic_account(address)?.map(|account| AccountInfo {
Ok(self.basic_account(address)?.map(|account| AccountInfo {
balance: account.balance,
nonce: account.nonce,
code_hash: account.bytecode_hash.unwrap_or(KECCAK_EMPTY),
@ -93,7 +98,7 @@ impl<DB: StateProvider> DatabaseRef for StateProviderDatabase<DB> {
///
/// Returns `Ok` with the bytecode if found, or the default bytecode otherwise.
fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
Ok(self.0.bytecode_by_hash(code_hash)?.unwrap_or_default().0)
Ok(self.bytecode_by_hash(code_hash)?.unwrap_or_default().0)
}
/// Retrieves the storage value at a specific index for a given address.

View File

@ -215,7 +215,7 @@ where
// if the request is a simple transfer we can optimize
if env.tx.data.is_empty() {
if let TransactTo::Call(to) = env.tx.transact_to {
if let Ok(code) = db.db.state().account_code(to) {
if let Ok(code) = db.db.account_code(to) {
let no_code_callee = code.map(|code| code.is_empty()).unwrap_or(true);
if no_code_callee {
// simple transfer, check if caller has sufficient funds
@ -427,13 +427,8 @@ where
// calculate the gas used using the access list
request.access_list = Some(access_list.clone());
let gas_used = self.estimate_gas_with(
cfg_with_spec_id,
env.block.clone(),
request,
db.db.state(),
None,
)?;
let gas_used =
self.estimate_gas_with(cfg_with_spec_id, env.block.clone(), request, &*db.db, None)?;
Ok(AccessListWithGasUsed { access_list, gas_used })
}