Skip to content

Commit

Permalink
feat(StateBuilder): switch builder option from without_bundle to with…
Browse files Browse the repository at this point in the history
…_bundle (bluealloy#688)

* feat(StateBuilder): make builder option  become

* make private field, nit rename

* fix tests
  • Loading branch information
rakita authored and DaniPopes committed Sep 6, 2023
1 parent 37ab8c4 commit 3ce1d60
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 2 additions & 0 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub fn execute_test_suit(
));
let mut state = revm::db::StateBuilder::default()
.with_cached_prestate(cache)
.with_bundle_update()
.build();
let mut evm = revm::new();
evm.database(&mut state);
Expand Down Expand Up @@ -294,6 +295,7 @@ pub fn execute_test_suit(
));
let mut state = revm::db::StateBuilder::default()
.with_cached_prestate(cache)
.with_bundle_update()
.build();
evm.database(&mut state);
let _ =
Expand Down
6 changes: 3 additions & 3 deletions crates/revm/src/db/states/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ mod tests {
/// state of the account before the block.
#[test]
fn reverts_preserve_old_values() {
let mut state = StateBuilder::default().build();
let mut state = StateBuilder::default().with_bundle_update().build();

let (slot1, slot2, slot3) = (U256::from(1), U256::from(2), U256::from(3));

Expand Down Expand Up @@ -555,7 +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().build();
let mut state = StateBuilder::default().with_bundle_update().build();

// Non-existing account.
let new_account_address = B160::from_slice(&[0x1; 20]);
Expand Down Expand Up @@ -721,7 +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().build();
let mut state = StateBuilder::default().with_bundle_update().build();

// Existing account.
let existing_account_address = B160::from_slice(&[0x1; 20]);
Expand Down
38 changes: 20 additions & 18 deletions crates/revm/src/db/states/state_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@ use revm_interpreter::primitives::{db::Database, B256};

/// Allows building of State and initializing it with different options.
pub struct StateBuilder<DB> {
pub with_state_clear: bool,
/// 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.
pub database: DB, //Box<dyn Database<Error = DBError> + Send + 'a>,
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.
pub with_bundle_prestate: Option<BundleState>,
with_bundle_prestate: Option<BundleState>,
/// This will initialize cache to this state.
pub with_cache_prestate: Option<CacheState>,
with_cache_prestate: Option<CacheState>,
/// Do we want to create reverts and update bundle state.
/// Default is true.
pub without_bundle_update: bool,
/// Default is false.
with_bundle_update: bool,
/// Do we want to merge transitions in background.
/// This will allows evm to continue executing.
/// Default is false.
pub with_background_transition_merge: bool,
with_background_transition_merge: bool,
/// If we want to set different block hashes
pub with_block_hashes: BTreeMap<u64, B256>,
with_block_hashes: BTreeMap<u64, B256>,
}

impl Default for StateBuilder<Box<EmptyDB>> {
Expand All @@ -34,7 +36,7 @@ impl Default for StateBuilder<Box<EmptyDB>> {
database: Box::<EmptyDB>::default(),
with_cache_prestate: None,
with_bundle_prestate: None,
without_bundle_update: false,
with_bundle_update: false,
with_background_transition_merge: false,
with_block_hashes: BTreeMap::new(),
}
Expand All @@ -58,7 +60,7 @@ impl<DB: Database> StateBuilder<DB> {
database,
with_cache_prestate: self.with_cache_prestate,
with_bundle_prestate: self.with_bundle_prestate,
without_bundle_update: self.without_bundle_update,
with_bundle_update: self.with_bundle_update,
with_background_transition_merge: self.with_background_transition_merge,
with_block_hashes: self.with_block_hashes,
}
Expand All @@ -85,13 +87,13 @@ impl<DB: Database> StateBuilder<DB> {
}
}

/// Don't make transitions and don't update bundle state.
/// Make transitions and update bundle state.
///
/// This is good option if we don't care about creating reverts
/// or getting output of changed states.
pub fn without_bundle_update(self) -> Self {
/// This is needed option if we want to create reverts
/// and getting output of changed states.
pub fn with_bundle_update(self) -> Self {
Self {
without_bundle_update: true,
with_bundle_update: true,
..self
}
}
Expand Down Expand Up @@ -135,10 +137,10 @@ impl<DB: Database> StateBuilder<DB> {
.with_cache_prestate
.unwrap_or(CacheState::new(self.with_state_clear)),
database: self.database,
transition_state: if self.without_bundle_update {
None
} else {
transition_state: if self.with_bundle_update {
Some(TransitionState::default())
} else {
None
},
bundle_state: self.with_bundle_prestate,
use_preloaded_bundle,
Expand Down

0 comments on commit 3ce1d60

Please sign in to comment.