1use alloy_chains::Chain;
4use alloy_consensus::BlockHeader;
5use alloy_hardforks::EthereumHardfork;
6use alloy_primitives::{Address, B256, U256};
7use alloy_provider::network::TransactionResponse;
8use parking_lot::RwLock;
9use revm::{
10 context::BlockEnv,
11 context_interface::block::BlobExcessGasAndPrice,
12 primitives::{
13 eip4844::{BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN, BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE},
14 map::{AddressHashMap, HashMap},
15 KECCAK_EMPTY,
16 },
17 state::{Account, AccountInfo, AccountStatus},
18 DatabaseCommit,
19};
20use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
21use std::{
22 collections::BTreeSet,
23 fs,
24 io::{BufWriter, Write},
25 path::{Path, PathBuf},
26 sync::Arc,
27};
28use url::Url;
29
30pub type StorageInfo = HashMap<U256, U256>;
31
32#[derive(Clone, Debug)]
34pub struct BlockchainDb {
35 db: Arc<MemDb>,
37 meta: Arc<RwLock<BlockchainDbMeta>>,
39 cache: Arc<JsonBlockCacheDB>,
41}
42
43impl BlockchainDb {
44 pub fn new(meta: BlockchainDbMeta, cache_path: Option<PathBuf>) -> Self {
55 Self::new_db(meta, cache_path, false)
56 }
57
58 pub fn new_skip_check(meta: BlockchainDbMeta, cache_path: Option<PathBuf>) -> Self {
70 Self::new_db(meta, cache_path, true)
71 }
72
73 fn new_db(meta: BlockchainDbMeta, cache_path: Option<PathBuf>, skip_check: bool) -> Self {
74 trace!(target: "forge::cache", cache=?cache_path, "initialising blockchain db");
75 let cache = cache_path
77 .as_ref()
78 .and_then(|p| {
79 JsonBlockCacheDB::load(p).ok().filter(|cache| {
80 if skip_check {
81 return true;
82 }
83 let mut existing = cache.meta().write();
84 existing.hosts.extend(meta.hosts.clone());
85 if meta != *existing {
86 warn!(target: "cache", "non-matching block metadata");
87 false
88 } else {
89 true
90 }
91 })
92 })
93 .unwrap_or_else(|| JsonBlockCacheDB::new(Arc::new(RwLock::new(meta)), cache_path));
94
95 Self { db: Arc::clone(cache.db()), meta: Arc::clone(cache.meta()), cache: Arc::new(cache) }
96 }
97
98 pub fn accounts(&self) -> &RwLock<AddressHashMap<AccountInfo>> {
100 &self.db.accounts
101 }
102
103 pub fn storage(&self) -> &RwLock<AddressHashMap<StorageInfo>> {
105 &self.db.storage
106 }
107
108 pub fn block_hashes(&self) -> &RwLock<HashMap<U256, B256>> {
110 &self.db.block_hashes
111 }
112
113 pub const fn meta(&self) -> &Arc<RwLock<BlockchainDbMeta>> {
115 &self.meta
116 }
117
118 pub const fn cache(&self) -> &Arc<JsonBlockCacheDB> {
120 &self.cache
121 }
122
123 pub const fn db(&self) -> &Arc<MemDb> {
125 &self.db
126 }
127}
128
129#[derive(Clone, Debug, Default, Eq, Serialize)]
131pub struct BlockchainDbMeta {
132 #[serde(default, skip_serializing_if = "Option::is_none")]
134 pub chain: Option<Chain>,
135 pub block_env: BlockEnv,
137 pub hosts: BTreeSet<String>,
139}
140
141impl BlockchainDbMeta {
142 pub fn new(block_env: BlockEnv, url: String) -> Self {
144 let host = Url::parse(&url)
145 .ok()
146 .and_then(|url| url.host().map(|host| host.to_string()))
147 .unwrap_or(url);
148
149 Self { chain: None, block_env, hosts: BTreeSet::from([host]) }
150 }
151
152 pub fn with_block<T: TransactionResponse, H: BlockHeader>(
154 mut self,
155 block: &alloy_rpc_types::Block<T, H>,
156 ) -> Self {
157 let blob_base_fee_update_fraction =
158 self.chain.map_or(BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE, |chain| {
159 match EthereumHardfork::from_chain_and_timestamp(chain, block.header.timestamp()) {
160 Some(EthereumHardfork::Cancun) => BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN,
161 _ => BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE,
162 }
163 });
164
165 self.block_env = BlockEnv {
166 number: U256::from(block.header.number()),
167 beneficiary: block.header.beneficiary(),
168 timestamp: U256::from(block.header.timestamp()),
169 difficulty: U256::from(block.header.difficulty()),
170 basefee: block.header.base_fee_per_gas().unwrap_or_default(),
171 gas_limit: block.header.gas_limit(),
172 prevrandao: block.header.mix_hash(),
173 blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(
174 block.header.excess_blob_gas().unwrap_or_default(),
175 blob_base_fee_update_fraction,
176 )),
177 };
178
179 self
180 }
181
182 pub fn with_url(mut self, url: &str) -> Self {
184 let host = Url::parse(url)
185 .ok()
186 .and_then(|url| url.host().map(|host| host.to_string()))
187 .unwrap_or(url.to_string());
188 self.hosts.insert(host);
189 self
190 }
191
192 pub fn set_chain(mut self, chain: Chain) -> Self {
194 self.chain = Some(chain);
195 self
196 }
197
198 pub fn set_block_env(mut self, block_env: revm::context::BlockEnv) -> Self {
200 self.block_env = block_env;
201 self
202 }
203}
204
205impl PartialEq for BlockchainDbMeta {
208 fn eq(&self, other: &Self) -> bool {
209 self.block_env == other.block_env
210 }
211}
212
213impl<'de> Deserialize<'de> for BlockchainDbMeta {
214 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
215 where
216 D: Deserializer<'de>,
217 {
218 struct BlockEnvBackwardsCompat {
224 inner: revm::context::BlockEnv,
225 }
226
227 impl<'de> Deserialize<'de> for BlockEnvBackwardsCompat {
228 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
229 where
230 D: Deserializer<'de>,
231 {
232 let mut value = serde_json::Value::deserialize(deserializer)?;
233
234 if let Some(obj) = value.as_object_mut() {
236 let default_value =
237 serde_json::to_value(revm::context::BlockEnv::default()).unwrap();
238 for (key, value) in default_value.as_object().unwrap() {
239 if !obj.contains_key(key) {
240 obj.insert(key.to_string(), value.clone());
241 }
242 }
243 }
244
245 let cfg_env: revm::context::BlockEnv =
246 serde_json::from_value(value).map_err(serde::de::Error::custom)?;
247 Ok(Self { inner: cfg_env })
248 }
249 }
250
251 #[derive(Deserialize)]
253 struct Meta {
254 chain: Option<Chain>,
255 block_env: BlockEnvBackwardsCompat,
256 #[serde(alias = "host")]
258 hosts: Hosts,
259 }
260
261 #[derive(Deserialize)]
262 #[serde(untagged)]
263 enum Hosts {
264 Multi(BTreeSet<String>),
265 Single(String),
266 }
267
268 let Meta { chain, block_env, hosts } = Meta::deserialize(deserializer)?;
269 Ok(Self {
270 chain,
271 block_env: block_env.inner,
272 hosts: match hosts {
273 Hosts::Multi(hosts) => hosts,
274 Hosts::Single(host) => BTreeSet::from([host]),
275 },
276 })
277 }
278}
279
280#[derive(Debug, Default)]
283pub struct MemDb {
284 pub accounts: RwLock<AddressHashMap<AccountInfo>>,
286 pub storage: RwLock<AddressHashMap<StorageInfo>>,
288 pub block_hashes: RwLock<HashMap<U256, B256>>,
290}
291
292impl MemDb {
293 pub fn clear(&self) {
295 self.accounts.write().clear();
296 self.storage.write().clear();
297 self.block_hashes.write().clear();
298 }
299
300 pub fn do_insert_account(&self, address: Address, account: AccountInfo) {
302 self.accounts.write().insert(address, account);
303 }
304
305 pub fn do_commit(&self, changes: HashMap<Address, Account>) {
307 let mut storage = self.storage.write();
308 let mut accounts = self.accounts.write();
309 for (add, mut acc) in changes {
310 if acc.is_empty() || acc.is_selfdestructed() {
311 accounts.remove(&add);
312 storage.remove(&add);
313 } else {
314 if let Some(code_hash) = acc
316 .info
317 .code
318 .as_ref()
319 .filter(|code| !code.is_empty())
320 .map(|code| code.hash_slow())
321 {
322 acc.info.code_hash = code_hash;
323 } else if acc.info.code_hash.is_zero() {
324 acc.info.code_hash = KECCAK_EMPTY;
325 }
326 accounts.insert(add, acc.info);
327
328 let acc_storage = storage.entry(add).or_default();
329 if acc.status.contains(AccountStatus::Created) {
330 acc_storage.clear();
331 }
332 for (index, value) in acc.storage {
333 if value.present_value().is_zero() {
334 acc_storage.remove(&index);
335 } else {
336 acc_storage.insert(index, value.present_value());
337 }
338 }
339 if acc_storage.is_empty() {
340 storage.remove(&add);
341 }
342 }
343 }
344 }
345}
346
347impl Clone for MemDb {
348 fn clone(&self) -> Self {
349 Self {
350 storage: RwLock::new(self.storage.read().clone()),
351 accounts: RwLock::new(self.accounts.read().clone()),
352 block_hashes: RwLock::new(self.block_hashes.read().clone()),
353 }
354 }
355}
356
357impl DatabaseCommit for MemDb {
358 fn commit(&mut self, changes: HashMap<Address, Account>) {
359 self.do_commit(changes)
360 }
361}
362
363#[derive(Debug)]
365pub struct JsonBlockCacheDB {
366 cache_path: Option<PathBuf>,
370 data: JsonBlockCacheData,
372}
373
374impl JsonBlockCacheDB {
375 fn new(meta: Arc<RwLock<BlockchainDbMeta>>, cache_path: Option<PathBuf>) -> Self {
377 Self { cache_path, data: JsonBlockCacheData { meta, data: Arc::new(Default::default()) } }
378 }
379
380 pub fn load(path: impl Into<PathBuf>) -> eyre::Result<Self> {
387 let path = path.into();
388 trace!(target: "cache", ?path, "reading json cache");
389 let contents = std::fs::read_to_string(&path).inspect_err(|err| {
390 warn!(?err, ?path, "Failed to read cache file");
391 })?;
392 let data = serde_json::from_str(&contents).inspect_err(|err| {
393 warn!(target: "cache", ?err, ?path, "Failed to deserialize cache data");
394 })?;
395 trace!(target: "cache", ?path, "read json cache");
396 Ok(Self { cache_path: Some(path), data })
397 }
398
399 pub const fn db(&self) -> &Arc<MemDb> {
401 &self.data.data
402 }
403
404 pub const fn meta(&self) -> &Arc<RwLock<BlockchainDbMeta>> {
406 &self.data.meta
407 }
408
409 pub const fn is_transient(&self) -> bool {
411 self.cache_path.is_none()
412 }
413
414 #[instrument(level = "warn", skip_all, fields(path = ?self.cache_path))]
416 pub fn flush(&self) {
417 let Some(path) = &self.cache_path else { return };
418 self.flush_to(path.as_path());
419 }
420
421 pub fn flush_to(&self, cache_path: &Path) {
423 let path: &Path = cache_path;
424
425 trace!(target: "cache", "saving json cache");
426
427 if let Some(parent) = path.parent() {
428 let _ = fs::create_dir_all(parent);
429 }
430
431 let file = match fs::File::create(path) {
432 Ok(file) => file,
433 Err(e) => return warn!(target: "cache", %e, "Failed to open json cache for writing"),
434 };
435
436 let mut writer = BufWriter::new(file);
437 if let Err(e) = serde_json::to_writer(&mut writer, &self.data) {
438 return warn!(target: "cache", %e, "Failed to write to json cache");
439 }
440 if let Err(e) = writer.flush() {
441 return warn!(target: "cache", %e, "Failed to flush to json cache");
442 }
443
444 trace!(target: "cache", "saved json cache");
445 }
446
447 pub fn cache_path(&self) -> Option<&Path> {
449 self.cache_path.as_deref()
450 }
451}
452
453#[derive(Debug)]
458pub struct JsonBlockCacheData {
459 pub meta: Arc<RwLock<BlockchainDbMeta>>,
460 pub data: Arc<MemDb>,
461}
462
463impl Serialize for JsonBlockCacheData {
464 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
465 where
466 S: Serializer,
467 {
468 let mut map = serializer.serialize_map(Some(4))?;
469
470 map.serialize_entry("meta", &self.meta.read().clone())?;
471 map.serialize_entry("accounts", &self.data.accounts.read().clone())?;
472 map.serialize_entry("storage", &self.data.storage.read().clone())?;
473 map.serialize_entry("block_hashes", &self.data.block_hashes.read().clone())?;
474
475 map.end()
476 }
477}
478
479impl<'de> Deserialize<'de> for JsonBlockCacheData {
480 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
481 where
482 D: Deserializer<'de>,
483 {
484 #[derive(Deserialize)]
485 struct Data {
486 meta: BlockchainDbMeta,
487 accounts: AddressHashMap<AccountInfo>,
488 storage: AddressHashMap<HashMap<U256, U256>>,
489 block_hashes: HashMap<U256, B256>,
490 }
491
492 let Data { meta, accounts, storage, block_hashes } = Data::deserialize(deserializer)?;
493
494 Ok(Self {
495 meta: Arc::new(RwLock::new(meta)),
496 data: Arc::new(MemDb {
497 accounts: RwLock::new(accounts),
498 storage: RwLock::new(storage),
499 block_hashes: RwLock::new(block_hashes),
500 }),
501 })
502 }
503}
504
505#[derive(Debug)]
510pub struct FlushJsonBlockCacheDB(pub Arc<JsonBlockCacheDB>);
511
512impl Drop for FlushJsonBlockCacheDB {
513 fn drop(&mut self) {
514 trace!(target: "fork::cache", "flushing cache");
515 self.0.flush();
516 trace!(target: "fork::cache", "flushed cache");
517 }
518}
519
520#[cfg(test)]
521mod tests {
522 use super::*;
523
524 #[test]
525 fn can_deserialize_cache() {
526 let s = r#"{
527 "meta": {
528 "cfg_env": {
529 "chain_id": 1337,
530 "perf_analyse_created_bytecodes": "Analyse",
531 "limit_contract_code_size": 18446744073709551615,
532 "memory_limit": 4294967295,
533 "disable_block_gas_limit": false,
534 "disable_eip3607": false,
535 "disable_base_fee": false
536 },
537 "block_env": {
538 "number": 15547871,
539 "coinbase": "0x0000000000000000000000000000000000000000",
540 "timestamp": 1663351871,
541 "difficulty": "0x0",
542 "basefee": 12448539171,
543 "gas_limit": 30000000,
544 "prevrandao": "0x0000000000000000000000000000000000000000000000000000000000000000"
545 },
546 "hosts": [
547 "eth-mainnet.alchemyapi.io"
548 ]
549 },
550 "accounts": {
551 "0xb8ffc3cd6e7cf5a098a1c92f48009765b24088dc": {
552 "balance": "0x0",
553 "nonce": 10,
554 "code_hash": "0x3ac64c95eedf82e5d821696a12daac0e1b22c8ee18a9fd688b00cfaf14550aad",
555 "code": {
556 "LegacyAnalyzed": {
557 "bytecode": "0x00",
558 "original_len": 0,
559 "jump_table": {
560 "order": "bitvec::order::Lsb0",
561 "head": {
562 "width": 8,
563 "index": 0
564 },
565 "bits": 1,
566 "data": [0]
567 }
568 }
569 }
570 }
571 },
572 "storage": {
573 "0xa354f35829ae975e850e23e9615b11da1b3dc4de": {
574 "0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e564": "0x5553444320795661756c74000000000000000000000000000000000000000000",
575 "0x10": "0x37fd60ff8346",
576 "0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563": "0xb",
577 "0x6": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
578 "0x5": "0x36ff5b93162e",
579 "0x14": "0x29d635a8e000",
580 "0x11": "0x63224c73",
581 "0x2": "0x6"
582 }
583 },
584 "block_hashes": {
585 "0xed3deb": "0xbf7be3174b261ea3c377b6aba4a1e05d5fae7eee7aab5691087c20cf353e9877",
586 "0xed3de9": "0xba1c3648e0aee193e7d00dffe4e9a5e420016b4880455641085a4731c1d32eef",
587 "0xed3de8": "0x61d1491c03a9295fb13395cca18b17b4fa5c64c6b8e56ee9cc0a70c3f6cf9855",
588 "0xed3de7": "0xb54560b5baeccd18350d56a3bee4035432294dc9d2b7e02f157813e1dee3a0be",
589 "0xed3dea": "0x816f124480b9661e1631c6ec9ee39350bda79f0cbfc911f925838d88e3d02e4b"
590 }
591}"#;
592
593 let cache: JsonBlockCacheData = serde_json::from_str(s).unwrap();
594 assert_eq!(cache.data.accounts.read().len(), 1);
595 assert_eq!(cache.data.storage.read().len(), 1);
596 assert_eq!(cache.data.block_hashes.read().len(), 5);
597
598 let _s = serde_json::to_string(&cache).unwrap();
599 }
600
601 #[test]
602 fn can_deserialize_cache_post_4844() {
603 let s = r#"{
604 "meta": {
605 "cfg_env": {
606 "chain_id": 1,
607 "kzg_settings": "Default",
608 "perf_analyse_created_bytecodes": "Analyse",
609 "limit_contract_code_size": 18446744073709551615,
610 "memory_limit": 134217728,
611 "disable_block_gas_limit": false,
612 "disable_eip3607": true,
613 "disable_base_fee": false,
614 "optimism": false
615 },
616 "block_env": {
617 "number": 18651580,
618 "coinbase": "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97",
619 "timestamp": 1700950019,
620 "gas_limit": 30000000,
621 "basefee": 26886078239,
622 "difficulty": "0xc6b1a299886016dea3865689f8393b9bf4d8f4fe8c0ad25f0058b3569297c057",
623 "prevrandao": "0xc6b1a299886016dea3865689f8393b9bf4d8f4fe8c0ad25f0058b3569297c057",
624 "blob_excess_gas_and_price": {
625 "excess_blob_gas": 0,
626 "blob_gasprice": 1
627 }
628 },
629 "hosts": [
630 "eth-mainnet.alchemyapi.io"
631 ]
632 },
633 "accounts": {
634 "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97": {
635 "balance": "0x8e0c373cfcdfd0eb",
636 "nonce": 128912,
637 "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
638 "code": {
639 "LegacyAnalyzed": {
640 "bytecode": "0x00",
641 "original_len": 0,
642 "jump_table": {
643 "order": "bitvec::order::Lsb0",
644 "head": {
645 "width": 8,
646 "index": 0
647 },
648 "bits": 1,
649 "data": [0]
650 }
651 }
652 }
653 }
654 },
655 "storage": {},
656 "block_hashes": {}
657}"#;
658
659 let cache: JsonBlockCacheData = serde_json::from_str(s).unwrap();
660 assert_eq!(cache.data.accounts.read().len(), 1);
661
662 let _s = serde_json::to_string(&cache).unwrap();
663 }
664
665 #[test]
666 fn can_return_cache_path_if_set() {
667 let cache_db = JsonBlockCacheDB::new(
669 Arc::new(RwLock::new(BlockchainDbMeta::default())),
670 Some(PathBuf::from("/tmp/foo")),
671 );
672 assert_eq!(Some(Path::new("/tmp/foo")), cache_db.cache_path());
673
674 let cache_db =
676 JsonBlockCacheDB::new(Arc::new(RwLock::new(BlockchainDbMeta::default())), None);
677 assert_eq!(None, cache_db.cache_path());
678 }
679}