Skip to content

[Feature] Relax trait bound EVM::DB = &'db mut State<DB> for BlockExecutor impls#234

Merged
mattsse merged 8 commits intoalloy-rs:mainfrom
0xForerunner:forerunner/state-db
Dec 1, 2025
Merged

[Feature] Relax trait bound EVM::DB = &'db mut State<DB> for BlockExecutor impls#234
mattsse merged 8 commits intoalloy-rs:mainfrom
0xForerunner:forerunner/state-db

Conversation

@0xForerunner
Copy link
Contributor

@0xForerunner 0xForerunner commented Nov 27, 2025

closes #233

Solution

Creates a new trait StateDB to encapsulate some functionality from State.

We would like to reuse this code in our custom block builder but we need to pass in a wrapped version of State. Currently this is not possible. This is the only solution I could come up with that doesn't contain any breaking changes. Hope this works for you. Happy to explore other solutions as well if this doesn't meet all the requirements.

PR Checklist

  • Added Tests
  • Added Documentation
  • No Breaking changes

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some nits,

this is valid use case and I think we can do this

wdyt @klkvr

Comment on lines 477 to 481
/// A type which has the state of the blockchain.
///
/// This trait encapsulates some of the functionality found in [`State`]
#[auto_impl::auto_impl(&mut)]
pub trait StateDB {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense,

I also think we'd might need something like this for BAL anyway eventually

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny you mention that haha. I'm working on implementing BAL for flashblocks!

Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes sense to me, I've considered something like this before but we didn't yet have a hard requirement for non-State db on reth side

Comment on lines 488 to 499
/// Iterates over received balances and increment all account balances.
///
/// **Note**: If account is not found inside cache state it will be loaded from database.
///
/// Update will create transitions for all accounts that are updated.
///
/// If using this to implement withdrawals, zero balances must be filtered out before calling
/// this function.
fn increment_balances(
&mut self,
balances: impl IntoIterator<Item = (Address, u128)>,
) -> Result<(), Self::Error>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be possible to implement this via just Database + DatabaseCommit API but maybe not worth it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is. I started doing this but felt bad rewriting the impl. Would you prefer I upstream a new DatabaseCommitExt trait into revm? We could also have the trait live here. I'm not sure what makes more sense.

Copy link
Contributor Author

@0xForerunner 0xForerunner Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@klkvr I think this PR would be the ideal solution if we want to go this route.

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pending @klkvr

) -> Result<(), Self::Error>;
}

/// auto_impl unable to reconcile return associated type from supertrait
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe <Self as Database>::Error works here, but we can keep this as is as welll

/// A type which has the state of the blockchain.
///
/// This trait encapsulates some of the functionality found in [`State`]
pub trait StateDB: revm::Database {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo we can keep StateDB here, hard to find a good name here -.-

@mattsse mattsse merged commit 74c0650 into alloy-rs:main Dec 1, 2025
27 checks passed
theochap pushed a commit to ethereum-optimism/optimism that referenced this pull request Jan 16, 2026
…Executor` impls (alloy-rs/evm#234)

* feat: StateDB trait

* feat: replace &mut State<DB> with impl StateDB

* feat: move StateDB to state.rs

* feat: remove StateDB associated type in favour of supertrait

* feat: StateDB use fully qualified syntax

* Apply suggestion from @klkvr

* Apply suggestion from @klkvr

* Apply suggestion from @klkvr

---------

Co-authored-by: Arsenii Kulikov <klkvrr@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Relax trait bound EVM::DB = &'db mut State<DB> for BlockExecutor impls

4 participants