From acf00086622049c1522cbbad066806491e9c0767 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 6 Sep 2023 16:25:36 +0200 Subject: [PATCH] chore: update methods --- bins/revme/src/statetest/runner.rs | 3 +- crates/revm/src/db/states/state.rs | 16 +++------ crates/revm/src/db/states/state_builder.rs | 39 +++++++++++++--------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/bins/revme/src/statetest/runner.rs b/bins/revme/src/statetest/runner.rs index c46b081e99..f412ae75db 100644 --- a/bins/revme/src/statetest/runner.rs +++ b/bins/revme/src/statetest/runner.rs @@ -5,7 +5,6 @@ use super::{ use hex_literal::hex; use indicatif::ProgressBar; use revm::{ - db::EmptyDB, inspectors::TracerEip3155, interpreter::CreateScheme, primitives::{ @@ -256,7 +255,7 @@ pub fn execute_test_suit( env.cfg.spec_id, revm::primitives::SpecId::SPURIOUS_DRAGON, )); - let mut state = revm::db::StateBuilder::::default() + let mut state = revm::db::StateBuilder::new() .with_cached_prestate(cache) .with_bundle_update() .build(); diff --git a/crates/revm/src/db/states/state.rs b/crates/revm/src/db/states/state.rs index f17f5b53a4..3701ad4ca5 100644 --- a/crates/revm/src/db/states/state.rs +++ b/crates/revm/src/db/states/state.rs @@ -272,7 +272,7 @@ mod tests { use crate::{ db::{ states::reverts::AccountInfoRevert, AccountRevert, AccountStatus, BundleAccount, - EmptyDB, RevertToSlot, + RevertToSlot, }, StateBuilder, }; @@ -280,7 +280,7 @@ mod tests { #[test] fn block_hash_cache() { - let mut state = StateBuilder::::default().build(); + let mut state = StateBuilder::new().build(); state.block_hash(U256::from(1)).unwrap(); state.block_hash(U256::from(2)).unwrap(); @@ -310,9 +310,7 @@ mod tests { /// state of the account before the block. #[test] fn reverts_preserve_old_values() { - let mut state = StateBuilder::::default() - .with_bundle_update() - .build(); + let mut state = StateBuilder::new().with_bundle_update().build(); let (slot1, slot2, slot3) = (U256::from(1), U256::from(2), U256::from(3)); @@ -557,9 +555,7 @@ mod tests { /// Checks that the accounts and storages that are changed within the block and reverted to their previous state do not appear in the reverts. #[test] fn bundle_scoped_reverts_collapse() { - let mut state = StateBuilder::::default() - .with_bundle_update() - .build(); + let mut state = StateBuilder::new().with_bundle_update().build(); // Non-existing account. let new_account_address = B160::from_slice(&[0x1; 20]); @@ -725,9 +721,7 @@ mod tests { /// Checks that the behavior of selfdestruct within the block is correct. #[test] fn selfdestruct_state_and_reverts() { - let mut state = StateBuilder::::default() - .with_bundle_update() - .build(); + let mut state = StateBuilder::new().with_bundle_update().build(); // Existing account. let existing_account_address = B160::from_slice(&[0x1; 20]); diff --git a/crates/revm/src/db/states/state_builder.rs b/crates/revm/src/db/states/state_builder.rs index d834b48e60..e22f45e17e 100644 --- a/crates/revm/src/db/states/state_builder.rs +++ b/crates/revm/src/db/states/state_builder.rs @@ -5,14 +5,11 @@ use revm_interpreter::primitives::{db::Database, B256}; /// Allows building of State and initializing it with different options. pub struct StateBuilder { + /// Database that we use to fetch data from. + database: DB, /// Enabled state clear flag that is introduced in Spurious Dragon hardfork. /// Default is true as spurious dragon happened long time ago. with_state_clear: bool, - /// Optional database that we use to fetch data from. If database is not present, we will - /// return not existing account and storage. - /// - /// Note: It is marked as Send so database can be shared between threads. - database: DB, //Box + Send + 'a>, /// if there is prestate that we want to use. /// This would mean that we have additional state layer between evm and disk/database. with_bundle_prestate: Option, @@ -29,11 +26,28 @@ pub struct StateBuilder { with_block_hashes: BTreeMap, } -impl Default for StateBuilder { +impl StateBuilder { + /// Create a new builder with an empty database. + /// + /// If you want to instatiate it with a specific database, use + /// [`new_with_database`](Self::new_with_database). + pub fn new() -> Self { + Self::default() + } +} + +impl Default for StateBuilder { fn default() -> Self { + Self::new_with_database(DB::default()) + } +} + +impl StateBuilder { + /// Create a new builder with the given database. + pub fn new_with_database(database: DB) -> Self { Self { + database, with_state_clear: true, - database: DB::default(), with_cache_prestate: None, with_bundle_prestate: None, with_bundle_update: false, @@ -41,14 +55,9 @@ impl Default for StateBuilder { with_block_hashes: BTreeMap::new(), } } -} - -impl StateBuilder { - /// Create default instance of builder. - pub fn new() -> StateBuilder> { - StateBuilder::>::default() - } + #[doc(hidden)] + #[deprecated = "instantiate with `new_with_database` instead"] pub fn with_database_boxed<'a, NewDBError>( self, database: Box + Send + 'a>, @@ -135,7 +144,7 @@ impl StateBuilder { State { cache: self .with_cache_prestate - .unwrap_or(CacheState::new(self.with_state_clear)), + .unwrap_or_else(|| CacheState::new(self.with_state_clear)), database: self.database, transition_state: if self.with_bundle_update { Some(TransitionState::default())