forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundry_fork_db.rs
More file actions
161 lines (132 loc) · 6.02 KB
/
Copy pathfoundry_fork_db.rs
File metadata and controls
161 lines (132 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! This example demonstrates how to use `foundry_fork_db` to build a minimal fork with a db that
//! caches responses from the RPC provider.
//!
//! `foundry_fork_db` is designed out-of-the-box to smartly cache and deduplicate requests to the
//! rpc provider, while fetching data that is missing from it's db instance.
//!
//! `foundry_fork_db` serves as the backend for Foundry's forking functionality in Anvil and Forge.
use std::sync::Arc;
use alloy::{
consensus::BlockHeader,
eips::BlockId,
network::{AnyNetwork, AnyRpcBlock, TransactionBuilder},
node_bindings::Anvil,
primitives::U256,
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
};
use alloy_evm::{eth::EthEvmContext, EthEvm, Evm};
use eyre::Result;
use foundry_fork_db::{cache::BlockchainDbMeta, BlockchainDb, SharedBackend};
use revm::{
context::{BlockEnv, Evm as RevmEvm, TxEnv},
context_interface::block::BlobExcessGasAndPrice,
database::WrapDatabaseRef,
handler::{instructions::EthInstructions, EthPrecompiles},
inspector::NoOpInspector,
DatabaseRef,
};
#[tokio::main]
async fn main() -> Result<()> {
let anvil = Anvil::new().spawn();
let provider =
ProviderBuilder::new().network::<AnyNetwork>().connect_http(anvil.endpoint_url());
let block = provider.get_block(BlockId::latest()).await?.unwrap();
// The `BlockchainDbMeta` is used a identifier when the db is flushed to the disk.
// This aids in cases where the disk contains data from multiple forks.
let meta = BlockchainDbMeta::default().with_block(&block.inner).with_url(/sitelet?url=https%3A%2F%2Fgithub.com%2Fabhi3700%2Fexamples%2Fblob%2Fmain%2Fexamples%2Fadvanced%2Fexamples%2F%26amp%3Banvil.endpoint%28));
let db = BlockchainDb::new(meta, None);
// Spawn the backend with the db instance.
// `SharedBackend` is used to send request to the `BackendHandler` which is responsible for
// filling missing data in the db, and also deduplicate requests that are being sent to the
// RPC provider.
//
// For example, if we send two requests to get_full_block(0) simultaneously, the
// `BackendHandler` is smart enough to only send one request to the RPC provider, and queue the
// other request until the response is received. Once the response from RPC provider is
// received it relays the response to both the requests // over their respective channels.
//
// The `SharedBackend` and `BackendHandler` communicate over an unbounded channel.
let shared = SharedBackend::spawn_backend(Arc::new(provider.clone()), db, None).await;
let start_t = std::time::Instant::now();
let block_rpc = shared.get_full_block(0).unwrap();
let time_rpc = start_t.elapsed();
// `SharedBackend` is cloneable and holds the channel to the same `BackendHandler`.
#[allow(clippy::redundant_clone)]
let cloned_backend = shared.clone();
// Block gets cached in the db
let start_t = std::time::Instant::now();
let block_cache = cloned_backend.get_full_block(0).unwrap();
let time_cache = start_t.elapsed();
assert_eq!(block_rpc, block_cache);
println!("-------get_full_block--------");
// The backend handle falls back to the RPC provider if the block is not in the cache.
println!("1st request (via rpc): {time_rpc:?}");
// The block is cached due to the previous request and can be fetched from db.
println!("2nd request (via fork db): {time_cache:?}\n");
let alice = anvil.addresses()[0];
let bob = anvil.addresses()[1];
let basefee = block.header.base_fee_per_gas.unwrap();
let tx_req = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_value(U256::from(100))
.with_max_fee_per_gas(basefee as u128)
.with_max_priority_fee_per_gas(basefee as u128 + 1)
.with_gas_limit(21000)
.with_nonce(0);
let mut evm = configure_evm(block, shared.clone());
// Fetches accounts from the RPC
let start_t = std::time::Instant::now();
let alice_bal = shared.basic_ref(alice)?.unwrap().balance;
let bob_bal = shared.basic_ref(bob)?.unwrap().balance;
let time_rpc = start_t.elapsed();
let res = evm.transact(configure_tx_env(tx_req)).unwrap();
let total_spent = U256::from(res.result.gas_used()) * U256::from(basefee) + U256::from(100);
shared.data().do_commit(res.state);
// Fetches accounts from the cache
let start_t = std::time::Instant::now();
let alice_bal_after = shared.basic_ref(alice)?.unwrap().balance;
let bob_bal_after = shared.basic_ref(bob)?.unwrap().balance;
let time_cache = start_t.elapsed();
println!("-------get_account--------");
println!("1st request (via rpc): {time_rpc:?}");
println!("2nd request (via fork db): {time_cache:?}\n");
assert_eq!(alice_bal_after, alice_bal - total_spent);
assert_eq!(bob_bal_after, bob_bal + U256::from(100));
Ok(())
}
fn configure_evm(
block: AnyRpcBlock,
shared: SharedBackend,
) -> EthEvm<WrapDatabaseRef<SharedBackend>, NoOpInspector> {
let block_env = BlockEnv {
number: block.header.number(),
beneficiary: block.header.beneficiary(),
timestamp: block.header.timestamp(),
gas_limit: block.header.gas_limit(),
basefee: block.header.base_fee_per_gas().unwrap_or(0),
prevrandao: block.header.mix_hash(),
difficulty: block.header.difficulty(),
blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(
block.header.excess_blob_gas().unwrap_or_default(),
false,
)),
};
let context =
EthEvmContext::new(WrapDatabaseRef(shared), revm_primitives::hardfork::SpecId::PRAGUE)
.with_block(block_env);
let evm = RevmEvm::new(context, EthInstructions::default(), EthPrecompiles::default())
.with_inspector(NoOpInspector);
EthEvm::new(evm, false)
}
fn configure_tx_env(tx_req: TransactionRequest) -> TxEnv {
TxEnv {
caller: tx_req.from.unwrap(),
kind: tx_req.to.unwrap(),
value: tx_req.value.unwrap(),
gas_price: tx_req.max_fee_per_gas.unwrap(),
gas_limit: tx_req.gas.unwrap_or_default(),
..Default::default()
}
}