Skip to content
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
11 changes: 1 addition & 10 deletions crates/handler/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,7 @@ impl EthFrame<EthInterpreter> {
let is_static = inputs.is_static;
let gas_limit = inputs.gas_limit;

if let Some(result) = precompiles
.run(
ctx,
&inputs.bytecode_address,
&interpreter_input,
is_static,
gas_limit,
)
.map_err(ERROR::from_string)?
{
if let Some(result) = precompiles.run(ctx, &inputs).map_err(ERROR::from_string)? {
if result.result.is_ok() {
ctx.journal_mut().checkpoint_commit();
} else {
Expand Down
18 changes: 6 additions & 12 deletions crates/handler/src/precompile_provider.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use auto_impl::auto_impl;
use context::{Cfg, LocalContextTr};
use context_interface::ContextTr;
use interpreter::{CallInput, Gas, InputsImpl, InstructionResult, InterpreterResult};
use interpreter::{CallInput, CallInputs, Gas, InstructionResult, InterpreterResult};
use precompile::PrecompileError;
use precompile::{PrecompileSpecId, Precompiles};
use primitives::{hardfork::SpecId, Address, Bytes};
Expand All @@ -23,10 +23,7 @@ pub trait PrecompileProvider<CTX: ContextTr> {
fn run(
&mut self,
context: &mut CTX,
address: &Address,
inputs: &InputsImpl,
is_static: bool,
gas_limit: u64,
inputs: &CallInputs,
) -> Result<Option<Self::Output>, String>;

/// Get the warm addresses.
Expand Down Expand Up @@ -93,18 +90,15 @@ impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
fn run(
&mut self,
context: &mut CTX,
address: &Address,
inputs: &InputsImpl,
_is_static: bool,
gas_limit: u64,
inputs: &CallInputs,
) -> Result<Option<InterpreterResult>, String> {
let Some(precompile) = self.precompiles.get(address) else {
let Some(precompile) = self.precompiles.get(&inputs.bytecode_address) else {
return Ok(None);
};

let mut result = InterpreterResult {
result: InstructionResult::Return,
gas: Gas::new(gas_limit),
gas: Gas::new(inputs.gas_limit),
output: Bytes::new(),
};

Expand All @@ -121,7 +115,7 @@ impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
CallInput::Bytes(bytes) => bytes.0.iter().as_slice(),
};

match precompile.execute(input_bytes, gas_limit) {
match precompile.execute(input_bytes, inputs.gas_limit) {
Ok(output) => {
let underflow = result.gas.record_cost(output.gas_used);
assert!(underflow, "Gas underflow is not possible");
Expand Down
10 changes: 3 additions & 7 deletions crates/op-revm/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use revm::{
context::Cfg,
context_interface::ContextTr,
handler::{EthPrecompiles, PrecompileProvider},
interpreter::{InputsImpl, InterpreterResult},
interpreter::{CallInputs, InterpreterResult},
precompile::{
self, bn254, secp256r1, Precompile, PrecompileError, PrecompileId, PrecompileResult,
Precompiles,
Expand Down Expand Up @@ -111,13 +111,9 @@ where
fn run(
&mut self,
context: &mut CTX,
address: &Address,
inputs: &InputsImpl,
is_static: bool,
gas_limit: u64,
inputs: &CallInputs,
) -> Result<Option<Self::Output>, String> {
self.inner
.run(context, address, inputs, is_static, gas_limit)
self.inner.run(context, inputs)
}

#[inline]
Expand Down
30 changes: 11 additions & 19 deletions examples/custom_precompile_journal/src/precompile_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use revm::{
context::Cfg,
context_interface::{ContextTr, JournalTr, LocalContextTr, Transaction},
handler::{EthPrecompiles, PrecompileProvider},
interpreter::{Gas, InputsImpl, InstructionResult, InterpreterResult},
interpreter::{CallInputs, Gas, InstructionResult, InterpreterResult},
precompile::{PrecompileError, PrecompileOutput, PrecompileResult},
primitives::{address, hardfork::SpecId, Address, Bytes, U256},
};
Expand Down Expand Up @@ -52,21 +52,15 @@ where
fn run(
&mut self,
context: &mut CTX,
address: &Address,
inputs: &InputsImpl,
is_static: bool,
gas_limit: u64,
inputs: &CallInputs,
) -> Result<Option<Self::Output>, String> {
// Check if this is our custom precompile
if *address == CUSTOM_PRECOMPILE_ADDRESS {
return Ok(Some(run_custom_precompile(
context, inputs, is_static, gas_limit,
)?));
if inputs.bytecode_address == CUSTOM_PRECOMPILE_ADDRESS {
return Ok(Some(run_custom_precompile(context, inputs)?));
}

// Otherwise, delegate to standard Ethereum precompiles
self.inner
.run(context, address, inputs, is_static, gas_limit)
self.inner.run(context, inputs)
}

fn warm_addresses(&self) -> Box<impl Iterator<Item = Address>> {
Expand All @@ -84,9 +78,7 @@ where
/// Runs our custom precompile
fn run_custom_precompile<CTX: ContextTr>(
context: &mut CTX,
inputs: &InputsImpl,
is_static: bool,
gas_limit: u64,
inputs: &CallInputs,
) -> Result<InterpreterResult, String> {
let input_bytes = match &inputs.input {
revm::interpreter::CallInput::SharedBuffer(range) => {
Expand All @@ -105,13 +97,13 @@ fn run_custom_precompile<CTX: ContextTr>(

let result = if input_bytes.is_empty() {
// Read storage operation
handle_read_storage(context, gas_limit)
handle_read_storage(context, inputs.gas_limit)
} else if input_bytes.len() == 32 {
if is_static {
if inputs.is_static {
return Err("Cannot modify state in static context".to_string());
}
// Write storage operation
handle_write_storage(context, &input_bytes, gas_limit)
handle_write_storage(context, &input_bytes, inputs.gas_limit)
} else {
Err(PrecompileError::Other("Invalid input length".to_string()))
};
Expand All @@ -124,7 +116,7 @@ fn run_custom_precompile<CTX: ContextTr>(
} else {
InstructionResult::Return
},
gas: Gas::new(gas_limit),
gas: Gas::new(inputs.gas_limit),
output: output.bytes,
};
let underflow = interpreter_result.gas.record_cost(output.gas_used);
Expand All @@ -139,7 +131,7 @@ fn run_custom_precompile<CTX: ContextTr>(
} else {
InstructionResult::PrecompileError
},
gas: Gas::new(gas_limit),
gas: Gas::new(inputs.gas_limit),
output: Bytes::new(),
}),
}
Expand Down
Loading