Skip to main content

reth_rpc/eth/
sim_bundle.rs

1//! `Eth` Sim bundle implementation and helpers.
2
3use alloy_consensus::{transaction::TxHashRef, BlockHeader};
4use alloy_eips::BlockNumberOrTag;
5use alloy_evm::{env::BlockEnvironment, overrides::apply_block_overrides};
6use alloy_primitives::U256;
7use alloy_rpc_types_eth::{BlockId, Log};
8use alloy_rpc_types_mev::{
9    BundleItem, Inclusion, MevSendBundle, Privacy, RefundConfig, SimBundleLogs, SimBundleOverrides,
10    SimBundleResponse, Validity,
11};
12use jsonrpsee::core::RpcResult;
13use reth_evm::{ConfigureEvm, Evm};
14use reth_primitives_traits::Recovered;
15use reth_rpc_api::MevSimApiServer;
16use reth_rpc_eth_api::{
17    helpers::{block::LoadBlock, Call, EthTransactions},
18    FromEthApiError, FromEvmError,
19};
20use reth_rpc_eth_types::{utils::recover_raw_transaction, EthApiError};
21use reth_storage_api::ProviderTx;
22use reth_tasks::pool::BlockingTaskGuard;
23use reth_transaction_pool::{PoolPooledTx, PoolTransaction, TransactionPool};
24use revm::{
25    context::Block, context_interface::result::ResultAndState, DatabaseCommit, DatabaseRef,
26};
27use std::{sync::Arc, time::Duration};
28use tracing::trace;
29
30/// Maximum bundle depth
31const MAX_NESTED_BUNDLE_DEPTH: usize = 5;
32
33/// Maximum body size
34const MAX_BUNDLE_BODY_SIZE: usize = 50;
35
36/// Default simulation timeout
37const DEFAULT_SIM_TIMEOUT: Duration = Duration::from_secs(5);
38
39/// Maximum simulation timeout
40const MAX_SIM_TIMEOUT: Duration = Duration::from_secs(30);
41
42/// Maximum payout cost
43const SBUNDLE_PAYOUT_MAX_COST: u64 = 30_000;
44
45/// A flattened representation of a bundle item containing transaction and associated metadata.
46#[derive(Clone, Debug)]
47pub struct FlattenedBundleItem<T> {
48    /// The signed transaction
49    pub tx: Recovered<T>,
50    /// Whether the transaction is allowed to revert
51    pub can_revert: bool,
52    /// Item-level inclusion constraints
53    pub inclusion: Inclusion,
54    /// Optional validity constraints for the bundle item
55    pub validity: Option<Validity>,
56    /// Optional privacy settings for the bundle item
57    pub privacy: Option<Privacy>,
58    /// Optional refund percent for the bundle item
59    pub refund_percent: Option<u64>,
60    /// Optional refund configs for the bundle item
61    pub refund_configs: Option<Vec<RefundConfig>>,
62}
63
64/// `Eth` sim bundle implementation.
65pub struct EthSimBundle<Eth> {
66    /// All nested fields bundled together.
67    inner: Arc<EthSimBundleInner<Eth>>,
68}
69
70impl<Eth> EthSimBundle<Eth> {
71    /// Create a new `EthSimBundle` instance.
72    pub fn new(eth_api: Eth, blocking_task_guard: BlockingTaskGuard) -> Self {
73        Self { inner: Arc::new(EthSimBundleInner { eth_api, blocking_task_guard }) }
74    }
75
76    /// Access the underlying `Eth` API.
77    pub fn eth_api(&self) -> &Eth {
78        &self.inner.eth_api
79    }
80
81    /// Builds a hierarchical `SimBundleLogs` structure from flattened transaction logs.
82    fn build_bundle_logs(
83        bundle: &MevSendBundle,
84        flat_logs: &[Vec<Log>],
85    ) -> Result<Vec<SimBundleLogs>, EthApiError> {
86        struct BundleFrame<'a> {
87            bundle: &'a MevSendBundle,
88            next_idx: usize,
89            logs: Vec<SimBundleLogs>,
90        }
91
92        let mut stack = vec![BundleFrame { bundle, next_idx: 0, logs: Vec::new() }];
93        let mut flat_log_idx = 0;
94        let mut root_logs = None;
95
96        while let Some(mut frame) = stack.pop() {
97            if frame.next_idx == frame.bundle.bundle_body.len() {
98                if let Some(parent) = stack.last_mut() {
99                    parent
100                        .logs
101                        .push(SimBundleLogs { tx_logs: None, bundle_logs: Some(frame.logs) });
102                } else {
103                    root_logs = Some(frame.logs);
104                }
105
106                continue;
107            }
108
109            match &frame.bundle.bundle_body[frame.next_idx] {
110                BundleItem::Tx { .. } => {
111                    let tx_logs = flat_logs.get(flat_log_idx).cloned().ok_or_else(|| {
112                        EthApiError::InvalidParams(EthSimBundleError::UnmatchedBundle.to_string())
113                    })?;
114
115                    frame.logs.push(SimBundleLogs { tx_logs: Some(tx_logs), bundle_logs: None });
116                    frame.next_idx += 1;
117                    flat_log_idx += 1;
118                    stack.push(frame);
119                }
120                BundleItem::Bundle { bundle } => {
121                    frame.next_idx += 1;
122                    stack.push(frame);
123                    stack.push(BundleFrame { bundle, next_idx: 0, logs: Vec::new() });
124                }
125                BundleItem::Hash { .. } => {
126                    return Err(EthApiError::InvalidParams(
127                        EthSimBundleError::InvalidBundle.to_string(),
128                    ));
129                }
130            }
131        }
132
133        if flat_log_idx != flat_logs.len() {
134            return Err(EthApiError::InvalidParams(EthSimBundleError::UnmatchedBundle.to_string()));
135        }
136
137        root_logs.ok_or_else(|| {
138            EthApiError::InvalidParams(EthSimBundleError::UnmatchedBundle.to_string())
139        })
140    }
141}
142
143impl<Eth> EthSimBundle<Eth>
144where
145    Eth: EthTransactions + LoadBlock + Call + 'static,
146{
147    /// Flattens a potentially nested bundle into a list of individual transactions in a
148    /// `FlattenedBundleItem` with their associated metadata. This handles recursive bundle
149    /// processing up to `MAX_NESTED_BUNDLE_DEPTH` and `MAX_BUNDLE_BODY_SIZE`, preserving
150    /// inclusion, validity and privacy settings from parent bundles.
151    fn parse_and_flatten_bundle(
152        &self,
153        request: &MevSendBundle,
154    ) -> Result<Vec<FlattenedBundleItem<ProviderTx<Eth::Provider>>>, EthApiError> {
155        let mut items = Vec::new();
156
157        // Stack for processing bundles
158        let mut stack = Vec::new();
159
160        // Start with initial bundle, index 0, and depth 1
161        stack.push((request, 0, 1));
162
163        while let Some((current_bundle, mut idx, depth)) = stack.pop() {
164            // Check max depth
165            if depth > MAX_NESTED_BUNDLE_DEPTH {
166                return Err(EthApiError::InvalidParams(EthSimBundleError::MaxDepth.to_string()));
167            }
168
169            // Determine inclusion, validity, and privacy
170            let inclusion = &current_bundle.inclusion;
171            let validity = &current_bundle.validity;
172            let privacy = &current_bundle.privacy;
173
174            // Validate inclusion parameters
175            let block_number = inclusion.block_number();
176            let max_block_number = inclusion.max_block_number().unwrap_or(block_number);
177
178            if max_block_number < block_number || block_number == 0 {
179                return Err(EthApiError::InvalidParams(
180                    EthSimBundleError::InvalidInclusion.to_string(),
181                ));
182            }
183
184            // Validate bundle body size
185            if current_bundle.bundle_body.len() > MAX_BUNDLE_BODY_SIZE {
186                return Err(EthApiError::InvalidParams(
187                    EthSimBundleError::BundleTooLarge.to_string(),
188                ));
189            }
190
191            // Validate validity and refund config
192            if let Some(validity) = &current_bundle.validity {
193                // Validate refund entries
194                if let Some(refunds) = &validity.refund {
195                    let mut total_percent = 0;
196                    for refund in refunds {
197                        if refund.body_idx as usize >= current_bundle.bundle_body.len() {
198                            return Err(EthApiError::InvalidParams(
199                                EthSimBundleError::InvalidValidity.to_string(),
200                            ));
201                        }
202                        if 100 - total_percent < refund.percent {
203                            return Err(EthApiError::InvalidParams(
204                                EthSimBundleError::InvalidValidity.to_string(),
205                            ));
206                        }
207                        total_percent += refund.percent;
208                    }
209                }
210
211                // Validate refund configs
212                if let Some(refund_configs) = &validity.refund_config {
213                    let mut total_percent = 0;
214                    for refund_config in refund_configs {
215                        if 100 - total_percent < refund_config.percent {
216                            return Err(EthApiError::InvalidParams(
217                                EthSimBundleError::InvalidValidity.to_string(),
218                            ));
219                        }
220                        total_percent += refund_config.percent;
221                    }
222                }
223            }
224
225            let body = &current_bundle.bundle_body;
226
227            // Process items in the current bundle
228            while idx < body.len() {
229                match &body[idx] {
230                    BundleItem::Tx { tx, can_revert } => {
231                        let recovered_tx = recover_raw_transaction::<PoolPooledTx<Eth::Pool>>(tx)?;
232                        let tx = recovered_tx.map(
233                            <Eth::Pool as TransactionPool>::Transaction::pooled_into_consensus,
234                        );
235
236                        let refund_percent =
237                            validity.as_ref().and_then(|v| v.refund.as_ref()).and_then(|refunds| {
238                                refunds.iter().find_map(|refund| {
239                                    (refund.body_idx as usize == idx).then_some(refund.percent)
240                                })
241                            });
242                        let refund_configs =
243                            validity.as_ref().and_then(|v| v.refund_config.clone());
244
245                        // Create FlattenedBundleItem with current inclusion, validity, and privacy
246                        let flattened_item = FlattenedBundleItem {
247                            tx,
248                            can_revert: *can_revert,
249                            inclusion: inclusion.clone(),
250                            validity: validity.clone(),
251                            privacy: privacy.clone(),
252                            refund_percent,
253                            refund_configs,
254                        };
255
256                        items.push(flattened_item);
257                        idx += 1;
258                    }
259                    BundleItem::Bundle { bundle } => {
260                        // Push the current bundle and next index onto the stack to resume later
261                        stack.push((current_bundle, idx + 1, depth));
262
263                        // process the nested bundle next
264                        stack.push((bundle, 0, depth + 1));
265                        break;
266                    }
267                    BundleItem::Hash { hash: _ } => {
268                        // Hash-only items are not allowed
269                        return Err(EthApiError::InvalidParams(
270                            EthSimBundleError::InvalidBundle.to_string(),
271                        ));
272                    }
273                }
274            }
275        }
276
277        Ok(items)
278    }
279
280    async fn sim_bundle_inner(
281        &self,
282        request: MevSendBundle,
283        overrides: SimBundleOverrides,
284        logs: bool,
285    ) -> Result<SimBundleResponse, Eth::Error> {
286        let SimBundleOverrides { parent_block, block_overrides, .. } = overrides;
287
288        // Parse and validate bundle
289        // Also, flatten the bundle here so that its easier to process
290        let flattened_bundle = self.parse_and_flatten_bundle(&request)?;
291
292        let block_id = parent_block.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest));
293        let (current_block, mut evm_env, current_block_id) =
294            self.eth_api().evm_env_and_recovered_block_at(block_id).await?;
295
296        let eth_api = self.inner.eth_api.clone();
297
298        let sim_response = self
299            .inner
300            .eth_api
301            .spawn_with_state_at_block(current_block_id, move |_, mut db| {
302                // Setup environment
303                let current_block_number = current_block.number();
304                let coinbase = evm_env.block_env.beneficiary();
305                let basefee = evm_env.block_env.basefee();
306
307                // apply overrides
308                apply_block_overrides(block_overrides, &mut db, evm_env.block_env.inner_mut());
309
310                let initial_coinbase_balance = DatabaseRef::basic_ref(&db, coinbase)
311                    .map_err(EthApiError::from_eth_err)?
312                    .map(|acc| acc.balance)
313                    .unwrap_or_default();
314
315                let mut coinbase_balance_before_tx = initial_coinbase_balance;
316                let mut total_gas_used = 0;
317                let mut total_profit = U256::ZERO;
318                let mut refundable_value = U256::ZERO;
319                let mut flat_logs: Vec<Vec<Log>> = Vec::new();
320
321                let mut evm = eth_api.evm_config().evm_with_env(db, evm_env);
322                let mut log_index = 0;
323
324                for (tx_index, item) in flattened_bundle.iter().enumerate() {
325                    // Check inclusion constraints
326                    let block_number = item.inclusion.block_number();
327                    let max_block_number =
328                        item.inclusion.max_block_number().unwrap_or(block_number);
329
330                    if current_block_number < block_number ||
331                        current_block_number > max_block_number
332                    {
333                        return Err(EthApiError::InvalidParams(
334                            EthSimBundleError::InvalidInclusion.to_string(),
335                        )
336                        .into());
337                    }
338
339                    let ResultAndState { result, state } = evm
340                        .transact(eth_api.evm_config().tx_env(&item.tx))
341                        .map_err(Eth::Error::from_evm_err)?;
342
343                    if !result.is_success() && !item.can_revert {
344                        return Err(EthApiError::InvalidParams(
345                            EthSimBundleError::BundleTransactionFailed.to_string(),
346                        )
347                        .into());
348                    }
349
350                    let gas_used = result.tx_gas_used();
351                    total_gas_used += gas_used;
352
353                    // coinbase is always present in the result state
354                    let coinbase_balance_after_tx =
355                        state.get(&coinbase).map(|acc| acc.info.balance).unwrap_or_default();
356
357                    let coinbase_diff =
358                        coinbase_balance_after_tx.saturating_sub(coinbase_balance_before_tx);
359                    total_profit += coinbase_diff;
360
361                    // Add to refundable value if this tx does not have a refund percent
362                    if item.refund_percent.is_none() {
363                        refundable_value += coinbase_diff;
364                    }
365
366                    // Update coinbase balance before next tx
367                    coinbase_balance_before_tx = coinbase_balance_after_tx;
368
369                    // Keep one log entry per executed transaction so we can rebuild the bundle
370                    // tree in execution order after simulation.
371                    if logs {
372                        let tx_logs: Vec<Log> = result
373                            .into_logs()
374                            .into_iter()
375                            .map(|inner| {
376                                let full_log = Log {
377                                    inner,
378                                    block_hash: Some(current_block.hash()),
379                                    block_number: Some(current_block.number()),
380                                    block_timestamp: Some(current_block.timestamp()),
381                                    transaction_hash: Some(*item.tx.tx_hash()),
382                                    transaction_index: Some(tx_index as u64),
383                                    log_index: Some(log_index),
384                                    removed: false,
385                                };
386                                log_index += 1;
387                                full_log
388                            })
389                            .collect();
390                        flat_logs.push(tx_logs);
391                    }
392
393                    // Apply state changes
394                    evm.db_mut().commit(state);
395                }
396
397                let body_logs =
398                    if logs { Self::build_bundle_logs(&request, &flat_logs)? } else { vec![] };
399
400                // After processing all transactions, process refunds
401                // Store the original refundable value to calculate all payouts correctly
402                let original_refundable_value = refundable_value;
403                for item in &flattened_bundle {
404                    if let Some(refund_percent) = item.refund_percent {
405                        let refund_configs = item.refund_configs.clone().unwrap_or_else(|| {
406                            vec![RefundConfig { address: item.tx.signer(), percent: 100 }]
407                        });
408
409                        // Calculate payout transaction fee
410                        let payout_tx_fee = U256::from(basefee) *
411                            U256::from(SBUNDLE_PAYOUT_MAX_COST) *
412                            U256::from(refund_configs.len() as u64);
413
414                        // Add gas used for payout transactions
415                        total_gas_used += SBUNDLE_PAYOUT_MAX_COST * refund_configs.len() as u64;
416
417                        // Calculate allocated refundable value (payout value) based on ORIGINAL
418                        // refundable value. This ensures all refund_percent values are
419                        // calculated from the same base.
420                        let payout_value = original_refundable_value * U256::from(refund_percent) /
421                            U256::from(100);
422
423                        if payout_tx_fee > payout_value {
424                            return Err(EthApiError::InvalidParams(
425                                EthSimBundleError::NegativeProfit.to_string(),
426                            )
427                            .into());
428                        }
429
430                        // Subtract payout value from total profit
431                        total_profit = total_profit.checked_sub(payout_value).ok_or(
432                            EthApiError::InvalidParams(
433                                EthSimBundleError::NegativeProfit.to_string(),
434                            ),
435                        )?;
436
437                        // Adjust refundable value
438                        refundable_value = refundable_value.checked_sub(payout_value).ok_or(
439                            EthApiError::InvalidParams(
440                                EthSimBundleError::NegativeProfit.to_string(),
441                            ),
442                        )?;
443                    }
444                }
445
446                // Calculate mev gas price
447                let mev_gas_price = if total_gas_used != 0 {
448                    total_profit / U256::from(total_gas_used)
449                } else {
450                    U256::ZERO
451                };
452
453                Ok(SimBundleResponse {
454                    success: true,
455                    state_block: current_block_number,
456                    error: None,
457                    logs: Some(body_logs),
458                    gas_used: total_gas_used,
459                    mev_gas_price,
460                    profit: total_profit,
461                    refundable_value,
462                    exec_error: None,
463                    revert: None,
464                })
465            })
466            .await?;
467
468        Ok(sim_response)
469    }
470}
471
472#[async_trait::async_trait]
473impl<Eth> MevSimApiServer for EthSimBundle<Eth>
474where
475    Eth: EthTransactions + LoadBlock + Call + 'static,
476{
477    async fn sim_bundle(
478        &self,
479        request: MevSendBundle,
480        overrides: SimBundleOverrides,
481    ) -> RpcResult<SimBundleResponse> {
482        trace!("mev_simBundle called, request: {:?}, overrides: {:?}", request, overrides);
483
484        let override_timeout = overrides.timeout;
485
486        let timeout = override_timeout
487            .map(Duration::from_secs)
488            .map(|d| d.min(MAX_SIM_TIMEOUT))
489            .unwrap_or(DEFAULT_SIM_TIMEOUT);
490
491        let bundle_res =
492            tokio::time::timeout(timeout, Self::sim_bundle_inner(self, request, overrides, true))
493                .await
494                .map_err(|_| {
495                    EthApiError::InvalidParams(EthSimBundleError::BundleTimeout.to_string())
496                })?;
497
498        bundle_res.map_err(Into::into)
499    }
500}
501
502/// Container type for `EthSimBundle` internals
503#[derive(Debug)]
504struct EthSimBundleInner<Eth> {
505    /// Access to commonly used code of the `eth` namespace
506    eth_api: Eth,
507    // restrict the number of concurrent tracing calls.
508    #[expect(dead_code)]
509    blocking_task_guard: BlockingTaskGuard,
510}
511
512impl<Eth> std::fmt::Debug for EthSimBundle<Eth> {
513    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
514        f.debug_struct("EthSimBundle").finish_non_exhaustive()
515    }
516}
517
518impl<Eth> Clone for EthSimBundle<Eth> {
519    fn clone(&self) -> Self {
520        Self { inner: Arc::clone(&self.inner) }
521    }
522}
523
524/// [`EthSimBundle`] specific errors.
525#[derive(Debug, thiserror::Error)]
526pub enum EthSimBundleError {
527    /// Thrown when max depth is reached
528    #[error("max depth reached")]
529    MaxDepth,
530    /// Thrown when a bundle is unmatched
531    #[error("unmatched bundle")]
532    UnmatchedBundle,
533    /// Thrown when a bundle is too large
534    #[error("bundle too large")]
535    BundleTooLarge,
536    /// Thrown when validity is invalid
537    #[error("invalid validity")]
538    InvalidValidity,
539    /// Thrown when inclusion is invalid
540    #[error("invalid inclusion")]
541    InvalidInclusion,
542    /// Thrown when a bundle is invalid
543    #[error("invalid bundle")]
544    InvalidBundle,
545    /// Thrown when a bundle simulation times out
546    #[error("bundle simulation timed out")]
547    BundleTimeout,
548    /// Thrown when a transaction is reverted in a bundle
549    #[error("bundle transaction failed")]
550    BundleTransactionFailed,
551    /// Thrown when a bundle simulation returns negative profit
552    #[error("bundle simulation returned negative profit")]
553    NegativeProfit,
554}
555
556#[cfg(test)]
557mod tests {
558    use super::*;
559    use alloy_primitives::Bytes;
560    use alloy_rpc_types_mev::{Inclusion, ProtocolVersion};
561
562    fn create_test_bundle(tx_bytes: Vec<Bytes>) -> MevSendBundle {
563        let body: Vec<BundleItem> =
564            tx_bytes.into_iter().map(|tx| BundleItem::Tx { tx, can_revert: false }).collect();
565        MevSendBundle {
566            bundle_body: body,
567            inclusion: Inclusion { block: 1, max_block: None },
568            validity: None,
569            privacy: None,
570            protocol_version: ProtocolVersion::V0_1,
571        }
572    }
573
574    fn create_nested_bundle(outer_tx: Bytes, inner_txs: Vec<Bytes>) -> MevSendBundle {
575        let inner_bundle = create_test_bundle(inner_txs);
576        MevSendBundle {
577            bundle_body: vec![
578                BundleItem::Tx { tx: outer_tx, can_revert: false },
579                BundleItem::Bundle { bundle: inner_bundle },
580            ],
581            inclusion: Inclusion { block: 1, max_block: None },
582            validity: None,
583            privacy: None,
584            protocol_version: ProtocolVersion::V0_1,
585        }
586    }
587
588    fn create_bundle_with_body(bundle_body: Vec<BundleItem>) -> MevSendBundle {
589        MevSendBundle {
590            bundle_body,
591            inclusion: Inclusion { block: 1, max_block: None },
592            validity: None,
593            privacy: None,
594            protocol_version: ProtocolVersion::V0_1,
595        }
596    }
597
598    fn create_bundle_logs(log_counts: &[usize]) -> Vec<Vec<Log>> {
599        log_counts.iter().map(|count| vec![Log::default(); *count]).collect()
600    }
601
602    fn assert_unmatched_bundle(result: Result<Vec<SimBundleLogs>, EthApiError>) {
603        assert!(matches!(
604            result,
605            Err(EthApiError::InvalidParams(ref message))
606                if message == &EthSimBundleError::UnmatchedBundle.to_string()
607        ));
608    }
609
610    #[test]
611    fn test_build_bundle_logs_single_tx() {
612        let bundle = create_test_bundle(vec![Bytes::from(vec![0x01, 0x02, 0x03])]);
613        let result =
614            EthSimBundle::<()>::build_bundle_logs(&bundle, &create_bundle_logs(&[1])).unwrap();
615
616        assert_eq!(result.len(), 1);
617        assert!(result[0].tx_logs.is_some());
618        assert!(result[0].bundle_logs.is_none());
619        assert_eq!(result[0].tx_logs.as_ref().unwrap().len(), 1);
620    }
621
622    #[test]
623    fn test_build_bundle_logs_empty_bundle() {
624        let bundle = create_test_bundle(vec![]);
625        let result = EthSimBundle::<()>::build_bundle_logs(&bundle, &[]).unwrap();
626
627        assert!(result.is_empty());
628    }
629
630    #[test]
631    fn test_build_bundle_logs_nested_bundle() {
632        let outer_tx = Bytes::from(vec![0x01, 0x02, 0x03]);
633        let inner_tx1 = Bytes::from(vec![0x04, 0x05, 0x06]);
634        let inner_tx2 = Bytes::from(vec![0x07, 0x08, 0x09]);
635        let bundle = create_nested_bundle(outer_tx, vec![inner_tx1, inner_tx2]);
636        let result =
637            EthSimBundle::<()>::build_bundle_logs(&bundle, &create_bundle_logs(&[1, 1, 2]))
638                .unwrap();
639
640        assert_eq!(result.len(), 2);
641        assert!(result[0].tx_logs.is_some());
642        assert!(result[0].bundle_logs.is_none());
643        assert_eq!(result[0].tx_logs.as_ref().unwrap().len(), 1);
644
645        assert!(result[1].tx_logs.is_none());
646        assert!(result[1].bundle_logs.is_some());
647
648        let nested_logs = result[1].bundle_logs.as_ref().unwrap();
649        assert_eq!(nested_logs.len(), 2);
650        assert!(nested_logs[0].tx_logs.is_some());
651        assert_eq!(nested_logs[0].tx_logs.as_ref().unwrap().len(), 1);
652        assert!(nested_logs[1].tx_logs.is_some());
653        assert_eq!(nested_logs[1].tx_logs.as_ref().unwrap().len(), 2);
654    }
655
656    #[test]
657    fn test_build_bundle_logs_duplicate_transactions_same_level() {
658        let duplicate_tx = Bytes::from(vec![0x01, 0x02, 0x03]);
659        let bundle = create_test_bundle(vec![duplicate_tx.clone(), duplicate_tx]);
660        let result =
661            EthSimBundle::<()>::build_bundle_logs(&bundle, &create_bundle_logs(&[1, 2])).unwrap();
662
663        assert_eq!(result.len(), 2);
664        assert_eq!(result[0].tx_logs.as_ref().unwrap().len(), 1);
665        assert_eq!(result[1].tx_logs.as_ref().unwrap().len(), 2);
666    }
667
668    #[test]
669    fn test_build_bundle_logs_duplicate_transactions_across_nested_bundles() {
670        let duplicate_tx = Bytes::from(vec![0x01, 0x02, 0x03]);
671        let bundle = create_nested_bundle(duplicate_tx.clone(), vec![duplicate_tx]);
672        let result =
673            EthSimBundle::<()>::build_bundle_logs(&bundle, &create_bundle_logs(&[1, 2])).unwrap();
674
675        assert_eq!(result.len(), 2);
676        assert!(result[1].bundle_logs.is_some());
677        assert_eq!(result[0].tx_logs.as_ref().unwrap().len(), 1);
678
679        let nested_logs = result[1].bundle_logs.as_ref().unwrap();
680        assert_eq!(nested_logs.len(), 1);
681        assert_eq!(nested_logs[0].tx_logs.as_ref().unwrap().len(), 2);
682    }
683
684    #[test]
685    fn test_build_bundle_logs_root_with_only_nested_bundles() {
686        let first_nested = create_test_bundle(vec![Bytes::from(vec![0x01])]);
687        let second_nested =
688            create_test_bundle(vec![Bytes::from(vec![0x02]), Bytes::from(vec![0x03])]);
689        let bundle = create_bundle_with_body(vec![
690            BundleItem::Bundle { bundle: first_nested },
691            BundleItem::Bundle { bundle: second_nested },
692        ]);
693        let result =
694            EthSimBundle::<()>::build_bundle_logs(&bundle, &create_bundle_logs(&[1, 1, 2]))
695                .unwrap();
696
697        assert_eq!(result.len(), 2);
698        assert!(result[0].tx_logs.is_none());
699        assert!(result[1].tx_logs.is_none());
700
701        let first_nested_logs = result[0].bundle_logs.as_ref().unwrap();
702        assert_eq!(first_nested_logs.len(), 1);
703        assert_eq!(first_nested_logs[0].tx_logs.as_ref().unwrap().len(), 1);
704
705        let second_nested_logs = result[1].bundle_logs.as_ref().unwrap();
706        assert_eq!(second_nested_logs.len(), 2);
707        assert_eq!(second_nested_logs[0].tx_logs.as_ref().unwrap().len(), 1);
708        assert_eq!(second_nested_logs[1].tx_logs.as_ref().unwrap().len(), 2);
709    }
710
711    #[test]
712    fn test_build_bundle_logs_deeply_nested_bundle() {
713        let leaf_bundle = create_test_bundle(vec![Bytes::from(vec![0x03])]);
714        let middle_bundle = create_bundle_with_body(vec![
715            BundleItem::Tx { tx: Bytes::from(vec![0x02]), can_revert: false },
716            BundleItem::Bundle { bundle: leaf_bundle },
717        ]);
718        let root_bundle = create_bundle_with_body(vec![
719            BundleItem::Tx { tx: Bytes::from(vec![0x01]), can_revert: false },
720            BundleItem::Bundle { bundle: middle_bundle },
721        ]);
722        let result =
723            EthSimBundle::<()>::build_bundle_logs(&root_bundle, &create_bundle_logs(&[1, 2, 3]))
724                .unwrap();
725
726        assert_eq!(result.len(), 2);
727        assert_eq!(result[0].tx_logs.as_ref().unwrap().len(), 1);
728
729        let middle_logs = result[1].bundle_logs.as_ref().unwrap();
730        assert_eq!(middle_logs.len(), 2);
731        assert_eq!(middle_logs[0].tx_logs.as_ref().unwrap().len(), 2);
732
733        let leaf_logs = middle_logs[1].bundle_logs.as_ref().unwrap();
734        assert_eq!(leaf_logs.len(), 1);
735        assert_eq!(leaf_logs[0].tx_logs.as_ref().unwrap().len(), 3);
736    }
737
738    #[test]
739    fn test_build_bundle_logs_mismatched_flat_logs() {
740        let bundle = create_test_bundle(vec![Bytes::from(vec![0x01, 0x02, 0x03])]);
741
742        assert_unmatched_bundle(EthSimBundle::<()>::build_bundle_logs(&bundle, &[]));
743        assert_unmatched_bundle(EthSimBundle::<()>::build_bundle_logs(
744            &bundle,
745            &create_bundle_logs(&[1, 2]),
746        ));
747    }
748}