revm_handler/
precompile_provider.rs1use auto_impl::auto_impl;
2use context::{Cfg, LocalContextTr};
3use context_interface::{ContextTr, JournalTr};
4use interpreter::{CallInputs, Gas, InstructionResult, InterpreterResult};
5use precompile::{PrecompileOutput, PrecompileSpecId, PrecompileStatus, Precompiles};
6use primitives::{hardfork::SpecId, Address, Bytes};
7use std::{
8 boxed::Box,
9 string::{String, ToString},
10};
11
12#[auto_impl(&mut, Box)]
14pub trait PrecompileProvider<CTX: ContextTr> {
15 type Output;
17
18 fn set_spec(&mut self, spec: <CTX::Cfg as Cfg>::Spec) -> bool;
22
23 fn run(
25 &mut self,
26 context: &mut CTX,
27 inputs: &CallInputs,
28 ) -> Result<Option<Self::Output>, String>;
29
30 fn warm_addresses(&self) -> Box<impl Iterator<Item = Address>>;
32
33 fn contains(&self, address: &Address) -> bool;
35}
36
37#[derive(Debug)]
39pub struct EthPrecompiles {
40 pub precompiles: &'static Precompiles,
42 pub spec: SpecId,
44}
45
46impl EthPrecompiles {
47 pub fn new(spec: SpecId) -> Self {
49 Self {
50 precompiles: Precompiles::new(PrecompileSpecId::from_spec_id(spec)),
51 spec,
52 }
53 }
54
55 pub fn warm_addresses(&self) -> Box<impl Iterator<Item = Address>> {
57 Box::new(self.precompiles.addresses().cloned())
58 }
59
60 pub fn contains(&self, address: &Address) -> bool {
62 self.precompiles.contains(address)
63 }
64}
65
66impl Clone for EthPrecompiles {
67 fn clone(&self) -> Self {
68 Self {
69 precompiles: self.precompiles,
70 spec: self.spec,
71 }
72 }
73}
74
75pub fn precompile_output_to_interpreter_result(
83 output: PrecompileOutput,
84 gas_limit: u64,
85) -> InterpreterResult {
86 let bytes = if output.status.is_success_or_revert() {
88 output.bytes
89 } else {
90 Bytes::new()
91 };
92
93 let mut result = InterpreterResult {
94 result: InstructionResult::Return,
95 gas: Gas::new_with_regular_gas_and_reservoir(gas_limit, output.reservoir),
96 output: bytes,
97 };
98
99 result.gas.set_state_gas_spent(output.state_gas_used);
101
102 if output.status.is_success_or_revert() {
104 let _ = result.gas.record_regular_cost(output.gas_used);
105 } else {
106 result.gas.spend_all();
107 }
108
109 result.result = match output.status {
111 PrecompileStatus::Success => InstructionResult::Return,
112 PrecompileStatus::Revert => InstructionResult::Revert,
113 PrecompileStatus::Halt(halt_reason) => {
114 if halt_reason.is_oog() {
115 InstructionResult::PrecompileOOG
116 } else {
117 InstructionResult::PrecompileError
118 }
119 }
120 };
121
122 result
123}
124
125impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
126 type Output = InterpreterResult;
127
128 fn set_spec(&mut self, spec: <CTX::Cfg as Cfg>::Spec) -> bool {
129 let spec = spec.into();
130 if spec == self.spec {
132 return false;
133 }
134 self.precompiles = Precompiles::new(PrecompileSpecId::from_spec_id(spec));
135 self.spec = spec;
136 true
137 }
138
139 fn run(
140 &mut self,
141 context: &mut CTX,
142 inputs: &CallInputs,
143 ) -> Result<Option<InterpreterResult>, String> {
144 let Some(precompile) = self.precompiles.get(&inputs.bytecode_address) else {
145 return Ok(None);
146 };
147
148 let output = precompile
149 .execute(
150 &inputs.input.as_bytes(context),
151 inputs.gas_limit,
152 inputs.reservoir,
153 )
154 .map_err(|e| e.to_string())?;
155
156 if let Some(halt_reason) = output.halt_reason() {
160 if !halt_reason.is_oog() && context.journal().depth() == 1 {
161 context
162 .local_mut()
163 .set_precompile_error_context(halt_reason.to_string());
164 }
165 }
166
167 let result = precompile_output_to_interpreter_result(output, inputs.gas_limit);
168 Ok(Some(result))
169 }
170
171 fn warm_addresses(&self) -> Box<impl Iterator<Item = Address>> {
172 Self::warm_addresses(self)
173 }
174
175 fn contains(&self, address: &Address) -> bool {
176 Self::contains(self, address)
177 }
178}