Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(EOF): Add EOF to inspector handle register #1469

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/revm/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ pub trait Inspector<DB: Database> {
outcome
}

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

/// Called when eof creating has ended.
fn eofcreate_end(
&mut self,
context: &mut EvmContext<DB>,
Expand Down
44 changes: 41 additions & 3 deletions crates/revm/src/inspector/handler_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,47 @@ pub fn inspector_handle_register<DB: Database, EXT: GetInspector<DB>>(
},
);

// TODO(EOF) EOF create call.
// Calls inspector `eofcreate` and `initialize_interp` functions. Queues the inputs for the `eofcreate_end`` function.
// Calls the old handler, and in case of inspector returning outcome,
// returns the outcome without executing eofcreate.
let eofcreate_input_stack_inner = eofcreate_input_stack.clone();
let old_handle = handler.execution.eofcreate.clone();
handler.execution.eofcreate = Arc::new(
move |ctx, mut inputs| -> Result<FrameOrResult, EVMError<DB::Error>> {
// Call inspector to change input or return outcome.
let outcome = ctx
.external
.get_inspector()
.eofcreate(&mut ctx.evm, &mut inputs);
eofcreate_input_stack_inner
.borrow_mut()
.push(inputs.clone());
if let Some(outcome) = outcome {
return Ok(FrameOrResult::Result(FrameResult::EOFCreate(outcome)));
}

let mut frame_or_result = old_handle(ctx, inputs);
if let Ok(FrameOrResult::Frame(frame)) = &mut frame_or_result {
ctx.external
.get_inspector()
.initialize_interp(frame.interpreter_mut(), &mut ctx.evm)
}
frame_or_result
},
);

// Pops eofcreate input from the stack and calls inspector `eofcreate_end` function.
// preserve the old handler and calls it with the outcome.
let eofcreate_input_stack_inner = eofcreate_input_stack.clone();
let old_handle = handler.execution.insert_eofcreate_outcome.clone();
handler.execution.insert_eofcreate_outcome = Arc::new(move |ctx, frame, mut outcome| {
let create_inputs = eofcreate_input_stack_inner.borrow_mut().pop().unwrap();
outcome = ctx
.external
.get_inspector()
.eofcreate_end(&mut ctx.evm, &create_inputs, outcome);
old_handle(ctx, frame, outcome)
});

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

// TODO(EOF) EOF create handle.

// last frame outcome
let old_handle = handler.execution.last_frame_return.clone();
handler.execution.last_frame_return = Arc::new(move |ctx, frame_result| {
Expand Down