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    /// Returns a reference to the instruction result.
49    ///
50    /// Provides access to the result of the executed instruction.
51    ///
52    /// # Returns
53    ///
54    /// A reference to the [`InstructionResult`].
55    pub fn instruction_result(&self) -> &InstructionResult {
56        &self.result.result
57    }
58
59    /// Returns the gas usage information.
60    ///
61    /// Provides access to the gas usage details of the executed instruction.
62    ///
63    /// # Returns
64    ///
65    /// An instance of [`Gas`] representing the gas usage.
66    pub fn gas(&self) -> Gas {
67        self.result.gas
68    }
69
70    /// Returns a reference to the output data.
71    ///
72    /// Provides access to the output data generated by the executed instruction.
73    ///
74    /// # Returns
75    ///
76    /// A reference to the output data as [`Bytes`].
77    pub fn output(&self) -> &Bytes {
78        &self.result.output
79    }
80
81    /// Returns the start position of the memory offset.
82    ///
83    /// Provides the starting index of the memory range where the output data is stored.
84    ///
85    /// # Returns
86    ///
87    /// The starting index of the memory offset as [`usize`].
88    pub fn memory_start(&self) -> usize {
89        self.memory_offset.start
90    }
91
92    /// Returns the length of the memory range.
93    ///
94    /// Provides the length of the memory range where the output data is stored.
95    ///
96    /// # Returns
97    ///
98    /// The length of the memory range as [`usize`].
99    pub fn memory_length(&self) -> usize {
100        self.memory_offset.len()
101    }
102}