bsv-overlay-engine 0.1.6

Transaction admission, lookup and GASP synchronization for BSV overlay services.
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
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
//! Storage trait for the Overlay Services Engine.
//!
//! Defines the async storage interface that backends (D1, SQLite, in-memory) must implement.
//! The Engine never talks to a database directly — everything goes through this trait.
//!
//! Ported from `~/bsv/overlay-services/src/storage/Storage.ts`.
//! Reference implementation: `~/bsv/overlay-services/src/storage/knex/KnexStorage.ts`.

#[cfg(feature = "memory-storage")]
use crate::beef_limits;

use async_trait::async_trait;

use crate::types::{AppliedTransaction, Outpoint, Output};

/// A stored transaction's txid plus its serialized BEEF bytes.
///
/// Returned by [`Storage::find_transactions_for_proof_check`] so the engine can
/// parse the BEEF and decide whether the target tx still needs a merkle proof.
#[derive(Debug, Clone)]
pub struct TransactionBeef {
    /// The transaction id (hex, lowercase).
    pub txid: String,
    /// The serialized BEEF bytes for this transaction.
    pub beef: Vec<u8>,
}

/// Overlay Services storage backend.
///
/// All methods are async. Uses `?Send` futures for wasm32 compatibility
/// (Cloudflare Workers D1).
#[async_trait(?Send)]
pub trait Storage {
    // ========================================================================
    // Write operations
    // ========================================================================

    /// Insert a new output into storage.
    ///
    /// If an output with the same (txid, outputIndex, topic) already exists, this is a no-op.
    /// If `output.beef` is Some, also upsert the BEEF into the transactions table
    /// (deduplicated by txid).
    async fn insert_output(&self, output: &Output) -> Result<(), StorageError>;

    /// Delete an output from storage.
    ///
    /// After deletion, if no other outputs reference the same txid, also delete
    /// the transaction's BEEF from the transactions table.
    async fn delete_output(
        &self,
        txid: &str,
        output_index: u32,
        topic: &str,
    ) -> Result<(), StorageError>;

    /// Mark an output as spent.
    async fn mark_utxo_as_spent(
        &self,
        txid: &str,
        output_index: u32,
        topic: &str,
    ) -> Result<(), StorageError>;

    /// Update the `consumed_by` list on an existing output.
    async fn update_consumed_by(
        &self,
        txid: &str,
        output_index: u32,
        topic: &str,
        consumed_by: &[Outpoint],
    ) -> Result<(), StorageError>;

    /// Update the BEEF data for a transaction (used when merkle proofs arrive).
    async fn update_transaction_beef(&self, txid: &str, beef: &[u8]) -> Result<(), StorageError>;

    /// Mark a transaction as already carrying its own merkle proof, WITHOUT
    /// rewriting its BEEF.
    ///
    /// This is the lightweight flag-flip used by
    /// [`Engine::complete_missing_proofs`](crate::Engine::complete_missing_proofs)
    /// when a scanned candidate turns out to ALREADY be proven (its stored BEEF
    /// proves the target tx) yet its backend `has_proof` flag is still `0` —
    /// e.g. a GASP-synced tx that arrived with a proof, written before overlay
    /// migration 0010 added the flag (the migration defaulted every existing
    /// row to `0`). Such rows are returned by
    /// [`Storage::find_transactions_for_proof_check`] every tick and skipped by
    /// the engine, clogging the candidate window and starving genuinely
    /// proofless rows behind them. Flipping the flag here drops them out.
    ///
    /// The reference D1 backend runs
    /// `UPDATE transactions SET has_proof = 1 WHERE txid = ?`. Backends that do
    /// not track a proof flag may use this no-op default. Idempotent.
    async fn mark_transaction_proven(&self, txid: &str) -> Result<(), StorageError> {
        let _ = txid;
        Ok(())
    }

    /// [`Self::mark_transaction_proven`] that ALSO records the block height
    /// the just-verified bump anchors the tx to (`proofHeight`, bsv-low M19
    /// R2 round 3 review MED-2), so the revalidation sweep's transactions
    /// leg can window the row. `None` records no anchor. Default: the plain
    /// latch (a backend without the column loses only the windowing, never
    /// correctness).
    async fn mark_transaction_proven_at(
        &self,
        txid: &str,
        height: Option<u64>,
    ) -> Result<(), StorageError> {
        let _ = height;
        self.mark_transaction_proven(txid).await
    }

    /// Update the block height on an output (when it gets mined).
    ///
    /// Optional — backends that don't track block height can provide a no-op default.
    async fn update_output_block_height(
        &self,
        txid: &str,
        output_index: u32,
        topic: &str,
        block_height: u32,
    ) -> Result<(), StorageError> {
        let _ = (txid, output_index, topic, block_height);
        Ok(())
    }

    /// Record that a transaction has been applied to a topic (deduplication).
    async fn insert_applied_transaction(&self, tx: &AppliedTransaction)
        -> Result<(), StorageError>;

    /// Check if a transaction has already been applied to a topic.
    async fn does_applied_transaction_exist(
        &self,
        tx: &AppliedTransaction,
    ) -> Result<bool, StorageError>;

    /// bsv-low PLAN-PRE-LOOP4 §H4 (2026-09-06): forget a (txid, topic) applied
    /// record so a re-submit of those bytes is re-validated instead of
    /// deduplicated away. The caller proves the row is PHANTOM (no stored
    /// output of that txid on that topic) before asking — see
    /// `Engine::forget_phantom_applied`.
    async fn delete_applied_transaction(&self, tx: &AppliedTransaction)
        -> Result<(), StorageError>;

    // ========================================================================
    // Read operations
    // ========================================================================

    /// Find a single output by txid + outputIndex, with optional topic and spent filters.
    ///
    /// If `include_beef` is true, load the BEEF from the transactions table and
    /// attach it to the returned Output.
    async fn find_output(
        &self,
        txid: &str,
        output_index: u32,
        topic: Option<&str>,
        spent: Option<bool>,
        include_beef: bool,
    ) -> Result<Option<Output>, StorageError>;

    /// Batch-find outputs by outpoints. More efficient than individual find_output calls.
    ///
    /// Default implementation falls back to individual lookups.
    async fn find_outputs_by_outpoints(
        &self,
        outpoints: &[Outpoint],
        include_beef: bool,
    ) -> Result<Vec<Output>, StorageError> {
        let mut results = Vec::with_capacity(outpoints.len());
        for op in outpoints {
            if let Some(output) = self
                .find_output(&op.txid, op.output_index, None, None, include_beef)
                .await?
            {
                results.push(output);
            }
        }
        Ok(results)
    }

    /// Lookup-only read. Backends with observation stamps apply the shared
    /// proof window here, before returning BEEF. Internal graph reads remain available.
    async fn find_served_outputs(
        &self,
        outpoints: &[Outpoint],
    ) -> Result<Vec<Output>, StorageError> {
        self.find_outputs_by_outpoints(outpoints, true).await
    }

    /// Find all outputs for a given transaction.
    async fn find_outputs_for_transaction(
        &self,
        txid: &str,
        include_beef: bool,
    ) -> Result<Vec<Output>, StorageError>;

    /// Find unspent outputs for a topic, ordered by score ascending.
    ///
    /// - `since`: minimum score threshold (exclusive of scores below this)
    /// - `limit`: maximum number of results
    async fn find_utxos_for_topic(
        &self,
        topic: &str,
        since: Option<f64>,
        limit: Option<u64>,
        include_beef: bool,
    ) -> Result<Vec<Output>, StorageError>;

    /// Return a bounded page of *proofless* stored transactions (txid + BEEF
    /// bytes) for proof-completion scanning.
    ///
    /// Backends MUST return only transactions whose own merkle proof is still
    /// missing — i.e. the historical/recent backlog the
    /// [`Engine::complete_missing_proofs`](crate::Engine::complete_missing_proofs)
    /// cron is meant to clear. The reference D1 backend keeps a `has_proof`
    /// flag (overlay migration 0010), set on every BEEF write, and answers this
    /// with `WHERE has_proof = 0 LIMIT {limit}` — so every proofless tx is
    /// eventually reached (no "newest N rows only" starvation) and proven rows
    /// are never re-fetched. The engine still defensively re-parses each
    /// returned BEEF and skips any that turn out already-proven, so an
    /// over-inclusive backend is merely less efficient, not incorrect.
    ///
    /// Backends that cannot enumerate transactions (or have nothing to
    /// complete) may return an empty `Vec` via this default, in which case
    /// proof completion is a no-op.
    ///
    /// `limit` bounds the returned page (and therefore the per-tick WoC fetch /
    /// CPU budget).
    ///
    /// `min_age_secs` is the PUSH-PRIMARY BACKSTOP gate (bsv-low #228 /
    /// arcade#259): rows stored less than `min_age_secs` ago are EXCLUDED —
    /// their proof is expected to arrive via the Arcade MINED webhook
    /// (`/arc-ingest`) at push speed, so polling them is wasted budget. Rules:
    /// - `0` disables the gate (poll everything — today's behaviour).
    /// - a row whose age is UNKNOWN (pre-migration `NULL` timestamp) MUST be
    ///   treated as OLD, i.e. eligible — the fail-safe direction is to poll
    ///   MORE, never to starve a row of its backstop.
    /// The reference D1 backend answers with
    /// `AND (created_at IS NULL OR created_at <= unixepoch() - min_age_secs)`.
    async fn find_transactions_for_proof_check(
        &self,
        limit: u64,
        min_age_secs: u64,
    ) -> Result<Vec<TransactionBeef>, StorageError> {
        let _ = (limit, min_age_secs);
        Ok(Vec::new())
    }

    // ========================================================================
    // GASP sync state
    // ========================================================================

    /// Update the last interaction score for a host+topic pair (upsert).
    async fn update_last_interaction(
        &self,
        host: &str,
        topic: &str,
        since: u64,
    ) -> Result<(), StorageError>;

    /// Get the last interaction score for a host+topic pair. Returns 0 if not found.
    async fn get_last_interaction(&self, host: &str, topic: &str) -> Result<u64, StorageError>;

    // ========================================================================
    // GASP peer health (bsv-low#302 — dead-peer quarantine)
    // ========================================================================

    // The three peer-health methods are REQUIRED (bsv-low #555, the
    // delta-2 fold's D2-L2): with defaults a wrapper that did not forward
    // one compiled and silently turned the quarantine or the yieldless bound
    // off. A backend that keeps no peer health answers as the old defaults
    // did (`Ok(())`, `PeerSyncHealth::default()`, `PeerYieldStreak::default()`):
    // fail-safe, every peer keeps being attempted. `host` is the peer's
    // NORMALIZED ORIGIN (`crate::gasp::peer_origin`), not its URL: the engine
    // passes it (the delta-2 fold's D2-M2).

    /// Record the outcome of ONE GASP sync attempt with `host` for `topic`
    /// (bsv-low#302). `success = true` resets the consecutive-failure count
    /// to 0 (full re-admission); `false` increments it. The backend stamps
    /// its own clock for the attempt time — the engine never supplies wall
    /// time. Quarantine-SKIPPED peers are NOT recorded (a skip is not an
    /// attempt; the last-attempt age must keep growing so the re-probe
    /// window opens). Nor is, with a per-graph budget, a sync of a peer at
    /// the quarantine threshold that did not fail and yielded nothing (bsv-low
    /// #555, the delta-4 fold's D4-L2): only a yield re-admits that peer.
    async fn record_peer_sync_outcome(
        &self,
        host: &str,
        topic: &str,
        success: bool,
    ) -> Result<(), StorageError>;

    /// Current sync health of the (host, topic) pairing (bsv-low#302). The
    /// backend answers with its consecutive-failure count and the AGE of
    /// the last attempt (relative seconds, so the engine needs no clock of
    /// its own). Pristine: never attempted, never quarantined.
    async fn get_peer_sync_health(
        &self,
        host: &str,
        topic: &str,
    ) -> Result<PeerSyncHealth, StorageError>;

    /// Record whether ONE GASP sync with `host` for `topic` YIELDED (it
    /// finalized a graph or moved the cursor) or not, and answer the
    /// yieldless STREAK it now holds (bsv-low #555, the delta fold's D-M2 and
    /// the delta-2 fold's D2-M1; `crate::gasp::yieldless_sync_failed`):
    /// - `true` ends the streak: count 0, no start; answers that.
    /// - `false` adds one to the streak and answers its count and the
    ///   seconds since its FIRST yieldless sync, on the backend's own clock
    ///   (0 on the sync that starts it). A streak whose LAST yieldless sync
    ///   is more than `crate::gasp::PEER_YIELDLESS_DECAY_SECS` old starts
    ///   again (count 1, age 0): the decay. NOT for a peer whose
    ///   [`Self::record_peer_sync_outcome`] count is above 0 (the delta-3
    ///   fold's D3-M1): the decay equals the reprobe window, so a quarantined
    ///   peer's probe would start a fresh streak, succeed and lift the
    ///   quarantine; its probe continues the streak instead and fails.
    ///
    /// The engine calls it only with a per-graph budget, only for a sync
    /// the peer served work, and before [`Self::record_peer_sync_outcome`].
    async fn record_peer_sync_yield(
        &self,
        host: &str,
        topic: &str,
        yielded: bool,
    ) -> Result<PeerYieldStreak, StorageError>;

    // ========================================================================
    // Deferred GASP graphs (bsv-low #555)
    // ========================================================================

    /// Save (REPLACE) the record of one deferred GASP graph, keyed by
    /// (`record.peer`, `record.topic`, `record.outpoint`). One row per graph:
    /// a later deferral of the same graph overwrites it, never appends.
    /// `AtCeiling`: refused at a bound of the storage's own (the worker's
    /// global ceiling), counted `too_many`; the walk then goes on under the
    /// per-peer budget alone, as before #555.
    ///
    /// The four deferred-graph methods are REQUIRED (the lens fold's M2,
    /// the house style of `TopicManager::identify_admissible_outputs`): with
    /// a default, a storage or WRAPPER that did not forward them compiled,
    /// and `Engine::set_graph_budget` over it saved nothing, walked every
    /// graph past the call budget from its root on every tick and never
    /// admitted it (`store_fault`), a hard cap on graph size worse than no
    /// budget. A wrapper forwards all four to the storage it wraps:
    ///
    /// ```ignore
    /// async fn put_deferred_graph(&self, record: &DeferredGraph)
    ///     -> Result<DeferredGraphSave, StorageError> {
    ///     self.inner.put_deferred_graph(record).await
    /// }
    /// // and the same for find_deferred_graphs, get_deferred_graph and
    /// // delete_deferred_graph.
    /// ```
    ///
    /// A backend that keeps no records answers none from
    /// `find_deferred_graphs` and `get_deferred_graph` and an `Err` from
    /// this one: every graph past the budget then fails its UTXO, so it
    /// must not be given a per-graph budget.
    async fn put_deferred_graph(
        &self,
        record: &crate::gasp::DeferredGraph,
    ) -> Result<crate::gasp::DeferredGraphSave, StorageError>;

    /// The KEYS of the records of (`host`, `topic`), lowest score first. A
    /// sync reads these up front and each record only when its UTXO is
    /// served (the lens fold's L3).
    async fn find_deferred_graphs(
        &self,
        host: &str,
        topic: &str,
    ) -> Result<Vec<crate::gasp::DeferredGraphKey>, StorageError>;

    /// The record of the graph of (`host`, `topic`) rooted at `outpoint`.
    async fn get_deferred_graph(
        &self,
        host: &str,
        topic: &str,
        outpoint: &str,
    ) -> Result<Option<crate::gasp::DeferredGraph>, StorageError>;

    /// Delete the record of the graph rooted at `outpoint`.
    async fn delete_deferred_graph(
        &self,
        host: &str,
        topic: &str,
        outpoint: &str,
    ) -> Result<(), StorageError>;
}

/// A (host, topic)'s yieldless streak (bsv-low #555, the delta-2 fold's
/// D2-M1) — the input to [`crate::gasp::yieldless_sync_failed`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct PeerYieldStreak {
    /// Consecutive yieldless syncs (0 after a yield).
    pub yieldless_syncs: u64,
    /// Seconds since the streak's first yieldless sync. `None` = no streak,
    /// or a backend with no clock: never past the time bound.
    pub secs_since_first: Option<u64>,
}

/// Durable per-(host, topic) GASP sync health (bsv-low#302) — the input to
/// the pure quarantine rule [`crate::gasp::peer_sync_quarantined`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct PeerSyncHealth {
    /// Consecutive sync attempts that ALL failed (timeout counts as a
    /// failure). Reset to 0 by any successful sync.
    pub consecutive_failures: u64,
    /// Seconds since the last recorded sync ATTEMPT (success or failure).
    /// `None` = never attempted / no health tracked — never quarantined.
    pub secs_since_last_attempt: Option<u64>,
}

// ============================================================================
// Rc delegation
// ============================================================================

/// Delegating impl so an `Rc<T: Storage>` is itself a `Storage` — lets a
/// caller keep a shared handle to the concrete backend (e.g. a test advancing
/// [`memory::MemoryStorage::advance_clock`]) while the engine owns a
/// `Box<dyn Storage>` over the same instance. Pure delegation, no semantics.
#[async_trait(?Send)]
impl<T: Storage + ?Sized> Storage for std::rc::Rc<T> {
    async fn insert_output(&self, output: &Output) -> Result<(), StorageError> {
        (**self).insert_output(output).await
    }
    async fn delete_output(
        &self,
        txid: &str,
        output_index: u32,
        topic: &str,
    ) -> Result<(), StorageError> {
        (**self).delete_output(txid, output_index, topic).await
    }
    async fn mark_utxo_as_spent(
        &self,
        txid: &str,
        output_index: u32,
        topic: &str,
    ) -> Result<(), StorageError> {
        (**self).mark_utxo_as_spent(txid, output_index, topic).await
    }
    async fn update_consumed_by(
        &self,
        txid: &str,
        output_index: u32,
        topic: &str,
        consumed_by: &[Outpoint],
    ) -> Result<(), StorageError> {
        (**self)
            .update_consumed_by(txid, output_index, topic, consumed_by)
            .await
    }
    async fn update_transaction_beef(&self, txid: &str, beef: &[u8]) -> Result<(), StorageError> {
        (**self).update_transaction_beef(txid, beef).await
    }
    async fn mark_transaction_proven(&self, txid: &str) -> Result<(), StorageError> {
        (**self).mark_transaction_proven(txid).await
    }
    async fn mark_transaction_proven_at(
        &self,
        txid: &str,
        height: Option<u64>,
    ) -> Result<(), StorageError> {
        // review LOW-4: forward the anchored latch too — without this the
        // Rc blanket hits the trait default (drops the height, re-introducing
        // MED-2) for any caller routing through an `Rc<dyn Storage>`.
        (**self).mark_transaction_proven_at(txid, height).await
    }
    async fn update_output_block_height(
        &self,
        txid: &str,
        output_index: u32,
        topic: &str,
        block_height: u32,
    ) -> Result<(), StorageError> {
        (**self)
            .update_output_block_height(txid, output_index, topic, block_height)
            .await
    }
    async fn insert_applied_transaction(
        &self,
        tx: &AppliedTransaction,
    ) -> Result<(), StorageError> {
        (**self).insert_applied_transaction(tx).await
    }
    async fn does_applied_transaction_exist(
        &self,
        tx: &AppliedTransaction,
    ) -> Result<bool, StorageError> {
        (**self).does_applied_transaction_exist(tx).await
    }
    async fn delete_applied_transaction(
        &self,
        tx: &AppliedTransaction,
    ) -> Result<(), StorageError> {
        (**self).delete_applied_transaction(tx).await
    }
    async fn find_output(
        &self,
        txid: &str,
        output_index: u32,
        topic: Option<&str>,
        spent: Option<bool>,
        include_beef: bool,
    ) -> Result<Option<Output>, StorageError> {
        (**self)
            .find_output(txid, output_index, topic, spent, include_beef)
            .await
    }
    async fn find_served_outputs(
        &self,
        outpoints: &[Outpoint],
    ) -> Result<Vec<Output>, StorageError> {
        (**self).find_served_outputs(outpoints).await
    }

    async fn find_outputs_for_transaction(
        &self,
        txid: &str,
        include_beef: bool,
    ) -> Result<Vec<Output>, StorageError> {
        (**self)
            .find_outputs_for_transaction(txid, include_beef)
            .await
    }
    async fn find_utxos_for_topic(
        &self,
        topic: &str,
        since: Option<f64>,
        limit: Option<u64>,
        include_beef: bool,
    ) -> Result<Vec<Output>, StorageError> {
        (**self)
            .find_utxos_for_topic(topic, since, limit, include_beef)
            .await
    }
    async fn find_transactions_for_proof_check(
        &self,
        limit: u64,
        min_age_secs: u64,
    ) -> Result<Vec<TransactionBeef>, StorageError> {
        (**self)
            .find_transactions_for_proof_check(limit, min_age_secs)
            .await
    }
    async fn update_last_interaction(
        &self,
        host: &str,
        topic: &str,
        since: u64,
    ) -> Result<(), StorageError> {
        (**self).update_last_interaction(host, topic, since).await
    }
    async fn get_last_interaction(&self, host: &str, topic: &str) -> Result<u64, StorageError> {
        (**self).get_last_interaction(host, topic).await
    }
    async fn record_peer_sync_outcome(
        &self,
        host: &str,
        topic: &str,
        success: bool,
    ) -> Result<(), StorageError> {
        (**self)
            .record_peer_sync_outcome(host, topic, success)
            .await
    }
    async fn get_peer_sync_health(
        &self,
        host: &str,
        topic: &str,
    ) -> Result<PeerSyncHealth, StorageError> {
        (**self).get_peer_sync_health(host, topic).await
    }
    async fn record_peer_sync_yield(
        &self,
        host: &str,
        topic: &str,
        yielded: bool,
    ) -> Result<PeerYieldStreak, StorageError> {
        (**self).record_peer_sync_yield(host, topic, yielded).await
    }
    async fn put_deferred_graph(
        &self,
        record: &crate::gasp::DeferredGraph,
    ) -> Result<crate::gasp::DeferredGraphSave, StorageError> {
        (**self).put_deferred_graph(record).await
    }
    async fn find_deferred_graphs(
        &self,
        host: &str,
        topic: &str,
    ) -> Result<Vec<crate::gasp::DeferredGraphKey>, StorageError> {
        (**self).find_deferred_graphs(host, topic).await
    }
    async fn get_deferred_graph(
        &self,
        host: &str,
        topic: &str,
        outpoint: &str,
    ) -> Result<Option<crate::gasp::DeferredGraph>, StorageError> {
        (**self).get_deferred_graph(host, topic, outpoint).await
    }
    async fn delete_deferred_graph(
        &self,
        host: &str,
        topic: &str,
        outpoint: &str,
    ) -> Result<(), StorageError> {
        (**self).delete_deferred_graph(host, topic, outpoint).await
    }
}

// ============================================================================
// Error type
// ============================================================================

/// Storage operation errors.
#[derive(Debug, thiserror::Error)]
pub enum StorageError {
    #[error("not found: {0}")]
    NotFound(String),

    #[error("duplicate: {0}")]
    Duplicate(String),

    #[error("database error: {0}")]
    Database(String),

    #[error("serialization error: {0}")]
    Serialization(String),

    #[error("{0}")]
    Other(String),
}

// ============================================================================
// In-memory storage (for tests and local dev)
// ============================================================================

#[cfg(any(test, feature = "memory-storage"))]
pub mod memory {
    use super::*;
    use std::collections::HashMap;
    use std::sync::Mutex;

    /// In-memory Storage implementation for testing and local development.
    ///
    /// NOT suitable for production — no persistence, no concurrency beyond Mutex.
    #[derive(Debug, Default)]
    pub struct MemoryStorage {
        /// outputs keyed by (txid, outputIndex, topic)
        outputs: Mutex<HashMap<(String, u32, String), Output>>,
        /// BEEF data keyed by txid
        transactions: Mutex<HashMap<String, Vec<u8>>>,
        /// txids whose `has_proof` flag is set. Models the real D1
        /// `transactions.has_proof` column (overlay migration 0010), which is
        /// the source of truth for `find_transactions_for_proof_check` — NOT a
        /// live re-parse of the BEEF. A txid is absent here (flag = 0) until
        /// something explicitly proves it, so a BEEF that already carries a
        /// proof but is not in this set is still returned as a candidate — the
        /// exact window-clog the cron must clear via `mark_transaction_proven`.
        proven: Mutex<std::collections::HashSet<String>>,
        /// applied transactions keyed by (txid, topic)
        applied: Mutex<HashMap<(String, String), bool>>,
        /// GASP sync state keyed by (host, topic)
        sync_state: Mutex<HashMap<(String, String), u64>>,
        /// Deterministic logical clock (seconds) for the push-primary backstop
        /// age gate — models the D1 backend's `unixepoch()`. Tests advance it
        /// with [`MemoryStorage::advance_clock`]; no wall clock is ever read
        /// (wasm-safe, deterministic).
        clock_secs: Mutex<u64>,
        /// First-store stamp (clock seconds) per txid — models the D1
        /// `transactions.created_at` column. A txid ABSENT here has UNKNOWN
        /// age and is treated as OLD (eligible) — the fail-safe direction.
        created_at: Mutex<HashMap<String, u64>>,
        /// GASP peer health keyed by (host, topic) —
        /// `(consecutive_failures, last_attempt_at_clock_secs)`. Models the
        /// D1 `gasp_peer_health` table (bsv-low#302); the logical clock
        /// above stands in for `unixepoch()`.
        peer_health: Mutex<HashMap<(String, String), (u64, u64)>>,
        /// review LOW-4: records the height an `_at` latch carried, so a pin
        /// can prove the `Rc` blanket forwards `mark_transaction_proven_at`
        /// (the trait default would drop the height).
        proven_at: Mutex<HashMap<String, Option<u64>>>,
        /// Deferred GASP graphs (bsv-low #555) keyed by (host, topic,
        /// outpoint): models the D1 `gasp_deferred_graphs` table.
        deferred_graphs: Mutex<HashMap<(String, String, String), crate::gasp::DeferredGraph>>,
        /// How many times a deferred graph was WRITTEN (a pin counts the
        /// storage writes of a deferral).
        deferred_graph_writes: Mutex<u64>,
        /// How many single records were READ (bsv-low #555, the lens
        /// fold's L3).
        deferred_graph_reads: Mutex<u64>,
        /// Test knob: the key read of the deferred graphs faults.
        deferred_graph_keys_fault: Mutex<bool>,
        /// Test knob: a NEW record past this many held is refused
        /// `AtCeiling` (the worker's global ceiling).
        deferred_graph_ceiling: Mutex<Option<usize>>,
        /// Test knob: a save that would take the bytes of every record held
        /// past this is refused `AtCeiling`, a held key's too (the worker's
        /// byte bounds, bsv-low #585).
        deferred_graph_byte_ceiling: Mutex<Option<usize>>,
        /// Test knob: every save of a deferred graph faults.
        deferred_graph_put_fault: Mutex<bool>,
        /// How many saves of a deferred graph were ASKED (written, refused
        /// or faulted).
        deferred_graph_put_attempts: Mutex<u64>,
        /// The yieldless streak keyed by (host, topic): `(count,
        /// first_at, last_at)` on the logical clock. Models the D1
        /// `gasp_peer_health` columns `yieldless_syncs`, `first_yieldless_at`
        /// and `last_yieldless_at` (bsv-low #555, the delta fold's D-M2 and
        /// the delta-2 fold's D2-M1).
        peer_yieldless: Mutex<HashMap<(String, String), YieldStreakRow>>,
    }

    /// `(count, first_at, last_at)` of a yieldless streak, on the logical
    /// clock.
    type YieldStreakRow = (u64, u64, u64);

    impl MemoryStorage {
        pub fn new() -> Self {
            Self::default()
        }

        /// The anchor height a `mark_transaction_proven_at` recorded for
        /// `txid` (review LOW-4): `None` = never called `_at`; `Some(h)` =
        /// called with height `h`.
        pub fn proven_at_height(&self, txid: &str) -> Option<Option<u64>> {
            self.proven_at.lock().unwrap().get(txid).copied()
        }

        /// Advance the deterministic logical clock by `secs` (test hook for
        /// the push-primary backstop age gate — see
        /// [`Storage::find_transactions_for_proof_check`]).
        pub fn advance_clock(&self, secs: u64) {
            *self.clock_secs.lock().unwrap() += secs;
        }

        /// Stamp `txid`'s first-store time at the current clock if absent
        /// (models `COALESCE(existing, unixepoch())` in the D1 backend).
        fn stamp_created(&self, txid: &str) {
            let now = *self.clock_secs.lock().unwrap();
            self.created_at
                .lock()
                .unwrap()
                .entry(txid.to_string())
                .or_insert(now);
        }

        /// Whether `txid` clears the backstop age gate: unknown age is OLD
        /// (eligible, fail-safe); otherwise `clock - created >= min_age_secs`.
        fn age_gate_open(&self, txid: &str, min_age_secs: u64) -> bool {
            if min_age_secs == 0 {
                return true;
            }
            let now = *self.clock_secs.lock().unwrap();
            match self.created_at.lock().unwrap().get(txid) {
                None => true, // unknown age → treated old → eligible
                Some(created) => now.saturating_sub(*created) >= min_age_secs,
            }
        }

        /// Every deferred GASP graph held, any peer or topic (bsv-low #555).
        pub fn deferred_graphs(&self) -> Vec<crate::gasp::DeferredGraph> {
            let mut all: Vec<_> = self
                .deferred_graphs
                .lock()
                .unwrap()
                .values()
                .cloned()
                .collect();
            all.sort_by(|a, b| (a.score, &a.outpoint).cmp(&(b.score, &b.outpoint)));
            all
        }

        /// How many deferred-graph writes were made (bsv-low #555).
        pub fn deferred_graph_writes(&self) -> u64 {
            *self.deferred_graph_writes.lock().unwrap()
        }

        /// How many single records were read (bsv-low #555).
        pub fn deferred_graph_reads(&self) -> u64 {
            *self.deferred_graph_reads.lock().unwrap()
        }

        /// Make the key read of the deferred graphs fault, or not (a test
        /// knob, bsv-low #555).
        pub fn set_deferred_graph_keys_fault(&self, fault: bool) {
            *self.deferred_graph_keys_fault.lock().unwrap() = fault;
        }

        /// Refuse a NEW record `AtCeiling` once this many are held, as the
        /// worker's global ceiling does (a test knob, bsv-low #555).
        pub fn set_deferred_graph_ceiling(&self, ceiling: Option<usize>) {
            *self.deferred_graph_ceiling.lock().unwrap() = ceiling;
        }

        /// Refuse `AtCeiling` a save, new or a replacement, that would take
        /// the bytes of every record held past this, as the worker's byte
        /// bounds do (a test knob, bsv-low #585). The held record stays.
        pub fn set_deferred_graph_byte_ceiling(&self, ceiling: Option<usize>) {
            *self.deferred_graph_byte_ceiling.lock().unwrap() = ceiling;
        }

        /// Make every save of a deferred graph fault, or not (a test knob,
        /// bsv-low #555's delta fold, D-L2).
        pub fn set_deferred_graph_put_fault(&self, fault: bool) {
            *self.deferred_graph_put_fault.lock().unwrap() = fault;
        }

        /// How many saves of a deferred graph were asked, whatever their
        /// answer (bsv-low #555's delta fold, D-L1).
        pub fn deferred_graph_put_attempts(&self) -> u64 {
            *self.deferred_graph_put_attempts.lock().unwrap()
        }

        /// The consecutive yieldless syncs held for (host, topic)
        /// (bsv-low #555's delta fold, D-M2). `host` may be a peer URL: it
        /// is read under its [`crate::gasp::peer_origin`], the key the
        /// engine writes (the delta-2 fold's D2-M2).
        pub fn peer_yieldless_syncs(&self, host: &str, topic: &str) -> u64 {
            self.peer_yieldless
                .lock()
                .unwrap()
                .get(&(crate::gasp::peer_origin(host), topic.to_string()))
                .map_or(0, |(count, _, _)| *count)
        }

        /// Count total outputs (for testing assertions).
        pub fn output_count(&self) -> usize {
            self.outputs.lock().unwrap().len()
        }

        /// Count total transactions (for testing assertions).
        pub fn transaction_count(&self) -> usize {
            self.transactions.lock().unwrap().len()
        }
    }

    #[async_trait(?Send)]
    impl Storage for MemoryStorage {
        async fn insert_output(&self, output: &Output) -> Result<(), StorageError> {
            let key = (
                output.txid.clone(),
                output.output_index,
                output.topic.clone(),
            );
            let mut outputs = self.outputs.lock().unwrap();

            // No-op if already exists (match KnexStorage behavior)
            if outputs.contains_key(&key) {
                // Still upsert BEEF if provided
                if let Some(ref beef) = output.beef {
                    self.transactions
                        .lock()
                        .unwrap()
                        .entry(output.txid.clone())
                        .or_insert_with(|| beef.clone());
                }
                return Ok(());
            }

            // Store output without BEEF (BEEF goes in transactions table)
            let mut stored = output.clone();
            stored.beef = None;
            outputs.insert(key, stored);

            // Upsert BEEF into transactions table
            if let Some(ref beef) = output.beef {
                self.transactions
                    .lock()
                    .unwrap()
                    .entry(output.txid.clone())
                    .or_insert_with(|| beef.clone());
                self.stamp_created(&output.txid);
            }

            Ok(())
        }

        async fn delete_output(
            &self,
            txid: &str,
            output_index: u32,
            topic: &str,
        ) -> Result<(), StorageError> {
            let key = (txid.to_string(), output_index, topic.to_string());
            let mut outputs = self.outputs.lock().unwrap();
            outputs.remove(&key);

            // If no more outputs reference this txid, remove the BEEF
            let has_remaining = outputs.keys().any(|(t, _, _)| t == txid);
            if !has_remaining {
                self.transactions.lock().unwrap().remove(txid);
                self.proven.lock().unwrap().remove(txid);
            }

            Ok(())
        }

        async fn mark_utxo_as_spent(
            &self,
            txid: &str,
            output_index: u32,
            topic: &str,
        ) -> Result<(), StorageError> {
            let key = (txid.to_string(), output_index, topic.to_string());
            if let Some(output) = self.outputs.lock().unwrap().get_mut(&key) {
                output.spent = true;
            }
            Ok(())
        }

        async fn update_consumed_by(
            &self,
            txid: &str,
            output_index: u32,
            topic: &str,
            consumed_by: &[Outpoint],
        ) -> Result<(), StorageError> {
            let key = (txid.to_string(), output_index, topic.to_string());
            if let Some(output) = self.outputs.lock().unwrap().get_mut(&key) {
                output.consumed_by = consumed_by.to_vec();
            }
            Ok(())
        }

        async fn update_transaction_beef(
            &self,
            txid: &str,
            beef: &[u8],
        ) -> Result<(), StorageError> {
            // Model the D1 backend: keep the proof flag accurate on every BEEF
            // write by inspecting whether the new BEEF proves the target tx.
            // (The proof-completion stitch calls back here with a proven BEEF,
            // which is how a row legitimately flips proofless → proven.)
            // A streaming fold: one element in hand, no `Beef` built.
            let has_proof = beef_limits::has_proof(beef, txid);
            if has_proof {
                self.proven.lock().unwrap().insert(txid.to_string());
            }
            self.transactions
                .lock()
                .unwrap()
                .insert(txid.to_string(), beef.to_vec());
            // Preserve-or-stamp: a rewrite keeps the ORIGINAL first-store
            // time (models the D1 COALESCE), so the backstop age stays real.
            self.stamp_created(txid);
            Ok(())
        }

        async fn mark_transaction_proven(&self, txid: &str) -> Result<(), StorageError> {
            // Lightweight flag-flip (no BEEF rewrite), idempotent. Mirrors the
            // D1 `UPDATE transactions SET has_proof = 1 WHERE txid = ?`.
            self.proven.lock().unwrap().insert(txid.to_string());
            Ok(())
        }

        async fn mark_transaction_proven_at(
            &self,
            txid: &str,
            height: Option<u64>,
        ) -> Result<(), StorageError> {
            // Models the D1 `MARK_TX_PROVEN_AT_SQL`: flip has_proof AND record
            // the anchor (review MED-2/LOW-4). Recorded so the Rc-forward pin
            // can observe the height reaching the inner store.
            self.proven.lock().unwrap().insert(txid.to_string());
            self.proven_at
                .lock()
                .unwrap()
                .insert(txid.to_string(), height);
            Ok(())
        }

        async fn update_output_block_height(
            &self,
            txid: &str,
            output_index: u32,
            topic: &str,
            block_height: u32,
        ) -> Result<(), StorageError> {
            let key = (txid.to_string(), output_index, topic.to_string());
            if let Some(output) = self.outputs.lock().unwrap().get_mut(&key) {
                output.block_height = Some(block_height);
            }
            Ok(())
        }

        async fn insert_applied_transaction(
            &self,
            tx: &AppliedTransaction,
        ) -> Result<(), StorageError> {
            self.applied
                .lock()
                .unwrap()
                .insert((tx.txid.clone(), tx.topic.clone()), true);
            Ok(())
        }

        async fn does_applied_transaction_exist(
            &self,
            tx: &AppliedTransaction,
        ) -> Result<bool, StorageError> {
            Ok(self
                .applied
                .lock()
                .unwrap()
                .contains_key(&(tx.txid.clone(), tx.topic.clone())))
        }

        async fn delete_applied_transaction(
            &self,
            tx: &AppliedTransaction,
        ) -> Result<(), StorageError> {
            self.applied
                .lock()
                .unwrap()
                .remove(&(tx.txid.clone(), tx.topic.clone()));
            Ok(())
        }

        async fn find_output(
            &self,
            txid: &str,
            output_index: u32,
            topic: Option<&str>,
            spent: Option<bool>,
            include_beef: bool,
        ) -> Result<Option<Output>, StorageError> {
            let outputs = self.outputs.lock().unwrap();

            // If topic is specified, do a direct lookup
            if let Some(topic) = topic {
                let key = (txid.to_string(), output_index, topic.to_string());
                if let Some(output) = outputs.get(&key) {
                    if let Some(s) = spent {
                        if output.spent != s {
                            return Ok(None);
                        }
                    }
                    let mut result = output.clone();
                    if include_beef {
                        result.beef = self.transactions.lock().unwrap().get(txid).cloned();
                    }
                    return Ok(Some(result));
                }
                return Ok(None);
            }

            // No topic — find first matching (txid, outputIndex)
            for ((t, oi, _), output) in outputs.iter() {
                if t == txid && *oi == output_index {
                    if let Some(s) = spent {
                        if output.spent != s {
                            continue;
                        }
                    }
                    let mut result = output.clone();
                    if include_beef {
                        result.beef = self.transactions.lock().unwrap().get(txid).cloned();
                    }
                    return Ok(Some(result));
                }
            }
            Ok(None)
        }

        async fn find_served_outputs(
            &self,
            outpoints: &[Outpoint],
        ) -> Result<Vec<Output>, StorageError> {
            let outputs = self.find_outputs_by_outpoints(outpoints, true).await?;
            let now = i64::try_from(*self.clock_secs.lock().unwrap())
                .unwrap_or(i64::MAX)
                .saturating_mul(1000);
            let proven = self.proven.lock().unwrap();
            let stamps = self.created_at.lock().unwrap();
            Ok(outputs
                .into_iter()
                .filter(|output| {
                    let seen = stamps
                        .get(&output.txid)
                        .and_then(|s| i64::try_from(*s).ok())
                        .map(|s| s.saturating_mul(1000));
                    crate::proof_window::verdict(proven.contains(&output.txid), seen, now)
                        == crate::proof_window::Verdict::Served
                })
                .collect())
        }

        async fn find_outputs_for_transaction(
            &self,
            txid: &str,
            include_beef: bool,
        ) -> Result<Vec<Output>, StorageError> {
            let outputs = self.outputs.lock().unwrap();
            let beef = if include_beef {
                self.transactions.lock().unwrap().get(txid).cloned()
            } else {
                None
            };

            let results: Vec<Output> = outputs
                .iter()
                .filter(|((t, _, _), _)| t == txid)
                .map(|(_, output)| {
                    let mut o = output.clone();
                    if include_beef {
                        o.beef.clone_from(&beef);
                    }
                    o
                })
                .collect();

            Ok(results)
        }

        async fn find_utxos_for_topic(
            &self,
            topic: &str,
            since: Option<f64>,
            limit: Option<u64>,
            include_beef: bool,
        ) -> Result<Vec<Output>, StorageError> {
            let outputs = self.outputs.lock().unwrap();
            let transactions = self.transactions.lock().unwrap();

            let mut results: Vec<Output> = outputs
                .iter()
                .filter(|((_, _, t), o)| {
                    t == topic && !o.spent && since.is_none_or(|s| o.score.unwrap_or(0.0) >= s)
                })
                .map(|(_, output)| {
                    let mut o = output.clone();
                    if include_beef {
                        o.beef = transactions.get(&o.txid).cloned();
                    }
                    o
                })
                .collect();

            // Sort by score ascending (matching KnexStorage behavior)
            results.sort_by(|a, b| {
                a.score
                    .unwrap_or(0.0)
                    .partial_cmp(&b.score.unwrap_or(0.0))
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            if let Some(limit) = limit {
                results.truncate(limit as usize);
            }

            Ok(results)
        }

        async fn find_transactions_for_proof_check(
            &self,
            limit: u64,
            min_age_secs: u64,
        ) -> Result<Vec<TransactionBeef>, StorageError> {
            // Models the real D1 `WHERE has_proof = 0 LIMIT {limit}` query
            // (overlay migration 0010): the candidate set is driven by the
            // `has_proof` FLAG, not a live re-parse of the BEEF. A row whose
            // BEEF already carries a proof but whose flag is still 0 (e.g. a
            // GASP-synced proof written before the migration, which defaulted
            // every existing row to 0) is therefore STILL returned here — it
            // only drops out once `mark_transaction_proven` flips the flag.
            // This reproduces the window-clog the engine must clear.
            //
            // The push-primary backstop age gate (#228) excludes rows younger
            // than `min_age_secs` (their proof is expected via /arc-ingest);
            // unknown-age rows stay eligible (fail-safe — trait doc).
            let candidates: Vec<TransactionBeef> = {
                let transactions = self.transactions.lock().unwrap();
                let proven = self.proven.lock().unwrap();
                transactions
                    .iter()
                    .filter(|(txid, _)| !proven.contains(*txid))
                    .map(|(txid, beef)| TransactionBeef {
                        txid: txid.clone(),
                        beef: beef.clone(),
                    })
                    .collect()
            };
            Ok(candidates
                .into_iter()
                .filter(|c| self.age_gate_open(&c.txid, min_age_secs))
                .take(limit as usize)
                .collect())
        }

        async fn update_last_interaction(
            &self,
            host: &str,
            topic: &str,
            since: u64,
        ) -> Result<(), StorageError> {
            self.sync_state
                .lock()
                .unwrap()
                .insert((host.to_string(), topic.to_string()), since);
            Ok(())
        }

        async fn get_last_interaction(&self, host: &str, topic: &str) -> Result<u64, StorageError> {
            Ok(*self
                .sync_state
                .lock()
                .unwrap()
                .get(&(host.to_string(), topic.to_string()))
                .unwrap_or(&0))
        }

        async fn record_peer_sync_outcome(
            &self,
            host: &str,
            topic: &str,
            success: bool,
        ) -> Result<(), StorageError> {
            let now = *self.clock_secs.lock().unwrap();
            let mut health = self.peer_health.lock().unwrap();
            let entry = health
                .entry((host.to_string(), topic.to_string()))
                .or_insert((0, now));
            entry.0 = if success { 0 } else { entry.0 + 1 };
            entry.1 = now;
            Ok(())
        }

        async fn get_peer_sync_health(
            &self,
            host: &str,
            topic: &str,
        ) -> Result<PeerSyncHealth, StorageError> {
            let now = *self.clock_secs.lock().unwrap();
            Ok(self
                .peer_health
                .lock()
                .unwrap()
                .get(&(host.to_string(), topic.to_string()))
                .map(|(fails, attempt_at)| PeerSyncHealth {
                    consecutive_failures: *fails,
                    secs_since_last_attempt: Some(now.saturating_sub(*attempt_at)),
                })
                .unwrap_or_default())
        }

        async fn record_peer_sync_yield(
            &self,
            host: &str,
            topic: &str,
            yielded: bool,
        ) -> Result<PeerYieldStreak, StorageError> {
            let now = *self.clock_secs.lock().unwrap();
            let key = (host.to_string(), topic.to_string());
            // The delta-3 fold's D3-M1: the decay only for a peer that is
            // not failed (the worker's statement reads the same row).
            let not_failed = self
                .peer_health
                .lock()
                .unwrap()
                .get(&key)
                .is_none_or(|(fails, _)| *fails == 0);
            let mut held = self.peer_yieldless.lock().unwrap();
            if yielded {
                held.remove(&key);
                return Ok(PeerYieldStreak::default());
            }
            let streak = held.entry(key).or_insert((0, now, now));
            if streak.0 == 0
                || (not_failed
                    && now.saturating_sub(streak.2) > crate::gasp::PEER_YIELDLESS_DECAY_SECS)
            {
                *streak = (0, now, now);
            }
            streak.0 += 1;
            streak.2 = now;
            Ok(PeerYieldStreak {
                yieldless_syncs: streak.0,
                secs_since_first: Some(now.saturating_sub(streak.1)),
            })
        }

        async fn put_deferred_graph(
            &self,
            record: &crate::gasp::DeferredGraph,
        ) -> Result<crate::gasp::DeferredGraphSave, StorageError> {
            let key = (
                record.peer.clone(),
                record.topic.clone(),
                record.outpoint.clone(),
            );
            *self.deferred_graph_put_attempts.lock().unwrap() += 1;
            if *self.deferred_graph_put_fault.lock().unwrap() {
                return Err(StorageError::Database("deferred graph save faulted".into()));
            }
            if let Some(ceiling) = *self.deferred_graph_ceiling.lock().unwrap() {
                let held = self.deferred_graphs.lock().unwrap();
                if !held.contains_key(&key) && held.len() >= ceiling {
                    return Ok(crate::gasp::DeferredGraphSave::AtCeiling);
                }
            }
            if let Some(ceiling) = *self.deferred_graph_byte_ceiling.lock().unwrap() {
                let held = self.deferred_graphs.lock().unwrap();
                let others: usize = held
                    .iter()
                    .filter(|(k, _)| **k != key)
                    .map(|(_, r)| r.byte_size())
                    .sum();
                if others + record.byte_size() > ceiling {
                    return Ok(crate::gasp::DeferredGraphSave::AtCeiling);
                }
            }
            *self.deferred_graph_writes.lock().unwrap() += 1;
            self.deferred_graphs.lock().unwrap().insert(
                (
                    record.peer.clone(),
                    record.topic.clone(),
                    record.outpoint.clone(),
                ),
                record.clone(),
            );
            Ok(crate::gasp::DeferredGraphSave::Saved)
        }

        async fn find_deferred_graphs(
            &self,
            host: &str,
            topic: &str,
        ) -> Result<Vec<crate::gasp::DeferredGraphKey>, StorageError> {
            if *self.deferred_graph_keys_fault.lock().unwrap() {
                return Err(StorageError::Database("deferred graph keys: fault".into()));
            }
            Ok(self
                .deferred_graphs()
                .into_iter()
                .filter(|r| r.peer == host && r.topic == topic)
                .map(|r| crate::gasp::DeferredGraphKey {
                    outpoint: r.outpoint,
                    score: r.score,
                })
                .collect())
        }

        async fn get_deferred_graph(
            &self,
            host: &str,
            topic: &str,
            outpoint: &str,
        ) -> Result<Option<crate::gasp::DeferredGraph>, StorageError> {
            *self.deferred_graph_reads.lock().unwrap() += 1;
            Ok(self
                .deferred_graphs
                .lock()
                .unwrap()
                .get(&(host.to_string(), topic.to_string(), outpoint.to_string()))
                .cloned())
        }

        async fn delete_deferred_graph(
            &self,
            host: &str,
            topic: &str,
            outpoint: &str,
        ) -> Result<(), StorageError> {
            self.deferred_graphs.lock().unwrap().remove(&(
                host.to_string(),
                topic.to_string(),
                outpoint.to_string(),
            ));
            Ok(())
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::memory::MemoryStorage;
    use super::*;
    use crate::types::AppliedTransaction;

    /// review LOW-4: the `Rc<T: Storage>` blanket must FORWARD
    /// `mark_transaction_proven_at`, not fall through to the trait default
    /// (which drops the height, re-introducing MED-2). Call it through an
    /// `Rc<dyn Storage>` and assert the height reached the inner store.
    #[tokio::test]
    async fn the_rc_blanket_forwards_the_anchored_proven_latch() {
        let store = std::rc::Rc::new(MemoryStorage::new());
        let via_dyn: std::rc::Rc<dyn Storage> = store.clone();
        via_dyn
            .mark_transaction_proven_at("tx", Some(965_772))
            .await
            .unwrap();
        assert_eq!(
            store.proven_at_height("tx"),
            Some(Some(965_772)),
            "the Rc blanket forwarded the height to the inner store"
        );
        // a height-less call still forwards (records None, not "never called")
        via_dyn
            .mark_transaction_proven_at("tx2", None)
            .await
            .unwrap();
        assert_eq!(store.proven_at_height("tx2"), Some(None));
        assert_eq!(
            store.proven_at_height("never"),
            None,
            "a txid never latched is absent"
        );
    }

    fn make_output(txid: &str, index: u32, topic: &str, score: f64) -> Output {
        Output {
            txid: txid.to_string(),
            output_index: index,
            output_script: vec![0x76, 0xa9],
            satoshis: 1000,
            topic: topic.to_string(),
            spent: false,
            outputs_consumed: vec![],
            consumed_by: vec![],
            beef: Some(vec![0xBE, 0xEF]),
            block_height: None,
            score: Some(score),
        }
    }

    #[tokio::test]
    async fn test_insert_and_find_output() {
        let store = MemoryStorage::new();
        let output = make_output("abc", 0, "tm_test", 1.0);
        store.insert_output(&output).await.unwrap();

        // Find without BEEF
        let found = store
            .find_output("abc", 0, Some("tm_test"), None, false)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(found.txid, "abc");
        assert_eq!(found.output_index, 0);
        assert!(found.beef.is_none());

        // Find with BEEF
        let found = store
            .find_output("abc", 0, Some("tm_test"), None, true)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(found.beef.unwrap(), vec![0xBE, 0xEF]);
    }

    #[tokio::test]
    async fn test_insert_duplicate_is_noop() {
        let store = MemoryStorage::new();
        let output = make_output("abc", 0, "tm_test", 1.0);
        store.insert_output(&output).await.unwrap();
        store.insert_output(&output).await.unwrap();
        assert_eq!(store.output_count(), 1);
    }

    #[tokio::test]
    async fn test_delete_output_cleans_up_beef() {
        let store = MemoryStorage::new();
        let output = make_output("abc", 0, "tm_test", 1.0);
        store.insert_output(&output).await.unwrap();
        assert_eq!(store.transaction_count(), 1);

        store.delete_output("abc", 0, "tm_test").await.unwrap();
        assert_eq!(store.output_count(), 0);
        assert_eq!(store.transaction_count(), 0); // BEEF cleaned up
    }

    #[tokio::test]
    async fn test_delete_output_keeps_beef_if_other_outputs_exist() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("abc", 0, "tm_test", 1.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("abc", 1, "tm_test", 2.0))
            .await
            .unwrap();
        assert_eq!(store.output_count(), 2);
        assert_eq!(store.transaction_count(), 1);

        store.delete_output("abc", 0, "tm_test").await.unwrap();
        assert_eq!(store.output_count(), 1);
        assert_eq!(store.transaction_count(), 1); // BEEF kept for output 1
    }

    #[tokio::test]
    async fn test_mark_utxo_as_spent() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("abc", 0, "tm_test", 1.0))
            .await
            .unwrap();

        store.mark_utxo_as_spent("abc", 0, "tm_test").await.unwrap();

        let found = store
            .find_output("abc", 0, Some("tm_test"), None, false)
            .await
            .unwrap()
            .unwrap();
        assert!(found.spent);
    }

    #[tokio::test]
    async fn test_find_output_with_spent_filter() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("abc", 0, "tm_test", 1.0))
            .await
            .unwrap();

        // Not spent — should find with spent=false, not with spent=true
        assert!(store
            .find_output("abc", 0, Some("tm_test"), Some(false), false)
            .await
            .unwrap()
            .is_some());
        assert!(store
            .find_output("abc", 0, Some("tm_test"), Some(true), false)
            .await
            .unwrap()
            .is_none());

        store.mark_utxo_as_spent("abc", 0, "tm_test").await.unwrap();

        // Now spent — reversed
        assert!(store
            .find_output("abc", 0, Some("tm_test"), Some(true), false)
            .await
            .unwrap()
            .is_some());
        assert!(store
            .find_output("abc", 0, Some("tm_test"), Some(false), false)
            .await
            .unwrap()
            .is_none());
    }

    #[tokio::test]
    async fn test_update_consumed_by() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("abc", 0, "tm_test", 1.0))
            .await
            .unwrap();

        let consumed = vec![Outpoint::new("def", 0), Outpoint::new("ghi", 1)];
        store
            .update_consumed_by("abc", 0, "tm_test", &consumed)
            .await
            .unwrap();

        let found = store
            .find_output("abc", 0, Some("tm_test"), None, false)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(found.consumed_by.len(), 2);
        assert_eq!(found.consumed_by[0].txid, "def");
        assert_eq!(found.consumed_by[1].output_index, 1);
    }

    #[tokio::test]
    async fn test_update_transaction_beef() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("abc", 0, "tm_test", 1.0))
            .await
            .unwrap();

        store
            .update_transaction_beef("abc", &[0xDE, 0xAD])
            .await
            .unwrap();

        let found = store
            .find_output("abc", 0, Some("tm_test"), None, true)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(found.beef.unwrap(), vec![0xDE, 0xAD]);
    }

    #[tokio::test]
    async fn test_update_output_block_height() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("abc", 0, "tm_test", 1.0))
            .await
            .unwrap();

        store
            .update_output_block_height("abc", 0, "tm_test", 850_000)
            .await
            .unwrap();

        let found = store
            .find_output("abc", 0, Some("tm_test"), None, false)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(found.block_height, Some(850_000));
    }

    #[tokio::test]
    async fn test_find_outputs_for_transaction() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("abc", 0, "tm_test", 1.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("abc", 1, "tm_test", 2.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("other", 0, "tm_test", 3.0))
            .await
            .unwrap();

        let results = store
            .find_outputs_for_transaction("abc", false)
            .await
            .unwrap();
        assert_eq!(results.len(), 2);
    }

    #[tokio::test]
    async fn test_find_utxos_for_topic() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("a", 0, "tm_test", 1.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("b", 0, "tm_test", 2.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("c", 0, "tm_test", 3.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("d", 0, "tm_other", 4.0))
            .await
            .unwrap();

        // Mark one as spent
        store.mark_utxo_as_spent("b", 0, "tm_test").await.unwrap();

        // All unspent for tm_test
        let results = store
            .find_utxos_for_topic("tm_test", None, None, false)
            .await
            .unwrap();
        assert_eq!(results.len(), 2); // a and c (b is spent)

        // With since filter
        let results = store
            .find_utxos_for_topic("tm_test", Some(2.0), None, false)
            .await
            .unwrap();
        assert_eq!(results.len(), 1); // only c (score 3.0 >= 2.0, a is 1.0)

        // With limit
        let results = store
            .find_utxos_for_topic("tm_test", None, Some(1), false)
            .await
            .unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].txid, "a"); // lowest score first
    }

    #[tokio::test]
    async fn test_find_utxos_for_topic_sorted_by_score() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("c", 0, "tm_test", 30.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("a", 0, "tm_test", 10.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("b", 0, "tm_test", 20.0))
            .await
            .unwrap();

        let results = store
            .find_utxos_for_topic("tm_test", None, None, false)
            .await
            .unwrap();
        assert_eq!(results[0].txid, "a");
        assert_eq!(results[1].txid, "b");
        assert_eq!(results[2].txid, "c");
    }

    #[tokio::test]
    async fn test_applied_transaction() {
        let store = MemoryStorage::new();
        let tx = AppliedTransaction {
            txid: "abc".to_string(),
            topic: "tm_test".to_string(),
        };

        assert!(!store.does_applied_transaction_exist(&tx).await.unwrap());
        store.insert_applied_transaction(&tx).await.unwrap();
        assert!(store.does_applied_transaction_exist(&tx).await.unwrap());

        // Different topic — should not exist
        let tx2 = AppliedTransaction {
            txid: "abc".to_string(),
            topic: "tm_other".to_string(),
        };
        assert!(!store.does_applied_transaction_exist(&tx2).await.unwrap());
    }

    #[tokio::test]
    async fn test_sync_state() {
        let store = MemoryStorage::new();

        assert_eq!(
            store
                .get_last_interaction("host1", "tm_test")
                .await
                .unwrap(),
            0
        );

        store
            .update_last_interaction("host1", "tm_test", 100)
            .await
            .unwrap();
        assert_eq!(
            store
                .get_last_interaction("host1", "tm_test")
                .await
                .unwrap(),
            100
        );

        // Update (upsert)
        store
            .update_last_interaction("host1", "tm_test", 200)
            .await
            .unwrap();
        assert_eq!(
            store
                .get_last_interaction("host1", "tm_test")
                .await
                .unwrap(),
            200
        );

        // Different host — independent
        assert_eq!(
            store
                .get_last_interaction("host2", "tm_test")
                .await
                .unwrap(),
            0
        );
    }

    #[tokio::test]
    async fn test_find_outputs_by_outpoints_batch() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("a", 0, "tm_test", 1.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("b", 0, "tm_test", 2.0))
            .await
            .unwrap();
        store
            .insert_output(&make_output("c", 0, "tm_test", 3.0))
            .await
            .unwrap();

        let outpoints = vec![
            Outpoint::new("a", 0),
            Outpoint::new("c", 0),
            Outpoint::new("missing", 0),
        ];
        let results = store
            .find_outputs_by_outpoints(&outpoints, false)
            .await
            .unwrap();
        assert_eq!(results.len(), 2); // a and c found, missing skipped
    }

    #[tokio::test]
    async fn test_find_output_without_topic() {
        let store = MemoryStorage::new();
        store
            .insert_output(&make_output("abc", 0, "tm_test", 1.0))
            .await
            .unwrap();

        // Find without specifying topic
        let found = store
            .find_output("abc", 0, None, None, false)
            .await
            .unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().topic, "tm_test");
    }
}