diff --git a/anvil/src/eth/backend/db.rs b/anvil/src/eth/backend/db.rs index 4b27d5923ef1..73bddace92c1 100644 --- a/anvil/src/eth/backend/db.rs +++ b/anvil/src/eth/backend/db.rs @@ -41,6 +41,9 @@ pub trait MaybeHashDatabase: DatabaseRef { /// 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); } @@ -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) {} } @@ -192,6 +197,10 @@ impl> MaybeHashDatabase for CacheDB { 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; @@ -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) } diff --git a/anvil/src/eth/backend/mem/fork_db.rs b/anvil/src/eth/backend/mem/fork_db.rs index b4a7aff59f56..2a70ae9e698c 100644 --- a/anvil/src/eth/backend/mem/fork_db.rs +++ b/anvil/src/eth/backend/mem/fork_db.rs @@ -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; @@ -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; } diff --git a/anvil/src/eth/backend/mem/in_memory_db.rs b/anvil/src/eth/backend/mem/in_memory_db.rs index 43341dda5343..b1a24de07eb4 100644 --- a/anvil/src/eth/backend/mem/in_memory_db.rs +++ b/anvil/src/eth/backend/mem/in_memory_db.rs @@ -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) } diff --git a/anvil/src/eth/backend/mem/mod.rs b/anvil/src/eth/backend/mem/mod.rs index 245c6ab7730a..27237f81420a 100644 --- a/anvil/src/eth/backend/mem/mod.rs +++ b/anvil/src/eth/backend/mem/mod.rs @@ -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())