Skip to content

Commit ad3965b

Browse files
authored
fix: restore commit bounds to drivers (#86)
1 parent 21f33a7 commit ad3965b

File tree

5 files changed

+90
-68
lines changed

5 files changed

+90
-68
lines changed

src/driver/alloy.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ use alloy::{
1111
EthSendBundle,
1212
},
1313
};
14-
use revm::primitives::{EVMError, ExecutionResult, SpecId};
14+
use revm::{
15+
primitives::{EVMError, ExecutionResult, SpecId},
16+
Database, DatabaseCommit,
17+
};
1518

1619
/// Possible errors that can occur while driving a bundle.
17-
pub enum BundleError<Db: revm::Database> {
20+
pub enum BundleError<Db: Database> {
1821
/// The block number of the bundle does not match the block number of the revm block configuration.
1922
BlockNumberMismatch,
2023
/// The timestamp of the bundle is out of range.
@@ -38,7 +41,7 @@ pub enum BundleError<Db: revm::Database> {
3841
},
3942
}
4043

41-
impl<Db: revm::Database> core::fmt::Display for BundleError<Db> {
44+
impl<Db: Database> core::fmt::Display for BundleError<Db> {
4245
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
4346
match self {
4447
Self::BlockNumberMismatch => {
@@ -58,25 +61,25 @@ impl<Db: revm::Database> core::fmt::Display for BundleError<Db> {
5861
}
5962
}
6063

61-
impl<Db: revm::Database> From<alloy::eips::eip2718::Eip2718Error> for BundleError<Db> {
64+
impl<Db: Database> From<alloy::eips::eip2718::Eip2718Error> for BundleError<Db> {
6265
fn from(err: alloy::eips::eip2718::Eip2718Error) -> Self {
6366
Self::TransactionDecodingError(err)
6467
}
6568
}
6669

67-
impl<Db: revm::Database> From<alloy::primitives::SignatureError> for BundleError<Db> {
70+
impl<Db: Database> From<alloy::primitives::SignatureError> for BundleError<Db> {
6871
fn from(err: alloy::primitives::SignatureError) -> Self {
6972
Self::TransactionSenderRecoveryError(err)
7073
}
7174
}
7275

73-
impl<Db: revm::Database> From<EVMError<Db::Error>> for BundleError<Db> {
76+
impl<Db: Database> From<EVMError<Db::Error>> for BundleError<Db> {
7477
fn from(inner: EVMError<Db::Error>) -> Self {
7578
Self::EVMError { inner }
7679
}
7780
}
7881

79-
impl<Db: revm::Database> std::error::Error for BundleError<Db> {
82+
impl<Db: Database> std::error::Error for BundleError<Db> {
8083
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
8184
match self {
8285
Self::TransactionDecodingError(err) => Some(err),
@@ -86,7 +89,7 @@ impl<Db: revm::Database> std::error::Error for BundleError<Db> {
8689
}
8790
}
8891

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

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

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

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

262-
fn run_bundle<'a, Db: revm::Database + revm::DatabaseCommit>(
265+
fn run_bundle<'a, Db: Database + revm::DatabaseCommit>(
263266
&mut self,
264267
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
265268
) -> DriveBundleResult<'a, Ext, Db, Self> {
@@ -389,7 +392,7 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
389392
}
390393
}
391394

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

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

403-
fn run_bundle<'a, Db: revm::Database + revm::DatabaseCommit>(
406+
fn run_bundle<'a, Db: Database + revm::DatabaseCommit>(
404407
&mut self,
405408
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
406409
) -> DriveBundleResult<'a, Ext, Db, Self> {
@@ -476,7 +479,7 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthSendBundle, EthBundleHash> {
476479
}
477480
}
478481

479-
fn post_bundle<Db: revm::Database + revm::DatabaseCommit>(
482+
fn post_bundle<Db: Database + revm::DatabaseCommit>(
480483
&mut self,
481484
_trevm: &crate::EvmNeedsTx<'_, Ext, Db>,
482485
) -> Result<(), Self::Error<Db>> {
@@ -541,9 +544,9 @@ impl From<EthCallBundle> for BundleBlockFiller {
541544
}
542545

543546
impl<Ext> BundleDriver<Ext> for EthCallBundle {
544-
type Error<Db: revm::Database> = BundleError<Db>;
547+
type Error<Db: Database + DatabaseCommit> = BundleError<Db>;
545548

546-
fn run_bundle<'a, Db: revm::Database + revm::DatabaseCommit>(
549+
fn run_bundle<'a, Db: Database + revm::DatabaseCommit>(
547550
&mut self,
548551
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
549552
) -> DriveBundleResult<'a, Ext, Db, Self> {
@@ -619,7 +622,7 @@ impl<Ext> BundleDriver<Ext> for EthCallBundle {
619622
}
620623
}
621624

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

636-
fn run_bundle<'a, Db: revm::Database + revm::DatabaseCommit>(
639+
fn run_bundle<'a, Db: Database + revm::DatabaseCommit>(
637640
&mut self,
638641
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
639642
) -> DriveBundleResult<'a, Ext, Db, Self> {
@@ -724,7 +727,7 @@ impl<Ext> BundleDriver<Ext> for EthSendBundle {
724727
Ok(t)
725728
}
726729

727-
fn post_bundle<Db: revm::Database + revm::DatabaseCommit>(
730+
fn post_bundle<Db: Database + revm::DatabaseCommit>(
728731
&mut self,
729732
_trevm: &crate::EvmNeedsTx<'_, Ext, Db>,
730733
) -> Result<(), Self::Error<Db>> {

src/driver/block.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Block, EvmBlockDriverErrored, EvmNeedsBlock, EvmNeedsTx};
2-
use revm::{primitives::EVMError, Database};
2+
use revm::{primitives::EVMError, Database, DatabaseCommit};
33

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

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

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

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

3131
/// Run post
32-
fn post_block<Db: Database>(
32+
fn post_block<Db: Database + DatabaseCommit>(
3333
&mut self,
3434
trevm: &EvmNeedsBlock<'_, Ext, Db>,
3535
) -> Result<(), Self::Error<Db>>;

src/driver/bundle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub type DriveBundleResult<'a, Ext, Db, T> =
99
/// entire lifecycle of a bundle, simulating the entire list of transactions.
1010
pub trait BundleDriver<Ext> {
1111
/// An error type for this driver.
12-
type Error<Db: Database>: core::error::Error + From<EVMError<Db::Error>>;
12+
type Error<Db: Database + DatabaseCommit>: core::error::Error + From<EVMError<Db::Error>>;
1313

1414
/// Run the transactions contained in the bundle.
1515
fn run_bundle<'a, Db: Database + DatabaseCommit>(

src/driver/chain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub trait ChainDriver<Ext> {
1414
type BlockDriver: BlockDriver<Ext>;
1515

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

src/evm.rs

+59-40
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,24 @@ impl<'a, Ext, Db: Database, TrevmState> Trevm<'a, Ext, Db, TrevmState> {
6161
&mut self.inner
6262
}
6363

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

69+
/// Deconstruct the [`Trevm`] into the backing DB, dropping the EVM handler
70+
/// table and any `Ext` type.
71+
///
72+
/// This is a wrapper for [`Evm::into_db_and_env_with_handler_cfg`], and
73+
/// then dropping the [`EnvWithHandlerCfg`]. If you need to retain the
74+
/// [`EnvWithHandlerCfg`], use [`Self::into_inner`] and then call
75+
/// [`Evm::into_db_and_env_with_handler_cfg`] on the inner EVM.
76+
///
77+
/// [`EnvWithHandlerCfg`]: revm::primitives::EnvWithHandlerCfg
78+
pub fn into_db(self) -> Db {
79+
self.inner.into_db_and_env_with_handler_cfg().0
80+
}
81+
6982
/// Get a reference to the inner env. This contains the current
7083
/// [`BlockEnv`], [`TxEnv`], and [`CfgEnv`].
7184
///
@@ -125,45 +138,6 @@ impl<'a, Ext, Db: Database, TrevmState> Trevm<'a, Ext, Db, TrevmState> {
125138
EvmErrored { inner: self.inner, state: ErroredState { error } }
126139
}
127140

128-
/// Get the current account info for a specific address.
129-
///
130-
/// Note: due to revm's DB model, this requires a mutable pointer.
131-
pub fn try_read_account(&mut self, address: Address) -> Result<Option<AccountInfo>, Db::Error> {
132-
self.inner.db_mut().basic(address)
133-
}
134-
135-
/// Get the current nonce for a specific address
136-
///
137-
/// Note: due to revm's DB model, this requires a mutable pointer.
138-
pub fn try_read_nonce(&mut self, address: Address) -> Result<u64, Db::Error> {
139-
self.try_read_account(address).map(|a| a.map(|a| a.nonce).unwrap_or_default())
140-
}
141-
142-
/// Get the current nonce for a specific address
143-
///
144-
/// Note: due to revm's DB model, this requires a mutable pointer.
145-
pub fn try_read_balance(&mut self, address: Address) -> Result<U256, Db::Error> {
146-
self.try_read_account(address).map(|a| a.map(|a| a.balance).unwrap_or_default())
147-
}
148-
149-
/// Get the value of a storage slot.
150-
///
151-
/// Note: due to revm's DB model, this requires a mutable pointer.
152-
pub fn try_read_storage(&mut self, address: Address, slot: U256) -> Result<U256, Db::Error> {
153-
self.inner.db_mut().storage(address, slot)
154-
}
155-
156-
/// Get the code at the given account, if any.
157-
///
158-
/// Note: due to revm's DB model, this requires a mutable pointer.
159-
pub fn try_read_code(&mut self, address: Address) -> Result<Option<Bytecode>, Db::Error> {
160-
let acct_info = self.try_read_account(address)?;
161-
match acct_info {
162-
Some(acct) => Ok(Some(self.inner.db_mut().code_by_hash(acct.code_hash)?)),
163-
None => Ok(None),
164-
}
165-
}
166-
167141
/// Apply [`StateOverride`]s to the current state. Errors if the overrides
168142
/// contain invalid bytecode.
169143
pub fn apply_state_overrides(
@@ -218,6 +192,48 @@ impl<'a, Ext, Db: Database, TrevmState> Trevm<'a, Ext, Db, TrevmState> {
218192
Ok(self)
219193
}
220194
}
195+
}
196+
197+
// Fallible DB Reads with &mut self
198+
impl<Ext, Db: Database, TrevmState> Trevm<'_, Ext, Db, TrevmState> {
199+
/// Get the current account info for a specific address.
200+
///
201+
/// Note: due to revm's DB model, this requires a mutable pointer.
202+
pub fn try_read_account(&mut self, address: Address) -> Result<Option<AccountInfo>, Db::Error> {
203+
self.inner.db_mut().basic(address)
204+
}
205+
206+
/// Get the current nonce for a specific address
207+
///
208+
/// Note: due to revm's DB model, this requires a mutable pointer.
209+
pub fn try_read_nonce(&mut self, address: Address) -> Result<u64, Db::Error> {
210+
self.try_read_account(address).map(|a| a.map(|a| a.nonce).unwrap_or_default())
211+
}
212+
213+
/// Get the current nonce for a specific address
214+
///
215+
/// Note: due to revm's DB model, this requires a mutable pointer.
216+
pub fn try_read_balance(&mut self, address: Address) -> Result<U256, Db::Error> {
217+
self.try_read_account(address).map(|a| a.map(|a| a.balance).unwrap_or_default())
218+
}
219+
220+
/// Get the value of a storage slot.
221+
///
222+
/// Note: due to revm's DB model, this requires a mutable pointer.
223+
pub fn try_read_storage(&mut self, address: Address, slot: U256) -> Result<U256, Db::Error> {
224+
self.inner.db_mut().storage(address, slot)
225+
}
226+
227+
/// Get the code at the given account, if any.
228+
///
229+
/// Note: due to revm's DB model, this requires a mutable pointer.
230+
pub fn try_read_code(&mut self, address: Address) -> Result<Option<Bytecode>, Db::Error> {
231+
let acct_info = self.try_read_account(address)?;
232+
match acct_info {
233+
Some(acct) => Ok(Some(self.inner.db_mut().code_by_hash(acct.code_hash)?)),
234+
None => Ok(None),
235+
}
236+
}
221237

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

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

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

355+
// Infalible DB Reads with &self
337356
impl<Ext, Db: Database<Error = Infallible> + DatabaseRef<Error = Infallible>, TrevmState>
338357
Trevm<'_, Ext, Db, TrevmState>
339358
{

0 commit comments

Comments
 (0)