Skip to content

Commit

Permalink
unify error types
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Sep 1, 2022
1 parent 670d1b3 commit 61afb08
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 36 deletions.
10 changes: 3 additions & 7 deletions anvil/src/eth/backend/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use foundry_evm::{
use hash_db::HashDB;
use serde::{Deserialize, Serialize};


/// Type alias for the `HashDB` representation of the Database
pub type AsHashDB = Box<dyn HashDB<KeccakHasher, Vec<u8>>>;

Expand Down Expand Up @@ -77,26 +76,23 @@ pub trait Db:

/// Sets the nonce of the given address
fn set_nonce(&mut self, address: Address, nonce: u64) -> DatabaseResult<()> {
let mut info =
self.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
let mut info = self.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
info.nonce = nonce;
self.insert_account(address, info);
Ok(())
}

/// Sets the balance of the given address
fn set_balance(&mut self, address: Address, balance: U256) -> DatabaseResult<()> {
let mut info =
self.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
let mut info = self.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
info.balance = balance;
self.insert_account(address, info);
Ok(())
}

/// Sets the balance of the given address
fn set_code(&mut self, address: Address, code: Bytes) -> DatabaseResult<()> {
let mut info =
self.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
let mut info = self.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
let code_hash = if code.as_ref().is_empty() {
KECCAK_EMPTY
} else {
Expand Down
3 changes: 1 addition & 2 deletions anvil/src/eth/backend/mem/fork_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ impl Db for ForkedDatabase {

fn set_storage_at(&mut self, address: Address, slot: U256, val: U256) -> DatabaseResult<()> {
// this ensures the account is loaded first
let _ = Database::basic(self, address)?
.ok_or(DatabaseError::MissingAccount(address))?;
let _ = Database::basic(self, address)?.ok_or(DatabaseError::MissingAccount(address))?;
self.database_mut().set_storage_at(address, slot, val)
}

Expand Down
8 changes: 3 additions & 5 deletions anvil/src/eth/backend/mem/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,11 @@ impl MaybeHashDatabase for MemDb {
}

fn clear_into_snapshot(&mut self) -> StateSnapshot {
// self.inner.clear_into_snapshot()
todo!()
self.inner.clear_into_snapshot()
}

fn init_from_snapshot(&mut self, _snapshot: StateSnapshot) {
// self.inner.init_from_snapshot(snapshot)
todo!()
fn init_from_snapshot(&mut self, snapshot: StateSnapshot) {
self.inner.init_from_snapshot(snapshot)
}
}

Expand Down
15 changes: 4 additions & 11 deletions anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ impl Backend {
fork_genesis_infos.clear();

for address in self.genesis.accounts.iter().copied() {
let mut info =
db.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
let mut info = db.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
info.balance = self.genesis.balance;
db.insert_account(address, info.clone());

Expand Down Expand Up @@ -835,8 +834,7 @@ impl Backend {
let to = if let Some(to) = request.to {
to
} else {
let nonce =
state.basic(from)?.ok_or(DatabaseError::MissingAccount(from))?.nonce;
let nonce = state.basic(from)?.ok_or(DatabaseError::MissingAccount(from))?.nonce;
get_contract_address(from, nonce)
};

Expand Down Expand Up @@ -1312,8 +1310,7 @@ impl Backend {
D: DatabaseRef<Error = DatabaseError>,
{
trace!(target: "backend", "get code for {:?}", address);
let account =
state.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
let account = state.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?;
if account.code_hash == KECCAK_EMPTY {
// if the code hash is `KECCAK_EMPTY`, we check no further
return Ok(Default::default())
Expand Down Expand Up @@ -1360,11 +1357,7 @@ impl Backend {
) -> Result<U256, BlockchainError> {
self.with_database_at(block_request, |db, _| {
trace!(target: "backend", "get nonce for {:?}", address);
Ok(db
.basic(address)?
.ok_or(DatabaseError::MissingAccount(address))?
.nonce
.into())
Ok(db.basic(address)?.ok_or(DatabaseError::MissingAccount(address))?.nonce.into())
})
.await?
}
Expand Down
1 change: 0 additions & 1 deletion anvil/src/eth/backend/mem/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Support for generating the state root for memdb storage

use crate::eth::{backend::db::AsHashDB, error::BlockchainError};
use anvil_core::eth::{state::StateOverride, trie::RefSecTrieDBMut};
use bytes::Bytes;
Expand Down
47 changes: 37 additions & 10 deletions evm/src/executor/backend/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use ethers::{
types::Address,
};
use hashbrown::HashMap as Map;
use revm::{db::DatabaseRef, Account, AccountInfo, Bytecode, Database, DatabaseCommit, InMemoryDB};
use revm::{db::DatabaseRef, Account, AccountInfo, Bytecode, Database, DatabaseCommit};
use revm::db::{CacheDB, EmptyDB};

use crate::executor::snapshot::Snapshots;

pub type InMemoryDB = CacheDB<EmptyDBWrapper>;

/// In memory Database for anvil
///
/// This acts like a wrapper type for [InMemoryDB] but is capable of applying snapshots
Expand All @@ -20,45 +23,45 @@ pub struct MemDb {

impl Default for MemDb {
fn default() -> Self {
Self { inner: InMemoryDB::default(), snapshots: Default::default() }
Self { inner: CacheDB::new(Default::default()), snapshots: Default::default() }
}
}

impl DatabaseRef for MemDb {
type Error = DatabaseError;
fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Ok(DatabaseRef::basic(&self.inner, address)?)
DatabaseRef::basic(&self.inner, address)
}

fn code_by_hash(&self, code_hash: H256) -> Result<Bytecode, Self::Error> {
Ok(DatabaseRef::code_by_hash(&self.inner, code_hash)?)
DatabaseRef::code_by_hash(&self.inner, code_hash)
}

fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
Ok(DatabaseRef::storage(&self.inner, address, index)?)
DatabaseRef::storage(&self.inner, address, index)
}

fn block_hash(&self, number: U256) -> Result<H256, Self::Error> {
Ok(DatabaseRef::block_hash(&self.inner, number)?)
DatabaseRef::block_hash(&self.inner, number)
}
}

impl Database for MemDb {
type Error = DatabaseError;
fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Ok(Database::basic(&mut self.inner, address)?)
Database::basic(&mut self.inner, address)
}

fn code_by_hash(&mut self, code_hash: H256) -> Result<Bytecode, Self::Error> {
Ok(Database::code_by_hash(&mut self.inner, code_hash)?)
Database::code_by_hash(&mut self.inner, code_hash)
}

fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
Ok(Database::storage(&mut self.inner, address, index)?)
Database::storage(&mut self.inner, address, index)
}

fn block_hash(&mut self, number: U256) -> Result<H256, Self::Error> {
Ok(Database::block_hash(&mut self.inner, number)?)
Database::block_hash(&mut self.inner, number)
}
}

Expand All @@ -67,3 +70,27 @@ impl DatabaseCommit for MemDb {
DatabaseCommit::commit(&mut self.inner, changes)
}
}


/// An empty database that always returns default values when queried.
///
/// This is just a simple wrapper for `revm::EmptyDB` but implements `DatabaseError` instead, this way we can unify all different `Database` impls
#[derive(Debug, Default, Clone)]
pub struct EmptyDBWrapper(EmptyDB);

impl DatabaseRef for EmptyDBWrapper {
type Error = DatabaseError;
fn basic(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
Ok(self.0.basic(address)?)
}
fn code_by_hash(&self, code_hash: H256) -> Result<Bytecode, Self::Error> {
Ok(self.0.code_by_hash(code_hash)?)
}
fn storage(&self, address: Address, index: U256) -> Result<U256, Self::Error> {
Ok(self.0.storage(address, index)?)
}

fn block_hash(&self, number: U256) -> Result<H256, Self::Error> {
Ok(self.0.block_hash(number)?)
}
}

0 comments on commit 61afb08

Please sign in to comment.