Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use alloy-trie for eth_getProof #7546

Merged
merged 10 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 30 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions crates/anvil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ foundry-evm.workspace = true
bytes = "1.4.0"
k256.workspace = true
ethers = { workspace = true, features = ["rustls", "ws", "ipc", "optimism"] }
trie-db = "0.23"
hash-db = "0.15"
memory-db = "0.29"
alloy-primitives = { workspace = true, features = ["serde"] }
alloy-consensus = { workspace = true, features = ["k256", "kzg"] }
alloy-network.workspace = true
Expand All @@ -48,6 +45,7 @@ alloy-providers.workspace = true
alloy-transport.workspace = true
alloy-chains.workspace = true
alloy-genesis.workspace = true
alloy-trie = "0.3"

# axum related
axum.workspace = true
Expand Down
49 changes: 16 additions & 33 deletions crates/anvil/src/eth/backend/db.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Helper types for working with [revm](foundry_evm::revm)

use crate::{mem::state::trie_hash_db, revm::primitives::AccountInfo};
use crate::revm::primitives::AccountInfo;
use alloy_primitives::{keccak256, Address, Bytes, B256, U256, U64};
use alloy_rpc_types::BlockId;
use anvil_core::eth::trie::KeccakHasher;
use foundry_common::errors::FsPathError;
use foundry_evm::{
backend::{DatabaseError, DatabaseResult, MemDb, RevertSnapshotAction, StateSnapshot},
Expand All @@ -14,22 +13,13 @@ use foundry_evm::{
Database, DatabaseCommit,
},
};
use hash_db::HashDB;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fmt, path::Path};

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

/// Helper trait get access to the data in `HashDb` form
/// Helper trait get access to the full state data of the database
#[auto_impl::auto_impl(Box)]
pub trait MaybeHashDatabase: DatabaseRef<Error = DatabaseError> {
/// Return the DB as read-only hashdb and the root key
fn maybe_as_hash_db(&self) -> Option<(AsHashDB, B256)> {
None
}
/// Return the storage DB as read-only hashdb and the storage root of the account
fn maybe_account_db(&self, _addr: Address) -> Option<(AsHashDB, B256)> {
pub trait MaybeFullDatabase: DatabaseRef<Error = DatabaseError> {
fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
None
}

Expand All @@ -43,15 +33,12 @@ pub trait MaybeHashDatabase: DatabaseRef<Error = DatabaseError> {
fn init_from_snapshot(&mut self, snapshot: StateSnapshot);
}

impl<'a, T: 'a + MaybeHashDatabase + ?Sized> MaybeHashDatabase for &'a T
impl<'a, T: 'a + MaybeFullDatabase + ?Sized> MaybeFullDatabase for &'a T
where
&'a T: DatabaseRef<Error = DatabaseError>,
{
fn maybe_as_hash_db(&self) -> Option<(AsHashDB, B256)> {
T::maybe_as_hash_db(self)
}
fn maybe_account_db(&self, addr: Address) -> Option<(AsHashDB, B256)> {
T::maybe_account_db(self, addr)
fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
T::maybe_as_full_db(self)
}

fn clear_into_snapshot(&mut self) -> StateSnapshot {
Expand Down Expand Up @@ -79,7 +66,7 @@ pub trait Db:
DatabaseRef<Error = DatabaseError>
+ Database<Error = DatabaseError>
+ DatabaseCommit
+ MaybeHashDatabase
+ MaybeFullDatabase
+ MaybeForkedDatabase
+ fmt::Debug
+ Send
Expand Down Expand Up @@ -220,9 +207,9 @@ impl<T: DatabaseRef<Error = DatabaseError> + Send + Sync + Clone + fmt::Debug> D
}
}

impl<T: DatabaseRef<Error = DatabaseError>> MaybeHashDatabase for CacheDB<T> {
fn maybe_as_hash_db(&self) -> Option<(AsHashDB, B256)> {
Some(trie_hash_db(&self.accounts))
impl<T: DatabaseRef<Error = DatabaseError>> MaybeFullDatabase for CacheDB<T> {
fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
Some(&self.accounts)
}

fn clear_into_snapshot(&mut self) -> StateSnapshot {
Expand Down Expand Up @@ -279,12 +266,12 @@ impl<T: DatabaseRef<Error = DatabaseError>> MaybeForkedDatabase for CacheDB<T> {
}

/// Represents a state at certain point
pub struct StateDb(pub(crate) Box<dyn MaybeHashDatabase + Send + Sync>);
pub struct StateDb(pub(crate) Box<dyn MaybeFullDatabase + Send + Sync>);

// === impl StateDB ===

impl StateDb {
pub fn new(db: impl MaybeHashDatabase + Send + Sync + 'static) -> Self {
pub fn new(db: impl MaybeFullDatabase + Send + Sync + 'static) -> Self {
Self(Box::new(db))
}
}
Expand All @@ -308,13 +295,9 @@ impl DatabaseRef for StateDb {
}
}

impl MaybeHashDatabase for StateDb {
fn maybe_as_hash_db(&self) -> Option<(AsHashDB, B256)> {
self.0.maybe_as_hash_db()
}

fn maybe_account_db(&self, addr: Address) -> Option<(AsHashDB, B256)> {
self.0.maybe_account_db(addr)
impl MaybeFullDatabase for StateDb {
fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
self.0.maybe_as_full_db()
}

fn clear_into_snapshot(&mut self) -> StateSnapshot {
Expand Down
8 changes: 4 additions & 4 deletions crates/anvil/src/eth/backend/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Genesis settings

use crate::eth::backend::db::{Db, MaybeHashDatabase};
use crate::eth::backend::db::{Db, MaybeFullDatabase};
use alloy_genesis::{Genesis, GenesisAccount};
use alloy_primitives::{Address, B256, U256};
use foundry_evm::{
Expand Down Expand Up @@ -84,7 +84,7 @@ impl GenesisConfig {
/// [AccountInfo]
pub(crate) fn state_db_at_genesis<'a>(
&self,
db: Box<dyn MaybeHashDatabase + 'a>,
db: Box<dyn MaybeFullDatabase + 'a>,
) -> AtGenesisStateDb<'a> {
AtGenesisStateDb {
genesis: self.genesis_init.clone(),
Expand All @@ -103,7 +103,7 @@ impl GenesisConfig {
pub(crate) struct AtGenesisStateDb<'a> {
genesis: Option<Genesis>,
accounts: HashMap<Address, AccountInfo>,
db: Box<dyn MaybeHashDatabase + 'a>,
db: Box<dyn MaybeFullDatabase + 'a>,
}

impl<'a> DatabaseRef for AtGenesisStateDb<'a> {
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<'a> DatabaseRef for AtGenesisStateDb<'a> {
}
}

impl<'a> MaybeHashDatabase for AtGenesisStateDb<'a> {
impl<'a> MaybeFullDatabase for AtGenesisStateDb<'a> {
fn clear_into_snapshot(&mut self) -> StateSnapshot {
self.db.clear_into_snapshot()
}
Expand Down
6 changes: 3 additions & 3 deletions crates/anvil/src/eth/backend/mem/fork_db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
eth::backend::db::{
Db, MaybeForkedDatabase, MaybeHashDatabase, SerializableAccountRecord, SerializableState,
Db, MaybeForkedDatabase, MaybeFullDatabase, SerializableAccountRecord, SerializableState,
StateDb,
},
revm::primitives::AccountInfo,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Db for ForkedDatabase {
}
}

impl MaybeHashDatabase for ForkedDatabase {
impl MaybeFullDatabase for ForkedDatabase {
fn clear_into_snapshot(&mut self) -> StateSnapshot {
let db = self.inner().db();
let accounts = std::mem::take(&mut *db.accounts.write());
Expand All @@ -104,7 +104,7 @@ impl MaybeHashDatabase for ForkedDatabase {
}
}

impl MaybeHashDatabase for ForkDbSnapshot {
impl MaybeFullDatabase for ForkDbSnapshot {
fn clear_into_snapshot(&mut self) -> StateSnapshot {
std::mem::take(&mut self.snapshot)
}
Expand Down
25 changes: 9 additions & 16 deletions crates/anvil/src/eth/backend/mem/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

use crate::{
eth::backend::db::{
AsHashDB, Db, MaybeForkedDatabase, MaybeHashDatabase, SerializableAccountRecord,
SerializableState, StateDb,
Db, MaybeForkedDatabase, MaybeFullDatabase, SerializableAccountRecord, SerializableState,
StateDb,
},
mem::state::{state_merkle_trie_root, storage_trie_db, trie_hash_db},
revm::primitives::AccountInfo,
mem::state::state_root,
revm::{db::DbAccount, primitives::AccountInfo},
};
use alloy_primitives::{Address, B256, U256, U64};
use alloy_rpc_types::BlockId;
use foundry_evm::{
backend::{DatabaseResult, StateSnapshot},
fork::BlockchainDb,
hashbrown::HashMap,
};

// reexport for convenience
Expand Down Expand Up @@ -90,25 +91,17 @@ impl Db for MemDb {
}

fn maybe_state_root(&self) -> Option<B256> {
Some(state_merkle_trie_root(&self.inner.accounts))
Some(state_root(&self.inner.accounts))
}

fn current_state(&self) -> StateDb {
StateDb::new(MemDb { inner: self.inner.clone(), ..Default::default() })
}
}

impl MaybeHashDatabase for MemDb {
fn maybe_as_hash_db(&self) -> Option<(AsHashDB, B256)> {
Some(trie_hash_db(&self.inner.accounts))
}

fn maybe_account_db(&self, addr: Address) -> Option<(AsHashDB, B256)> {
if let Some(acc) = self.inner.accounts.get(&addr) {
Some(storage_trie_db(&acc.storage))
} else {
Some(storage_trie_db(&Default::default()))
}
impl MaybeFullDatabase for MemDb {
fn maybe_as_full_db(&self) -> Option<&HashMap<Address, DbAccount>> {
Some(&self.inner.accounts)
}

fn clear_into_snapshot(&mut self) -> StateSnapshot {
Expand Down
Loading
Loading