evm-oracle-state 0.2.0

EVM-backed Chainlink-style oracle state tracking over evm-fork-cache
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
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
use std::{
    collections::BTreeSet,
    fmt,
    sync::Arc,
    time::{SystemTime, UNIX_EPOCH},
};

use alloy_network::{Ethereum, Network};
use alloy_primitives::{Address, Bytes, U256};
use alloy_sol_types::SolCall;
use evm_fork_cache::{
    ColdStartCall, ColdStartConfig, ColdStartPlan, ColdStartPlanner, ColdStartResults,
    ColdStartStep, StateView,
    cache::EvmCache,
    reactive::{
        EventSubscriber, HandlerId, InterestOwnerSubscriber, ReactiveBatchReport, ReactiveConfig,
        ReactiveEngine, ReactiveHandler, ReactiveInterest, ReactiveRuntime, SubscriberBackfill,
    },
};

use crate::{
    ChainlinkFeedProvider, FeedConfig, FeedId, FeedRegistration, OracleAdapterFeedSkip,
    OracleAdapterPlugin, OracleCodeRegistry, OracleCodeWarmupPolicy, OracleCodeWarmupReport,
    OracleDiscoveryContext, OracleError, OracleFeedReadinessReport, OracleFeedStatus,
    OracleHookEvent, OraclePrice, OraclePriceCorrected, OraclePriceUpdate, OracleReactiveHandler,
    OracleReadOverlay, OracleReconciler, OracleRegistry, OracleSignal, OracleStorageAdapter,
    OracleStorageSync, OracleTracker, RoundData, StalenessPolicy,
};

type OracleEventCallback = Arc<dyn Fn(&OracleHookEvent) + Send + Sync>;

alloy_sol_types::sol! {
    function latestRoundData() external view returns (
        uint80 roundId,
        int256 answer,
        uint256 startedAt,
        uint256 updatedAt,
        uint80 answeredInRound
    );
}

/// Builder-friendly Chainlink feed registration.
#[derive(Clone, Debug)]
pub struct ChainlinkFeed {
    config: FeedConfig,
}

impl ChainlinkFeed {
    /// Create a feed registration for a Chainlink-compatible proxy.
    pub fn new(proxy: Address) -> Self {
        Self {
            config: FeedConfig {
                proxy,
                ..Default::default()
            },
        }
    }

    /// Set a stable feed id.
    pub fn id(mut self, id: impl Into<String>) -> Self {
        self.config.id = Some(FeedId::new(id));
        self
    }

    /// Set a stable feed id.
    pub fn feed_id(mut self, id: FeedId) -> Self {
        self.config.id = Some(id);
        self
    }

    /// Set a human-readable label.
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.config.label = Some(label.into());
        self
    }

    /// Set the base symbol.
    pub fn base(mut self, base: impl Into<String>) -> Self {
        self.config.base = Some(base.into());
        self
    }

    /// Set the quote symbol.
    pub fn quote(mut self, quote: impl Into<String>) -> Self {
        self.config.quote = Some(quote.into());
        self
    }

    /// Set a max-age staleness policy.
    pub fn max_age_secs(mut self, max_age_secs: u64) -> Self {
        self.config.staleness = StalenessPolicy::max_age(max_age_secs);
        self
    }

    /// Set the full staleness policy.
    pub fn staleness(mut self, staleness: StalenessPolicy) -> Self {
        self.config.staleness = staleness;
        self
    }

    /// Allow zero or negative answers for signed generic feeds.
    pub fn allow_zero_or_negative_answer(mut self, allow: bool) -> Self {
        self.config.staleness = self.config.staleness.allow_zero_or_negative_answer(allow);
        self
    }

    /// Convert into the lower-level registration config.
    pub fn into_config(self) -> FeedConfig {
        self.config
    }
}

impl From<ChainlinkFeed> for FeedConfig {
    fn from(feed: ChainlinkFeed) -> Self {
        feed.into_config()
    }
}

/// Builder for a high-level oracle runtime facade.
pub struct OracleRuntimeBuilder<P> {
    provider: P,
    feeds: Vec<ChainlinkFeed>,
    now_timestamp: Option<u64>,
    storage_sync: OracleStorageSync,
    callbacks: Vec<OracleEventCallback>,
}

/// Cache-native runtime builder with first-class oracle adapter plugins.
pub struct OracleCacheRuntimeBuilder {
    adapters: Vec<Arc<dyn OracleAdapterPlugin>>,
    now_timestamp: Option<u64>,
    storage_sync: OracleStorageSync,
    storage_warmup_enabled: bool,
    storage_warmup_mode: OracleStorageWarmupMode,
    code_registry: OracleCodeRegistry,
    code_warmup_policy: OracleCodeWarmupPolicy,
    callbacks: Vec<OracleEventCallback>,
}

/// Best-effort cache-native oracle runtime build report.
#[non_exhaustive]
#[derive(Debug)]
pub struct OracleCacheRuntimeBuildReport {
    /// Runtime seeded with every successfully discovered feed.
    pub runtime: OracleRuntime<()>,
    /// Feeds that were skipped because an adapter could not register them.
    pub skipped: Vec<OracleAdapterFeedSkip>,
    /// Feed/runtime readiness after discovery, warmup, and skipped-source classification.
    pub feed_statuses: Vec<OracleFeedReadinessReport>,
    /// Storage slots warmed for direct oracle event writes.
    pub storage_warmup: OracleStorageWarmupReport,
    /// Bytecode seed/verify/etch results.
    pub code_warmup: OracleCodeWarmupReport,
}

/// Result of prewarming oracle storage slots through `evm-fork-cache`.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct OracleStorageWarmupReport {
    /// Warmup mode used for this run.
    pub mode: OracleStorageWarmupMode,
    /// Per-feed readiness after storage warmup.
    pub feed_statuses: Vec<OracleFeedReadinessReport>,
    /// Slots requested for warmup.
    pub requested_slots: usize,
    /// Slots successfully loaded into the cache.
    pub loaded_slots: usize,
    /// Slots that failed to load.
    pub failed_slots: Vec<OracleStorageWarmupFailure>,
    /// Read calls submitted to cold-start discovery.
    pub discovery_calls: usize,
    /// Cold-start summary when the warmup used `EvmCache::run_cold_start`.
    pub cold_start: Option<OracleColdStartWarmupReport>,
}

impl OracleStorageWarmupReport {
    /// Return true when no storage warmup work was requested or performed.
    pub fn is_empty(&self) -> bool {
        self.requested_slots == 0
            && self.loaded_slots == 0
            && self.failed_slots.is_empty()
            && self.discovery_calls == 0
            && self.cold_start.is_none()
    }
}

/// Storage warmup implementation to use for oracle direct-write slots.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum OracleStorageWarmupMode {
    /// Bulk-fetch declared hot slots through `EvmCache::prewarm_slots`.
    #[default]
    PrewarmSlots,
    /// Verify declared hot slots through `EvmCache::run_cold_start`.
    ColdStart,
}

/// Summary of an oracle cold-start storage warmup run.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct OracleColdStartWarmupReport {
    /// Cold-start rounds executed.
    pub rounds: usize,
    /// Total slots requested in verify phases.
    pub verified_slots: usize,
    /// Slots whose fetched value changed and was injected.
    pub changed_slots: usize,
    /// Total fetch failures across verify/probe phases.
    pub failed_slots: usize,
    /// Total storage slots discovered by optional discovery calls.
    pub discovered_slots: usize,
    /// Total accounts discovered by optional discovery calls.
    pub discovered_accounts: usize,
    /// Total discovery calls executed.
    pub discover_calls: usize,
}

/// One failed oracle storage warmup slot.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OracleStorageWarmupFailure {
    /// Contract address.
    pub address: Address,
    /// Storage slot.
    pub slot: U256,
    /// Fetch error message.
    pub reason: String,
}

/// Result of registering oracle handlers with a reactive engine.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct OracleReactiveInstallReport {
    /// Handler ids submitted to the engine.
    pub handler_ids: Vec<HandlerId>,
}

impl OracleReactiveInstallReport {
    /// Number of handlers submitted.
    pub fn len(&self) -> usize {
        self.handler_ids.len()
    }

    /// Return true when no handlers were submitted.
    pub fn is_empty(&self) -> bool {
        self.handler_ids.is_empty()
    }
}

/// Result of unregistering oracle handlers from a reactive engine.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct OracleReactiveUninstallReport {
    /// Handler ids requested for removal.
    pub handler_ids: Vec<HandlerId>,
    /// Handler ids that were present and removed.
    pub removed_handler_ids: Vec<HandlerId>,
}

/// Result of refreshing oracle handlers after runtime mutation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct OracleReactiveRefreshReport {
    /// Handler ids requested for removal from the previous install handle.
    pub previous_handler_ids: Vec<HandlerId>,
    /// Previous handler ids that were present and removed.
    pub removed_handler_ids: Vec<HandlerId>,
    /// Current handler ids installed after the refresh.
    pub installed_handler_ids: Vec<HandlerId>,
}

/// Result of mutating a runtime's oracle registration set.
#[derive(Clone, Debug, Default)]
pub struct OracleRuntimeMutationReport {
    /// Feed ids registered by this mutation.
    pub registered_feed_ids: Vec<FeedId>,
    /// Feed ids removed by this mutation.
    pub removed_feed_ids: Vec<FeedId>,
    /// Feeds skipped by adapter discovery, when applicable.
    pub skipped: Vec<OracleAdapterFeedSkip>,
    /// Feed/runtime readiness after the mutation.
    pub feed_statuses: Vec<OracleFeedReadinessReport>,
    /// Current handler ids after this mutation.
    pub current_handler_ids: Vec<HandlerId>,
}

#[derive(Clone)]
struct OracleAdapterRuntimeState {
    adapter: Arc<dyn OracleAdapterPlugin>,
    registrations: Vec<FeedRegistration>,
}

impl fmt::Debug for OracleAdapterRuntimeState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("OracleAdapterRuntimeState")
            .field("adapter_id", &self.adapter.adapter_id())
            .field("registrations_len", &self.registrations.len())
            .finish()
    }
}

impl<P> OracleRuntimeBuilder<P> {
    /// Create a runtime builder around a provider.
    pub fn new(provider: P) -> Self {
        Self {
            provider,
            feeds: Vec::new(),
            now_timestamp: None,
            storage_sync: OracleStorageSync::chainlink_defaults(),
            callbacks: Vec::new(),
        }
    }

    /// Add one feed registration.
    pub fn feed(mut self, feed: ChainlinkFeed) -> Self {
        self.feeds.push(feed);
        self
    }

    /// Set a fixed timestamp for deterministic registration status classification.
    pub fn now_timestamp(mut self, now_timestamp: u64) -> Self {
        self.now_timestamp = Some(now_timestamp);
        self
    }

    /// Add one storage adapter.
    pub fn storage_adapter<A>(mut self, adapter: A) -> Self
    where
        A: OracleStorageAdapter + 'static,
    {
        self.storage_sync.push_adapter(Arc::new(adapter));
        self
    }

    /// Replace the storage-sync registry.
    pub fn storage_sync(mut self, storage_sync: OracleStorageSync) -> Self {
        self.storage_sync = storage_sync;
        self
    }

    /// Subscribe to every typed oracle event emitted by this runtime.
    pub fn on_event<F>(mut self, callback: F) -> Self
    where
        F: Fn(&OracleHookEvent) + Send + Sync + 'static,
    {
        self.callbacks.push(Arc::new(callback));
        self
    }

    /// Subscribe to immediate event-derived price updates.
    pub fn on_price_update<F>(self, callback: F) -> Self
    where
        F: Fn(&OraclePriceUpdate) + Send + Sync + 'static,
    {
        self.on_event(move |event| {
            if let OracleHookEvent::PriceUpdate(update) = event {
                callback(update);
            }
        })
    }

    /// Subscribe to proxy corrections.
    pub fn on_price_corrected<F>(self, callback: F) -> Self
    where
        F: Fn(&OraclePriceCorrected) + Send + Sync + 'static,
    {
        self.on_event(move |event| {
            if let OracleHookEvent::PriceCorrected(corrected) = event {
                callback(corrected);
            }
        })
    }
}

impl<P: ChainlinkFeedProvider> OracleRuntimeBuilder<P> {
    /// Register feeds and construct the runtime.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::Provider`] when a proxy read fails,
    /// [`OracleError::DuplicateFeedId`]/[`OracleError::DuplicateProxy`] when
    /// two feeds collide, and [`OracleError::Config`] when the system clock is
    /// before the UNIX epoch and no `now_timestamp` was set.
    pub async fn build(self) -> Result<OracleRuntime<P>, OracleError> {
        let now_timestamp = match self.now_timestamp {
            Some(now_timestamp) => now_timestamp,
            None => SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map_err(crate::error::clock_error)?
                .as_secs(),
        };
        let mut registry = OracleRegistry::new_at_timestamp(now_timestamp);
        for feed in self.feeds {
            registry
                .register_chainlink_feed(&self.provider, feed.into_config())
                .await?;
        }

        Ok(OracleRuntime {
            provider: self.provider,
            tracker: OracleTracker::new(registry),
            storage_sync: self.storage_sync,
            callbacks: self.callbacks,
            adapter_states: Vec::new(),
        })
    }
}

impl Default for OracleCacheRuntimeBuilder {
    fn default() -> Self {
        Self {
            adapters: Vec::new(),
            now_timestamp: None,
            storage_sync: OracleStorageSync::chainlink_defaults(),
            storage_warmup_enabled: true,
            storage_warmup_mode: OracleStorageWarmupMode::default(),
            code_registry: OracleCodeRegistry::default(),
            code_warmup_policy: OracleCodeWarmupPolicy::default(),
            callbacks: Vec::new(),
        }
    }
}

impl OracleCacheRuntimeBuilder {
    /// Install one oracle adapter plugin.
    pub fn install_adapter<A>(mut self, adapter: A) -> Self
    where
        A: OracleAdapterPlugin + 'static,
    {
        self.adapters.push(Arc::new(adapter));
        self
    }

    /// Set a fixed timestamp for deterministic registration status classification.
    pub fn now_timestamp(mut self, now_timestamp: u64) -> Self {
        self.now_timestamp = Some(now_timestamp);
        self
    }

    /// Add one storage adapter.
    pub fn storage_adapter<A>(mut self, adapter: A) -> Self
    where
        A: OracleStorageAdapter + 'static,
    {
        self.storage_sync.push_adapter(Arc::new(adapter));
        self
    }

    /// Replace the storage-sync registry.
    pub fn storage_sync(mut self, storage_sync: OracleStorageSync) -> Self {
        self.storage_sync = storage_sync;
        self
    }

    /// Enable or disable oracle storage warmup during cache-native builds.
    ///
    /// Enabled by default. When enabled, the builder asks installed storage
    /// adapters which slots must be hot for direct event writes and bulk-loads
    /// them through the cache before returning the runtime.
    pub fn storage_warmup(mut self, enabled: bool) -> Self {
        self.storage_warmup_enabled = enabled;
        self
    }

    /// Set the oracle storage warmup mode.
    pub fn storage_warmup_mode(mut self, mode: OracleStorageWarmupMode) -> Self {
        self.storage_warmup_mode = mode;
        self
    }

    /// Use `EvmCache::run_cold_start` for declared oracle storage slots.
    pub fn storage_cold_start(self) -> Self {
        self.storage_warmup_mode(OracleStorageWarmupMode::ColdStart)
    }

    /// Disable oracle storage warmup.
    pub fn disable_storage_warmup(self) -> Self {
        self.storage_warmup(false)
    }

    /// Replace the bytecode seed/etch registry.
    pub fn code_registry(mut self, registry: OracleCodeRegistry) -> Self {
        self.code_registry = registry;
        self
    }

    /// Set the bytecode warmup failure policy.
    pub fn code_warmup_policy(mut self, policy: OracleCodeWarmupPolicy) -> Self {
        self.code_warmup_policy = policy;
        self
    }

    /// Add a canonical bytecode seed that must verify against on-chain code hash.
    pub fn code_seed(mut self, address: Address, code: Bytes) -> Self {
        self.code_registry = self.code_registry.seed(address, code);
        self
    }

    /// Add multiple canonical bytecode seeds that must verify against chain code hash.
    pub fn code_seeds<I>(mut self, seeds: I) -> Self
    where
        I: IntoIterator<Item = (Address, Bytes)>,
    {
        self.code_registry = self.code_registry.seed_many(seeds);
        self
    }

    /// Add explicit simulation-only bytecode at `address`.
    pub fn code_etch(mut self, address: Address, code: Bytes) -> Self {
        self.code_registry = self.code_registry.etch(address, code);
        self
    }

    /// Add multiple explicit simulation-only bytecode etches.
    pub fn code_etches<I>(mut self, etches: I) -> Self
    where
        I: IntoIterator<Item = (Address, Bytes)>,
    {
        self.code_registry = self.code_registry.etch_many(etches);
        self
    }

    /// Subscribe to every typed oracle event emitted by this runtime.
    pub fn on_event<F>(mut self, callback: F) -> Self
    where
        F: Fn(&OracleHookEvent) + Send + Sync + 'static,
    {
        self.callbacks.push(Arc::new(callback));
        self
    }

    /// Subscribe to immediate event-derived price updates.
    pub fn on_price_update<F>(self, callback: F) -> Self
    where
        F: Fn(&OraclePriceUpdate) + Send + Sync + 'static,
    {
        self.on_event(move |event| {
            if let OracleHookEvent::PriceUpdate(update) = event {
                callback(update);
            }
        })
    }

    /// Register adapter-discovered feeds and construct the runtime.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::FeedSkipped`] when any adapter reported a
    /// skipped feed (use [`Self::build_report`] to receive skips as data),
    /// [`OracleError::Policy`] when code warmup fails the configured
    /// [`OracleCodeWarmupPolicy`], [`OracleError::Provider`] when adapter
    /// discovery reads fail, [`OracleError::DuplicateFeedId`]/
    /// [`OracleError::DuplicateProxy`] when discovered feeds collide, and
    /// [`OracleError::Config`] when the system clock is before the UNIX epoch
    /// and no `now_timestamp` was set.
    pub async fn build(self, cache: &mut EvmCache) -> Result<OracleRuntime<()>, OracleError> {
        let report = self.build_report(cache).await?;
        if let Some(skipped) = report.skipped.first() {
            return Err(cache_runtime_skip_error(skipped));
        }
        Ok(report.runtime)
    }

    /// Register every compatible adapter feed and return skipped feeds instead of failing fast.
    pub async fn build_report(
        self,
        cache: &mut EvmCache,
    ) -> Result<OracleCacheRuntimeBuildReport, OracleError> {
        let now_timestamp = match self.now_timestamp {
            Some(now_timestamp) => now_timestamp,
            None => SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map_err(crate::error::clock_error)?
                .as_secs(),
        };

        let code_warmup = self
            .code_registry
            .apply_to_cache_with_policy(cache, self.code_warmup_policy)?;
        let mut discovered_by_adapter = Vec::new();
        let mut seeded = Vec::new();
        let mut warmup_registrations = Vec::new();
        let mut skipped = Vec::new();
        for adapter in &self.adapters {
            let report = adapter
                .discover(OracleDiscoveryContext {
                    cache,
                    now_timestamp,
                })
                .await?;
            skipped.extend(report.skipped);
            let registrations = report
                .feeds
                .iter()
                .map(|feed| feed.registration.clone())
                .collect::<Vec<_>>();
            seeded.extend(report.feeds.into_iter().map(|feed| {
                warmup_registrations.push(feed.registration.clone());
                (feed.registration, feed.round)
            }));
            discovered_by_adapter.push(OracleAdapterRuntimeState {
                adapter: Arc::clone(adapter),
                registrations,
            });
        }

        let storage_warmup = if self.storage_warmup_enabled {
            prewarm_oracle_storage(
                cache,
                &self.storage_sync,
                &warmup_registrations.iter().collect::<Vec<_>>(),
                self.storage_warmup_mode,
            )
        } else {
            OracleStorageWarmupReport {
                feed_statuses: feed_readiness_from_registrations(&warmup_registrations),
                ..OracleStorageWarmupReport::default()
            }
        };
        let mut feed_statuses = storage_warmup.feed_statuses.clone();
        feed_statuses.extend(skipped.iter().map(skipped_feed_readiness));
        let tracker = OracleTracker::from_registrations_at_timestamp(seeded, now_timestamp)?;
        Ok(OracleCacheRuntimeBuildReport {
            runtime: OracleRuntime {
                provider: (),
                tracker,
                storage_sync: self.storage_sync,
                callbacks: self.callbacks,
                adapter_states: discovered_by_adapter,
            },
            skipped,
            feed_statuses,
            storage_warmup,
            code_warmup,
        })
    }
}

/// High-level facade for registration, typed events, reads, and reconciliation.
pub struct OracleRuntime<P> {
    provider: P,
    tracker: OracleTracker,
    storage_sync: OracleStorageSync,
    callbacks: Vec<OracleEventCallback>,
    adapter_states: Vec<OracleAdapterRuntimeState>,
}

impl<P: fmt::Debug> fmt::Debug for OracleRuntime<P> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("OracleRuntime")
            .field("provider", &self.provider)
            .field("tracker", &self.tracker)
            .field("storage_sync", &self.storage_sync)
            .field("callbacks_len", &self.callbacks.len())
            .field("adapter_states", &self.adapter_states)
            .finish()
    }
}

impl<P> OracleRuntime<P> {
    /// Start building a runtime around a provider.
    pub fn builder(provider: P) -> OracleRuntimeBuilder<P> {
        OracleRuntimeBuilder::new(provider)
    }

    /// Borrow the provider used for authoritative proxy reads and reconciliation.
    pub fn provider(&self) -> &P {
        &self.provider
    }

    /// Borrow the typed state store for snapshot, price, and registration reads.
    pub fn tracker(&self) -> &OracleTracker {
        &self.tracker
    }

    /// Mutably borrow the tracker.
    ///
    /// **Warning:** mutating the tracker directly (registering or removing
    /// feeds through [`OracleTracker`] methods) desynchronizes this runtime's
    /// adapter bookkeeping: `adapter_states` still references removed feeds
    /// (or misses added ones), so [`Self::reactive_handlers`] and the
    /// `refresh_handlers*` methods rebuild handler routing from a stale feed
    /// set. Use [`Self::register_seeded_feed`], [`Self::register_adapter`],
    /// [`Self::unregister_feed_by_id`], or [`Self::unregister_feed_by_proxy`]
    /// instead — they keep typed state and handler routing in sync. Reserve
    /// this accessor for operations that do not change the registration set
    /// (for example [`OracleTracker::reconcile`] or
    /// [`OracleTracker::apply_batch_report`]).
    pub fn tracker_mut(&mut self) -> &mut OracleTracker {
        &mut self.tracker
    }

    /// Return feed/runtime readiness for all registered feeds.
    pub fn feed_readiness(&self) -> Vec<OracleFeedReadinessReport> {
        self.tracker.feed_readiness()
    }

    pub(crate) fn storage_sync(&self) -> &OracleStorageSync {
        &self.storage_sync
    }

    /// Replace the direct-storage registry used by subsequently constructed
    /// reactive handlers and storage warmup calls.
    ///
    /// This is primarily useful after cache-native discovery returns an
    /// [`OracleTracker`] that needs a deployment-specific storage adapter:
    /// construct the facade with [`OracleRuntime::from_tracker`], then retain
    /// the custom registry here before calling [`Self::reactive_runtime`],
    /// [`Self::reactive_handler`], or [`Self::prewarm_storage`].
    ///
    /// If this runtime's handlers are already installed in a live reactive
    /// engine, replace the registry only as part of a handler refresh so the
    /// installed handlers cannot retain the old storage policy.
    pub fn with_storage_sync(mut self, storage_sync: OracleStorageSync) -> Self {
        self.storage_sync = storage_sync;
        self
    }

    /// Build a fresh built-in Chainlink-compatible reactive handler from current registrations.
    ///
    /// Adapter plugin handlers are not included. Use [`Self::reactive_handlers`],
    /// [`Self::reactive_runtime`], or [`Self::register_subscriber`] when the runtime
    /// was built with installed adapter plugins.
    pub fn reactive_handler(&self) -> OracleReactiveHandler {
        OracleReactiveHandler::with_storage_sync(
            self.tracker.registrations().collect(),
            self.storage_sync.clone(),
        )
    }

    /// Build fresh reactive handlers for every source installed in this runtime.
    ///
    /// The first handler is the built-in Chainlink-compatible handler. Any adapter
    /// plugin handlers discovered during cache-native startup follow it.
    ///
    /// Handler ids must be unique across installed adapter plugins: when two
    /// adapters return handlers with the same [`HandlerId`], only the first
    /// handler is kept and the second adapter's event routing is silently
    /// dropped (a `tracing` warning is emitted). An adapter handler that
    /// reuses the built-in Chainlink handler id is fine — the built-in
    /// handler is rebuilt from *all* current registrations, so it already
    /// routes that adapter's feeds.
    pub fn reactive_handlers(&self) -> Vec<Arc<dyn ReactiveHandler<Ethereum>>> {
        let built_in: Arc<dyn ReactiveHandler<Ethereum>> = Arc::new(self.reactive_handler());
        let built_in_id = built_in.id();
        let mut adapter_ids = BTreeSet::new();
        let mut handlers = vec![built_in];
        for state in self
            .adapter_states
            .iter()
            .filter(|state| !state.registrations.is_empty())
        {
            let handler = state
                .adapter
                .reactive_handler(state.registrations.clone(), self.storage_sync.clone());
            let id = handler.id();
            if id == built_in_id {
                // By design: the adapter delegates to the built-in Chainlink
                // handler, which is already built from all registrations.
                continue;
            }
            if !adapter_ids.insert(id.clone()) {
                tracing::warn!(
                    handler_id = %id,
                    adapter_id = %state.adapter.adapter_id(),
                    "duplicate reactive HandlerId from adapter plugins; \
                     dropping this adapter's handler routing"
                );
                continue;
            }
            handlers.push(handler);
        }
        handlers
    }

    /// Return current reactive handler ids for this runtime.
    pub fn reactive_handler_ids(&self) -> Vec<HandlerId> {
        self.reactive_handlers()
            .into_iter()
            .map(|handler| handler.id())
            .collect()
    }

    /// Build a reactive runtime with this oracle runtime's handlers already installed.
    pub fn reactive_runtime(&self) -> Result<ReactiveRuntime<Ethereum>, OracleError> {
        let mut runtime = ReactiveRuntime::<Ethereum>::new(ReactiveConfig::default());
        for handler in self.reactive_handlers() {
            runtime.register_handler(handler).map_err(runtime_error)?;
        }
        Ok(runtime)
    }

    /// Register this runtime's handlers with an engine, using continuity-safe backfill.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::Reactive`] when the engine rejects a handler
    /// registration (for example a handler id that is already installed).
    pub fn install_handlers<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
    ) -> Result<OracleReactiveInstallReport, OracleError>
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        let mut report = OracleReactiveInstallReport::default();
        for handler in self.reactive_handlers() {
            let id = handler.id();
            engine.register_handler(handler).map_err(runtime_error)?;
            report.handler_ids.push(id);
        }
        Ok(report)
    }

    /// Refresh an existing engine install after registering or unregistering feeds.
    ///
    /// `installed` must be the report returned by a previous install/refresh.
    /// The old handler ids in that report are removed first, then the runtime's
    /// current handlers are installed with continuity-safe backfill.
    pub fn refresh_handlers<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
        installed: &mut OracleReactiveInstallReport,
    ) -> Result<OracleReactiveRefreshReport, OracleError>
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        self.refresh_handlers_inner(engine, installed, OracleHandlerRefreshBackfill::Continuity)
    }

    /// Refresh an existing engine install after mutation using explicit backfill.
    pub fn refresh_handlers_with_backfill<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
        installed: &mut OracleReactiveInstallReport,
        backfill: SubscriberBackfill,
    ) -> Result<OracleReactiveRefreshReport, OracleError>
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        self.refresh_handlers_inner(
            engine,
            installed,
            OracleHandlerRefreshBackfill::Explicit(backfill),
        )
    }

    /// Refresh an existing engine install after mutation without backfill.
    pub fn refresh_handlers_live_only<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
        installed: &mut OracleReactiveInstallReport,
    ) -> Result<OracleReactiveRefreshReport, OracleError>
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        self.refresh_handlers_inner(engine, installed, OracleHandlerRefreshBackfill::LiveOnly)
    }

    /// Unregister exactly the handlers recorded by a previous install/refresh report.
    pub fn uninstall_installed_handlers<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
        installed: &mut OracleReactiveInstallReport,
    ) -> OracleReactiveUninstallReport
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        let handler_ids = std::mem::take(&mut installed.handler_ids);
        let removed_handler_ids = unregister_handler_ids(engine, &handler_ids);
        OracleReactiveUninstallReport {
            handler_ids,
            removed_handler_ids,
        }
    }

    /// Register this runtime's handlers with explicit owner-scoped backfill.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::Reactive`] when the engine rejects a handler
    /// registration (for example a handler id that is already installed).
    pub fn install_handlers_with_backfill<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
        backfill: SubscriberBackfill,
    ) -> Result<OracleReactiveInstallReport, OracleError>
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        let mut report = OracleReactiveInstallReport::default();
        for handler in self.reactive_handlers() {
            let id = handler.id();
            engine
                .register_handler_with_backfill(handler, backfill)
                .map_err(runtime_error)?;
            report.handler_ids.push(id);
        }
        Ok(report)
    }

    /// Register this runtime's handlers without any backfill.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::Reactive`] when the engine rejects a handler
    /// registration (for example a handler id that is already installed).
    pub fn install_handlers_live_only<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
    ) -> Result<OracleReactiveInstallReport, OracleError>
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        let mut report = OracleReactiveInstallReport::default();
        for handler in self.reactive_handlers() {
            let id = handler.id();
            engine
                .register_handler_live_only(handler)
                .map_err(runtime_error)?;
            report.handler_ids.push(id);
        }
        Ok(report)
    }

    /// Unregister this runtime's handlers from an engine.
    ///
    /// **Warning:** this recomputes handler ids from the *current*
    /// registration set. If the runtime's registrations changed since the
    /// handlers were installed (feeds registered or unregistered, adapters
    /// added), the recomputed set can miss handlers that are still installed
    /// in the engine, leaving them registered. Prefer
    /// [`Self::uninstall_installed_handlers`] with the report returned by the
    /// original install/refresh call — it removes exactly the handler ids
    /// that were installed.
    pub fn uninstall_handlers<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
    ) -> OracleReactiveUninstallReport
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        let mut report = OracleReactiveUninstallReport::default();
        for handler in self.reactive_handlers() {
            let id = handler.id();
            report.handler_ids.push(id.clone());
            if engine.unregister_handler(&id).is_some() {
                report.removed_handler_ids.push(id);
            }
        }
        report
    }

    /// Register this runtime's oracle interests with an event subscriber.
    pub fn register_subscriber<S>(&self, subscriber: &mut S) -> Result<(), OracleError>
    where
        S: EventSubscriber<Ethereum>,
    {
        subscriber
            .register_interests(&self.reactive_interests())
            .map_err(runtime_error)
    }

    /// Read one subscriber batch, apply it to the cache, and return the typed
    /// batch digest, or `None` when the subscriber has no batch ready.
    pub async fn next_subscriber_events<S>(
        &mut self,
        cache: &mut EvmCache,
        runtime: &mut ReactiveRuntime<Ethereum>,
        subscriber: &mut S,
    ) -> Result<Option<crate::OracleBatchReport>, OracleError>
    where
        S: EventSubscriber<Ethereum>,
    {
        let Some(batch) = subscriber.next_batch().await.map_err(runtime_error)? else {
            return Ok(None);
        };
        let report = runtime.ingest_batch(cache, batch).map_err(runtime_error)?;
        self.apply_batch_report(&report).map(Some)
    }

    /// Build a read overlay over the current tracker state.
    pub fn read_overlay(&self) -> OracleReadOverlay<'_> {
        OracleReadOverlay::new(&self.tracker)
    }

    /// Register an already-discovered feed and seed its latest round.
    ///
    /// This updates typed state immediately. If a reactive engine has already
    /// installed this runtime's handlers, call one of the `refresh_handlers*`
    /// methods with the previous install report so the subscriber receives the
    /// new event interests.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::DuplicateFeedId`] or
    /// [`OracleError::DuplicateProxy`] when the registration collides with an
    /// existing feed; typed state is unchanged in that case.
    pub fn register_seeded_feed(
        &mut self,
        registration: FeedRegistration,
        round: RoundData,
    ) -> Result<OracleRuntimeMutationReport, OracleError> {
        let id = registration.id.clone();
        self.tracker
            .insert_seeded_registration(registration, round)?;
        Ok(self.mutation_report([id], []))
    }

    /// Register an already-discovered adapter-owned feed and seed its latest round.
    ///
    /// The adapter must already be installed in this runtime. This is the
    /// low-level path for adapter code that discovers one additional feed and
    /// wants that feed included in the adapter handler rebuilt on refresh.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::Config`] when `adapter_id` is not installed in
    /// this runtime, and [`OracleError::DuplicateFeedId`]/
    /// [`OracleError::DuplicateProxy`] when the registration collides with an
    /// existing feed.
    pub fn register_adapter_seeded_feed(
        &mut self,
        adapter_id: impl AsRef<str>,
        registration: FeedRegistration,
        round: RoundData,
    ) -> Result<OracleRuntimeMutationReport, OracleError> {
        let id = registration.id.clone();
        let state_index = self
            .adapter_state_index(adapter_id.as_ref())
            .ok_or_else(|| adapter_not_installed(adapter_id.as_ref()))?;
        self.tracker
            .insert_seeded_registration(registration.clone(), round)?;
        self.adapter_states[state_index]
            .registrations
            .push(registration);
        Ok(self.mutation_report([id], []))
    }

    /// Unregister a feed by id.
    ///
    /// This removes typed state, pending reconciliation, and any adapter routing
    /// references for the feed. Refresh installed handlers afterward to apply
    /// the routing change to a live engine.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::FeedNotFound`] when no feed is registered under
    /// `id`.
    pub fn unregister_feed_by_id(
        &mut self,
        id: FeedId,
    ) -> Result<OracleRuntimeMutationReport, OracleError> {
        let removed = self
            .tracker
            .remove_by_id(id)
            .ok_or(OracleError::FeedNotFound)?;
        let id = removed.id.clone();
        self.remove_adapter_registration(id.clone());
        Ok(self.mutation_report([], [id]))
    }

    /// Unregister a feed by proxy.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::FeedNotFound`] when no feed is registered under
    /// `proxy`.
    pub fn unregister_feed_by_proxy(
        &mut self,
        proxy: Address,
    ) -> Result<OracleRuntimeMutationReport, OracleError> {
        let removed = self
            .tracker
            .remove_by_proxy(proxy)
            .ok_or(OracleError::FeedNotFound)?;
        let id = removed.id.clone();
        self.remove_adapter_registration(id.clone());
        Ok(self.mutation_report([], [id]))
    }

    /// Return the latest typed price by feed id string.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::FeedNotFound`] when no feed is registered under
    /// `id` or the feed has no current snapshot.
    pub fn price(&self, id: impl AsRef<str>) -> Result<OraclePrice, OracleError> {
        self.tracker.price(id)
    }

    /// Return the latest typed price by proxy address.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::FeedNotFound`] when no feed is registered under
    /// `proxy` or the feed has no current snapshot.
    pub fn price_by_proxy(&self, proxy: Address) -> Result<OraclePrice, OracleError> {
        self.tracker.price_by_proxy(proxy)
    }

    /// Return the latest round by feed id string.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::FeedNotFound`] when no feed is registered under
    /// `id` or the feed has no current snapshot.
    pub fn latest_round(&self, id: impl AsRef<str>) -> Result<RoundData, OracleError> {
        self.tracker.latest_round(id)
    }

    /// Apply a committed reactive batch and return the typed batch digest.
    ///
    /// The returned [`crate::OracleBatchReport`] carries the raw per-event
    /// hooks (`events`, in emission order) plus the digested per-feed changes,
    /// continuity incidents, and the `requires_full_refresh` routing flag —
    /// the same consumer shape as `evm-amm-state`'s `AmmSyncBatchReport`.
    /// Registered callbacks fire for every event before this returns.
    ///
    /// Legacy `oracle.answer_updated` signals (emitted by custom handlers that
    /// predate the rich [`OraclePriceUpdate`] payload) update typed tracker
    /// state but are **not** decoded into [`OracleHookEvent`]s, so `on_event`/
    /// `on_price_update` callbacks do not fire for them. Emit the rich
    /// `oracle.price_update` signal from custom handlers to get typed
    /// callbacks.
    pub fn apply_batch_report<N: Network>(
        &mut self,
        report: &ReactiveBatchReport<N>,
    ) -> Result<crate::OracleBatchReport, OracleError> {
        let mut events = Vec::new();
        for signal in report
            .applied
            .iter()
            .flat_map(|applied| applied.hook_signals.iter())
        {
            if let Some(signal) = OracleSignal::from_hook(signal)? {
                events.push(signal.to_event());
            }
        }

        self.tracker.apply_batch_report(report)?;
        self.emit(&events);
        Ok(crate::OracleBatchReport::from_events(events))
    }

    /// Reconcile all currently pending proxy-kind event updates.
    ///
    /// Derived-source requests
    /// ([`crate::OracleReconciliationKind::DerivedProtocolRead`]) are left
    /// queued; satisfy those with [`OracleRuntime::reconcile_derived`].
    pub async fn reconcile_pending(&mut self) -> Result<Vec<OracleHookEvent>, OracleError>
    where
        P: ChainlinkFeedProvider,
    {
        let mut reconciler = OracleReconciler::default();
        for request in self
            .tracker
            .pending_reconciliations()
            .iter()
            .filter(|request| request.kind == crate::OracleReconciliationKind::Proxy)
            .cloned()
        {
            reconciler.enqueue(request);
        }

        let mut events = Vec::new();
        while let Some(result) = reconciler
            .reconcile_next(&mut self.tracker, &self.provider)
            .await?
        {
            events.extend(result.hooks);
        }
        self.emit(&events);
        Ok(events)
    }

    /// Reconcile all currently pending derived-source updates through their
    /// protocols' own view calls (Morpho `price()`, Euler `getQuote`), read
    /// through `cache`.
    ///
    /// Successful reads promote `EventPending` snapshots to `Confirmed` or
    /// `Corrected` and fire the registered hook callbacks; failed reads leave
    /// their requests queued and are surfaced in
    /// [`crate::DerivedReconcileReport::failed`].
    pub fn reconcile_derived(
        &mut self,
        cache: &mut evm_fork_cache::cache::EvmCache,
    ) -> crate::DerivedReconcileReport {
        let report = self.tracker.reconcile_derived_pending_with(cache);
        let events: Vec<OracleHookEvent> = report
            .reconciled
            .iter()
            .flat_map(|result| result.hooks.iter().cloned())
            .collect();
        self.emit(&events);
        report
    }

    fn emit(&self, events: &[OracleHookEvent]) {
        for event in events {
            for callback in &self.callbacks {
                callback(event);
            }
        }
    }

    /// Return the complete log/event interest set for built-in and adapter handlers.
    pub fn reactive_interests(&self) -> Vec<ReactiveInterest<Ethereum>> {
        let mut interests = Vec::new();
        for handler in self.reactive_handlers() {
            interests.extend(handler.interests());
        }
        interests
    }

    /// Return the oracle storage slots this runtime can prewarm for direct event writes.
    pub fn storage_warmup_slots(&self) -> Vec<(Address, U256)> {
        self.storage_sync
            .warm_slots_for_registrations(self.tracker.registrations_iter())
    }

    /// Bulk-load direct-write oracle storage slots into the supplied cache.
    pub fn prewarm_storage(&self, cache: &mut EvmCache) -> OracleStorageWarmupReport {
        let registrations = self.tracker.registrations_iter().collect::<Vec<_>>();
        prewarm_oracle_storage(
            cache,
            &self.storage_sync,
            &registrations,
            OracleStorageWarmupMode::default(),
        )
    }

    /// Verify direct-write oracle storage slots through `EvmCache::run_cold_start`.
    pub fn cold_start_storage(&self, cache: &mut EvmCache) -> OracleStorageWarmupReport {
        let registrations = self.tracker.registrations_iter().collect::<Vec<_>>();
        prewarm_oracle_storage(
            cache,
            &self.storage_sync,
            &registrations,
            OracleStorageWarmupMode::ColdStart,
        )
    }

    fn refresh_handlers_inner<S>(
        &self,
        engine: &mut ReactiveEngine<S, Ethereum>,
        installed: &mut OracleReactiveInstallReport,
        backfill: OracleHandlerRefreshBackfill,
    ) -> Result<OracleReactiveRefreshReport, OracleError>
    where
        S: InterestOwnerSubscriber<Ethereum>,
    {
        let previous_handler_ids = std::mem::take(&mut installed.handler_ids);
        let removed_handler_ids = unregister_handler_ids(engine, &previous_handler_ids);
        let new_install = match backfill {
            OracleHandlerRefreshBackfill::Continuity => self.install_handlers(engine)?,
            OracleHandlerRefreshBackfill::Explicit(backfill) => {
                self.install_handlers_with_backfill(engine, backfill)?
            }
            OracleHandlerRefreshBackfill::LiveOnly => self.install_handlers_live_only(engine)?,
        };
        installed.handler_ids = new_install.handler_ids.clone();
        Ok(OracleReactiveRefreshReport {
            previous_handler_ids,
            removed_handler_ids,
            installed_handler_ids: installed.handler_ids.clone(),
        })
    }

    fn mutation_report(
        &self,
        registered_feed_ids: impl IntoIterator<Item = FeedId>,
        removed_feed_ids: impl IntoIterator<Item = FeedId>,
    ) -> OracleRuntimeMutationReport {
        OracleRuntimeMutationReport {
            registered_feed_ids: registered_feed_ids.into_iter().collect(),
            removed_feed_ids: removed_feed_ids.into_iter().collect(),
            skipped: Vec::new(),
            feed_statuses: feed_readiness_from_registrations(self.tracker.registrations_iter()),
            current_handler_ids: self.reactive_handler_ids(),
        }
    }

    fn adapter_state_index(&self, adapter_id: &str) -> Option<usize> {
        self.adapter_states
            .iter()
            .position(|state| state.adapter.adapter_id().as_str() == adapter_id)
    }

    fn push_adapter_state(
        &mut self,
        adapter: Arc<dyn OracleAdapterPlugin>,
        registrations: Vec<FeedRegistration>,
    ) {
        if registrations.is_empty() {
            return;
        }

        let adapter_id = adapter.adapter_id();
        if let Some(existing) = self
            .adapter_states
            .iter_mut()
            .find(|state| state.adapter.adapter_id().as_str() == adapter_id.as_str())
        {
            existing.registrations.extend(registrations);
            return;
        }

        self.adapter_states.push(OracleAdapterRuntimeState {
            adapter,
            registrations,
        });
    }

    fn remove_adapter_registration(&mut self, id: FeedId) {
        for state in &mut self.adapter_states {
            state
                .registrations
                .retain(|registration| registration.id != id);
        }
    }
}

impl OracleRuntime<()> {
    /// Start building a cache-native runtime from oracle adapter plugins.
    pub fn cache_builder() -> OracleCacheRuntimeBuilder {
        OracleCacheRuntimeBuilder::default()
    }

    /// Build a runtime facade from a cache-native tracker.
    ///
    /// The returned runtime starts with **Chainlink-default storage sync and
    /// no adapter-plugin routing**, regardless of how the tracker's feeds
    /// were discovered: any custom [`OracleStorageSync`] configuration is
    /// reset to [`OracleStorageSync::chainlink_defaults`] unless replaced with
    /// [`OracleRuntime::with_storage_sync`], and non-Chainlink
    /// feeds already in the tracker (Pyth, RedStone, custom adapter families)
    /// remain visible in typed state but receive **no reactive events** —
    /// their adapter handlers are gone until the adapters are re-installed
    /// via [`Self::register_adapter`]. Prefer keeping the runtime returned by
    /// [`OracleCacheRuntimeBuilder::build`] when adapter plugins are in play.
    pub fn from_tracker(tracker: OracleTracker) -> Self {
        Self {
            provider: (),
            tracker,
            storage_sync: OracleStorageSync::chainlink_defaults(),
            callbacks: Vec::new(),
            adapter_states: Vec::new(),
        }
    }

    /// Discover and register feeds from an adapter on an existing cache-native runtime.
    ///
    /// This mutates typed state and retains the adapter for future handler
    /// rebuilds. If handlers are already installed in an engine, call one of the
    /// `refresh_handlers*` methods with the previous install report afterward.
    ///
    /// # Errors
    ///
    /// Returns [`OracleError::Provider`] when adapter discovery reads fail,
    /// and [`OracleError::DuplicateFeedId`]/[`OracleError::DuplicateProxy`]
    /// when a discovered feed collides with an existing registration. On
    /// error the runtime is unchanged: feeds inserted earlier in the same
    /// call are rolled back and the adapter is not retained.
    pub async fn register_adapter<A>(
        &mut self,
        adapter: A,
        cache: &mut EvmCache,
    ) -> Result<OracleRuntimeMutationReport, OracleError>
    where
        A: OracleAdapterPlugin + 'static,
    {
        let adapter = Arc::new(adapter);
        let discovery = adapter
            .discover(OracleDiscoveryContext {
                cache,
                now_timestamp: self.tracker.now_timestamp(),
            })
            .await?;

        // Insert discovered feeds live and roll back on failure instead of
        // cloning the whole tracker for every call. This is an exact inverse:
        // discovery ran before any mutation, `insert_seeded_registration`
        // touches only the registration map, the proxy-id index, and the
        // snapshot map, and `remove_by_id` removes exactly those entries plus
        // pending reconciliations — which a just-inserted proxy cannot have,
        // because nothing runs between the inserts and the rollback.
        let mut registered_feed_ids: Vec<FeedId> = Vec::new();
        let mut registrations = Vec::new();
        for feed in &discovery.feeds {
            if let Err(error) = self
                .tracker
                .insert_seeded_registration(feed.registration.clone(), feed.round.clone())
            {
                for id in registered_feed_ids {
                    self.tracker.remove_by_id(id);
                }
                return Err(error);
            }
            registered_feed_ids.push(feed.registration.id.clone());
            registrations.push(feed.registration.clone());
        }

        self.push_adapter_state(adapter, registrations);
        let mut feed_statuses =
            feed_readiness_from_registrations(self.tracker.registrations_iter());
        feed_statuses.extend(discovery.skipped.iter().map(skipped_feed_readiness));
        Ok(OracleRuntimeMutationReport {
            registered_feed_ids,
            removed_feed_ids: Vec::new(),
            skipped: discovery.skipped,
            feed_statuses,
            current_handler_ids: self.reactive_handler_ids(),
        })
    }
}

#[derive(Clone, Copy, Debug)]
enum OracleHandlerRefreshBackfill {
    Continuity,
    Explicit(SubscriberBackfill),
    LiveOnly,
}

fn runtime_error(error: impl ToString) -> OracleError {
    OracleError::Reactive(error.to_string())
}

fn adapter_not_installed(adapter_id: &str) -> OracleError {
    OracleError::Config(crate::error::OracleConfigError::AdapterNotInstalled {
        adapter: crate::OracleAdapterId::new(adapter_id.to_string()),
    })
}

fn feed_readiness_from_registrations<'a>(
    registrations: impl IntoIterator<Item = &'a FeedRegistration>,
) -> Vec<OracleFeedReadinessReport> {
    registrations
        .into_iter()
        .map(|registration| OracleFeedReadinessReport {
            id: Some(registration.id.clone()),
            proxy: registration.proxy,
            status: registration.status,
            reason: None,
        })
        .collect()
}

fn skipped_feed_readiness(skipped: &OracleAdapterFeedSkip) -> OracleFeedReadinessReport {
    OracleFeedReadinessReport {
        id: skipped.feed.id(),
        proxy: skipped.proxy,
        status: OracleFeedStatus::Unsupported,
        reason: Some(skipped.reason.to_string()),
    }
}

fn storage_warmup_feed_readiness(
    registrations: &[&FeedRegistration],
    failed_slots: &[OracleStorageWarmupFailure],
) -> Vec<OracleFeedReadinessReport> {
    if failed_slots.is_empty() {
        return feed_readiness_from_registrations(registrations.iter().copied());
    }

    let failed_addresses = failed_slots
        .iter()
        .map(|failure| failure.address)
        .collect::<BTreeSet<_>>();
    registrations
        .iter()
        .map(|registration| {
            let failed = feed_storage_addresses(registration)
                .into_iter()
                .any(|address| failed_addresses.contains(&address));
            OracleFeedReadinessReport {
                id: Some(registration.id.clone()),
                proxy: registration.proxy,
                status: if failed {
                    OracleFeedStatus::Degraded
                } else {
                    registration.status
                },
                reason: failed
                    .then(|| "oracle storage warmup failed for one or more feed slots".to_string()),
            }
        })
        .collect()
}

fn feed_storage_addresses(registration: &FeedRegistration) -> Vec<Address> {
    let mut addresses = vec![registration.proxy];
    addresses.extend(registration.current_aggregator);
    addresses.extend(
        registration
            .source
            .event_aggregators(registration.current_aggregator),
    );
    addresses.sort_unstable();
    addresses.dedup();
    addresses
}

fn unregister_handler_ids<S>(
    engine: &mut ReactiveEngine<S, Ethereum>,
    handler_ids: &[HandlerId],
) -> Vec<HandlerId>
where
    S: InterestOwnerSubscriber<Ethereum>,
{
    let mut removed = Vec::new();
    for id in handler_ids {
        if engine.unregister_handler(id).is_some() {
            removed.push(id.clone());
        }
    }
    removed
}

fn cache_runtime_skip_error(skipped: &OracleAdapterFeedSkip) -> OracleError {
    OracleError::FeedSkipped(Box::new(crate::OracleFeedSkip::from_adapter_skip(skipped)))
}

fn prewarm_oracle_storage(
    cache: &mut EvmCache,
    storage_sync: &OracleStorageSync,
    registrations: &[&crate::FeedRegistration],
    mode: OracleStorageWarmupMode,
) -> OracleStorageWarmupReport {
    let slots = storage_sync.warm_slots_for_registrations(registrations.iter().copied());
    let discovery_calls = oracle_cold_start_discovery_calls(registrations);
    let requested_slots = slots.len();
    if slots.is_empty() && discovery_calls.is_empty() {
        return OracleStorageWarmupReport {
            mode,
            feed_statuses: feed_readiness_from_registrations(registrations.iter().copied()),
            ..OracleStorageWarmupReport::default()
        };
    }

    match mode {
        OracleStorageWarmupMode::PrewarmSlots => {
            let report = cache.prewarm_slots(&slots);
            let failed_slots = report
                .failed
                .into_iter()
                .map(|(address, slot, error)| OracleStorageWarmupFailure {
                    address,
                    slot,
                    reason: error.to_string(),
                })
                .collect::<Vec<_>>();
            let feed_statuses = storage_warmup_feed_readiness(registrations, &failed_slots);
            OracleStorageWarmupReport {
                mode,
                feed_statuses,
                requested_slots,
                loaded_slots: report.loaded,
                failed_slots,
                discovery_calls: 0,
                cold_start: None,
            }
        }
        OracleStorageWarmupMode::ColdStart => {
            let mut planner =
                OracleSlotColdStartPlanner::new(slots.clone(), discovery_calls.clone());
            match cache.run_cold_start(&mut planner, ColdStartConfig::default()) {
                Ok(report) => {
                    let failed = report.failed_slots;
                    let cold_start = OracleColdStartWarmupReport::from(report);
                    OracleStorageWarmupReport {
                        mode,
                        feed_statuses: feed_readiness_from_registrations(
                            registrations.iter().copied(),
                        ),
                        requested_slots,
                        loaded_slots: requested_slots.saturating_sub(failed),
                        failed_slots: Vec::new(),
                        discovery_calls: cold_start.discover_calls,
                        cold_start: Some(cold_start),
                    }
                }
                Err(error) => {
                    let failed_slots = slots
                        .into_iter()
                        .map(|(address, slot)| OracleStorageWarmupFailure {
                            address,
                            slot,
                            reason: error.to_string(),
                        })
                        .collect::<Vec<_>>();
                    let feed_statuses = storage_warmup_feed_readiness(registrations, &failed_slots);
                    OracleStorageWarmupReport {
                        mode,
                        feed_statuses,
                        requested_slots,
                        loaded_slots: 0,
                        failed_slots,
                        discovery_calls: discovery_calls.len(),
                        cold_start: None,
                    }
                }
            }
        }
    }
}

struct OracleSlotColdStartPlanner {
    slots: Vec<(Address, U256)>,
    slot_set: BTreeSet<(Address, U256)>,
    discover: Vec<ColdStartCall>,
    verified_discovered_slots: bool,
}

impl OracleSlotColdStartPlanner {
    fn new(slots: Vec<(Address, U256)>, discover: Vec<ColdStartCall>) -> Self {
        let slot_set = slots.iter().copied().collect();
        Self {
            slots,
            slot_set,
            discover,
            verified_discovered_slots: false,
        }
    }
}

impl ColdStartPlanner for OracleSlotColdStartPlanner {
    fn initial_plan(&mut self, _state: &dyn StateView) -> ColdStartPlan {
        ColdStartPlan {
            verify: self.slots.clone(),
            discover: self.discover.clone(),
            ..Default::default()
        }
    }

    fn on_results(&mut self, results: &ColdStartResults, _state: &dyn StateView) -> ColdStartStep {
        if self.verified_discovered_slots {
            return ColdStartStep::Done;
        }
        self.verified_discovered_slots = true;

        let mut slots = results
            .discovered
            .iter()
            .flat_map(|discovered| discovered.access.slots.iter().copied())
            .filter(|slot| !self.slot_set.contains(slot))
            .collect::<Vec<_>>();
        slots.sort_unstable();
        slots.dedup();
        if slots.is_empty() {
            return ColdStartStep::Done;
        }

        ColdStartStep::Continue(ColdStartPlan {
            verify: slots,
            ..Default::default()
        })
    }
}

impl From<evm_fork_cache::ColdStartRunReport> for OracleColdStartWarmupReport {
    fn from(report: evm_fork_cache::ColdStartRunReport) -> Self {
        Self {
            rounds: report.rounds,
            verified_slots: report.verified_slots,
            changed_slots: report.changed_slots,
            failed_slots: report.failed_slots,
            discovered_slots: report.discovered_slots,
            discovered_accounts: report.discovered_accounts,
            discover_calls: report
                .per_round
                .iter()
                .map(|round| round.discover_calls)
                .sum(),
        }
    }
}

fn oracle_cold_start_discovery_calls(
    registrations: &[&crate::FeedRegistration],
) -> Vec<ColdStartCall> {
    registrations
        .iter()
        .map(|registration| {
            let read_proxy = registration.source.read_proxy(registration.proxy);
            ColdStartCall {
                from: Address::ZERO,
                to: read_proxy,
                calldata: Bytes::from(latestRoundDataCall {}.abi_encode()),
                restrict_to: Some(vec![read_proxy]),
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use evm_fork_cache::{ColdStartCallResult, StorageAccessList};
    use revm::context::result::{ExecutionResult, Output, SuccessReason};

    #[derive(Default)]
    struct EmptyStateView;

    impl StateView for EmptyStateView {
        fn storage(&self, _address: Address, _slot: U256) -> Option<U256> {
            None
        }
    }

    #[test]
    fn cold_start_planner_verifies_discovered_slots_in_second_round() {
        let declared = (Address::repeat_byte(0x11), U256::from(1_u64));
        let discovered = (Address::repeat_byte(0x22), U256::from(2_u64));
        let mut planner = OracleSlotColdStartPlanner::new(vec![declared], Vec::new());
        let initial = planner.initial_plan(&EmptyStateView);
        assert_eq!(initial.verify, vec![declared]);

        let mut access = StorageAccessList::default();
        access.slots.insert(discovered);
        access.slots.insert(declared);
        let results = ColdStartResults {
            discovered: vec![ColdStartCallResult {
                result: ExecutionResult::Success {
                    reason: SuccessReason::Return,
                    gas_used: 0,
                    gas_refunded: 0,
                    logs: Vec::new(),
                    output: Output::Call(Bytes::new()),
                },
                access,
            }],
            ..Default::default()
        };

        let ColdStartStep::Continue(next) = planner.on_results(&results, &EmptyStateView) else {
            panic!("discovered slots should schedule a follow-up verify round");
        };
        assert_eq!(next.verify, vec![discovered]);
        assert!(next.discover.is_empty());

        assert!(matches!(
            planner.on_results(&ColdStartResults::default(), &EmptyStateView),
            ColdStartStep::Done
        ));
    }
}