Skip to content

Commit 827d572

Browse files
authored
refactor(handler): provide &CallInputstoPrecompileProvider::run (bluealloy#2921)
1 parent d8fd92f commit 827d572

File tree

4 files changed

+21
-48
lines changed

4 files changed

+21
-48
lines changed

crates/handler/src/frame.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,7 @@ impl EthFrame<EthInterpreter> {
193193
let is_static = inputs.is_static;
194194
let gas_limit = inputs.gas_limit;
195195

196-
if let Some(result) = precompiles
197-
.run(
198-
ctx,
199-
&inputs.bytecode_address,
200-
&interpreter_input,
201-
is_static,
202-
gas_limit,
203-
)
204-
.map_err(ERROR::from_string)?
205-
{
196+
if let Some(result) = precompiles.run(ctx, &inputs).map_err(ERROR::from_string)? {
206197
if result.result.is_ok() {
207198
ctx.journal_mut().checkpoint_commit();
208199
} else {

crates/handler/src/precompile_provider.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use auto_impl::auto_impl;
22
use context::{Cfg, LocalContextTr};
33
use context_interface::ContextTr;
4-
use interpreter::{CallInput, Gas, InputsImpl, InstructionResult, InterpreterResult};
4+
use interpreter::{CallInput, CallInputs, Gas, InstructionResult, InterpreterResult};
55
use precompile::PrecompileError;
66
use precompile::{PrecompileSpecId, Precompiles};
77
use primitives::{hardfork::SpecId, Address, Bytes};
@@ -23,10 +23,7 @@ pub trait PrecompileProvider<CTX: ContextTr> {
2323
fn run(
2424
&mut self,
2525
context: &mut CTX,
26-
address: &Address,
27-
inputs: &InputsImpl,
28-
is_static: bool,
29-
gas_limit: u64,
26+
inputs: &CallInputs,
3027
) -> Result<Option<Self::Output>, String>;
3128

3229
/// Get the warm addresses.
@@ -93,18 +90,15 @@ impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
9390
fn run(
9491
&mut self,
9592
context: &mut CTX,
96-
address: &Address,
97-
inputs: &InputsImpl,
98-
_is_static: bool,
99-
gas_limit: u64,
93+
inputs: &CallInputs,
10094
) -> Result<Option<InterpreterResult>, String> {
101-
let Some(precompile) = self.precompiles.get(address) else {
95+
let Some(precompile) = self.precompiles.get(&inputs.bytecode_address) else {
10296
return Ok(None);
10397
};
10498

10599
let mut result = InterpreterResult {
106100
result: InstructionResult::Return,
107-
gas: Gas::new(gas_limit),
101+
gas: Gas::new(inputs.gas_limit),
108102
output: Bytes::new(),
109103
};
110104

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

124-
match precompile.execute(input_bytes, gas_limit) {
118+
match precompile.execute(input_bytes, inputs.gas_limit) {
125119
Ok(output) => {
126120
let underflow = result.gas.record_cost(output.gas_used);
127121
assert!(underflow, "Gas underflow is not possible");

crates/op-revm/src/precompiles.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use revm::{
44
context::Cfg,
55
context_interface::ContextTr,
66
handler::{EthPrecompiles, PrecompileProvider},
7-
interpreter::{InputsImpl, InterpreterResult},
7+
interpreter::{CallInputs, InterpreterResult},
88
precompile::{
99
self, bn254, secp256r1, Precompile, PrecompileError, PrecompileId, PrecompileResult,
1010
Precompiles,
@@ -111,13 +111,9 @@ where
111111
fn run(
112112
&mut self,
113113
context: &mut CTX,
114-
address: &Address,
115-
inputs: &InputsImpl,
116-
is_static: bool,
117-
gas_limit: u64,
114+
inputs: &CallInputs,
118115
) -> Result<Option<Self::Output>, String> {
119-
self.inner
120-
.run(context, address, inputs, is_static, gas_limit)
116+
self.inner.run(context, inputs)
121117
}
122118

123119
#[inline]

examples/custom_precompile_journal/src/precompile_provider.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use revm::{
44
context::Cfg,
55
context_interface::{ContextTr, JournalTr, LocalContextTr, Transaction},
66
handler::{EthPrecompiles, PrecompileProvider},
7-
interpreter::{Gas, InputsImpl, InstructionResult, InterpreterResult},
7+
interpreter::{CallInputs, Gas, InstructionResult, InterpreterResult},
88
precompile::{PrecompileError, PrecompileOutput, PrecompileResult},
99
primitives::{address, hardfork::SpecId, Address, Bytes, U256},
1010
};
@@ -52,21 +52,15 @@ where
5252
fn run(
5353
&mut self,
5454
context: &mut CTX,
55-
address: &Address,
56-
inputs: &InputsImpl,
57-
is_static: bool,
58-
gas_limit: u64,
55+
inputs: &CallInputs,
5956
) -> Result<Option<Self::Output>, String> {
6057
// Check if this is our custom precompile
61-
if *address == CUSTOM_PRECOMPILE_ADDRESS {
62-
return Ok(Some(run_custom_precompile(
63-
context, inputs, is_static, gas_limit,
64-
)?));
58+
if inputs.bytecode_address == CUSTOM_PRECOMPILE_ADDRESS {
59+
return Ok(Some(run_custom_precompile(context, inputs)?));
6560
}
6661

6762
// Otherwise, delegate to standard Ethereum precompiles
68-
self.inner
69-
.run(context, address, inputs, is_static, gas_limit)
63+
self.inner.run(context, inputs)
7064
}
7165

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

10698
let result = if input_bytes.is_empty() {
10799
// Read storage operation
108-
handle_read_storage(context, gas_limit)
100+
handle_read_storage(context, inputs.gas_limit)
109101
} else if input_bytes.len() == 32 {
110-
if is_static {
102+
if inputs.is_static {
111103
return Err("Cannot modify state in static context".to_string());
112104
}
113105
// Write storage operation
114-
handle_write_storage(context, &input_bytes, gas_limit)
106+
handle_write_storage(context, &input_bytes, inputs.gas_limit)
115107
} else {
116108
Err(PrecompileError::Other("Invalid input length".to_string()))
117109
};
@@ -124,7 +116,7 @@ fn run_custom_precompile<CTX: ContextTr>(
124116
} else {
125117
InstructionResult::Return
126118
},
127-
gas: Gas::new(gas_limit),
119+
gas: Gas::new(inputs.gas_limit),
128120
output: output.bytes,
129121
};
130122
let underflow = interpreter_result.gas.record_cost(output.gas_used);
@@ -139,7 +131,7 @@ fn run_custom_precompile<CTX: ContextTr>(
139131
} else {
140132
InstructionResult::PrecompileError
141133
},
142-
gas: Gas::new(gas_limit),
134+
gas: Gas::new(inputs.gas_limit),
143135
output: Bytes::new(),
144136
}),
145137
}

0 commit comments

Comments
 (0)