Skip to content

fix: restore commit bounds to drivers #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions src/driver/alloy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ use alloy::{
EthSendBundle,
},
};
use revm::primitives::{EVMError, ExecutionResult, SpecId};
use revm::{
primitives::{EVMError, ExecutionResult, SpecId},
Database, DatabaseCommit,
};

/// Possible errors that can occur while driving a bundle.
pub enum BundleError<Db: revm::Database> {
pub enum BundleError<Db: Database> {
/// The block number of the bundle does not match the block number of the revm block configuration.
BlockNumberMismatch,
/// The timestamp of the bundle is out of range.
Expand All @@ -38,7 +41,7 @@ pub enum BundleError<Db: revm::Database> {
},
}

impl<Db: revm::Database> core::fmt::Display for BundleError<Db> {
impl<Db: Database> core::fmt::Display for BundleError<Db> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::BlockNumberMismatch => {
Expand All @@ -58,25 +61,25 @@ impl<Db: revm::Database> core::fmt::Display for BundleError<Db> {
}
}

impl<Db: revm::Database> From<alloy::eips::eip2718::Eip2718Error> for BundleError<Db> {
impl<Db: Database> From<alloy::eips::eip2718::Eip2718Error> for BundleError<Db> {
fn from(err: alloy::eips::eip2718::Eip2718Error) -> Self {
Self::TransactionDecodingError(err)
}
}

impl<Db: revm::Database> From<alloy::primitives::SignatureError> for BundleError<Db> {
impl<Db: Database> From<alloy::primitives::SignatureError> for BundleError<Db> {
fn from(err: alloy::primitives::SignatureError) -> Self {
Self::TransactionSenderRecoveryError(err)
}
}

impl<Db: revm::Database> From<EVMError<Db::Error>> for BundleError<Db> {
impl<Db: Database> From<EVMError<Db::Error>> for BundleError<Db> {
fn from(inner: EVMError<Db::Error>) -> Self {
Self::EVMError { inner }
}
}

impl<Db: revm::Database> std::error::Error for BundleError<Db> {
impl<Db: Database> std::error::Error for BundleError<Db> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::TransactionDecodingError(err) => Some(err),
Expand All @@ -86,7 +89,7 @@ impl<Db: revm::Database> std::error::Error for BundleError<Db> {
}
}

impl<Db: revm::Database> core::fmt::Debug for BundleError<Db> {
impl<Db: Database> core::fmt::Debug for BundleError<Db> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::TimestampOutOfRange => write!(f, "TimestampOutOfRange"),
Expand Down Expand Up @@ -141,7 +144,7 @@ where

impl<B, R> BundleProcessor<B, R> {
/// Decode and validate the transactions in the bundle, performing EIP4844 gas checks.
pub fn decode_and_validate_txs<Db: revm::Database>(
pub fn decode_and_validate_txs<Db: Database + DatabaseCommit>(
txs: &[Bytes],
) -> Result<Vec<TxEnvelope>, BundleError<Db>> {
let txs = txs
Expand Down Expand Up @@ -175,7 +178,7 @@ impl BundleProcessor<EthCallBundle, EthCallBundleResponse> {
}

/// Process a bundle transaction and accumulate the results into a [EthCallBundleTransactionResult].
pub fn process_call_bundle_tx<Db: revm::Database>(
pub fn process_call_bundle_tx<Db: Database + DatabaseCommit>(
tx: &TxEnvelope,
pre_sim_coinbase_balance: U256,
post_sim_coinbase_balance: U256,
Expand Down Expand Up @@ -257,9 +260,9 @@ impl BundleProcessor<EthCallBundle, EthCallBundleResponse> {
}

impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResponse> {
type Error<Db: revm::Database> = BundleError<Db>;
type Error<Db: Database + DatabaseCommit> = BundleError<Db>;

fn run_bundle<'a, Db: revm::Database + revm::DatabaseCommit>(
fn run_bundle<'a, Db: Database + revm::DatabaseCommit>(
&mut self,
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
) -> DriveBundleResult<'a, Ext, Db, Self> {
Expand Down Expand Up @@ -389,7 +392,7 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
}
}

fn post_bundle<Db: revm::Database + revm::DatabaseCommit>(
fn post_bundle<Db: Database + revm::DatabaseCommit>(
&mut self,
_trevm: &crate::EvmNeedsTx<'_, Ext, Db>,
) -> Result<(), Self::Error<Db>> {
Expand All @@ -398,9 +401,9 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
}

impl<Ext> BundleDriver<Ext> for BundleProcessor<EthSendBundle, EthBundleHash> {
type Error<Db: revm::Database> = BundleError<Db>;
type Error<Db: Database + DatabaseCommit> = BundleError<Db>;

fn run_bundle<'a, Db: revm::Database + revm::DatabaseCommit>(
fn run_bundle<'a, Db: Database + revm::DatabaseCommit>(
&mut self,
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
) -> DriveBundleResult<'a, Ext, Db, Self> {
Expand Down Expand Up @@ -476,7 +479,7 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthSendBundle, EthBundleHash> {
}
}

fn post_bundle<Db: revm::Database + revm::DatabaseCommit>(
fn post_bundle<Db: Database + revm::DatabaseCommit>(
&mut self,
_trevm: &crate::EvmNeedsTx<'_, Ext, Db>,
) -> Result<(), Self::Error<Db>> {
Expand Down Expand Up @@ -541,9 +544,9 @@ impl From<EthCallBundle> for BundleBlockFiller {
}

impl<Ext> BundleDriver<Ext> for EthCallBundle {
type Error<Db: revm::Database> = BundleError<Db>;
type Error<Db: Database + DatabaseCommit> = BundleError<Db>;

fn run_bundle<'a, Db: revm::Database + revm::DatabaseCommit>(
fn run_bundle<'a, Db: Database + revm::DatabaseCommit>(
&mut self,
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
) -> DriveBundleResult<'a, Ext, Db, Self> {
Expand Down Expand Up @@ -619,7 +622,7 @@ impl<Ext> BundleDriver<Ext> for EthCallBundle {
}
}

fn post_bundle<Db: revm::Database + revm::DatabaseCommit>(
fn post_bundle<Db: Database + revm::DatabaseCommit>(
&mut self,
_trevm: &crate::EvmNeedsTx<'_, Ext, Db>,
) -> Result<(), Self::Error<Db>> {
Expand All @@ -631,9 +634,9 @@ impl<Ext> BundleDriver<Ext> for EthCallBundle {
/// This allows us to drive a bundle of transactions and accumulate the resulting state in the EVM.
/// Allows to simply take an [EthSendBundle] and get the resulting EVM state.
impl<Ext> BundleDriver<Ext> for EthSendBundle {
type Error<Db: revm::Database> = BundleError<Db>;
type Error<Db: Database + DatabaseCommit> = BundleError<Db>;

fn run_bundle<'a, Db: revm::Database + revm::DatabaseCommit>(
fn run_bundle<'a, Db: Database + revm::DatabaseCommit>(
&mut self,
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
) -> DriveBundleResult<'a, Ext, Db, Self> {
Expand Down Expand Up @@ -724,7 +727,7 @@ impl<Ext> BundleDriver<Ext> for EthSendBundle {
Ok(t)
}

fn post_bundle<Db: revm::Database + revm::DatabaseCommit>(
fn post_bundle<Db: Database + revm::DatabaseCommit>(
&mut self,
_trevm: &crate::EvmNeedsTx<'_, Ext, Db>,
) -> Result<(), Self::Error<Db>> {
Expand Down
8 changes: 4 additions & 4 deletions src/driver/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Block, EvmBlockDriverErrored, EvmNeedsBlock, EvmNeedsTx};
use revm::{primitives::EVMError, Database};
use revm::{primitives::EVMError, Database, DatabaseCommit};

/// The result of running transactions for a block driver.
pub type RunTxResult<'a, Ext, Db, T> =
Expand All @@ -17,19 +17,19 @@ pub trait BlockDriver<Ext> {
type Block: Block;

/// An error type for this driver.
type Error<Db: Database>: core::error::Error + From<EVMError<Db::Error>>;
type Error<Db: Database + DatabaseCommit>: core::error::Error + From<EVMError<Db::Error>>;

/// Get a reference to the block filler for this driver.
fn block(&self) -> &Self::Block;

/// Run the transactions for the block.
fn run_txns<'a, Db: Database>(
fn run_txns<'a, Db: Database + DatabaseCommit>(
&mut self,
trevm: EvmNeedsTx<'a, Ext, Db>,
) -> RunTxResult<'a, Ext, Db, Self>;

/// Run post
fn post_block<Db: Database>(
fn post_block<Db: Database + DatabaseCommit>(
&mut self,
trevm: &EvmNeedsBlock<'_, Ext, Db>,
) -> Result<(), Self::Error<Db>>;
Expand Down
2 changes: 1 addition & 1 deletion src/driver/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub type DriveBundleResult<'a, Ext, Db, T> =
/// entire lifecycle of a bundle, simulating the entire list of transactions.
pub trait BundleDriver<Ext> {
/// An error type for this driver.
type Error<Db: Database>: core::error::Error + From<EVMError<Db::Error>>;
type Error<Db: Database + DatabaseCommit>: core::error::Error + From<EVMError<Db::Error>>;

/// Run the transactions contained in the bundle.
fn run_bundle<'a, Db: Database + DatabaseCommit>(
Expand Down
2 changes: 1 addition & 1 deletion src/driver/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub trait ChainDriver<Ext> {
type BlockDriver: BlockDriver<Ext>;

/// An error type for this driver.
type Error<Db: Database>: core::error::Error
type Error<Db: Database + DatabaseCommit>: core::error::Error
+ From<EVMError<Db::Error>>
+ From<<Self::BlockDriver as BlockDriver<Ext>>::Error<Db>>;

Expand Down
99 changes: 59 additions & 40 deletions src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,24 @@ impl<'a, Ext, Db: Database, TrevmState> Trevm<'a, Ext, Db, TrevmState> {
&mut self.inner
}

/// Destructure the [`Trevm`] into its parts.
/// Destructure the [`Trevm`] into its inner EVM.
pub fn into_inner(self) -> Box<Evm<'a, Ext, Db>> {
self.inner
}

/// Deconstruct the [`Trevm`] into the backing DB, dropping the EVM handler
/// table and any `Ext` type.
///
/// This is a wrapper for [`Evm::into_db_and_env_with_handler_cfg`], and
/// then dropping the [`EnvWithHandlerCfg`]. If you need to retain the
/// [`EnvWithHandlerCfg`], use [`Self::into_inner`] and then call
/// [`Evm::into_db_and_env_with_handler_cfg`] on the inner EVM.
///
/// [`EnvWithHandlerCfg`]: revm::primitives::EnvWithHandlerCfg
pub fn into_db(self) -> Db {
self.inner.into_db_and_env_with_handler_cfg().0
}

/// Get a reference to the inner env. This contains the current
/// [`BlockEnv`], [`TxEnv`], and [`CfgEnv`].
///
Expand Down Expand Up @@ -125,45 +138,6 @@ impl<'a, Ext, Db: Database, TrevmState> Trevm<'a, Ext, Db, TrevmState> {
EvmErrored { inner: self.inner, state: ErroredState { error } }
}

/// Get the current account info for a specific address.
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_account(&mut self, address: Address) -> Result<Option<AccountInfo>, Db::Error> {
self.inner.db_mut().basic(address)
}

/// Get the current nonce for a specific address
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_nonce(&mut self, address: Address) -> Result<u64, Db::Error> {
self.try_read_account(address).map(|a| a.map(|a| a.nonce).unwrap_or_default())
}

/// Get the current nonce for a specific address
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_balance(&mut self, address: Address) -> Result<U256, Db::Error> {
self.try_read_account(address).map(|a| a.map(|a| a.balance).unwrap_or_default())
}

/// Get the value of a storage slot.
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_storage(&mut self, address: Address, slot: U256) -> Result<U256, Db::Error> {
self.inner.db_mut().storage(address, slot)
}

/// Get the code at the given account, if any.
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_code(&mut self, address: Address) -> Result<Option<Bytecode>, Db::Error> {
let acct_info = self.try_read_account(address)?;
match acct_info {
Some(acct) => Ok(Some(self.inner.db_mut().code_by_hash(acct.code_hash)?)),
None => Ok(None),
}
}

/// Apply [`StateOverride`]s to the current state. Errors if the overrides
/// contain invalid bytecode.
pub fn apply_state_overrides(
Expand Down Expand Up @@ -218,6 +192,48 @@ impl<'a, Ext, Db: Database, TrevmState> Trevm<'a, Ext, Db, TrevmState> {
Ok(self)
}
}
}

// Fallible DB Reads with &mut self
impl<Ext, Db: Database, TrevmState> Trevm<'_, Ext, Db, TrevmState> {
/// Get the current account info for a specific address.
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_account(&mut self, address: Address) -> Result<Option<AccountInfo>, Db::Error> {
self.inner.db_mut().basic(address)
}

/// Get the current nonce for a specific address
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_nonce(&mut self, address: Address) -> Result<u64, Db::Error> {
self.try_read_account(address).map(|a| a.map(|a| a.nonce).unwrap_or_default())
}

/// Get the current nonce for a specific address
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_balance(&mut self, address: Address) -> Result<U256, Db::Error> {
self.try_read_account(address).map(|a| a.map(|a| a.balance).unwrap_or_default())
}

/// Get the value of a storage slot.
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_storage(&mut self, address: Address, slot: U256) -> Result<U256, Db::Error> {
self.inner.db_mut().storage(address, slot)
}

/// Get the code at the given account, if any.
///
/// Note: due to revm's DB model, this requires a mutable pointer.
pub fn try_read_code(&mut self, address: Address) -> Result<Option<Bytecode>, Db::Error> {
let acct_info = self.try_read_account(address)?;
match acct_info {
Some(acct) => Ok(Some(self.inner.db_mut().code_by_hash(acct.code_hash)?)),
None => Ok(None),
}
}

/// Get the gas allowance for a specific caller and gas price.
pub fn try_gas_allowance(
Expand All @@ -234,6 +250,7 @@ impl<'a, Ext, Db: Database, TrevmState> Trevm<'a, Ext, Db, TrevmState> {
}
}

// Fallible DB Reads with &self
impl<Ext, Db: Database + DatabaseRef, TrevmState> Trevm<'_, Ext, Db, TrevmState> {
/// Get the current account info for a specific address.
pub fn try_read_account_ref(
Expand Down Expand Up @@ -296,6 +313,7 @@ impl<Ext, Db: Database + DatabaseRef, TrevmState> Trevm<'_, Ext, Db, TrevmState>
}
}

// Infallible DB Reads with &mut self
impl<Ext, Db: Database<Error = Infallible>, TrevmState> Trevm<'_, Ext, Db, TrevmState> {
/// Get the current account info for a specific address.
///
Expand Down Expand Up @@ -334,6 +352,7 @@ impl<Ext, Db: Database<Error = Infallible>, TrevmState> Trevm<'_, Ext, Db, Trevm
}
}

// Infalible DB Reads with &self
impl<Ext, Db: Database<Error = Infallible> + DatabaseRef<Error = Infallible>, TrevmState>
Trevm<'_, Ext, Db, TrevmState>
{
Expand Down
Loading