@@ -17,24 +17,29 @@ use alloy::{
1717 providers:: { Provider , ProviderBuilder } ,
1818 rpc:: types:: TransactionRequest ,
1919} ;
20+ use alloy_evm:: { eth:: EthEvmContext , EthEvm , Evm } ;
2021use eyre:: Result ;
2122use foundry_fork_db:: { cache:: BlockchainDbMeta , BlockchainDb , SharedBackend } ;
22- use revm:: { db:: CacheDB , DatabaseRef , Evm } ;
23- use revm_primitives:: { BlobExcessGasAndPrice , BlockEnv , TxEnv } ;
23+ use revm:: {
24+ context:: { BlockEnv , Evm as RevmEvm , TxEnv } ,
25+ context_interface:: block:: BlobExcessGasAndPrice ,
26+ database:: WrapDatabaseRef ,
27+ handler:: { instructions:: EthInstructions , EthPrecompiles } ,
28+ inspector:: NoOpInspector ,
29+ DatabaseRef ,
30+ } ;
2431
2532#[ tokio:: main]
2633async fn main ( ) -> Result < ( ) > {
2734 let anvil = Anvil :: new ( ) . spawn ( ) ;
28- let provider = ProviderBuilder :: new ( ) . network :: < AnyNetwork > ( ) . on_http ( anvil. endpoint_url ( ) ) ;
35+ let provider =
36+ ProviderBuilder :: new ( ) . network :: < AnyNetwork > ( ) . connect_http ( anvil. endpoint_url ( ) ) ;
2937
3038 let block = provider. get_block ( BlockId :: latest ( ) ) . await ?. unwrap ( ) ;
3139
3240 // The `BlockchainDbMeta` is used a identifier when the db is flushed to the disk.
3341 // This aids in cases where the disk contains data from multiple forks.
34- let meta = BlockchainDbMeta :: default ( )
35- . with_chain_id ( 31337 )
36- . with_block ( & block. inner )
37- . with_url ( & anvil. endpoint ( ) ) ;
42+ let meta = BlockchainDbMeta :: default ( ) . with_block ( & block. inner ) . with_url ( & anvil. endpoint ( ) ) ;
3843
3944 let db = BlockchainDb :: new ( meta, None ) ;
4045
@@ -45,9 +50,8 @@ async fn main() -> Result<()> {
4550 //
4651 // For example, if we send two requests to get_full_block(0) simultaneously, the
4752 // `BackendHandler` is smart enough to only send one request to the RPC provider, and queue the
48- // other request until the response is received.
49- // Once the response from RPC provider is received it relays the response to both the requests
50- // over their respective channels.
53+ // other request until the response is received. Once the response from RPC provider is
54+ // received it relays the response to both the requests // over their respective channels.
5155 //
5256 // The `SharedBackend` and `BackendHandler` communicate over an unbounded channel.
5357 let shared = SharedBackend :: spawn_backend ( Arc :: new ( provider. clone ( ) ) , db, None ) . await ;
@@ -69,9 +73,9 @@ async fn main() -> Result<()> {
6973
7074 println ! ( "-------get_full_block--------" ) ;
7175 // The backend handle falls back to the RPC provider if the block is not in the cache.
72- println ! ( "1st request (via rpc): {:?}" , time_rpc ) ;
76+ println ! ( "1st request (via rpc): {time_rpc :?}" ) ;
7377 // The block is cached due to the previous request and can be fetched from db.
74- println ! ( "2nd request (via fork db): {:?}\n " , time_cache ) ;
78+ println ! ( "2nd request (via fork db): {time_cache :?}\n " ) ;
7579
7680 let alice = anvil. addresses ( ) [ 0 ] ;
7781 let bob = anvil. addresses ( ) [ 1 ] ;
@@ -87,15 +91,15 @@ async fn main() -> Result<()> {
8791 . with_gas_limit ( 21000 )
8892 . with_nonce ( 0 ) ;
8993
90- let mut evm = configure_evm_env ( block, shared. clone ( ) , configure_tx_env ( tx_req ) ) ;
94+ let mut evm = configure_evm ( block, shared. clone ( ) ) ;
9195
9296 // Fetches accounts from the RPC
9397 let start_t = std:: time:: Instant :: now ( ) ;
9498 let alice_bal = shared. basic_ref ( alice) ?. unwrap ( ) . balance ;
9599 let bob_bal = shared. basic_ref ( bob) ?. unwrap ( ) . balance ;
96100 let time_rpc = start_t. elapsed ( ) ;
97101
98- let res = evm. transact ( ) . unwrap ( ) ;
102+ let res = evm. transact ( configure_tx_env ( tx_req ) ) . unwrap ( ) ;
99103
100104 let total_spent = U256 :: from ( res. result . gas_used ( ) ) * U256 :: from ( basefee) + U256 :: from ( 100 ) ;
101105
@@ -108,27 +112,25 @@ async fn main() -> Result<()> {
108112 let time_cache = start_t. elapsed ( ) ;
109113
110114 println ! ( "-------get_account--------" ) ;
111- println ! ( "1st request (via rpc): {:?}" , time_rpc ) ;
112- println ! ( "2nd request (via fork db): {:?}\n " , time_cache ) ;
115+ println ! ( "1st request (via rpc): {time_rpc :?}" ) ;
116+ println ! ( "2nd request (via fork db): {time_cache :?}\n " ) ;
113117
114118 assert_eq ! ( alice_bal_after, alice_bal - total_spent) ;
115119 assert_eq ! ( bob_bal_after, bob_bal + U256 :: from( 100 ) ) ;
116120
117121 Ok ( ( ) )
118122}
119123
120- fn configure_evm_env (
124+ fn configure_evm (
121125 block : AnyRpcBlock ,
122126 shared : SharedBackend ,
123- tx_env : TxEnv ,
124- ) -> Evm < ' static , ( ) , CacheDB < SharedBackend > > {
125- let basefee = block. header . base_fee_per_gas ( ) . map ( U256 :: from) . unwrap_or_default ( ) ;
127+ ) -> EthEvm < WrapDatabaseRef < SharedBackend > , NoOpInspector > {
126128 let block_env = BlockEnv {
127- number : U256 :: from ( block. header . number ( ) ) ,
128- coinbase : block. header . beneficiary ( ) ,
129- timestamp : U256 :: from ( block. header . timestamp ( ) ) ,
130- gas_limit : U256 :: from ( block. header . gas_limit ( ) ) ,
131- basefee,
129+ number : block. header . number ( ) ,
130+ beneficiary : block. header . beneficiary ( ) ,
131+ timestamp : block. header . timestamp ( ) ,
132+ gas_limit : block. header . gas_limit ( ) ,
133+ basefee : block . header . base_fee_per_gas ( ) . unwrap_or ( 0 ) ,
132134 prevrandao : block. header . mix_hash ( ) ,
133135 difficulty : block. header . difficulty ( ) ,
134136 blob_excess_gas_and_price : Some ( BlobExcessGasAndPrice :: new (
@@ -137,19 +139,22 @@ fn configure_evm_env(
137139 ) ) ,
138140 } ;
139141
140- let db = CacheDB :: new ( shared) ;
142+ let context =
143+ EthEvmContext :: new ( WrapDatabaseRef ( shared) , revm_primitives:: hardfork:: SpecId :: PRAGUE )
144+ . with_block ( block_env) ;
141145
142- let evm = Evm :: builder ( ) . with_block_env ( block_env) . with_db ( db) . with_tx_env ( tx_env) . build ( ) ;
146+ let evm = RevmEvm :: new ( context, EthInstructions :: default ( ) , EthPrecompiles :: default ( ) )
147+ . with_inspector ( NoOpInspector ) ;
143148
144- evm
149+ EthEvm :: new ( evm, false )
145150}
146151
147152fn configure_tx_env ( tx_req : TransactionRequest ) -> TxEnv {
148153 TxEnv {
149154 caller : tx_req. from . unwrap ( ) ,
150- transact_to : tx_req. to . unwrap ( ) ,
155+ kind : tx_req. to . unwrap ( ) ,
151156 value : tx_req. value . unwrap ( ) ,
152- gas_price : U256 :: from ( tx_req. max_fee_per_gas . unwrap ( ) ) ,
157+ gas_price : tx_req. max_fee_per_gas . unwrap ( ) ,
153158 gas_limit : tx_req. gas . unwrap_or_default ( ) ,
154159 ..Default :: default ( )
155160 }
0 commit comments