Skip to content

Commit

Permalink
chore: make impl Default for StateBuilder generic (bluealloy#690)
Browse files Browse the repository at this point in the history
* chore: make `impl Default for StateBuilder` generic

chore: update methods

* Update crates/revm/src/db/states/state_builder.rs
  • Loading branch information
DaniPopes authored and mikelodder7 committed Sep 12, 2023
1 parent 1d7fb9a commit 743d0e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
26 changes: 13 additions & 13 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use std::io::stdout;
use std::{
ffi::OsStr,
path::{Path, PathBuf},
sync::{atomic::AtomicBool, Arc, Mutex},
time::{Duration, Instant},
};

use super::{
merkle_trie::{log_rlp_hash, state_merkle_trie_root},
models::{SpecName, TestSuit},
};
use hex_literal::hex;
use indicatif::ProgressBar;
use revm::inspectors::TracerEip3155;
use revm::primitives::keccak256;
use revm::{
inspectors::TracerEip3155,
interpreter::CreateScheme,
primitives::{Bytecode, Env, ExecutionResult, HashMap, SpecId, TransactTo, B160, B256, U256},
primitives::{
keccak256, Bytecode, Env, ExecutionResult, HashMap, SpecId, TransactTo, B160, B256, U256,
},
};
use std::{
ffi::OsStr,
io::stdout,
path::{Path, PathBuf},
sync::atomic::Ordering,
sync::{atomic::AtomicBool, Arc, Mutex},
time::{Duration, Instant},
};
use std::sync::atomic::Ordering;
use thiserror::Error;
use walkdir::{DirEntry, WalkDir};

Expand Down Expand Up @@ -255,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::State::builder()
.with_cached_prestate(cache)
.with_bundle_update()
.build();
Expand Down
41 changes: 24 additions & 17 deletions crates/revm/src/db/states/state_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ use revm_interpreter::primitives::{

/// 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,
/// 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 @@ -32,27 +29,37 @@ pub struct StateBuilder<DB> {
with_block_hashes: BTreeMap<u64, B256>,
}

impl Default for StateBuilder<EmptyDB> {
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: EmptyDB::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<EmptyDB> {
StateBuilder::default()
}

/// With generic database.
/// Set the database.
pub fn with_database<ODB: Database>(self, database: ODB) -> StateBuilder<ODB> {
// cast to the different database,
// Note that we return different type depending of the database NewDBError.
Expand All @@ -67,7 +74,7 @@ impl<DB: Database> StateBuilder<DB> {
}
}

/// Takes [DatabaseRef] and wraps [WrapDatabaseRef] around it.
/// Takes [DatabaseRef] and wraps it with [WrapDatabaseRef].
pub fn with_database_ref<ODB: DatabaseRef>(
self,
database: ODB,
Expand Down Expand Up @@ -152,7 +159,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 743d0e2

Please sign in to comment.