11use  crate :: { evm:: Trevm ,  helpers:: Ctx ,  states:: EvmNeedsCfg } ; 
22use  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 , 
55} ; 
66
77/// Error that can occur when building a Trevm instance. 
@@ -19,13 +19,14 @@ pub struct TrevmBuilder<Db, Insp> {
1919    pub ( crate )  db :  Option < Db > , 
2020    pub ( crate )  insp :  Insp , 
2121    pub ( crate )  spec :  SpecId , 
22+     pub ( crate )  precompiles :  Option < & ' static  Precompiles > , 
2223} 
2324
2425impl  TrevmBuilder < InMemoryDB ,  NoOpInspector >  { 
2526    /// Create a new builder with the default database and inspector. 
2627#[ allow( clippy:: new_without_default) ]   // default would make bad devex :( 
2728    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  } 
2930    } 
3031} 
3132
@@ -35,12 +36,17 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
3536    where 
3637        Db :  Database , 
3738    { 
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+         } 
3945    } 
4046
4147    /// Set the inspector for the EVM. 
4248pub  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  } 
4450    } 
4551
4652    /// Set the spec id for the EVM. 
@@ -49,6 +55,22 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
4955        self 
5056    } 
5157
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+ 
5274    /// Build the Trevm instance. 
5375pub  fn  build_trevm ( self )  -> Result < EvmNeedsCfg < Db ,  Insp > ,  TrevmBuilderError > 
5476    where 
@@ -57,7 +79,13 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
5779    { 
5880        let  db = self . db . ok_or ( TrevmBuilderError :: DatabaseNotSet ) ?; 
5981        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+ 
6189        Ok ( Trevm :: from ( evm) ) 
6290    } 
6391} 
0 commit comments