revm_interpreter/
interpreter_action.rs

1mod call_inputs;
2mod call_outcome;
3mod create_inputs;
4mod create_outcome;
5mod eof_create_inputs;
6
7pub use call_inputs::{CallInput, CallInputs, CallScheme, CallValue};
8pub use call_outcome::CallOutcome;
9pub use create_inputs::CreateInputs;
10pub use create_outcome::CreateOutcome;
11pub use eof_create_inputs::{EOFCreateInputs, EOFCreateKind};
12use primitives::Bytes;
13
14use crate::{Gas, InstructionResult, InterpreterResult};
15use std::boxed::Box;
16
17#[derive(Clone, Debug, PartialEq, Eq)]
18#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
19pub enum FrameInput {
20    /// `CALL`, `CALLCODE`, `DELEGATECALL`, `STATICCALL`
21    /// or EOF `EXTCALL`, `EXTDELEGATECALL`, `EXTSTATICCALL` instruction called.
22    Call(Box<CallInputs>),
23    /// `CREATE` or `CREATE2` instruction called.
24    Create(Box<CreateInputs>),
25    /// EOF `CREATE` instruction called.
26    EOFCreate(Box<EOFCreateInputs>),
27}
28
29impl AsMut<Self> for FrameInput {
30    fn as_mut(&mut self) -> &mut Self {
31        self
32    }
33}
34
35#[derive(Clone, Debug, PartialEq, Eq)]
36#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37pub enum InterpreterAction {
38    /// New frame
39    NewFrame(FrameInput),
40    /// Interpreter finished execution.
41    Return(InterpreterResult),
42}
43
44impl InterpreterAction {
45    /// Returns `true` if action is call.
46    pub fn is_call(&self) -> bool {
47        matches!(self, InterpreterAction::NewFrame(FrameInput::Call(..)))
48    }
49
50    /// Returns `true` if action is create.
51    pub fn is_create(&self) -> bool {
52        matches!(self, InterpreterAction::NewFrame(FrameInput::Create(..)))
53    }
54
55    /// Returns `true` if action is return.
56    pub fn is_return(&self) -> bool {
57        matches!(self, InterpreterAction::Return { .. })
58    }
59
60    /// Returns [`InterpreterResult`] if action is return.
61    ///
62    /// Else it returns [None].
63    pub fn into_result_return(self) -> Option<InterpreterResult> {
64        match self {
65            InterpreterAction::Return(result) => Some(result),
66            _ => None,
67        }
68    }
69
70    /// Returns [`InstructionResult`] if action is return.
71    ///
72    /// Else it returns [None].
73    pub fn instruction_result(&self) -> Option<InstructionResult> {
74        match self {
75            InterpreterAction::Return(result) => Some(result.result),
76            _ => None,
77        }
78    }
79
80    /// Create new frame action with the given frame input.
81    pub fn new_frame(frame_input: FrameInput) -> Self {
82        Self::NewFrame(frame_input)
83    }
84
85    /// Create new halt action with the given result and gas.
86    pub fn new_halt(result: InstructionResult, gas: Gas) -> Self {
87        Self::Return(InterpreterResult::new(result, Bytes::new(), gas))
88    }
89
90    /// Create new return action with the given result, output and gas.
91    pub fn new_return(result: InstructionResult, output: Bytes, gas: Gas) -> Self {
92        Self::Return(InterpreterResult::new(result, output, gas))
93    }
94
95    /// Create new stop action.
96    pub fn new_stop() -> Self {
97        Self::Return(InterpreterResult::new(
98            InstructionResult::Stop,
99            Bytes::new(),
100            Gas::new(0),
101        ))
102    }
103}