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::{PrecompileSpecId, 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
75impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
76 type Output = InterpreterResult;
77
78 fn set_spec(&mut self, spec: <CTX::Cfg as Cfg>::Spec) -> bool {
79 let spec = spec.into();
80 if spec == self.spec {
82 return false;
83 }
84 self.precompiles = Precompiles::new(PrecompileSpecId::from_spec_id(spec));
85 self.spec = spec;
86 true
87 }
88
89 fn run(
90 &mut self,
91 context: &mut CTX,
92 inputs: &CallInputs,
93 ) -> Result<Option<InterpreterResult>, String> {
94 let Some(precompile) = self.precompiles.get(&inputs.bytecode_address) else {
95 return Ok(None);
96 };
97
98 let mut result = InterpreterResult {
99 result: InstructionResult::Return,
100 gas: Gas::new(inputs.gas_limit),
101 output: Bytes::new(),
102 };
103
104 let exec_result = precompile.execute(&inputs.input.as_bytes(context), inputs.gas_limit);
105 match exec_result {
106 Ok(output) => {
107 result.gas.record_refund(output.gas_refunded);
108 let success = result.gas.record_cost(output.gas_used);
109 assert!(success, "Gas underflow is not possible");
110 result.result = if output.reverted {
111 InstructionResult::Revert
112 } else {
113 InstructionResult::Return
114 };
115 result.output = output.bytes;
116 }
117 Err(e) => {
118 if e.is_fatal() {
119 return Err(e.to_string());
120 }
121 result.result = if e.is_oog() {
122 InstructionResult::PrecompileOOG
123 } else {
124 InstructionResult::PrecompileError
125 };
126 if !e.is_oog() && context.journal().depth() == 1 {
130 context
131 .local_mut()
132 .set_precompile_error_context(e.to_string());
133 }
134 }
135 }
136 Ok(Some(result))
137 }
138
139 fn warm_addresses(&self) -> Box<impl Iterator<Item = Address>> {
140 Self::warm_addresses(self)
141 }
142
143 fn contains(&self, address: &Address) -> bool {
144 Self::contains(self, address)
145 }
146}