Skip to content

Commit 3e1576c

Browse files
committed
feat: use macros
1 parent 1036cab commit 3e1576c

File tree

1 file changed

+140
-134
lines changed

1 file changed

+140
-134
lines changed

src/driver/alloy.rs

Lines changed: 140 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{Block, BundleDriver, DriveBundleResult};
1+
use crate::{
2+
trevm_bail, trevm_ensure, unwrap_or_trevm_err, Block, BundleDriver, DriveBundleResult,
3+
};
24
use alloy_consensus::{Transaction, TxEip4844Variant, TxEnvelope};
35
use alloy_eips::{eip2718::Decodable2718, BlockNumberOrTag};
46
use alloy_primitives::{bytes::Buf, keccak256, Address, TxKind, U256};
@@ -93,21 +95,22 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
9395
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
9496
) -> DriveBundleResult<'a, Ext, Db, Self> {
9597
// Check if the block we're in is valid for this bundle. Both must match
96-
if trevm.inner().block().number.to::<u64>() != self.bundle.block_number {
97-
return Err(trevm.errored(BundleError::BlockNumberMismatch));
98-
}
98+
trevm_ensure!(
99+
trevm.inner().block().number.to::<u64>() == self.bundle.block_number,
100+
trevm,
101+
BundleError::BlockNumberMismatch
102+
);
99103

100104
// Check if the bundle has any transactions
101-
if self.bundle.txs.is_empty() {
102-
return Err(trevm.errored(BundleError::BundleEmpty));
103-
}
105+
trevm_ensure!(!self.bundle.txs.is_empty(), trevm, BundleError::BundleEmpty);
104106

105107
// Check if the state block number is valid (not 0, and not a tag)
106-
if !self.bundle.state_block_number.is_number()
107-
|| self.bundle.state_block_number.as_number().unwrap_or(0) == 0
108-
{
109-
return Err(trevm.errored(BundleError::BlockNumberMismatch));
110-
}
108+
trevm_ensure!(
109+
self.bundle.state_block_number.is_number()
110+
&& self.bundle.state_block_number.as_number().unwrap_or(0) != 0,
111+
trevm,
112+
BundleError::BlockNumberMismatch
113+
);
111114

112115
// Set the state block number this simulation was based on
113116
self.response.state_block_number = self
@@ -123,27 +126,25 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
123126
// Therefore we keep this mutable trevm instance, and set it to the new one after we're done simulating.
124127
let mut trevm = trevm;
125128

126-
let txs = self
127-
.bundle
128-
.txs
129-
.iter()
130-
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
131-
.collect::<Result<Vec<_>, _>>();
132-
let txs = match txs {
133-
Ok(txs) => txs,
134-
Err(e) => return Err(trevm.errored(BundleError::TransactionDecodingError(e))),
135-
};
129+
let txs = unwrap_or_trevm_err!(
130+
self.bundle
131+
.txs
132+
.iter()
133+
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
134+
.collect::<Result<Vec<_>, _>>(),
135+
trevm
136+
);
136137

137138
// Check that the bundle does not exceed the maximum gas limit for blob transactions
138-
if txs
139-
.iter()
140-
.filter_map(|tx| tx.as_eip4844())
141-
.map(|tx| tx.tx().tx().blob_gas())
142-
.sum::<u64>()
143-
> MAX_BLOB_GAS_PER_BLOCK
144-
{
145-
return Err(trevm.errored(BundleError::Eip4844BlobGasExceeded));
146-
}
139+
trevm_ensure!(
140+
txs.iter()
141+
.filter_map(|tx| tx.as_eip4844())
142+
.map(|tx| tx.tx().tx().blob_gas())
143+
.sum::<u64>()
144+
<= MAX_BLOB_GAS_PER_BLOCK,
145+
trevm,
146+
BundleError::Eip4844BlobGasExceeded
147+
);
147148

148149
// Cache the pre simulation coinbase balance, so we can use it to calculate the coinbase diff after every tx simulated.
149150
let initial_coinbase_balance =
@@ -179,27 +180,24 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
179180
let gas_used = execution_result.gas_used();
180181

181182
// Fetch the address and coinbase balance after the transaction, to start calculating and results
182-
let from_address = tx
183-
.recover_signer()
184-
.map_err(|e| BundleError::TransactionSenderRecoveryError(e));
185-
let from_address = match from_address {
186-
Ok(addr) => addr,
187-
Err(e) => return Err(committed_trevm.errored(e)),
188-
};
183+
let from_address = unwrap_or_trevm_err!(
184+
tx.recover_signer()
185+
.map_err(|e| BundleError::TransactionSenderRecoveryError(e)),
186+
committed_trevm
187+
);
189188

190189
// Extend the hash bytes with the transaction hash to calculate the bundle hash later
191190
hash_bytes.extend_from_slice(tx.tx_hash().as_slice());
192191

193192
// Get the post simulation coinbase balance
194-
let post_sim_coinbase_balance =
195-
match committed_trevm.try_read_balance(coinbase) {
196-
Ok(balance) => balance,
197-
Err(e) => {
198-
return Err(committed_trevm.errored(BundleError::EVMError {
199-
inner: revm::primitives::EVMError::Database(e),
200-
}))
193+
let post_sim_coinbase_balance = unwrap_or_trevm_err!(
194+
committed_trevm.try_read_balance(coinbase).map_err(|e| {
195+
BundleError::EVMError {
196+
inner: revm::primitives::EVMError::Database(e),
201197
}
202-
};
198+
}),
199+
committed_trevm
200+
);
203201

204202
// Calculate the gas fees paid
205203
let gas_fees = match tx {
@@ -224,8 +222,10 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
224222
}
225223
},
226224
_ => {
227-
return Err(committed_trevm
228-
.errored(BundleError::UnsupportedTransactionType))
225+
trevm_bail!(
226+
committed_trevm,
227+
BundleError::UnsupportedTransactionType
228+
)
229229
}
230230
};
231231

@@ -245,8 +245,10 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthCallBundle, EthCallBundleResp
245245
}
246246
},
247247
_ => {
248-
return Err(committed_trevm
249-
.errored(BundleError::UnsupportedTransactionType))
248+
trevm_bail!(
249+
committed_trevm,
250+
BundleError::UnsupportedTransactionType
251+
)
250252
}
251253
};
252254

@@ -330,39 +332,41 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthSendBundle, EthBundleHash> {
330332
) -> DriveBundleResult<'a, Ext, Db, Self> {
331333
{
332334
// Check if the block we're in is valid for this bundle. Both must match
333-
if trevm.inner().block().number.to::<u64>() != self.bundle.block_number {
334-
return Err(trevm.errored(BundleError::BlockNumberMismatch));
335-
}
335+
trevm_ensure!(
336+
trevm.inner().block().number.to::<u64>() == self.bundle.block_number,
337+
trevm,
338+
BundleError::BlockNumberMismatch
339+
);
336340

337341
// Check for start timestamp range validity
338342
if let Some(min_timestamp) = self.bundle.min_timestamp {
339-
if trevm.inner().block().timestamp.to::<u64>() < min_timestamp {
340-
return Err(trevm.errored(BundleError::TimestampOutOfRange));
341-
}
343+
trevm_ensure!(
344+
trevm.inner().block().timestamp.to::<u64>() >= min_timestamp,
345+
trevm,
346+
BundleError::TimestampOutOfRange
347+
);
342348
}
343349

344350
// Check for end timestamp range validity
345351
if let Some(max_timestamp) = self.bundle.max_timestamp {
346-
if trevm.inner().block().timestamp.to::<u64>() > max_timestamp {
347-
return Err(trevm.errored(BundleError::TimestampOutOfRange));
348-
}
352+
trevm_ensure!(
353+
trevm.inner().block().timestamp.to::<u64>() <= max_timestamp,
354+
trevm,
355+
BundleError::TimestampOutOfRange
356+
);
349357
}
350358

351359
// Check if the bundle has any transactions
352-
if self.bundle.txs.is_empty() {
353-
return Err(trevm.errored(BundleError::BundleEmpty));
354-
}
360+
trevm_ensure!(!self.bundle.txs.is_empty(), trevm, BundleError::BundleEmpty);
355361

356-
let txs = self
357-
.bundle
358-
.txs
359-
.iter()
360-
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
361-
.collect::<Result<Vec<_>, _>>();
362-
let txs = match txs {
363-
Ok(txs) => txs,
364-
Err(e) => return Err(trevm.errored(BundleError::TransactionDecodingError(e))),
365-
};
362+
let txs = unwrap_or_trevm_err!(
363+
self.bundle
364+
.txs
365+
.iter()
366+
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
367+
.collect::<Result<Vec<_>, _>>(),
368+
trevm
369+
);
366370

367371
// Check that the bundle does not exceed the maximum gas limit for blob transactions
368372
if txs
@@ -399,7 +403,7 @@ impl<Ext> BundleDriver<Ext> for BundleProcessor<EthSendBundle, EthBundleHash> {
399403
if self.bundle.reverting_tx_hashes.contains(tx.tx_hash()) {
400404
run_result.accept_state()
401405
} else {
402-
return Err(run_result.errored(BundleError::BundleReverted));
406+
trevm_bail!(run_result, BundleError::BundleReverted);
403407
}
404408
}
405409
};
@@ -486,48 +490,47 @@ impl<Ext> BundleDriver<Ext> for EthCallBundle {
486490
&mut self,
487491
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
488492
) -> DriveBundleResult<'a, Ext, Db, Self> {
489-
// 1. Check if the block we're in is valid for this bundle. Both must match
490-
if trevm.inner().block().number.to::<u64>() != self.block_number {
491-
return Err(trevm.errored(BundleError::BlockNumberMismatch));
492-
}
493+
// Check if the block we're in is valid for this bundle. Both must match
494+
trevm_ensure!(
495+
trevm.inner().block().number.to::<u64>() == self.block_number,
496+
trevm,
497+
BundleError::BlockNumberMismatch
498+
);
493499

494500
// Check if the bundle has any transactions
495-
if self.txs.is_empty() {
496-
return Err(trevm.errored(BundleError::BundleEmpty));
497-
}
501+
trevm_ensure!(!self.txs.is_empty(), trevm, BundleError::BundleEmpty);
498502

499503
// Check if the state block number is valid (not 0, and not a tag)
500-
if !self.state_block_number.is_number()
501-
|| self.state_block_number.as_number().unwrap_or(0) == 0
502-
{
503-
return Err(trevm.errored(BundleError::BlockNumberMismatch));
504-
}
504+
trevm_ensure!(
505+
self.state_block_number.is_number()
506+
&& self.state_block_number.as_number().unwrap_or(0) != 0,
507+
trevm,
508+
BundleError::BlockNumberMismatch
509+
);
505510

506511
let bundle_filler = BundleBlockFiller::from(self.clone());
507512

508513
let run_result = trevm.try_with_block(&bundle_filler, |trevm| {
509514
let mut trevm = trevm;
510515

511-
let txs = self
512-
.txs
513-
.iter()
514-
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
515-
.collect::<Result<Vec<_>, _>>();
516-
let txs = match txs {
517-
Ok(txs) => txs,
518-
Err(e) => return Err(trevm.errored(BundleError::TransactionDecodingError(e))),
519-
};
516+
let txs = unwrap_or_trevm_err!(
517+
self.txs
518+
.iter()
519+
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
520+
.collect::<Result<Vec<_>, _>>(),
521+
trevm
522+
);
520523

521524
// Check that the bundle does not exceed the maximum gas limit for blob transactions
522-
if txs
523-
.iter()
524-
.filter_map(|tx| tx.as_eip4844())
525-
.map(|tx| tx.tx().tx().blob_gas())
526-
.sum::<u64>()
527-
> MAX_BLOB_GAS_PER_BLOCK
528-
{
529-
return Err(trevm.errored(BundleError::Eip4844BlobGasExceeded));
530-
}
525+
trevm_ensure!(
526+
txs.iter()
527+
.filter_map(|tx| tx.as_eip4844())
528+
.map(|tx| tx.tx().tx().blob_gas())
529+
.sum::<u64>()
530+
<= MAX_BLOB_GAS_PER_BLOCK,
531+
trevm,
532+
BundleError::Eip4844BlobGasExceeded
533+
);
531534

532535
for tx in txs.iter() {
533536
let run_result = trevm.run_tx(tx);
@@ -573,49 +576,52 @@ impl<Ext> BundleDriver<Ext> for EthSendBundle {
573576
trevm: crate::EvmNeedsTx<'a, Ext, Db>,
574577
) -> DriveBundleResult<'a, Ext, Db, Self> {
575578
// Check if the block we're in is valid for this bundle. Both must match
576-
if trevm.inner().block().number.to::<u64>() != self.block_number {
577-
return Err(trevm.errored(BundleError::BlockNumberMismatch));
578-
}
579+
trevm_ensure!(
580+
trevm.inner().block().number.to::<u64>() == self.block_number,
581+
trevm,
582+
BundleError::BlockNumberMismatch
583+
);
579584

580585
// Check for start timestamp range validity
586+
581587
if let Some(min_timestamp) = self.min_timestamp {
582-
if trevm.inner().block().timestamp.to::<u64>() < min_timestamp {
583-
return Err(trevm.errored(BundleError::TimestampOutOfRange));
584-
}
588+
trevm_ensure!(
589+
trevm.inner().block().timestamp.to::<u64>() >= min_timestamp,
590+
trevm,
591+
BundleError::TimestampOutOfRange
592+
);
585593
}
586594

587595
// Check for end timestamp range validity
588596
if let Some(max_timestamp) = self.max_timestamp {
589-
if trevm.inner().block().timestamp.to::<u64>() > max_timestamp {
590-
return Err(trevm.errored(BundleError::TimestampOutOfRange));
591-
}
597+
trevm_ensure!(
598+
trevm.inner().block().timestamp.to::<u64>() <= max_timestamp,
599+
trevm,
600+
BundleError::TimestampOutOfRange
601+
);
592602
}
593603

594604
// Check if the bundle has any transactions
595-
if self.txs.is_empty() {
596-
return Err(trevm.errored(BundleError::BundleEmpty));
597-
}
605+
trevm_ensure!(!self.txs.is_empty(), trevm, BundleError::BundleEmpty);
598606

599-
let txs = self
600-
.txs
601-
.iter()
602-
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
603-
.collect::<Result<Vec<_>, _>>();
604-
let txs = match txs {
605-
Ok(txs) => txs,
606-
Err(e) => return Err(trevm.errored(BundleError::TransactionDecodingError(e))),
607-
};
607+
let txs = unwrap_or_trevm_err!(
608+
self.txs
609+
.iter()
610+
.map(|tx| TxEnvelope::decode_2718(&mut tx.chunk()))
611+
.collect::<Result<Vec<_>, _>>(),
612+
trevm
613+
);
608614

609615
// Check that the bundle does not exceed the maximum gas limit for blob transactions
610-
if txs
611-
.iter()
612-
.filter_map(|tx| tx.as_eip4844())
613-
.map(|tx| tx.tx().tx().blob_gas())
614-
.sum::<u64>()
615-
> MAX_BLOB_GAS_PER_BLOCK
616-
{
617-
return Err(trevm.errored(BundleError::Eip4844BlobGasExceeded));
618-
}
616+
trevm_ensure!(
617+
txs.iter()
618+
.filter_map(|tx| tx.as_eip4844())
619+
.map(|tx| tx.tx().tx().blob_gas())
620+
.sum::<u64>()
621+
<= MAX_BLOB_GAS_PER_BLOCK,
622+
trevm,
623+
BundleError::Eip4844BlobGasExceeded
624+
);
619625

620626
// Store the current evm state in this mutable variable, so we can continually use the freshest state for each simulation
621627
let mut t = trevm;
@@ -638,7 +644,7 @@ impl<Ext> BundleDriver<Ext> for EthSendBundle {
638644
if self.reverting_tx_hashes.contains(tx.tx_hash()) {
639645
run_result.accept_state()
640646
} else {
641-
return Err(run_result.errored(BundleError::BundleReverted));
647+
trevm_bail!(run_result, BundleError::BundleReverted)
642648
}
643649
}
644650
};

0 commit comments

Comments
 (0)