decdn-node 0.1.0

The deCDN node daemon (decdn-node): runtime, request handlers, payment settlement, and the admin RPC server
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
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
//! Two-endpoint loopback test for `cdn/probe/v1`.
//!
//! Spawns a server endpoint running the probe handler, connects a client
//! endpoint over iroh on localhost, sends a `ProbeRequest`, and verifies the
//! response echoes the request, reports content availability, and carries a
//! valid EIP-712 `slash_sig` (ADR 005 / ADR 014, #318).

use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use std::num::NonZeroUsize;
use std::sync::Arc;
use std::time::Duration;

use alloy::dyn_abi::Eip712Domain;
use alloy::primitives::{Address, B256, Signature};
use alloy::signers::local::PrivateKeySigner;
use bao_tree::io::outboard::PreOrderMemOutboard;
use decdn_cache::range_pull::{IROH_BLOCK_SIZE, align_range, encode_verified_range};
use decdn_cache::{CHUNK_GROUP_BYTES, CacheEngine, FilesystemOrigin, Hash};
use decdn_common::config::ResolvedSecurity;
use decdn_incentive::ProbeSlashData;
use decdn_node::dht::routing::NodeId;
use decdn_node::dht::staker_set::{ConfigStakerSet, StakerSet};
use decdn_node::dispatch::{ConnectionLimiter, RejectReason};
use decdn_node::handlers::probe::{ProbeHandler, StakeLanePolicy};
use decdn_node::handlers::probe_rate_limit::{ProbeRateLimiter, ProbeRejectLayer};
use decdn_node::metrics::Metrics;
use decdn_node::rate_limit::RateLimitConfig;
use decdn_protocol::{
    ALPN_PROBE, APP_ERR_RATE_LIMITED, Coverage, DISCOVERY_BLOCK_BYTES, MAX_MESSAGE_SIZE,
    ProbeMessage, ProbeResponseExt, SLASH_SIG_LEN, decode_message, encode_message,
    message::{ProbeRequest, ProbeResponse, ProbeResponseBody},
    num_blocks, parse_probe_response_ext, read_frame, write_frame,
};
use iroh::endpoint::{
    ApplicationClose, Connection, ConnectionError, IdleTimeout, QuicTransportConfig, ReadError,
    ReadToEndError, VarInt, presets,
};
use iroh::protocol::ProtocolHandler;
use iroh::{Endpoint, EndpointAddr, RelayMode, SecretKey};
use tokio::task::JoinHandle;
mod support;
use support::shutdown;

/// Deterministic test `SlashJudge` EIP-712 domain (Arbitrum Sepolia chain id,
/// fixture verifying-contract address).
fn test_slash_domain() -> Eip712Domain {
    decdn_incentive::slash_judge_domain(421_614, Address::repeat_byte(0x11))
}

/// Open an empty cache (no origin) in a fresh temp dir. The returned
/// `TempDir` must be kept alive for the cache's lifetime.
async fn empty_cache() -> anyhow::Result<(CacheEngine, tempfile::TempDir)> {
    let tmp = tempfile::tempdir()?;
    let cache = CacheEngine::open(tmp.path(), vec![], 16).await?;
    Ok((cache, tmp))
}

/// Open a cache pre-seeded with `payload` (pulled+verified into the store via
/// a filesystem origin, then the origin dir is dropped). Returns the cache,
/// the blob hash, and the temp dirs to keep alive.
async fn cache_with_blob(payload: &[u8]) -> anyhow::Result<(CacheEngine, Hash, tempfile::TempDir)> {
    let hash = Hash::new(payload);
    let origin_dir = tempfile::tempdir()?;
    let hex = hash.to_hex();
    let shard = hex
        .get(..2)
        .ok_or_else(|| anyhow::anyhow!("hex too short"))?;
    let dir = origin_dir.path().join(shard);
    std::fs::create_dir_all(&dir)?;
    std::fs::write(dir.join(hex.as_str()), payload)?;

    let cache_dir = tempfile::tempdir()?;
    let origin = Arc::new(FilesystemOrigin::new(origin_dir.path()).await?);
    let cache = CacheEngine::open(
        cache_dir.path(),
        vec![origin as Arc<dyn decdn_cache::Origin>],
        16,
    )
    .await?;
    let _ = cache.get(hash).await?; // populate the local store
    drop(origin_dir); // prove subsequent reads are local
    Ok((cache, hash, cache_dir))
}

/// Open a cache pre-seeded with two distinct blobs from a single filesystem
/// origin (one engine per dir — iroh-blobs is single-writer). Returns the
/// cache, both hashes, and the cache temp dir to keep alive.
async fn cache_with_two_blobs(
    a: &[u8],
    b: &[u8],
) -> anyhow::Result<(CacheEngine, Hash, Hash, tempfile::TempDir)> {
    let origin_dir = tempfile::tempdir()?;
    for payload in [a, b] {
        let hex = Hash::new(payload).to_hex();
        let shard = hex
            .get(..2)
            .ok_or_else(|| anyhow::anyhow!("hex too short"))?;
        let dir = origin_dir.path().join(shard);
        std::fs::create_dir_all(&dir)?;
        std::fs::write(dir.join(hex.as_str()), payload)?;
    }

    let cache_dir = tempfile::tempdir()?;
    let origin = Arc::new(FilesystemOrigin::new(origin_dir.path()).await?);
    let cache = CacheEngine::open(
        cache_dir.path(),
        vec![origin as Arc<dyn decdn_cache::Origin>],
        16,
    )
    .await?;
    let (ha, hb) = (Hash::new(a), Hash::new(b));
    let _ = cache.get(ha).await?; // populate the local store
    let _ = cache.get(hb).await?;
    drop(origin_dir);
    Ok((cache, ha, hb, cache_dir))
}

/// Open a cache whose STORE holds two blobs pulled through a single fs
/// origin, then removes `foreign_payload`'s object from that origin (leaving
/// only `own_payload` servable) and refreshes the origin-held index. Models
/// the origin-only policy's target shape: a store hit for content this node's
/// own origin can no longer serve (#1759).
async fn cache_with_own_and_foreign(
    own_payload: &[u8],
    foreign_payload: &[u8],
) -> anyhow::Result<(
    CacheEngine,
    Hash,
    Hash,
    tempfile::TempDir,
    tempfile::TempDir,
)> {
    let origin_dir = tempfile::tempdir()?;
    for payload in [own_payload, foreign_payload] {
        let hex = Hash::new(payload).to_hex();
        let shard = hex
            .get(..2)
            .ok_or_else(|| anyhow::anyhow!("hex too short"))?;
        let dir = origin_dir.path().join(shard);
        std::fs::create_dir_all(&dir)?;
        std::fs::write(dir.join(hex.as_str()), payload)?;
    }

    let cache_dir = tempfile::tempdir()?;
    let origin = Arc::new(FilesystemOrigin::new(origin_dir.path()).await?);
    let cache = CacheEngine::open(
        cache_dir.path(),
        vec![origin as Arc<dyn decdn_cache::Origin>],
        16,
    )
    .await?;
    let (own_hash, foreign_hash) = (Hash::new(own_payload), Hash::new(foreign_payload));
    let _ = cache.get(own_hash).await?; // populate the local store
    let _ = cache.get(foreign_hash).await?;

    // Remove the foreign object from the fs origin: the store still holds it
    // (pulled above), but this node's own origin can no longer serve it — the
    // "store hit outside this node's own origin" shape the origin-only policy
    // must not advertise.
    let hex = foreign_hash.to_hex();
    let shard = hex
        .get(..2)
        .ok_or_else(|| anyhow::anyhow!("hex too short"))?;
    std::fs::remove_file(origin_dir.path().join(shard).join(hex.as_str()))?;
    cache.rescan_origins().await;

    Ok((cache, own_hash, foreign_hash, origin_dir, cache_dir))
}

/// Deterministic pseudo-random payload of `len` bytes (xorshift32), matching
/// the synthesis `decdn-cache`'s own coverage tests use — content doesn't
/// matter here, only that it hashes and bao-verifies consistently.
fn synth_blob(len: usize) -> ([u8; 32], Vec<u8>, bytes::Bytes) {
    let mut plaintext = vec![0u8; len];
    let mut x: u32 = 0x9e37_79b9;
    for b in &mut plaintext {
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        *b = x.to_le_bytes()[0];
    }
    let ob = PreOrderMemOutboard::create(&plaintext, IROH_BLOCK_SIZE);
    (*ob.root.as_bytes(), plaintext, bytes::Bytes::from(ob.data))
}

/// Encode the header-less interleaved bao for `[off, off + len)` of a blob
/// with the given `root`/`plaintext`/`outboard`/`total` size — the same
/// `admit_bao`-ready shape `decdn-cache`'s own tests build, reimplemented
/// here off the crate's public `range_pull` helpers (the cache crate's own
/// `synth_blob`/`bao_for` are private test-only fns, not exported).
fn bao_for(
    root: [u8; 32],
    plaintext: &[u8],
    outboard: bytes::Bytes,
    off: u64,
    len: u64,
    total: u64,
) -> anyhow::Result<(Hash, bao_tree::ChunkRanges, bytes::Bytes)> {
    let aligned = align_range(off, len, total)?;
    let s = usize::try_from(aligned.fetch_start())?;
    let e = usize::try_from(aligned.fetch_end())?;
    let slice = plaintext
        .get(s..e)
        .ok_or_else(|| anyhow::anyhow!("aligned range out of bounds"))?;
    let encoded = encode_verified_range(root, &aligned, slice, outboard)?;
    Ok((Hash::from(root), aligned.chunk_ranges().clone(), encoded))
}

/// Open an origin-less cache and admit a **partial** two-discovery-block blob
/// into it: block 0 (`[0, DISCOVERY_BLOCK_BYTES)`) admitted in full, plus the
/// blob's trailing chunk group (which is what lets iroh-blobs learn the
/// `Partial` blob's total size — it only reports one once the FINAL chunk is
/// present), while block 1's middle group is left missing. This is the
/// "cached partial holder" shape #1506 exists to advertise: a store that
/// holds ≥1 discovery block but is not `Complete`, so the OLD
/// `Complete`-gated `has_blob` would report `false` for it.
async fn cache_with_partial_two_block_blob()
-> anyhow::Result<(CacheEngine, Hash, tempfile::TempDir)> {
    let cache_dir = tempfile::tempdir()?;
    let cache = CacheEngine::open(cache_dir.path(), vec![], 16).await?;

    let group = CHUNK_GROUP_BYTES;
    let total = DISCOVERY_BLOCK_BYTES + 3 * group;
    let (root, plaintext, outboard) = synth_blob(usize::try_from(total)?);

    let (hash, block0_ranges, block0_bao) = bao_for(
        root,
        &plaintext,
        outboard.clone(),
        0,
        DISCOVERY_BLOCK_BYTES,
        total,
    )?;
    cache.admit_bao(hash, block0_ranges, block0_bao).await?;

    let (_, tail_ranges, tail_bao) =
        bao_for(root, &plaintext, outboard, total - group, group, total)?;
    cache.admit_bao(hash, tail_ranges, tail_bao).await?;

    Ok((cache, hash, cache_dir))
}

/// Parse the integer value of an `OpenMetrics` counter/gauge line
/// (`<name> <value>`) out of the encoded exposition text. Returns `None` if
/// the metric is absent — distinct from `Some(0)` so a missing counter is
/// never mistaken for an un-incremented one.
fn metric_value(text: &str, name: &str) -> Option<u64> {
    text.lines().find_map(|line| {
        let rest = line.strip_prefix(name)?.strip_prefix(' ')?;
        rest.trim().parse().ok()
    })
}

/// Build a `ProbeHandler` with the given delivery bounds, returning the
/// handler plus the random signer and domain so tests can verify `slash_sig`.
#[allow(clippy::too_many_arguments)]
fn build_handler_bounds(
    server_id: iroh::PublicKey,
    rate: u64,
    metrics: &Arc<Metrics>,
    limiter: Arc<ConnectionLimiter>,
    cache: CacheEngine,
) -> (Arc<ProbeHandler>, Arc<PrivateKeySigner>, Eip712Domain) {
    // Most tests don't exercise the ADR 005 probe rate limiter — wire a
    // permissive one so only the layer under test (the `ConnectionLimiter`,
    // hold budget, etc.) can fire.
    build_handler_with_probe_limiter(
        server_id,
        rate,
        metrics,
        limiter,
        permissive_probe_rate_limiter(metrics),
        cache,
    )
}

/// Like [`build_handler_bounds`] but with an explicit probe rate limiter, so a
/// test can install a strict [`ProbeRateLimiter`] (ADR 005 §Probe rate
/// limiting).
#[allow(clippy::too_many_arguments)]
fn build_handler_with_probe_limiter(
    server_id: iroh::PublicKey,
    rate: u64,
    metrics: &Arc<Metrics>,
    limiter: Arc<ConnectionLimiter>,
    probe_limiter: Arc<ProbeRateLimiter>,
    cache: CacheEngine,
) -> (Arc<ProbeHandler>, Arc<PrivateKeySigner>, Eip712Domain) {
    let signer = Arc::new(PrivateKeySigner::random());
    let domain = test_slash_domain();
    let handler = Arc::new(ProbeHandler::new(
        server_id,
        rate,
        Arc::clone(metrics),
        limiter,
        probe_limiter,
        cache,
        Arc::clone(&signer),
        domain.clone(),
        // No stake-lane reservation for the general-purpose builder; the
        // dedicated reservation tests use `build_handler_with_lane` (#757).
        None,
        // Relay foreign namespaces by default; the origin-only policy test
        // uses `build_handler_origin_only` (#1759).
        true,
        None,
    ));
    (handler, signer, domain)
}

/// Build a `ProbeHandler` carrying a stake-lane reservation policy (#757).
/// `stakers` seeds an in-memory [`ConfigStakerSet`]; a probe whose client
/// `NodeId` is in that set is treated as a stake-lane (node-to-node)
/// requester and never reserved out.
#[allow(clippy::too_many_arguments)]
fn build_handler_with_lane(
    server_id: iroh::PublicKey,
    rate: u64,
    metrics: &Arc<Metrics>,
    limiter: Arc<ConnectionLimiter>,
    cache: CacheEngine,
    stakers: std::collections::HashSet<NodeId>,
    reserved_holds: NonZeroUsize,
    max_holds: usize,
) -> (Arc<ProbeHandler>, Arc<PrivateKeySigner>, Eip712Domain) {
    let signer = Arc::new(PrivateKeySigner::random());
    let domain = test_slash_domain();
    let staker_set: Arc<dyn StakerSet> = Arc::new(ConfigStakerSet::new(stakers));
    let policy = StakeLanePolicy::new(staker_set, reserved_holds, max_holds);
    let handler = Arc::new(ProbeHandler::new(
        server_id,
        rate,
        Arc::clone(metrics),
        limiter,
        permissive_probe_rate_limiter(metrics),
        cache,
        Arc::clone(&signer),
        domain.clone(),
        Some(policy),
        true,
        None,
    ));
    (handler, signer, domain)
}

/// Build a `ProbeHandler` with `relay_foreign_namespaces = false` (ADR 002
/// origin-only node policy, #1759): the handler advertises backend-held
/// content only, regardless of what the store happens to hold.
#[allow(clippy::too_many_arguments)]
fn build_handler_origin_only(
    server_id: iroh::PublicKey,
    rate: u64,
    metrics: &Arc<Metrics>,
    limiter: Arc<ConnectionLimiter>,
    cache: CacheEngine,
) -> (Arc<ProbeHandler>, Arc<PrivateKeySigner>, Eip712Domain) {
    let signer = Arc::new(PrivateKeySigner::random());
    let domain = test_slash_domain();
    let handler = Arc::new(ProbeHandler::new(
        server_id,
        rate,
        Arc::clone(metrics),
        limiter,
        permissive_probe_rate_limiter(metrics),
        cache,
        Arc::clone(&signer),
        domain.clone(),
        None,
        false,
        None,
    ));
    (handler, signer, domain)
}

/// Default-bounds handler (no effective rate clamp).
fn build_handler(
    server_id: iroh::PublicKey,
    rate: u64,
    metrics: &Arc<Metrics>,
    limiter: Arc<ConnectionLimiter>,
    cache: CacheEngine,
) -> (Arc<ProbeHandler>, Arc<PrivateKeySigner>, Eip712Domain) {
    build_handler_bounds(server_id, rate, metrics, limiter, cache)
}

/// Build a permissive `ConnectionLimiter` suitable for tests that don't
/// exercise rate-limiting behaviour.
fn permissive_limiter(metrics: &Arc<Metrics>) -> Arc<ConnectionLimiter> {
    let cfg = ResolvedSecurity {
        max_concurrent_handlers: u32::MAX,
        per_source_rate_per_sec: 1_000_000.0,
        per_source_burst: u32::MAX,
        max_tracked_sources: 4096,
    };
    Arc::new(ConnectionLimiter::new(&cfg, Arc::clone(metrics)))
}

/// Build a permissive `ProbeRateLimiter` for tests that don't exercise the
/// ADR 005 probe rate-limiting behaviour (all layers effectively unbounded).
fn permissive_probe_rate_limiter(metrics: &Arc<Metrics>) -> Arc<ProbeRateLimiter> {
    let cfg = RateLimitConfig {
        per_peer_rate_per_sec: 1e9,
        per_peer_burst: u32::MAX,
        per_ip_rate_per_sec: 1e9,
        per_ip_burst: u32::MAX,
        global_rate_per_sec: 1e9,
        global_burst: u32::MAX,
        max_tracked_per_ip: 4096,
        max_tracked_per_peer: 4096,
    };
    Arc::new(ProbeRateLimiter::new(&cfg, Arc::clone(metrics)))
}

fn fresh_key() -> SecretKey {
    SecretKey::generate()
}

/// Build an endpoint bound to 127.0.0.1 with relays disabled and no discovery.
/// Returns the endpoint plus its local socket address.
async fn local_endpoint(
    secret_key: SecretKey,
    alpns: Vec<Vec<u8>>,
) -> anyhow::Result<(Endpoint, SocketAddr)> {
    let bind = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
    let ep = Endpoint::builder(presets::Minimal)
        .secret_key(secret_key)
        .alpns(alpns)
        .relay_mode(RelayMode::Disabled)
        .bind_addr(bind)
        .map_err(|e| anyhow::anyhow!("bind_addr: {e}"))?
        .bind()
        .await
        .map_err(|e| anyhow::anyhow!("bind: {e}"))?;
    let addr = ep
        .bound_sockets()
        .into_iter()
        .find(SocketAddr::is_ipv4)
        .ok_or_else(|| anyhow::anyhow!("no IPv4 bound socket"))?;
    let addr = match addr {
        SocketAddr::V4(v4) if v4.ip().is_unspecified() => {
            SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, v4.port()))
        }
        other => other,
    };
    Ok((ep, addr))
}

/// Verify a 65-byte `slash_sig` recovers to `signer`'s Ethereum address over
/// the body's frozen signed set (ADR 014 §1).
fn assert_slash_sig_valid(
    resp: &ProbeResponse,
    signer: &PrivateKeySigner,
    domain: &Eip712Domain,
) -> anyhow::Result<()> {
    anyhow::ensure!(
        resp.slash_sig.len() == SLASH_SIG_LEN,
        "slash_sig must be exactly {SLASH_SIG_LEN} bytes, got {}",
        resp.slash_sig.len()
    );
    let sig = Signature::try_from(resp.slash_sig.as_slice())
        .map_err(|e| anyhow::anyhow!("slash_sig parse: {e}"))?;
    ProbeSlashData {
        hash: B256::from(resp.body.hash),
        has_blob: resp.body.has_blob,
        rate_per_mb: resp.body.rate_per_mb,
        timestamp_us: resp.body.timestamp_us,
    }
    .verify_signer(&sig, signer.address(), domain)
    .map_err(|e| anyhow::anyhow!("slash_sig verify: {e}"))?;
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn probe_roundtrip() -> anyhow::Result<()> {
    let rate_per_mb: u64 = 42;

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    let (cache, _cache_tmp) = empty_cache().await?;
    let (handler, signer, domain) = build_handler(server_id, rate_per_mb, &metrics, limiter, cache);

    let (server_ep, server_addr) = local_endpoint(server_sk, vec![ALPN_PROBE.to_vec()]).await?;

    let server_ep_bg = server_ep.clone();
    let accept_task = tokio::spawn(async move {
        if let Some(incoming) = server_ep_bg.accept().await {
            let connecting = incoming
                .accept()
                .map_err(|e| anyhow::anyhow!("accept: {e}"))?;
            let conn = connecting
                .await
                .map_err(|e| anyhow::anyhow!("handshake: {e}"))?;
            handler
                .accept(conn)
                .await
                .map_err(|e| anyhow::anyhow!("{e}"))?;
        }
        Ok::<_, anyhow::Error>(())
    });

    let (client_ep, _) = local_endpoint(fresh_key(), vec![]).await?;

    let target = EndpointAddr::new(server_id).with_ip_addr(server_addr);

    let conn = client_ep
        .connect(target, ALPN_PROBE)
        .await
        .map_err(|e| anyhow::anyhow!("connect: {e}"))?;

    let (mut send, mut recv) = conn
        .open_bi()
        .await
        .map_err(|e| anyhow::anyhow!("open_bi: {e}"))?;

    let req = ProbeRequest {
        hash: [0x5au8; 32],
        timestamp_us: 0x00c0_ffee,
    };
    let payload = encode_message(&ProbeMessage::Request(req))?;
    write_frame(&mut send, &payload)
        .await
        .map_err(|e| anyhow::anyhow!("write: {e}"))?;
    send.finish().map_err(|e| anyhow::anyhow!("finish: {e}"))?;

    let frame = read_frame(&mut recv)
        .await
        .map_err(|e| anyhow::anyhow!("read: {e}"))?;
    let (msg, tail) = decode_message::<ProbeMessage>(&frame)?;
    let (resp, resp_ext): (ProbeResponse, ProbeResponseExt) = match msg {
        ProbeMessage::Response(r) => (r, parse_probe_response_ext(tail)?),
        ProbeMessage::Request(_) => anyhow::bail!("unexpected request variant on client"),
    };

    assert_eq!(resp.body.timestamp_us, req.timestamp_us, "timestamp echoed");
    assert_eq!(resp.body.hash, req.hash, "hash echoed");
    assert_eq!(resp.body.rate_per_mb, rate_per_mb, "rate (unclamped)");
    assert!(
        !resp.body.has_blob,
        "empty cache must report has_blob=false"
    );
    assert_eq!(resp_ext.total_bytes, None, "no size when blob absent");
    assert_slash_sig_valid(&resp, &signer, &domain)?;

    conn.close(0u32.into(), b"bye");
    shutdown([], [&client_ep]).await?;

    // Allow the server task to finish handling before closing its endpoint.
    support::reap("accept", accept_task).await??;
    shutdown([], [&server_ep]).await?;
    Ok(())
}

/// Spin up a probe server and return the client's connected [`Connection`]
/// plus the background accept task, endpoints, and the server's metrics. The
/// handler returns `Ok(())` after a request-read fault (the peer has its close
/// code, and the fault is counted in `decdn_probe_read_faults_total`) and `Err`
/// when the client never opens a stream (`ACCEPT_BI_TIMEOUT`).
struct Harness {
    client_conn: Connection,
    accept_task: JoinHandle<anyhow::Result<()>>,
    /// The probe handler's registry, for asserting fault counters.
    metrics: Arc<Metrics>,
    client_ep: Endpoint,
    server_ep: Endpoint,
    /// Kept alive so the cache dir outlives the connection.
    _cache_tmp: tempfile::TempDir,
}

async fn spin_up_probe_harness() -> anyhow::Result<Harness> {
    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    let (cache, cache_tmp) = empty_cache().await?;
    let (handler, _signer, _domain) = build_handler(server_id, 1, &metrics, limiter, cache);
    let (server_ep, server_addr) = local_endpoint(server_sk, vec![ALPN_PROBE.to_vec()]).await?;

    let server_ep_bg = server_ep.clone();
    let accept_task = tokio::spawn(async move {
        let incoming = server_ep_bg
            .accept()
            .await
            .ok_or_else(|| anyhow::anyhow!("no incoming connection"))?;
        let connecting = incoming
            .accept()
            .map_err(|e| anyhow::anyhow!("accept: {e}"))?;
        let conn = connecting
            .await
            .map_err(|e| anyhow::anyhow!("handshake: {e}"))?;
        handler
            .accept(conn)
            .await
            .map_err(|e| anyhow::anyhow!("{e}"))
    });

    let (client_ep, _) = local_endpoint(fresh_key(), vec![]).await?;
    let target = EndpointAddr::new(server_id).with_ip_addr(server_addr);
    let client_conn = client_ep
        .connect(target, ALPN_PROBE)
        .await
        .map_err(|e| anyhow::anyhow!("connect: {e}"))?;

    Ok(Harness {
        client_conn,
        accept_task,
        metrics,
        client_ep,
        server_ep,
        _cache_tmp: cache_tmp,
    })
}

/// Expect `recv.read_to_end` to fail with `expected_code` delivered either as a
/// stream `RESET_STREAM` or as a connection-level `CONNECTION_CLOSE` carrying
/// an application error code. The probe handler closes the connection with
/// the same app code it resets the stream with (probe is 1:1
/// connection:stream), so either form is a correct observation of the ADR 013
/// mapping.
async fn assert_reset_with_code(
    recv: &mut iroh::endpoint::RecvStream,
    expected_code: u32,
) -> anyhow::Result<()> {
    let expected = VarInt::from_u32(expected_code);
    match recv.read_to_end(4096).await {
        Err(ReadToEndError::Read(ReadError::Reset(code))) if code == expected => Ok(()),
        Err(ReadToEndError::Read(ReadError::ConnectionLost(
            ConnectionError::ApplicationClosed(ApplicationClose { error_code, .. }),
        ))) if error_code == expected => Ok(()),
        other => {
            anyhow::bail!("expected error carrying app code {expected_code:#x}, got {other:?}")
        }
    }
}

async fn tear_down(h: Harness) -> anyhow::Result<()> {
    h.client_conn.close(0u32.into(), b"bye");
    shutdown([], [&h.client_ep]).await?;
    // Only the join matters here, not the handler's result — bounded, so a
    // handler that parks fails here rather than parking the test.
    let _ = support::reap("accept", h.accept_task).await;
    shutdown([], [&h.server_ep]).await?;
    Ok(())
}

#[tokio::test(flavor = "multi_thread")]
async fn probe_oversized_frame_returns_too_large_code() -> anyhow::Result<()> {
    let h = spin_up_probe_harness().await?;
    let (mut send, mut recv) = h
        .client_conn
        .open_bi()
        .await
        .map_err(|e| anyhow::anyhow!("open_bi: {e}"))?;

    // Write a varint length-prefix that exceeds MAX_MESSAGE_SIZE with no payload.
    let mut bogus = Vec::new();
    let mut v = MAX_MESSAGE_SIZE + 1;
    loop {
        let byte = (v & 0x7F) as u8;
        v >>= 7;
        if v == 0 {
            bogus.push(byte);
            break;
        }
        bogus.push(byte | 0x80);
    }
    send.write_all(&bogus)
        .await
        .map_err(|e| anyhow::anyhow!("write: {e}"))?;
    send.finish().map_err(|e| anyhow::anyhow!("finish: {e}"))?;

    assert_reset_with_code(&mut recv, 0x02).await?;
    tear_down(h).await
}

#[tokio::test(flavor = "multi_thread")]
async fn probe_garbage_postcard_returns_malformed_code() -> anyhow::Result<()> {
    let h = spin_up_probe_harness().await?;
    let (mut send, mut recv) = h
        .client_conn
        .open_bi()
        .await
        .map_err(|e| anyhow::anyhow!("open_bi: {e}"))?;

    // Discriminant 0x00 is `Request`, a known variant, but its body
    // (`hash` 32 bytes + `timestamp_us`) is truncated to a single byte —
    // postcard hits end-of-input mid-struct. A genuine parse fault → 0x03,
    // distinct from an unknown discriminant (see
    // `probe_unknown_discriminant_returns_unsupported_code`).
    write_frame(&mut send, &[0x00u8, 0x01])
        .await
        .map_err(|e| anyhow::anyhow!("write: {e}"))?;
    send.finish().map_err(|e| anyhow::anyhow!("finish: {e}"))?;

    assert_reset_with_code(&mut recv, 0x03).await?;
    tear_down(h).await
}

/// ADR 013 §Application Error Codes: an unknown enum discriminant is the
/// Tier-2 graceful-evolution signal — `UNSUPPORTED_MESSAGE` (0x01), NOT
/// `MALFORMED_MESSAGE` (0x03). Pairs
/// `probe_garbage_postcard_returns_malformed_code` (genuine parse fault under
/// a known discriminant).
#[tokio::test(flavor = "multi_thread")]
async fn probe_unknown_discriminant_returns_unsupported_code() -> anyhow::Result<()> {
    let h = spin_up_probe_harness().await?;
    let (mut send, mut recv) = h
        .client_conn
        .open_bi()
        .await
        .map_err(|e| anyhow::anyhow!("open_bi: {e}"))?;

    // Discriminant 99 is far past ProbeMessage's two declared variants.
    write_frame(&mut send, &[99u8, 0])
        .await
        .map_err(|e| anyhow::anyhow!("write: {e}"))?;
    send.finish().map_err(|e| anyhow::anyhow!("finish: {e}"))?;

    assert_reset_with_code(&mut recv, 0x01).await?;
    tear_down(h).await
}

/// #577 M1 — a transport-level truncation (client promises `len` bytes
/// then finishes the stream early) hits `FrameError::Io(UnexpectedEof)`
/// in `read_frame`, which must surface as `APP_ERR_NO_ERROR` (0x00),
/// not `APP_ERR_MALFORMED_MESSAGE` (0x03). A dropped connection is not
/// a protocol fault; collapsing the two would push peers toward the
/// wrong backoff/penalty discipline. Pairs the existing
/// `probe_garbage_postcard_returns_malformed_code` (genuine
/// `Decode`-class fault, 0x03) and
/// `probe_read_timeout_closes_with_zero_code` (timeout, 0x00).
#[tokio::test(flavor = "multi_thread")]
async fn probe_io_truncated_frame_returns_zero_code() -> anyhow::Result<()> {
    let h = spin_up_probe_harness().await?;
    let (mut send, mut recv) = h
        .client_conn
        .open_bi()
        .await
        .map_err(|e| anyhow::anyhow!("open_bi: {e}"))?;

    // Write a varint length prefix promising 100 bytes of payload, then
    // write only 3 bytes and finish the stream. The server's
    // `read_frame` reads the varint, allocates the buffer, then
    // `read_exact` short-reads → `FrameError::Io(UnexpectedEof)`.
    let mut bogus = Vec::new();
    let mut v: u32 = 100;
    loop {
        let byte = (v & 0x7F) as u8;
        v >>= 7;
        if v == 0 {
            bogus.push(byte);
            break;
        }
        bogus.push(byte | 0x80);
    }
    bogus.extend_from_slice(b"abc");
    send.write_all(&bogus)
        .await
        .map_err(|e| anyhow::anyhow!("write: {e}"))?;
    send.finish().map_err(|e| anyhow::anyhow!("finish: {e}"))?;

    assert_reset_with_code(&mut recv, 0x00).await?;
    tear_down(h).await
}

#[tokio::test(flavor = "multi_thread")]
async fn probe_response_on_server_stream_returns_unsupported_code() -> anyhow::Result<()> {
    let h = spin_up_probe_harness().await?;
    let (mut send, mut recv) = h
        .client_conn
        .open_bi()
        .await
        .map_err(|e| anyhow::anyhow!("open_bi: {e}"))?;

    // Well-formed frame, but the wrong variant (server expects Request).
    let payload = encode_message(&ProbeMessage::Response(ProbeResponse {
        body: ProbeResponseBody {
            hash: [0u8; 32],
            has_blob: false,
            rate_per_mb: 0,
            timestamp_us: 0,
        },
        slash_sig: vec![0u8; SLASH_SIG_LEN],
    }))?;
    write_frame(&mut send, &payload)
        .await
        .map_err(|e| anyhow::anyhow!("write: {e}"))?;
    send.finish().map_err(|e| anyhow::anyhow!("finish: {e}"))?;

    assert_reset_with_code(&mut recv, 0x01).await?;
    tear_down(h).await
}

// ProbeHandler has two phase-level timeouts: ACCEPT_BI_TIMEOUT (client
// connected but never opened a bi-stream) and PROBE_READ_TIMEOUT (client
// opened a stream but never completed a frame). Both are 5s at runtime;
// gating tests on the real deadline would slow every CI run, so each test
// fast-forwards the handler's `tokio::time::timeout` with
// `tokio::time::pause()` + `advance()`. Both need the current-thread runtime.

/// Covers the `PROBE_READ_TIMEOUT` arm of `read_probe_request`: the handler
/// resets the stream with `APP_ERR_NO_ERROR`, closes the connection with code 0
/// and reason `probe-error`, counts the fault, and returns `Ok(())` (#2320).
///
/// The client must write before the server sees the stream: noq announces a
/// new stream to the peer only on its first `STREAM` frame, so an unwritten
/// `open_bi` leaves the server in `accept_bi` and `ACCEPT_BI_TIMEOUT` fires
/// instead. The one byte is an unfinished varint length prefix, which parks
/// `read_frame` without ending it. The stream stays open: a `finish()` would
/// end `read_frame` through the `FrameError::Io` arm.
///
/// The handshake and the write run on the real clock. A paused clock
/// auto-advances to the next timer whenever the runtime idles on socket I/O,
/// which can fire `ACCEPT_BI_TIMEOUT` before the stream reaches the server.
#[tokio::test]
async fn probe_read_timeout_closes_with_zero_code() -> anyhow::Result<()> {
    let h = spin_up_probe_harness().await?;
    let (mut send, _recv) = h
        .client_conn
        .open_bi()
        .await
        .map_err(|e| anyhow::anyhow!("open_bi: {e}"))?;
    // 0x80 is a varint byte with its continuation bit set: `read_frame` waits
    // for the next length byte.
    send.write_all(&[0x80])
        .await
        .map_err(|e| anyhow::anyhow!("write: {e}"))?;

    // Give the server real time to accept the stream and enter `read_frame`,
    // which drops the `ACCEPT_BI_TIMEOUT` timer and arms `PROBE_READ_TIMEOUT`.
    // Loopback delivery takes microseconds; the margin covers a loaded runner.
    tokio::time::sleep(Duration::from_millis(250)).await;

    // Fast-forward past PROBE_READ_TIMEOUT, then resume at once: the deadline
    // has passed, so the timer fires on the next driver turn, and the close
    // frame then crosses the socket on the real clock.
    tokio::time::pause();
    tokio::time::advance(Duration::from_secs(6)).await;
    tokio::time::resume();

    // ADR 013 defines no timeout-specific code; the handler closes with
    // `APP_ERR_NO_ERROR` (0) and the `probe-error` reason. The reason is what
    // tells this close apart from the implicit code-0, empty-reason close of a
    // dropped connection. Bounded on the real clock, so a handler that never
    // reaches the timeout arm fails here instead of parking the test.
    let closed = tokio::time::timeout(Duration::from_secs(10), h.client_conn.closed())
        .await
        .map_err(|_| anyhow::anyhow!("the server never closed the connection"))?;
    match closed {
        ConnectionError::ApplicationClosed(ApplicationClose { error_code, reason })
            if error_code == VarInt::from_u32(0) && reason.as_ref() == b"probe-error" => {}
        other => anyhow::bail!("expected an app close (0, \"probe-error\"), got {other:?}"),
    }

    let handled = support::reap("accept", h.accept_task).await?;
    anyhow::ensure!(
        handled.is_ok(),
        "the read-timeout arm returns Ok(()), got {handled:?}"
    );
    let text = h.metrics.encode()?;
    assert_eq!(
        metric_value(&text, "decdn_probe_read_faults_total"),
        Some(1),
        "the read timeout is counted once as a probe read fault"
    );

    h.client_conn.close(0u32.into(), b"bye");
    shutdown([], [&h.client_ep, &h.server_ep]).await?;
    Ok(())
}

#[tokio::test(start_paused = true)]
async fn probe_accept_bi_timeout_errors_handler() -> anyhow::Result<()> {
    let h = spin_up_probe_harness().await?;
    // Deliberately do NOT call open_bi. The handler's first await is
    // `tokio::time::timeout(ACCEPT_BI_TIMEOUT, conn.accept_bi())`, which
    // must time out and return Err.
    tokio::time::advance(Duration::from_secs(6)).await;

    // Confirm the server task returned Err with the expected message.
    // Just `.await` — no wrapper timeout, because under `start_paused`
    // `tokio::time::timeout` itself runs on the virtual clock and would
    // not trip on a non-timer deadlock. Cargo's test-harness global
    // timeout covers that pathological case.
    let joined = h
        .accept_task
        .await
        .map_err(|e| anyhow::anyhow!("join: {e}"))?;
    let Err(err) = joined else {
        anyhow::bail!("handler should have returned Err on ACCEPT_BI_TIMEOUT, got Ok");
    };
    let msg = err.to_string();
    anyhow::ensure!(
        msg.contains("accept_bi timed out"),
        "expected accept_bi timeout error, got: {msg}"
    );

    // Manual teardown — `h.accept_task` was consumed above, so the shared
    // `tear_down` helper can't run as-is. Teardown's deadline runs on the real
    // clock: a paused one auto-advances past it whenever the runtime idles on
    // socket I/O, a spurious breach.
    tokio::time::resume();
    h.client_conn.close(0u32.into(), b"bye");
    shutdown([], [&h.client_ep, &h.server_ep]).await?;
    Ok(())
}

/// Observe a rate-limit rejection's application close with a bounded retry,
/// requiring the `APP_ERR_RATE_LIMITED` (`0x10`) code **and** `expected_reason`
/// (the layer label, e.g. `"per-source"` / `"per_peer"`) at least once (#1594).
///
/// The ADR-005 reject path closes the connection with `0x10` + the layer-label
/// reason bytes, but QUIC's `CONNECTION_CLOSE` is best-effort and
/// unacknowledged: over loopback the server can tear the connection down before
/// that frame is delivered, and the client then observes an implicit code-0
/// close (quinn closes a dropped connection with code 0 / empty reason) or a
/// transport-level teardown instead. That is a rare (~1/several-thousand under
/// CI load) transport artifact, not a rate-limit-layer fault, so retry a fresh
/// rejected connection until the layer-labelled close is seen.
///
/// Requiring `expected_reason` keeps the test proving the reason-byte layering
/// it exists for: a `0x10` close carrying a *different* label is the also-`0x10`
/// global / per-IP cap and fails immediately (it is not a transient). `connect`
/// yields a fresh `(Endpoint, Connection)` per attempt; the endpoint is kept
/// alive across `closed()` and closed before the next attempt.
async fn observe_rate_limit_close<F, Fut>(
    expected_reason: &[u8],
    mut connect: F,
) -> anyhow::Result<()>
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = anyhow::Result<(Endpoint, Connection)>>,
{
    const MAX_ATTEMPTS: usize = 8;
    let expected_code = VarInt::from_u32(APP_ERR_RATE_LIMITED);
    let mut last_transient = None;
    for _ in 0..MAX_ATTEMPTS {
        let (ep, conn) = connect().await?;
        let close_err = conn.closed().await;
        match close_err {
            ConnectionError::ApplicationClosed(ApplicationClose { error_code, reason })
                if error_code == expected_code =>
            {
                if reason.as_ref() == expected_reason {
                    shutdown([], [&ep]).await?;
                    return Ok(());
                }
                anyhow::bail!(
                    "rate-limit close carried the wrong layer label: expected {:?}, got {:?}",
                    String::from_utf8_lossy(expected_reason),
                    String::from_utf8_lossy(reason.as_ref()),
                );
            }
            // Transport race (#1594): an implicit code-0 close or a
            // transport-level teardown beat the reject frame over loopback.
            // Retry with a fresh connection.
            other => last_transient = Some(other),
        }
        shutdown([], [&ep]).await?;
    }
    anyhow::bail!(
        "never observed an APP_ERR_RATE_LIMITED close carrying {:?} in {MAX_ATTEMPTS} attempts; \
         last transient close: {last_transient:?}",
        String::from_utf8_lossy(expected_reason),
    )
}

/// End-to-end check that the per-source rate limiter rejects with
/// `APP_ERR_RATE_LIMITED` (`0x10`) on the wire. Without this, the dispatch
/// reject path is dead code under tests — every other test in this file uses
/// `permissive_limiter`.
///
/// Strict per-IP burst=1 limiter. The bucket for the loopback source key is
/// drained out-of-band via the limiter's test hook, so the single live client
/// connection is unconditionally rejected by `ConnectionLimiter::acquire` at
/// the top of `serve` and observes `APP_ERR_RATE_LIMITED` on its
/// `CONNECTION_CLOSE`.
///
/// Draining the bucket out of band is what keeps this deterministic: the test
/// opens no throwaway connection to charge the bucket, so nothing races the one
/// live connection's establishment, and the accept loop assumes no connection
/// count.
#[tokio::test(flavor = "multi_thread")]
async fn probe_rate_limit_returns_rate_limited_close_code() -> anyhow::Result<()> {
    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());

    // burst=1 per-source so the loopback source key rejects after a single
    // charge. Global is loose so it doesn't interfere.
    let strict = ResolvedSecurity {
        max_concurrent_handlers: 64,
        per_source_rate_per_sec: 0.001, // negligible refill within the test window
        per_source_burst: 1,
        max_tracked_sources: 32,
    };
    let limiter = Arc::new(ConnectionLimiter::new(&strict, Arc::clone(&metrics)));

    // Pre-drain the per-source bucket for the loopback source key. The client
    // endpoint binds to 127.0.0.1, so the server resolves the connection's
    // source key to `127.0.0.1` (`source_key` leaves IPv4 unchanged); charging
    // it here exhausts the burst-1 budget before the live connection arrives.
    // Dropping the returned permit releases only the global semaphore slot —
    // the consumed per-source token is time-based and stays spent for the full
    // refill period (1 / `per_source_rate_per_sec`, ~1000s here).
    drop(
        limiter
            .acquire_for_test(Some(std::net::IpAddr::V4(Ipv4Addr::LOCALHOST)))
            .map_err(|r| anyhow::anyhow!("pre-drain unexpectedly rejected: {r:?}"))?,
    );

    let limiter_probe = Arc::clone(&limiter);
    let (cache, _cache_tmp) = empty_cache().await?;
    let (handler, _signer, _domain) = build_handler(server_id, 1, &metrics, limiter, cache);

    let (server_ep, server_addr) = local_endpoint(server_sk, vec![ALPN_PROBE.to_vec()]).await?;
    let server_ep_bg = server_ep.clone();
    let handler_bg = Arc::clone(&handler);
    // Accept in a loop, with no assumption about how many connections arrive:
    // `observe_rate_limit_close` may open more than one to ride out the rare
    // loopback close-frame race (#1594), so the server must service each.
    // Per-connection errors are swallowed so one transient teardown never tears
    // down the accept loop.
    let accept_task = tokio::spawn(async move {
        while let Some(incoming) = server_ep_bg.accept().await {
            let Ok(connecting) = incoming.accept() else {
                continue;
            };
            let Ok(conn) = connecting.await else {
                continue;
            };
            let _ = handler_bg.accept(conn).await;
        }
    });

    let target = EndpointAddr::new(server_id).with_ip_addr(server_addr);
    // Every connection is rejected by `ConnectionLimiter::acquire` at the top of
    // `serve` and closed with APP_ERR_RATE_LIMITED + the `per-source` layer
    // label. Observe with a bounded retry to absorb the #1594 close-frame race;
    // requiring the `per-source` reason bytes still proves the per-source layer
    // fired rather than the also-0x10 global cap.
    observe_rate_limit_close(RejectReason::PerSource.as_str().as_bytes(), move || {
        let target = target.clone();
        async move {
            let (client_ep, _) = local_endpoint(fresh_key(), vec![]).await?;
            // Close the just-bound endpoint on a connect error so a retried
            // attempt never leaks a socket/driver task (matches the file's
            // explicit-`close()` cleanup convention).
            match client_ep.connect(target, ALPN_PROBE).await {
                Ok(conn) => Ok((client_ep, conn)),
                Err(e) => {
                    shutdown([], [&client_ep]).await?;
                    Err(anyhow::anyhow!("connect: {e}"))
                }
            }
        }
    })
    .await?;

    // The live connection must have charged the *same* pre-drained key, not a
    // fresh one: a single tracked source confirms `peer_ip` resolved it to
    // 127.0.0.1 (a different key would have been admitted, not rejected). Every
    // retry binds a fresh client endpoint on 127.0.0.1, so the tracked-source
    // count stays 1 regardless of how many attempts the race required.
    assert_eq!(
        limiter_probe.per_source_tracked(),
        1,
        "live connection must hit the pre-drained 127.0.0.1 bucket"
    );

    shutdown([accept_task], [&server_ep]).await?;
    Ok(())
}

/// The requester side of the close the test above proves is emitted (#1986):
/// `probe_once` — the shared `cdn/probe/v1` client the node's cache-miss probe
/// fan-out and the CLI both use — must surface a `0x10` shed as the typed
/// [`UpstreamRateLimited`] sentinel carrying the layer label, recoverable with
/// `downcast_ref` through the stage context it adds. Without that the node's
/// `probe_candidate` sees a bare `open_bi failed` string and scores the shedding
/// peer `Unreachable`, the penalty a handler-level load-shed `NotFound` is
/// exonerated from.
///
/// Same strict per-source fixture as `probe_rate_limit_returns_rate_limited_close_code`,
/// and the same bounded retry over the #1594 close-frame race: an attempt whose
/// error is NOT the sentinel is retried on a fresh endpoint, and only a `0x10`
/// carrying the wrong label fails at once.
#[tokio::test(flavor = "multi_thread")]
async fn probe_once_types_a_rate_limit_close_as_upstream_rate_limited() -> anyhow::Result<()> {
    use decdn_client::UpstreamRateLimited;
    use decdn_client::probe::probe_once;

    const MAX_ATTEMPTS: usize = 8;

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());

    let strict = ResolvedSecurity {
        max_concurrent_handlers: 64,
        per_source_rate_per_sec: 0.001,
        per_source_burst: 1,
        max_tracked_sources: 32,
    };
    let limiter = Arc::new(ConnectionLimiter::new(&strict, Arc::clone(&metrics)));
    drop(
        limiter
            .acquire_for_test(Some(std::net::IpAddr::V4(Ipv4Addr::LOCALHOST)))
            .map_err(|r| anyhow::anyhow!("pre-drain unexpectedly rejected: {r:?}"))?,
    );

    let (cache, _cache_tmp) = empty_cache().await?;
    let (handler, _signer, _domain) = build_handler(server_id, 1, &metrics, limiter, cache);

    let (server_ep, server_addr) = local_endpoint(server_sk, vec![ALPN_PROBE.to_vec()]).await?;
    let server_ep_bg = server_ep.clone();
    let handler_bg = Arc::clone(&handler);
    let accept_task = tokio::spawn(async move {
        while let Some(incoming) = server_ep_bg.accept().await {
            let Ok(connecting) = incoming.accept() else {
                continue;
            };
            let Ok(conn) = connecting.await else {
                continue;
            };
            let _ = handler_bg.accept(conn).await;
        }
    });

    let target = EndpointAddr::new(server_id).with_ip_addr(server_addr);
    let mut last_transient = None;
    let mut observed = None;
    for _ in 0..MAX_ATTEMPTS {
        let (client_ep, _) = local_endpoint(fresh_key(), vec![]).await?;
        let res = probe_once(
            &client_ep,
            target.clone(),
            [0x11u8; 32],
            1_700_000_000_000_000,
            Duration::from_secs(5),
        )
        .await;
        shutdown([], [&client_ep]).await?;
        let Err(err) = res else {
            anyhow::bail!("a pre-drained per-source bucket must reject every probe");
        };
        match err.downcast_ref::<UpstreamRateLimited>() {
            Some(shed) => {
                observed = Some(shed.clone());
                break;
            }
            // Transport race (#1594): the reject frame lost to an implicit code-0
            // close or a transport teardown. Retry on a fresh connection.
            None => last_transient = Some(err),
        }
    }
    let shed = observed.ok_or_else(|| {
        anyhow::anyhow!(
            "never observed an UpstreamRateLimited probe error in {MAX_ATTEMPTS} attempts; \
             last transient error: {last_transient:?}"
        )
    })?;
    assert_eq!(
        shed.label.as_deref(),
        Some(RejectReason::PerSource.as_str()),
        "the sentinel must carry the per-source layer label off the close reason bytes"
    );

    shutdown([accept_task], [&server_ep]).await?;
    Ok(())
}

/// End-to-end check that the ADR 005 §Probe rate limiting three-layer limiter
/// (#982) — distinct from the `ConnectionLimiter` exercised above — rejects a
/// probe with `APP_ERR_RATE_LIMITED` (`0x10`) and the `per_peer` layer label on
/// the wire.
///
/// Strict per-peer burst=1 probe limiter; the per-peer bucket for the client's
/// `NodeId` is pre-drained out-of-band via `ProbeRateLimiter::check` (the
/// per-peer layer is keyed by `NodeId` independent of IP, so this is robust to
/// loopback path selection). The single live connection from that `NodeId` is
/// then unconditionally rejected at the per-peer layer. Draining out of band
/// keeps this deterministic, the same rationale as
/// `probe_rate_limit_returns_rate_limited_close_code`.
#[tokio::test(flavor = "multi_thread")]
async fn probe_three_layer_limiter_rejects_per_peer() -> anyhow::Result<()> {
    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());

    // Permissive connection limiter so only the probe three-layer limiter can
    // fire; strict per-peer (burst=1, negligible refill) probe limiter.
    let conn_limiter = permissive_limiter(&metrics);
    let strict_probe = RateLimitConfig {
        per_peer_rate_per_sec: 0.001, // negligible refill within the test window
        per_peer_burst: 1,
        per_ip_rate_per_sec: 1e9,
        per_ip_burst: u32::MAX,
        global_rate_per_sec: 1e9,
        global_burst: u32::MAX,
        max_tracked_per_ip: 4096,
        max_tracked_per_peer: 4096,
    };
    let probe_limiter = Arc::new(ProbeRateLimiter::new(&strict_probe, Arc::clone(&metrics)));

    // Pin the client key so we know its NodeId before connecting, then drain
    // the per-peer bucket for it. `peer_ip = None` skips the (loose) per-IP
    // layer; the per-peer charge is all we need.
    let client_sk = fresh_key();
    let client_id = client_sk.public();
    let client_node = NodeId::from_bytes(*client_id.as_bytes());
    probe_limiter
        .check(&client_node, None)
        .map_err(|l| anyhow::anyhow!("pre-drain unexpectedly rejected: {l:?}"))?;

    let (cache, _cache_tmp) = empty_cache().await?;
    let (handler, _signer, _domain) = build_handler_with_probe_limiter(
        server_id,
        1,
        &metrics,
        conn_limiter,
        Arc::clone(&probe_limiter),
        cache,
    );

    let (server_ep, server_addr) = local_endpoint(server_sk, vec![ALPN_PROBE.to_vec()]).await?;
    let server_ep_bg = server_ep.clone();
    let handler_bg = Arc::clone(&handler);
    // Loop-accept for the same reason as the per-source test, and with the same
    // absence of a count assumption: the bounded retry in
    // `observe_rate_limit_close` may open more than one connection to ride out
    // the #1594 close-frame race.
    let accept_task = tokio::spawn(async move {
        while let Some(incoming) = server_ep_bg.accept().await {
            let Ok(connecting) = incoming.accept() else {
                continue;
            };
            let Ok(conn) = connecting.await else {
                continue;
            };
            let _ = handler_bg.accept(conn).await;
        }
    });

    let target = EndpointAddr::new(server_id).with_ip_addr(server_addr);
    // Each connection is rejected at the ADR-005 per-peer layer and closed with
    // APP_ERR_RATE_LIMITED + the `per_peer` label. Every retry rebinds a fresh
    // client endpoint under the *same* pinned `client_sk`, so the requester
    // `NodeId` — and thus the pre-drained per-peer bucket — is unchanged and the
    // rejection is reproduced. Requiring the `per_peer` reason bytes proves the
    // per-peer layer fired (the gap #982 closed), not the also-0x10 per-IP or
    // global cap.
    observe_rate_limit_close(ProbeRejectLayer::PerPeer.as_str().as_bytes(), move || {
        let target = target.clone();
        let client_sk = client_sk.clone();
        async move {
            let (client_ep, _) = local_endpoint(client_sk, vec![]).await?;
            // Close the just-bound endpoint on a connect error so a retried
            // attempt never leaks a socket/driver task (matches the file's
            // explicit-`close()` cleanup convention).
            match client_ep.connect(target, ALPN_PROBE).await {
                Ok(conn) => Ok((client_ep, conn)),
                Err(e) => {
                    shutdown([], [&client_ep]).await?;
                    Err(anyhow::anyhow!("connect: {e}"))
                }
            }
        }
    })
    .await?;

    let scrape = metrics.encode().map_err(|e| anyhow::anyhow!("{e}"))?;
    // Normally exactly one per-peer rejection; the #1594 close-frame race can
    // make the observation retry, and every retried rejection also increments
    // this counter, so assert `>= 1` rather than a brittle exact count. The
    // reason-byte assertion above already proves it was the per-peer layer.
    let rejected = metric_value(&scrape, "decdn_probe_rate_limit_rejected_per_peer_total");
    anyhow::ensure!(
        rejected.is_some_and(|v| v >= 1),
        "probe per-peer rejection must appear in /metrics scrape:\n{scrape}"
    );

    shutdown([accept_task], [&server_ep]).await?;
    Ok(())
}

/// Drive one full probe exchange against `handler` on a loopback pair and
/// return the decoded response. Handles endpoint setup/teardown.
async fn run_one_probe(
    server_sk: SecretKey,
    handler: Arc<ProbeHandler>,
    req: ProbeRequest,
) -> anyhow::Result<(ProbeResponse, ProbeResponseExt)> {
    run_one_probe_as(fresh_key(), server_sk, handler, req).await
}

/// Like [`run_one_probe`] but with an explicit client `SecretKey`, so a test
/// can place the client's `NodeId` into the handler's staker set and exercise
/// the stake-lane probe-acceptance path (#757).
async fn run_one_probe_as(
    client_sk: SecretKey,
    server_sk: SecretKey,
    handler: Arc<ProbeHandler>,
    req: ProbeRequest,
) -> anyhow::Result<(ProbeResponse, ProbeResponseExt)> {
    let server_id = server_sk.public();
    let (server_ep, server_addr) = local_endpoint(server_sk, vec![ALPN_PROBE.to_vec()]).await?;
    let server_ep_bg = server_ep.clone();
    let accept_task = tokio::spawn(async move {
        if let Some(incoming) = server_ep_bg.accept().await {
            let connecting = incoming
                .accept()
                .map_err(|e| anyhow::anyhow!("accept: {e}"))?;
            let conn = connecting
                .await
                .map_err(|e| anyhow::anyhow!("handshake: {e}"))?;
            handler
                .accept(conn)
                .await
                .map_err(|e| anyhow::anyhow!("{e}"))?;
        }
        Ok::<_, anyhow::Error>(())
    });

    let (client_ep, _) = local_endpoint(client_sk, vec![]).await?;
    let target = EndpointAddr::new(server_id).with_ip_addr(server_addr);
    let conn = client_ep
        .connect(target, ALPN_PROBE)
        .await
        .map_err(|e| anyhow::anyhow!("connect: {e}"))?;
    let (mut send, mut recv) = conn
        .open_bi()
        .await
        .map_err(|e| anyhow::anyhow!("open_bi: {e}"))?;
    write_frame(&mut send, &encode_message(&ProbeMessage::Request(req))?)
        .await
        .map_err(|e| anyhow::anyhow!("write: {e}"))?;
    send.finish().map_err(|e| anyhow::anyhow!("finish: {e}"))?;
    let frame = read_frame(&mut recv)
        .await
        .map_err(|e| anyhow::anyhow!("read: {e}"))?;
    let (msg, tail) = decode_message::<ProbeMessage>(&frame)?;
    let resp = match msg {
        ProbeMessage::Response(r) => (r, parse_probe_response_ext(tail)?),
        ProbeMessage::Request(_) => anyhow::bail!("unexpected request variant on client"),
    };
    conn.close(0u32.into(), b"bye");
    shutdown([], [&client_ep]).await?;
    support::reap("accept", accept_task).await??;
    shutdown([], [&server_ep]).await?;
    Ok(resp)
}

/// A node holding the blob signs `has_blob: true`, reports `total_bytes`,
/// and the `slash_sig` verifies (ADR 005 §`cdn/probe/v1`, #318).
#[tokio::test(flavor = "multi_thread")]
async fn probe_has_blob_true_for_cached_blob() -> anyhow::Result<()> {
    let payload = b"probe-served content-addressed bytes";
    let (cache, hash, _cache_tmp) = cache_with_blob(payload).await?;

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    let (handler, signer, domain) = build_handler(server_id, 7, &metrics, limiter, cache);

    let req = ProbeRequest {
        hash: *hash.as_bytes(),
        timestamp_us: 0xabc_def,
    };
    let (resp, resp_ext) = run_one_probe(server_sk, handler, req).await?;

    anyhow::ensure!(resp.body.has_blob, "cached blob must report has_blob=true");
    anyhow::ensure!(
        resp_ext.total_bytes == Some(payload.len() as u64),
        "total_bytes should report the blob size, got {:?}",
        resp_ext.total_bytes
    );
    anyhow::ensure!(resp.body.hash == *hash.as_bytes(), "hash echoed");
    assert_slash_sig_valid(&resp, &signer, &domain)?;
    Ok(())
}

/// ADR 011 §Serving while chain-stale: a node holding the blob but whose chain
/// reads are stale answers `has_blob: false` — advertising it would be signed
/// slash evidence for a hash the node can no longer confirm is not taken down.
/// The freshness handle here is never stamped, so it reads stale, exactly as it
/// would after the grace window elapsed with no successful poll tick.
#[tokio::test(flavor = "multi_thread")]
async fn probe_has_blob_false_when_chain_stale() -> anyhow::Result<()> {
    let payload = b"probe content while the chain is unreachable";
    let (cache, hash, _cache_tmp) = cache_with_blob(payload).await?;

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    let signer = Arc::new(PrivateKeySigner::random());
    let domain = test_slash_domain();
    // A never-stamped freshness handle reads stale (the boot enumeration and every
    // successful poll tick stamp it; neither has run here).
    let stale = decdn_node::chain_freshness::ChainFreshness::new(Duration::from_mins(30));
    let handler = Arc::new(ProbeHandler::new(
        server_id,
        7,
        Arc::clone(&metrics),
        limiter,
        permissive_probe_rate_limiter(&metrics),
        cache,
        Arc::clone(&signer),
        domain.clone(),
        None,
        true,
        Some(stale),
    ));

    let req = ProbeRequest {
        hash: *hash.as_bytes(),
        timestamp_us: 0xabc_def,
    };
    let (resp, resp_ext) = run_one_probe(server_sk, handler, req).await?;

    anyhow::ensure!(
        !resp.body.has_blob,
        "a chain-stale node must not advertise a held blob"
    );
    anyhow::ensure!(
        resp_ext.total_bytes.is_none(),
        "a suppressed advertisement carries no size, got {:?}",
        resp_ext.total_bytes
    );
    anyhow::ensure!(
        resp_ext.consistent_with(resp.body.has_blob),
        "has_blob and coverage.is_empty() must be a biconditional"
    );
    // Still a well-formed, signed response — this is a compliance posture, not a
    // fault.
    assert_slash_sig_valid(&resp, &signer, &domain)?;
    Ok(())
}

/// #1506: `has_blob` is redefined from "holds the whole blob" to "will serve
/// at least one discovery block". A node holding only discovery block 0 of a
/// two-block blob (never `Complete`, so the OLD `Complete`-gated `has_blob`
/// answered `false`) now signs `has_blob: true`, and the extension's
/// `coverage` matches the cache's own derivation exactly: block 0 covered,
/// block 1 not.
#[tokio::test(flavor = "multi_thread")]
async fn probe_has_blob_true_and_partial_coverage_for_cached_partial_holder() -> anyhow::Result<()>
{
    let (cache, hash, _cache_tmp) = cache_with_partial_two_block_blob().await?;
    // Cross-check against the cache's own derivation (`CacheEngine::coverage`) so
    // this test also catches the handler quietly diverging from it.
    let direct_coverage = cache.coverage(hash).await?;
    anyhow::ensure!(direct_coverage.covers(0) && !direct_coverage.covers(1));
    let (_, direct_size) = cache.coverage_sized(hash).await?;
    anyhow::ensure!(direct_size.is_some(), "the fixture partial knows its size");

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    let (handler, signer, domain) = build_handler(server_id, 7, &metrics, limiter, cache);

    let req = ProbeRequest {
        hash: *hash.as_bytes(),
        timestamp_us: 0xf00d,
    };
    let (resp, resp_ext) = run_one_probe(server_sk, handler, req).await?;

    anyhow::ensure!(
        resp.body.has_blob,
        "a partial holder covering >=1 block must report has_blob=true (#1506), \
         not just a Complete holder"
    );
    anyhow::ensure!(
        resp_ext.coverage.covers(0),
        "block 0 was admitted in full and must be advertised as covered"
    );
    anyhow::ensure!(
        !resp_ext.coverage.covers(1),
        "block 1's middle group was never admitted and must not be advertised"
    );
    anyhow::ensure!(
        resp_ext.coverage == direct_coverage,
        "the handler's advertised coverage must match the cache's own derivation exactly"
    );
    // A partial holder advertises its blob size with its blocks (#2195), so a
    // requester can tell whether a set of partials spans the blob.
    anyhow::ensure!(
        resp_ext.total_bytes == direct_size,
        "a partial holder must advertise its size {direct_size:?}, got {:?}",
        resp_ext.total_bytes
    );
    anyhow::ensure!(
        resp_ext.consistent_with(resp.body.has_blob),
        "has_blob and coverage.is_empty() must be a biconditional"
    );
    assert_slash_sig_valid(&resp, &signer, &domain)?;
    Ok(())
}

/// #1506: an origin-serve-capable node (origin-held index or live origin size
/// probe) that holds NOTHING in its local cache signs `has_blob: true` with
/// all-ones `coverage` — an origin serves every block and already knows the
/// size. This is the origin all-ones case: distinct from the cached-partial
/// case above, and from a probe answer that leaves `coverage` unset.
#[tokio::test(flavor = "multi_thread")]
async fn probe_origin_held_advertises_full_coverage_without_caching() -> anyhow::Result<()> {
    let payload = b"origin-held content this node has never pulled into its own cache";
    let origin_dir = tempfile::tempdir()?;
    let hash = Hash::new(payload);
    let hex = hash.to_hex();
    let shard = hex
        .get(..2)
        .ok_or_else(|| anyhow::anyhow!("hex too short"))?;
    let dir = origin_dir.path().join(shard);
    std::fs::create_dir_all(&dir)?;
    std::fs::write(dir.join(hex.as_str()), payload)?;

    let cache_dir = tempfile::tempdir()?;
    let origin = Arc::new(FilesystemOrigin::new(origin_dir.path()).await?);
    let cache = CacheEngine::open(
        cache_dir.path(),
        vec![origin as Arc<dyn decdn_cache::Origin>],
        16,
    )
    .await?;
    // Deliberately no `cache.get(hash)` — the store must stay empty; presence
    // comes only from the fs-origin index (#1130).
    anyhow::ensure!(
        !cache.has(hash).await?,
        "the store must hold nothing for this test to exercise the origin-held path"
    );

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    let (handler, signer, domain) = build_handler(server_id, 7, &metrics, limiter, cache);

    let req = ProbeRequest {
        hash: *hash.as_bytes(),
        timestamp_us: 0xbeef,
    };
    let (resp, resp_ext) = run_one_probe(server_sk, handler, req).await?;

    anyhow::ensure!(
        resp.body.has_blob,
        "an origin-held blob must be advertised even though nothing is cached"
    );
    anyhow::ensure!(
        resp_ext.total_bytes == Some(payload.len() as u64),
        "origin content's total_bytes should report the backend size, got {:?}",
        resp_ext.total_bytes
    );
    anyhow::ensure!(
        resp_ext.coverage == Coverage::full(num_blocks(payload.len() as u64)),
        "an origin can serve every block, so coverage must be all-ones"
    );
    anyhow::ensure!(
        resp_ext.consistent_with(resp.body.has_blob),
        "has_blob and coverage.is_empty() must be a biconditional"
    );
    assert_slash_sig_valid(&resp, &signer, &domain)?;
    Ok(())
}

/// Origin-only node policy (ADR 002, #1759): with `relay_foreign_namespaces
/// = false`, the probe handler advertises exactly what its own backend
/// holds. A store hit for content outside the node's own origin — leftover
/// from before the toggle, or seeded some other way — must NOT be
/// advertised, because the serve gate now declines it and an advertise/decline
/// mismatch is the reputation hazard this policy closes. Own (fs-origin-held)
/// content is still advertised.
#[tokio::test(flavor = "multi_thread")]
async fn origin_only_probe_advertises_backend_only() -> anyhow::Result<()> {
    let own_payload = b"origin-only: own fs-origin content";
    let foreign_payload = b"origin-only: leftover store content, now foreign";
    let (cache, own_hash, foreign_hash, _origin_tmp, _cache_tmp) =
        cache_with_own_and_foreign(own_payload, foreign_payload).await?;

    let metrics = Arc::new(Metrics::new());
    let (handler, _signer, _domain) = build_handler_origin_only(
        fresh_key().public(),
        7,
        &metrics,
        permissive_limiter(&metrics),
        cache,
    );

    let foreign_req = ProbeRequest {
        hash: *foreign_hash.as_bytes(),
        timestamp_us: 1,
    };
    let (foreign_resp, foreign_resp_ext) =
        run_one_probe(fresh_key(), Arc::clone(&handler), foreign_req).await?;
    anyhow::ensure!(
        !foreign_resp.body.has_blob,
        "origin-only node must not advertise a store hit outside its own origin"
    );
    anyhow::ensure!(
        foreign_resp_ext.total_bytes.is_none(),
        "a non-advertised hash must carry no total_bytes hint, got {:?}",
        foreign_resp_ext.total_bytes
    );

    let own_req = ProbeRequest {
        hash: *own_hash.as_bytes(),
        timestamp_us: 2,
    };
    let (own_resp, own_resp_ext) = run_one_probe(fresh_key(), handler, own_req).await?;
    anyhow::ensure!(
        own_resp.body.has_blob,
        "origin-only node must still advertise its own backend-held content"
    );
    anyhow::ensure!(
        own_resp_ext.total_bytes == Some(own_payload.len() as u64),
        "own content's total_bytes should report the backend size, got {:?}",
        own_resp_ext.total_bytes
    );

    Ok(())
}

/// A node with the eviction-hold path **disabled by config**
/// (`max_probe_holds == 0`) holds the blob but answers `has_blob: false` with a
/// valid `slash_sig` over `has_blob=false`. The reason is the operator opt-out,
/// NOT the missing guarantee — `BudgetExhausted` also places no hold and still
/// advertises. `max_probe_holds = 0` means "do not advertise store-backed
/// content at all" (ADR 005 §Hold budget).
/// Exercises the handler's `HoldsDisabled` arm end-to-end and asserts the
/// outcome is counted as a *disabled* event, NOT as budget pressure (#739).
#[tokio::test(flavor = "multi_thread")]
async fn probe_holds_disabled_signs_has_blob_false_and_counts_disabled() -> anyhow::Result<()> {
    let payload = b"present but un-holdable";
    let (cache, hash, _cache_tmp) = cache_with_blob(payload).await?;
    cache.set_max_probe_holds(0); // disable holds -> HoldsDisabled

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    let (handler, signer, domain) = build_handler(server_id, 7, &metrics, limiter, cache);

    let req = ProbeRequest {
        hash: *hash.as_bytes(),
        timestamp_us: 0x1234,
    };
    let (resp, resp_ext) = run_one_probe(server_sk, handler, req).await?;

    anyhow::ensure!(
        !resp.body.has_blob,
        "holds-disabled must yield has_blob=false even though the blob is cached"
    );
    anyhow::ensure!(
        resp_ext.total_bytes.is_none(),
        "no size advertised when has_blob=false, got {:?}",
        resp_ext.total_bytes
    );
    // The signature must cover has_blob=false (not a stale true).
    assert_slash_sig_valid(&resp, &signer, &domain)?;

    // An intentional disable must increment the disabled counter and leave
    // the budget-pressure counter at zero — otherwise the "increase
    // max_probe_holds" alert fires on a config the operator chose.
    let text = metrics.encode()?;
    anyhow::ensure!(
        metric_value(
            &text,
            "decdn_probe_hold_unavailable_total{reason=\"disabled\"}"
        ) == Some(1),
        "holds-disabled probe must bump the reason=disabled child of \
         decdn_probe_hold_unavailable_total:\n{text}"
    );
    anyhow::ensure!(
        metric_value(
            &text,
            "decdn_probe_hold_unavailable_total{reason=\"exhausted\"}"
        ) == Some(0),
        "an intentional disable must NOT inflate the budget-pressure counter:\n{text}"
    );
    Ok(())
}

/// A node with a positive but fully-occupied hold budget still advertises
/// `has_blob: true` (the hold is best-effort — presence, not a guaranteed hold,
/// governs the answer) but places no hold, and counts the event as genuine
/// budget pressure (`reason="exhausted"`), NOT as a config disable (#739). This
/// is the signal whose alert remedy is "increase `max_probe_holds`".
#[tokio::test(flavor = "multi_thread")]
async fn probe_budget_exhausted_still_advertises_and_counts_exhausted() -> anyhow::Result<()> {
    let a: &[u8] = b"first popular blob";
    let b: &[u8] = b"second popular blob";
    let (cache, ha, hb, _cache_tmp) = cache_with_two_blobs(a, b).await?;
    // One slot, two distinct cached blobs: the first hold fills the budget,
    // the second probe finds it exhausted (max > 0).
    cache.set_max_probe_holds(1);
    anyhow::ensure!(
        cache.try_probe_hold(ha).await? == decdn_cache::ProbeHoldOutcome::Held,
        "first hold should fit the budget"
    );
    // Kept so the test can prove the budget was respected, not just reported:
    // `has_blob` looks identical whether the second probe forwent the hold or
    // overran `max_probe_holds`.
    let cache_probe = cache.clone();

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    let (handler, signer, domain) = build_handler(server_id, 7, &metrics, limiter, cache);

    let req = ProbeRequest {
        hash: *hb.as_bytes(),
        timestamp_us: 0x55,
    };
    let (resp, resp_ext) = run_one_probe(server_sk, handler, req).await?;

    anyhow::ensure!(
        resp.body.has_blob,
        "budget-exhausted must still advertise has_blob=true — the hold is forgone, not the answer"
    );
    anyhow::ensure!(
        resp_ext.total_bytes == Some(b.len() as u64),
        "the advertised blob must carry its size, got {:?}",
        resp_ext.total_bytes
    );
    assert_slash_sig_valid(&resp, &signer, &domain)?;

    // The budget is a hard ceiling: the advertised-but-unheld blob must not
    // have taken a second slot. Only `ha` is held.
    anyhow::ensure!(
        cache_probe.probe_hold_slots_used() == 1,
        "budget-exhausted must forgo the hold, leaving max_probe_holds=1 slot \
         in use; found {}",
        cache_probe.probe_hold_slots_used()
    );

    let text = metrics.encode()?;
    anyhow::ensure!(
        metric_value(
            &text,
            "decdn_probe_hold_unavailable_total{reason=\"exhausted\"}"
        ) == Some(1),
        "genuine budget exhaustion must bump the reason=exhausted child of \
         decdn_probe_hold_unavailable_total:\n{text}"
    );
    anyhow::ensure!(
        metric_value(
            &text,
            "decdn_probe_hold_unavailable_total{reason=\"disabled\"}"
        ) == Some(0),
        "budget pressure must NOT be counted as a config disable:\n{text}"
    );
    Ok(())
}

/// With a stake-lane reservation configured (#757, ADR 003 §Admission and
/// Priority), a probe from a client that is NOT a registered operator still
/// advertises `has_blob: true` but places no eviction hold once hold usage
/// reaches the end-client ceiling — the reservation protects node-to-node hold
/// slots, it does not suppress an honest answer. Here `max_holds=1, reserved=1`
/// gives a ceiling of `0`, so the end-client is shed from the hold immediately
/// even though the blob is cached. The event is counted as a stake-lane
/// reservation — never as budget exhaustion (`reason="exhausted"`) or a config
/// disable (`reason="disabled"`), whose alerts have different remedies.
#[tokio::test(flavor = "multi_thread")]
async fn probe_end_client_reserved_out_still_advertises() -> anyhow::Result<()> {
    let payload = b"reserved-for-stake-lane content";
    let (cache, hash, _cache_tmp) = cache_with_blob(payload).await?;
    cache.set_max_probe_holds(1);
    // Kept so the test can inspect hold state after the probe. Asserting the
    // wire answer alone cannot distinguish "advertised, no hold" from
    // "advertised AND consumed a reserved slot" — the latter is exactly the
    // regression #757 exists to prevent, and it is invisible in `has_blob`.
    let cache_probe = cache.clone();

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    // Empty staker set => the fresh client key is an unregistered end-client.
    // reserved=1, max=1 => end-client ceiling is 0, so any end-client probe
    // under any hold usage (including zero) is reserved out.
    let (handler, signer, domain) = build_handler_with_lane(
        server_id,
        7,
        &metrics,
        limiter,
        cache,
        std::collections::HashSet::new(),
        NonZeroUsize::MIN,
        1,
    );

    let req = ProbeRequest {
        hash: *hash.as_bytes(),
        timestamp_us: 0x9001,
    };
    let (resp, resp_ext) = run_one_probe(server_sk, handler, req).await?;

    anyhow::ensure!(
        resp.body.has_blob,
        "an end-client under a stake-lane reservation must still get has_blob=true (hold shed, not the answer)"
    );
    anyhow::ensure!(
        resp_ext.total_bytes == Some(payload.len() as u64),
        "the advertised blob must carry its size, got {:?}",
        resp_ext.total_bytes
    );
    assert_slash_sig_valid(&resp, &signer, &domain)?;

    // The point of the reservation: the end-client got an honest answer but
    // consumed NO hold slot, leaving the whole budget for the stake lane.
    anyhow::ensure!(
        cache_probe.probe_hold_slots_used() == 0,
        "a reserved-out end-client must place no hold; {} slot(s) in use",
        cache_probe.probe_hold_slots_used()
    );

    let text = metrics.encode()?;
    anyhow::ensure!(
        metric_value(
            &text,
            "decdn_probe_hold_unavailable_total{reason=\"stake_lane_reserved\"}"
        ) == Some(1),
        "reservation refusal must bump the reason=stake_lane_reserved child of \
         decdn_probe_hold_unavailable_total:\n{text}"
    );
    anyhow::ensure!(
        metric_value(
            &text,
            "decdn_probe_hold_unavailable_total{reason=\"exhausted\"}"
        ) == Some(0),
        "a stake-lane reservation must NOT be counted as budget pressure:\n{text}"
    );
    anyhow::ensure!(
        metric_value(
            &text,
            "decdn_probe_hold_unavailable_total{reason=\"disabled\"}"
        ) == Some(0),
        "a stake-lane reservation must NOT be counted as a config disable:\n{text}"
    );
    Ok(())
}

/// Under the very same reservation that sheds an end-client, a probe from a
/// registered operator (its `NodeId` in the staker set) is admitted: it
/// passes the reservation gate, takes a hold, and signs `has_blob: true`
/// (#757). This is the headroom the reservation exists to protect for
/// node-to-node cache-miss probes.
#[tokio::test(flavor = "multi_thread")]
async fn probe_stake_lane_requester_keeps_reserved_headroom() -> anyhow::Result<()> {
    let payload = b"served to a node-to-node requester";
    let (cache, hash, _cache_tmp) = cache_with_blob(payload).await?;
    cache.set_max_probe_holds(1);
    // The mirror of the end-client assertion: a stake-lane requester must
    // actually CONSUME the slot the reservation held open for it.
    let cache_probe = cache.clone();

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let client_sk = fresh_key();
    let client_node_id = NodeId::from_bytes(*client_sk.public().as_bytes());
    let metrics = Arc::new(Metrics::new());
    let limiter = permissive_limiter(&metrics);
    // Same aggressive reservation (reserved=1, max=1) as the end-client test,
    // but this requester IS a registered operator.
    let mut stakers = std::collections::HashSet::new();
    stakers.insert(client_node_id);
    let (handler, signer, domain) = build_handler_with_lane(
        server_id,
        7,
        &metrics,
        limiter,
        cache,
        stakers,
        NonZeroUsize::MIN,
        1,
    );

    let req = ProbeRequest {
        hash: *hash.as_bytes(),
        timestamp_us: 0x9002,
    };
    let (resp, resp_ext) = run_one_probe_as(client_sk, server_sk, handler, req).await?;

    anyhow::ensure!(
        resp.body.has_blob,
        "a stake-lane requester must keep its reserved headroom -> has_blob=true"
    );
    anyhow::ensure!(
        resp_ext.total_bytes == Some(payload.len() as u64),
        "stake-lane requester should be served the blob size, got {:?}",
        resp_ext.total_bytes
    );
    assert_slash_sig_valid(&resp, &signer, &domain)?;

    // Unlike the shed end-client, this requester went through the real hold
    // path and took the reserved slot. Together the two tests pin the
    // reservation's actual behaviour, not just its counter.
    anyhow::ensure!(
        cache_probe.probe_hold_slots_used() == 1,
        "a stake-lane requester must consume its reserved hold slot; {} in use",
        cache_probe.probe_hold_slots_used()
    );

    let text = metrics.encode()?;
    anyhow::ensure!(
        metric_value(
            &text,
            "decdn_probe_hold_unavailable_total{reason=\"stake_lane_reserved\"}"
        ) == Some(0),
        "a stake-lane requester must NOT trip the reservation counter, and the \
         series must be present at zero rather than absent:\n{text}"
    );
    Ok(())
}

/// Verify that `QuicTransportConfig::max_idle_timeout` actually closes a
/// silent connection (the wiring `quic_transport_config` relies on).
/// The runtime value is 30s per ADR 005; we shorten it to 300ms here so
/// the test runs in well under a second. `keep_alive_interval` is parked
/// at 60s on both ends so the path stays silent across the idle window —
/// otherwise the keep-alive PINGs the runtime sends would refresh the
/// timer and the test could never observe the close.
#[tokio::test(flavor = "multi_thread")]
async fn idle_timeout_closes_quiet_connection() -> anyhow::Result<()> {
    let idle = Duration::from_millis(300);
    let build_cfg = || -> anyhow::Result<QuicTransportConfig> {
        let it: IdleTimeout = idle
            .try_into()
            .map_err(|e| anyhow::anyhow!("idle timeout: {e}"))?;
        Ok(QuicTransportConfig::builder()
            .max_idle_timeout(Some(it))
            .keep_alive_interval(Duration::from_mins(1))
            .max_concurrent_bidi_streams(VarInt::from_u32(100))
            .build())
    };

    let server_sk = fresh_key();
    let server_id = server_sk.public();
    let server_bind = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
    let server_ep = Endpoint::builder(presets::Minimal)
        .secret_key(server_sk)
        .transport_config(build_cfg()?)
        .alpns(vec![ALPN_PROBE.to_vec()])
        .relay_mode(RelayMode::Disabled)
        .bind_addr(server_bind)
        .map_err(|e| anyhow::anyhow!("server bind_addr: {e}"))?
        .bind()
        .await
        .map_err(|e| anyhow::anyhow!("server bind: {e}"))?;
    let server_addr = server_ep
        .bound_sockets()
        .into_iter()
        .find(SocketAddr::is_ipv4)
        .ok_or_else(|| anyhow::anyhow!("no IPv4 bound socket"))?;
    let server_addr = match server_addr {
        SocketAddr::V4(v4) if v4.ip().is_unspecified() => {
            SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, v4.port()))
        }
        other => other,
    };

    // Server task: accept the connection but don't drive any application
    // handler. We're testing transport-level idle close, not protocol
    // behaviour — the only thing that should close the connection is the
    // idle timer.
    let server_ep_bg = server_ep.clone();
    let accept_task = tokio::spawn(async move {
        if let Some(incoming) = server_ep_bg.accept().await {
            let connecting = incoming
                .accept()
                .map_err(|e| anyhow::anyhow!("accept: {e}"))?;
            let conn = connecting
                .await
                .map_err(|e| anyhow::anyhow!("handshake: {e}"))?;
            let _ = conn.closed().await;
        }
        Ok::<_, anyhow::Error>(())
    });

    let client_bind = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0);
    let client_ep = Endpoint::builder(presets::Minimal)
        .secret_key(fresh_key())
        .transport_config(build_cfg()?)
        .relay_mode(RelayMode::Disabled)
        .bind_addr(client_bind)
        .map_err(|e| anyhow::anyhow!("client bind_addr: {e}"))?
        .bind()
        .await
        .map_err(|e| anyhow::anyhow!("client bind: {e}"))?;

    let target = EndpointAddr::new(server_id).with_ip_addr(server_addr);
    let client_conn = client_ep
        .connect(target, ALPN_PROBE)
        .await
        .map_err(|e| anyhow::anyhow!("connect: {e}"))?;

    // Wait for the idle timer to fire. The bound is generous compared to
    // `idle` so a slow CI doesn't flake the test, but tight enough that a
    // wiring regression (no `transport_config(...)` call, default 30s
    // timeout) fails fast instead of stalling for the full default.
    let close_err = tokio::time::timeout(Duration::from_secs(3), client_conn.closed())
        .await
        .map_err(|_| {
            anyhow::anyhow!("connection did not idle-close within 3s (idle window: {idle:?})")
        })?;

    match close_err {
        ConnectionError::TimedOut => {}
        other => anyhow::bail!("expected ConnectionError::TimedOut, got {other:?}"),
    }

    shutdown([], [&client_ep]).await?;
    support::reap("accept", accept_task).await??;
    shutdown([], [&server_ep]).await?;
    Ok(())
}