Skip to main content

revm_interpreter/interpreter_action/
create_outcome.rs

1use crate::{Gas, InstructionResult, InterpreterResult};
2use primitives::{Address, Bytes};
3
4/// Represents the outcome of a create operation in an interpreter.
5///
6/// This struct holds the result of the operation along with an optional address.
7///
8/// It provides methods to determine the next action based on the result of the operation.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct CreateOutcome {
12    /// The result of the interpreter operation
13    pub result: InterpreterResult,
14    /// An optional address associated with the create operation
15    pub address: Option<Address>,
16    /// EIP-8037: whether the created address was already alive (existing,
17    /// non-empty) before this CREATE. When the create succeeds at such an
18    /// address no new account leaf is created, so the upfront `create_state_gas`
19    /// is refunded to the parent's reservoir.
20    pub target_was_alive: bool,
21}
22
23impl CreateOutcome {
24    /// Constructs a new [`CreateOutcome`].
25    ///
26    /// # Arguments
27    ///
28    /// * `result` - An [`InterpreterResult`] representing the result of the interpreter operation.
29    /// * `address` - An optional [`Address`] associated with the create operation.
30    ///
31    /// # Returns
32    ///
33    /// A new [`CreateOutcome`] instance.
34    pub const fn new(result: InterpreterResult, address: Option<Address>) -> Self {
35        Self {
36            result,
37            address,
38            target_was_alive: false,
39        }
40    }
41
42    /// Constructs a new [`CreateOutcome`] for an out-of-gas error.
43    ///
44    /// # Arguments
45    ///
46    /// * `gas_limit` - The gas limit that was exceeded.
47    ///
48    /// # Returns
49    ///
50    /// A new [`CreateOutcome`] instance with no address.
51    pub fn new_oog(gas_limit: u64, reservoir: u64) -> Self {
52        Self::new(InterpreterResult::new_oog(gas_limit, reservoir), None)
53    }
54
55    /// Retrieves a reference to the [`InstructionResult`] from the [`InterpreterResult`].
56    ///
57    /// This method provides access to the [`InstructionResult`] which represents the
58    /// outcome of the instruction execution.
59    ///
60    /// It encapsulates the result information such as whether the instruction was
61    /// executed successfully, resulted in a revert, or encountered a fatal error.
62    ///
63    /// # Returns
64    ///
65    /// A reference to the [`InstructionResult`].
66    pub const fn instruction_result(&self) -> &InstructionResult {
67        &self.result.result
68    }
69
70    /// Retrieves a reference to the output bytes from the [`InterpreterResult`].
71    ///
72    /// This method returns the output of the interpreted operation.
73    ///
74    /// The output is typically used when the operation successfully completes and
75    /// returns data.
76    ///
77    /// # Returns
78    ///
79    /// A reference to the output [`Bytes`].
80    pub const fn output(&self) -> &Bytes {
81        &self.result.output
82    }
83
84    /// Retrieves a reference to the [`Gas`] details from the [`InterpreterResult`].
85    ///
86    /// This method provides access to the gas details of the operation, which includes
87    /// information about gas used, remaining, and refunded.
88    ///
89    /// It is essential for understanding the gas consumption of the operation.
90    ///
91    /// # Returns
92    ///
93    /// A reference to the [`Gas`] details.
94    pub const fn gas(&self) -> &Gas {
95        &self.result.gas
96    }
97}