degenbot-simulation 0.6.0-alpha.14

In-process revm simulation executor + dispatch fan-out (pyo3-free core; ADR-019 D4 fold).
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
//! Cross-block warm cache for immutable/long-TTL account data — bytecode +
//! account existence (the `basic_ref` + `code_by_hash_ref` rows). Sits
//! underneath the per-block `CacheDB` in the `BlockSimHandle` DB stack so the
//! contract code-load RPCs stop repeating every block while the per-block
//! `CacheDB` (overrides + mutable storage, fresh each block, drops at EOB) is
//! preserved.
//!
//! ## What this caches (the staleness line)
//!
//! Only the immutable rows:
//! - `basic_ref` — `AccountInfo` FOR CONTRACTS ONLY. An EOA's nonce/balance
//!   mutate every block, so EOA rows (empty code hash) are never cached —
//!   they fall through to the per-block `CacheDB` + RPC path fresh at each
//!   block build. (A cached contract's AccountInfo byte-RARELY mutates:
//!   contract nonces advance only via `CREATE*`; a stale contract nonce does
//!   not gate frame-replay validation the way a sender nonce does, and the
//!   TTL refresh bounds that edge.)
//! - `code_by_hash_ref` — `Bytecode` keyed by `code_hash` (immutable by hash).
//!
//! `storage_ref` + `block_hash_ref` are forwarded UNCHECKED to the inner `Db`
//! (the mutable-per-block row — V2 reserves slot 8 / V3 `slot0`+ticks change
//! every block there's a swap; the chain block hash is per-chain, not
//! per-block-snapshot). Caching these would re-introduce the K-invariant /
//! `LOK` stale-state-divergence reverts the option-B path hit.
//!
//! ## The TTL (a safety net, not the primary correctness mechanism)
//!
//! Each entry records the block it was loaded at; an entry read at block `B`
//! is a **miss** when `B - loaded_block > WARM_CODE_CACHE_TTL_BLOCKS`. A
//! re-fetch on miss re-caches with `loaded_block = B`.
//!
//! In the bot's domain every cached contract is immutable (EIP-1967 proxies
//! upgrade via the implementation storage slot — uncached; the injected
//! executor is per-block via `stateOverrides`, never read here). The TTL is a
//! belt-and-suspenders net against:
//! - **EIP-6780-disabled selfdestruct edge cases** (pre-Cancun metamorphic
//!   `CREATE2`-redeploy-to-same-address: a contract that selfdestructs then
//!   redeploys with different bytecode at the same address would be stale).
//!   EIP-6780 (Cancun, mainnet since March 2024) disables code-deletion on
//!   selfdestruct unless the contract was created in the same tx — so the
//!   vector is effectively closed for any persistent contract.
//! - **Future protocol changes** to code mutability.
//!
//! ## Composition
//!
//! ```text
//! CacheDB (per-block: overrides + mutable storage, fresh each block, drops at EOB)
//!   → WarmCodeCache (persistent: bytecode + account-existence, per-entry TTL'd)
//!        ↳ holds Arc<RwLock<WarmCodeCacheInner>> cloned from the engine owner
//!        ↳ the WarmCodeCache<Db> wrapper value itself is per-block
//!    → BotStateDb (forwarding storage fallback)
//!      → WrapDatabaseAsync<AlloyDB> (RPC cold-miss)
//! ```
//!
//! ## Ownership
//!
//! The wrapper value (`WarmCodeCache<Db>`) is per-block — constructed in
//! `BlockSimHandle::build`, holds the per-block `Db`, drops at end of block.
//! Only the inner map (`Arc<RwLock<WarmCodeCacheInner>>`) persists across
//! blocks — cloned from the engine owner (`PyArbitrageEngine` / the
//! standalone Rust `Bot`). This preserves the per-block `CacheDB` discipline
//! while sharing the bytecode cache across blocks.

// Solidity/EVM + Rust-ecosystem identifiers (EIP-6780, EIP-1967, DatabaseRef,
// CacheDB, WrapDatabaseAsync, BlockSimHandle, etc.) are ubiquitous here —
// match `degenbot-simulation`'s convention.

use alloy::primitives::map::{AddressHashMap, B256HashMap};
use alloy::primitives::Address;
use parking_lot::RwLock;
use revm::bytecode::Bytecode;
use revm::database_interface::DatabaseRef;
use revm::primitives::{StorageKey, StorageValue, B256};
use revm::state::AccountInfo;
use std::sync::Arc;

/// The default per-entry TTL in blocks — ≈ 1 day of mainnet blocks (12s
/// block time → ~7200 blocks/hour → ~10 000 ≈ 1.4 days).
///
/// A safety net, not the primary correctness mechanism — in the bot's domain
/// every cached contract is immutable (EIP-1967 proxies upgrade via the
/// implementation *storage* slot, which this cache does NOT serve; the
/// injected executor is per-block via `stateOverrides`). See the module doc
/// for the EIP-6780 selfdestruct + future-protocol-change analysis.
pub const WARM_CODE_CACHE_TTL_BLOCKS: u64 = 10_000;

/// The cross-block persistent inner state — bytecode + account-existence maps,
/// each entry tagged with the block it was loaded at for per-entry TTL expiry.
///
/// Held behind `Arc<RwLock<...>>` so the engine owner (`PyArbitrageEngine` /
/// the standalone Rust `Bot`) shares one instance across every per-block
/// `WarmCodeCache` wrapper value. The maps grow with the working set (a few
/// hundred contracts for one bot's pool-set); there is no LRU eviction in
/// Tier 1 — unbounded growth is not a near-term concern (a follow-up if
/// measurement shows otherwise).
pub struct WarmCodeCacheInner {
    /// `(loaded_block, AccountInfo)` per address. `AccountInfo` carries
    /// `code_hash` (the bytecode is fetched separately via `code_by_hash_ref`,
    /// or eagerly inline in `basic_ref` — revm's `AlloyDB` does the latter).
    /// `None` (no-account) IS cached (existence-negative) — saves the `basic`
    /// RPC on the no-code-EOA case the simulate's `balanceOf`-to-fresh-
    /// addresses hits.
    accounts: AddressHashMap<(u64, Option<AccountInfo>)>,
    /// `(loaded_block, Bytecode)` per `code_hash` (immutable by hash).
    bytecode: B256HashMap<(u64, Bytecode)>,
    /// The fixed-offset TTL in blocks. An entry loaded at `loaded_block` is
    /// a miss when `block - loaded_block > ttl_blocks` (strictly greater — an
    /// entry loaded THIS block is always fresh).
    ttl_blocks: u64,
}

impl WarmCodeCacheInner {
    /// Construct an empty inner state with the given per-entry TTL.
    #[must_use]
    fn new(ttl_blocks: u64) -> Self {
        Self {
            accounts: AddressHashMap::default(),
            bytecode: B256HashMap::default(),
            ttl_blocks,
        }
    }

    /// Construct a shared (`Arc<RwLock<...>>`) empty inner state with the
    /// default TTL — the engine-owner construction shape (the `PyArbitrageEngine`
    /// / standalone Rust `Bot` calls this once at construction + holds the arc
    /// for the engine's life, cloning it into each per-block
    /// `WarmCodeCache::with_owner`).
    #[must_use]
    pub fn shared_default() -> Arc<RwLock<Self>> {
        Arc::new(RwLock::new(Self::new(WARM_CODE_CACHE_TTL_BLOCKS)))
    }

    /// Construct a shared inner state with a custom TTL (for tests + the
    /// benchmark's TTL-boundary assertion).
    #[must_use]
    pub fn shared_with_ttl(ttl_blocks: u64) -> Arc<RwLock<Self>> {
        Arc::new(RwLock::new(Self::new(ttl_blocks)))
    }

    /// Is an entry loaded at `loaded_block` still fresh at `block`?
    /// Fresh iff `block - loaded_block <= ttl_blocks` (an entry loaded THIS
    /// block is always fresh — `block - loaded_block = 0 <= ttl`).
    fn is_fresh(&self, loaded_block: u64, block: u64) -> bool {
        block.saturating_sub(loaded_block) <= self.ttl_blocks
    }
}

/// A cross-block warm `DatabaseRef` wrapper — caches `basic_ref` +
/// `code_by_hash_ref` (the immutable rows) with a per-entry TTL, forwards
/// `storage_ref` + `block_hash_ref` to the inner `Db` (the mutable-per-block
/// row, never cached).
///
/// The wrapper value is per-block (constructed in `BlockSimHandle::build`,
/// holds the per-block `Db`); only the inner `Arc<RwLock<WarmCodeCacheInner>>`
/// persists across blocks — cloned from the engine owner. See the module doc.
///
/// # Errors
///
/// `Error = Db::Error` — the cache layer is infallible; errors surface from
/// the inner `Db` on a cache miss (the RPC cold-load path).
pub struct WarmCodeCache<Db>
where
    Db: DatabaseRef,
{
    /// The cross-block persistent maps (bytecode + account-existence), shared
    /// via the engine owner's arc.
    cache: Arc<RwLock<WarmCodeCacheInner>>,
    /// The block this cache view is for (drives per-entry TTL expiry checks).
    block: u64,
    /// The per-block `DatabaseRef` (forwards `storage_ref` + `block_hash_ref`
    /// untouched; serves as the cold-miss fallback for `basic_ref` +
    /// `code_by_hash_ref`).
    db: Db,
}

impl<Db> WarmCodeCache<Db>
where
    Db: DatabaseRef,
{
    /// Build a per-block view over a shared inner cache + a per-block `Db`.
    /// The `cache` arc is cloned from the engine owner; the `block` drives
    /// per-entry TTL expiry; the `db` is the per-block fallback
    /// (`BotStateDb<WrapDatabaseAsync<AlloyDB>>` in production).
    #[must_use]
    pub fn with_owner(cache: Arc<RwLock<WarmCodeCacheInner>>, block: u64, db: Db) -> Self {
        Self { cache, block, db }
    }
}

impl<Db> DatabaseRef for WarmCodeCache<Db>
where
    Db: DatabaseRef,
{
    type Error = Db::Error;

    /// Serve from the warm cache if the entry is fresh; else forward to the
    /// inner `Db` + cache the result (the existence-negative `None` IS cached
    /// — saves the `basic` RPC on the no-code-EOA case).
    ///
    /// # Errors
    ///
    /// Returns the inner `Db`'s error on a cache miss (the RPC `basic` fetch).
    fn basic_ref(&self, address: Address) -> Result<Option<AccountInfo>, Self::Error> {
        // Fast path: read-locked cache hit (fresh entry).
        {
            let guard = self.cache.read();
            if let Some((loaded_block, info)) = guard.accounts.get(&address) {
                if guard.is_fresh(*loaded_block, self.block) {
                    // SIMPIPE T1 lab: warm-cache served (cross-block/candidate
                    // reuse signal).
                    super::sim_metrics::record_basic_warm_hit();
                    return Ok(info.clone());
                }
            }
        }
        // Miss (or stale): forward. EOA rows are NEVER cached — an EOA's
        // nonce + balance mutate every block, and the warm layer is
        // cross-block: caching them froze frame-replay nonce validation
        // at its first touch for a full TTL window (observed live as replay
        // errors stuck at the capture-time `expected` across head advances).
        let (info, dur) = super::sim_metrics::timed(|| self.db.basic_ref(address));
        super::sim_metrics::record_basic(dur);
        let info = info?;
        let is_contract = info
            .as_ref()
            .is_some_and(|i| i.code_hash != revm::primitives::KECCAK_EMPTY);
        if is_contract {
            let mut guard = self.cache.write();
            guard.accounts.insert(address, (self.block, info.clone()));
        }
        Ok(info)
    }

    /// Serve bytecode from the warm cache if the entry is fresh; else forward
    /// to the inner `Db` + cache the result.
    ///
    /// In revm's `AlloyDB`, `basic_ref` eagerly loads code inline — so
    /// `code_by_hash_ref` is only reached when `basic_ref` returned a
    /// `code_hash` without the code (the spike-verified panic-safety
    /// invariant — never in production). Caching here is defensive + handles
    /// a `Db` whose `basic_ref` does NOT eagerly load code.
    ///
    /// # Errors
    ///
    /// Returns the inner `Db`'s error on a cache miss.
    fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
        {
            let guard = self.cache.read();
            if let Some((loaded_block, code)) = guard.bytecode.get(&code_hash) {
                if guard.is_fresh(*loaded_block, self.block) {
                    return Ok(code.clone());
                }
            }
        }
        let (code, dur) = super::sim_metrics::timed(|| self.db.code_by_hash_ref(code_hash));
        super::sim_metrics::record_code(dur);
        let code = code?;
        let mut guard = self.cache.write();
        guard.bytecode.insert(code_hash, (self.block, code.clone()));
        Ok(code)
    }

    /// Forward to the inner `Db` — storage is the mutable-per-block row (V2
    /// reserves slot 8, V3 `slot0`/`liquidity`/`ticks` change every block
    /// there's a swap). NEVER cached (caching would re-introduce the
    /// K-invariant / stale-state-divergence reverts the option-B path hit).
    ///
    /// # Errors
    ///
    /// Returns the inner `Db`'s error if the RPC fetch fails.
    fn storage_ref(
        &self,
        address: Address,
        index: StorageKey,
    ) -> Result<StorageValue, Self::Error> {
        // SIMPIPE T1 lab: storage ALWAYS forwards — the latency split sizes
        // the anchor-served (fast) vs RPC-reached (slow) shares.
        let (value, dur) = super::sim_metrics::timed(|| self.db.storage_ref(address, index));
        super::sim_metrics::record_storage(dur);
        value
    }

    /// Forward to the inner `Db` — block hashes are per-chain, not
    /// per-block-snapshot. NEVER cached.
    ///
    /// # Errors
    ///
    /// Returns the inner `Db`'s error if the RPC fetch fails.
    fn block_hash_ref(&self, number: u64) -> Result<B256, Self::Error> {
        super::sim_metrics::record_block_hash();
        self.db.block_hash_ref(number)
    }
}

#[expect(clippy::expect_used)]
#[cfg(test)]
mod tests {
    use super::*;
    use alloy::primitives::{address, U256};
    use revm::database_interface::EmptyDB;
    use std::cell::Cell;

    /// A counting mock `DatabaseRef` — wraps `EmptyDB` + records every call.
    /// `EmptyDB` returns `None` for `basic_ref` + `KECCAK_EMPTY` bytecode for
    /// `code_by_hash_ref` (both error-free), so the mock serves as the
    /// cold-miss fallback the warm cache forwards to.
    #[expect(clippy::struct_field_names)]
    struct CountingMockDb {
        basic_calls: Cell<u64>,
        code_by_hash_calls: Cell<u64>,
        storage_calls: Cell<u64>,
        block_hash_calls: Cell<u64>,
    }

    impl CountingMockDb {
        const EMPTY_CODE_HASH: B256 = B256::ZERO;
        fn new() -> Self {
            Self {
                basic_calls: Cell::new(0),
                code_by_hash_calls: Cell::new(0),
                storage_calls: Cell::new(0),
                block_hash_calls: Cell::new(0),
            }
        }
    }

    impl DatabaseRef for CountingMockDb {
        type Error = core::convert::Infallible;
        fn basic_ref(&self, _address: Address) -> Result<Option<AccountInfo>, Self::Error> {
            self.basic_calls.set(self.basic_calls.get() + 1);
            // Return a trivially-populated AccountInfo (some balance + the
            // sentinel code hash) so the cache stores a non-vacuous value.
            Ok(Some(AccountInfo::new(
                U256::from(1),
                0,
                Self::EMPTY_CODE_HASH,
                revm::bytecode::Bytecode::new_legacy(alloy::primitives::Bytes::from_static(&[
                    0x00,
                ])),
            )))
        }
        fn code_by_hash_ref(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
            self.code_by_hash_calls
                .set(self.code_by_hash_calls.get() + 1);
            Ok(revm::bytecode::Bytecode::new_legacy(
                alloy::primitives::Bytes::new(),
            ))
        }
        fn storage_ref(
            &self,
            _address: Address,
            _index: StorageKey,
        ) -> Result<StorageValue, Self::Error> {
            self.storage_calls.set(self.storage_calls.get() + 1);
            Ok(StorageValue::ZERO)
        }
        fn block_hash_ref(&self, _number: u64) -> Result<B256, Self::Error> {
            self.block_hash_calls.set(self.block_hash_calls.get() + 1);
            Ok(B256::ZERO)
        }
    }

    const ADDR: Address = address!("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

    fn view(
        cache: &Arc<RwLock<WarmCodeCacheInner>>,
        block: u64,
        db: CountingMockDb,
    ) -> WarmCodeCache<CountingMockDb> {
        WarmCodeCache::with_owner(Arc::clone(cache), block, db)
    }

    // ── Acceptance test 1: cache hit avoids second DB read ──────────────

    #[test]
    fn basic_ref_second_call_same_block_hits_cache() {
        let inner = WarmCodeCacheInner::shared_with_ttl(10);
        let v1 = view(&inner, 100, CountingMockDb::new());
        let _ = v1.basic_ref(ADDR);
        // Second call — a FRESH wrapper struct reusing the SAME arc, same block.
        let v2 = view(&inner, 100, CountingMockDb::new());
        let _ = v2.basic_ref(ADDR);
        // Only the first wrapper's db saw a call; the second hit the cache.
        // NOTE: each view has its own CountingMockDb, so we assert via the
        // shared inner map instead: the entry is present + loaded at block 100.
        let guard = inner.read();
        assert_eq!(guard.accounts.len(), 1, "exactly one cached account");
        let (loaded_block, info) = guard.accounts.get(&ADDR).expect("cached");
        assert_eq!(*loaded_block, 100);
        assert!(info.is_some(), "cached value is Some");
    }

    // ── Acceptance test 1b: the mock is actually hit on the FIRST call ──

    #[test]
    fn basic_ref_first_call_hits_the_db() {
        let inner = WarmCodeCacheInner::shared_with_ttl(10);
        let db = CountingMockDb::new();
        let v = view(&inner, 100, db);
        let _ = v.basic_ref(ADDR);
        // The db the view owns records the call. Verify via the shared inner
        // (the entry was inserted — only happens after a db forward).
        assert!(inner.read().accounts.contains_key(&ADDR));
    }

    // ── Acceptance test 2: TTL expiry re-fetches ────────────────────────

    #[test]
    fn ttl_expiry_refetches_on_stale_read() {
        // ttl = 5. Load at block 10 (fresh). Read at block 15 (15-10=5 <= 5,
        // still fresh). Read at block 16 (16-10=6 > 5, stale → re-fetch).
        let inner = WarmCodeCacheInner::shared_with_ttl(5);
        // Load at block 10.
        let v = view(&inner, 10, CountingMockDb::new());
        let info10 = v.basic_ref(ADDR).unwrap();
        let loaded_block_10 = inner.read().accounts.get(&ADDR).expect("cached").0;
        assert_eq!(loaded_block_10, 10);
        // Read at block 15 — fresh (15-10=5 <= 5), entry NOT rewritten.
        let v15 = view(&inner, 15, CountingMockDb::new());
        let info15 = v15.basic_ref(ADDR).unwrap();
        assert_eq!(info15, info10, "fresh read returns the cached value");
        let loaded_block_15 = inner.read().accounts.get(&ADDR).expect("cached").0;
        assert_eq!(
            loaded_block_15, 10,
            "fresh read does NOT rewrite loaded_block"
        );
        // Read at block 16 — stale (16-10=6 > 5), re-fetch + rewrite.
        let v16 = view(&inner, 16, CountingMockDb::new());
        let _ = v16.basic_ref(ADDR).unwrap();
        let loaded_block_16 = inner.read().accounts.get(&ADDR).expect("cached").0;
        assert_eq!(
            loaded_block_16, 16,
            "stale read re-fetches + rewrites loaded_block"
        );
    }

    // ── Acceptance test 3: storage_ref NEVER cached ─────────────────────

    #[test]
    fn storage_ref_is_never_cached() {
        let inner = WarmCodeCacheInner::shared_with_ttl(100);
        let db = CountingMockDb::new();
        let v = view(&inner, 100, db);
        let slot = StorageKey::ZERO;
        // Two reads of the same (addr, slot) at the same block.
        let _ = v.storage_ref(ADDR, slot);
        let _ = v.storage_ref(ADDR, slot);
        assert_eq!(
            v.db.storage_calls.get(),
            2,
            "both storage reads hit the inner db (never cached)"
        );
        assert!(
            inner.read().accounts.is_empty(),
            "storage reads never populate the accounts map"
        );
    }

    // ── Acceptance test 4: block_hash_ref NEVER cached ───────────────────

    #[test]
    fn block_hash_ref_is_never_cached() {
        let inner = WarmCodeCacheInner::shared_with_ttl(100);
        let db = CountingMockDb::new();
        let v = view(&inner, 100, db);
        let _ = v.block_hash_ref(50);
        let _ = v.block_hash_ref(50);
        assert_eq!(
            v.db.block_hash_calls.get(),
            2,
            "both block_hash reads hit the inner db (never cached)"
        );
    }

    // ── Acceptance test 5: code_by_hash_ref caches + TTLs ────────────────

    #[test]
    fn code_by_hash_ref_caches_and_expires() {
        let inner = WarmCodeCacheInner::shared_with_ttl(5);
        let hash = B256::repeat_byte(0xab);
        // Load at block 20.
        let v = view(&inner, 20, CountingMockDb::new());
        let code20 = v.code_by_hash_ref(hash).unwrap();
        assert_eq!(v.db.code_by_hash_calls.get(), 1, "first call hits the db");
        let loaded_block_20 = inner.read().bytecode.get(&hash).expect("cached").0;
        assert_eq!(loaded_block_20, 20);
        // Read at block 25 (25-20=5 <= 5) — fresh, cache hit.
        let v25 = view(&inner, 25, CountingMockDb::new());
        let code25 = v25.code_by_hash_ref(hash).unwrap();
        assert_eq!(
            code25.hash_slow(),
            code20.hash_slow(),
            "fresh returns cached"
        );
        let loaded_block_25 = inner.read().bytecode.get(&hash).expect("cached").0;
        assert_eq!(loaded_block_25, 20, "fresh read does NOT rewrite");
        // Read at block 26 (26-20=6 > 5) — stale, re-fetch + rewrite.
        let v26 = view(&inner, 26, CountingMockDb::new());
        let _ = v26.code_by_hash_ref(hash).unwrap();
        let loaded_block_26 = inner.read().bytecode.get(&hash).expect("cached").0;
        assert_eq!(loaded_block_26, 26, "stale re-fetches + rewrites");
    }

    // ── Acceptance test 6: None (no-account) IS cached ───────────────────

    #[test]
    fn basic_ref_none_is_not_cached() {
        // A None-returning fallback: an absent account is an EOA-shaped row
        // (nonce/balance mutate per-block) and must NOT enter the cross-block
        // warm cache: caching it froze sender nonces at first touch here.
        struct NoneDb {
            calls: Cell<u64>,
        }
        impl DatabaseRef for NoneDb {
            type Error = core::convert::Infallible;
            fn basic_ref(&self, _address: Address) -> Result<Option<AccountInfo>, Self::Error> {
                self.calls.set(self.calls.get() + 1);
                Ok(None)
            }
            fn code_by_hash_ref(&self, _code_hash: B256) -> Result<Bytecode, Self::Error> {
                Ok(Bytecode::new_legacy(alloy::primitives::Bytes::new()))
            }
            fn storage_ref(
                &self,
                _address: Address,
                _index: StorageKey,
            ) -> Result<StorageValue, Self::Error> {
                Ok(StorageValue::ZERO)
            }
            fn block_hash_ref(&self, _number: u64) -> Result<B256, Self::Error> {
                Ok(B256::ZERO)
            }
        }
        let inner = WarmCodeCacheInner::shared_with_ttl(100);
        let db = NoneDb {
            calls: Cell::new(0),
        };
        let v = WarmCodeCache::with_owner(Arc::clone(&inner), 100, db);
        let info1 = v.basic_ref(ADDR).unwrap();
        assert!(info1.is_none(), "first call returns None");
        assert_eq!(v.db.calls.get(), 1, "first call hit the db");
        let info2 = v.basic_ref(ADDR).unwrap();
        assert!(info2.is_none(), "second call returns None again");
        assert_eq!(
            v.db.calls.get(),
            2,
            "EOA-shaped rows never cache — the second call re-queries"
        );
    }

    // ── Acceptance test 7: an EmptyDB-backed view compiles + forwards ────

    #[test]
    fn warm_code_cache_compiles_over_empty_db() {
        // The smoke-test shape: a WarmCodeCache<EmptyDB> (no RPC, no BotState).
        // EmptyDB::basic_ref returns Ok(None) (no account) — an EOA-shaped
        // row the warm cache deliberately NEVER caches (nonce/balance mutate
        // per-block on real chains). This proves the type bound
        // (no Display requirement on Error) compiles + the forwarding path
        // works.
        let inner = WarmCodeCacheInner::shared_default();
        let v = WarmCodeCache::with_owner(Arc::clone(&inner), 1, EmptyDB::default());
        let info = v.basic_ref(ADDR).unwrap();
        assert!(info.is_none(), "EmptyDB returns no account");
        assert_eq!(
            inner.read().accounts.len(),
            0,
            "Ok(None) is never cached — EOA-shaped rows stay live"
        );
    }

    // ── Ensure no pyo3 in this core crate (the invariant) ────────────────

    #[test]
    fn warm_code_cache_module_has_no_pyo3_dependency() {
        // The no-pyo3-in-cores invariant is enforced by `just
        // check-no-pyo3-in-cores` at the crate level; this test is a
        // documentation anchor (the module imports no pyo3 symbols). The
        // module-level `use` list above (parking_lot, alloy, revm, std) is the
        // proof — no `pyo3` import exists in this file.
    }
}