revm_handler/
frame_data.rs

1use context_interface::result::Output;
2use core::ops::Range;
3use interpreter::{CallOutcome, CreateOutcome, Gas, InstructionResult, InterpreterResult};
4use primitives::Address;
5
6/// Call Frame
7#[derive(Debug, Clone)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct CallFrame {
10    /// Call frame has return memory range where output will be stored.
11    pub return_memory_range: Range<usize>,
12}
13
14/// Create Frame
15#[derive(Debug, Clone)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17pub struct CreateFrame {
18    /// Create frame has a created address.
19    pub created_address: Address,
20}
21
22/// Frame Data
23///
24/// [`FrameData`] bundles different types of frames.
25#[derive(Debug, Clone)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27pub enum FrameData {
28    /// Call frame data.
29    Call(CallFrame),
30    /// Create frame data.
31    Create(CreateFrame),
32}
33
34/// Frame Result
35#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
36#[derive(Debug, Clone)]
37pub enum FrameResult {
38    /// Call frame result.
39    Call(CallOutcome),
40    /// Create frame result.
41    Create(CreateOutcome),
42}
43
44impl FrameResult {
45    /// Casts frame result to interpreter result.
46    #[inline]
47    pub fn into_interpreter_result(self) -> InterpreterResult {
48        match self {
49            FrameResult::Call(outcome) => outcome.result,
50            FrameResult::Create(outcome) => outcome.result,
51        }
52    }
53
54    /// Returns execution output.
55    #[inline]
56    pub fn output(&self) -> Output {
57        match self {
58            FrameResult::Call(outcome) => Output::Call(outcome.result.output.clone()),
59            FrameResult::Create(outcome) => {
60                Output::Create(outcome.result.output.clone(), outcome.address)
61            }
62        }
63    }
64
65    /// Returns reference to gas.
66    #[inline]
67    pub fn gas(&self) -> &Gas {
68        match self {
69            FrameResult::Call(outcome) => &outcome.result.gas,
70            FrameResult::Create(outcome) => &outcome.result.gas,
71        }
72    }
73
74    /// Returns mutable reference to interpreter result.
75    #[inline]
76    pub fn gas_mut(&mut self) -> &mut Gas {
77        match self {
78            FrameResult::Call(outcome) => &mut outcome.result.gas,
79            FrameResult::Create(outcome) => &mut outcome.result.gas,
80        }
81    }
82
83    /// Returns reference to interpreter result.
84    #[inline]
85    pub fn interpreter_result(&self) -> &InterpreterResult {
86        match self {
87            FrameResult::Call(outcome) => &outcome.result,
88            FrameResult::Create(outcome) => &outcome.result,
89        }
90    }
91
92    /// Returns mutable reference to interpreter result.
93    #[inline]
94    pub fn interpreter_result_mut(&mut self) -> &InterpreterResult {
95        match self {
96            FrameResult::Call(outcome) => &mut outcome.result,
97            FrameResult::Create(outcome) => &mut outcome.result,
98        }
99    }
100
101    /// Return Instruction result.
102    #[inline]
103    pub fn instruction_result(&self) -> InstructionResult {
104        self.interpreter_result().result
105    }
106}
107
108impl FrameData {
109    /// Creates a new create frame data.
110    pub fn new_create(created_address: Address) -> Self {
111        Self::Create(CreateFrame { created_address })
112    }
113
114    /// Creates a new call frame data.
115    pub fn new_call(return_memory_range: Range<usize>) -> Self {
116        Self::Call(CallFrame {
117            return_memory_range,
118        })
119    }
120
121    /// Returns true if frame is call frame.
122    pub fn is_call(&self) -> bool {
123        matches!(self, Self::Call { .. })
124    }
125
126    /// Returns true if frame is create frame.
127    pub fn is_create(&self) -> bool {
128        matches!(self, Self::Create { .. })
129    }
130
131    /// Returns created address if frame is create otherwise returns None.
132    pub fn created_address(&self) -> Option<Address> {
133        match self {
134            Self::Create(create_frame) => Some(create_frame.created_address),
135            _ => None,
136        }
137    }
138}