Skip to content

Commit

Permalink
chore(state): make bundle state non-optional (#828)
Browse files Browse the repository at this point in the history
* chore(state): make bundle state non-optional

* clippy
  • Loading branch information
rkrasiuk authored Oct 23, 2023
1 parent 206278e commit 11aa0c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
27 changes: 7 additions & 20 deletions crates/revm/src/db/states/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ pub struct State<DB: Database> {
pub transition_state: Option<TransitionState>,
/// After block is finishes we merge those changes inside bundle.
/// Bundle is used to update database and create changesets.
///
/// Bundle state can be present if we want to use preloaded bundle.
pub bundle_state: Option<BundleState>,
/// Bundle state can be set on initialization if we want to use preloaded bundle.
pub bundle_state: BundleState,
/// Addition layer that is going to be used to fetched values before fetching values
/// from database.
///
Expand All @@ -70,13 +69,8 @@ impl State<EmptyDB> {
impl<DB: Database> State<DB> {
/// Returns the size hint for the inner bundle state.
/// See [BundleState::size_hint] for more info.
///
/// Returns `0` if bundle state is not set.
pub fn bundle_size_hint(&self) -> usize {
self.bundle_state
.as_ref()
.map(|s| s.size_hint())
.unwrap_or_default()
self.bundle_state.size_hint()
}

/// Iterate over received balances and increment all account balances.
Expand Down Expand Up @@ -173,7 +167,6 @@ impl<DB: Database> State<DB> {
pub fn merge_transitions(&mut self, retention: BundleRetention) {
if let Some(transition_state) = self.transition_state.as_mut().map(TransitionState::take) {
self.bundle_state
.get_or_insert(BundleState::default())
.apply_transitions_and_create_reverts(transition_state, retention);
}
}
Expand All @@ -183,10 +176,8 @@ impl<DB: Database> State<DB> {
hash_map::Entry::Vacant(entry) => {
if self.use_preloaded_bundle {
// load account from bundle state
if let Some(Some(account)) = self
.bundle_state
.as_ref()
.map(|bundle| bundle.account(&address).cloned().map(Into::into))
if let Some(account) =
self.bundle_state.account(&address).cloned().map(Into::into)
{
return Ok(entry.insert(account));
}
Expand Down Expand Up @@ -217,7 +208,7 @@ impl<DB: Database> State<DB> {
///
/// this will panic.
pub fn take_bundle(&mut self) -> BundleState {
core::mem::take(self.bundle_state.as_mut().unwrap())
core::mem::take(&mut self.bundle_state)
}
}

Expand All @@ -233,11 +224,7 @@ impl<DB: Database> Database for State<DB> {
hash_map::Entry::Occupied(entry) => Ok(entry.get().clone()),
hash_map::Entry::Vacant(entry) => {
if self.use_preloaded_bundle {
if let Some(Some(code)) = self
.bundle_state
.as_ref()
.map(|bundle| bundle.contracts.get(&code_hash))
{
if let Some(code) = self.bundle_state.contracts.get(&code_hash) {
entry.insert(code.clone());
return Ok(code.clone());
}
Expand Down
8 changes: 2 additions & 6 deletions crates/revm/src/db/states/state_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,8 @@ impl<DB: Database> StateBuilder<DB> {
.with_cache_prestate
.unwrap_or_else(|| CacheState::new(self.with_state_clear)),
database: self.database,
transition_state: if self.with_bundle_update {
Some(TransitionState::default())
} else {
None
},
bundle_state: self.with_bundle_prestate,
transition_state: self.with_bundle_update.then(TransitionState::default),
bundle_state: self.with_bundle_prestate.unwrap_or_default(),
use_preloaded_bundle,
block_hashes: self.with_block_hashes,
}
Expand Down

0 comments on commit 11aa0c8

Please sign in to comment.