Skip to content

Commit 72c7c40

Browse files
authored
feat(EOF): Add EOF to inspector handle register (#1469)
* feat(EOF): Add EOF to inspector handle register * add comments, remove TODO
1 parent c844f15 commit 72c7c40

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

crates/revm/src/inspector.rs

+4
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ pub trait Inspector<DB: Database> {
135135
outcome
136136
}
137137

138+
/// Called when EOF creating is called.
139+
///
140+
/// This can happen from create TX or from EOFCREATE opcode.
138141
fn eofcreate(
139142
&mut self,
140143
context: &mut EvmContext<DB>,
@@ -145,6 +148,7 @@ pub trait Inspector<DB: Database> {
145148
None
146149
}
147150

151+
/// Called when eof creating has ended.
148152
fn eofcreate_end(
149153
&mut self,
150154
context: &mut EvmContext<DB>,

crates/revm/src/inspector/handler_register.rs

+41-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,47 @@ pub fn inspector_handle_register<DB: Database, EXT: GetInspector<DB>>(
161161
},
162162
);
163163

164-
// TODO(EOF) EOF create call.
164+
// Calls inspector `eofcreate` and `initialize_interp` functions. Queues the inputs for the `eofcreate_end`` function.
165+
// Calls the old handler, and in case of inspector returning outcome,
166+
// returns the outcome without executing eofcreate.
167+
let eofcreate_input_stack_inner = eofcreate_input_stack.clone();
168+
let old_handle = handler.execution.eofcreate.clone();
169+
handler.execution.eofcreate = Arc::new(
170+
move |ctx, mut inputs| -> Result<FrameOrResult, EVMError<DB::Error>> {
171+
// Call inspector to change input or return outcome.
172+
let outcome = ctx
173+
.external
174+
.get_inspector()
175+
.eofcreate(&mut ctx.evm, &mut inputs);
176+
eofcreate_input_stack_inner
177+
.borrow_mut()
178+
.push(inputs.clone());
179+
if let Some(outcome) = outcome {
180+
return Ok(FrameOrResult::Result(FrameResult::EOFCreate(outcome)));
181+
}
182+
183+
let mut frame_or_result = old_handle(ctx, inputs);
184+
if let Ok(FrameOrResult::Frame(frame)) = &mut frame_or_result {
185+
ctx.external
186+
.get_inspector()
187+
.initialize_interp(frame.interpreter_mut(), &mut ctx.evm)
188+
}
189+
frame_or_result
190+
},
191+
);
192+
193+
// Pops eofcreate input from the stack and calls inspector `eofcreate_end` function.
194+
// preserve the old handler and calls it with the outcome.
195+
let eofcreate_input_stack_inner = eofcreate_input_stack.clone();
196+
let old_handle = handler.execution.insert_eofcreate_outcome.clone();
197+
handler.execution.insert_eofcreate_outcome = Arc::new(move |ctx, frame, mut outcome| {
198+
let create_inputs = eofcreate_input_stack_inner.borrow_mut().pop().unwrap();
199+
outcome = ctx
200+
.external
201+
.get_inspector()
202+
.eofcreate_end(&mut ctx.evm, &create_inputs, outcome);
203+
old_handle(ctx, frame, outcome)
204+
});
165205

166206
// call outcome
167207
let call_input_stack_inner = call_input_stack.clone();
@@ -188,8 +228,6 @@ pub fn inspector_handle_register<DB: Database, EXT: GetInspector<DB>>(
188228
old_handle(ctx, frame, outcome)
189229
});
190230

191-
// TODO(EOF) EOF create handle.
192-
193231
// last frame outcome
194232
let old_handle = handler.execution.last_frame_return.clone();
195233
handler.execution.last_frame_return = Arc::new(move |ctx, frame_result| {

0 commit comments

Comments
 (0)