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
//! Forked EVM **simulation engine** for EVM search, MEV, and backtesting.
//!
//! `evm-fork-cache` simulates EVM transactions against recent on-chain state
//! without re-deriving that state on every call. It builds on [`revm`],
//! [`alloy`], and [`foundry-fork-db`] to provide a lazy-loading state cache,
//! immutable snapshots shareable across threads, per-simulation overlays, a
//! freshness control plane, and the state-manipulation helpers a search loop
//! needs (balance overrides, batched multicalls, verified code seeding +
//! Foundry-style bytecode etching, CREATE3 address derivation, and an
//! extensible revert decoder).
//!
//! [`revm`]: https://github.com/bluealloy/revm
//! [`alloy`]: https://github.com/alloy-rs/alloy
//! [`foundry-fork-db`]: https://github.com/foundry-rs/foundry-fork-db
//!
//! # The state stack
//!
//! Reads flow up; the fork DB lazily fetches misses from RPC. Writes and purges
//! are applied directly to the cache (no RPC on the hot path).
//!
//! ```text
//! EvmOverlay × N isolated, Send simulations (cheap Arc clones)
//! ▲ clone × N
//! EvmSnapshot immutable, point-in-time, Send + Sync
//! ▲ snapshot()
//! EvmCache lazy RPC fetch + local state cache + targeted writes/purge
//! ▲ lazy fetch
//! RPC provider
//! ```
//!
//! The entry point is [`cache::EvmCache`]: construct one over an RPC backend
//! (see [`cache::EvmCacheBuilder`]), then snapshot it with
//! [`cache::EvmCache::snapshot`] to fan out parallel simulations, each
//! driving its own [`cache::EvmOverlay`]. `EvmCache` is `!Send` (it owns the
//! mutable fork and blocks on RPC internally); `EvmSnapshot` is `Send + Sync`
//! and `EvmOverlay` is `Send`, so the fan-out parallelizes safely.
//!
//! # Modules
//!
//! - [`cache`] — the fork cache, snapshots, overlays, and on-disk persistence.
//! - [`access_list`] / [`access_set`] — EIP-2930 access-list construction and
//! EIP-2929 warm-slot tracking for gas estimation.
//! - [`errors`] — structured simulation errors ([`errors::SimError`]) and an
//! extensible revert-reason decoder you can teach your own custom Solidity
//! error selectors.
//! - [`freshness`] — the four-layer freshness model (classification, observation,
//! policy, mechanism) and the optimistic verify-and-rerun execution loop with
//! deferred validation.
//! - [`state_update`] — the generic state-mutation vocabulary (`StateUpdate` /
//! `AccountPatch` / `PurgeScope`, plus relative `SlotDelta` read-modify-write and
//! masked `SlotMasked` writes) applied by `EvmCache::apply_update` /
//! `apply_updates` / `modify_slot`, with a structured `StateDiff` output
//! (Pillar B.1).
//! - [`events`] — the event → state pipeline (Pillar B.2): `EventDecoder` /
//! `StateView` / `DecoderRegistry` decode an on-chain `Log` into `StateUpdate`s,
//! and `EventPipeline` ingests, reorg-purges, and reconciles a block's logs.
//! Ships an ERC-20 `Transfer` decoder plus traits for external decoders.
//! - `reactive` — default-enabled provider-neutral handler runtime for logs,
//! blocks, and pending transaction signals. Pure handlers emit `StateUpdate`s,
//! invalidations, resync requests, speculative signals, and hooks; the runtime
//! validates and applies canonical cache mutations, journals canonical block
//! effects for depth-bounded reorg recovery, and includes a live
//! `AlloySubscriber` (WebSocket) transport.
//! - `cold_start` — default-enabled (reactive-gated) declarative warming of a
//! working set of accounts/slots into the cache in one batched pass
//! (`EvmCache::run_cold_start` + `ColdStartPlanner`), returning a structured
//! `ColdStartRunReport`. Each round verifies any pending code seeds first
//! (the `verify_code` phase), so sims never run over unverified claims.
//! - [`bundle`] — multi-transaction bundle execution over cumulative block state
//! ([`EvmOverlay::simulate_bundle`](cache::EvmOverlay::simulate_bundle)): ordered
//! txs, an `Atomic`/`AllowReverts` revert policy, and coinbase/miner-payment
//! accounting.
//! - [`inspector`] — an [`Inspector`](revm::Inspector) that captures ERC20
//! `Transfer` events to reconstruct balance deltas from a simulation.
//! - [`tracing`] — a call-frame [`Inspector`](revm::Inspector)
//! ([`CallTracer`] building a [`CallTrace`] tree) plus [`InspectorStack`] for
//! composing several inspectors over one pass, driven through
//! [`EvmOverlay::call_raw_with_inspector`](cache::EvmOverlay::call_raw_with_inspector).
//! - [`bulk_storage`] — bulk storage extraction over `eth_call` state
//! overrides (the **default** batch storage fetcher): thousands of slots —
//! across many contracts — per call, plus custom storage programs and
//! account-fields/block-context extractors. See
//! `docs/bulk-storage-extraction.md` for measured economics.
//! - [`multicall`] — batched read-only calls through Multicall3.
//! - [`deploy`] / [`create3`] — contract deployment and CREATE3 address math.
//! - [`prefetch_registry`] — two-stage storage-slot pre-warming
//! (complemented by the declarative [`cache::EvmCache::prewarm_slots`]).
//! - [`mapping_probe`] — trace-based discovery of hash-derived storage slots:
//! derive a mapping's base slot and byte-order layout (Solidity / Vyper /
//! Solady / nested) from one simulation, via `EvmCache::trace_hashed_slots` /
//! `discover_erc20_balance_slot` / `track_erc20_balances`.
//!
//! # Requirements
//!
//! Any constructor or method that may touch RPC fetches missing state through a
//! synchronous façade over an async provider
//! ([`tokio::task::block_in_place`]), so it must run on a **multi-thread** tokio
//! runtime:
//!
//! ```ignore
//! #[tokio::main(flavor = "multi_thread")]
//! async fn main() { /* ... */ }
//!
//! #[tokio::test(flavor = "multi_thread")]
//! async fn my_test() { /* ... */ }
//! ```
//!
//! On a current-thread runtime, fetch paths do **not** panic: the synchronous
//! bridge checks the runtime flavor up front and returns a typed
//! [`RuntimeError`] (`CurrentThreadRuntime`) instead, so a
//! misconfigured runtime surfaces as a handled error rather than a panic deep in
//! a callback. The offline examples and integration tests build the cache over a
//! mocked provider and never reach the network, so they are exempt.
//!
//! # Error handling
//!
//! Simulation entry points that distinguish failure modes return
//! [`errors::SimulationResult`] (`Result<T, SimError>`), where
//! [`SimError`] separates a decoded [`Revert`](errors::SimError::Revert),
//! an EVM [`Halt`](errors::SimError::Halt), and an unexpected host-side
//! [`Other`](errors::SimError::Other) [`SimHostError`]
//! (RPC, database, ABI encoding). Other fallible modules expose domain errors
//! such as [`CacheError`], [`FreshnessError`], and [`StorageFetchError`] rather
//! than erasing failures
//! into a dynamic catch-all error. The
//! freshness loop never silently trusts stale data: a transient RPC failure
//! surfaces as [`freshness::Validation::Unverified`] so callers can retry rather
//! than act on unverified results.
//!
//! # Maturity & stability
//!
//! This crate is **pre-1.0** and developed against a phased roadmap (see
//! `docs/ROADMAP.md`). Until 1.0, breaking changes may land in minor releases;
//! each is recorded in the crate `CHANGELOG.md`. MSRV is Rust 1.88 (edition 2024).
//!
//! The `examples/` directory has runnable, documented walkthroughs of each
//! module — offline ones that need no network, plus a few that fork real chain
//! state over RPC. See the crate README for the full list.
pub use StorageAccessList;
// Bulk storage extraction over eth_call state overrides — the default batch
// storage fetcher since 0.2.0 (see docs/bulk-storage-extraction.md).
pub use ;
// Phase 6 Track A+B: bundle simulation + coinbase accounting public vocabulary.
pub use ;
// Primary entry points, hoisted to the crate root for discoverability. The
// fully-qualified module paths (`cache::EvmCache`, `reactive::ReactiveRuntime`,
// …) remain valid, so this is purely additive.
pub use ;
pub use ;
pub use ;
pub use Erc20TransferDecoder;
pub use ;
pub use ;
// Trace-based hash-derived storage-slot discovery (v0.2.1).
pub use ;
pub use ;
pub use ;
pub use ;