revm/
frame.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crate::{
    interpreter::Interpreter,
    primitives::{Address, Output},
    JournalCheckpoint,
};
use core::ops::Range;
use revm_interpreter::{CallOutcome, CreateOutcome, Gas, InstructionResult, InterpreterResult};
use std::boxed::Box;

/// Call CallStackFrame.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CallFrame {
    /// Call frame has return memory range where output will be stored.
    pub return_memory_range: Range<usize>,
    /// Frame data.
    pub frame_data: FrameData,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateFrame {
    /// Create frame has a created address.
    pub created_address: Address,
    /// Frame data.
    pub frame_data: FrameData,
}

/// Eof Create Frame.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EOFCreateFrame {
    pub created_address: Address,
    pub frame_data: FrameData,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FrameData {
    /// Journal checkpoint.
    pub checkpoint: JournalCheckpoint,
    /// Interpreter.
    pub interpreter: Interpreter,
}

/// Call stack frame.
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Frame {
    Call(Box<CallFrame>),
    Create(Box<CreateFrame>),
    EOFCreate(Box<EOFCreateFrame>),
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug)]
pub enum FrameResult {
    Call(CallOutcome),
    Create(CreateOutcome),
    EOFCreate(CreateOutcome),
}

impl FrameResult {
    /// Casts frame result to interpreter result.
    #[inline]
    pub fn into_interpreter_result(self) -> InterpreterResult {
        match self {
            FrameResult::Call(outcome) => outcome.result,
            FrameResult::Create(outcome) => outcome.result,
            FrameResult::EOFCreate(outcome) => outcome.result,
        }
    }

    /// Returns execution output.
    #[inline]
    pub fn output(&self) -> Output {
        match self {
            FrameResult::Call(outcome) => Output::Call(outcome.result.output.clone()),
            FrameResult::Create(outcome) => {
                Output::Create(outcome.result.output.clone(), outcome.address)
            }
            FrameResult::EOFCreate(outcome) => {
                Output::Create(outcome.result.output.clone(), outcome.address)
            }
        }
    }

    /// Returns reference to gas.
    #[inline]
    pub fn gas(&self) -> &Gas {
        match self {
            FrameResult::Call(outcome) => &outcome.result.gas,
            FrameResult::Create(outcome) => &outcome.result.gas,
            FrameResult::EOFCreate(outcome) => &outcome.result.gas,
        }
    }

    /// Returns mutable reference to interpreter result.
    #[inline]
    pub fn gas_mut(&mut self) -> &mut Gas {
        match self {
            FrameResult::Call(outcome) => &mut outcome.result.gas,
            FrameResult::Create(outcome) => &mut outcome.result.gas,
            FrameResult::EOFCreate(outcome) => &mut outcome.result.gas,
        }
    }

    /// Returns reference to interpreter result.
    #[inline]
    pub fn interpreter_result(&self) -> &InterpreterResult {
        match self {
            FrameResult::Call(outcome) => &outcome.result,
            FrameResult::Create(outcome) => &outcome.result,
            FrameResult::EOFCreate(outcome) => &outcome.result,
        }
    }

    /// Returns mutable reference to interpreter result.
    #[inline]
    pub fn interpreter_result_mut(&mut self) -> &InterpreterResult {
        match self {
            FrameResult::Call(outcome) => &mut outcome.result,
            FrameResult::Create(outcome) => &mut outcome.result,
            FrameResult::EOFCreate(outcome) => &mut outcome.result,
        }
    }

    /// Return Instruction result.
    #[inline]
    pub fn instruction_result(&self) -> InstructionResult {
        self.interpreter_result().result
    }
}

/// Contains either a frame or a result.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug)]
pub enum FrameOrResult {
    /// Boxed call or create frame.
    Frame(Frame),
    /// Call or create result.
    Result(FrameResult),
}

impl Frame {
    pub fn new_create(
        created_address: Address,
        checkpoint: JournalCheckpoint,
        interpreter: Interpreter,
    ) -> Self {
        Frame::Create(Box::new(CreateFrame {
            created_address,
            frame_data: FrameData {
                checkpoint,
                interpreter,
            },
        }))
    }

    pub fn new_call(
        return_memory_range: Range<usize>,
        checkpoint: JournalCheckpoint,
        interpreter: Interpreter,
    ) -> Self {
        Frame::Call(Box::new(CallFrame {
            return_memory_range,
            frame_data: FrameData {
                checkpoint,
                interpreter,
            },
        }))
    }

    /// Returns true if frame is call frame.
    pub fn is_call(&self) -> bool {
        matches!(self, Frame::Call { .. })
    }

    /// Returns true if frame is create frame.
    pub fn is_create(&self) -> bool {
        matches!(self, Frame::Create { .. })
    }

    /// Returns created address if frame is create otherwise returns None.
    pub fn created_address(&self) -> Option<Address> {
        match self {
            Frame::Create(create_frame) => Some(create_frame.created_address),
            _ => None,
        }
    }

    /// Takes frame and returns frame data.
    pub fn into_frame_data(self) -> FrameData {
        match self {
            Frame::Call(call_frame) => call_frame.frame_data,
            Frame::Create(create_frame) => create_frame.frame_data,
            Frame::EOFCreate(eof_create_frame) => eof_create_frame.frame_data,
        }
    }

    /// Returns reference to frame data.
    pub fn frame_data(&self) -> &FrameData {
        match self {
            Self::Call(call_frame) => &call_frame.frame_data,
            Self::Create(create_frame) => &create_frame.frame_data,
            Self::EOFCreate(eof_create_frame) => &eof_create_frame.frame_data,
        }
    }

    /// Returns mutable reference to frame data.
    pub fn frame_data_mut(&mut self) -> &mut FrameData {
        match self {
            Self::Call(call_frame) => &mut call_frame.frame_data,
            Self::Create(create_frame) => &mut create_frame.frame_data,
            Self::EOFCreate(eof_create_frame) => &mut eof_create_frame.frame_data,
        }
    }

    /// Returns a reference to the interpreter.
    pub fn interpreter(&self) -> &Interpreter {
        &self.frame_data().interpreter
    }

    /// Returns a mutable reference to the interpreter.
    pub fn interpreter_mut(&mut self) -> &mut Interpreter {
        &mut self.frame_data_mut().interpreter
    }
}

impl FrameOrResult {
    /// Creates new create frame.
    pub fn new_create_frame(
        created_address: Address,
        checkpoint: JournalCheckpoint,
        interpreter: Interpreter,
    ) -> Self {
        Self::Frame(Frame::new_create(created_address, checkpoint, interpreter))
    }

    pub fn new_eofcreate_frame(
        created_address: Address,
        checkpoint: JournalCheckpoint,
        interpreter: Interpreter,
    ) -> Self {
        Self::Frame(Frame::EOFCreate(Box::new(EOFCreateFrame {
            created_address,
            frame_data: FrameData {
                checkpoint,
                interpreter,
            },
        })))
    }

    /// Creates new call frame.
    pub fn new_call_frame(
        return_memory_range: Range<usize>,
        checkpoint: JournalCheckpoint,
        interpreter: Interpreter,
    ) -> Self {
        Self::Frame(Frame::new_call(
            return_memory_range,
            checkpoint,
            interpreter,
        ))
    }

    /// Creates new create result.
    pub fn new_create_result(
        interpreter_result: InterpreterResult,
        address: Option<Address>,
    ) -> Self {
        FrameOrResult::Result(FrameResult::Create(CreateOutcome {
            result: interpreter_result,
            address,
        }))
    }

    pub fn new_eofcreate_result(
        interpreter_result: InterpreterResult,
        address: Option<Address>,
    ) -> Self {
        FrameOrResult::Result(FrameResult::EOFCreate(CreateOutcome {
            result: interpreter_result,
            address,
        }))
    }

    pub fn new_call_result(
        interpreter_result: InterpreterResult,
        memory_offset: Range<usize>,
    ) -> Self {
        FrameOrResult::Result(FrameResult::Call(CallOutcome {
            result: interpreter_result,
            memory_offset,
        }))
    }
}