Skip to main content

reth_execution_types/
execution_outcome.rs

1use crate::{BlockExecutionOutput, BlockExecutionResult};
2use alloc::{vec, vec::Vec};
3use alloy_eips::eip7685::Requests;
4use alloy_primitives::{
5    logs_bloom,
6    map::{AddressMap, B256Map, HashMap},
7    Address, BlockNumber, Bloom, Log, B256, U256,
8};
9use reth_primitives_traits::{Account, Bytecode, Receipt, StorageEntry};
10use reth_trie_common::{HashedPostState, KeyHasher};
11use revm::{
12    database::{states::BundleState, BundleAccount},
13    state::AccountInfo,
14};
15
16/// Type used to initialize revms bundle state.
17pub type BundleStateInit = AddressMap<(Option<Account>, Option<Account>, B256Map<(U256, U256)>)>;
18
19/// Types used inside `RevertsInit` to initialize revms reverts.
20pub type AccountRevertInit = (Option<Option<Account>>, Vec<StorageEntry>);
21
22/// Type used to initialize revms reverts.
23pub type RevertsInit = HashMap<BlockNumber, AddressMap<AccountRevertInit>>;
24
25/// Represents a changed account
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub struct ChangedAccount {
28    /// The address of the account.
29    pub address: Address,
30    /// Account nonce.
31    pub nonce: u64,
32    /// Account balance.
33    pub balance: U256,
34}
35
36impl ChangedAccount {
37    /// Creates a new [`ChangedAccount`] with the given address and 0 balance and nonce.
38    pub const fn empty(address: Address) -> Self {
39        Self { address, nonce: 0, balance: U256::ZERO }
40    }
41}
42
43/// Represents the outcome of block execution, including post-execution changes and reverts.
44///
45/// The `ExecutionOutcome` structure aggregates the state changes over an arbitrary number of
46/// blocks, capturing the resulting state, receipts, and requests following the execution.
47#[derive(Debug, Clone, PartialEq, Eq)]
48#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
49pub struct ExecutionOutcome<T = reth_ethereum_primitives::Receipt> {
50    /// Bundle state with reverts.
51    pub bundle: BundleState,
52    /// The collection of receipts.
53    /// Outer vector stores receipts for each block sequentially.
54    /// The inner vector stores receipts ordered by transaction number.
55    pub receipts: Vec<Vec<T>>,
56    /// First block of bundle state.
57    pub first_block: BlockNumber,
58    /// The collection of EIP-7685 requests.
59    /// Outer vector stores requests for each block sequentially.
60    /// The inner vector stores requests ordered by transaction number.
61    ///
62    /// A transaction may have zero or more requests, so the length of the inner vector is not
63    /// guaranteed to be the same as the number of transactions.
64    pub requests: Vec<Requests>,
65}
66
67impl<T> Default for ExecutionOutcome<T> {
68    fn default() -> Self {
69        Self {
70            bundle: Default::default(),
71            receipts: Default::default(),
72            first_block: Default::default(),
73            requests: Default::default(),
74        }
75    }
76}
77
78impl<T> ExecutionOutcome<T> {
79    /// Creates a new `ExecutionOutcome`.
80    ///
81    /// This constructor initializes a new `ExecutionOutcome` instance with the provided
82    /// bundle state, receipts, first block number, and EIP-7685 requests.
83    pub const fn new(
84        bundle: BundleState,
85        receipts: Vec<Vec<T>>,
86        first_block: BlockNumber,
87        requests: Vec<Requests>,
88    ) -> Self {
89        Self { bundle, receipts, first_block, requests }
90    }
91
92    /// Creates a new `ExecutionOutcome` from initialization parameters.
93    ///
94    /// This constructor initializes a new `ExecutionOutcome` instance using detailed
95    /// initialization parameters.
96    pub fn new_init(
97        state_init: BundleStateInit,
98        revert_init: RevertsInit,
99        contracts_init: impl IntoIterator<Item = (B256, Bytecode)>,
100        receipts: Vec<Vec<T>>,
101        first_block: BlockNumber,
102        requests: Vec<Requests>,
103    ) -> Self {
104        // sort reverts by block number
105        let mut reverts = revert_init.into_iter().collect::<Vec<_>>();
106        reverts.sort_unstable_by_key(|a| a.0);
107
108        // initialize revm bundle
109        let bundle = BundleState::new(
110            state_init.into_iter().map(|(address, (original, present, storage))| {
111                (
112                    address,
113                    original.map(Into::into),
114                    present.map(Into::into),
115                    storage.into_iter().map(|(k, s)| (k.into(), s)).collect(),
116                )
117            }),
118            reverts.into_iter().map(|(_, reverts)| {
119                // does not need to be sorted, it is done when taking reverts.
120                reverts.into_iter().map(|(address, (original, storage))| {
121                    (
122                        address,
123                        original.map(|i| i.map(Into::into)),
124                        storage.into_iter().map(|entry| (entry.key.into(), entry.value)),
125                    )
126                })
127            }),
128            contracts_init.into_iter().map(|(code_hash, bytecode)| (code_hash, bytecode.0)),
129        );
130
131        Self { bundle, receipts, first_block, requests }
132    }
133
134    /// Creates a new `ExecutionOutcome` from a single block execution result.
135    pub fn single(block_number: u64, output: BlockExecutionOutput<T>) -> Self {
136        Self {
137            bundle: output.state,
138            receipts: vec![output.result.receipts],
139            first_block: block_number,
140            requests: vec![output.result.requests],
141        }
142    }
143
144    /// Creates a new `ExecutionOutcome` from multiple [`BlockExecutionResult`]s.
145    pub fn from_blocks(
146        first_block: u64,
147        bundle: BundleState,
148        results: Vec<BlockExecutionResult<T>>,
149    ) -> Self {
150        let mut value = Self {
151            bundle,
152            first_block,
153            receipts: Vec::with_capacity(results.len()),
154            requests: Vec::with_capacity(results.len()),
155        };
156        for result in results {
157            value.receipts.push(result.receipts);
158            value.requests.push(result.requests);
159        }
160        value
161    }
162
163    /// Return revm bundle state.
164    pub const fn state(&self) -> &BundleState {
165        &self.bundle
166    }
167
168    /// Returns mutable revm bundle state.
169    pub const fn state_mut(&mut self) -> &mut BundleState {
170        &mut self.bundle
171    }
172
173    /// Set first block.
174    pub const fn set_first_block(&mut self, first_block: BlockNumber) {
175        self.first_block = first_block;
176    }
177
178    /// Return iterator over all accounts
179    pub fn accounts_iter(&self) -> impl Iterator<Item = (Address, Option<&AccountInfo>)> {
180        self.bundle.state().iter().map(|(a, acc)| (*a, acc.info.as_ref()))
181    }
182
183    /// Return iterator over all [`BundleAccount`]s in the bundle
184    pub fn bundle_accounts_iter(&self) -> impl Iterator<Item = (Address, &BundleAccount)> {
185        self.bundle.state().iter().map(|(a, acc)| (*a, acc))
186    }
187
188    /// Get account if account is known.
189    pub fn account(&self, address: &Address) -> Option<Option<Account>> {
190        self.bundle.account(address).map(|a| a.info.as_ref().map(Into::into))
191    }
192
193    /// Returns the state [`BundleAccount`] for the given account.
194    pub fn account_state(&self, address: &Address) -> Option<&BundleAccount> {
195        self.bundle.account(address)
196    }
197
198    /// Get storage if value is known.
199    ///
200    /// This means that depending on status we can potentially return `U256::ZERO`.
201    pub fn storage(&self, address: &Address, storage_key: U256) -> Option<U256> {
202        self.bundle.account(address).and_then(|a| a.storage_slot(storage_key))
203    }
204
205    /// Return bytecode if known.
206    pub fn bytecode(&self, code_hash: &B256) -> Option<Bytecode> {
207        self.bundle.bytecode(code_hash).map(Bytecode)
208    }
209
210    /// Returns [`HashedPostState`] for this execution outcome.
211    /// See [`HashedPostState::from_bundle_state`] for more info.
212    pub fn hash_state_slow<KH: KeyHasher>(&self) -> HashedPostState {
213        HashedPostState::from_bundle_state::<KH>(&self.bundle.state)
214    }
215
216    /// Transform block number to the index of block.
217    pub const fn block_number_to_index(&self, block_number: BlockNumber) -> Option<usize> {
218        if self.first_block > block_number {
219            return None
220        }
221        let index = block_number - self.first_block;
222        if index >= self.receipts.len() as u64 {
223            return None
224        }
225        Some(index as usize)
226    }
227
228    /// Returns the receipt root for all recorded receipts.
229    /// Note: this function calculated Bloom filters for every receipt and created merkle trees
230    /// of receipt. This is an expensive operation.
231    pub fn generic_receipts_root_slow(
232        &self,
233        block_number: BlockNumber,
234        f: impl FnOnce(&[T]) -> B256,
235    ) -> Option<B256> {
236        Some(f(self.receipts.get(self.block_number_to_index(block_number)?)?))
237    }
238
239    /// Returns reference to receipts.
240    pub const fn receipts(&self) -> &Vec<Vec<T>> {
241        &self.receipts
242    }
243
244    /// Returns mutable reference to receipts.
245    pub const fn receipts_mut(&mut self) -> &mut Vec<Vec<T>> {
246        &mut self.receipts
247    }
248
249    /// Return all block receipts
250    pub fn receipts_by_block(&self, block_number: BlockNumber) -> &[T] {
251        let Some(index) = self.block_number_to_index(block_number) else { return &[] };
252        &self.receipts[index]
253    }
254
255    /// Returns an iterator over receipt slices, one per block.
256    ///
257    /// This is a more ergonomic alternative to `receipts()` that yields slices
258    /// instead of requiring indexing into a nested `Vec<Vec<T>>`.
259    pub fn receipts_iter(&self) -> impl Iterator<Item = &[T]> + '_ {
260        self.receipts.iter().map(|v| v.as_slice())
261    }
262
263    /// Is execution outcome empty.
264    pub const fn is_empty(&self) -> bool {
265        self.len() == 0
266    }
267
268    /// Number of blocks in the execution outcome.
269    pub const fn len(&self) -> usize {
270        self.receipts.len()
271    }
272
273    /// Return first block of the execution outcome
274    pub const fn first_block(&self) -> BlockNumber {
275        self.first_block
276    }
277
278    /// Return last block of the execution outcome
279    pub const fn last_block(&self) -> BlockNumber {
280        (self.first_block + self.len() as u64).saturating_sub(1)
281    }
282
283    /// Revert the state to the given block number.
284    ///
285    /// Returns false if the block number is not in the bundle state.
286    ///
287    /// # Note
288    ///
289    /// The provided block number will stay inside the bundle state.
290    pub fn revert_to(&mut self, block_number: BlockNumber) -> bool {
291        let Some(index) = self.block_number_to_index(block_number) else { return false };
292
293        // +1 is for number of blocks that we have as index is included.
294        let new_len = index + 1;
295        let rm_trx: usize = self.len() - new_len;
296
297        // remove receipts
298        self.receipts.truncate(new_len);
299        // remove requests
300        self.requests.truncate(new_len);
301        // Revert last n reverts.
302        self.bundle.revert(rm_trx);
303
304        true
305    }
306
307    /// Splits the block range state at a given block number.
308    /// Returns two split states ([..at], [at..]).
309    /// The plain state of the 2nd bundle state will contain extra changes
310    /// that were made in state transitions belonging to the lower state.
311    ///
312    /// # Panics
313    ///
314    /// If the target block number is not included in the state block range.
315    pub fn split_at(self, at: BlockNumber) -> (Option<Self>, Self)
316    where
317        T: Clone,
318    {
319        if at == self.first_block {
320            return (None, self)
321        }
322
323        let (mut lower_state, mut higher_state) = (self.clone(), self);
324
325        // Revert lower state to [..at].
326        lower_state.revert_to(at.checked_sub(1).unwrap());
327
328        // Truncate higher state to [at..].
329        let at_idx = higher_state.block_number_to_index(at).unwrap();
330        higher_state.receipts = higher_state.receipts.split_off(at_idx);
331        // Ensure that there are enough requests to truncate.
332        // Sometimes we just have receipts and no requests.
333        if at_idx < higher_state.requests.len() {
334            higher_state.requests = higher_state.requests.split_off(at_idx);
335        }
336        higher_state.bundle.take_n_reverts(at_idx);
337        higher_state.first_block = at;
338
339        (Some(lower_state), higher_state)
340    }
341
342    /// Extend one state from another
343    ///
344    /// For state this is very sensitive operation and should be used only when
345    /// we know that other state was build on top of this one.
346    /// In most cases this would be true.
347    pub fn extend(&mut self, other: Self) {
348        self.bundle.extend(other.bundle);
349        self.receipts.extend(other.receipts);
350        self.requests.extend(other.requests);
351    }
352
353    /// Prepends present the state with the given `BundleState`.
354    /// It adds changes from the given state but does not override any existing changes.
355    ///
356    /// Reverts and receipts are not updated.
357    pub fn prepend_state(&mut self, mut other: BundleState) {
358        let other_len = other.reverts.len();
359        // take this bundle
360        let this_bundle = core::mem::take(&mut self.bundle);
361        // extend other bundle with this
362        other.extend(this_bundle);
363        // discard other reverts
364        other.take_n_reverts(other_len);
365        // swap bundles
366        core::mem::swap(&mut self.bundle, &mut other)
367    }
368
369    /// Create a new instance with updated receipts.
370    pub fn with_receipts(mut self, receipts: Vec<Vec<T>>) -> Self {
371        self.receipts = receipts;
372        self
373    }
374
375    /// Create a new instance with updated requests.
376    pub fn with_requests(mut self, requests: Vec<Requests>) -> Self {
377        self.requests = requests;
378        self
379    }
380
381    /// Returns an iterator over all changed accounts from the `ExecutionOutcome`.
382    ///
383    /// This method filters the accounts to return only those that have undergone changes
384    /// and maps them into `ChangedAccount` instances, which include the address, nonce, and
385    /// balance.
386    pub fn changed_accounts(&self) -> impl Iterator<Item = ChangedAccount> + '_ {
387        self.accounts_iter().filter_map(|(addr, acc)| acc.map(|acc| (addr, acc))).map(
388            |(address, acc)| ChangedAccount { address, nonce: acc.nonce, balance: acc.balance },
389        )
390    }
391}
392
393impl<T: Receipt<Log = Log>> ExecutionOutcome<T> {
394    /// Returns an iterator over all block logs.
395    pub fn logs(&self, block_number: BlockNumber) -> Option<impl Iterator<Item = &Log>> {
396        let index = self.block_number_to_index(block_number)?;
397        Some(self.receipts[index].iter().flat_map(|r| r.logs()))
398    }
399
400    /// Return blocks logs bloom
401    pub fn block_logs_bloom(&self, block_number: BlockNumber) -> Option<Bloom> {
402        Some(logs_bloom(self.logs(block_number)?))
403    }
404}
405
406impl ExecutionOutcome {
407    /// Returns the ethereum receipt root for all recorded receipts.
408    ///
409    /// Note: this function calculated Bloom filters for every receipt and created merkle trees
410    /// of receipt. This is an expensive operation.
411    pub fn ethereum_receipts_root(&self, block_number: BlockNumber) -> Option<B256> {
412        self.generic_receipts_root_slow(
413            block_number,
414            reth_ethereum_primitives::calculate_receipt_root_no_memo,
415        )
416    }
417}
418
419impl<T> From<(BlockExecutionOutput<T>, BlockNumber)> for ExecutionOutcome<T> {
420    fn from((output, block_number): (BlockExecutionOutput<T>, BlockNumber)) -> Self {
421        Self::single(block_number, output)
422    }
423}
424
425#[cfg(feature = "serde-bincode-compat")]
426pub(super) mod serde_bincode_compat {
427    use alloc::{borrow::Cow, vec::Vec};
428    use alloy_eips::eip7685::Requests;
429    use alloy_primitives::{BlockNumber, Bytes};
430    use reth_primitives_traits::Receipt;
431    use revm::database::BundleState;
432    use serde::{Deserialize, Deserializer, Serialize, Serializer};
433    use serde_with::{DeserializeAs, SerializeAs};
434
435    /// Bincode-compatible [`super::ExecutionOutcome`] serde implementation.
436    ///
437    /// Intended to use with the [`serde_with::serde_as`] macro in the following way:
438    /// ```rust
439    /// use reth_execution_types::{serde_bincode_compat, ExecutionOutcome};
440    /// use serde::{Deserialize, Serialize};
441    /// use serde_with::serde_as;
442    ///
443    /// #[serde_as]
444    /// #[derive(Serialize, Deserialize)]
445    /// struct Data {
446    ///     #[serde_as(as = "serde_bincode_compat::ExecutionOutcome<'_>")]
447    ///     chain: ExecutionOutcome,
448    /// }
449    /// ```
450    #[derive(Debug, Serialize, Deserialize)]
451    pub struct ExecutionOutcome<'a> {
452        bundle: Cow<'a, BundleState>,
453        receipts: Vec<Vec<Bytes>>,
454        first_block: BlockNumber,
455        #[expect(clippy::owned_cow)]
456        requests: Cow<'a, Vec<Requests>>,
457    }
458
459    impl<'a, T> From<&'a super::ExecutionOutcome<T>> for ExecutionOutcome<'a>
460    where
461        T: Receipt,
462    {
463        fn from(value: &'a super::ExecutionOutcome<T>) -> Self {
464            ExecutionOutcome {
465                bundle: Cow::Borrowed(&value.bundle),
466                receipts: value
467                    .receipts
468                    .iter()
469                    .map(|vec| {
470                        vec.iter().map(|receipt| Bytes::from(alloy_rlp::encode(receipt))).collect()
471                    })
472                    .collect(),
473                first_block: value.first_block,
474                requests: Cow::Borrowed(&value.requests),
475            }
476        }
477    }
478
479    impl<T> From<ExecutionOutcome<'_>> for super::ExecutionOutcome<T>
480    where
481        T: Receipt,
482    {
483        fn from(value: ExecutionOutcome<'_>) -> Self {
484            Self {
485                bundle: value.bundle.into_owned(),
486                receipts: value
487                    .receipts
488                    .into_iter()
489                    .map(|vec| {
490                        vec.into_iter()
491                            .map(|rlp| {
492                                T::decode(&mut rlp.as_ref())
493                                    .expect("invalid RLP for receipt in serde_bincode_compat")
494                            })
495                            .collect()
496                    })
497                    .collect(),
498                first_block: value.first_block,
499                requests: value.requests.into_owned(),
500            }
501        }
502    }
503
504    impl<T> SerializeAs<super::ExecutionOutcome<T>> for ExecutionOutcome<'_>
505    where
506        T: Receipt,
507    {
508        fn serialize_as<S>(
509            source: &super::ExecutionOutcome<T>,
510            serializer: S,
511        ) -> Result<S::Ok, S::Error>
512        where
513            S: Serializer,
514        {
515            ExecutionOutcome::from(source).serialize(serializer)
516        }
517    }
518
519    impl<'de, T> DeserializeAs<'de, super::ExecutionOutcome<T>> for ExecutionOutcome<'de>
520    where
521        T: Receipt,
522    {
523        fn deserialize_as<D>(deserializer: D) -> Result<super::ExecutionOutcome<T>, D::Error>
524        where
525            D: Deserializer<'de>,
526        {
527            ExecutionOutcome::deserialize(deserializer).map(Into::into)
528        }
529    }
530
531    #[cfg(test)]
532    mod tests {
533        use super::super::{serde_bincode_compat, ExecutionOutcome};
534        use rand::Rng;
535        use reth_ethereum_primitives::Receipt;
536        use serde::{Deserialize, Serialize};
537        use serde_with::serde_as;
538
539        #[test]
540        fn test_chain_bincode_roundtrip() {
541            #[serde_as]
542            #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
543            struct Data<T: reth_primitives_traits::Receipt> {
544                #[serde_as(as = "serde_bincode_compat::ExecutionOutcome<'_>")]
545                data: ExecutionOutcome<T>,
546            }
547
548            let mut bytes = [0u8; 1024];
549            rand::rng().fill(bytes.as_mut_slice());
550            let data = Data {
551                data: ExecutionOutcome {
552                    bundle: Default::default(),
553                    receipts: vec![],
554                    first_block: 0,
555                    requests: vec![],
556                },
557            };
558
559            let encoded = bincode::serialize(&data).unwrap();
560            let decoded = bincode::deserialize::<Data<Receipt>>(&encoded).unwrap();
561            assert_eq!(decoded, data);
562        }
563    }
564}
565
566#[cfg(test)]
567mod tests {
568    use super::*;
569    use alloy_consensus::TxType;
570    use alloy_primitives::{bytes, Address, LogData, B256};
571
572    #[test]
573    fn test_initialization() {
574        // Create a new BundleState object with initial data
575        let bundle = BundleState::new(
576            vec![(Address::new([2; 20]), None, Some(AccountInfo::default()), HashMap::default())],
577            vec![vec![(Address::new([2; 20]), None, vec![])]],
578            vec![],
579        );
580
581        // Create a Receipts object with a vector of receipt vectors
582        let receipts = vec![vec![Some(reth_ethereum_primitives::Receipt {
583            tx_type: TxType::Legacy,
584            cumulative_gas_used: 46913,
585            logs: vec![],
586            success: true,
587        })]];
588
589        // Create a Requests object with a vector of requests
590        let requests = vec![Requests::new(vec![bytes!("dead"), bytes!("beef"), bytes!("beebee")])];
591
592        // Define the first block number
593        let first_block = 123;
594
595        // Create a ExecutionOutcome object with the created bundle, receipts, requests, and
596        // first_block
597        let exec_res = ExecutionOutcome {
598            bundle: bundle.clone(),
599            receipts: receipts.clone(),
600            requests: requests.clone(),
601            first_block,
602        };
603
604        // Assert that creating a new ExecutionOutcome using the constructor matches exec_res
605        assert_eq!(
606            ExecutionOutcome::new(bundle, receipts.clone(), first_block, requests.clone()),
607            exec_res
608        );
609
610        // Create a BundleStateInit object and insert initial data
611        let mut state_init: BundleStateInit = AddressMap::default();
612        state_init
613            .insert(Address::new([2; 20]), (None, Some(Account::default()), B256Map::default()));
614
615        // Create an AddressMap for account reverts and insert initial data
616        let mut revert_inner: AddressMap<AccountRevertInit> = AddressMap::default();
617        revert_inner.insert(Address::new([2; 20]), (None, vec![]));
618
619        // Create a RevertsInit object and insert the revert_inner data
620        let mut revert_init: RevertsInit = HashMap::default();
621        revert_init.insert(123, revert_inner);
622
623        // Assert that creating a new ExecutionOutcome using the new_init method matches
624        // exec_res
625        assert_eq!(
626            ExecutionOutcome::new_init(
627                state_init,
628                revert_init,
629                vec![],
630                receipts,
631                first_block,
632                requests,
633            ),
634            exec_res
635        );
636    }
637
638    #[test]
639    fn test_block_number_to_index() {
640        // Create a Receipts object with a vector of receipt vectors
641        let receipts = vec![vec![Some(reth_ethereum_primitives::Receipt {
642            tx_type: TxType::Legacy,
643            cumulative_gas_used: 46913,
644            logs: vec![],
645            success: true,
646        })]];
647
648        // Define the first block number
649        let first_block = 123;
650
651        // Create a ExecutionOutcome object with the created bundle, receipts, requests, and
652        // first_block
653        let exec_res = ExecutionOutcome {
654            bundle: Default::default(),
655            receipts,
656            requests: vec![],
657            first_block,
658        };
659
660        // Test before the first block
661        assert_eq!(exec_res.block_number_to_index(12), None);
662
663        // Test after the first block but index larger than receipts length
664        assert_eq!(exec_res.block_number_to_index(133), None);
665
666        // Test after the first block
667        assert_eq!(exec_res.block_number_to_index(123), Some(0));
668    }
669
670    #[test]
671    fn test_get_logs() {
672        // Create a Receipts object with a vector of receipt vectors
673        let receipts = vec![vec![reth_ethereum_primitives::Receipt {
674            tx_type: TxType::Legacy,
675            cumulative_gas_used: 46913,
676            logs: vec![Log::<LogData>::default()],
677            success: true,
678        }]];
679
680        // Define the first block number
681        let first_block = 123;
682
683        // Create a ExecutionOutcome object with the created bundle, receipts, requests, and
684        // first_block
685        let exec_res = ExecutionOutcome {
686            bundle: Default::default(),
687            receipts,
688            requests: vec![],
689            first_block,
690        };
691
692        // Get logs for block number 123
693        let logs: Vec<&Log> = exec_res.logs(123).unwrap().collect();
694
695        // Assert that the logs match the expected logs
696        assert_eq!(logs, vec![&Log::<LogData>::default()]);
697    }
698
699    #[test]
700    fn test_receipts_by_block() {
701        // Create a Receipts object with a vector of receipt vectors
702        let receipts = vec![vec![Some(reth_ethereum_primitives::Receipt {
703            tx_type: TxType::Legacy,
704            cumulative_gas_used: 46913,
705            logs: vec![Log::<LogData>::default()],
706            success: true,
707        })]];
708
709        // Define the first block number
710        let first_block = 123;
711
712        // Create a ExecutionOutcome object with the created bundle, receipts, requests, and
713        // first_block
714        let exec_res = ExecutionOutcome {
715            bundle: Default::default(), // Default value for bundle
716            receipts,                   // Include the created receipts
717            requests: vec![],           // Empty vector for requests
718            first_block,                // Set the first block number
719        };
720
721        // Get receipts for block number 123 and convert the result into a vector
722        let receipts_by_block: Vec<_> = exec_res.receipts_by_block(123).iter().collect();
723
724        // Assert that the receipts for block number 123 match the expected receipts
725        assert_eq!(
726            receipts_by_block,
727            vec![&Some(reth_ethereum_primitives::Receipt {
728                tx_type: TxType::Legacy,
729                cumulative_gas_used: 46913,
730                logs: vec![Log::<LogData>::default()],
731                success: true,
732            })]
733        );
734    }
735
736    #[test]
737    fn test_receipts_len() {
738        // Create a Receipts object with a vector of receipt vectors
739        let receipts = vec![vec![Some(reth_ethereum_primitives::Receipt {
740            tx_type: TxType::Legacy,
741            cumulative_gas_used: 46913,
742            logs: vec![Log::<LogData>::default()],
743            success: true,
744        })]];
745
746        // Create an empty Receipts object
747        let receipts_empty = vec![];
748
749        // Define the first block number
750        let first_block = 123;
751
752        // Create a ExecutionOutcome object with the created bundle, receipts, requests, and
753        // first_block
754        let exec_res = ExecutionOutcome {
755            bundle: Default::default(), // Default value for bundle
756            receipts,                   // Include the created receipts
757            requests: vec![],           // Empty vector for requests
758            first_block,                // Set the first block number
759        };
760
761        // Assert that the length of receipts in exec_res is 1
762        assert_eq!(exec_res.len(), 1);
763
764        // Assert that exec_res is not empty
765        assert!(!exec_res.is_empty());
766
767        // Create a ExecutionOutcome object with an empty Receipts object
768        let exec_res_empty_receipts: ExecutionOutcome = ExecutionOutcome {
769            bundle: Default::default(), // Default value for bundle
770            receipts: receipts_empty,   // Include the empty receipts
771            requests: vec![],           // Empty vector for requests
772            first_block,                // Set the first block number
773        };
774
775        // Assert that the length of receipts in exec_res_empty_receipts is 0
776        assert_eq!(exec_res_empty_receipts.len(), 0);
777
778        // Assert that exec_res_empty_receipts is empty
779        assert!(exec_res_empty_receipts.is_empty());
780    }
781
782    #[test]
783    fn test_revert_to() {
784        // Create a random receipt object
785        let receipt = reth_ethereum_primitives::Receipt {
786            tx_type: TxType::Legacy,
787            cumulative_gas_used: 46913,
788            logs: vec![],
789            success: true,
790        };
791
792        // Create a Receipts object with a vector of receipt vectors
793        let receipts = vec![vec![Some(receipt.clone())], vec![Some(receipt.clone())]];
794
795        // Define the first block number
796        let first_block = 123;
797
798        // Create a request.
799        let request = bytes!("deadbeef");
800
801        // Create a vector of Requests containing the request.
802        let requests =
803            vec![Requests::new(vec![request.clone()]), Requests::new(vec![request.clone()])];
804
805        // Create a ExecutionOutcome object with the created bundle, receipts, requests, and
806        // first_block
807        let mut exec_res =
808            ExecutionOutcome { bundle: Default::default(), receipts, requests, first_block };
809
810        // Assert that the revert_to method returns true when reverting to the initial block number.
811        assert!(exec_res.revert_to(123));
812
813        // Assert that the receipts are properly cut after reverting to the initial block number.
814        assert_eq!(exec_res.receipts, vec![vec![Some(receipt)]]);
815
816        // Assert that the requests are properly cut after reverting to the initial block number.
817        assert_eq!(exec_res.requests, vec![Requests::new(vec![request])]);
818
819        // Assert that the revert_to method returns false when attempting to revert to a block
820        // number greater than the initial block number.
821        assert!(!exec_res.revert_to(133));
822
823        // Assert that the revert_to method returns false when attempting to revert to a block
824        // number less than the initial block number.
825        assert!(!exec_res.revert_to(10));
826    }
827
828    #[test]
829    fn test_extend_execution_outcome() {
830        // Create a Receipt object with specific attributes.
831        let receipt = reth_ethereum_primitives::Receipt {
832            tx_type: TxType::Legacy,
833            cumulative_gas_used: 46913,
834            logs: vec![],
835            success: true,
836        };
837
838        // Create a Receipts object containing the receipt.
839        let receipts = vec![vec![Some(receipt.clone())]];
840
841        // Create a request.
842        let request = bytes!("deadbeef");
843
844        // Create a vector of Requests containing the request.
845        let requests = vec![Requests::new(vec![request.clone()])];
846
847        // Define the initial block number.
848        let first_block = 123;
849
850        // Create an ExecutionOutcome object.
851        let mut exec_res =
852            ExecutionOutcome { bundle: Default::default(), receipts, requests, first_block };
853
854        // Extend the ExecutionOutcome object by itself.
855        exec_res.extend(exec_res.clone());
856
857        // Assert the extended ExecutionOutcome matches the expected outcome.
858        assert_eq!(
859            exec_res,
860            ExecutionOutcome {
861                bundle: Default::default(),
862                receipts: vec![vec![Some(receipt.clone())], vec![Some(receipt)]],
863                requests: vec![Requests::new(vec![request.clone()]), Requests::new(vec![request])],
864                first_block: 123,
865            }
866        );
867    }
868
869    #[test]
870    fn test_split_at_execution_outcome() {
871        // Create a random receipt object
872        let receipt = reth_ethereum_primitives::Receipt {
873            tx_type: TxType::Legacy,
874            cumulative_gas_used: 46913,
875            logs: vec![],
876            success: true,
877        };
878
879        // Create a Receipts object with a vector of receipt vectors
880        let receipts = vec![
881            vec![Some(receipt.clone())],
882            vec![Some(receipt.clone())],
883            vec![Some(receipt.clone())],
884        ];
885
886        // Define the first block number
887        let first_block = 123;
888
889        // Create a request.
890        let request = bytes!("deadbeef");
891
892        // Create a vector of Requests containing the request.
893        let requests = vec![
894            Requests::new(vec![request.clone()]),
895            Requests::new(vec![request.clone()]),
896            Requests::new(vec![request.clone()]),
897        ];
898
899        // Create a ExecutionOutcome object with the created bundle, receipts, requests, and
900        // first_block
901        let exec_res =
902            ExecutionOutcome { bundle: Default::default(), receipts, requests, first_block };
903
904        // Split the ExecutionOutcome at block number 124
905        let result = exec_res.clone().split_at(124);
906
907        // Define the expected lower ExecutionOutcome after splitting
908        let lower_execution_outcome = ExecutionOutcome {
909            bundle: Default::default(),
910            receipts: vec![vec![Some(receipt.clone())]],
911            requests: vec![Requests::new(vec![request.clone()])],
912            first_block,
913        };
914
915        // Define the expected higher ExecutionOutcome after splitting
916        let higher_execution_outcome = ExecutionOutcome {
917            bundle: Default::default(),
918            receipts: vec![vec![Some(receipt.clone())], vec![Some(receipt)]],
919            requests: vec![Requests::new(vec![request.clone()]), Requests::new(vec![request])],
920            first_block: 124,
921        };
922
923        // Assert that the split result matches the expected lower and higher outcomes
924        assert_eq!(result.0, Some(lower_execution_outcome));
925        assert_eq!(result.1, higher_execution_outcome);
926
927        // Assert that splitting at the first block number returns None for the lower outcome
928        assert_eq!(exec_res.clone().split_at(123), (None, exec_res));
929    }
930
931    #[test]
932    fn test_changed_accounts() {
933        // Set up some sample accounts
934        let address1 = Address::random();
935        let address2 = Address::random();
936        let address3 = Address::random();
937
938        // Set up account info with some changes
939        let account_info1 = AccountInfo {
940            nonce: 1,
941            balance: U256::from(100),
942            code_hash: B256::ZERO,
943            code: None,
944            account_id: None,
945        };
946        let account_info2 = AccountInfo {
947            nonce: 2,
948            balance: U256::from(200),
949            code_hash: B256::ZERO,
950            code: None,
951            account_id: None,
952        };
953
954        // Set up the bundle state with these accounts
955        let mut bundle_state = BundleState::default();
956        bundle_state.state.insert(
957            address1,
958            BundleAccount {
959                info: Some(account_info1),
960                storage: Default::default(),
961                original_info: Default::default(),
962                status: Default::default(),
963            },
964        );
965        bundle_state.state.insert(
966            address2,
967            BundleAccount {
968                info: Some(account_info2),
969                storage: Default::default(),
970                original_info: Default::default(),
971                status: Default::default(),
972            },
973        );
974
975        // Unchanged account
976        bundle_state.state.insert(
977            address3,
978            BundleAccount {
979                info: None,
980                storage: Default::default(),
981                original_info: Default::default(),
982                status: Default::default(),
983            },
984        );
985
986        let execution_outcome: ExecutionOutcome = ExecutionOutcome {
987            bundle: bundle_state,
988            receipts: Default::default(),
989            first_block: 0,
990            requests: vec![],
991        };
992
993        // Get the changed accounts
994        let changed_accounts: Vec<ChangedAccount> = execution_outcome.changed_accounts().collect();
995
996        // Assert that the changed accounts match the expected ones
997        assert_eq!(changed_accounts.len(), 2);
998
999        assert!(changed_accounts.contains(&ChangedAccount {
1000            address: address1,
1001            nonce: 1,
1002            balance: U256::from(100)
1003        }));
1004
1005        assert!(changed_accounts.contains(&ChangedAccount {
1006            address: address2,
1007            nonce: 2,
1008            balance: U256::from(200)
1009        }));
1010    }
1011}