Skip to content

Commit

Permalink
chore: update methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Sep 6, 2023
1 parent 089fbf5 commit acf0008
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
3 changes: 1 addition & 2 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::{
use hex_literal::hex;
use indicatif::ProgressBar;
use revm::{
db::EmptyDB,
inspectors::TracerEip3155,
interpreter::CreateScheme,
primitives::{
Expand Down Expand Up @@ -256,7 +255,7 @@ pub fn execute_test_suit(
env.cfg.spec_id,
revm::primitives::SpecId::SPURIOUS_DRAGON,
));
let mut state = revm::db::StateBuilder::<EmptyDB>::default()
let mut state = revm::db::StateBuilder::new()
.with_cached_prestate(cache)
.with_bundle_update()
.build();
Expand Down
16 changes: 5 additions & 11 deletions crates/revm/src/db/states/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,15 @@ mod tests {
use crate::{
db::{
states::reverts::AccountInfoRevert, AccountRevert, AccountStatus, BundleAccount,
EmptyDB, RevertToSlot,
RevertToSlot,
},
StateBuilder,
};
use revm_interpreter::primitives::{keccak256, StorageSlot};

#[test]
fn block_hash_cache() {
let mut state = StateBuilder::<EmptyDB>::default().build();
let mut state = StateBuilder::new().build();
state.block_hash(U256::from(1)).unwrap();
state.block_hash(U256::from(2)).unwrap();

Expand Down Expand Up @@ -310,9 +310,7 @@ mod tests {
/// state of the account before the block.
#[test]
fn reverts_preserve_old_values() {
let mut state = StateBuilder::<EmptyDB>::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));

Expand Down Expand Up @@ -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::<EmptyDB>::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]);
Expand Down Expand Up @@ -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::<EmptyDB>::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]);
Expand Down
39 changes: 24 additions & 15 deletions crates/revm/src/db/states/state_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ use revm_interpreter::primitives::{db::Database, B256};

/// Allows building of State and initializing it with different options.
pub struct StateBuilder<DB> {
/// 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<dyn Database<Error = DBError> + 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<BundleState>,
Expand All @@ -29,26 +26,38 @@ pub struct StateBuilder<DB> {
with_block_hashes: BTreeMap<u64, B256>,
}

impl<DB: Default> Default for StateBuilder<DB> {
impl StateBuilder<EmptyDB> {
/// 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<DB: Database + Default> Default for StateBuilder<DB> {
fn default() -> Self {
Self::new_with_database(DB::default())
}
}

impl<DB: Database> StateBuilder<DB> {
/// 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,
with_background_transition_merge: false,
with_block_hashes: BTreeMap::new(),
}
}
}

impl<DB: Database> StateBuilder<DB> {
/// Create default instance of builder.
pub fn new() -> StateBuilder<Box<EmptyDB>> {
StateBuilder::<Box<EmptyDB>>::default()
}

#[doc(hidden)]
#[deprecated = "instantiate with `new_with_database` instead"]
pub fn with_database_boxed<'a, NewDBError>(
self,
database: Box<dyn Database<Error = NewDBError> + Send + 'a>,
Expand Down Expand Up @@ -135,7 +144,7 @@ impl<DB: Database> StateBuilder<DB> {
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())
Expand Down

0 comments on commit acf0008

Please sign in to comment.