1
1
use crate :: { evm:: Trevm , helpers:: Ctx , states:: EvmNeedsCfg } ;
2
2
use revm:: {
3
- database:: in_memory_db:: InMemoryDB , inspector :: NoOpInspector , primitives :: hardfork :: SpecId ,
4
- Database , Inspector , MainBuilder ,
3
+ database:: in_memory_db:: InMemoryDB , handler :: EthPrecompiles , inspector :: NoOpInspector ,
4
+ precompile :: Precompiles , primitives :: hardfork :: SpecId , Database , Inspector , MainBuilder ,
5
5
} ;
6
6
7
7
/// Error that can occur when building a Trevm instance.
@@ -19,13 +19,14 @@ pub struct TrevmBuilder<Db, Insp> {
19
19
pub ( crate ) db : Option < Db > ,
20
20
pub ( crate ) insp : Insp ,
21
21
pub ( crate ) spec : SpecId ,
22
+ pub ( crate ) precompiles : Option < & ' static Precompiles > ,
22
23
}
23
24
24
25
impl TrevmBuilder < InMemoryDB , NoOpInspector > {
25
26
/// Create a new builder with the default database and inspector.
26
27
#[ allow( clippy:: new_without_default) ] // default would make bad devex :(
27
28
pub const fn new ( ) -> Self {
28
- Self { db : None , insp : NoOpInspector , spec : SpecId :: PRAGUE }
29
+ Self { db : None , insp : NoOpInspector , spec : SpecId :: PRAGUE , precompiles : None }
29
30
}
30
31
}
31
32
@@ -35,12 +36,17 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
35
36
where
36
37
Db : Database ,
37
38
{
38
- TrevmBuilder { db : Some ( db) , insp : self . insp , spec : self . spec }
39
+ TrevmBuilder {
40
+ db : Some ( db) ,
41
+ insp : self . insp ,
42
+ spec : self . spec ,
43
+ precompiles : self . precompiles ,
44
+ }
39
45
}
40
46
41
47
/// Set the inspector for the EVM.
42
48
pub fn with_insp < OInsp > ( self , insp : OInsp ) -> TrevmBuilder < Db , OInsp > {
43
- TrevmBuilder { db : self . db , insp, spec : self . spec }
49
+ TrevmBuilder { db : self . db , insp, spec : self . spec , precompiles : self . precompiles }
44
50
}
45
51
46
52
/// Set the spec id for the EVM.
@@ -49,6 +55,22 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
49
55
self
50
56
}
51
57
58
+ /// Set the precompiles for the EVM.
59
+ ///
60
+ /// The precompiles must be a static reference to a precompiles instance.
61
+ /// If not using a built-in [`Precompiles`], it is generally recommended to
62
+ /// use a `OnceLock` to create this borrow.
63
+ pub const fn with_precompiles ( mut self , precompiles : & ' static Precompiles ) -> Self {
64
+ self . precompiles = Some ( precompiles) ;
65
+ self
66
+ }
67
+
68
+ /// Set the precompiles for the EVM from the current spec id.
69
+ pub fn with_precompiles_from_spec ( mut self ) -> Self {
70
+ self . precompiles = Some ( Precompiles :: new ( self . spec . into ( ) ) ) ;
71
+ self
72
+ }
73
+
52
74
/// Build the Trevm instance.
53
75
pub fn build_trevm ( self ) -> Result < EvmNeedsCfg < Db , Insp > , TrevmBuilderError >
54
76
where
@@ -57,7 +79,13 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
57
79
{
58
80
let db = self . db . ok_or ( TrevmBuilderError :: DatabaseNotSet ) ?;
59
81
let ctx = Ctx :: new ( db, self . spec ) ;
60
- let evm = ctx. build_mainnet_with_inspector ( self . insp ) ;
82
+
83
+ let mut evm = ctx. build_mainnet_with_inspector ( self . insp ) ;
84
+
85
+ if let Some ( precompiles) = self . precompiles {
86
+ evm. precompiles = EthPrecompiles { precompiles, spec : self . spec } ;
87
+ }
88
+
61
89
Ok ( Trevm :: from ( evm) )
62
90
}
63
91
}
0 commit comments