Skip to content

Commit b58a5b4

Browse files
authored
refactor: trevm_try (#92)
1 parent 2b4e88e commit b58a5b4

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

src/driver/alloy.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
system::{MAX_BLOB_GAS_PER_BLOCK_CANCUN, MAX_BLOB_GAS_PER_BLOCK_PRAGUE},
3-
trevm_bail, trevm_ensure, unwrap_or_trevm_err, Block, BundleDriver, DriveBundleResult,
3+
trevm_bail, trevm_ensure, trevm_try, Block, BundleDriver, DriveBundleResult,
44
};
55
use alloy::{
66
consensus::{Transaction, TxEip4844Variant, TxEnvelope},
@@ -299,10 +299,10 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
299299
let mut trevm = trevm;
300300

301301
// Decode and validate the transactions in the bundle
302-
let txs = unwrap_or_trevm_err!(Self::decode_and_validate_txs(&self.bundle.txs), trevm);
302+
let txs = trevm_try!(Self::decode_and_validate_txs(&self.bundle.txs), trevm);
303303

304304
// Cache the pre simulation coinbase balance, so we can use it to calculate the coinbase diff after every tx simulated.
305-
let initial_coinbase_balance = unwrap_or_trevm_err!(
305+
let initial_coinbase_balance = trevm_try!(
306306
trevm.try_read_balance(trevm.inner().block().coinbase).map_err(|e| {
307307
BundleError::EVMError { inner: revm::primitives::EVMError::Database(e) }
308308
}),
@@ -332,7 +332,7 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
332332
let basefee = committed_trevm.inner().block().basefee;
333333

334334
// Get the post simulation coinbase balance
335-
let post_sim_coinbase_balance = unwrap_or_trevm_err!(
335+
let post_sim_coinbase_balance = trevm_try!(
336336
committed_trevm.try_read_balance(coinbase).map_err(|e| {
337337
BundleError::EVMError {
338338
inner: revm::primitives::EVMError::Database(e),
@@ -342,7 +342,7 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
342342
);
343343

344344
// Process the transaction and accumulate the results
345-
let (response, post_sim_coinbase_balance) = unwrap_or_trevm_err!(
345+
let (response, post_sim_coinbase_balance) = trevm_try!(
346346
Self::process_call_bundle_tx(
347347
tx,
348348
pre_sim_coinbase_balance,
@@ -437,7 +437,7 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthSendBundle, EthBundleHash> {
437437
trevm_ensure!(!self.bundle.txs.is_empty(), trevm, BundleError::BundleEmpty);
438438

439439
// Decode and validate the transactions in the bundle
440-
let txs = unwrap_or_trevm_err!(Self::decode_and_validate_txs(&self.bundle.txs), trevm);
440+
let txs = trevm_try!(Self::decode_and_validate_txs(&self.bundle.txs), trevm);
441441

442442
// Store the current evm state in this mutable variable, so we can continually use the freshest state for each simulation
443443
let mut t = trevm;
@@ -573,7 +573,7 @@ impl<Ext> BundleDriver<Ext> for EthCallBundle {
573573
let run_result = trevm.try_with_block(&bundle_filler, |trevm| {
574574
let mut trevm = trevm;
575575

576-
let txs = unwrap_or_trevm_err!(
576+
let txs = trevm_try!(
577577
self.txs
578578
.iter()
579579
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
@@ -669,7 +669,7 @@ impl<Ext> BundleDriver<Ext> for EthSendBundle {
669669
// Check if the bundle has any transactions
670670
trevm_ensure!(!self.txs.is_empty(), trevm, BundleError::BundleEmpty);
671671

672-
let txs = unwrap_or_trevm_err!(
672+
let txs = trevm_try!(
673673
self.txs
674674
.iter()
675675
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))

src/evm.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1138,9 +1138,9 @@ impl<'a, Ext, Db: Database + TryStateAcc> EvmNeedsBlock<'a, Ext, Db> {
11381138
) -> Result<BundleState, EvmErrored<'a, Ext, Db, <Db as TryStateAcc>::Error>> {
11391139
let db = self.inner.db_mut();
11401140

1141-
unwrap_or_trevm_err!(db.try_merge_transitions(BundleRetention::Reverts), self);
1141+
trevm_try!(db.try_merge_transitions(BundleRetention::Reverts), self);
11421142

1143-
let bundle = unwrap_or_trevm_err!(db.try_take_bundle(), self);
1143+
let bundle = trevm_try!(db.try_take_bundle(), self);
11441144

11451145
Ok(bundle)
11461146
}
@@ -1490,7 +1490,7 @@ impl<'a, Ext, Db: Database + TryStateAcc> EvmNeedsTx<'a, Ext, Db> {
14901490
overrides.fill_block(&mut self.inner);
14911491

14921492
if let Some(hashes) = overrides.block_hash.as_ref() {
1493-
unwrap_or_trevm_err!(self.inner.db_mut().try_set_block_hashes(hashes), self);
1493+
trevm_try!(self.inner.db_mut().try_set_block_hashes(hashes), self);
14941494
}
14951495

14961496
Ok(self)
@@ -1707,7 +1707,7 @@ impl<'a, Ext, Db: Database> EvmReady<'a, Ext, Db> {
17071707
) -> Result<(crate::EstimationResult, Self), EvmErrored<'a, Ext, Db>> {
17081708
use tracing::{debug, enabled};
17091709

1710-
if let Some(est) = crate::unwrap_or_trevm_err!(self.estimate_gas_simple_transfer(), self) {
1710+
if let Some(est) = crate::trevm_try!(self.estimate_gas_simple_transfer(), self) {
17111711
return Ok((crate::EstimationResult::basic_transfer_success(est), self));
17121712
}
17131713

@@ -1731,7 +1731,7 @@ impl<'a, Ext, Db: Database> EvmReady<'a, Ext, Db> {
17311731
let _e = span.enter();
17321732

17331733
// Cap the gas limit to the caller's allowance and block limit
1734-
unwrap_or_trevm_err!(self.cap_tx_gas(), self);
1734+
trevm_try!(self.cap_tx_gas(), self);
17351735
search_range.maybe_lower_max(self.gas_limit());
17361736

17371737
// Raise the floor to the amount of gas required to initialize the EVM.
@@ -2005,10 +2005,7 @@ impl<'a, Ext, Db: Database> EvmTransacted<'a, Ext, Db> {
20052005
{
20062006
let Trevm { mut inner, state: TransactedState { result } } = self;
20072007

2008-
unwrap_or_trevm_err!(
2009-
inner.db_mut().try_commit(result.state),
2010-
Trevm { inner, state: NeedsTx::new() }
2011-
);
2008+
trevm_try!(inner.db_mut().try_commit(result.state), Trevm { inner, state: NeedsTx::new() });
20122009
Ok((result.result, Trevm { inner, state: NeedsTx::new() }))
20132010
}
20142011

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@
336336
//!
337337
//! [`EVMError<Db>`]: revm::primitives::EVMError<Db>
338338
//! [typestate pattern]: https://cliffle.com/blog/rust-typestate/
339-
//! [crate readme]: https://github.com/init4tt/trevm
339+
//! [crate readme]: https://github.com/init4tech/trevm
340340
//! [EIP-2537]: https://eips.ethereum.org/EIPS/eip-2537
341341
//! [EIP-2935]: https://eips.ethereum.org/EIPS/eip-2935
342342
//! [EIP-4788]: https://eips.ethereum.org/EIPS/eip-4788

src/macros.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// Unwraps a Result, returning the value if successful, or returning an errored `Trevm` if not.
22
#[macro_export]
3+
#[deprecated = "Please use `trevm_tri!` instead"]
34
macro_rules! unwrap_or_trevm_err {
45
($e:expr, $trevm:expr) => {
56
match $e {
@@ -9,6 +10,17 @@ macro_rules! unwrap_or_trevm_err {
910
};
1011
}
1112

13+
/// Unwraps a Result, returning the value if successful, or returning an errored `Trevm` if not.
14+
#[macro_export]
15+
macro_rules! trevm_try {
16+
($e:expr, $trevm:expr) => {
17+
match $e {
18+
Ok(val) => val,
19+
Err(e) => return Err($trevm.errored(e.into())),
20+
}
21+
};
22+
}
23+
1224
/// Executes a condition, returning an errored `Trevm` if not successful.
1325
#[macro_export]
1426
macro_rules! trevm_ensure {

0 commit comments

Comments
 (0)