revm_interpreter/interpreter_action/
call_outcome.rs

1use crate::{Gas, InstructionResult, InterpreterResult};
2use core::ops::Range;
3use primitives::{Bytes, Log};
4use std::vec::Vec;
5
6/// Represents the outcome of a call operation in a virtual machine.
7///
8/// This struct encapsulates the result of executing an instruction by an interpreter, including
9/// the result itself, gas usage information, and the memory offset where output data is stored.
10///
11/// # Fields
12///
13/// * `result` - The result of the interpreter's execution, including output data and gas usage.
14/// * `memory_offset` - The range in memory where the output data is located.
15#[derive(Clone, Debug, PartialEq, Eq)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17pub struct CallOutcome {
18    /// The result of the interpreter's execution, including output data and gas usage
19    pub result: InterpreterResult,
20    /// The range in memory where the output data is located
21    pub memory_offset: Range<usize>,
22    /// Flag to indicate if the call is precompile call.
23    /// Used by inspector so it can copy the logs for Inspector::logs call.
24    pub was_precompile_called: bool,
25    /// Precompile call logs. Needs as revert/halt would delete them from Journal.
26    /// So they can't be accessed by inspector.
27    pub precompile_call_logs: Vec<Log>,
28}
29
30impl CallOutcome {
31    /// Constructs a new [`CallOutcome`].
32    ///
33    /// Creates an instance of [`CallOutcome`] with the given interpreter result and memory offset.
34    ///
35    /// # Arguments
36    ///
37    /// * `result` - The result of the interpreter's execution.
38    /// * `memory_offset` - The range in memory indicating where the output data is stored.
39    pub fn new(result: InterpreterResult, memory_offset: Range<usize>) -> Self {
40        Self {
41            result,
42            memory_offset,
43            was_precompile_called: false,
44            precompile_call_logs: Vec::new(),
45        }
46    }
47
48    /// Constructs a new [`CallOutcome`] for an out-of-gas error.
49    ///
50    /// # Arguments
51    ///
52    /// * `gas_limit` - The gas limit that was exceeded.
53    /// * `memory_offset` - The range in memory indicating where the output data is stored.
54    pub fn new_oog(gas_limit: u64, memory_offset: Range<usize>) -> Self {
55        Self::new(InterpreterResult::new_oog(gas_limit), memory_offset)
56    }
57
58    /// Returns a reference to the instruction result.
59    ///
60    /// Provides access to the result of the executed instruction.
61    ///
62    /// # Returns
63    ///
64    /// A reference to the [`InstructionResult`].
65    pub fn instruction_result(&self) -> &InstructionResult {
66        &self.result.result
67    }
68
69    /// Returns the gas usage information.
70    ///
71    /// Provides access to the gas usage details of the executed instruction.
72    ///
73    /// # Returns
74    ///
75    /// An instance of [`Gas`] representing the gas usage.
76    pub fn gas(&self) -> Gas {
77        self.result.gas
78    }
79
80    /// Returns a reference to the output data.
81    ///
82    /// Provides access to the output data generated by the executed instruction.
83    ///
84    /// # Returns
85    ///
86    /// A reference to the output data as [`Bytes`].
87    pub fn output(&self) -> &Bytes {
88        &self.result.output
89    }
90
91    /// Returns the start position of the memory offset.
92    ///
93    /// Provides the starting index of the memory range where the output data is stored.
94    ///
95    /// # Returns
96    ///
97    /// The starting index of the memory offset as [`usize`].
98    pub fn memory_start(&self) -> usize {
99        self.memory_offset.start
100    }
101
102    /// Returns the length of the memory range.
103    ///
104    /// Provides the length of the memory range where the output data is stored.
105    ///
106    /// # Returns
107    ///
108    /// The length of the memory range as [`usize`].
109    pub fn memory_length(&self) -> usize {
110        self.memory_offset.len()
111    }
112}