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
//! Manager-authored acceptance tests for the reactive runtime feature.
//!
//! These tests intentionally describe the new public contract before the
//! implementation exists. They should fail on the current log-only event pipeline
//! and pass once `evm_fork_cache::reactive` provides a provider-agnostic handler
//! runtime.
#![cfg(feature = "reactive")]

mod common;

use std::sync::{Arc, Mutex};

use alloy_network::Ethereum;
use alloy_primitives::{Address, B256, Bytes, Log as PrimitiveLog, U256, keccak256};
use alloy_rpc_types_eth::{Filter, Log};
use anyhow::Result;

use common::{install_mock_erc20, setup_cache};
use evm_fork_cache::events::StateView;
use evm_fork_cache::reactive::{
    AppliedReport, BlockRef, ChainStatus, HandlerError, HandlerId, HandlerOutcome, HookSignal,
    InputRef, InputSource, InvalidationReason, InvalidationRequest, LogInterest, LogMatcher,
    PendingTxInterest, ReactiveConfig, ReactiveContext, ReactiveEffect, ReactiveError,
    ReactiveHandler, ReactiveHook, ReactiveInput, ReactiveInputBatch, ReactiveInputRecord,
    ReactiveInterest, ReactiveReport, ReactiveRuntime, ReportTag, ResyncBlock, ResyncId,
    ResyncPriority, ResyncReason, ResyncRequest, ResyncTarget, RouteKeySpec, SpeculativeId,
    SpeculativeRequest, StateEffectQuality,
};
use evm_fork_cache::{PurgeScope, StateUpdate};

fn rpc_log(
    address: Address,
    topics: Vec<B256>,
    block_number: u64,
    tx_index: u64,
    log_index: u64,
) -> Log {
    let block_hash = B256::repeat_byte(block_number as u8);
    Log {
        inner: PrimitiveLog::new_unchecked(address, topics, Bytes::new()),
        block_hash: Some(block_hash),
        block_number: Some(block_number),
        block_timestamp: Some(1_700_000_000 + block_number),
        transaction_hash: Some(B256::repeat_byte((tx_index + 1) as u8)),
        transaction_index: Some(tx_index),
        log_index: Some(log_index),
        removed: false,
    }
}

fn included_context(block_number: u64, log_index: u64) -> ReactiveContext {
    let block = BlockRef {
        number: block_number,
        hash: B256::repeat_byte(block_number as u8),
        parent_hash: Some(B256::repeat_byte(block_number.saturating_sub(1) as u8)),
        timestamp: Some(1_700_000_000 + block_number),
    };

    ReactiveContext {
        chain_id: Some(1),
        source: InputSource::Batch,
        chain_status: ChainStatus::Included {
            block: block.clone(),
            confirmations: 0,
        },
        block: Some(block),
        transaction_index: Some(0),
        log_index: Some(log_index),
    }
}

fn batch(records: Vec<(ReactiveInput<Ethereum>, ReactiveContext)>) -> ReactiveInputBatch<Ethereum> {
    ReactiveInputBatch::new(
        records
            .into_iter()
            .map(|(input, ctx)| ReactiveInputRecord::new(input, ctx))
            .collect(),
    )
}

#[derive(Clone)]
struct SlotWriter {
    id: HandlerId,
    interest: ReactiveInterest,
    slot: U256,
    value: U256,
    hook_kind: &'static str,
}

impl SlotWriter {
    fn any_log_from(id: &'static str, address: Address, slot: U256, value: U256) -> Self {
        Self {
            id: HandlerId::new(id),
            interest: ReactiveInterest::Logs(LogInterest {
                provider_filter: Filter::new().address(address),
                local_matcher: None,
                route_key: Some(RouteKeySpec::EmitterAddress),
            }),
            slot,
            value,
            hook_kind: "slot.write",
        }
    }
}

impl ReactiveHandler<Ethereum> for SlotWriter {
    fn id(&self) -> HandlerId {
        self.id.clone()
    }

    fn interests(&self) -> Vec<ReactiveInterest> {
        vec![self.interest.clone()]
    }

    fn handle(
        &self,
        _ctx: &ReactiveContext,
        input: &ReactiveInput<Ethereum>,
        _state: &dyn StateView,
    ) -> Result<HandlerOutcome, HandlerError> {
        let ReactiveInput::Log(log) = input else {
            return Ok(HandlerOutcome::empty(StateEffectQuality::NoStateEffect));
        };

        Ok(HandlerOutcome {
            effects: vec![
                ReactiveEffect::StateUpdate(StateUpdate::slot(
                    log.address(),
                    self.slot,
                    self.value,
                )),
                ReactiveEffect::Hook(HookSignal {
                    namespace: "test".into(),
                    kind: self.hook_kind.into(),
                    labels: vec![ReportTag::new("handler", self.id.as_str())],
                    payload: None,
                }),
            ],
            quality: StateEffectQuality::ExactFromInput,
            tags: vec![ReportTag::new("slot", self.slot.to_string())],
        })
    }
}

struct LogIndexWriter {
    id: HandlerId,
    address: Address,
    slot: U256,
}

impl ReactiveHandler<Ethereum> for LogIndexWriter {
    fn id(&self) -> HandlerId {
        self.id.clone()
    }

    fn interests(&self) -> Vec<ReactiveInterest> {
        vec![ReactiveInterest::Logs(LogInterest {
            provider_filter: Filter::new().address(self.address),
            local_matcher: None,
            route_key: Some(RouteKeySpec::EmitterAddress),
        })]
    }

    fn handle(
        &self,
        ctx: &ReactiveContext,
        input: &ReactiveInput<Ethereum>,
        _state: &dyn StateView,
    ) -> Result<HandlerOutcome, HandlerError> {
        let ReactiveInput::Log(log) = input else {
            return Ok(HandlerOutcome::empty(StateEffectQuality::NoStateEffect));
        };
        let value = U256::from(ctx.log_index.expect("log context carries log index"));
        Ok(HandlerOutcome {
            effects: vec![ReactiveEffect::StateUpdate(StateUpdate::slot(
                log.address(),
                self.slot,
                value,
            ))],
            quality: StateEffectQuality::ExactFromInput,
            tags: vec![],
        })
    }
}

struct TopicMatcher {
    index: usize,
    value: B256,
}

impl LogMatcher for TopicMatcher {
    fn matches(&self, log: &Log) -> bool {
        log.topics().get(self.index) == Some(&self.value)
    }
}

#[derive(Default)]
struct RecordingHook {
    applied_values: Arc<Mutex<Vec<U256>>>,
    hook_signals: Arc<Mutex<Vec<String>>>,
}

impl ReactiveHook<Ethereum> for RecordingHook {
    fn on_report(&self, report: Arc<ReactiveReport<Ethereum>>) {
        if let ReactiveReport::Applied(AppliedReport {
            diff, hook_signals, ..
        }) = report.as_ref()
        {
            self.applied_values
                .lock()
                .unwrap()
                .extend(diff.slots.iter().map(|change| change.new));
            self.hook_signals.lock().unwrap().extend(
                hook_signals
                    .iter()
                    .map(|signal| format!("{}:{}", signal.namespace, signal.kind)),
            );
        }
    }
}

#[tokio::test]
async fn reactive_runtime_applies_state_updates_and_dispatches_applied_hooks() -> Result<()> {
    let emitter = Address::repeat_byte(0x71);
    let slot = U256::from(7);
    let value = U256::from(99);
    let mut cache = setup_cache().await?;
    install_mock_erc20(&mut cache, emitter);

    let hook = Arc::new(RecordingHook::default());
    let mut runtime = ReactiveRuntime::<Ethereum>::new(ReactiveConfig::default());
    runtime.register_handler(Arc::new(SlotWriter::any_log_from(
        "writer", emitter, slot, value,
    )))?;
    runtime.register_hook(hook.clone())?;

    let report = runtime.ingest_batch(
        &mut cache,
        batch(vec![(
            ReactiveInput::Log(rpc_log(emitter, vec![keccak256(b"Write()")], 10, 0, 0)),
            included_context(10, 0),
        )]),
    )?;

    assert_eq!(cache.cached_storage_value(emitter, slot), Some(value));
    assert_eq!(report.applied.len(), 1);
    assert_eq!(report.applied[0].handler_id, HandlerId::new("writer"));
    assert_eq!(
        report.applied[0].quality,
        StateEffectQuality::ExactFromInput
    );
    assert_eq!(*hook.applied_values.lock().unwrap(), vec![value]);
    assert_eq!(
        *hook.hook_signals.lock().unwrap(),
        vec!["test:slot.write".to_string()]
    );
    Ok(())
}

#[tokio::test]
async fn reactive_runtime_orders_logs_dedupes_inputs_and_allows_sequential_writes() -> Result<()> {
    let emitter = Address::repeat_byte(0x72);
    let slot = U256::from(8);
    let mut cache = setup_cache().await?;
    install_mock_erc20(&mut cache, emitter);

    let mut runtime = ReactiveRuntime::<Ethereum>::new(ReactiveConfig::default());
    runtime.register_handler(Arc::new(LogIndexWriter {
        id: HandlerId::new("log-index-writer"),
        address: emitter,
        slot,
    }))?;

    let topic = keccak256(b"Ordered()");
    let block = 20;
    let report = runtime.ingest_batch(
        &mut cache,
        batch(vec![
            (
                ReactiveInput::Log(rpc_log(emitter, vec![topic], block, 0, 2)),
                included_context(block, 2),
            ),
            (
                ReactiveInput::Log(rpc_log(emitter, vec![topic], block, 0, 1)),
                included_context(block, 1),
            ),
            (
                ReactiveInput::Log(rpc_log(emitter, vec![topic], block, 0, 1)),
                included_context(block, 1),
            ),
        ]),
    )?;

    let applied_log_indexes: Vec<u64> = report
        .applied
        .iter()
        .map(|applied| match applied.input_ref {
            InputRef::Log { log_index, .. } => log_index,
            _ => panic!("expected log input ref"),
        })
        .collect();

    assert_eq!(applied_log_indexes, vec![1, 2]);
    assert_eq!(
        cache.cached_storage_value(emitter, slot),
        Some(U256::from(2))
    );
    Ok(())
}

#[tokio::test]
async fn reactive_runtime_routes_shared_emitters_with_local_topic_matchers() -> Result<()> {
    let vault = Address::repeat_byte(0x73);
    let pool_a = B256::repeat_byte(0xa0);
    let pool_b = B256::repeat_byte(0xb0);
    let slot = U256::from(9);
    let mut cache = setup_cache().await?;
    install_mock_erc20(&mut cache, vault);

    let mut handler_a = SlotWriter::any_log_from("pool-a", vault, slot, U256::from(100));
    handler_a.interest = ReactiveInterest::Logs(LogInterest {
        provider_filter: Filter::new().address(vault),
        local_matcher: Some(Arc::new(TopicMatcher {
            index: 1,
            value: pool_a,
        })),
        route_key: Some(RouteKeySpec::Topic { index: 1 }),
    });

    let mut handler_b = SlotWriter::any_log_from("pool-b", vault, slot, U256::from(200));
    handler_b.interest = ReactiveInterest::Logs(LogInterest {
        provider_filter: Filter::new().address(vault),
        local_matcher: Some(Arc::new(TopicMatcher {
            index: 1,
            value: pool_b,
        })),
        route_key: Some(RouteKeySpec::Topic { index: 1 }),
    });

    let mut runtime = ReactiveRuntime::<Ethereum>::new(ReactiveConfig::default());
    runtime.register_handler(Arc::new(handler_a))?;
    runtime.register_handler(Arc::new(handler_b))?;

    let report = runtime.ingest_batch(
        &mut cache,
        batch(vec![(
            ReactiveInput::Log(rpc_log(
                vault,
                vec![keccak256(b"Swap(bytes32)"), pool_b],
                30,
                0,
                0,
            )),
            included_context(30, 0),
        )]),
    )?;

    assert_eq!(report.applied.len(), 1);
    assert_eq!(report.applied[0].handler_id, HandlerId::new("pool-b"));
    assert_eq!(
        cache.cached_storage_value(vault, slot),
        Some(U256::from(200))
    );
    Ok(())
}

#[tokio::test]
async fn reactive_runtime_rejects_conflicting_effects_for_one_input() -> Result<()> {
    let emitter = Address::repeat_byte(0x74);
    let slot = U256::from(10);
    let mut cache = setup_cache().await?;
    let before = cache.cached_storage_value(emitter, slot);

    let mut runtime = ReactiveRuntime::<Ethereum>::new(ReactiveConfig::default());
    runtime.register_handler(Arc::new(SlotWriter::any_log_from(
        "first",
        emitter,
        slot,
        U256::from(1),
    )))?;
    runtime.register_handler(Arc::new(SlotWriter::any_log_from(
        "second",
        emitter,
        slot,
        U256::from(2),
    )))?;

    let err = runtime
        .ingest_batch(
            &mut cache,
            batch(vec![(
                ReactiveInput::Log(rpc_log(emitter, vec![keccak256(b"Conflict()")], 40, 0, 0)),
                included_context(40, 0),
            )]),
        )
        .expect_err("conflicting writes must be rejected before mutation");

    assert!(matches!(err, ReactiveError::ConflictingEffects { .. }));
    assert_eq!(cache.cached_storage_value(emitter, slot), before);
    Ok(())
}

struct PendingCanonicalWriter;

impl ReactiveHandler<Ethereum> for PendingCanonicalWriter {
    fn id(&self) -> HandlerId {
        HandlerId::new("pending-writer")
    }

    fn interests(&self) -> Vec<ReactiveInterest> {
        vec![ReactiveInterest::PendingTransactions(
            PendingTxInterest::default(),
        )]
    }

    fn handle(
        &self,
        _ctx: &ReactiveContext,
        _input: &ReactiveInput<Ethereum>,
        _state: &dyn StateView,
    ) -> Result<HandlerOutcome, HandlerError> {
        Ok(HandlerOutcome {
            effects: vec![ReactiveEffect::StateUpdate(StateUpdate::purge(
                Address::repeat_byte(0x75),
                PurgeScope::AllStorage,
            ))],
            quality: StateEffectQuality::RequiresRepair,
            tags: vec![],
        })
    }
}

#[tokio::test]
async fn reactive_runtime_rejects_canonical_cache_effects_for_pending_inputs() -> Result<()> {
    let mut cache = setup_cache().await?;
    let mut runtime = ReactiveRuntime::<Ethereum>::new(ReactiveConfig::default());
    runtime.register_handler(Arc::new(PendingCanonicalWriter))?;

    let err = runtime
        .ingest_batch(
            &mut cache,
            batch(vec![(
                ReactiveInput::PendingTxHash(B256::repeat_byte(0x99)),
                ReactiveContext {
                    chain_id: Some(1),
                    source: InputSource::Batch,
                    chain_status: ChainStatus::Pending,
                    block: None,
                    transaction_index: None,
                    log_index: None,
                },
            )]),
        )
        .expect_err("pending inputs must not mutate canonical cache state");

    assert!(matches!(err, ReactiveError::InvalidPendingEffect { .. }));
    Ok(())
}

struct InvalidateAndResyncHandler {
    address: Address,
    slot: U256,
}

impl ReactiveHandler<Ethereum> for InvalidateAndResyncHandler {
    fn id(&self) -> HandlerId {
        HandlerId::new("invalidate-resync")
    }

    fn interests(&self) -> Vec<ReactiveInterest> {
        vec![ReactiveInterest::Logs(LogInterest {
            provider_filter: Filter::new().address(self.address),
            local_matcher: None,
            route_key: Some(RouteKeySpec::EmitterAddress),
        })]
    }

    fn handle(
        &self,
        _ctx: &ReactiveContext,
        _input: &ReactiveInput<Ethereum>,
        _state: &dyn StateView,
    ) -> Result<HandlerOutcome, HandlerError> {
        Ok(HandlerOutcome {
            effects: vec![
                ReactiveEffect::Invalidate(InvalidationRequest {
                    scope: PurgeScope::Slots(vec![self.slot]),
                    address: self.address,
                    reason: InvalidationReason::HandlerRequested,
                }),
                ReactiveEffect::Resync(ResyncRequest {
                    id: ResyncId::new("resync-slot"),
                    reason: ResyncReason::HandlerRequested,
                    block: ResyncBlock::Hash {
                        number: 50,
                        hash: B256::repeat_byte(0x50),
                        require_canonical: true,
                    },
                    targets: vec![ResyncTarget::StorageSlot {
                        address: self.address,
                        slot: self.slot,
                    }],
                    priority: ResyncPriority::High,
                }),
            ],
            quality: StateEffectQuality::AppliedWithPendingResync,
            tags: vec![],
        })
    }
}

#[tokio::test]
async fn reactive_runtime_lowers_invalidations_and_surfaces_resync_requests() -> Result<()> {
    let address = Address::repeat_byte(0x76);
    let slot = U256::from(11);
    let mut cache = setup_cache().await?;
    install_mock_erc20(&mut cache, address);
    cache
        .db_mut()
        .insert_account_storage(address, slot, U256::from(123))?;

    let mut runtime = ReactiveRuntime::<Ethereum>::new(ReactiveConfig::default());
    runtime.register_handler(Arc::new(InvalidateAndResyncHandler { address, slot }))?;

    let report = runtime.ingest_batch(
        &mut cache,
        batch(vec![(
            ReactiveInput::Log(rpc_log(address, vec![keccak256(b"Repair()")], 50, 0, 0)),
            included_context(50, 0),
        )]),
    )?;

    assert_eq!(report.applied.len(), 1);
    assert_eq!(report.applied[0].invalidations.len(), 1);
    assert_eq!(report.applied[0].resyncs.len(), 1);
    assert_eq!(report.applied[0].diff.purged.len(), 1);
    assert_eq!(report.resyncs.len(), 1);
    assert_eq!(report.resyncs[0].priority, ResyncPriority::High);
    Ok(())
}

struct PendingSpeculativeHandler;

impl ReactiveHandler<Ethereum> for PendingSpeculativeHandler {
    fn id(&self) -> HandlerId {
        HandlerId::new("pending-speculative")
    }

    fn interests(&self) -> Vec<ReactiveInterest> {
        vec![ReactiveInterest::PendingTransactions(
            PendingTxInterest::default(),
        )]
    }

    fn handle(
        &self,
        _ctx: &ReactiveContext,
        _input: &ReactiveInput<Ethereum>,
        _state: &dyn StateView,
    ) -> Result<HandlerOutcome, HandlerError> {
        Ok(HandlerOutcome {
            effects: vec![ReactiveEffect::Speculative(SpeculativeRequest {
                id: SpeculativeId::new("pending-signal"),
                input_ref: InputRef::PendingTx {
                    chain_id: None,
                    hash: B256::ZERO,
                },
                labels: vec![ReportTag::new("kind", "pending")],
            })],
            quality: StateEffectQuality::NoStateEffect,
            tags: vec![],
        })
    }
}

#[tokio::test]
async fn reactive_runtime_allows_speculative_pending_effects_without_cache_mutation() -> Result<()>
{
    let mut cache = setup_cache().await?;
    let mut runtime = ReactiveRuntime::<Ethereum>::new(ReactiveConfig::default());
    runtime.register_handler(Arc::new(PendingSpeculativeHandler))?;
    let tx_hash = B256::repeat_byte(0x77);

    let report = runtime.ingest_batch(
        &mut cache,
        batch(vec![(
            ReactiveInput::PendingTxHash(tx_hash),
            ReactiveContext {
                chain_id: Some(1),
                source: InputSource::Batch,
                chain_status: ChainStatus::Pending,
                block: None,
                transaction_index: None,
                log_index: None,
            },
        )]),
    )?;

    assert!(report.applied[0].state_updates.is_empty());
    assert_eq!(report.speculative.len(), 1);
    assert_eq!(
        report.speculative[0].input_ref,
        InputRef::PendingTx {
            chain_id: Some(1),
            hash: tx_hash,
        }
    );
    Ok(())
}