From 9ee522aeeeff2994c9db7129525ae47b11c2d12a Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Thu, 27 Jul 2023 15:32:48 -0400 Subject: [PATCH 1/4] feat: add in-place insert/remove methods for snapshots --- evm/src/executor/snapshot.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/evm/src/executor/snapshot.rs b/evm/src/executor/snapshot.rs index c5b39a02dcc16..39c2f42c6da98 100644 --- a/evm/src/executor/snapshot.rs +++ b/evm/src/executor/snapshot.rs @@ -41,12 +41,25 @@ impl Snapshots { snapshot } + /// Removes the snapshot with the given `id`. + /// + /// Does not remove snapshots after it. + pub fn remove_at(&mut self, id: U256) -> Option { + self.snapshots.remove(&id) + } + /// Inserts the new snapshot and returns the id pub fn insert(&mut self, snapshot: T) -> U256 { let id = self.next_id(); self.snapshots.insert(id, snapshot); id } + + /// Inserts the new snapshot at the given `id`, without auto-incrementing the next `id`. + pub fn insert_at(&mut self, snapshot: T, id: U256) -> bool { + self.snapshots.insert(id, snapshot); + true + } } impl Default for Snapshots { From 4bfc0e3f25a0c14039df10aa530022915c20f971 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Thu, 27 Jul 2023 15:33:13 -0400 Subject: [PATCH 2/4] chore: make fork snapshots clone --- evm/src/executor/backend/snapshot.rs | 2 +- evm/src/executor/fork/database.rs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/evm/src/executor/backend/snapshot.rs b/evm/src/executor/backend/snapshot.rs index ce9db6e3edce4..b38689737c1a0 100644 --- a/evm/src/executor/backend/snapshot.rs +++ b/evm/src/executor/backend/snapshot.rs @@ -6,7 +6,7 @@ use revm::{ use serde::{Deserialize, Serialize}; /// A minimal abstraction of a state at a certain point in time -#[derive(Default, Debug, Serialize, Deserialize)] +#[derive(Default, Clone, Debug, Serialize, Deserialize)] pub struct StateSnapshot { pub accounts: Map, pub storage: Map>, diff --git a/evm/src/executor/fork/database.rs b/evm/src/executor/fork/database.rs index 359fcb04db9f5..152d6144cac03 100644 --- a/evm/src/executor/fork/database.rs +++ b/evm/src/executor/fork/database.rs @@ -114,8 +114,9 @@ impl ForkedDatabase { } pub fn revert_snapshot(&mut self, id: U256) -> bool { - let snapshot = { self.snapshots().lock().remove(id) }; + let snapshot = { self.snapshots().lock().remove_at(id) }; if let Some(snapshot) = snapshot { + self.snapshots().lock().insert_at(snapshot.clone(), id); let ForkDbSnapshot { local, snapshot: StateSnapshot { accounts, storage, block_hashes }, @@ -200,7 +201,7 @@ impl DatabaseCommit for ForkedDatabase { /// Represents a snapshot of the database /// /// This mimics `revm::CacheDB` -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct ForkDbSnapshot { pub local: CacheDB, pub snapshot: StateSnapshot, From 0a36ef77b21f7f332f2b395d3c7da85da5f97bd6 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Thu, 27 Jul 2023 15:33:49 -0400 Subject: [PATCH 3/4] chore: persist snapshots before rolling them back --- evm/src/executor/backend/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/evm/src/executor/backend/mod.rs b/evm/src/executor/backend/mod.rs index aa4329eec7774..bb5f730cf50f2 100644 --- a/evm/src/executor/backend/mod.rs +++ b/evm/src/executor/backend/mod.rs @@ -880,7 +880,9 @@ impl DatabaseExt for Backend { current: &mut Env, ) -> Option { trace!(?id, "revert snapshot"); - if let Some(mut snapshot) = self.inner.snapshots.remove(id) { + if let Some(mut snapshot) = self.inner.snapshots.remove_at(id) { + // Re-insert snapshot to persist it + self.inner.snapshots.insert_at(snapshot.clone(), id); // need to check whether there's a global failure which means an error occurred either // during the snapshot or even before if self.is_global_failure(current_state) { From ddeb69c5864d803779f0ad183c8df7f9b378d593 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Thu, 27 Jul 2023 15:49:36 -0400 Subject: [PATCH 4/4] chore: make api consistent --- evm/src/executor/snapshot.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/evm/src/executor/snapshot.rs b/evm/src/executor/snapshot.rs index 39c2f42c6da98..a549362b101ae 100644 --- a/evm/src/executor/snapshot.rs +++ b/evm/src/executor/snapshot.rs @@ -55,10 +55,12 @@ impl Snapshots { id } - /// Inserts the new snapshot at the given `id`, without auto-incrementing the next `id`. - pub fn insert_at(&mut self, snapshot: T, id: U256) -> bool { + /// Inserts the new snapshot at the given `id`. + /// + /// Does not auto-increment the next `id`. + pub fn insert_at(&mut self, snapshot: T, id: U256) -> U256 { self.snapshots.insert(id, snapshot); - true + id } }