Skip to content

Commit

Permalink
fix(anvil): clear db on reset
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Oct 17, 2022
1 parent b60deaa commit 700dbcd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion anvil/src/eth/backend/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub trait MaybeHashDatabase: DatabaseRef<Error = DatabaseError> {
/// Clear the state and move it into a new `StateSnapshot`
fn clear_into_snapshot(&mut self) -> StateSnapshot;

/// Clears the entire database
fn clear(&mut self);

/// Reverses `clear_into_snapshot` by initializing the db's state with the snapshot
fn init_from_snapshot(&mut self, snapshot: StateSnapshot);
}
Expand All @@ -57,9 +60,11 @@ where
}

fn clear_into_snapshot(&mut self) -> StateSnapshot {
unimplemented!()
unreachable!("never called for DatabaseRef")
}

fn clear(&mut self) {}

fn init_from_snapshot(&mut self, _snapshot: StateSnapshot) {}
}

Expand Down Expand Up @@ -192,6 +197,10 @@ impl<T: DatabaseRef<Error = DatabaseError>> MaybeHashDatabase for CacheDB<T> {
StateSnapshot { accounts, storage: account_storage, block_hashes }
}

fn clear(&mut self) {
self.clear_into_snapshot();
}

fn init_from_snapshot(&mut self, snapshot: StateSnapshot) {
let StateSnapshot { accounts, mut storage, block_hashes } = snapshot;

Expand Down Expand Up @@ -255,6 +264,10 @@ impl MaybeHashDatabase for StateDb {
self.0.clear_into_snapshot()
}

fn clear(&mut self) {
self.0.clear()
}

fn init_from_snapshot(&mut self, snapshot: StateSnapshot) {
self.0.init_from_snapshot(snapshot)
}
Expand Down
10 changes: 10 additions & 0 deletions anvil/src/eth/backend/mem/fork_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ impl MaybeHashDatabase for ForkedDatabase {
StateSnapshot { accounts, storage, block_hashes }
}

fn clear(&mut self) {
self.flush_cache();
self.clear_into_snapshot();
}

fn init_from_snapshot(&mut self, snapshot: StateSnapshot) {
let db = self.inner().db();
let StateSnapshot { accounts, storage, block_hashes } = snapshot;
Expand All @@ -70,6 +75,11 @@ impl MaybeHashDatabase for ForkDbSnapshot {
std::mem::take(&mut self.snapshot)
}

fn clear(&mut self) {
std::mem::take(&mut self.snapshot);
self.local.clear()
}

fn init_from_snapshot(&mut self, snapshot: StateSnapshot) {
self.snapshot = snapshot;
}
Expand Down
4 changes: 4 additions & 0 deletions anvil/src/eth/backend/mem/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ impl MaybeHashDatabase for MemDb {
self.inner.clear_into_snapshot()
}

fn clear(&mut self) {
self.inner.clear();
}

fn init_from_snapshot(&mut self, snapshot: StateSnapshot) {
self.inner.init_from_snapshot(snapshot)
}
Expand Down
4 changes: 4 additions & 0 deletions anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ impl Backend {
// insert back all genesis accounts, by reusing cached `AccountInfo`s we don't need to
// fetch the data via RPC again
let mut db = self.db.write().await;

// clear database
db.clear();

let fork_genesis_infos = self.genesis.fork_genesis_account_infos.lock();
for (address, info) in
self.genesis.accounts.iter().copied().zip(fork_genesis_infos.iter().cloned())
Expand Down

0 comments on commit 700dbcd

Please sign in to comment.