Skip to content

Commit 624ead4

Browse files
authored
refactor: use &TxHash in Bundle trait methods (#39)
Changed `is_allowed_to_fail` and `is_optional` to accept `&TxHash` instead of `TxHash`. These methods are primarily used for lookups in collections, and accepting a reference allows implementations to use methods like `Vec::contains()` and `HashSet::contains()` directly without requiring callers to copy the hash.
1 parent 005369b commit 624ead4

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

examples/custom-bundle-type.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ impl Bundle<CustomPlatform> for CustomBundleType {
179179
Eligibility::Eligible
180180
}
181181

182-
fn is_allowed_to_fail(&self, tx: alloy::primitives::TxHash) -> bool {
183-
self.reverting_txs.contains(&tx)
182+
fn is_allowed_to_fail(&self, tx: &alloy::primitives::TxHash) -> bool {
183+
self.reverting_txs.contains(tx)
184184
}
185185

186-
fn is_optional(&self, tx: alloy::primitives::TxHash) -> bool {
187-
self.dropping_txs.contains(&tx)
186+
fn is_optional(&self, tx: &alloy::primitives::TxHash) -> bool {
187+
self.dropping_txs.contains(tx)
188188
}
189189

190190
fn validate_post_execution(

src/payload/exec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<P: Platform> Executable<P> {
205205
let mut results = Vec::with_capacity(bundle.transactions().len());
206206

207207
for transaction in bundle.transactions() {
208-
let tx_hash = *transaction.tx_hash();
208+
let tx_hash = transaction.tx_hash();
209209
let optional = bundle.is_optional(tx_hash);
210210
let allowed_to_fail = bundle.is_allowed_to_fail(tx_hash);
211211

@@ -224,16 +224,16 @@ impl<P: Platform> Executable<P> {
224224
// Optional failing transaction, not allowed to fail
225225
// or optional invalid transaction: discard it
226226
Ok(_) | Err(_) if optional => {
227-
discarded.push(tx_hash);
227+
discarded.push(*tx_hash);
228228
}
229229
// Non-Optional failing transaction, not allowed to fail: invalidate the
230230
// bundle
231231
Ok(_) => {
232-
return Err(ExecutionError::BundleTransactionReverted(tx_hash));
232+
return Err(ExecutionError::BundleTransactionReverted(*tx_hash));
233233
}
234234
// Non-Optional invalid transaction: invalidate the bundle
235235
Err(err) => {
236-
return Err(ExecutionError::InvalidBundleTransaction(tx_hash, err));
236+
return Err(ExecutionError::InvalidBundleTransaction(*tx_hash, err));
237237
}
238238
}
239239
}

src/platform/bundle.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ pub trait Bundle<P: Platform>:
7777
///
7878
/// The `tx` is the hash of the transaction to check, that should be part of
7979
/// this bundle.
80-
fn is_allowed_to_fail(&self, tx: TxHash) -> bool;
80+
fn is_allowed_to_fail(&self, tx: &TxHash) -> bool;
8181

8282
/// Checks if a transaction with the given hash may be removed from the
8383
/// bundle without affecting its validity.
84-
fn is_optional(&self, tx: TxHash) -> bool;
84+
fn is_optional(&self, tx: &TxHash) -> bool;
8585

8686
/// An optional check for bundle implementations that have validity
8787
/// requirements on the resulting state.
@@ -300,12 +300,12 @@ impl<P: Platform> Bundle<P> for FlashbotsBundle<P> {
300300
)
301301
}
302302

303-
fn is_allowed_to_fail(&self, tx: TxHash) -> bool {
304-
self.reverting_tx_hashes.contains(&tx)
303+
fn is_allowed_to_fail(&self, tx: &TxHash) -> bool {
304+
self.reverting_tx_hashes.contains(tx)
305305
}
306306

307-
fn is_optional(&self, tx: TxHash) -> bool {
308-
self.dropping_tx_hashes.contains(&tx)
307+
fn is_optional(&self, tx: &TxHash) -> bool {
308+
self.dropping_tx_hashes.contains(tx)
309309
}
310310

311311
fn hash(&self) -> B256 {

src/platform/ext/bundle.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<P: Platform, T: Bundle<P>> BundleExt<P> for T {
3838
self
3939
.transactions()
4040
.iter()
41-
.filter(|tx| !self.is_allowed_to_fail(*tx.tx_hash()))
41+
.filter(|tx| !self.is_allowed_to_fail(tx.tx_hash()))
4242
}
4343

4444
/// Returns an iterator over all transactions that can be removed from the
@@ -49,7 +49,7 @@ impl<P: Platform, T: Bundle<P>> BundleExt<P> for T {
4949
self
5050
.transactions()
5151
.iter()
52-
.filter(|tx| self.is_optional(*tx.tx_hash()))
52+
.filter(|tx| self.is_optional(tx.tx_hash()))
5353
}
5454

5555
/// Returns an iterator over all transactions that are required to be present
@@ -61,7 +61,7 @@ impl<P: Platform, T: Bundle<P>> BundleExt<P> for T {
6161
self
6262
.transactions()
6363
.iter()
64-
.filter(|tx| !self.is_optional(*tx.tx_hash()))
64+
.filter(|tx| !self.is_optional(tx.tx_hash()))
6565
}
6666

6767
/// Returns an iterator over all transactions that must be included and may
@@ -70,8 +70,7 @@ impl<P: Platform, T: Bundle<P>> BundleExt<P> for T {
7070
&self,
7171
) -> impl Iterator<Item = &Recovered<types::Transaction<P>>> {
7272
self.transactions().iter().filter(|tx| {
73-
!self.is_allowed_to_fail(*tx.tx_hash())
74-
&& !self.is_optional(*tx.tx_hash())
73+
!self.is_allowed_to_fail(tx.tx_hash()) && !self.is_optional(tx.tx_hash())
7574
})
7675
}
7776
}

src/steps/revert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<P: Platform> Step<P> for RemoveRevertedTransactions {
132132
.failed_txs()
133133
.filter_map(|(tx, result)| {
134134
bundle
135-
.is_optional(*tx.tx_hash())
135+
.is_optional(tx.tx_hash())
136136
.then_some((*tx.tx_hash(), result.gas_used()))
137137
})
138138
.collect::<Vec<_>>();
@@ -241,7 +241,7 @@ fn has_failures<P: Platform>(checkpoint: &Checkpoint<P>) -> bool {
241241
.iter()
242242
.map(|r| !r.is_success())
243243
.zip(result.transactions().iter().map(|tx| tx.tx_hash()))
244-
.any(|(is_failure, tx_hash)| is_failure && bundle.is_optional(*tx_hash));
244+
.any(|(is_failure, tx_hash)| is_failure && bundle.is_optional(tx_hash));
245245
}
246246

247247
false

0 commit comments

Comments
 (0)