Skip to main content

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 const 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>, reservoir: u64) -> Self {
55        Self::new(
56            InterpreterResult::new_oog(gas_limit, reservoir),
57            memory_offset,
58        )
59    }
60
61    /// Returns a reference to the instruction result.
62    ///
63    /// Provides access to the result of the executed instruction.
64    ///
65    /// # Returns
66    ///
67    /// A reference to the [`InstructionResult`].
68    pub const fn instruction_result(&self) -> &InstructionResult {
69        &self.result.result
70    }
71
72    /// Returns the gas usage information.
73    ///
74    /// Provides access to the gas usage details of the executed instruction.
75    ///
76    /// # Returns
77    ///
78    /// An instance of [`Gas`] representing the gas usage.
79    pub const fn gas(&self) -> Gas {
80        self.result.gas
81    }
82
83    /// Returns a reference to the output data.
84    ///
85    /// Provides access to the output data generated by the executed instruction.
86    ///
87    /// # Returns
88    ///
89    /// A reference to the output data as [`Bytes`].
90    pub const fn output(&self) -> &Bytes {
91        &self.result.output
92    }
93
94    /// Returns the start position of the memory offset.
95    ///
96    /// Provides the starting index of the memory range where the output data is stored.
97    ///
98    /// # Returns
99    ///
100    /// The starting index of the memory offset as [`usize`].
101    pub const fn memory_start(&self) -> usize {
102        self.memory_offset.start
103    }
104
105    /// Returns the length of the memory range.
106    ///
107    /// Provides the length of the memory range where the output data is stored.
108    ///
109    /// # Returns
110    ///
111    /// The length of the memory range as [`usize`].
112    pub fn memory_length(&self) -> usize {
113        self.memory_offset.len()
114    }
115}