evm-fork-cache 0.1.0

Forked EVM state cache, snapshots, overlays, and simulation utilities for EVM search
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;

use alloy_eips::eip2930::{AccessList, AccessListItem};
use alloy_primitives::{Address, B256, Bytes, TxKind, U256};
use anyhow::{Result, anyhow};
use foundry_fork_db::{DatabaseError, SharedBackend};
use revm::{
    Context, ExecuteCommitEvm, ExecuteEvm, InspectEvm, MainBuilder, MainContext,
    context::{BlockEnv, CfgEnv, Journal, LocalContext, TxEnv, result::ExecutionResult},
    database_interface::{Database, DatabaseRef},
    state::{AccountInfo, Bytecode},
};

use super::snapshot::EvmSnapshot;
use super::{CallSimulationResult, SimStatus, TxConfig, unix_timestamp_secs_saturating};
use crate::access_set::StorageAccessList;
use crate::bundle::{BundleOptions, BundleResult, BundleTx, RevertPolicy, TxOutcome};
use crate::errors::{SimError, SimulationError, SimulationResult};
use crate::inspector::TransferInspector;

type OverlayEvm<'a> = revm::MainnetEvm<
    Context<BlockEnv, TxEnv, CfgEnv, &'a mut EvmOverlay, Journal<&'a mut EvmOverlay>, ()>,
>;

type InspectorOverlayEvm<'a, INSP> = revm::MainnetEvm<
    Context<BlockEnv, TxEnv, CfgEnv, &'a mut EvmOverlay, Journal<&'a mut EvmOverlay>, ()>,
    INSP,
>;

/// Per-simulation mutable overlay on an immutable snapshot.
///
/// Lookup order: dirty layer → snapshot → ext_db (optional RPC fallback).
///
/// This type is `Send` (unlike `EvmCache`) because it uses no `Rc`/`RefCell`.
/// Each simulation task gets its own `EvmOverlay` with a cheap `Arc::clone`
/// of the shared `EvmSnapshot`.
///
/// # Reuse across simulations (Pillar A.2)
///
/// A worker doing many sims against the same snapshot can call [`Self::new`]
/// once and [`Self::reset`] between sims instead of allocating a fresh overlay
/// each time. The reusable shared-memory buffer is also recycled across calls —
/// see [`Self::call_raw`] — without making the overlay `!Send`.
pub struct EvmOverlay {
    snapshot: Arc<EvmSnapshot>,
    /// Per-simulation mutations (accounts fetched from ext_db, committed changes).
    dirty_accounts: HashMap<Address, AccountInfo>,
    /// Per-simulation storage mutations.
    dirty_storage: HashMap<Address, HashMap<U256, U256>>,
    /// Optional RPC fallback for data not in snapshot.
    ext_db: Option<SharedBackend>,
    /// Reusable shared-memory buffer, recycled across the build→transact→revert
    /// call methods to avoid reallocating a 64 KB `Vec` per call.
    ///
    /// Stored as a plain `Vec<u8>` (not an `Rc`) so the overlay stays `Send`. A
    /// call method `mem::take`s it, wraps it in a method-local `Rc<RefCell<_>>`
    /// for revm's [`LocalContext`], runs, then reclaims and clears it after the
    /// EVM is dropped (see [`Self::build_evm_with_local`]).
    reusable_buffer: Vec<u8>,
    /// Target pre-allocation (bytes) for [`Self::reusable_buffer`] and each
    /// per-call buffer, taken from the snapshot's configured
    /// [`SharedMemoryCapacity`](super::SharedMemoryCapacity) so overlays honor the
    /// capacity set on the originating [`EvmCache`].
    buffer_capacity: usize,
}

impl EvmOverlay {
    /// Create a new overlay on the given snapshot.
    ///
    /// The reusable shared-memory buffer is pre-allocated to the snapshot's
    /// configured shared-memory capacity (see
    /// [`SharedMemoryCapacity`](super::SharedMemoryCapacity)).
    pub fn new(snapshot: Arc<EvmSnapshot>, ext_db: Option<SharedBackend>) -> Self {
        let buffer_capacity = snapshot.shared_memory_capacity;
        Self {
            snapshot,
            dirty_accounts: HashMap::new(),
            dirty_storage: HashMap::new(),
            ext_db,
            reusable_buffer: Vec::with_capacity(buffer_capacity),
            buffer_capacity,
        }
    }

    /// Clear the per-simulation dirty layer so this overlay can be reused for the
    /// next simulation against the same snapshot, without reallocating (Pillar
    /// A.2).
    ///
    /// A worker doing K sims calls [`Self::new`] once and `reset()` between sims
    /// instead of allocating a fresh overlay (plus dirty maps plus an `Arc`
    /// clone) each time. After `reset()` the overlay reads the pristine snapshot
    /// again — it is exactly equivalent to a freshly-built overlay on the same
    /// snapshot. The snapshot `Arc`, the optional `ext_db`, and the reusable
    /// shared-memory buffer (kept at capacity) are retained.
    pub fn reset(&mut self) {
        self.dirty_accounts.clear();
        self.dirty_storage.clear();
        // Keep: snapshot Arc, ext_db, and the reusable buffer. The buffer is
        // already cleared after each call, so nothing to do for it here.
    }

    /// Chain ID of the block context captured by the underlying snapshot.
    ///
    /// This is the value installed into `cfg.chain_id` by [`Self::build_evm`].
    pub fn chain_id(&self) -> u64 {
        self.snapshot.chain_id
    }

    /// Block number of the snapshot's block context, or `None` if it was not
    /// captured.
    ///
    /// When present this is the `block.number` simulations run against; when
    /// `None`, [`Self::build_evm`] leaves revm's default block number in place.
    pub fn block_number(&self) -> Option<u64> {
        self.snapshot.block_number
    }

    /// Base fee of the snapshot's block context, or `None` if it was not
    /// captured.
    ///
    /// Note that base-fee checks are disabled in the simulation EVM, so this is
    /// informational rather than enforced against the transaction.
    pub fn basefee(&self) -> Option<u64> {
        self.snapshot.basefee
    }

    /// Timestamp of the snapshot's block context, or `None` if it was not
    /// captured.
    ///
    /// When `None`, [`Self::build_evm`] substitutes the current wall-clock time
    /// for `block.timestamp`.
    pub fn timestamp(&self) -> Option<u64> {
        self.snapshot.timestamp
    }

    /// A fresh [`LocalContext`] with a newly-allocated 64 KB shared-memory buffer.
    ///
    /// Used by the public [`Self::build_evm`], which hands out the EVM and cannot
    /// reclaim its buffer afterwards. The internal call methods instead recycle
    /// [`Self::reusable_buffer`] via [`Self::build_evm_with_local`].
    fn fresh_local(&self) -> LocalContext {
        LocalContext {
            shared_memory_buffer: Rc::new(RefCell::new(Vec::with_capacity(self.buffer_capacity))),
            precompile_error_message: None,
        }
    }

    /// Build a revm EVM instance backed by this overlay, using a caller-supplied
    /// [`LocalContext`].
    ///
    /// This is the shared body behind [`Self::build_evm`] and the internal call
    /// methods. The call methods pass a `local` wrapping the recycled
    /// [`Self::reusable_buffer`] (Pillar A.2) and reclaim it after the EVM is
    /// dropped; [`Self::build_evm`] passes a fresh one.
    ///
    /// Note: the returned EVM is `!Send` (due to `LocalContext`'s `Rc<RefCell>`),
    /// but this is fine because it's created and used within a single task.
    fn build_evm_with_local(&mut self, local: LocalContext) -> OverlayEvm<'_> {
        // Read snapshot values before the mutable borrow of self
        let chain_id = self.snapshot.chain_id;
        let spec_id = self.snapshot.spec_id;
        let timestamp = self
            .snapshot
            .timestamp
            .unwrap_or_else(|| unix_timestamp_secs_saturating(std::time::SystemTime::now()));
        let block_number = self.snapshot.block_number;
        let basefee = self.snapshot.basefee;
        let coinbase = self.snapshot.coinbase;
        let prevrandao = self.snapshot.prevrandao;
        let gas_limit = self.snapshot.gas_limit;

        let mut evm = Context::mainnet()
            .with_db(&mut *self)
            .with_local(local)
            .modify_cfg_chained(|cfg| {
                cfg.disable_nonce_check = true;
                cfg.disable_eip3607 = true;
                cfg.disable_base_fee = true;
                cfg.disable_balance_check = true;
                cfg.chain_id = chain_id;
                cfg.limit_contract_code_size = None;
                cfg.tx_chain_id_check = false;
                cfg.spec = spec_id;
            })
            .build_mainnet();

        evm.block.timestamp = U256::from(timestamp);
        if let Some(number) = block_number {
            evm.block.number = U256::from(number);
        }
        if let Some(basefee) = basefee {
            evm.block.basefee = basefee;
        }
        if let Some(coinbase) = coinbase {
            evm.block.beneficiary = coinbase;
        }
        if let Some(prevrandao) = prevrandao {
            evm.block.prevrandao = Some(prevrandao);
        }
        if let Some(gas_limit) = gas_limit {
            evm.block.gas_limit = gas_limit;
        }
        evm
    }

    /// Build a revm EVM instance backed by this overlay.
    ///
    /// This allocates a fresh 64 KB shared-memory buffer each call: it hands the
    /// EVM out to the caller and cannot reclaim the buffer afterwards, so it
    /// cannot recycle the overlay's reusable buffer. The internal call methods
    /// ([`Self::call_raw`], etc.) recycle the buffer instead (Pillar A.2).
    ///
    /// Note: The returned EVM is `!Send` (due to `LocalContext`'s `Rc<RefCell>`),
    /// but this is fine because it's created and used within a single task.
    pub fn build_evm(&mut self) -> OverlayEvm<'_> {
        let local = self.fresh_local();
        self.build_evm_with_local(local)
    }

    /// Execute a non-committing call and return the raw [`ExecutionResult`].
    ///
    /// The EVM state is reverted to a checkpoint after execution on *both*
    /// success and failure, so the call never mutates this overlay's dirty
    /// layer. Each overlay simulation is therefore isolated: repeated calls all
    /// observe the same base state.
    ///
    /// A revert or halt is *not* an error here — it is reported through the
    /// returned [`ExecutionResult`] variant. Only failure to build or transact
    /// the call yields `Err`.
    ///
    /// # Errors
    ///
    /// Returns an error if the [`TxEnv`] cannot be built from the given inputs,
    /// or if revm fails to transact the call (for example a database error
    /// while loading state from the RPC fallback).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use std::sync::Arc;
    /// # use alloy_primitives::{Address, Bytes};
    /// # use evm_fork_cache::cache::{EvmOverlay, EvmSnapshot};
    /// # fn run(snapshot: Arc<EvmSnapshot>) -> anyhow::Result<()> {
    /// let mut overlay = EvmOverlay::new(snapshot, None);
    /// let result = overlay.call_raw(Address::ZERO, Address::ZERO, Bytes::new())?;
    /// // State is reverted; a second call sees the same base state.
    /// let _again = overlay.call_raw(Address::ZERO, Address::ZERO, Bytes::new())?;
    /// # let _ = result;
    /// # Ok(())
    /// # }
    /// ```
    pub fn call_raw(
        &mut self,
        from: Address,
        to: Address,
        calldata: Bytes,
    ) -> Result<ExecutionResult> {
        let tx = TxEnv::builder()
            .caller(from)
            .kind(TxKind::Call(to))
            .data(calldata)
            .value(U256::ZERO)
            .build()
            .map_err(|e| anyhow!("Failed to build tx env: {:?}", e))?;

        // Recycle the reusable buffer (Pillar A.2): take it out as a plain Vec
        // (keeping the overlay Send), lend it to a method-local Rc<RefCell> for
        // revm's LocalContext, then reclaim and clear it after the EVM is dropped.
        let buffer = Rc::new(RefCell::new(std::mem::take(&mut self.reusable_buffer)));
        let local = LocalContext {
            shared_memory_buffer: Rc::clone(&buffer),
            precompile_error_message: None,
        };

        let result = {
            let mut evm = self.build_evm_with_local(local);
            use revm::context_interface::JournalTr;
            let checkpoint = evm.journaled_state.checkpoint();
            let result = evm
                .transact_one(tx)
                .map_err(|e| anyhow!("Failed to transact: {:?}", e));
            evm.journaled_state.checkpoint_revert(checkpoint);
            result
        };

        self.reclaim_buffer(buffer);
        result
    }

    /// Reclaim the recycled shared-memory buffer after the EVM (and its
    /// `LocalContext` clone of the `Rc`) has been dropped, clearing it for the
    /// next call.
    ///
    /// The `Rc` was only ever held by the dropped EVM and this method's local, so
    /// `try_unwrap` succeeds in the normal path. If a panic somewhere left an
    /// extra strong reference the buffer is simply re-allocated next call — no
    /// correctness impact.
    fn reclaim_buffer(&mut self, buffer: Rc<RefCell<Vec<u8>>>) {
        if let Ok(cell) = Rc::try_unwrap(buffer) {
            let mut buf = cell.into_inner();
            buf.clear();
            self.reusable_buffer = buf;
        } else {
            self.reusable_buffer = Vec::with_capacity(self.buffer_capacity);
        }
    }

    /// Build a revm EVM instance with an inspector, backed by this overlay, using
    /// a caller-supplied [`LocalContext`].
    ///
    /// Like [`Self::build_evm_with_local`] but attaches `inspector`. The call
    /// methods pass a `local` wrapping the recycled [`Self::reusable_buffer`]
    /// (Pillar A.2) and reclaim it after the EVM is dropped.
    fn build_evm_with_inspector_local<INSP>(
        &mut self,
        inspector: INSP,
        local: LocalContext,
    ) -> InspectorOverlayEvm<'_, INSP> {
        let chain_id = self.snapshot.chain_id;
        let spec_id = self.snapshot.spec_id;
        let timestamp = self
            .snapshot
            .timestamp
            .unwrap_or_else(|| unix_timestamp_secs_saturating(std::time::SystemTime::now()));
        let block_number = self.snapshot.block_number;
        let basefee = self.snapshot.basefee;
        let coinbase = self.snapshot.coinbase;
        let prevrandao = self.snapshot.prevrandao;
        let gas_limit = self.snapshot.gas_limit;

        let mut evm = Context::mainnet()
            .with_db(&mut *self)
            .with_local(local)
            .modify_cfg_chained(|cfg| {
                cfg.disable_nonce_check = true;
                cfg.disable_eip3607 = true;
                cfg.disable_base_fee = true;
                cfg.disable_balance_check = true;
                cfg.chain_id = chain_id;
                cfg.limit_contract_code_size = None;
                cfg.tx_chain_id_check = false;
                cfg.spec = spec_id;
            })
            .build_mainnet_with_inspector(inspector);

        evm.block.timestamp = U256::from(timestamp);
        if let Some(number) = block_number {
            evm.block.number = U256::from(number);
        }
        if let Some(basefee) = basefee {
            evm.block.basefee = basefee;
        }
        if let Some(coinbase) = coinbase {
            evm.block.beneficiary = coinbase;
        }
        if let Some(prevrandao) = prevrandao {
            evm.block.prevrandao = Some(prevrandao);
        }
        if let Some(gas_limit) = gas_limit {
            evm.block.gas_limit = gas_limit;
        }
        evm
    }

    /// Simulate a call with transfer tracking via the `TransferInspector`.
    ///
    /// This is the overlay-compatible equivalent of
    /// [`super::EvmCache::simulate_with_transfer_tracking`]. It captures ERC20
    /// Transfer events during execution to compute balance deltas for `owner`
    /// (restricted to `tokens` when provided) without relying on pre/post
    /// balance queries.
    ///
    /// On a reverting or halting call the EVM state is reverted to a checkpoint
    /// before returning, so a failed simulation never mutates this overlay. On
    /// success the call either commits the journaled changes into the overlay's
    /// dirty layer (`commit == true`) or reverts them (`commit == false`); a
    /// non-committing run leaves each overlay simulation isolated from the next.
    ///
    /// # Errors
    ///
    /// Returns an error if the [`TxEnv`] cannot be built, if revm fails to
    /// transact the call, if the call reverts (mapped from the revert payload),
    /// or if the call halts. In every error case the EVM state is reverted
    /// first, regardless of `commit`.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use std::sync::Arc;
    /// # use alloy_primitives::{Address, Bytes};
    /// # use evm_fork_cache::cache::{EvmOverlay, EvmSnapshot};
    /// # fn run(snapshot: Arc<EvmSnapshot>, token: Address, owner: Address) -> anyhow::Result<()> {
    /// let mut overlay = EvmOverlay::new(snapshot, None);
    /// let sim = overlay.simulate_with_transfer_tracking(
    ///     owner,
    ///     token,
    ///     Bytes::new(),
    ///     owner,
    ///     Some([token]),
    ///     false, // non-committing: state is reverted afterwards
    /// )?;
    /// let _delta = sim.token_deltas.get(&token);
    /// # Ok(())
    /// # }
    /// ```
    pub fn simulate_with_transfer_tracking(
        &mut self,
        from: Address,
        to: Address,
        calldata: Bytes,
        owner: Address,
        tokens: Option<impl IntoIterator<Item = Address>>,
        commit: bool,
    ) -> SimulationResult<CallSimulationResult> {
        let tx = TxEnv::builder()
            .caller(from)
            .kind(TxKind::Call(to))
            .data(calldata)
            .value(U256::ZERO)
            .build()
            .map_err(|e| SimError::Other(anyhow!("Failed to build tx env: {:?}", e)))?;

        let inspector = TransferInspector::new();

        // Recycle the reusable buffer (Pillar A.2); reclaimed after the EVM drops.
        let buffer = Rc::new(RefCell::new(std::mem::take(&mut self.reusable_buffer)));
        let local = LocalContext {
            shared_memory_buffer: Rc::clone(&buffer),
            precompile_error_message: None,
        };

        let outcome = {
            let mut evm = self.build_evm_with_inspector_local(inspector, local);

            use revm::context_interface::JournalTr;
            let checkpoint = evm.journaled_state.checkpoint();

            let result = evm
                .inspect_one_tx(tx)
                .map_err(|e| SimError::Other(anyhow!("Failed to transact: {:?}", e)));

            match result {
                Ok(ExecutionResult::Success {
                    logs,
                    gas_used,
                    output,
                    ..
                }) => {
                    let token_deltas = if let Some(token_list) = tokens {
                        evm.inspector.balance_deltas_for_tokens(owner, token_list)
                    } else {
                        evm.inspector.balance_deltas(owner)
                    };

                    // Extract EIP-2930 access list from journaled state
                    let access_list = extract_access_list(&evm.journaled_state.state);

                    if commit {
                        evm.commit_inner();
                    } else {
                        evm.journaled_state.checkpoint_revert(checkpoint);
                    }

                    Ok(CallSimulationResult {
                        status: SimStatus::Success,
                        gas_used,
                        token_deltas,
                        logs,
                        access_list,
                        output: output.into_data(),
                    })
                }
                Ok(ExecutionResult::Revert { gas_used, output }) => {
                    evm.journaled_state.checkpoint_revert(checkpoint);
                    Err(SimulationError::from_revert(gas_used, output).into())
                }
                Ok(ExecutionResult::Halt { reason, gas_used }) => {
                    evm.journaled_state.checkpoint_revert(checkpoint);
                    Err(SimError::Halt {
                        reason: format!("{reason:?}"),
                        gas_used,
                    })
                }
                Err(err) => {
                    evm.journaled_state.checkpoint_revert(checkpoint);
                    Err(err)
                }
            }
        };

        self.reclaim_buffer(buffer);
        outcome
    }

    /// Run a single call with a caller-supplied [`Inspector`](revm::Inspector),
    /// returning the raw [`ExecutionResult`] and handing the inspector back for the
    /// caller to read.
    ///
    /// This is the inspector-generic public seam: where
    /// [`Self::simulate_with_transfer_tracking`] hard-wires the
    /// [`TransferInspector`], this accepts any
    /// [`revm::Inspector`] — a [`CallTracer`](crate::tracing::CallTracer), an
    /// [`InspectorStack`](crate::tracing::InspectorStack) composing several, or a
    /// caller-defined one. It honors a full [`TxConfig`] (value/gas/nonce/access
    /// list) exactly like [`Self::call_raw_with_access_list_with`] and recycles the
    /// reusable shared-memory buffer like the other call methods.
    ///
    /// Unlike `simulate_with_transfer_tracking`, a revert or halt is **not** an
    /// error: the raw [`ExecutionResult`] variant
    /// ([`Success`](ExecutionResult::Success) /
    /// [`Revert`](ExecutionResult::Revert) / [`Halt`](ExecutionResult::Halt)) is
    /// returned as `Ok` so the inspector's captured frames (e.g. a reverted call
    /// tree) remain observable. Only a tx-env build failure or a transact/database
    /// error yields `Err`.
    ///
    /// On a successful transact the journaled changes are either committed into the
    /// overlay's dirty layer (`commit == true`) or reverted (`commit == false`),
    /// matching [`Self::simulate_with_transfer_tracking`]. On a revert/halt the
    /// checkpoint is always reverted regardless of `commit`, so a failed call never
    /// mutates this overlay. On a transact error the checkpoint is reverted too.
    ///
    /// # Errors
    ///
    /// Returns an error if the [`TxEnv`] cannot be built from `from`/`to`/`tx`, or
    /// if revm fails to transact the call (e.g. a database error while loading
    /// state).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use std::sync::Arc;
    /// # use alloy_primitives::{Address, Bytes};
    /// # use evm_fork_cache::cache::{EvmOverlay, EvmSnapshot, TxConfig};
    /// # use evm_fork_cache::CallTracer;
    /// # fn run(snapshot: Arc<EvmSnapshot>, to: Address) -> anyhow::Result<()> {
    /// let mut overlay = EvmOverlay::new(snapshot, None);
    /// let (result, tracer) = overlay.call_raw_with_inspector(
    ///     Address::ZERO,
    ///     to,
    ///     Bytes::new(),
    ///     &TxConfig::default(),
    ///     CallTracer::new(),
    ///     false,
    /// )?;
    /// let _ = result;
    /// let _trace = tracer.into_trace();
    /// # Ok(())
    /// # }
    /// ```
    pub fn call_raw_with_inspector<I>(
        &mut self,
        from: Address,
        to: Address,
        calldata: Bytes,
        tx: &TxConfig,
        inspector: I,
        commit: bool,
    ) -> SimulationResult<(ExecutionResult, I)>
    where
        I: for<'a> revm::Inspector<
                Context<
                    BlockEnv,
                    TxEnv,
                    CfgEnv,
                    &'a mut EvmOverlay,
                    Journal<&'a mut EvmOverlay>,
                    (),
                >,
            >,
    {
        let mut builder = TxEnv::builder()
            .caller(from)
            .kind(TxKind::Call(to))
            .data(calldata)
            .value(tx.value);
        if let Some(gas_limit) = tx.gas_limit {
            builder = builder.gas_limit(gas_limit);
        }
        if let Some(gas_price) = tx.gas_price {
            builder = builder.gas_price(gas_price);
        }
        if let Some(nonce) = tx.nonce {
            builder = builder.nonce(nonce);
        }
        if let Some(access_list) = &tx.access_list {
            builder = builder.access_list(access_list.clone());
        }
        let tx_env = builder
            .build()
            .map_err(|e| SimError::Other(anyhow!("Failed to build tx env: {:?}", e)))?;

        // Recycle the reusable buffer (Pillar A.2); reclaimed after the EVM drops.
        let buffer = Rc::new(RefCell::new(std::mem::take(&mut self.reusable_buffer)));
        let local = LocalContext {
            shared_memory_buffer: Rc::clone(&buffer),
            precompile_error_message: None,
        };

        let outcome = {
            let mut evm = self.build_evm_with_inspector_local(inspector, local);

            use revm::context_interface::JournalTr;
            let checkpoint = evm.journaled_state.checkpoint();

            match evm.inspect_one_tx(tx_env) {
                Ok(result) => {
                    if commit && matches!(result, ExecutionResult::Success { .. }) {
                        evm.commit_inner();
                    } else {
                        evm.journaled_state.checkpoint_revert(checkpoint);
                    }
                    // Hand the inspector back to the caller.
                    Ok((result, evm.inspector))
                }
                Err(e) => {
                    evm.journaled_state.checkpoint_revert(checkpoint);
                    Err(SimError::Other(anyhow!("Failed to transact: {:?}", e)))
                }
            }
        };

        self.reclaim_buffer(buffer);
        outcome
    }

    /// Apply `txs` in order against this overlay over **cumulative** block state,
    /// with a revert policy and coinbase/miner-payment accounting (Phase 6
    /// Track A+B).
    ///
    /// Each transaction observes the committed writes of the ones before it:
    /// the bundle runs on a single overlay/EVM with one outer checkpoint plus a
    /// per-transaction inner checkpoint, so it does **not** rebuild a fresh
    /// overlay per transaction. See the [`bundle`](crate::bundle) module for the
    /// public vocabulary ([`BundleTx`], [`BundleOptions`], [`RevertPolicy`],
    /// [`TxOutcome`], [`BundleResult`]).
    ///
    /// # Revert policy
    ///
    /// - [`RevertPolicy::Atomic`]: the first transaction that reverts/halts
    ///   rolls the whole bundle back to the outer checkpoint, sets
    ///   `succeeded = false`, and stops (`per_tx` ends at the failing
    ///   transaction). `coinbase_payment` is `0` and the overlay is unchanged.
    /// - [`RevertPolicy::AllowReverts`]: a revert at a whitelisted index rolls
    ///   back only that transaction (inner checkpoint) and execution continues;
    ///   a revert at a non-whitelisted index behaves like `Atomic`.
    ///
    /// # Coinbase accounting
    ///
    /// `coinbase_payment` is the block beneficiary's balance delta across the kept
    /// transactions. Under EIP-1559 revm credits the beneficiary only the priority
    /// fee (`(effective_gas_price − basefee) × gas_used`) and burns the base fee
    /// in-EVM, so the delta is the honest miner payment (plus any direct coinbase
    /// tips). Saturating.
    ///
    /// # Commit semantics
    ///
    /// `opts.commit == true` folds the bundle's cumulative state into this
    /// overlay's dirty layer (observable by subsequent overlay calls);
    /// `false` reverts the outer checkpoint so the overlay is unchanged. A
    /// failed atomic bundle never leaves partial state regardless of `commit`.
    ///
    /// # Errors
    ///
    /// Returns [`SimError`] if a transaction environment cannot be built or revm
    /// fails to transact (e.g. a database error). A transaction *reverting* is
    /// not an error — it is reported through the per-transaction
    /// [`TxOutcome`] and the revert policy.
    pub fn simulate_bundle(
        &mut self,
        txs: &[BundleTx],
        opts: &BundleOptions,
    ) -> SimulationResult<BundleResult> {
        // Build every TxEnv up front so a build failure surfaces as an error
        // before we touch the EVM/journal (and the borrow of `self` is clean).
        let tx_envs: Vec<TxEnv> = txs
            .iter()
            .map(|bt| {
                let mut builder = TxEnv::builder()
                    .caller(bt.from)
                    .kind(TxKind::Call(bt.to))
                    .data(bt.calldata.clone())
                    .value(bt.tx.value);
                if let Some(gas_limit) = bt.tx.gas_limit {
                    builder = builder.gas_limit(gas_limit);
                }
                if let Some(gas_price) = bt.tx.gas_price {
                    builder = builder.gas_price(gas_price);
                }
                if let Some(nonce) = bt.tx.nonce {
                    builder = builder.nonce(nonce);
                }
                if let Some(access_list) = &bt.tx.access_list {
                    builder = builder.access_list(access_list.clone());
                }
                builder
                    .build()
                    .map_err(|e| SimError::Other(anyhow!("Failed to build tx env: {:?}", e)))
            })
            .collect::<Result<_, _>>()?;

        // Resolve the beneficiary and read its pre-bundle balance before the
        // mutable borrow of `self` by the EVM (the post-bundle delta is the miner
        // payment; revm already burns the base fee per EIP-1559).
        let beneficiary = self
            .snapshot
            .coinbase
            .unwrap_or_else(|| revm::context::BlockEnv::default().beneficiary);
        let pre_beneficiary_balance = self
            .basic(beneficiary)
            .map_err(|e| SimError::Other(anyhow!("Failed to load beneficiary: {:?}", e)))?
            .map(|info| info.balance)
            .unwrap_or(U256::ZERO);

        // Recycle the reusable buffer (Pillar A.2); reclaimed after the EVM drops.
        let buffer = Rc::new(RefCell::new(std::mem::take(&mut self.reusable_buffer)));
        let local = LocalContext {
            shared_memory_buffer: Rc::clone(&buffer),
            precompile_error_message: None,
        };

        let outcome = {
            use revm::context_interface::JournalTr;
            let mut evm = self.build_evm_with_local(local);

            // Outer checkpoint: the whole-bundle savepoint.
            let outer = evm.journaled_state.checkpoint();

            let mut per_tx: Vec<TxOutcome> = Vec::with_capacity(tx_envs.len());
            let mut total_gas: u64 = 0;
            let mut aborted = false;

            'bundle: for (idx, tx_env) in tx_envs.into_iter().enumerate() {
                // Inner checkpoint: this transaction's savepoint.
                let inner = evm.journaled_state.checkpoint();
                let result = match evm.transact_one(tx_env) {
                    Ok(result) => result,
                    Err(e) => {
                        // Host/transact error: undo this tx and the whole bundle,
                        // reclaim the buffer, and surface as SimError.
                        evm.journaled_state.checkpoint_revert(inner);
                        evm.journaled_state.checkpoint_revert(outer);
                        drop(evm);
                        self.reclaim_buffer(buffer);
                        return Err(SimError::Other(anyhow!("Failed to transact: {:?}", e)));
                    }
                };

                let gas_used = result.gas_used();
                let reverted = !result.is_success();
                let logs = result.logs().to_vec();
                total_gas = total_gas.saturating_add(gas_used);

                per_tx.push(TxOutcome {
                    result,
                    gas_used,
                    reverted,
                    logs,
                });

                if reverted {
                    let allowed = match &opts.revert_policy {
                        RevertPolicy::Atomic => false,
                        RevertPolicy::AllowReverts(idxs) => idxs.contains(&idx),
                    };
                    if allowed {
                        // Roll back only this transaction; later txs still run.
                        evm.journaled_state.checkpoint_revert(inner);
                        continue 'bundle;
                    } else {
                        // Atomic abort: roll the whole bundle back and stop.
                        evm.journaled_state.checkpoint_revert(outer);
                        aborted = true;
                        break 'bundle;
                    }
                }
                // Successful tx: its effects stay journaled for the next tx.
            }

            if aborted {
                // State is reverted to the pre-bundle outer checkpoint regardless
                // of `commit`; no payment.
                BundleResult {
                    per_tx,
                    coinbase_payment: U256::ZERO,
                    gas_used: total_gas,
                    succeeded: false,
                }
            } else {
                // Read the beneficiary's post-bundle balance from the journaled
                // state (present iff it was touched) BEFORE commit/revert, since
                // `commit_inner` finalizes (drains) the journal and an outer
                // revert would undo the credit.
                let post_beneficiary_balance = evm
                    .journaled_state
                    .state
                    .get(&beneficiary)
                    .map(|acct| acct.info.balance)
                    .unwrap_or(pre_beneficiary_balance);
                // revm already excludes the base fee from the beneficiary credit
                // (EIP-1559), so the delta is the honest miner payment.
                let coinbase_payment =
                    post_beneficiary_balance.saturating_sub(pre_beneficiary_balance);

                if opts.commit {
                    evm.commit_inner();
                } else {
                    evm.journaled_state.checkpoint_revert(outer);
                }

                BundleResult {
                    per_tx,
                    coinbase_payment,
                    gas_used: total_gas,
                    succeeded: true,
                }
            }
        };

        self.reclaim_buffer(buffer);
        Ok(outcome)
    }

    /// Execute a non-committing call and return the result plus the touched
    /// [`StorageAccessList`].
    ///
    /// The access list is collected from every account marked touched in the
    /// journaled state after execution, recording both the touched accounts and
    /// the storage slots accessed under each.
    ///
    /// The EVM state is reverted to a checkpoint after a successful transact on
    /// both success and revert/halt outcomes, so the call never mutates this
    /// overlay's dirty layer and each overlay simulation stays isolated. As with
    /// [`Self::call_raw`], a revert or halt is reported through the returned
    /// [`ExecutionResult`] rather than as an error.
    ///
    /// # Errors
    ///
    /// Returns an error if the [`TxEnv`] cannot be built, or if revm fails to
    /// transact the call (for example a database error while loading state).
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use std::sync::Arc;
    /// # use alloy_primitives::{Address, Bytes};
    /// # use evm_fork_cache::cache::{EvmOverlay, EvmSnapshot};
    /// # fn run(snapshot: Arc<EvmSnapshot>) -> anyhow::Result<()> {
    /// let mut overlay = EvmOverlay::new(snapshot, None);
    /// let (result, access_list) =
    ///     overlay.call_raw_with_access_list(Address::ZERO, Address::ZERO, Bytes::new())?;
    /// # let _ = (result, access_list);
    /// # Ok(())
    /// # }
    /// ```
    pub fn call_raw_with_access_list(
        &mut self,
        from: Address,
        to: Address,
        calldata: Bytes,
    ) -> Result<(ExecutionResult, StorageAccessList)> {
        self.call_raw_with_access_list_with(from, to, calldata, &TxConfig::default())
    }

    /// Like [`call_raw_with_access_list`](Self::call_raw_with_access_list) but
    /// honors a full [`TxConfig`]: native `value`, `gas_limit`, `gas_price`,
    /// `nonce`, and a pre-warming EIP-2930 `access_list`.
    ///
    /// This is what the freshness optimistic loop uses so a [`SimRequest`]'s tx
    /// environment — e.g. a payable call carrying `value`, or a gas-bounded call
    /// — is reproduced faithfully instead of silently running as a zero-value,
    /// default-gas call. Like the shorthand it is non-committing (the checkpoint
    /// is reverted) and returns the captured storage access list.
    ///
    /// [`SimRequest`]: crate::freshness::SimRequest
    pub fn call_raw_with_access_list_with(
        &mut self,
        from: Address,
        to: Address,
        calldata: Bytes,
        tx: &TxConfig,
    ) -> Result<(ExecutionResult, StorageAccessList)> {
        let mut builder = TxEnv::builder()
            .caller(from)
            .kind(TxKind::Call(to))
            .data(calldata)
            .value(tx.value);
        if let Some(gas_limit) = tx.gas_limit {
            builder = builder.gas_limit(gas_limit);
        }
        if let Some(gas_price) = tx.gas_price {
            builder = builder.gas_price(gas_price);
        }
        if let Some(nonce) = tx.nonce {
            builder = builder.nonce(nonce);
        }
        if let Some(access_list) = &tx.access_list {
            builder = builder.access_list(access_list.clone());
        }
        let tx_env = builder
            .build()
            .map_err(|e| anyhow!("Failed to build tx env: {:?}", e))?;

        // Recycle the reusable buffer (Pillar A.2); reclaimed after the EVM drops.
        let buffer = Rc::new(RefCell::new(std::mem::take(&mut self.reusable_buffer)));
        let local = LocalContext {
            shared_memory_buffer: Rc::clone(&buffer),
            precompile_error_message: None,
        };

        let outcome = {
            let mut evm = self.build_evm_with_local(local);
            use revm::context_interface::JournalTr;
            let checkpoint = evm.journaled_state.checkpoint();
            match evm.transact_one(tx_env) {
                Ok(result) => {
                    let mut access_list = StorageAccessList::default();
                    for (address, account) in evm.journaled_state.state.iter() {
                        if account.is_touched() {
                            access_list.accounts.insert(*address);
                            for (slot_key, _) in account.storage.iter() {
                                access_list.slots.insert((*address, *slot_key));
                            }
                        }
                    }
                    evm.journaled_state.checkpoint_revert(checkpoint);
                    Ok((result, access_list))
                }
                Err(e) => {
                    // Revert the checkpoint even on a host/transact error so the EVM
                    // journal is not left dirty (mirrors `call_raw`).
                    evm.journaled_state.checkpoint_revert(checkpoint);
                    Err(anyhow!("Failed to transact: {:?}", e))
                }
            }
        };

        self.reclaim_buffer(buffer);
        outcome
    }

    /// Write a storage value into this overlay's dirty layer.
    ///
    /// The dirty layer takes precedence over the snapshot on subsequent reads
    /// (see the lookup order on [`EvmOverlay`]), so this injects a value into a
    /// snapshot-backed overlay without mutating the shared snapshot.
    ///
    /// # Freshness validation
    ///
    /// This is the freshness validator's correction step. When a slot the
    /// snapshot captured is found to be stale, the validator writes the
    /// freshly-fetched value here and then re-runs the simulation (e.g. via
    /// [`Self::call_raw`]): the re-run reads the corrected slot out of the dirty
    /// layer instead of the stale snapshot value, so the corrected result
    /// becomes observable. Because the override lives only in this overlay,
    /// other overlays sharing the same `Arc<EvmSnapshot>` are unaffected.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use std::sync::Arc;
    /// # use alloy_primitives::{Address, Bytes, U256};
    /// # use evm_fork_cache::cache::{EvmOverlay, EvmSnapshot};
    /// # fn run(snapshot: Arc<EvmSnapshot>, token: Address, slot: U256) -> anyhow::Result<()> {
    /// let mut overlay = EvmOverlay::new(snapshot, None);
    /// // Inject the fresh value, then re-run to observe the corrected result.
    /// overlay.override_slot(token, slot, U256::from(42u64));
    /// let corrected = overlay.call_raw(Address::ZERO, token, Bytes::new())?;
    /// # let _ = corrected;
    /// # Ok(())
    /// # }
    /// ```
    pub fn override_slot(&mut self, address: Address, slot: U256, value: U256) {
        self.dirty_storage
            .entry(address)
            .or_default()
            .insert(slot, value);
    }
}

impl revm::database_interface::DatabaseCommit for EvmOverlay {
    fn commit(&mut self, changes: alloy_primitives::map::HashMap<Address, revm::state::Account>) {
        for (address, account) in changes {
            self.dirty_accounts.insert(address, account.info);
            let storage = self.dirty_storage.entry(address).or_default();
            for (slot, value) in account.storage {
                storage.insert(slot, value.present_value);
            }
        }
    }
}

impl Database for EvmOverlay {
    type Error = DatabaseError;

    fn basic(&mut self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
        // 1. Check dirty layer
        if let Some(info) = self.dirty_accounts.get(&address) {
            return Ok(Some(info.clone()));
        }
        // 2. Check snapshot (O(1) HashMap lookup, no locks). `account_info` folds
        //    the two snapshot tiers (overlay â–¸ base) and already short-circuits a
        //    NotExisting account to None — it must NOT fall through to the ext_db,
        //    mirroring revm `DbAccount::info()` and the live `EvmCache` read.
        if self.snapshot.accounts_not_existing.contains(&address) {
            return Ok(None);
        }
        if let Some(info) = self.snapshot.account_info(address) {
            return Ok(Some(info.clone()));
        }
        // 3. RPC fallback
        if let Some(ref ext_db) = self.ext_db {
            let info = ext_db.basic_ref(address)?;
            if let Some(ref info) = info {
                self.dirty_accounts.insert(address, info.clone());
            }
            return Ok(info);
        }
        Ok(None)
    }

    fn code_by_hash(&mut self, code_hash: B256) -> Result<Bytecode, Self::Error> {
        // Check dirty accounts first
        for info in self.dirty_accounts.values() {
            if info.code_hash == code_hash
                && let Some(code) = &info.code
            {
                return Ok(code.clone());
            }
        }
        // Check the snapshot's code index (overlay â–¸ base).
        if let Some(code) = self.snapshot.code(code_hash) {
            return Ok(code.clone());
        }
        // RPC fallback
        if let Some(ref ext_db) = self.ext_db {
            return ext_db.code_by_hash_ref(code_hash);
        }
        Ok(Bytecode::default())
    }

    fn storage(&mut self, address: Address, index: U256) -> Result<U256, Self::Error> {
        // 1. Check dirty layer
        if let Some(account_storage) = self.dirty_storage.get(&address)
            && let Some(value) = account_storage.get(&index)
        {
            return Ok(*value);
        }
        // 2. Check snapshot (O(1)). `storage_value` folds the two tiers (overlay â–¸
        //    cleared-as-ZERO â–¸ base); a cleared account's absent slot reads ZERO
        //    and must NOT fall through to the ext_db, mirroring the live EVM SLOAD
        //    for a StorageCleared/NotExisting account.
        if let Some(value) = self.snapshot.storage_value(address, index) {
            return Ok(value);
        }
        // 3. RPC fallback
        if let Some(ref ext_db) = self.ext_db {
            let value = ext_db.storage_ref(address, index)?;
            self.dirty_storage
                .entry(address)
                .or_default()
                .insert(index, value);
            return Ok(value);
        }
        Ok(U256::ZERO)
    }

    fn block_hash(&mut self, number: u64) -> Result<B256, Self::Error> {
        if let Some(hash) = self.snapshot.block_hashes.get(&number) {
            return Ok(*hash);
        }
        if let Some(ref ext_db) = self.ext_db {
            return ext_db.block_hash_ref(number);
        }
        // Snapshots never populate `block_hashes` (the live cache does not track
        // block hashes), so without an `ext_db` the `BLOCKHASH` opcode resolves to
        // ZERO. Overlays built internally (e.g. the freshness validator) pass
        // `ext_db = None`; a contract that reads `BLOCKHASH` through such an
        // overlay sees ZERO. Documented in docs/KNOWN_ISSUES.md.
        Ok(B256::ZERO)
    }
}

fn extract_access_list(state: &revm::state::EvmState) -> AccessList {
    let items: Vec<AccessListItem> = state
        .iter()
        .filter(|(_, account)| account.is_touched())
        .map(|(address, account)| AccessListItem {
            address: *address,
            storage_keys: account
                .storage
                .keys()
                .map(|slot| B256::from(*slot))
                .collect(),
        })
        .collect();
    AccessList(items)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cache::snapshot::BaseState;
    use revm::primitives::hardfork::SpecId;
    use std::collections::HashSet;

    /// Build a two-tier `EvmSnapshot` whose cold base holds the given accounts,
    /// storage, and code, with an empty hot overlay — the shape
    /// `create_snapshot_deep_clone` produces. The `Arc`-per-account storage of the
    /// base is built from the plain per-account maps.
    fn snap(
        accounts: HashMap<Address, AccountInfo>,
        storage: HashMap<Address, HashMap<U256, U256>>,
        code_by_hash: HashMap<B256, Bytecode>,
        block_hashes: HashMap<u64, B256>,
    ) -> Arc<EvmSnapshot> {
        let base = BaseState {
            accounts,
            storage: storage
                .into_iter()
                .map(|(addr, slots)| (addr, Arc::new(slots)))
                .collect(),
            code_by_hash,
        };
        Arc::new(EvmSnapshot {
            base: Arc::new(base),
            overlay_accounts: HashMap::new(),
            overlay_storage: HashMap::new(),
            overlay_code_by_hash: HashMap::new(),
            storage_cleared: HashSet::new(),
            accounts_not_existing: HashSet::new(),
            block_hashes,
            block_number: None,
            basefee: None,
            coinbase: None,
            prevrandao: None,
            gas_limit: None,
            chain_id: 42161,
            timestamp: None,
            spec_id: SpecId::CANCUN,
            shared_memory_capacity: 64_000,
        })
    }

    #[test]
    fn test_overlay_is_send() {
        fn assert_send<T: Send>() {}
        assert_send::<EvmOverlay>();
    }

    #[test]
    fn test_overlay_basic_from_snapshot() {
        let mut accounts = HashMap::new();
        let info = AccountInfo {
            balance: U256::from(1000),
            nonce: 1,
            code_hash: B256::ZERO,
            code: None,
            account_id: None,
        };
        let addr = Address::repeat_byte(0x01);
        accounts.insert(addr, info);

        let snapshot = snap(accounts, HashMap::new(), HashMap::new(), HashMap::new());

        let mut overlay = EvmOverlay::new(snapshot, None);
        let result = overlay.basic(addr).unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().balance, U256::from(1000));
    }

    #[test]
    fn test_overlay_storage_from_snapshot() {
        let addr = Address::repeat_byte(0x01);
        let slot = U256::from(42);
        let value = U256::from(999);

        let mut storage = HashMap::new();
        let mut account_storage = HashMap::new();
        account_storage.insert(slot, value);
        storage.insert(addr, account_storage);

        let snapshot = snap(HashMap::new(), storage, HashMap::new(), HashMap::new());

        let mut overlay = EvmOverlay::new(snapshot, None);
        let result = overlay.storage(addr, slot).unwrap();
        assert_eq!(result, value);
    }

    #[test]
    fn test_overlay_dirty_overrides_snapshot() {
        let addr = Address::repeat_byte(0x01);
        let slot = U256::from(42);

        let mut storage = HashMap::new();
        let mut account_storage = HashMap::new();
        account_storage.insert(slot, U256::from(100));
        storage.insert(addr, account_storage);

        let snapshot = snap(HashMap::new(), storage, HashMap::new(), HashMap::new());

        let mut overlay = EvmOverlay::new(snapshot, None);

        // Write to dirty layer
        overlay
            .dirty_storage
            .entry(addr)
            .or_default()
            .insert(slot, U256::from(200));

        // Should read dirty value, not snapshot
        let result = overlay.storage(addr, slot).unwrap();
        assert_eq!(result, U256::from(200));
    }

    #[test]
    fn test_overlay_missing_returns_zero() {
        let snapshot = snap(
            HashMap::new(),
            HashMap::new(),
            HashMap::new(),
            HashMap::new(),
        );

        let mut overlay = EvmOverlay::new(snapshot, None);
        let addr = Address::repeat_byte(0x99);
        let result = overlay.storage(addr, U256::from(1)).unwrap();
        assert_eq!(result, U256::ZERO);

        let account = overlay.basic(addr).unwrap();
        assert!(account.is_none());
    }

    #[test]
    fn test_overlay_code_by_hash_from_snapshot() {
        let code = Bytecode::new_raw(Bytes::from(vec![0x60, 0x00, 0x60, 0x00]));
        let hash = code.hash_slow();

        let mut code_by_hash = HashMap::new();
        code_by_hash.insert(hash, code.clone());

        let snapshot = snap(HashMap::new(), HashMap::new(), code_by_hash, HashMap::new());

        let mut overlay = EvmOverlay::new(snapshot, None);
        let result = overlay.code_by_hash(hash).unwrap();
        assert_eq!(result.len(), 4);
    }

    #[test]
    fn test_overlay_block_hash() {
        let mut block_hashes = HashMap::new();
        let hash = B256::repeat_byte(0xAB);
        block_hashes.insert(42u64, hash);

        let snapshot = snap(HashMap::new(), HashMap::new(), HashMap::new(), block_hashes);

        let mut overlay = EvmOverlay::new(snapshot, None);
        assert_eq!(overlay.block_hash(42).unwrap(), hash);
        assert_eq!(overlay.block_hash(99).unwrap(), B256::ZERO);
    }
}