foundry-fork-db 0.27.0

Fork database used by Foundry
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
//! Cache related abstraction

use alloy_chains::Chain;
use alloy_primitives::{Address, B256, U256, map::U256Map};
use parking_lot::RwLock;
use revm::{
    DatabaseCommit,
    context::BlockEnv,
    primitives::{KECCAK_EMPTY, StorageKeyMap, map::AddressHashMap},
    state::{Account, AccountInfo, AccountStatus},
};
use serde::{
    Deserialize, Deserializer, Serialize, Serializer, de::DeserializeOwned, ser::SerializeMap,
};
#[cfg(not(feature = "zstd"))]
use std::io::{BufWriter, Write};
use std::{
    collections::BTreeSet,
    fs,
    path::{Path, PathBuf},
    sync::Arc,
};
use url::Url;
#[cfg(feature = "zstd")]
use zstd::{Encoder, decode_all};

pub type StorageInfo = StorageKeyMap<U256>;

/// Zstd frame magic number: `0x28B52FFD` (little-endian).
const ZSTD_FRAME_MAGIC: [u8; 4] = [0x28, 0xB5, 0x2F, 0xFD];

/// A shareable Block database
#[derive(Clone, Debug)]
pub struct BlockchainDb<B = BlockEnv> {
    /// Contains all the data
    db: Arc<MemDb>,
    /// metadata of the current config
    meta: Arc<RwLock<BlockchainDbMeta<B>>>,
    /// the cache that can be flushed
    cache: Arc<JsonBlockCacheDB<B>>,
}

impl<B: ForkBlockEnv> BlockchainDb<B> {
    /// Creates a new instance of the [BlockchainDb].
    ///
    /// If a `cache_path` is provided it attempts to load a previously stored [JsonBlockCacheData]
    /// and will try to use the cached entries it holds.
    ///
    /// This will return a new and empty [MemDb] if
    ///   - `cache_path` is `None`
    ///   - the file the `cache_path` points to, does not exist
    ///   - the file contains malformed data, or if it couldn't be read
    ///   - the provided `meta` differs from [BlockchainDbMeta] that's stored on disk
    pub fn new(meta: BlockchainDbMeta<B>, cache_path: Option<PathBuf>) -> Self {
        Self::new_db(meta, cache_path, false)
    }

    /// Creates a new instance of the [BlockchainDb] and skips check when comparing meta
    /// This is useful for offline-start mode when we don't want to fetch metadata of `block`.
    ///
    /// if a `cache_path` is provided it attempts to load a previously stored [JsonBlockCacheData]
    /// and will try to use the cached entries it holds.
    ///
    /// This will return a new and empty [MemDb] if
    ///   - `cache_path` is `None`
    ///   - the file the `cache_path` points to, does not exist
    ///   - the file contains malformed data, or if it couldn't be read
    ///   - the provided `meta` differs from [BlockchainDbMeta] that's stored on disk
    pub fn new_skip_check(meta: BlockchainDbMeta<B>, cache_path: Option<PathBuf>) -> Self {
        Self::new_db(meta, cache_path, true)
    }

    fn new_db(meta: BlockchainDbMeta<B>, cache_path: Option<PathBuf>, skip_check: bool) -> Self {
        trace!(target: "forge::cache", cache=?cache_path, "initialising blockchain db");
        // read cache and check if metadata matches
        let cache = cache_path
            .as_ref()
            .and_then(|p| {
                JsonBlockCacheDB::load(p).ok().filter(|cache| {
                    if skip_check {
                        return true;
                    }
                    let mut existing = cache.meta().write();
                    existing.hosts.extend(meta.hosts.clone());
                    if meta == *existing {
                        true
                    } else {
                        warn!(target: "cache", "non-matching block metadata");
                        false
                    }
                })
            })
            .unwrap_or_else(|| JsonBlockCacheDB::new(Arc::new(RwLock::new(meta)), cache_path));

        Self { db: Arc::clone(cache.db()), meta: Arc::clone(cache.meta()), cache: Arc::new(cache) }
    }

    /// Returns the map that holds the account related info
    pub fn accounts(&self) -> &RwLock<AddressHashMap<AccountInfo>> {
        &self.db.accounts
    }

    /// Returns the map that holds the storage related info
    pub fn storage(&self) -> &RwLock<AddressHashMap<StorageInfo>> {
        &self.db.storage
    }

    /// Returns the map that holds all the block hashes
    pub fn block_hashes(&self) -> &RwLock<U256Map<B256>> {
        &self.db.block_hashes
    }

    /// Returns the Env related metadata
    pub const fn meta(&self) -> &Arc<RwLock<BlockchainDbMeta<B>>> {
        &self.meta
    }

    /// Returns the inner cache
    pub const fn cache(&self) -> &Arc<JsonBlockCacheDB<B>> {
        &self.cache
    }

    /// Returns the underlying storage
    pub const fn db(&self) -> &Arc<MemDb> {
        &self.db
    }
}

/// Marker trait for block environment types that can be used with the forking backend.
///
/// Automatically implemented for any `B: Serialize + DeserializeOwned + Default + Clone +
/// PartialEq + Send + Sync + 'static`.
pub trait ForkBlockEnv:
    Serialize + DeserializeOwned + Default + Clone + PartialEq + Send + Sync + 'static
{
}

impl<B: Serialize + DeserializeOwned + Default + Clone + PartialEq + Send + Sync + 'static>
    ForkBlockEnv for B
{
}

/// relevant identifying markers in the context of [BlockchainDb]
#[derive(Clone, Debug, Default, Eq)]
pub struct BlockchainDbMeta<B> {
    /// The chain of the blockchain of the block environment
    pub chain: Option<Chain>,
    /// The block environment
    pub block_env: B,
    /// All the hosts used to connect to
    pub hosts: BTreeSet<String>,
}

impl<B> BlockchainDbMeta<B> {
    /// Creates a new instance
    pub fn new(block_env: B, url: String) -> Self {
        let host = Url::parse(&url)
            .ok()
            .and_then(|url| url.host().map(|host| host.to_string()))
            .unwrap_or(url);

        Self { chain: None, block_env, hosts: BTreeSet::from([host]) }
    }

    /// Infers the host from the provided url and adds it to the set of hosts
    pub fn with_url(mut self, url: &str) -> Self {
        let host = Url::parse(url)
            .ok()
            .and_then(|url| url.host().map(|host| host.to_string()))
            .unwrap_or(url.to_string());
        self.hosts.insert(host);
        self
    }

    /// Sets the [Chain] of this instance
    pub const fn set_chain(mut self, chain: Chain) -> Self {
        self.chain = Some(chain);
        self
    }

    /// Sets the block environment of this instance
    pub fn set_block_env(mut self, block_env: B) -> Self {
        self.block_env = block_env;
        self
    }
}

// ignore hosts to not invalidate the cache when different endpoints are used, as it's commonly the
// case for http vs ws endpoints
impl<B: PartialEq> PartialEq for BlockchainDbMeta<B> {
    fn eq(&self, other: &Self) -> bool {
        self.block_env == other.block_env
    }
}

impl<B: Serialize> Serialize for BlockchainDbMeta<B> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;

        let field_count = if self.chain.is_some() { 3 } else { 2 };
        let mut s = serializer.serialize_struct("BlockchainDbMeta", field_count)?;
        if let Some(chain) = &self.chain {
            s.serialize_field("chain", chain)?;
        }
        s.serialize_field("block_env", &self.block_env)?;
        s.serialize_field("hosts", &self.hosts)?;
        s.end()
    }
}

/// A backwards compatible representation of a block environment type `B`.
///
/// This prevents deserialization errors of cache files caused by breaking changes to the
/// block environment, for example enabling an optional feature that adds new fields.
/// By filling in missing fields from `B::default()` we can prevent cache file issues.
struct BlockEnvBackwardsCompat<B> {
    inner: B,
}

impl<'de, B: DeserializeOwned + Default + Serialize> Deserialize<'de>
    for BlockEnvBackwardsCompat<B>
{
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let mut value = serde_json::Value::deserialize(deserializer)?;

        // we check for any missing fields here
        if let Some(obj) = value.as_object_mut() {
            let default_value = serde_json::to_value(B::default()).unwrap();
            for (key, value) in default_value.as_object().unwrap() {
                if !obj.contains_key(key) {
                    obj.insert(key.clone(), value.clone());
                }
            }
        }

        let inner: B = serde_json::from_value(value).map_err(serde::de::Error::custom)?;
        Ok(Self { inner })
    }
}

#[derive(Deserialize)]
#[serde(untagged)]
enum Hosts {
    Multi(BTreeSet<String>),
    Single(String),
}

impl From<Hosts> for BTreeSet<String> {
    fn from(hosts: Hosts) -> Self {
        match hosts {
            Hosts::Multi(hosts) => hosts,
            Hosts::Single(host) => Self::from([host]),
        }
    }
}

impl<'de, B: DeserializeOwned + Default + Serialize> Deserialize<'de> for BlockchainDbMeta<B> {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(bound = "B: DeserializeOwned + Default + Serialize")]
        struct Meta<B> {
            chain: Option<Chain>,
            block_env: BlockEnvBackwardsCompat<B>,
            #[serde(alias = "host")]
            hosts: Hosts,
        }

        let Meta { chain, block_env, hosts } = Meta::deserialize(deserializer)?;
        Ok(Self { chain, block_env: block_env.inner, hosts: hosts.into() })
    }
}

/// In Memory cache containing all fetched accounts and storage slots
/// and their values from RPC
#[derive(Debug, Default)]
pub struct MemDb {
    /// Account related data
    pub accounts: RwLock<AddressHashMap<AccountInfo>>,
    /// Storage related data
    pub storage: RwLock<AddressHashMap<StorageInfo>>,
    /// All retrieved block hashes
    pub block_hashes: RwLock<U256Map<B256>>,
}

impl MemDb {
    /// Clears all data stored in this db
    pub fn clear(&self) {
        self.accounts.write().clear();
        self.storage.write().clear();
        self.block_hashes.write().clear();
    }

    // Inserts the account, replacing it if it exists already
    pub fn do_insert_account(&self, address: Address, account: AccountInfo) {
        self.accounts.write().insert(address, account);
    }

    /// The implementation of [DatabaseCommit::commit()]
    pub fn do_commit(&self, changes: AddressHashMap<Account>) {
        let mut storage = self.storage.write();
        let mut accounts = self.accounts.write();
        for (add, mut acc) in changes {
            if acc.is_empty() || acc.is_selfdestructed() {
                accounts.remove(&add);
                storage.remove(&add);
            } else {
                // insert account
                if let Some(code_hash) = acc
                    .info
                    .code
                    .as_ref()
                    .filter(|code| !code.is_empty())
                    .map(|code| code.hash_slow())
                {
                    acc.info.code_hash = code_hash;
                } else if acc.info.code_hash.is_zero() {
                    acc.info.code_hash = KECCAK_EMPTY;
                }
                accounts.insert(add, acc.info);

                let acc_storage = storage.entry(add).or_default();
                if acc.status.contains(AccountStatus::Created) {
                    acc_storage.clear();
                }
                for (index, value) in acc.storage {
                    if value.present_value().is_zero() {
                        acc_storage.remove(&index);
                    } else {
                        acc_storage.insert(index, value.present_value());
                    }
                }
                if acc_storage.is_empty() {
                    storage.remove(&add);
                }
            }
        }
    }
}

impl Clone for MemDb {
    fn clone(&self) -> Self {
        Self {
            storage: RwLock::new(self.storage.read().clone()),
            accounts: RwLock::new(self.accounts.read().clone()),
            block_hashes: RwLock::new(self.block_hashes.read().clone()),
        }
    }
}

impl DatabaseCommit for MemDb {
    fn commit(&mut self, changes: AddressHashMap<Account>) {
        self.do_commit(changes)
    }
}

/// A DB that stores the cached content in a json file
#[derive(Debug)]
pub struct JsonBlockCacheDB<B> {
    /// Where this cache file is stored.
    ///
    /// If this is a [None] then caching is disabled
    cache_path: Option<PathBuf>,
    /// Object that's stored in a json file
    data: JsonBlockCacheData<B>,
}

impl<B> JsonBlockCacheDB<B> {
    /// Creates a new instance.
    fn new(meta: Arc<RwLock<BlockchainDbMeta<B>>>, cache_path: Option<PathBuf>) -> Self {
        Self { cache_path, data: JsonBlockCacheData { meta, data: Arc::new(Default::default()) } }
    }

    /// Returns the [MemDb] it holds access to
    pub const fn db(&self) -> &Arc<MemDb> {
        &self.data.data
    }

    /// Metadata stored alongside the data
    pub const fn meta(&self) -> &Arc<RwLock<BlockchainDbMeta<B>>> {
        &self.data.meta
    }

    /// Returns `true` if this is a transient cache and nothing will be flushed
    pub const fn is_transient(&self) -> bool {
        self.cache_path.is_none()
    }

    /// Returns the cache path.
    pub fn cache_path(&self) -> Option<&Path> {
        self.cache_path.as_deref()
    }
}

impl<B: ForkBlockEnv> JsonBlockCacheDB<B> {
    /// Loads the contents of the diskmap file and returns the read object.
    ///
    /// When the `zstd` feature is enabled, this will transparently detect and decompress
    /// zstd-compressed cache files (by checking for the zstd magic bytes `0x28B52FFD`).
    /// Plain JSON files are always supported regardless of feature flags.
    ///
    /// # Errors
    /// This will fail if
    ///   - the `path` does not exist
    ///   - the format does not match [JsonBlockCacheData]
    pub fn load(path: impl Into<PathBuf>) -> eyre::Result<Self> {
        let path = path.into();
        trace!(target: "cache", ?path, "reading json cache");
        let raw = fs::read(&path).inspect_err(|err| {
            warn!(?err, ?path, "Failed to read cache file");
        })?;

        let json_bytes = maybe_decompress(&raw, &path).inspect_err(|err| {
            warn!(target: "cache", ?err, ?path, "Failed to decode cache file");
        })?;

        let data = serde_json::from_slice(&json_bytes).inspect_err(|err| {
            warn!(target: "cache", ?err, ?path, "Failed to deserialize cache data");
        })?;
        trace!(target: "cache", ?path, "read json cache");
        Ok(Self { cache_path: Some(path), data })
    }
}

/// Decompresses the raw bytes if they are zstd-compressed, otherwise returns them as-is.
///
/// When the `zstd` feature is **not** enabled and zstd-compressed data is detected, this
/// returns an error with a helpful message.
fn maybe_decompress(raw: &[u8], path: &Path) -> eyre::Result<Vec<u8>> {
    if !raw.starts_with(&ZSTD_FRAME_MAGIC) {
        return Ok(raw.to_vec());
    }

    #[cfg(feature = "zstd")]
    {
        trace!(target: "cache", ?path, "decompressing zstd cache");
        let decompressed = decode_all(raw).inspect_err(|err| {
            warn!(target: "cache", ?err, ?path, "Failed to decompress zstd cache");
        })?;
        Ok(decompressed)
    }

    #[cfg(not(feature = "zstd"))]
    {
        eyre::bail!(
            "cache file at `{}` is zstd-compressed but the `zstd` feature is not enabled",
            path.display()
        );
    }
}

impl<B: Serialize + Clone> JsonBlockCacheDB<B> {
    /// Flushes the DB to disk if caching is enabled.
    #[instrument(level = "warn", skip_all, fields(path = ?self.cache_path))]
    pub fn flush(&self) {
        let Some(path) = &self.cache_path else { return };
        self.flush_to(path.as_path());
    }

    /// Flushes the DB to a specific file.
    ///
    /// When the `zstd` feature is enabled, the cache is written as zstd-compressed JSON.
    /// Otherwise, plain JSON is written.
    pub fn flush_to(&self, cache_path: &Path) {
        let path: &Path = cache_path;

        trace!(target: "cache", "saving json cache");

        if let Some(parent) = path.parent() {
            let _ = fs::create_dir_all(parent);
        }

        let file = match fs::File::create(path) {
            Ok(file) => file,
            Err(e) => return warn!(target: "cache", %e, "Failed to open json cache for writing"),
        };

        #[cfg(feature = "zstd")]
        {
            // Default zstd compression level (3) offers a good balance of speed and ratio.
            let mut encoder = match Encoder::new(file, 3) {
                Ok(enc) => enc,
                Err(e) => return warn!(target: "cache", %e, "Failed to create zstd encoder"),
            };
            if let Err(e) = serde_json::to_writer(&mut encoder, &self.data) {
                return warn!(target: "cache", %e, "Failed to write to zstd json cache");
            }
            if let Err(e) = encoder.finish() {
                return warn!(target: "cache", %e, "Failed to finish zstd compression");
            }
        }

        #[cfg(not(feature = "zstd"))]
        {
            let mut writer = BufWriter::new(file);
            if let Err(e) = serde_json::to_writer(&mut writer, &self.data) {
                return warn!(target: "cache", %e, "Failed to write to json cache");
            }
            if let Err(e) = writer.flush() {
                return warn!(target: "cache", %e, "Failed to flush to json cache");
            }
        }

        trace!(target: "cache", "saved json cache");
    }
}

/// The Data the [JsonBlockCacheDB] can read and flush
///
/// This will be deserialized in a JSON object with the keys:
/// `["meta", "accounts", "storage", "block_hashes"]`
#[derive(Debug)]
pub struct JsonBlockCacheData<B> {
    pub meta: Arc<RwLock<BlockchainDbMeta<B>>>,
    pub data: Arc<MemDb>,
}

impl<B: Serialize + Clone> Serialize for JsonBlockCacheData<B> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut map = serializer.serialize_map(Some(4))?;

        map.serialize_entry("meta", &self.meta.read().clone())?;
        map.serialize_entry("accounts", &self.data.accounts.read().clone())?;
        map.serialize_entry("storage", &self.data.storage.read().clone())?;
        map.serialize_entry("block_hashes", &self.data.block_hashes.read().clone())?;

        map.end()
    }
}

impl<'de, B: DeserializeOwned + Default + Serialize> Deserialize<'de> for JsonBlockCacheData<B> {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        struct Data<B> {
            meta: B,
            accounts: AddressHashMap<AccountInfo>,
            storage: AddressHashMap<StorageInfo>,
            block_hashes: U256Map<B256>,
        }

        let Data { meta, accounts, storage, block_hashes } =
            Data::<BlockchainDbMeta<B>>::deserialize(deserializer)?;

        Ok(Self {
            meta: Arc::new(RwLock::new(meta)),
            data: Arc::new(MemDb {
                accounts: RwLock::new(accounts),
                storage: RwLock::new(storage),
                block_hashes: RwLock::new(block_hashes),
            }),
        })
    }
}

/// A type that flushes a `JsonBlockCacheDB` on drop
///
/// This type intentionally does not implement `Clone` since it's intended that there's only once
/// instance that will flush the cache.
#[derive(Debug)]
pub struct FlushJsonBlockCacheDB<B: Serialize + Clone>(pub Arc<JsonBlockCacheDB<B>>);

impl<B: Serialize + Clone> Drop for FlushJsonBlockCacheDB<B> {
    fn drop(&mut self) {
        trace!(target: "fork::cache", "flushing cache");
        self.0.flush();
        trace!(target: "fork::cache", "flushed cache");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_deserialize_cache() {
        let s = r#"{
    "meta": {
        "cfg_env": {
            "chain_id": 1337,
            "perf_analyse_created_bytecodes": "Analyse",
            "limit_contract_code_size": 18446744073709551615,
            "memory_limit": 4294967295,
            "disable_block_gas_limit": false,
            "disable_eip3607": false,
            "disable_base_fee": false
        },
        "block_env": {
            "number": 15547871,
            "coinbase": "0x0000000000000000000000000000000000000000",
            "timestamp": 1663351871,
            "difficulty": "0x0",
            "basefee": 12448539171,
            "gas_limit": 30000000,
            "prevrandao": "0x0000000000000000000000000000000000000000000000000000000000000000"
        },
        "hosts": [
            "eth-mainnet.alchemyapi.io"
        ]
    },
    "accounts": {
        "0xb8ffc3cd6e7cf5a098a1c92f48009765b24088dc": {
            "balance": "0x0",
            "nonce": 10,
            "code_hash": "0x3ac64c95eedf82e5d821696a12daac0e1b22c8ee18a9fd688b00cfaf14550aad",
            "code": {
                "LegacyAnalyzed": {
                    "bytecode": "0x00",
                    "original_len": 0,
                    "jump_table": {
                      "order": "bitvec::order::Lsb0",
                      "head": {
                        "width": 8,
                        "index": 0
                      },
                      "bits": 1,
                      "data": [0]
                    }
                }
            }
        }
    },
    "storage": {
        "0xa354f35829ae975e850e23e9615b11da1b3dc4de": {
            "0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e564": "0x5553444320795661756c74000000000000000000000000000000000000000000",
            "0x10": "0x37fd60ff8346",
            "0x290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563": "0xb",
            "0x6": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
            "0x5": "0x36ff5b93162e",
            "0x14": "0x29d635a8e000",
            "0x11": "0x63224c73",
            "0x2": "0x6"
        }
    },
    "block_hashes": {
        "0xed3deb": "0xbf7be3174b261ea3c377b6aba4a1e05d5fae7eee7aab5691087c20cf353e9877",
        "0xed3de9": "0xba1c3648e0aee193e7d00dffe4e9a5e420016b4880455641085a4731c1d32eef",
        "0xed3de8": "0x61d1491c03a9295fb13395cca18b17b4fa5c64c6b8e56ee9cc0a70c3f6cf9855",
        "0xed3de7": "0xb54560b5baeccd18350d56a3bee4035432294dc9d2b7e02f157813e1dee3a0be",
        "0xed3dea": "0x816f124480b9661e1631c6ec9ee39350bda79f0cbfc911f925838d88e3d02e4b"
    }
}"#;

        let cache: JsonBlockCacheData<BlockEnv> = serde_json::from_str(s).unwrap();
        assert_eq!(cache.data.accounts.read().len(), 1);
        assert_eq!(cache.data.storage.read().len(), 1);
        assert_eq!(cache.data.block_hashes.read().len(), 5);

        let _s = serde_json::to_string(&cache).unwrap();
    }

    #[test]
    fn can_deserialize_cache_post_4844() {
        let s = r#"{
    "meta": {
        "cfg_env": {
            "chain_id": 1,
            "kzg_settings": "Default",
            "perf_analyse_created_bytecodes": "Analyse",
            "limit_contract_code_size": 18446744073709551615,
            "memory_limit": 134217728,
            "disable_block_gas_limit": false,
            "disable_eip3607": true,
            "disable_base_fee": false,
            "optimism": false
        },
        "block_env": {
            "number": 18651580,
            "coinbase": "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97",
            "timestamp": 1700950019,
            "gas_limit": 30000000,
            "basefee": 26886078239,
            "difficulty": "0xc6b1a299886016dea3865689f8393b9bf4d8f4fe8c0ad25f0058b3569297c057",
            "prevrandao": "0xc6b1a299886016dea3865689f8393b9bf4d8f4fe8c0ad25f0058b3569297c057",
            "blob_excess_gas_and_price": {
                "excess_blob_gas": 0,
                "blob_gasprice": 1
            }
        },
        "hosts": [
            "eth-mainnet.alchemyapi.io"
        ]
    },
    "accounts": {
        "0x4838b106fce9647bdf1e7877bf73ce8b0bad5f97": {
            "balance": "0x8e0c373cfcdfd0eb",
            "nonce": 128912,
            "code_hash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
            "code": {
                "LegacyAnalyzed": {
                    "bytecode": "0x00",
                    "original_len": 0,
                    "jump_table": {
                      "order": "bitvec::order::Lsb0",
                      "head": {
                        "width": 8,
                        "index": 0
                      },
                      "bits": 1,
                      "data": [0]
                    }
                }
            }
        }
    },
    "storage": {},
    "block_hashes": {}
}"#;

        let cache: JsonBlockCacheData<BlockEnv> = serde_json::from_str(s).unwrap();
        assert_eq!(cache.data.accounts.read().len(), 1);

        let _s = serde_json::to_string(&cache).unwrap();
    }

    #[test]
    fn roundtrip_meta_block_env() {
        let meta = BlockchainDbMeta {
            chain: Some(Chain::mainnet()),
            block_env: BlockEnv { number: U256::from(1u64), ..Default::default() },
            hosts: BTreeSet::from(["eth-mainnet.alchemyapi.io".to_string()]),
        };
        let json = serde_json::to_string(&meta).unwrap();
        let recovered: BlockchainDbMeta<BlockEnv> = serde_json::from_str(&json).unwrap();
        assert_eq!(meta, recovered);
    }

    #[test]
    fn can_return_cache_path_if_set() {
        // set
        let cache_db = JsonBlockCacheDB::<BlockEnv>::new(
            Arc::new(RwLock::new(BlockchainDbMeta::default())),
            Some(PathBuf::from("/tmp/foo")),
        );
        assert_eq!(Some(Path::new("/tmp/foo")), cache_db.cache_path());

        // unset
        let cache_db = JsonBlockCacheDB::<BlockEnv>::new(
            Arc::new(RwLock::new(BlockchainDbMeta::default())),
            None,
        );
        assert_eq!(None, cache_db.cache_path());
    }

    #[test]
    fn is_zstd_detection() {
        // zstd magic bytes
        assert!([0x28, 0xB5, 0x2F, 0xFD, 0x00].starts_with(&ZSTD_FRAME_MAGIC));
        // plain JSON
        assert!(!b"{\"meta\":{}}".starts_with(&ZSTD_FRAME_MAGIC));
        // too short
        assert!(![0x28, 0xB5].starts_with(&ZSTD_FRAME_MAGIC));
        // empty
        assert!(![].starts_with(&ZSTD_FRAME_MAGIC));
    }

    #[test]
    fn flush_and_load_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.json");

        let meta = BlockchainDbMeta::<BlockEnv>::default();
        let db = BlockchainDb::new(meta, Some(path.clone()));

        // Insert some data
        db.accounts().write().insert(
            Address::ZERO,
            AccountInfo { balance: U256::from(42u64), nonce: 1, ..Default::default() },
        );

        db.cache().flush();
        assert!(path.exists());

        let raw = fs::read(&path).unwrap();
        #[cfg(feature = "zstd")]
        assert!(
            raw.starts_with(&ZSTD_FRAME_MAGIC),
            "should be zstd-compressed with the zstd feature"
        );
        #[cfg(not(feature = "zstd"))]
        assert!(!raw.starts_with(&ZSTD_FRAME_MAGIC), "should be plain JSON without zstd feature");

        // Reload
        let loaded = JsonBlockCacheDB::<BlockEnv>::load(&path).unwrap();
        assert_eq!(loaded.db().accounts.read().len(), 1);
        assert_eq!(loaded.db().accounts.read()[&Address::ZERO].balance, U256::from(42u64));
    }

    #[cfg(not(feature = "zstd"))]
    #[test]
    fn load_errors_on_zstd_data_without_feature() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.json.zst");

        fs::write(&path, [0x28, 0xB5, 0x2F, 0xFD, 0x00]).unwrap();

        let err = JsonBlockCacheDB::<BlockEnv>::load(&path).unwrap_err();
        assert!(err.to_string().contains("zstd-compressed but the `zstd` feature is not enabled"));
    }

    #[cfg(feature = "zstd")]
    #[test]
    fn zstd_flush_and_load_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.json.zst");

        let meta = BlockchainDbMeta::<BlockEnv>::default();
        let db = BlockchainDb::new(meta, Some(path.clone()));

        db.accounts().write().insert(
            Address::ZERO,
            AccountInfo { balance: U256::from(100u64), nonce: 5, ..Default::default() },
        );
        db.storage().write().insert(Address::ZERO, Default::default());

        db.cache().flush();
        assert!(path.exists());

        // Verify on-disk bytes have zstd magic
        let raw = fs::read(&path).unwrap();
        assert!(
            raw.starts_with(&ZSTD_FRAME_MAGIC),
            "should be zstd-compressed with the zstd feature"
        );

        // Reload and verify data integrity
        let loaded = JsonBlockCacheDB::<BlockEnv>::load(&path).unwrap();
        assert_eq!(loaded.db().accounts.read().len(), 1);
        assert_eq!(loaded.db().accounts.read()[&Address::ZERO].nonce, 5);
    }

    #[cfg(feature = "zstd")]
    #[test]
    fn zstd_can_load_plain_json() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("cache.json");

        // Write plain JSON manually
        let meta = BlockchainDbMeta::<BlockEnv>::default();
        let data = JsonBlockCacheData::<BlockEnv> {
            meta: Arc::new(RwLock::new(meta)),
            data: Arc::new(MemDb::default()),
        };
        let json = serde_json::to_vec(&data).unwrap();
        fs::write(&path, &json).unwrap();

        // Should load fine even with zstd feature enabled
        let loaded = JsonBlockCacheDB::<BlockEnv>::load(&path).unwrap();
        assert_eq!(loaded.db().accounts.read().len(), 0);
    }
}