11use crate :: { evm:: Trevm , helpers:: Ctx , EvmNeedsCfg } ;
22use revm:: {
3- database :: in_memory_db :: InMemoryDB , handler:: EthPrecompiles , inspector:: NoOpInspector ,
4- precompile :: Precompiles , primitives:: hardfork:: SpecId , Database , Inspector , MainBuilder ,
3+ handler:: EthPrecompiles , inspector:: NoOpInspector , precompile :: Precompiles ,
4+ primitives:: hardfork:: SpecId , Database , Inspector , MainBuilder ,
55} ;
66
77/// Error that can occur when building a Trevm instance.
@@ -13,48 +13,65 @@ pub enum TrevmBuilderError {
1313 DatabaseNotSet ,
1414}
1515
16+ #[ allow( unnameable_types) ]
17+ #[ derive( Debug , Copy , Clone ) ]
18+ pub struct BuilderNeedsDb {
19+ _private : ( ) ,
20+ }
21+
22+ #[ allow( unnameable_types) ]
23+ #[ derive( Debug , Copy , Clone ) ]
24+ pub struct BuilderReady < Db > {
25+ db : Db ,
26+ }
27+
1628/// A builder for [`Trevm`] that allows configuring the EVM.
1729#[ derive( Debug , Clone ) ]
18- pub struct TrevmBuilder < Db , Insp > {
19- pub ( crate ) db : Option < Db > ,
30+ pub struct TrevmBuilder < Insp , State = BuilderNeedsDb > {
2031 pub ( crate ) insp : Insp ,
2132 pub ( crate ) spec : SpecId ,
2233 pub ( crate ) precompiles : Option < & ' static Precompiles > ,
34+ pub ( crate ) state : State ,
2335}
2436
25- impl TrevmBuilder < InMemoryDB , NoOpInspector > {
37+ impl TrevmBuilder < NoOpInspector > {
2638 /// Create a new builder with the default database and inspector.
2739 #[ allow( clippy:: new_without_default) ] // default would make bad devex :(
2840 pub const fn new ( ) -> Self {
29- Self { db : None , insp : NoOpInspector , spec : SpecId :: PRAGUE , precompiles : None }
41+ Self {
42+ insp : NoOpInspector ,
43+ spec : SpecId :: PRAGUE ,
44+ precompiles : None ,
45+ state : BuilderNeedsDb { _private : ( ) } ,
46+ }
3047 }
3148}
3249
33- impl < Db , Insp > TrevmBuilder < Db , Insp > {
50+ impl < Insp , State > TrevmBuilder < Insp , State > {
3451 /// Set the database for the EVM.
35- pub fn with_db < Odb > ( self , db : Odb ) -> TrevmBuilder < Odb , Insp >
52+ pub fn with_db < Odb > ( self , db : Odb ) -> TrevmBuilder < Insp , BuilderReady < Odb > >
3653 where
37- Db : Database ,
54+ Odb : Database ,
3855 {
3956 TrevmBuilder {
40- db : Some ( db) ,
4157 insp : self . insp ,
4258 spec : self . spec ,
4359 precompiles : self . precompiles ,
60+ state : BuilderReady { db } ,
4461 }
4562 }
4663
4764 /// Set the inspector for the EVM.
4865 ///
4966 /// Equivalent to [`Self::with_inspector`].
50- pub fn with_insp < OInsp > ( self , insp : OInsp ) -> TrevmBuilder < Db , OInsp > {
51- TrevmBuilder { db : self . db , insp, spec : self . spec , precompiles : self . precompiles }
67+ pub fn with_insp < OInsp > ( self , insp : OInsp ) -> TrevmBuilder < OInsp , State > {
68+ TrevmBuilder { insp, spec : self . spec , precompiles : self . precompiles , state : self . state }
5269 }
5370
5471 /// Set the inspector for the EVM.
5572 ///
5673 /// Equivalent to [`Self::with_insp`].
57- pub fn with_inspector < OInsp > ( self , insp : OInsp ) -> TrevmBuilder < Db , OInsp > {
74+ pub fn with_inspector < OInsp > ( self , insp : OInsp ) -> TrevmBuilder < OInsp , State > {
5875 self . with_insp ( insp)
5976 }
6077
@@ -79,14 +96,16 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
7996 self . precompiles = Some ( Precompiles :: new ( self . spec . into ( ) ) ) ;
8097 self
8198 }
99+ }
82100
101+ impl < Insp , Db > TrevmBuilder < Insp , BuilderReady < Db > > {
83102 /// Build the Trevm instance.
84- pub fn build_trevm ( self ) -> Result < EvmNeedsCfg < Db , Insp > , TrevmBuilderError >
103+ pub fn build_trevm ( self ) -> EvmNeedsCfg < Db , Insp >
85104 where
86105 Db : Database ,
87106 Insp : Inspector < Ctx < Db > > ,
88107 {
89- let db = self . db . ok_or ( TrevmBuilderError :: DatabaseNotSet ) ? ;
108+ let db = self . state . db ;
90109 let ctx = Ctx :: new ( db, self . spec ) ;
91110
92111 let mut evm = ctx. build_mainnet_with_inspector ( self . insp ) ;
@@ -95,7 +114,7 @@ impl<Db, Insp> TrevmBuilder<Db, Insp> {
95114 evm. precompiles = EthPrecompiles { precompiles, spec : self . spec } ;
96115 }
97116
98- Ok ( Trevm :: from ( evm) )
117+ Trevm :: from ( evm)
99118 }
100119}
101120
0 commit comments