ant-node 0.23.1

Pure quantum-proof network node for the Autonomi decentralized network
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
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
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
//! Payment verifier with LRU cache and EVM verification.
//!
//! This is the core payment verification logic for ant-node.
//! All new data requires EVM payment on Arbitrum (no free tier).

use crate::ant_protocol::CLOSE_GROUP_SIZE;
use crate::error::{Error, Result};
use crate::logging::{debug, info, warn};
use crate::payment::cache::{CacheStats, PaidKey, VerifiedCache, XorName};
use crate::payment::pricing::{calculate_price, derive_records_stored_from_price};
use crate::payment::proof::{
    deserialize_merkle_proof, deserialize_single_node_proof, detect_proof_type, ProofType,
};
use crate::replication::commitment::MAX_COMMITMENT_KEY_COUNT;
use crate::replication::config::K_BUCKET_SIZE;
use crate::storage::ChunkStore;
use ant_protocol::payment::verify::{verify_quote_content, verify_quote_signature};
use evmlib::common::{Amount, QuoteHash};
use evmlib::contract::payment_vault;
use evmlib::merkle_batch_payment::{OnChainPaymentInfo, PoolHash};
use evmlib::Network as EvmNetwork;
use evmlib::PaymentQuote;
use evmlib::ProofOfPayment;
use evmlib::RewardsAddress;
use lru::LruCache;
use parking_lot::{Mutex, RwLock};
use saorsa_core::identity::node_identity::peer_id_from_public_key_bytes;
use saorsa_core::identity::PeerId;
use saorsa_core::P2PNode;
use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::sync::Arc;
use std::time::Instant;

/// Minimum allowed size for a payment proof in bytes.
///
/// This minimum ensures the proof contains at least a basic cryptographic hash or identifier.
/// Proofs smaller than this are rejected as they cannot contain sufficient payment information.
pub const MIN_PAYMENT_PROOF_SIZE_BYTES: usize = 32;

/// Maximum allowed size for a payment proof in bytes (512 KB).
///
/// Single-node proofs with 7 ML-DSA-65 quotes reach ~40 KB; with ADR-0004
/// commitment sidecars (one ~5.3 KB commitment per bound quote, ~13 KB each
/// after rmp encoding) they reach ~150 KB.
/// Merkle proofs include 16 candidate nodes (each with ~1,952-byte ML-DSA pub
/// key and ~3,309-byte signature) plus merkle branch hashes, totaling ~130 KB.
/// A merkle proof that ALSO ships all 16 commitment sidecars (clients built
/// before the sidecars were dropped from the per-chunk bundle) reaches
/// ~342 KB — the previous 256 KB cap rejected every such PUT the moment nodes
/// carried live commitments, which is exactly how it was discovered.
/// 512 KB accepts every shape above with headroom while still capping memory
/// during verification.
pub const MAX_PAYMENT_PROOF_SIZE_BYTES: usize = 524_288;

const PAID_QUOTE_PAYMENT_MULTIPLIER: u64 = 3;
const PAYMENT_VERIFY_SLOW_LOG_MS: u128 = 500;

/// Number of nearest DHT peers accepted for paid-quote issuer locality.
///
/// This is the Kademlia K width, intentionally wider than `CLOSE_GROUP_SIZE`.
const PAID_QUOTE_ISSUER_CLOSENESS_WIDTH: usize = K_BUCKET_SIZE;

/// Default tolerance for the receiver-side price floor, as a percentage of the
/// close group's MEDIAN commitment-bound price (see
/// [`PaymentVerifier::group_reference_price`]).
///
/// Derived from production, not chosen: on ant-prod-01 over 2026-07-29..08-04,
/// pricing each receiver against its OWN price rejected 0.377% of honest stores
/// while still admitting the cheapest-of-K underpayment at 62.9% of receivers,
/// because node prices span ~4x across the fleet and an honest client pays the
/// group median. Against the group median instead, honest payments land near the
/// reference and the underpayment lands well below it, so a tolerance separates
/// them.
///
/// At the minimum permitted view, 65% rejects 0.140% of honest payments while
/// catching ~90% of underpayment, against the own-price floor's measured 0.377%
/// and 37.1%. Both axes improve, by roughly 2.7x on rejections rather than the
/// order of magnitude an earlier revision claimed. 60% is the conservative
/// alternative: 5x safer on honest traffic, at 66% detection.
///
/// The full sweep, the sample-size table and the simulation's assumptions live
/// in ADR-0006 — deliberately in one place, because the same tables
/// duplicated here and in the ADR had already drifted apart once. Tighten or
/// loosen only from observed shadow telemetry, never speculatively.
const PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT: u64 = 65;

/// Minimum number of fresh NEIGHBOUR commitments (this node's own is counted
/// separately) required before the price floor will evaluate at all.
///
/// Below this the local view is too thin for a median to mean anything (fresh
/// start, post-churn, a gossip cache that has not filled), so the floor SKIPS
/// rather than guessing. Skipping is the safe direction: the cost is a missed
/// underpayment, whereas guessing from a handful of peers rejects honest stores
/// whose payment already settled on-chain and cannot be refunded.
///
/// The value is not a round number chosen by feel. A median over a small sample
/// of a distribution this wide is unstable, and the floor is *more* dangerous
/// than the own-price reference it replaces until the sample is large: at 3
/// neighbours it rejects 2.0% of honest stores, at 7 it rejects 0.6%, and only
/// past about 11 does it beat the 0.377% the own-price floor measured in
/// production. The gate therefore sits at 15 of the
/// [`PAID_QUOTE_ISSUER_CLOSENESS_WIDTH`] closest, with the full table in
/// ADR-0006.
///
/// Availability cost is expected to be small — the gossip TTL is hours against a
/// much shorter sweep, so a settled node should normally know most of its
/// neighbours — but that is an assumption, not a measurement, and the sample is
/// drawn from peers close to the CHUNK while the cache is filled from peers
/// close to THIS NODE, so edge-of-range chunks will skip more often. The
/// `skipped=true` / `skip_reason` telemetry exists to measure that in shadow
/// mode before anyone enforces.
///
/// A node that could suppress its neighbours' gossip can therefore disable this
/// receiver's floor, but cannot make it reject honest traffic. That asymmetry is
/// deliberate.
const PRICE_FLOOR_MIN_NEIGHBOUR_COMMITMENTS: usize = 15;

/// Why a price-floor evaluation did not produce a reference price.
///
/// Shadow mode exists to calibrate
/// [`PRICE_FLOOR_MIN_NEIGHBOUR_COMMITMENTS`] from real deployments, and these
/// three causes call for completely different responses: a mid-rotation node is
/// expected and self-correcting, an unwired cache is a startup-order bug, and a
/// persistently thin sample means the gate is set above what the network can
/// supply. Collapsing them into one counter would make the telemetry unable to
/// answer the only question it is there to answer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SkipReason {
    /// The gossip commitment cache is not attached.
    NoCommitmentCache,
    /// No local routing view of the close group (no P2P handle attached).
    NoRoutingView,
    /// This node has no live commitment of its own.
    NoOwnCommitment,
    /// Fewer fresh neighbour commitments than the gate requires.
    ThinSample,
}

impl SkipReason {
    /// Only ever called from the price-floor telemetry, which compiles to
    /// nothing without the `logging` feature.
    #[cfg_attr(not(feature = "logging"), allow(dead_code))]
    const fn as_str(self) -> &'static str {
        match self {
            Self::NoCommitmentCache => "no_commitment_cache",
            Self::NoRoutingView => "no_routing_view",
            Self::NoOwnCommitment => "no_own_commitment",
            Self::ThinSample => "thin_sample",
        }
    }
}

/// What the price floor could see of the close group on one admission.
///
/// `neighbours` is the quantity the gate actually tests; `total` includes this
/// node's own commitment and is what the median is taken over. Both are logged
/// because reporting only the total makes `group_sample=15, skipped=true` look
/// like a contradiction when it is simply 14 neighbours plus self.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct GroupSample {
    neighbours: usize,
    total: usize,
    skip_reason: Option<SkipReason>,
}

impl GroupSample {
    const fn none(reason: SkipReason) -> Self {
        Self {
            neighbours: 0,
            total: 0,
            skip_reason: Some(reason),
        }
    }

    const fn skipped(self, reason: SkipReason) -> Self {
        Self {
            skip_reason: Some(reason),
            ..self
        }
    }

    /// Only ever called from the price-floor telemetry, which compiles to
    /// nothing without the `logging` feature.
    #[cfg_attr(not(feature = "logging"), allow(dead_code))]
    fn skip_reason_str(self) -> &'static str {
        self.skip_reason.map_or("none", SkipReason::as_str)
    }
}

/// Environment variable enabling price-floor ENFORCEMENT (`1`/`true`).
///
/// Absent or any other value means shadow mode: the floor is computed and
/// logged, never enforced. Per-node so enforcement can roll out canary-first;
/// unsetting it (and restarting) is the kill switch.
pub const PRICE_FLOOR_ENFORCE_ENV: &str = "ANT_PRICE_FLOOR_ENFORCE";

/// Environment variable overriding the price-floor tolerance percentage
/// (`0..=100`). Invalid or absent values use the default.
pub const PRICE_FLOOR_TOLERANCE_ENV: &str = "ANT_PRICE_FLOOR_TOLERANCE_PERCENT";

/// Receiver-side price floor for single-node store admissions.
///
/// ADR-0004 gives every quote a price CEILING (`price ==
/// calculate_price(committed_key_count)`), but a ceiling is not a revenue
/// floor: a modified client can fetch quotes from the whole neighbourhood and
/// settle only the cheapest valid one in a 1-quote proof. This policy is the
/// floor half: the settled amount must also clear the close group's MEDIAN
/// commitment-bound price, scaled by a tolerance.
///
/// The reference is the group's median rather than this receiver's own price.
/// A client pays `3x` the median of the quotes it collected, so comparing that
/// to a single node's price compares a median to one draw from a distribution
/// spanning ~4x — which in production rejected honest stores on the fullest
/// nodes while admitting underpayment at the emptiest. The reference is built
/// only from this node's own commitment and its neighbours' TTL-fresh gossiped
/// ones, never from the payment bundle.
///
/// Rollout is shadow-first: with `enforce == false` (the default) the floor
/// is evaluated and logged on every single-node store admission but never
/// rejects. Enforcement is enabled per node via [`PRICE_FLOOR_ENFORCE_ENV`]
/// for canary rollout. A floor rejection is an economic admission decision
/// only — it must never feed trust/misbehaviour scoring.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PriceFloorConfig {
    /// When true, a below-floor settled payment is rejected. When false
    /// (default), the verifier only logs would-reject telemetry.
    pub enforce: bool,
    /// Floor as a percentage of the close group's median commitment-bound
    /// price (`0..=100`).
    pub tolerance_percent: u64,
}

impl Default for PriceFloorConfig {
    fn default() -> Self {
        Self {
            enforce: false,
            tolerance_percent: PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT,
        }
    }
}

impl PriceFloorConfig {
    /// Build from the environment (see [`PRICE_FLOOR_ENFORCE_ENV`] and
    /// [`PRICE_FLOOR_TOLERANCE_ENV`]). Never panics.
    ///
    /// An unset tolerance uses the default. A tolerance that is *present but
    /// invalid* (unparseable or `> 100`) is an operator error: rather than
    /// silently enforce at the default, this **fails closed to shadow mode**
    /// (`enforce = false`) and logs a prominent error, so a fat-fingered
    /// tolerance can never enforce an unintended floor against real payments.
    #[must_use]
    pub fn from_env() -> Self {
        let enforce_requested = std::env::var(PRICE_FLOOR_ENFORCE_ENV)
            .is_ok_and(|v| matches!(v.trim(), "1" | "true" | "TRUE" | "True"));

        let tolerance_raw = std::env::var(PRICE_FLOOR_TOLERANCE_ENV).ok();
        let valid_tolerance = tolerance_raw
            .as_deref()
            .map(str::trim)
            .and_then(|s| s.parse::<u64>().ok())
            .filter(|percent| *percent <= 100);
        // A tolerance that is set but does not parse to `0..=100` is an operator
        // error. Fail CLOSED: never enforce a tolerance the operator did not
        // actually specify.
        let tolerance_present_but_invalid = tolerance_raw.is_some() && valid_tolerance.is_none();

        if tolerance_present_but_invalid {
            if enforce_requested {
                crate::logging::error!(
                    "{PRICE_FLOOR_TOLERANCE_ENV}={tolerance_raw:?} is not an integer in 0..=100; \
                     refusing to enforce the price floor with an unspecified tolerance. \
                     Price-floor enforcement DISABLED (shadow mode). Fix the value and restart \
                     to enable enforcement."
                );
            } else {
                crate::logging::warn!(
                    "{PRICE_FLOOR_TOLERANCE_ENV}={tolerance_raw:?} is not an integer in 0..=100; \
                     using the default {PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT}% for shadow-mode \
                     telemetry."
                );
            }
        }

        Self {
            enforce: enforce_requested && !tolerance_present_but_invalid,
            tolerance_percent: valid_tolerance.unwrap_or(PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT),
        }
    }
}

#[derive(Clone, Copy)]
struct LegacyMedianCandidate<'a> {
    encoded_peer_id: &'a evmlib::EncodedPeerId,
    quote: &'a PaymentQuote,
    expected_amount: Amount,
}

fn median_quote_index(quote_count: usize) -> usize {
    quote_count / 2
}

/// The settlement multiplier a merkle receipt must satisfy, from the instant it
/// was stamped (ADR-0008).
///
/// `PAID_QUOTE_PAYMENT_MULTIPLIER` (3x, matching the single-node path) for a
/// receipt stamped at or after `enforced_from`; the historic `1` for one
/// stamped before it, which was bought under the previous rule and cannot be
/// refunded.
///
/// The boundary needs no sunset logic because receipt expiry supplies it: a
/// receipt older than one week is refused regardless, so once
/// `enforced_from + one week` has passed no valid receipt can still reach the
/// `1` branch.
///
/// Expiry bounds backdating rather than preventing it. The stamp is
/// client-chosen and quoting nodes sign what they are asked for, so during the
/// week after `enforced_from` a modified client can stamp just before the
/// boundary and still settle at 1x — the same route honest in-flight receipts
/// take. It closes when such stamps expire, at `enforced_from + one week`, and
/// it cannot be narrowed without also refusing receipts bought in good faith
/// under the previous rule.
fn merkle_required_multiplier(receipt_timestamp: u64, enforced_from: u64) -> u64 {
    if receipt_timestamp >= enforced_from {
        PAID_QUOTE_PAYMENT_MULTIPLIER
    } else {
        1
    }
}

/// Per-node payment implied by the merkle contract formula, for a given
/// payment multiplier.
///
/// The contract settles `total = median16(amount) x 2^depth` and splits it
/// evenly across the `depth` nodes it paid, so
/// `per_node = multiplier x median16(price) x 2^depth / depth`.
///
/// `candidate_prices` are the candidates' **signed quoted** prices as they
/// appear in the proof — always 1x, because the multiplier is applied by the
/// client to the on-chain payable `amount` and never to the signed quote
/// (ADR-0008). Passing `multiplier = 1` therefore reproduces the historic
/// merkle expectation, and `PAID_QUOTE_PAYMENT_MULTIPLIER` the single-node
/// parity expectation.
///
/// `median16` is the upper median (index `len / 2`), matching Solidity's
/// `median16` with `k = 8`. Depth `0` yields zero: there is nothing to pay.
///
/// **Not linear in `multiplier`.** The trailing `/ depth` is integer division,
/// so `expected(m)` is `floor(m x median x 2^depth / depth)`, which is NOT
/// generally `m x expected(1)` — the two differ by up to `m - 1` wei whenever
/// `depth` does not divide `median x 2^depth`. For example median 901 at depth
/// 7 gives `expected(3) = 49426` but `3 x expected(1) = 49425`. Callers must
/// therefore ask for the multiplier they mean and compare against that value;
/// scaling a 1x result is wrong. The order here — multiply the total, then
/// divide once — is deliberate, and matches the contract, which computes
/// `totalAmount` before splitting it.
/// Does this underpayment look like a client that applied no multiplier at
/// all, as opposed to one that merely paid too little?
///
/// Compares against the **bare** expectation rather than scaling the paid
/// amount up by the required multiplier. [`merkle_expected_per_node`] floors
/// after multiplying, so `expected(m)` is not generally `m * expected(1)`: at
/// median 901 and depth 7 they differ by one wei. A scaled comparison
/// therefore misses genuinely unmultiplied settlements at every depth that
/// does not divide its leaf count evenly, and those are exactly the clients
/// the upgrade advice exists for.
fn underpayment_looks_like_a_stale_client(
    paid: Amount,
    expected_bare: Amount,
    required_multiplier: u64,
) -> bool {
    required_multiplier > 1 && paid == expected_bare
}

fn merkle_expected_per_node(
    candidate_prices: &[Amount],
    depth: u8,
    multiplier: u64,
) -> Result<Amount> {
    if depth == 0 {
        return Ok(Amount::ZERO);
    }

    let mut sorted = candidate_prices.to_vec();
    sorted.sort_unstable(); // ascending
    let median_price = *sorted
        .get(sorted.len() / 2)
        .ok_or_else(|| Error::Payment("empty candidate pool in merkle proof".into()))?;

    let leaves = 1u64
        .checked_shl(u32::from(depth))
        .ok_or_else(|| Error::Payment("merkle proof depth too large".into()))?;

    let total_amount = median_price
        .checked_mul(Amount::from(leaves))
        .and_then(|total| total.checked_mul(Amount::from(multiplier)))
        .ok_or_else(|| Error::Payment("merkle total payment overflow".into()))?;

    Ok(total_amount / Amount::from(u64::from(depth)))
}

fn payment_proof_type_label(payment_proof: Option<&[u8]>) -> &'static str {
    match payment_proof.and_then(detect_proof_type) {
        Some(ProofType::Merkle) => "merkle",
        Some(ProofType::SingleNode) => "single_node",
        Some(_) => "unsupported",
        None if payment_proof.is_some() => "unknown",
        None => "none",
    }
}

/// Configuration for EVM payment verification.
///
/// EVM verification is always on. All new data requires on-chain
/// payment verification. The network field selects which EVM chain to use.
#[derive(Debug, Clone)]
pub struct EvmVerifierConfig {
    /// EVM network to use (Arbitrum One, Arbitrum Sepolia, etc.)
    pub network: EvmNetwork,
}

impl Default for EvmVerifierConfig {
    fn default() -> Self {
        Self {
            network: EvmNetwork::ArbitrumOne,
        }
    }
}

/// Configuration for the payment verifier.
///
/// All new data requires EVM payment on Arbitrum. The cache stores
/// previously verified payments to avoid redundant on-chain lookups.
#[derive(Debug, Clone)]
pub struct PaymentVerifierConfig {
    /// EVM verifier configuration.
    pub evm: EvmVerifierConfig,
    /// Cache capacity (number of `XorName` values to cache).
    pub cache_capacity: usize,
    /// Close-group width exposed to storage and replication admission callers.
    pub close_group_size: usize,
    /// Local node's rewards address.
    ///
    /// Kept in the verifier config for payment policies that bind receipts to
    /// this node's payout address.
    pub local_rewards_address: RewardsAddress,
    /// Receiver-side price floor policy (shadow mode by default).
    pub price_floor: PriceFloorConfig,
}

/// What a payment authorizes, and where it routes.
///
/// One typed value doing both jobs. A chunk pays for its own address; a pointer
/// pays for a *state* while the close group that may quote it is the one around
/// its address — two different addresses with two different meanings.
///
/// This is also the paid-cache key, and being an enum is what makes that safe:
/// a raw 32-byte key would file both kinds under one value, so anything that
/// put a chunk on a pointer's identifier would buy the pointer's update at
/// chunk price. `state_id` is a `derive_key` output, so putting a chunk on one
/// is a preimage problem rather than a string anyone can write down — and
/// distinct variants cannot collide however the bytes are chosen, so the cache
/// does not rest on that assumption either.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum PaymentTarget {
    /// A chunk: paid for and stored at one address.
    Chunk(XorName),
    /// A pointer state: routed at the pointer's address, paid at its `state_id`.
    Pointer {
        /// The pointer's address, stable for its life. Selects the close group.
        routing: XorName,
        /// The state paid for, which changes with every update.
        state_id: XorName,
    },
}

impl PaymentTarget {
    /// A chunk, where one address does both jobs.
    #[must_use]
    pub const fn same(address: XorName) -> Self {
        Self::Chunk(address)
    }

    /// A pointer, routed at one address and paid at another.
    #[must_use]
    pub const fn split(routing: XorName, state_id: XorName) -> Self {
        Self::Pointer { routing, state_id }
    }

    /// The address whose close group is responsible, and whose members' quotes
    /// therefore count.
    #[must_use]
    pub const fn routing(&self) -> &XorName {
        match self {
            Self::Chunk(address) => address,
            Self::Pointer { routing, .. } => routing,
        }
    }

    /// What the quote must actually name.
    #[must_use]
    pub const fn content(&self) -> &XorName {
        match self {
            Self::Chunk(address) => address,
            Self::Pointer { state_id, .. } => state_id,
        }
    }
}

/// The fresh admission path a payment proof is being verified for.
///
/// - **`ClientPut`** — the node is admitting a chunk store from a direct
///   client PUT. The verifier applies store-strength cache semantics and live
///   payment checks.
/// - **`FreshReplication`** — the node is admitting a chunk store via the
///   immediate fresh-write fan-out. The receiver is about to store the newly
///   written chunk as if the client PUT it there directly, so this context is
///   verified EXACTLY like `ClientPut` (store-strength cache semantics, same
///   live checks, same price-floor policy). It exists as a separate variant so
///   price-floor telemetry can distinguish direct ingress from fan-out — the
///   two paths can legitimately diverge during commitment rotation, and the
///   floor policy for fan-out must be tunable from observed data without
///   touching direct-PUT behaviour.
/// - **`PaidListAdmission`** — the node is admitting fresh paid-list metadata.
///   It runs the same live payment checks, but writes a weaker cache entry
///   that does not authorize future chunk stores. The price floor never
///   applies here: paid-list records reprice no fresh economic decision.
///
/// The caller must check local receiver/admission membership before invoking
/// the verifier for replication admission: fresh chunk replication requires
/// local close-group responsibility, and fresh paid-list replication requires
/// local paid-list close-group membership. Direct client PUT deliberately does
/// not perform a receiver-responsibility gate. The verifier itself only checks
/// payment proof validity and that the paid quote's issuer is in the K closest
/// peers for the quoted chunk address.
///
/// Later neighbour-sync repair does not include proof-of-payment bytes and
/// does not call this verifier. It authorizes repair from network evidence:
/// majority storage among the configured close group, or majority paid-list
/// membership among the closest K.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VerificationContext {
    /// The node is admitting a chunk store from a direct client PUT, with
    /// store-strength cache semantics.
    ClientPut,
    /// The node is admitting a chunk store via immediate fresh replication —
    /// verified identically to `ClientPut`, split out for price-floor
    /// telemetry and policy.
    FreshReplication,
    /// The node is admitting fresh paid-list metadata with paid-list-strength
    /// cache semantics.
    PaidListAdmission,
}

impl VerificationContext {
    /// True for contexts that admit chunk BYTES with store-strength cache
    /// semantics: direct client PUT and immediate fresh replication. These two
    /// are verified identically everywhere — the only policy that reads the
    /// variant itself (rather than this predicate) is price-floor telemetry.
    #[must_use]
    pub fn is_store_admission(self) -> bool {
        matches!(self, Self::ClientPut | Self::FreshReplication)
    }
}

/// Status returned by payment verification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaymentStatus {
    /// Data was found in local cache - previously paid.
    CachedAsVerified,
    /// New data - payment required.
    PaymentRequired,
    /// Payment was provided and verified.
    PaymentVerified,
}

/// Outcome of the ADR-0004 quote-vs-commitment cross-check (see
/// [`PaymentVerifier::cross_check_binding`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CrossCheck {
    /// Pin resolves to the commitment and the counts agree: nothing to report.
    Match,
    /// Pin resolves to the commitment but the claimed and committed counts
    /// disagree: deterministic, first-occurrence contradiction (evidence).
    Mismatch {
        /// The key count the quote claimed.
        quoted_key_count: u32,
        /// The key count the pinned commitment actually attests.
        committed_key_count: u32,
    },
    /// The supplied commitment does not hash to the quote's pin: the pin is
    /// unresolved (treat as fetch/skip), never evidence.
    PinDoesNotResolve,
}

impl PaymentStatus {
    /// Returns true if the data can be stored (cached or payment verified).
    #[must_use]
    pub fn can_store(&self) -> bool {
        matches!(self, Self::CachedAsVerified | Self::PaymentVerified)
    }

    /// Returns true if this status indicates the data was already paid for.
    #[must_use]
    pub fn is_cached(&self) -> bool {
        matches!(self, Self::CachedAsVerified)
    }
}

/// Default capacity for the merkle pool cache (number of pool hashes to cache).
const DEFAULT_POOL_CACHE_CAPACITY: usize = 1_000;

/// Capacity of the ADR-0008 parity-telemetry first-emission cache: how many
/// recently measured pool hashes this node remembers in order to emit one
/// sample per settlement instead of one per admitted chunk.
///
/// Bounded, and deliberately its own cache rather than a flag on the pool
/// cache: the pool cache is populated as soon as the on-chain record is read,
/// which happens *before* the paid indices, addresses and amounts are checked,
/// so keying first-emission off it would let a proof that is rejected moments
/// later suppress (or produce) a sample. This one is written only after full
/// admission validation.
///
/// LRU eviction means a pool that goes quiet for more than
/// `MERKLE_PARITY_TELEMETRY_CACHE_CAPACITY` distinct pools can be sampled a
/// second time. That is the intended trade: the cache bounds memory, and the
/// stream stays deduplicable by `pool` downstream.
const MERKLE_PARITY_TELEMETRY_CACHE_CAPACITY: usize = 1_000;

/// ADR-0004: max commitment sidecars processed per bundle. A legitimate bundle
/// carries at most one commitment per quote/candidate — `CANDIDATES_PER_POOL`
/// (16) is the larger of the single-node (`CLOSE_GROUP_SIZE` = 7) and merkle
/// cases, so it covers both. Excess sidecars from a malicious client are
/// ignored before any deserialize/verify work (bounds the hot-path cost).
const MAX_SIDECARS_PER_BUNDLE: usize = evmlib::merkle_batch_payment::CANDIDATES_PER_POOL;

/// Shared handle to the replication engine's gossip commitment cache
/// (`last_commitment_by_peer`), used by the ADR-0004 cross-check to resolve a
/// quote's pin against a neighbour's recently gossiped commitment. A `tokio`
/// `RwLock` to match the engine's; read with `.await` on the async path.
type CommitmentCache = Arc<
    tokio::sync::RwLock<
        HashMap<PeerId, crate::replication::commitment_state::PeerCommitmentRecord>,
    >,
>;

/// Per-`(peer, pin)` negative cache for unresolved ADR-0004 pin fetches: a pin a
/// peer answered `NotRetained` (or that timed out) is remembered so repeated
/// bundles don't re-fetch it. Behind an `Arc` so the detached fetch task owns a
/// handle without borrowing the verifier.
type PinFetchNegativeCache = Arc<Mutex<LruCache<(PeerId, [u8; 32]), ()>>>;

/// Test-override shape for one on-chain settlement: the settled amount and
/// the recorded `bytes16` rewards-address prefix (`None` = matches the quote).
#[cfg(any(test, feature = "test-utils"))]
type TestSettlementOverride = (Amount, Option<[u8; 16]>);

/// Main payment verifier for ant-node.
///
/// Uses:
/// 1. LRU cache for fast lookups of previously verified `XorName` values
/// 2. EVM payment verification for new data (always required)
/// 3. Pool-level cache for merkle batch payments (avoids repeated on-chain queries)
pub struct PaymentVerifier {
    /// LRU cache of verified `XorName` values.
    cache: VerifiedCache,
    /// LRU cache of verified merkle pool hashes → on-chain payment info.
    pool_cache: Mutex<LruCache<PoolHash, OnChainPaymentInfo>>,
    /// LRU cache of pool hashes whose candidate closeness has already been
    /// verified by this node. Collapses the per-chunk Kademlia lookup cost
    /// within a batch (256 chunks × 1 pool = 1 lookup instead of 256).
    closeness_pass_cache: Mutex<LruCache<PoolHash, ()>>,
    /// In-flight closeness lookups, keyed by pool hash. Lets concurrent PUTs
    /// for the same pool coalesce onto a single Kademlia lookup AND share
    /// its result — on both success and failure — which bounds `DoS`
    /// amplification to one lookup per unique `pool_hash` regardless of
    /// concurrency.
    inflight_closeness: Mutex<LruCache<PoolHash, Arc<ClosenessSlot>>>,
    /// P2P node handle, attached post-construction so paid-quote verification
    /// can check paid-quote issuer K-closeness, and merkle verification can
    /// check that candidate `pub_keys` map to peers actually close to the pool
    /// midpoint in the live DHT. `None` in unit tests that don't exercise
    /// live-DHT checks; production startup MUST call [`attach_p2p_node`].
    p2p_node: RwLock<Option<Arc<P2PNode>>>,
    /// Chunk store handle, attached post-construction. Retained for
    /// store-backed verifier checks that need the authoritative on-disk record
    /// count without depending on a side counter that may drift from
    /// replication/repair/prune paths. NOTE: the ADR-0006 price floor does NOT
    /// read this — it is bound to the live storage commitment via
    /// [`Self::local_commitment_source`], not the on-disk count (the old floor
    /// compared unlike counts and false-rejected honest quotes). `None` in unit
    /// tests that don't exercise store-backed checks; production wires it via
    /// [`Self::attach_storage`].
    storage: RwLock<Option<Arc<ChunkStore>>>,
    /// Test-only override for the paid-quote issuer K-closest check.
    ///
    /// Production code derives closest peers from the attached [`P2PNode`].
    #[cfg(any(test, feature = "test-utils"))]
    test_paid_quote_k_closest_override: RwLock<Option<Vec<[u8; 32]>>>,
    /// Test-only override for `completedPayments(quote_hash)`.
    ///
    /// Production always queries the payment vault; unit tests use this to
    /// exercise the full verifier path without starting an EVM chain. Maps a
    /// quote hash to the settled amount plus the recorded rewards-address
    /// prefix (`None` means "matches the quote", the honest-settlement
    /// default). The prefix is 16 bytes because the vault packs the address
    /// as `bytes16` in the `completedPayments` slot.
    #[cfg(any(test, feature = "test-utils"))]
    test_completed_payments_override: RwLock<HashMap<QuoteHash, TestSettlementOverride>>,
    // NOTE: the test-only own-peer-id override was removed with the ADR-retired
    // quote-freshness/staleness gate (ADR-0004 binds price to the committed
    // count instead), so it no longer has any reader.
    /// ADR-0004 gossip commitment cache, shared with the replication engine
    /// (`last_commitment_by_peer`). The cross-check resolves a quote's
    /// `commitment_pin` against the neighbour's most recently gossiped
    /// commitment held here, *only if seen within the answerability TTL*;
    /// otherwise the pin is treated as unknown (fetch/skip), never a penalty.
    /// A `tokio` `RwLock` to match the engine's; read with `.await` on the
    /// async verification path. `None` until [`Self::attach_commitment_cache`]
    /// (unit tests, or pre-replication startup).
    commitment_cache: RwLock<Option<CommitmentCache>>,
    /// ADR-0004 negative cache for unresolved pin fetches: a `(peer, pin)` that
    /// resolved to `NotRetained` or timed out is remembered here so repeated
    /// bundles citing the same unknown pin don't re-fetch (bounding the
    /// amplification an attacker can drive). Keyed by `(PeerId, pin)`. Behind an
    /// `Arc` so the detached background fetch task (which runs off the payment
    /// hot path) can read and update it without borrowing the verifier.
    pin_fetch_negative_cache: PinFetchNegativeCache,
    /// ADR-0004: sender to surface monetized pins (commitments that backed a
    /// payment) to the replication engine's deterministic first-audit drainer.
    /// `None` until [`Self::attach_monetized_pin_sender`] (unit tests, or
    /// pre-replication startup), in which case no first audit is scheduled.
    monetized_pin_tx:
        RwLock<Option<tokio::sync::mpsc::Sender<crate::replication::MonetizedPinEvent>>>,
    /// Price-floor input: the SAME live commitment source the local
    /// `QuoteGenerator` prices from, read via the non-mutating snapshot so the
    /// floor never extends commitment answerability. `None` until
    /// [`Self::attach_local_commitment_source`] (unit tests, or storage
    /// disabled / pre-replication startup) — the floor then SKIPS the admission
    /// entirely rather than pricing against anything, so a node in that state
    /// never rejects.
    local_commitment_source: RwLock<Option<Arc<dyn crate::payment::quote::CommitmentSource>>>,
    /// Live price-floor policy, initialized from
    /// [`PaymentVerifierConfig::price_floor`]. Behind a lock so tests (and any
    /// future ops surface) can flip enforcement without rebuilding the
    /// verifier.
    price_floor: RwLock<PriceFloorConfig>,
    /// ADR-0008: the instant from which a merkle receipt must settle at the
    /// single-node 3x multiplier, defaulting to
    /// [`crate::replication::config::MERKLE_PARITY_ENFORCED_FROM_UNIX`].
    ///
    /// Behind a lock purely so tests can pin it. Every merkle receipt is
    /// necessarily stamped within one week of now (older ones are expired,
    /// future ones refused), so a test that used the production boundary would
    /// silently change which side of it the test data fell on as the wall clock
    /// crossed that date. Pinning the boundary keeps those tests deterministic
    /// forever.
    merkle_parity_from: RwLock<u64>,
    /// ADR-0008: pool hashes this node has already emitted a parity telemetry
    /// line for. Bounded by
    /// [`MERKLE_PARITY_TELEMETRY_CACHE_CAPACITY`]; see that constant for why
    /// this is separate from [`Self::pool_cache`].
    ///
    /// Check-and-insert happens under this one lock, so concurrent admissions
    /// of different chunks from the same batch emit exactly one line between
    /// them rather than one each.
    merkle_parity_logged: Mutex<LruCache<PoolHash, ()>>,
    /// Count of ADR-0008 parity lines this verifier has actually emitted.
    /// Incremented only on first emission for a pool, so it measures
    /// settlements sampled rather than chunks admitted.
    merkle_parity_emissions: std::sync::atomic::AtomicUsize,
    /// Configuration.
    config: PaymentVerifierConfig,
}

/// Shared state for an inflight closeness verification. The leader publishes
/// its result via the `OnceLock`; waiters read that result directly instead
/// of racing on a cache re-check. Wrapped in an `Arc` and held both by the
/// leader's drop guard and by each waiting task.
struct ClosenessSlot {
    notify: Arc<tokio::sync::Notify>,
    /// `Some(Ok(()))` on success, `Some(Err(msg))` on failure, `None` if the
    /// leader disappeared without publishing (panic, cancellation).
    result: std::sync::OnceLock<std::result::Result<(), String>>,
}

impl ClosenessSlot {
    fn new() -> Self {
        Self {
            notify: Arc::new(tokio::sync::Notify::new()),
            result: std::sync::OnceLock::new(),
        }
    }

    /// Build an owned `Notified` future that snapshots the `notify_waiters`
    /// counter at call time. Awaiting this future after dropping external
    /// locks is race-free: if `notify_waiters` fires between construction
    /// and the first poll, the snapshot mismatch resolves the future
    /// immediately.
    fn notified_owned(&self) -> tokio::sync::futures::OwnedNotified {
        Arc::clone(&self.notify).notified_owned()
    }
}

/// Drop guard that publishes the leader's result, clears the inflight slot,
/// and wakes all waiters. Fires on every exit path: success, failure, panic,
/// future-cancellation.
///
/// The guard owns its own `Arc<ClosenessSlot>` so `notify_waiters` still
/// fires even if LRU pressure evicted the slot before the leader finished.
/// Waiters see the published result via `result.get()`; the `Notify` is only
/// the wake-up signal.
struct InflightGuard<'a> {
    slot_cache: &'a Mutex<LruCache<PoolHash, Arc<ClosenessSlot>>>,
    pool_hash: PoolHash,
    slot: Arc<ClosenessSlot>,
}

impl InflightGuard<'_> {
    /// Publish the leader's result. Called exactly once by the leader on
    /// every successful or explicit-error exit. If dropped without calling
    /// (panic, cancellation) the guard still wakes waiters but leaves
    /// `result` empty, which waiters treat as a transient failure and retry.
    fn publish(&self, result: &Result<()>) {
        let stored: std::result::Result<(), String> = match result {
            Ok(()) => Ok(()),
            Err(e) => Err(e.to_string()),
        };
        let _ = self.slot.result.set(stored);
    }
}

impl Drop for InflightGuard<'_> {
    fn drop(&mut self) {
        // Remove the slot entry if it's still ours. A separate leader may
        // have inserted a new slot for the same pool_hash after LRU
        // eviction — don't pop someone else's entry.
        {
            let mut cache = self.slot_cache.lock();
            if let Some(existing) = cache.peek(&self.pool_hash) {
                if Arc::ptr_eq(existing, &self.slot) {
                    cache.pop(&self.pool_hash);
                }
            }
        }
        // Wake every waiter registered against OUR slot, regardless of
        // whether the cache entry is still ours.
        self.slot.notify.notify_waiters();
    }
}

impl PaymentVerifier {
    /// Create a new payment verifier.
    #[must_use]
    pub fn new(config: PaymentVerifierConfig) -> Self {
        const _: () = assert!(
            DEFAULT_POOL_CACHE_CAPACITY > 0,
            "pool cache capacity must be > 0"
        );
        let cache = VerifiedCache::with_capacity(config.cache_capacity);
        let pool_cache_size =
            NonZeroUsize::new(DEFAULT_POOL_CACHE_CAPACITY).unwrap_or(NonZeroUsize::MIN);
        let pool_cache = Mutex::new(LruCache::new(pool_cache_size));
        let closeness_pass_cache = Mutex::new(LruCache::new(pool_cache_size));
        let inflight_closeness = Mutex::new(LruCache::new(pool_cache_size));

        let cache_capacity = config.cache_capacity;
        info!("Payment verifier initialized (cache_capacity={cache_capacity}, evm=always-on, pool_cache={DEFAULT_POOL_CACHE_CAPACITY})");

        // Loud warning if a production binary was accidentally built with
        // `test-utils`: that feature flips the live-DHT payment-check
        // fail-open switches when P2PNode isn't attached. Safe in tests, never
        // intended for prod.
        #[cfg(feature = "test-utils")]
        crate::logging::error!(
            "PaymentVerifier: built with `test-utils` feature — payment live-DHT \
             checks fall back to fail-open when no P2PNode is attached. This \
             feature is for test binaries only; production nodes must be built \
             without it."
        );

        Self {
            cache,
            pool_cache,
            closeness_pass_cache,
            inflight_closeness,
            p2p_node: RwLock::new(None),
            storage: RwLock::new(None),
            #[cfg(any(test, feature = "test-utils"))]
            test_paid_quote_k_closest_override: RwLock::new(None),
            #[cfg(any(test, feature = "test-utils"))]
            test_completed_payments_override: RwLock::new(HashMap::new()),
            commitment_cache: RwLock::new(None),
            pin_fetch_negative_cache: Arc::new(Mutex::new(LruCache::new(
                NonZeroUsize::new(crate::replication::config::PIN_FETCH_NEGATIVE_CACHE_CAPACITY)
                    .unwrap_or(NonZeroUsize::MIN),
            ))),
            monetized_pin_tx: RwLock::new(None),
            local_commitment_source: RwLock::new(None),
            price_floor: RwLock::new(config.price_floor),
            merkle_parity_from: RwLock::new(
                crate::replication::config::MERKLE_PARITY_ENFORCED_FROM_UNIX,
            ),
            merkle_parity_logged: Mutex::new(LruCache::new(
                NonZeroUsize::new(MERKLE_PARITY_TELEMETRY_CACHE_CAPACITY)
                    .unwrap_or(NonZeroUsize::MIN),
            )),
            merkle_parity_emissions: std::sync::atomic::AtomicUsize::new(0),
            config,
        }
    }

    /// Attach the ADR-0004 monetized-pin sender (the replication engine's
    /// first-audit drainer channel) so the cross-check can route commitments
    /// that backed a payment into a deterministic first audit. Idempotent;
    /// absent (unit tests / pre-replication) no first audit is scheduled.
    pub fn attach_monetized_pin_sender(
        &self,
        tx: tokio::sync::mpsc::Sender<crate::replication::MonetizedPinEvent>,
    ) {
        *self.monetized_pin_tx.write() = Some(tx);
        debug!("PaymentVerifier: ADR-0004 monetized-pin sender attached");
    }

    /// Attach the ADR-0004 gossip commitment cache (the replication engine's
    /// `last_commitment_by_peer`) so the cross-check can resolve a quote's
    /// `commitment_pin` against the neighbour's recently gossiped commitment.
    ///
    /// Wired by the node once the replication engine exists, alongside the
    /// quote generator's commitment source. Idempotent. Absent (unit tests,
    /// pre-replication startup), the cross-check resolves no pins from gossip
    /// and falls back to fetch/skip — never a penalty.
    pub fn attach_commitment_cache(&self, cache: CommitmentCache) {
        *self.commitment_cache.write() = Some(cache);
        debug!("PaymentVerifier: ADR-0004 commitment cache attached");
    }

    /// Attach the node's [`P2PNode`] handle so paid-quote verification can
    /// check issuer closeness, and merkle-payment verification can check
    /// candidate `pub_keys` against the DHT's actual closest peers to the pool
    /// midpoint.
    ///
    /// Production startup MUST call this once the `P2PNode` exists. Without
    /// it, live-DHT payment checks fail CLOSED in release builds with a visible
    /// error and fail open in test builds. Idempotent: calling twice replaces
    /// the handle.
    pub fn attach_p2p_node(&self, node: Arc<P2PNode>) {
        *self.p2p_node.write() = Some(node);
        debug!("PaymentVerifier: P2PNode attached for payment live-DHT checks");
    }

    /// Configured close-group width used by storage admission callers.
    #[must_use]
    pub fn close_group_size(&self) -> usize {
        self.config.close_group_size
    }

    /// Attach the node's [`ChunkStore`] handle for store-backed verifier
    /// checks that read the authoritative on-disk record count.
    ///
    /// NOTE: the ADR-0006 price floor does NOT depend on this handle — it is
    /// priced from the close group's gossiped commitments plus this node's own
    /// ([`Self::attach_local_commitment_source`]), and a missing commitment
    /// makes the floor skip rather than reject. So a node without storage
    /// attached still admits PUTs; this
    /// attachment only feeds any current/future store-count-backed checks.
    /// Idempotent: calling twice replaces the handle.
    pub fn attach_storage(&self, storage: Arc<ChunkStore>) {
        *self.storage.write() = Some(storage);
        debug!("PaymentVerifier: ChunkStore attached for paid-quote price-floor checks");
    }

    /// Attach the live commitment source for the price floor: the SAME
    /// `ResponderCommitmentState` the local `QuoteGenerator` prices from, so
    /// the floor and this node's own quotes read one pricing basis by
    /// construction. Read via the non-mutating snapshot only. Idempotent.
    /// Absent (unit tests, storage disabled, pre-replication startup), the
    /// floor SKIPS the admission entirely with `skip_reason=no_own_commitment`
    /// rather than pricing against anything, so a node in that state never
    /// rejects.
    pub fn attach_local_commitment_source(
        &self,
        source: Arc<dyn crate::payment::quote::CommitmentSource>,
    ) {
        *self.local_commitment_source.write() = Some(source);
        debug!("PaymentVerifier: local commitment source attached for price-floor checks");
    }

    /// The live price-floor policy this verifier applies.
    #[must_use]
    pub fn price_floor_config(&self) -> PriceFloorConfig {
        *self.price_floor.read()
    }

    /// Test-only setter for the price-floor policy, so enforcement and
    /// tolerance can be exercised without environment variables.
    #[cfg(any(test, feature = "test-utils"))]
    pub fn set_price_floor_for_tests(&self, config: PriceFloorConfig) {
        *self.price_floor.write() = config;
    }

    /// Test-only setter for the ADR-0008 merkle parity boundary, so both the
    /// pre-boundary (historic 1x) and enforcing (3x) regimes can be exercised
    /// against fixed receipt timestamps instead of the wall clock. `0` enforces
    /// on every receipt; `u64::MAX` puts every receipt in the legacy window.
    #[cfg(any(test, feature = "test-utils"))]
    pub fn set_merkle_parity_from_for_tests(&self, enforced_from: u64) {
        *self.merkle_parity_from.write() = enforced_from;
    }

    /// Number of ADR-0008 parity telemetry lines this verifier has emitted.
    ///
    /// One per settlement pool this node admitted a store for — not one per
    /// chunk, and none at all for proofs that failed validation or for
    /// non-store contexts. Test-only surface for the cardinality regression
    /// tests; the production signal is the log line itself.
    #[cfg(any(test, feature = "test-utils"))]
    #[must_use]
    pub fn merkle_parity_emission_count(&self) -> usize {
        self.merkle_parity_emissions
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Test-only setter for local closest peers used by the paid-quote
    /// issuer K-closest check.
    #[cfg(any(test, feature = "test-utils"))]
    pub fn set_paid_quote_k_closest_for_tests(&self, peer_ids: Vec<[u8; 32]>) {
        *self.test_paid_quote_k_closest_override.write() = Some(peer_ids);
    }

    /// Compatibility alias for older tests that called this the close group.
    /// The check now accepts the K closest peers for the quoted chunk address.
    #[cfg(any(test, feature = "test-utils"))]
    pub fn set_paid_quote_close_group_for_tests(&self, peer_ids: Vec<[u8; 32]>) {
        self.set_paid_quote_k_closest_for_tests(peer_ids);
    }

    /// Compatibility alias for older tests that called this the known-peer
    /// set. The check now accepts the K closest peers for the quoted chunk
    /// address.
    #[cfg(any(test, feature = "test-utils"))]
    pub fn set_paid_quote_known_peers_for_tests(&self, peer_ids: Vec<[u8; 32]>) {
        self.set_paid_quote_k_closest_for_tests(peer_ids);
    }

    /// Test-only setter for an on-chain completed payment amount. The recorded
    /// rewards address is treated as matching the quote (honest settlement).
    #[cfg(any(test, feature = "test-utils"))]
    pub fn set_completed_payment_for_tests(&self, quote_hash: QuoteHash, amount: Amount) {
        self.test_completed_payments_override
            .write()
            .insert(quote_hash, (amount, None));
    }

    /// Test-only setter for an on-chain completed payment with an explicit
    /// recorded rewards address, to exercise the settlement-redirect rejection.
    #[cfg(any(test, feature = "test-utils"))]
    pub fn set_completed_payment_with_address_for_tests(
        &self,
        quote_hash: QuoteHash,
        amount: Amount,
        rewards_address: evmlib::RewardsAddress,
    ) {
        self.test_completed_payments_override.write().insert(
            quote_hash,
            (amount, Some(Self::rewards_address_prefix(&rewards_address))),
        );
    }

    /// The vault's `completedPayments` slot packs the payee as
    /// `bytes16(bytes20(rewardsAddress))` — the leading 16 bytes of the
    /// address (Solidity `bytesN` conversions truncate on the right). This is
    /// the form the on-chain record is compared in.
    fn rewards_address_prefix(rewards_address: &evmlib::RewardsAddress) -> [u8; 16] {
        let mut prefix = [0u8; 16];
        if let Some(head) = rewards_address.as_slice().get(..16) {
            prefix.copy_from_slice(head);
        }
        prefix
    }

    /// Check if payment is required for the given `XorName`.
    ///
    /// This is the main entry point for payment verification:
    /// 1. Check LRU cache (fast path)
    /// 2. If not cached, payment is required
    ///
    /// The fast path is context-aware. A store-admission lookup (`ClientPut` /
    /// `FreshReplication`) is satisfied only by a close-group store
    /// verification. A `PaidListAdmission` lookup is satisfied by either a
    /// paid-list or client-PUT verification.
    ///
    /// # Arguments
    ///
    /// * `xorname` - The content-addressed name of the data
    /// * `context` - The verification context of the caller
    ///
    /// # Returns
    ///
    /// * `PaymentStatus::CachedAsVerified` - Found in local cache (previously paid)
    /// * `PaymentStatus::PaymentRequired` - Not cached (payment required)
    pub fn check_payment_required(
        &self,
        xorname: &XorName,
        context: VerificationContext,
    ) -> PaymentStatus {
        self.check_payment_required_keyed(PaymentTarget::Chunk(*xorname), context)
    }

    /// As [`Self::check_payment_required`], for an already-typed cache key.
    ///
    /// A pointer's entry is filed under both its routing address and the state
    /// paid for, so it can never be satisfied by a chunk that happens to sit at
    /// either value.
    #[must_use]
    pub fn check_payment_required_keyed(
        &self,
        key: PaidKey,
        context: VerificationContext,
    ) -> PaymentStatus {
        let xorname = match &key {
            PaymentTarget::Chunk(address) => address,
            PaymentTarget::Pointer { state_id, .. } => state_id,
        };
        // Check LRU cache (fast path)
        let cached = if context.is_store_admission() {
            self.cache.contains_client_put_verified_key(&key)
        } else {
            self.cache.contains_paid_list_verified_key(&key)
        };
        if cached {
            if crate::logging::enabled!(crate::logging::Level::DEBUG) {
                debug!("Data {} found in verified cache", hex::encode(xorname));
            }
            return PaymentStatus::CachedAsVerified;
        }

        // Not in cache - payment required
        if crate::logging::enabled!(crate::logging::Level::DEBUG) {
            debug!(
                "Data {} not in cache - payment required",
                hex::encode(xorname)
            );
        }
        PaymentStatus::PaymentRequired
    }

    /// Verify that a PUT request has valid payment.
    ///
    /// This is the complete payment verification flow:
    /// 1. Check if data is in cache (previously paid)
    /// 2. If not, verify the provided payment proof
    ///
    /// # Arguments
    ///
    /// * `xorname` - The content-addressed name of the data
    /// * `payment_proof` - Optional payment proof (required if not in cache)
    /// * `context` - Which fresh admission path is verifying the proof — see
    ///   [`VerificationContext`] for cache-strength semantics
    ///
    /// # Returns
    ///
    /// * `Ok(PaymentStatus)` - Verification succeeded
    /// * `Err(Error::Payment)` - No payment and not cached, or payment invalid
    ///
    /// # Errors
    ///
    /// Returns an error if payment is required but not provided, or if payment is invalid.
    pub async fn verify_payment(
        &self,
        xorname: &XorName,
        payment_proof: Option<&[u8]>,
        context: VerificationContext,
    ) -> Result<PaymentStatus> {
        let started = Instant::now();
        let proof_type = payment_proof_type_label(payment_proof);
        let proof_bytes = payment_proof.map_or(0, <[u8]>::len);
        let result = self
            .verify_payment_inner(&PaymentTarget::same(*xorname), payment_proof, context)
            .await;
        let elapsed_ms = started.elapsed().as_millis();

        match &result {
            Ok(status) if elapsed_ms >= PAYMENT_VERIFY_SLOW_LOG_MS => {
                info!(
                    target: "ant_node::payment::verify",
                    "Slow payment verification: context={context:?}, proof_type={proof_type}, proof_bytes={proof_bytes}, status={status:?}, elapsed_ms={elapsed_ms}",
                );
            }
            Ok(status) => {
                debug!(
                    target: "ant_node::payment::verify",
                    "Payment verification: context={context:?}, proof_type={proof_type}, proof_bytes={proof_bytes}, status={status:?}, elapsed_ms={elapsed_ms}",
                );
            }
            Err(e) if elapsed_ms >= PAYMENT_VERIFY_SLOW_LOG_MS => {
                warn!(
                    target: "ant_node::payment::verify",
                    "Slow payment verification failed: context={context:?}, proof_type={proof_type}, proof_bytes={proof_bytes}, elapsed_ms={elapsed_ms}: {e}",
                );
            }
            Err(e) => {
                debug!(
                    target: "ant_node::payment::verify",
                    "Payment verification failed: context={context:?}, proof_type={proof_type}, proof_bytes={proof_bytes}, elapsed_ms={elapsed_ms}: {e}",
                );
            }
        }

        result
    }

    async fn verify_payment_inner(
        &self,
        target: &PaymentTarget,
        payment_proof: Option<&[u8]>,
        context: VerificationContext,
    ) -> Result<PaymentStatus> {
        // What the proof must name: for a chunk its address, for a pointer the
        // state being paid for. Caching under the *address* would mark every
        // future update of a pointer as already paid — the 1.0 free-update
        // defect — so the cache is keyed separately, and never shares a key
        // with the chunk whose address happens to equal this state.
        let xorname = target.content();
        let cache_key = *target;
        // First check if payment is required
        let status = self.check_payment_required_keyed(cache_key, context);

        match status {
            PaymentStatus::CachedAsVerified => {
                // No payment needed - already in cache
                Ok(status)
            }
            PaymentStatus::PaymentRequired => {
                // EVM verification is always on — verify the proof
                if let Some(proof) = payment_proof {
                    let proof_len = proof.len();
                    if proof_len < MIN_PAYMENT_PROOF_SIZE_BYTES {
                        return Err(Error::Payment(format!(
                            "Payment proof too small: {proof_len} bytes (min {MIN_PAYMENT_PROOF_SIZE_BYTES})"
                        )));
                    }
                    if proof_len > MAX_PAYMENT_PROOF_SIZE_BYTES {
                        return Err(Error::Payment(format!(
                            "Payment proof too large: {proof_len} bytes (max {MAX_PAYMENT_PROOF_SIZE_BYTES} bytes)"
                        )));
                    }

                    // Detect proof type from version tag byte
                    match detect_proof_type(proof) {
                        Some(ProofType::Merkle) => {
                            self.verify_merkle_payment(target, proof, context).await?;
                        }
                        Some(ProofType::SingleNode) => {
                            let parsed = deserialize_single_node_proof(proof).map_err(|e| {
                                Error::Payment(format!("Failed to deserialize payment proof: {e}"))
                            })?;

                            if !parsed.tx_hashes.is_empty() {
                                debug!(
                                    "Proof includes {} transaction hash(es)",
                                    parsed.tx_hashes.len()
                                );
                            }

                            self.verify_evm_payment(
                                target,
                                &parsed.proof_of_payment,
                                &parsed.commitment_sidecars,
                                context,
                            )
                            .await?;
                        }
                        None => {
                            let tag = proof.first().copied().unwrap_or(0);
                            return Err(Error::Payment(format!(
                                "Unknown payment proof type tag: 0x{tag:02x}"
                            )));
                        }
                        // ant-protocol marks `ProofType` as `#[non_exhaustive]`.
                        // A future proof variant that this node does not yet
                        // understand must be rejected, not silently accepted.
                        Some(_) => {
                            let tag = proof.first().copied().unwrap_or(0);
                            return Err(Error::Payment(format!(
                                "Unsupported payment proof type tag: 0x{tag:02x} (this node's protocol version does not handle it — upgrade ant-node)"
                            )));
                        }
                    }

                    // Cache the verified xorname at the context's verification
                    // strength. Stronger entries satisfy weaker future lookups,
                    // but not the reverse.
                    if context.is_store_admission() {
                        self.cache.insert_key(cache_key);
                    } else {
                        self.cache.insert_paid_list_verified_key(cache_key);
                    }

                    Ok(PaymentStatus::PaymentVerified)
                } else {
                    // No payment provided in production mode
                    let xorname_hex = hex::encode(xorname);
                    Err(Error::Payment(format!(
                        "Payment required for new data {xorname_hex}"
                    )))
                }
            }
            PaymentStatus::PaymentVerified => Err(Error::Payment(
                "Unexpected PaymentVerified status from check_payment_required".to_string(),
            )),
        }
    }

    /// Verify that a pointer state was paid for.
    ///
    /// `routing_address` is the pointer's address: it selects the close group
    /// whose quotes count, and it never changes for the life of the pointer.
    /// `paid_content` is the record's `state_id`: it is what the quote must
    /// name, and it changes with every update. Passing one address for both —
    /// which is all the chunk path can express — would either check the wrong
    /// close group or mark every future update of the pointer as already paid.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Payment`] if the proof is missing, malformed, names a
    /// different state, was issued by a peer outside the routing address's
    /// close group, or did not settle on chain.
    pub async fn verify_pointer_payment(
        &self,
        routing_address: &XorName,
        paid_content: &XorName,
        payment_proof: &[u8],
    ) -> Result<()> {
        self.verify_pointer_payment_in(
            routing_address,
            paid_content,
            payment_proof,
            VerificationContext::ClientPut,
        )
        .await
    }

    /// As [`Self::verify_pointer_payment`], in `context`.
    ///
    /// Fresh replication verifies under
    /// [`VerificationContext::FreshReplication`], exactly as a chunk offer does:
    /// the checks are identical, the context only keeps the price-floor
    /// telemetry apart.
    ///
    /// # Errors
    ///
    /// As [`Self::verify_pointer_payment`].
    pub async fn verify_pointer_payment_in(
        &self,
        routing_address: &XorName,
        paid_content: &XorName,
        payment_proof: &[u8],
        context: VerificationContext,
    ) -> Result<()> {
        let target = PaymentTarget::split(*routing_address, *paid_content);
        match self
            .verify_payment_inner(&target, Some(payment_proof), context)
            .await?
        {
            PaymentStatus::CachedAsVerified | PaymentStatus::PaymentVerified => Ok(()),
            PaymentStatus::PaymentRequired => Err(Error::Payment(format!(
                "no settled payment for pointer state {}",
                hex::encode(paid_content)
            ))),
        }
    }

    /// Get cache statistics.
    #[must_use]
    pub fn cache_stats(&self) -> CacheStats {
        self.cache.stats()
    }

    /// Get the number of cached entries.
    #[must_use]
    pub fn cache_len(&self) -> usize {
        self.cache.len()
    }

    /// Pre-populate the payment cache for a given address.
    ///
    /// This marks the address as already paid, so subsequent `verify_payment`
    /// calls will return `CachedAsVerified` without on-chain verification.
    /// Useful for test setups where real EVM payment is not needed.
    #[cfg(any(test, feature = "test-utils"))]
    pub fn cache_insert(&self, xorname: XorName) {
        self.cache.insert(xorname);
    }

    /// Mark a pointer state as paid, so verifying it needs no chain lookup.
    /// The pointer counterpart of [`Self::cache_insert`]: keyed by the state
    /// and routed at the address, as a real pointer payment is.
    #[cfg(any(test, feature = "test-utils"))]
    pub fn cache_insert_pointer(&self, routing_address: XorName, state_id: XorName) {
        self.cache
            .insert_key(PaymentTarget::split(routing_address, state_id));
    }

    /// Mark startup content as prepaid for the in-process browser devnet.
    ///
    /// This remains crate-private and feature-gated: it is used only by
    /// [`crate::devnet::Devnet::publish_public_file`] before that local devnet
    /// is handed to a browser. The subsequent PUT still traverses the normal
    /// address, responsibility, payment-cache, storage, and read-verification
    /// checks.
    #[cfg(all(feature = "webrtc-direct", feature = "test-utils"))]
    pub(crate) fn cache_insert_browser_devnet_seed(&self, xorname: XorName) {
        self.cache.insert(xorname);
    }

    /// Pre-populate the merkle pool cache. Testing helper that lets e2e tests
    /// bypass the on-chain `completedMerklePayments` lookup when the point of
    /// the test is to exercise merkle-verification logic BEFORE the on-chain
    /// call (e.g. the pay-yourself closeness check).
    #[cfg(any(test, feature = "test-utils"))]
    pub fn pool_cache_insert(&self, pool_hash: PoolHash, info: OnChainPaymentInfo) {
        let mut cache = self.pool_cache.lock();
        cache.put(pool_hash, info);
    }

    /// Verify a single-node EVM payment proof.
    ///
    /// Verification steps:
    /// 1. Between 1 and `CLOSE_GROUP_SIZE` quotes are present
    /// 2. Median-priced candidate quotes are derived from the supplied bundle
    /// 3. Each candidate is checked for content binding, peer binding, and a
    ///    valid ML-DSA-65 signature
    /// 4. Each candidate must also come from a local K-close peer
    /// 5. A candidate is accepted only if `completedPayments(quoteHash)` is at
    ///    least 3x the median price
    ///
    /// Non-median quotes are parsed only to locate the median. Their content,
    /// peer bindings, and signatures are deliberately ignored: the paid
    /// quote's content hash, quote hash, signature, issuer
    /// K-closeness check, and on-chain settlement are the authority. A
    /// one-quote proof is valid when that single quote passes these checks and
    /// was paid 3x.
    async fn verify_evm_payment(
        &self,
        target: &PaymentTarget,
        payment: &ProofOfPayment,
        commitment_sidecars: &[Vec<u8>],
        context: VerificationContext,
    ) -> Result<()> {
        // `content` is what a quote must name; `routing` is whose close group
        // may issue it. For a chunk they are one address, for a pointer they
        // are not.
        let xorname = target.content();
        if crate::logging::enabled!(crate::logging::Level::DEBUG) {
            let xorname_hex = hex::encode(xorname);
            let quote_count = payment.peer_quotes.len();
            debug!(
                "Verifying EVM payment for {xorname_hex} with {quote_count} quotes ({context:?})"
            );
        }

        Self::validate_quote_structure(payment)?;
        // ADR-0004: re-run the `price == calculate_price(committed_key_count)`
        // arithmetic/binding check on EVERY quote in the bundle (all single-node
        // quotes), per the ADR's "every storer re-runs the
        // price-equals-formula-of-count check on every quote in the bundle"
        // rule — bundle-level, before median selection (the candidate loop below
        // only sees median-priced quotes). This hard cutover also RETIRES the
        // percentage-based own-quote price-staleness gate: a quote's price is
        // now exactly bound to its committed count here (both the `(n>0, Some)`
        // and baseline `(0, None)` shapes), and the committed responsible count
        // legitimately differs from the on-disk count, so the old gate would
        // FALSE-REJECT healthy ADR quotes. The binding gate supersedes it.
        Self::validate_quote_arithmetic(payment)?;
        let candidates = Self::legacy_median_candidates(payment)?;
        let mut failures = Vec::with_capacity(candidates.len());
        // The paid (median) price and settled on-chain amount of the winning
        // candidate, kept for the receiver-side price-floor policy below.
        let mut verified_paid_quote: Option<(Amount, Amount)> = None;
        // ADR-0004 Amendment 2: remember WHICH candidate's on-chain settlement
        // verified — only that peer's commitment actually earned money, so only
        // its pin is nominated for a deterministic first audit below. The
        // non-median quotes merely locate the median and stay covered by the
        // gossip-lottery audit path.
        let mut paid_peer: Option<[u8; 32]> = None;

        for candidate in candidates {
            let paid_price = candidate.quote.price;
            let candidate_peer = *candidate.encoded_peer_id.as_bytes();
            match self.verify_legacy_median_candidate(target, candidate).await {
                Ok(settled_amount) => {
                    verified_paid_quote = Some((paid_price, settled_amount));
                    // First settlement-verified median candidate wins the paid
                    // slot and the sole first-audit nomination. If a client
                    // settled several TIED-median candidates, only this one is
                    // first-audited; gossiped extras keep the ADR-0002 lottery,
                    // sidecar-only extras are an accepted best-effort residual
                    // (ADR-0004 Amendment 2). The honest client pays one.
                    paid_peer = Some(candidate_peer);
                    break;
                }
                Err(err) => failures.push(err.to_string()),
            }
        }

        let Some((paid_price, settled_amount)) = verified_paid_quote else {
            let xorname_hex = hex::encode(xorname);
            let details = if failures.is_empty() {
                "no median-priced candidates were available".to_string()
            } else {
                failures.join("; ")
            };
            return Err(Error::Payment(format!(
                "Median quote payment verification failed for {xorname_hex}: {details}"
            )));
        };

        // Receiver-side price floor (single-node store admissions only). Runs
        // AFTER the paid quote's signature and settlement verified above, so
        // unauthenticated bundles can never poison floor telemetry. Shadow
        // mode logs; enforcement rejects — an economic admission decision
        // only, never trust/misbehaviour evidence.
        // The floor compares against the median commitment price of the close
        // group that is RESPONSIBLE for the data, so it takes the routing
        // address. For a chunk that is the same value; for a pointer the paid
        // content is its state, which names no close group at all.
        self.enforce_price_floor(target.routing(), paid_price, settled_amount, context)
            .await?;

        // ADR-0004 observe-only telemetry: log off-curve quotes only AFTER the
        // paid (median) quote's ML-DSA-65 signature has verified above, so
        // unauthenticated senders cannot poison rollout logs. In enforce mode
        // `validate_quote_arithmetic` already rejected; this is a no-op there.
        Self::log_off_curve_single_node(payment);

        // ADR-0004 cross-check + first-audit enqueue (store admissions only —
        // direct client PUT and immediate fresh replication, exactly the paths
        // that previously verified under `ClientPut`) runs ONLY after on-chain
        // payment verification has SUCCEEDED above, so an unpaid (but signed)
        // bundle can never enqueue audits or drive pin fetches — closing the
        // free-amplification path.
        if context.is_store_admission() {
            self.cross_check_quotes(payment, commitment_sidecars, paid_peer)
                .await;
        }

        if crate::logging::enabled!(crate::logging::Level::INFO) {
            let xorname_hex = hex::encode(xorname);
            info!("EVM payment verified for {xorname_hex}");
        }
        Ok(())
    }

    fn legacy_median_candidates(
        payment: &ProofOfPayment,
    ) -> Result<Vec<LegacyMedianCandidate<'_>>> {
        let mut sorted_quotes: Vec<(&evmlib::EncodedPeerId, &PaymentQuote)> = payment
            .peer_quotes
            .iter()
            .map(|(encoded_peer_id, quote)| (encoded_peer_id, quote))
            .collect();
        sorted_quotes.sort_by_key(|(_, quote)| quote.price);
        let quote_count = sorted_quotes.len();
        let median_index = median_quote_index(quote_count);
        let median_price = sorted_quotes
            .get(median_index)
            .ok_or_else(|| {
                Error::Payment(format!("Missing paid quote at median index {median_index}"))
            })?
            .1
            .price;
        let expected_amount = median_price
            .checked_mul(Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER))
            .ok_or_else(|| {
                Error::Payment(format!(
                    "Median quote payment amount overflow for price {median_price}"
                ))
            })?;

        if expected_amount == Amount::ZERO || median_price == Amount::ZERO {
            return Err(Error::Payment(format!(
                "Median quote has zero price/amount (price={median_price}, amount={expected_amount}); refusing to verify as paid"
            )));
        }

        Ok(sorted_quotes
            .into_iter()
            .filter(|(_, quote)| quote.price == median_price)
            .map(|(encoded_peer_id, quote)| LegacyMedianCandidate {
                encoded_peer_id,
                quote,
                expected_amount,
            })
            .collect())
    }

    /// Fully validate one median-priced candidate. On success returns the
    /// settled on-chain amount for the candidate's quote hash, which the
    /// price-floor policy compares against the close group's median
    /// commitment price (the settled amount, not the quoted price, so an
    /// honest client may overpay a cheap quote to clear stricter receivers).
    async fn verify_legacy_median_candidate(
        &self,
        target: &PaymentTarget,
        candidate: LegacyMedianCandidate<'_>,
    ) -> Result<Amount> {
        // The two checks take different addresses. The quote must name what was
        // paid for; the issuer must be close to what the network routes. A
        // chunk supplies one address for both, a pointer two.
        Self::validate_paid_quote_content(target.content(), candidate)?;
        let issuer_peer_id =
            Self::validate_paid_quote_peer_binding(candidate.encoded_peer_id, candidate.quote)?;

        self.validate_paid_quote_issuer_k_closest(target.routing(), &issuer_peer_id)
            .await?;

        Self::validate_paid_quote_signature(candidate).await?;

        let (on_chain_amount, recorded_rewards_prefix) = self
            .completed_payment_settlement(candidate.quote.hash())
            .await?;
        // ADR-0004 Amendment 2: the settlement must be RECORDED FOR THE QUOTE'S
        // rewards address, not merely under its quote hash. The vault stores
        // whatever `(rewardsAddress, amount)` the payer supplied for the hash,
        // so an amount-only check would accept a payment the client redirected
        // to its own wallet — the issuer would be treated (and first-audited)
        // as "paid" without ever being compensated. Honest clients build the
        // payment from the quote itself, so this never rejects a legit upload.
        // The vault packs the payee as `bytes16`, so the comparison is on the
        // leading 16 bytes (128 bits — far beyond grinding range).
        if let Some(recorded) = recorded_rewards_prefix {
            let expected = Self::rewards_address_prefix(&candidate.quote.rewards_address);
            if recorded != expected {
                return Err(Error::Payment(format!(
                    "Median-priced quote settlement for peer {:?} was redirected: recorded rewards address prefix {} does not match the quote's {}",
                    candidate.encoded_peer_id,
                    hex::encode(recorded),
                    hex::encode(expected)
                )));
            }
        }
        if on_chain_amount >= candidate.expected_amount {
            return Ok(on_chain_amount);
        }

        Err(Error::Payment(format!(
            "Median-priced quote for peer {:?} was not paid enough: expected at least {}, got {on_chain_amount}",
            candidate.encoded_peer_id, candidate.expected_amount
        )))
    }

    /// The peer ids this node considers closest to `xorname`, by pure XOR
    /// distance, mirroring the selection the paid-quote closeness check uses.
    ///
    /// Returns an empty vec when the node is not wired to the P2P layer, which
    /// callers must treat as "no local view" rather than "no close peers".
    async fn local_k_closest_peer_ids(&self, xorname: &XorName) -> Vec<PeerId> {
        #[cfg(any(test, feature = "test-utils"))]
        if let Some(k_closest_peer_ids) = self.test_paid_quote_k_closest_override.read().as_ref() {
            return k_closest_peer_ids
                .iter()
                .map(|bytes| PeerId::from_bytes(*bytes))
                .collect();
        }

        let Some(p2p_node) = self.p2p_node.read().as_ref().map(Arc::clone) else {
            return Vec::new();
        };
        p2p_node
            .dht_manager()
            .find_closest_nodes_local_by_distance_with_self(
                xorname,
                PAID_QUOTE_ISSUER_CLOSENESS_WIDTH,
            )
            .await
            .into_iter()
            .map(|node| node.peer_id)
            .collect()
    }

    /// The close group's MEDIAN commitment-bound price for `xorname`, and the
    /// number of peers that median was taken over.
    ///
    /// This is the price floor's reference, and choosing the median of the group
    /// rather than this node's own price is the whole point of the policy.
    ///
    /// A client pays `3x` the MEDIAN of the quotes it collected. Pricing the
    /// floor against a receiver's OWN price compares that median to a single
    /// draw from a distribution that spans ~4x across the fleet, so the fullest
    /// receivers reject honest payments while the emptiest admit deep
    /// underpayment — measured on ant-prod-01, 0.377% of honest stores rejected
    /// with the cheapest-of-K underpayment still admitted by 62.9% of receivers.
    /// Both sides of that are the same defect: the reference is a sample, and
    /// the payment is a median. Comparing median to median removes it.
    ///
    /// The reference is built ONLY from state this node already holds:
    ///
    /// * the K peers closest to `xorname` in its own routing table, and
    /// * those peers' own signed commitments from the gossip cache, TTL-gated
    ///   by [`GOSSIP_ANSWERABILITY_TTL`], plus this node's own live commitment.
    ///
    /// Nothing in the payment bundle feeds it. A client cannot pad, prune or
    /// reorder quotes to move this number, which is what makes the floor
    /// resistant to the quote-selection games it exists to stop.
    ///
    /// The remaining lever is gossiping a false commitment, and it cuts BOTH
    /// ways, which is why the sample gate is set where it is:
    ///
    /// * **Understating** drags the median down and weakens the floor. The cost
    ///   is a missed underpayment.
    /// * **Overstating** drags it up and makes this node reject settled, honest
    ///   payments. That is the dangerous direction, because the money is already
    ///   spent and cannot be refunded, so a griefer would be spending nothing to
    ///   destroy someone else's payment.
    ///
    /// The bound is NOT "a majority of the group", and it is worth stating
    /// precisely because it is what an enforcement decision rests on. The floor
    /// does not need the median to reach the liars' value — only to move past
    /// the tolerance headroom. Each liar shifts the order statistic by one rank,
    /// and on the measured fleet spread one rank is worth roughly 15% of price
    /// while a 65% tolerance buys 1.54x. So **about a quarter of the sample**
    /// suffices in either direction: measured on this crate's own fixture, 4 of
    /// 15 overstating neighbours reject an honest payment sitting at the true
    /// median, and 4 of 15 understating ones (placed above the median, where an
    /// attacker would put them) admit a cheapest-of-K underpayment.
    ///
    /// Counts above `MAX_COMMITMENT_KEY_COUNT` are dropped outright, since
    /// gossip ingest authenticates the sender without bounding the count. That
    /// caps how far a single liar can reach but does not change the cardinality
    /// bound above. A signature proves who said a number, never that the number
    /// is true, and address grinding into a close group is a known adjacent
    /// problem. This is why the policy ships shadow-only: the ~25% bound is a
    /// reason not to enforce yet, not a property to rely on.
    ///
    /// Returns the reference price only when at least
    /// [`PRICE_FLOOR_MIN_NEIGHBOUR_COMMITMENTS`] neighbour commitments are
    /// fresh AND this node has its own live commitment; the `usize` is the
    /// sample size either way, so the caller can log how close it came. A `None`
    /// price means "do not evaluate".
    ///
    /// [`GOSSIP_ANSWERABILITY_TTL`]: crate::replication::commitment_state::GOSSIP_ANSWERABILITY_TTL
    async fn group_reference_price(&self, xorname: &XorName) -> (Option<Amount>, GroupSample) {
        // This node's own live commitment. It is in the group whenever it is
        // being asked to store the chunk, so it belongs in the sample. Bound to
        // a local first so the source lock is released before the awaits below.
        //
        // It is counted separately from the neighbour tally: the gate below asks
        // "how much of the group do I actually know", and answering that with a
        // number that includes yourself overstates it by one, which matters most
        // exactly when the view is thinnest.
        // Wiring faults are reported before state faults, so a startup-order bug
        // is never mistaken for "this node is mid-rotation" or for "the gate is
        // above what the network supplies". Those three have three different
        // fixes and shadow mode exists to tell them apart.
        let Some(cache) = self.commitment_cache.read().as_ref().map(Arc::clone) else {
            return (None, GroupSample::none(SkipReason::NoCommitmentCache));
        };
        // Cache is checked first so a node without one does not pay for a
        // routing-table scan it will discard.
        let closest = self.local_k_closest_peer_ids(xorname).await;
        if closest.is_empty() {
            return (None, GroupSample::none(SkipReason::NoRoutingView));
        }

        let own_source = self.local_commitment_source.read().as_ref().map(Arc::clone);
        // No own commitment means fresh, retired, or mid-rotation. Such a node
        // prices its OWN quotes at baseline, so letting it hold an incoming
        // payment to its neighbours' (higher) median would reject settlements it
        // would itself have quoted. Skip, which is also what ADR-0006 says a
        // receiver in this state does.
        let Some(own) = own_source.and_then(|source| source.current_binding_snapshot()) else {
            return (None, GroupSample::none(SkipReason::NoOwnCommitment));
        };
        let now = std::time::Instant::now();
        let ttl = crate::replication::commitment_state::GOSSIP_ANSWERABILITY_TTL;
        let mut key_counts: Vec<u32> = {
            let guard = cache.read().await;
            closest
                .iter()
                .filter_map(|peer_id| {
                    let record = guard.get(peer_id)?;
                    // Stale gossip is treated as unknown rather than as a low
                    // price, so a peer going quiet can never drag the median
                    // down, nor count toward the minimum sample.
                    if now.saturating_duration_since(record.received_at) >= ttl {
                        return None;
                    }
                    record
                        .last_commitment()
                        .map(|c| c.key_count)
                        .filter(|count| *count <= MAX_COMMITMENT_KEY_COUNT)
                })
                .collect()
        };

        let neighbours_known = key_counts.len();
        // Own count is held to the same cap as a neighbour's. Unreachable today
        // (the local builder refuses to build over-cap), but an asymmetry here
        // is the kind that quietly becomes a hole later.
        if own.key_count <= MAX_COMMITMENT_KEY_COUNT {
            key_counts.push(own.key_count);
        }
        let sample = GroupSample {
            neighbours: neighbours_known,
            total: key_counts.len(),
            skip_reason: None,
        };

        if neighbours_known < PRICE_FLOOR_MIN_NEIGHBOUR_COMMITMENTS {
            return (None, sample.skipped(SkipReason::ThinSample));
        }

        // Median of the PRICES, via the median key count. `calculate_price` is
        // monotonic in the key count, so the two orderings agree and taking the
        // median count avoids averaging two U256 prices.
        //
        // The LOWER median on even samples, deliberately: the upper median
        // rounds the reference UP, and a reference that is too high rejects
        // honest payments that have already settled on-chain and cannot be
        // refunded. (The merkle settlement path deliberately uses the UPPER
        // median instead: there it must match the on-chain contract's formula,
        // not choose a safe direction.) ADR-0006 carries the
        // measured difference.
        key_counts.sort_unstable();
        let median_key_count = key_counts
            .get((sample.total.saturating_sub(1)) / 2)
            .copied()
            .unwrap_or_default();
        let reference = calculate_price(usize::try_from(median_key_count).unwrap_or_default());
        (Some(reference), sample)
    }

    /// Receiver-side price floor for single-node store admissions.
    ///
    /// Requires the settled on-chain amount to also clear
    /// `PAID_QUOTE_PAYMENT_MULTIPLIER × tolerance% ×` the close group's median
    /// commitment-bound price (see [`Self::group_reference_price`]). Together
    /// with the per-quote check above, the effective rule is
    /// `settled >= 3 × max(paid_price, tolerated_floor)`.
    ///
    /// When the group reference cannot be built — too few fresh commitments, no
    /// P2P handle, no gossip cache — the floor SKIPS the admission rather than
    /// falling back to this node's own price. Falling back is what the previous
    /// revision did, and it is precisely the comparison that rejected honest
    /// stores on the fullest nodes. A skipped evaluation is logged with its
    /// real `neighbours` / `group_sample` counts and a `skip_reason`, so shadow
    /// telemetry can separate an unwired node from one whose gate simply sits
    /// above what the network supplies.
    ///
    /// Always emits one telemetry line per evaluated admission (target
    /// `ant_node::payment::price_floor`) so shadow mode measures the exact
    /// rejections enforcement would cause. Rejects only when
    /// [`PriceFloorConfig::enforce`] is set.
    async fn enforce_price_floor(
        &self,
        xorname: &XorName,
        paid_price: Amount,
        settled_amount: Amount,
        context: VerificationContext,
    ) -> Result<()> {
        if !context.is_store_admission() {
            return Ok(());
        }

        let floor = *self.price_floor.read();
        let tolerance_percent = floor.tolerance_percent.min(100);

        let (reference, group_sample) = self.group_reference_price(xorname).await;
        let Some(reference_price) = reference else {
            // Not enough of the close group is known to price anything. Skip
            // rather than guess: see `group_reference_price`. The reason and
            // both counts are logged because shadow telemetry has to separate a
            // mid-rotation node from an unwired cache from a gate set above what
            // the network can supply — three causes with three different fixes.
            info!(
                target: "ant_node::payment::price_floor",
                "Price floor: context={context:?}, xorname={}, paid_price={paid_price}, \
                 settled={settled_amount}, neighbours={}, group_sample={}, reference_price=0, \
                 tolerance_percent={tolerance_percent}, required=0, below_floor=false, \
                 skipped=true, skip_reason={}, enforce={}",
                hex::encode(xorname),
                group_sample.neighbours,
                group_sample.total,
                group_sample.skip_reason_str(),
                floor.enforce,
            );
            return Ok(());
        };

        // Amount is a U256: reference_price is bounded by the saturating pricing
        // curve and the multipliers are <= 300, so this arithmetic cannot
        // overflow; saturating keeps that a proof instead of an assumption.
        let tolerated_price =
            reference_price.saturating_mul(Amount::from(tolerance_percent)) / Amount::from(100u64);
        let required_amount =
            tolerated_price.saturating_mul(Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER));
        let below_floor = settled_amount < required_amount;

        info!(
            target: "ant_node::payment::price_floor",
            "Price floor: context={context:?}, xorname={}, paid_price={paid_price}, \
             settled={settled_amount}, neighbours={}, group_sample={}, \
             reference_price={reference_price}, tolerance_percent={tolerance_percent}, \
             required={required_amount}, below_floor={below_floor}, skipped=false, \
             skip_reason=none, enforce={}",
            hex::encode(xorname),
            group_sample.neighbours,
            group_sample.total,
            floor.enforce,
        );

        if below_floor && floor.enforce {
            return Err(Error::Payment(format!(
                "Settled payment {settled_amount} is below the close-group price floor \
                 {required_amount} ({PAID_QUOTE_PAYMENT_MULTIPLIER}x {tolerance_percent}% of \
                 the group median price {reference_price}, taken over {} peers)",
                group_sample.total
            )));
        }

        Ok(())
    }

    fn validate_paid_quote_content(
        xorname: &XorName,
        candidate: LegacyMedianCandidate<'_>,
    ) -> Result<()> {
        if verify_quote_content(candidate.quote, xorname) {
            return Ok(());
        }

        let expected_hex = hex::encode(xorname);
        let actual_hex = hex::encode(candidate.quote.content.0);
        Err(Error::Payment(format!(
            "Paid quote content address mismatch for peer {:?}: expected {expected_hex}, got {actual_hex}",
            candidate.encoded_peer_id
        )))
    }

    async fn validate_paid_quote_signature(candidate: LegacyMedianCandidate<'_>) -> Result<()> {
        let quote_for_signature = candidate.quote.clone();
        let peer_id_for_error = candidate.encoded_peer_id.clone();
        tokio::task::spawn_blocking(move || {
            if !verify_quote_signature(&quote_for_signature) {
                return Err(Error::Payment(format!(
                    "Paid quote ML-DSA-65 signature verification failed for peer {peer_id_for_error:?}"
                )));
            }
            Ok(())
        })
        .await
        .map_err(|e| Error::Payment(format!("Signature verification task failed: {e}")))?
    }

    /// Look up the on-chain settlement recorded for `quote_hash`: the settled
    /// amount and the `bytes16` rewards-address prefix it was recorded for.
    /// The prefix is `None` only on the test-override path (meaning "matches
    /// the quote"); the production contract read always returns the recorded
    /// prefix so the caller's redirect check always applies.
    async fn completed_payment_settlement(
        &self,
        quote_hash: QuoteHash,
    ) -> Result<(Amount, Option<[u8; 16]>)> {
        #[cfg(any(test, feature = "test-utils"))]
        {
            let completed_payment_override = {
                self.test_completed_payments_override
                    .read()
                    .get(&quote_hash)
                    .copied()
            };
            if let Some((amount, recorded)) = completed_payment_override {
                return Ok((amount, recorded));
            }
        }

        let provider = evmlib::utils::http_provider(self.config.evm.network.rpc_url().clone());
        let vault_address = *self.config.evm.network.payment_vault_address();
        let contract = payment_vault::interface::IPaymentVault::new(vault_address, provider);

        let result = contract.completedPayments(quote_hash).call().await;
        // V2-834: EVM RPC is counted per call (bodies live inside alloy).
        super::traffic::record(
            super::traffic::EvmRpcCall::CompletedPayments,
            result.is_ok(),
        );
        let result =
            result.map_err(|e| Error::Payment(format!("completedPayments lookup failed: {e}")))?;

        Ok((Amount::from(result.amount), Some(result.rewardsAddress.0)))
    }

    fn validate_paid_quote_peer_binding(
        encoded_peer_id: &evmlib::EncodedPeerId,
        quote: &PaymentQuote,
    ) -> Result<PeerId> {
        let expected_peer_id = peer_id_from_public_key_bytes(&quote.pub_key)
            .map_err(|e| Error::Payment(format!("Invalid ML-DSA public key in quote: {e}")))?;

        if expected_peer_id.as_bytes() != encoded_peer_id.as_bytes() {
            let expected_hex = expected_peer_id.to_hex();
            let actual_hex = hex::encode(encoded_peer_id.as_bytes());
            return Err(Error::Payment(format!(
                "Paid quote pub_key does not belong to claimed peer {encoded_peer_id:?}: \
                 BLAKE3(pub_key) = {expected_hex}, peer_id = {actual_hex}"
            )));
        }

        Ok(expected_peer_id)
    }

    async fn validate_paid_quote_issuer_k_closest(
        &self,
        xorname: &XorName,
        issuer_peer_id: &PeerId,
    ) -> Result<()> {
        #[cfg(any(test, feature = "test-utils"))]
        if let Some(k_closest_peer_ids) = self.test_paid_quote_k_closest_override.read().as_ref() {
            if k_closest_peer_ids
                .iter()
                .any(|peer_id| peer_id == issuer_peer_id.as_bytes())
            {
                return Ok(());
            }
            let issuer_closeness_width = PAID_QUOTE_ISSUER_CLOSENESS_WIDTH;
            return Err(Error::Payment(format!(
                "Paid quote issuer {} is not among this node's local K={issuer_closeness_width} closest peers for {}",
                issuer_peer_id.to_hex(),
                hex::encode(xorname)
            )));
        }

        let attached = self.p2p_node.read().as_ref().map(Arc::clone);
        let Some(p2p_node) = attached else {
            #[cfg(any(test, feature = "test-utils"))]
            {
                crate::logging::warn!(
                    "PaymentVerifier: no P2PNode attached; paid-quote issuer \
                     K-closest check SKIPPED (test build). Production startup MUST call \
                     PaymentVerifier::attach_p2p_node."
                );
                return Ok(());
            }
            #[cfg(not(any(test, feature = "test-utils")))]
            {
                crate::logging::error!(
                    "PaymentVerifier: no P2PNode attached; rejecting paid-quote \
                     payment. This is a node-startup bug — \
                     PaymentVerifier::attach_p2p_node must be called before \
                     any PUT handler runs."
                );
                return Err(Error::Payment(
                    "Paid quote rejected: verifier is not wired to the P2P \
                     layer; cannot verify issuer closeness."
                        .into(),
                ));
            }
        };

        // Closeness *verification* must mirror the uploader's pure XOR-distance
        // peer selection. `find_closest_nodes_local_with_self` reranks the local
        // routing table by reachability (preferring directly-reachable peers,
        // XOR only as a tiebreaker), which demotes an XOR-close relay-only /
        // NAT'd peer out of the compared window and falsely rejects an honest
        // payment that legitimately quoted that peer. Use the XOR-only sibling
        // so this check matches how the client chose the quoted K-closest set.
        let issuer_closeness_width = PAID_QUOTE_ISSUER_CLOSENESS_WIDTH;
        let closest = p2p_node
            .dht_manager()
            .find_closest_nodes_local_by_distance_with_self(xorname, issuer_closeness_width)
            .await;
        if closest.iter().any(|node| node.peer_id == *issuer_peer_id) {
            return Ok(());
        }

        Err(Error::Payment(format!(
            "Paid quote issuer {} is not among this node's local K={issuer_closeness_width} closest peers for {}",
            issuer_peer_id.to_hex(),
            hex::encode(xorname)
        )))
    }

    /// Validate quote count, uniqueness, and basic structure.
    fn validate_quote_structure(payment: &ProofOfPayment) -> Result<()> {
        if payment.peer_quotes.is_empty() {
            return Err(Error::Payment("Payment has no quotes".to_string()));
        }

        let quote_count = payment.peer_quotes.len();
        if quote_count > CLOSE_GROUP_SIZE {
            return Err(Error::Payment(format!(
                "Payment must have at most {CLOSE_GROUP_SIZE} quotes, got {quote_count}"
            )));
        }

        let mut seen: Vec<&evmlib::EncodedPeerId> = Vec::with_capacity(quote_count);
        for (encoded_peer_id, _) in &payment.peer_quotes {
            if seen.contains(&encoded_peer_id) {
                return Err(Error::Payment(format!(
                    "Duplicate peer ID in payment quotes: {encoded_peer_id:?}"
                )));
            }
            seen.push(encoded_peer_id);
        }

        Ok(())
    }

    /// ADR-0004: enforce that every quoted price lies exactly on the public
    /// pricing curve.
    ///
    /// **Scope**: the price is forced by the quote's own signed
    /// `committed_key_count`, not merely required to be *somewhere* on the
    /// curve. `PaymentQuote` now carries `committed_key_count` and
    /// `commitment_pin`, both covered by the quote signature and the quote
    /// hash, so [`Self::binding_violation`] can hold the price to the count the
    /// quoter committed to. What this gate does not do is resolve the pin
    /// against the commitment the peer actually gossiped — a quoter can still
    /// commit to a count it does not store and price consistently with it.
    /// That cross-check is [`Self::cross_check_quotes`], gated separately by
    /// [`crate::replication::config::QUOTE_COMMITMENT_MISMATCH_TRUST_ENABLED`]
    /// because it carries a trust penalty rather than being reject-only.
    ///
    /// **Check**: exact recomputation, never price-inversion. The price is
    /// compared against `calculate_price(committed_key_count)` recomputed from
    /// the signed count. Inverting the price to a count would silently accept
    /// any value between two curve points, and would also let a quote price
    /// itself off one count while committing to another.
    ///
    /// **Where it runs**: in every [`VerificationContext`] over **every**
    /// quote in **both** quote types — all 7 single-node quotes
    /// ([`Self::validate_quote_arithmetic`]) and all 16 merkle candidates
    /// ([`Self::validate_merkle_candidate_arithmetic`]) — because the rule
    /// "every storer re-runs the price-equals-formula-of-count check on every
    /// quote in the bundle" (ADR-0004) needs no peer-specific state and depends
    /// only on the bundle itself, so every honest storer reaches the same
    /// verdict with no split-brain risk.
    ///
    /// **Reject-only**, per ADR-0004: no trust evidence is emitted, no audit
    /// is scheduled. The rejection is the consequence. The gate is
    /// rollout-gated by
    /// [`crate::replication::config::QUOTE_ARITHMETIC_RECHECK_ENABLED`]; when
    /// `false`, off-curve quotes are accepted and only telemetered
    /// ([`Self::log_off_curve_single_node`] /
    /// [`Self::log_off_curve_merkle`]), matching ADR-0004's observe-only
    /// rollout. Telemetry is invoked **after** ML-DSA-65 signature
    /// verification so unauthenticated senders cannot poison the rollout
    /// logs.
    fn validate_quote_arithmetic(payment: &ProofOfPayment) -> Result<()> {
        if !crate::replication::config::QUOTE_ARITHMETIC_RECHECK_ENABLED {
            return Ok(());
        }
        for (encoded_peer_id, quote) in &payment.peer_quotes {
            if let Some(detail) = Self::quote_arithmetic_violation(quote) {
                return Err(Error::Payment(format!(
                    "ADR-0004 off-curve quote rejected for peer {encoded_peer_id:?}: {detail}"
                )));
            }
        }
        Ok(())
    }

    /// The ADR-0004 forced-price rule for a single quote, returning a human
    /// diagnostic iff the quote violates it. Shared by single-node quotes and
    /// merkle candidates via [`Self::binding_violation`].
    ///
    /// The rule is the ADR's exact one — `price == calculate_price(
    /// committed_key_count)`, recomputed, never inverted from the price (which
    /// rounds) — PLUS a binding-shape check. There is no "legacy degradation"
    /// that infers an old quote from its field values: a `(0, None)` quote is
    /// rejected unless its price is exactly `calculate_price(0)`, closing the
    /// bypass where a modified node strips the pin yet prices above baseline.
    /// Old-format quotes (which never carried these fields) are tolerated only
    /// at the *wire-decode* layer, where they decode as `(0, None)` and are then
    /// held to the same baseline rule; an explicit version negotiation, not
    /// field inference, is the sanctioned path if non-baseline legacy quotes
    /// must ever be accepted.
    fn quote_arithmetic_violation(quote: &evmlib::PaymentQuote) -> Option<String> {
        Self::binding_violation(
            quote.committed_key_count,
            quote.commitment_pin,
            &quote.price,
        )
    }

    /// The shared ADR-0004 binding rule over a `(committed_key_count,
    /// commitment_pin, price)` triple, used for both quote types.
    ///
    /// Enforces, in order:
    /// 1. **Shape.** `(0, None)` baseline or `(n>0, Some(pin))` bound; the mixed
    ///    shapes `(n>0, None)` and `(0, Some(_))` are always rejected — a count
    ///    without a pin is unauditable, and a pin without a count is incoherent.
    /// 2. **Cap.** `committed_key_count <= MAX_COMMITMENT_KEY_COUNT`; a count a
    ///    commitment could never legitimately attest is rejected before pricing.
    /// 3. **Forced price.** `price == calculate_price(committed_key_count)`, by
    ///    exact recomputation.
    fn binding_violation(
        committed_key_count: u32,
        commitment_pin: Option<[u8; 32]>,
        price: &Amount,
    ) -> Option<String> {
        match (committed_key_count, commitment_pin.is_some()) {
            (0, false) | (1.., true) => {}
            (1.., false) => {
                return Some(format!(
                    "binding shape invalid: committed_key_count={committed_key_count} > 0 \
                     but commitment_pin is None (unauditable count)"
                ));
            }
            (0, true) => {
                return Some(
                    "binding shape invalid: committed_key_count=0 with a commitment_pin \
                     (incoherent baseline)"
                        .to_string(),
                );
            }
        }
        if committed_key_count > crate::replication::commitment::MAX_COMMITMENT_KEY_COUNT {
            return Some(format!(
                "committed_key_count={committed_key_count} exceeds MAX_COMMITMENT_KEY_COUNT={}",
                crate::replication::commitment::MAX_COMMITMENT_KEY_COUNT
            ));
        }
        let expected = calculate_price(Self::candidate_count_to_usize(u64::from(
            committed_key_count,
        )));
        if &expected == price {
            None
        } else {
            Some(format!(
                "price {price} does not equal calculate_price(committed_key_count={committed_key_count}) = {expected}"
            ))
        }
    }

    /// Pure ADR-0004 cross-check: compare a quote's claimed `(key_count, pin)`
    /// against a resolved signed commitment.
    ///
    /// This is the decision core of "peers cross-check the original": given a
    /// quote's binding and the actual `StorageCommitment` the pin was resolved
    /// to (from the sidecar, the gossip cache, or a fetch), decide whether the
    /// quote contradicts the commitment. It is deliberately a pure function over
    /// the two artifacts so it is exhaustively unit-testable without any cache,
    /// network, or trust wiring; the caller owns resolution and emission.
    ///
    /// Outcomes:
    /// - [`CrossCheck::Match`] — the pin matches the commitment's hash and the
    ///   counts agree: nothing to report.
    /// - [`CrossCheck::Mismatch`] — the pin matches the commitment's hash but
    ///   the quote's `committed_key_count` differs from the commitment's
    ///   `key_count`. Two artifacts signed by the same key contradict each
    ///   other: this is the deterministic, first-occurrence evidence.
    /// - [`CrossCheck::PinDoesNotResolve`] — the supplied commitment's hash does
    ///   not equal the quote's pin (wrong/garbled resolution). NOT evidence: the
    ///   caller must treat it as an unresolved pin (fetch/skip), never a
    ///   penalty, exactly like an unanswerable pin.
    ///
    /// A baseline quote `(0, None)` is never cross-checked (it pins nothing);
    /// callers skip it before reaching here.
    fn cross_check_binding(
        quoted_key_count: u32,
        quoted_pin: [u8; 32],
        commitment: &crate::replication::commitment::StorageCommitment,
    ) -> CrossCheck {
        // The pin IS the commitment hash; if the resolved commitment hashes to
        // something else, this is not the artifact the quote pinned.
        match crate::replication::commitment::commitment_hash(commitment) {
            Some(h) if h == quoted_pin => {
                if commitment.key_count == quoted_key_count {
                    CrossCheck::Match
                } else {
                    CrossCheck::Mismatch {
                        quoted_key_count,
                        committed_key_count: commitment.key_count,
                    }
                }
            }
            _ => CrossCheck::PinDoesNotResolve,
        }
    }

    /// Resolve a cached peer commitment record to its commitment *only if* it
    /// was seen within the answerability TTL; a staler entry is treated as
    /// unknown (ADR-0004: "a cached commitment older than the answerability TTL
    /// is treated as unknown"). Pure over `(record, now, ttl)` so the TTL
    /// boundary is unit-testable without the async cache/network path.
    fn fresh_cached_commitment(
        rec: &crate::replication::commitment_state::PeerCommitmentRecord,
        pin: [u8; 32],
        now: std::time::Instant,
        ttl: std::time::Duration,
    ) -> Option<crate::replication::commitment::StorageCommitment> {
        if now.saturating_duration_since(rec.received_at) >= ttl {
            return None; // stale cache entry -> treat as unknown
        }
        // Only resolve when the cached commitment is actually the one the quote
        // pinned. The auditor cache holds a peer's LATEST gossiped commitment,
        // which may be a DIFFERENT pin than this quote's; returning it would make
        // `cross_check_binding` yield `PinDoesNotResolve` and wrongly suppress
        // the fetch fallback for the quoted pin. A pin mismatch here means "not
        // cached" -> fall through to fetch.
        if rec.commitment_hash() != Some(pin) {
            return None;
        }
        rec.last_commitment().cloned()
    }

    /// Resolve a `(peer, pin)` from the gossip commitment cache, if the cache is
    /// wired and holds a fresh entry whose hash matches the pin. Shared by the
    /// single-node and merkle cross-check paths.
    async fn cache_resolve(
        cache: Option<&CommitmentCache>,
        peer_id: PeerId,
        pin: [u8; 32],
        now: std::time::Instant,
        ttl: std::time::Duration,
    ) -> Option<crate::replication::commitment::StorageCommitment> {
        let cache = cache?;
        let guard = cache.read().await;
        guard
            .get(&peer_id)
            .and_then(|rec| Self::fresh_cached_commitment(rec, pin, now, ttl))
    }

    /// Parse and validate ADR-0004 commitment sidecars into a `(peer, pin) ->
    /// commitment` map. Each blob is deserialized and held to the SAME gates as
    /// a gossip-ingested or fetched commitment (peer id derived from its own
    /// `sender_peer_id`, `BLAKE3(pubkey) == sender_peer_id`, valid signature),
    /// keyed by `(its own peer, its own hash)`. Resolution then matches a quote
    /// only when both the quote's peer AND pin equal the sidecar's, so a sidecar
    /// can never satisfy a different peer's or a different pin's quote. An
    /// unparseable or invalid sidecar is silently skipped (resolution falls back
    /// to gossip/fetch), never a hard error on the payment path.
    fn index_valid_sidecars(
        sidecars: &[Vec<u8>],
    ) -> HashMap<(PeerId, [u8; 32]), crate::replication::commitment::StorageCommitment> {
        use crate::replication::commitment::MAX_COMMITMENT_SIDECAR_BYTES;
        let mut map = HashMap::new();
        // Bound the number of sidecars we even look at: a legitimate bundle has
        // at most one commitment per quote/candidate. `MAX_SIDECARS_PER_BUNDLE`
        // (= CANDIDATES_PER_POOL, the larger of the two) caps the deserialize/
        // verify work a malicious client can force on the hot path.
        for blob in sidecars.iter().take(MAX_SIDECARS_PER_BUNDLE) {
            // Cap blob size before parsing: never attempt to deserialize an
            // oversized commitment.
            if blob.len() > MAX_COMMITMENT_SIDECAR_BYTES {
                continue;
            }
            let Ok(commitment) =
                rmp_serde::from_slice::<crate::replication::commitment::StorageCommitment>(blob)
            else {
                continue; // unparseable -> skip
            };
            let peer_id = PeerId::from_bytes(commitment.sender_peer_id);
            let Some(pin) = crate::replication::commitment::commitment_hash(&commitment) else {
                continue;
            };
            // Validate against its own (peer, pin): peer binding + pubkey
            // derivation + signature + hash==pin.
            if Self::fetched_commitment_is_valid(&commitment, &peer_id, pin) {
                map.insert((peer_id, pin), commitment);
            }
        }
        map
    }

    /// ADR-0004 "peers cross-check the original": for each non-baseline quote in
    /// a client-put bundle, resolve its `commitment_pin` and report a count/pin
    /// contradiction.
    ///
    /// Resolution order: the sidecar first (the commitment arrived with the
    /// quote, validated synchronously — no state, no network), then the gossip
    /// cache (only if the neighbour's commitment was seen within
    /// `GOSSIP_ANSWERABILITY_TTL`; a staler entry is treated as unknown, per the
    /// ADR's "cached commitment older than the answerability TTL is treated as
    /// unknown"), then an off-hot-path `GetCommitmentByPin` fetch for pins still
    /// unresolved. A pin that resolves nowhere is simply skipped — an unresolved
    /// pin is never a penalty.
    ///
    /// A genuine [`CrossCheck::Mismatch`] is a deterministic, first-occurrence
    /// contradiction between two same-key-signed artifacts: when enforcing, it
    /// emits [`FailureEvidence::QuoteCommitmentMismatch`] to the trust engine
    /// (same lane as a confirmed deterministic audit failure — NOT the timeout
    /// silence lane); when observe-only, it only logs. Always best-effort: a
    /// missing cache or absent `P2PNode` degrades to "resolve nothing", never an
    /// error on the payment path — the synchronous arithmetic gate and the
    /// later audit remain the load-bearing checks.
    async fn cross_check_quotes(
        &self,
        payment: &ProofOfPayment,
        commitment_sidecars: &[Vec<u8>],
        paid_peer: Option<[u8; 32]>,
    ) {
        let now = std::time::Instant::now();
        let ttl = crate::replication::commitment_state::GOSSIP_ANSWERABILITY_TTL;
        let p2p = self.p2p_node.read().as_ref().map(Arc::clone);
        let monetized_pin_tx = self.monetized_pin_tx.read().as_ref().cloned();
        let cache = self.commitment_cache.read().as_ref().map(Arc::clone);

        // ADR-0004 "the commitment arrived with the quote": parse and FULLY
        // validate the sidecars (peer/pubkey/signature/hash gates, keyed by
        // `(peer, pin)`), so the cross-check resolves synchronously without a
        // gossip-cache hit or a post-payment fetch. An invalid sidecar is simply
        // dropped (resolution falls back to gossip/fetch), never a hard error.
        let sidecar_map = Self::index_valid_sidecars(commitment_sidecars);

        // Inline pass: resolve from the sidecar first, then the gossip cache
        // (cheap, no network). Pins that don't resolve here are collected for
        // the off-hot-path fetch.
        let mut unresolved: Vec<(PeerId, [u8; 32], u32, Vec<u8>)> = Vec::new();
        for (encoded_peer_id, quote) in &payment.peer_quotes {
            let Some(pin) = quote.commitment_pin else {
                continue; // baseline quote pins nothing
            };
            let peer_id = PeerId::from_bytes(*encoded_peer_id.as_bytes());

            // ADR-0004 Amendment 2: only the PAID candidate's commitment earned
            // money, so only its pin is routed for a deterministic first audit
            // (the drainer dedups, rate-budgets, and respects the cooldown).
            // Pre-amendment this fired for every pinned quote in the bundle —
            // up to CLOSE_GROUP_SIZE audits per proof for peers that were never
            // paid — a 7x term in the fleet-wide amplification that degraded
            // v0.14.3. Best-effort: a closed channel just means no first audit
            // is scheduled, never an error on the payment path.
            let is_paid = paid_peer.is_some_and(|paid| paid == *encoded_peer_id.as_bytes());
            if is_paid {
                if let Some(ref tx) = monetized_pin_tx {
                    // Bounded queue: drop on full (best-effort, penalty-free;
                    // the peer's next settled payment re-nominates it). Count a
                    // Full drop so ingress saturation is observable; a Closed
                    // channel just means the engine is shutting down.
                    if let Err(tokio::sync::mpsc::error::TrySendError::Full(_)) =
                        tx.try_send(crate::replication::MonetizedPinEvent {
                            peer: peer_id,
                            pin,
                            key_count: quote.committed_key_count,
                            quote_ts: quote.timestamp,
                        })
                    {
                        crate::replication::note_monetized_ingress_drop();
                    }
                }
            }
            // Resolution order: sidecar (synchronous, no state) -> gossip cache
            // (fresh within TTL) -> fetch fallback (collected as unresolved).
            let resolved = match sidecar_map.get(&(peer_id, pin)) {
                Some(c) => Some(c.clone()),
                None => Self::cache_resolve(cache.as_ref(), peer_id, pin, now, ttl).await,
            };
            match resolved {
                Some(commitment) => {
                    let artifact = rmp_serde::to_vec(quote).unwrap_or_default();
                    Self::handle_cross_check(
                        &peer_id,
                        pin,
                        quote.committed_key_count,
                        artifact,
                        &commitment,
                        p2p.as_ref(),
                    )
                    .await;
                }
                None => unresolved.push((
                    peer_id,
                    pin,
                    quote.committed_key_count,
                    rmp_serde::to_vec(quote).unwrap_or_default(),
                )),
            }
        }

        // Off-hot-path fallback: fetch the unresolved pins via
        // `GetCommitmentByPin` and cross-check the results in a detached task,
        // so `verify_payment` does not block on the network.
        if unresolved.is_empty() {
            return;
        }
        let Some(p2p) = p2p else {
            return; // no P2P handle: cannot fetch, leave graced
        };
        let neg_cache = Arc::clone(&self.pin_fetch_negative_cache);
        tokio::spawn(async move {
            Self::drain_unresolved_pin_fetches(&p2p, &neg_cache, unresolved).await;
        });
    }

    /// Fetch each unresolved pin via `GetCommitmentByPin` and cross-check the
    /// result. Bounded at [`MAX_PIN_FETCHES_PER_BUNDLE`] per call, negatively
    /// cached per `(peer, pin)`, and graced on any miss/timeout. Shared by the
    /// single-node and merkle cross-check paths; meant to run in a detached task
    /// off the payment hot path.
    async fn drain_unresolved_pin_fetches(
        p2p: &Arc<P2PNode>,
        neg_cache: &PinFetchNegativeCache,
        unresolved: Vec<(PeerId, [u8; 32], u32, Vec<u8>)>,
    ) {
        let mut fetched = 0usize;
        for (peer_id, pin, quoted_key_count, artifact) in unresolved {
            if fetched >= crate::replication::config::MAX_PIN_FETCHES_PER_BUNDLE {
                debug!("ADR-0004 pin-fetch cap reached for this bundle; leaving rest graced");
                break;
            }
            // Skip pins already known-unresolvable for this peer.
            if neg_cache.lock().get(&(peer_id, pin)).is_some() {
                continue;
            }
            fetched += 1;
            match Self::fetch_commitment_by_pin(p2p, &peer_id, pin).await {
                Some(commitment) => {
                    Self::handle_cross_check(
                        &peer_id,
                        pin,
                        quoted_key_count,
                        artifact,
                        &commitment,
                        Some(p2p),
                    )
                    .await;
                }
                None => {
                    // NotRetained / timeout / malformed: graced (never a
                    // penalty), but remembered so we don't re-fetch.
                    neg_cache.lock().put((peer_id, pin), ());
                }
            }
        }
    }

    /// Apply the ADR-0004 cross-check verdict for one resolved `(peer, pin,
    /// quoted_count)` against `commitment`, emitting a trust failure on a
    /// genuine mismatch (when enforcing) or logging it (observe-only). Shared by
    /// the inline cache pass and the background fetch path so both reach the
    /// same verdict and emission.
    async fn handle_cross_check(
        peer_id: &PeerId,
        pin: [u8; 32],
        quoted_key_count: u32,
        quote_artifact: Vec<u8>,
        commitment: &crate::replication::commitment::StorageCommitment,
        p2p: Option<&Arc<P2PNode>>,
    ) {
        let CrossCheck::Mismatch {
            quoted_key_count,
            committed_key_count,
        } = Self::cross_check_binding(quoted_key_count, pin, commitment)
        else {
            return; // Match or PinDoesNotResolve: nothing to report
        };
        // The evidence is only meaningful if it carries the signed quote
        // artifact (one of the two contradicting same-key signatures). An empty
        // artifact — a re-serialization failure upstream — would produce
        // non-portable, unverifiable evidence, so grace it (log) instead of
        // emitting it: the deterministic first audit still convicts a genuine
        // inflater on the disk bytes.
        if quote_artifact.is_empty() {
            warn!(
                "ADR-0004 quote/commitment mismatch for {peer_id}: dropping evidence, \
                 quote artifact failed to serialize (graced; the audit still runs)"
            );
            return;
        }
        // Build the portable evidence variant — the two same-key-signed
        // artifacts that contradict each other, carried in full so any third
        // party can re-verify both signatures and recompute the contradiction.
        // This value IS the record; `emit_mismatch_evidence` turns it into the
        // trust action (or an observe-only log).
        let evidence = crate::replication::types::FailureEvidence::QuoteCommitmentMismatch {
            peer: *peer_id,
            pinned_commitment: pin,
            quoted_key_count,
            committed_key_count,
            quote_artifact,
            commitment: Box::new(commitment.clone()),
        };
        Self::emit_mismatch_evidence(&evidence, p2p).await;
    }

    /// Route a `QuoteCommitmentMismatch` evidence record: when enforcing, report
    /// it to the trust engine as a confirmed deterministic failure (an
    /// `ApplicationFailure` — same lane as a confirmed audit failure, NOT the
    /// timeout silence lane); when observe-only, only log it. Separated so the
    /// evidence→action mapping is unit-testable independent of resolution.
    async fn emit_mismatch_evidence(
        evidence: &crate::replication::types::FailureEvidence,
        p2p: Option<&Arc<P2PNode>>,
    ) {
        let crate::replication::types::FailureEvidence::QuoteCommitmentMismatch {
            peer,
            quoted_key_count,
            committed_key_count,
            ..
        } = evidence
        else {
            return; // only this variant is handled here
        };
        let enforce = crate::replication::config::QUOTE_COMMITMENT_MISMATCH_TRUST_ENABLED;
        if enforce {
            warn!(
                "ADR-0004 quote/commitment mismatch (enforcing) for {peer}: quote claims \
                 {quoted_key_count} keys but pinned commitment attests {committed_key_count}"
            );
            if let Some(p2p) = p2p {
                p2p.report_trust_event(
                    peer,
                    saorsa_core::TrustEvent::ApplicationFailure(
                        crate::replication::config::AUDIT_FAILURE_TRUST_WEIGHT,
                    ),
                )
                .await;
            }
        } else {
            warn!(
                "ADR-0004 quote/commitment mismatch observed (not enforcing) for {peer}: quote \
                 claims {quoted_key_count} keys but pinned commitment attests {committed_key_count}"
            );
        }
    }

    /// Fetch a peer's commitment by pin via `GetCommitmentByPin`, returning it
    /// only if the peer answered `Found` with a commitment that (a) is validly
    /// signed and peer-bound and (b) actually hashes to the requested pin.
    /// `None` on `NotRetained`, timeout, malformed, or any verification failure
    /// — all graced (the caller never penalises an unresolved pin).
    async fn fetch_commitment_by_pin(
        p2p: &Arc<P2PNode>,
        peer_id: &PeerId,
        pin: [u8; 32],
    ) -> Option<crate::replication::commitment::StorageCommitment> {
        use crate::replication::config::{PIN_FETCH_TIMEOUT, REPLICATION_PROTOCOL_ID};
        use crate::replication::protocol::{
            GetCommitmentByPin, GetCommitmentByPinResponse, ReplicationMessage,
            ReplicationMessageBody,
        };
        let msg = ReplicationMessage {
            request_id: 0,
            body: ReplicationMessageBody::GetCommitmentByPin(GetCommitmentByPin { pin }),
        };
        let encoded = msg.encode().ok()?;
        let resp = p2p
            .send_request(peer_id, REPLICATION_PROTOCOL_ID, encoded, PIN_FETCH_TIMEOUT)
            .await
            .ok()?;
        let decoded = ReplicationMessage::decode(&resp.data).ok()?;
        let ReplicationMessageBody::GetCommitmentByPinResponse(GetCommitmentByPinResponse::Found {
            commitment,
        }) = decoded.body
        else {
            return None; // NotRetained / unexpected -> graced
        };
        Self::fetched_commitment_is_valid(&commitment, peer_id, pin).then_some(commitment)
    }

    /// The untrusted-fetched-commitment validation gates, pure over
    /// `(commitment, peer_id, pin)` so they are unit-testable. A fetched
    /// commitment is accepted only if it passes the SAME gates as a gossip
    /// ingest, so a peer cannot answer with another peer's (validly signed)
    /// commitment and have it pass as its own:
    ///   (a) it is bound to THIS peer (`sender_peer_id == peer_id`),
    ///   (b) the embedded pubkey derives that peer id (`BLAKE3(pk) == id`),
    ///   (c) its signature is valid (binds the pubkey),
    ///   (d) it actually hashes to the pin we asked for.
    fn fetched_commitment_is_valid(
        commitment: &crate::replication::commitment::StorageCommitment,
        peer_id: &PeerId,
        pin: [u8; 32],
    ) -> bool {
        commitment.sender_peer_id == *peer_id.as_bytes()
            && *blake3::hash(&commitment.sender_public_key).as_bytes() == commitment.sender_peer_id
            && crate::replication::commitment::verify_commitment_signature(commitment)
            && crate::replication::commitment::commitment_hash(commitment) == Some(pin)
    }

    /// Single-node telemetry for off-curve quotes. Always returns; never
    /// errors. MUST be called only after ML-DSA-65 signature verification has
    /// passed, so unauthenticated peers cannot drive log volume.
    fn log_off_curve_single_node(payment: &ProofOfPayment) {
        if crate::replication::config::QUOTE_ARITHMETIC_RECHECK_ENABLED {
            return; // enforce mode already rejected; no separate telemetry.
        }
        for (encoded_peer_id, quote) in &payment.peer_quotes {
            if let Some(detail) = Self::quote_arithmetic_violation(quote) {
                warn!(
                    "ADR-0004 off-curve single-node quote observed (not enforcing): \
                     peer {encoded_peer_id:?} {detail}"
                );
            }
        }
    }

    /// ADR-0004 sister gate for the merkle batch path: every candidate's
    /// `price` field must lie on the pricing curve, by exact recomputation.
    /// See [`Self::validate_quote_arithmetic`] for the rationale; semantics
    /// (reject-only, rollout-gated, no trust evidence) are identical.
    fn validate_merkle_candidate_arithmetic(
        pool: &evmlib::merkle_payments::MerklePaymentCandidatePool,
    ) -> Result<()> {
        if !crate::replication::config::QUOTE_ARITHMETIC_RECHECK_ENABLED {
            return Ok(());
        }
        for candidate in &pool.candidate_nodes {
            if let Some(detail) = Self::binding_violation(
                candidate.committed_key_count,
                candidate.commitment_pin,
                &candidate.price,
            ) {
                return Err(Error::Payment(format!(
                    "ADR-0004 merkle candidate rejected (reward {}): {detail}",
                    candidate.reward_address
                )));
            }
        }
        Ok(())
    }

    /// Merkle batch telemetry for off-curve candidates. Always returns; never
    /// errors. MUST be called only after ML-DSA-65 signature verification has
    /// passed.
    fn log_off_curve_merkle(pool: &evmlib::merkle_payments::MerklePaymentCandidatePool) {
        if crate::replication::config::QUOTE_ARITHMETIC_RECHECK_ENABLED {
            return; // enforce mode already rejected; no separate telemetry.
        }
        for candidate in &pool.candidate_nodes {
            if let Some(detail) = Self::binding_violation(
                candidate.committed_key_count,
                candidate.commitment_pin,
                &candidate.price,
            ) {
                warn!(
                    "ADR-0004 merkle candidate violation observed (not enforcing): \
                     reward {} {detail}",
                    candidate.reward_address
                );
            }
        }
    }

    /// ADR-0008 merkle payment-parity telemetry.
    ///
    /// Records what a batch actually settled per paid node against what
    /// single-node parity requires, and which regime the receipt's timestamp put
    /// it in. Parity is enforced, so a post-boundary shortfall has already been
    /// rejected before this runs and cannot appear here; what this measures is
    /// how much traffic is still arriving on pre-boundary (1x) receipts, which
    /// is what tells us the compatibility window can be considered closed.
    /// Observational only: it never rejects and never feeds trust scoring — a
    /// client on a legacy receipt is running old code, not misbehaving.
    ///
    /// Two call-site requirements, both load-bearing for the signal:
    /// - **After full admission validation.** Every paid index, reward address
    ///   and amount must already have been checked. Logging earlier lets a
    ///   proof that is rejected moments later enter the rollout signal, which
    ///   would understate parity with samples that never resulted in a store.
    /// - **Store admissions only.** A paid-list admission reprices nothing and
    ///   would double-count a batch already measured when it was stored.
    ///
    /// Keyed by `pool_hash`, not by chunk address: one on-chain settlement
    /// covers up to 256 chunks, so a line per chunk would emit 256 duplicate
    /// samples of one economic event and let a big batch outvote a small one.
    ///
    /// Carrying the pool hash in the line is not enough on its own — it makes
    /// the stream *deduplicable* but leaves the emitted cardinality at one line
    /// per chunk. So the first emission for a pool is recorded in
    /// [`Self::merkle_parity_logged`] and later admissions from the same batch
    /// return without logging. The check-and-insert is a single locked
    /// operation, so concurrent admissions from one batch also produce exactly
    /// one line. "Once per pool" is per node/verifier: every storer that admits
    /// a chunk from the batch still contributes its own sample, which is what
    /// makes the signal a measure of traffic reaching the fleet.
    fn log_merkle_parity(
        &self,
        pool_hash: PoolHash,
        payment_info: &OnChainPaymentInfo,
        expected_bare: Amount,
        expected_parity: Amount,
        required_multiplier: u64,
    ) {
        // Check-and-insert under one lock: the loser of a race sees its own
        // insert report a prior entry and stays quiet.
        if self
            .merkle_parity_logged
            .lock()
            .put(pool_hash, ())
            .is_some()
        {
            return;
        }
        self.merkle_parity_emissions
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);

        let min_paid = payment_info
            .paid_node_addresses
            .iter()
            .map(|(_, _, amount)| *amount)
            .min()
            .unwrap_or(Amount::ZERO);
        let at_parity = min_paid >= expected_parity;
        let legacy_receipt = required_multiplier < PAID_QUOTE_PAYMENT_MULTIPLIER;

        info!(
            target: "ant_node::payment::merkle_parity",
            "Merkle parity: pool={}, depth={}, paid_nodes={}, receipt_ts={}, \
             min_paid={min_paid}, expected_bare={expected_bare}, \
             expected_parity={expected_parity}, required_multiplier={required_multiplier}, \
             legacy_receipt={legacy_receipt}, at_parity={at_parity}",
            hex::encode(pool_hash),
            payment_info.depth,
            payment_info.paid_node_addresses.len(),
            payment_info.merkle_payment_timestamp,
        );
    }

    /// Pure curve-canonicality predicate: does `price` lie exactly on the
    /// pricing curve? Equivalent to "there exists some non-negative integer
    /// `n` such that `calculate_price(n) == price`".
    ///
    /// Separated from the rollout-gated outer gates so the canonicality rule
    /// itself is unit-testable independent of the gate. Callers MUST use this
    /// and not `derive_records_stored_from_price` directly: the latter floors
    /// and is not a canonicality test.
    ///
    /// Saturation: `derive_records_stored_from_price` saturates to `u64::MAX`
    /// for prices beyond `calculate_price(u64::MAX)`, and
    /// [`Self::candidate_count_to_usize`] saturates to `usize::MAX` on 32-bit
    /// targets. Both saturation regimes converge on `calculate_price`'s own
    /// saturation ceiling; an honest in-range price (which can never approach
    /// these regions — `MAX_COMMITMENT_KEY_COUNT` is `1_000_000`) round-trips
    /// exactly.
    #[allow(dead_code)] // boolean convenience for tests + follow-up slices
    fn quote_price_is_on_curve(price: &Amount) -> bool {
        Self::price_off_curve_diagnostics(price).is_none()
    }

    /// Returns `Some((candidate_count, recomputed))` iff `price` is off-curve;
    /// `None` iff `price` is on-curve. The tuple is the diagnostic detail used
    /// by both the rejection error message and the telemetry warning.
    fn price_off_curve_diagnostics(price: &Amount) -> Option<(u64, Amount)> {
        let candidate_count = derive_records_stored_from_price(*price);
        let recomputed = calculate_price(Self::candidate_count_to_usize(candidate_count));
        if recomputed == *price {
            None
        } else {
            Some((candidate_count, recomputed))
        }
    }

    /// Narrow the canonicality predicate's `u64` candidate into `usize` for
    /// [`calculate_price`]. On every 64-bit target (the only supported
    /// production target) this is the identity; on 32-bit targets we saturate
    /// to `usize::MAX`, which matches `calculate_price`'s own
    /// `Amount::saturating_mul` behaviour so the round-trip still terminates
    /// in the same saturation regime rather than panicking.
    fn candidate_count_to_usize(candidate_count: u64) -> usize {
        usize::try_from(candidate_count).unwrap_or(usize::MAX)
    }

    /// Minimum number of candidate `pub_keys` (out of 16) whose derived
    /// `PeerId` must be among the DHT's actual closest peers to the pool
    /// midpoint address for the pool to be accepted.
    ///
    /// Set to a simple majority (9/16). Two nodes' views of the closest set
    /// to a midpoint diverge on a young, high-churn, NAT-heavy network — by
    /// more than a near-unanimous threshold tolerates — so a stricter bar
    /// rejected honest pools whose candidates are genuinely drawn from the
    /// midpoint's close group but don't all reappear in this storer's own
    /// lookup. A majority absorbs that divergence while still requiring most
    /// candidates to be real peers the live DHT lists as closest.
    ///
    /// Security cost: a lower threshold widens the room for the "pay-yourself"
    /// attack — an attacker running real neighbourhood peers needs fewer of
    /// them to clear a majority than to clear a near-unanimous bar. No theft
    /// of funds is possible regardless (payment binds on-chain to the rewards
    /// address); the cost is that grinding storage payments back to your own
    /// nodes gets cheaper. Each counted candidate must still be a peer the
    /// live DHT actually returns as closest — a fabricated off-network key
    /// cannot satisfy this — so the floor is "run N real top-K Sybil nodes
    /// AND grind the midpoint", just with a smaller N. Pairs with the planned
    /// pool-midpoint consensus-anchor work, which removes the midpoint
    /// grinding freedom that makes a low threshold dangerous.
    const CANDIDATE_CLOSENESS_REQUIRED: usize = 9;

    /// Timeout for the authoritative network lookup used by the closeness
    /// check.
    ///
    /// Iterative Kademlia lookups can cascade through `MAX_ITERATIONS = 20`
    /// rounds in saorsa-core's `find_closest_nodes_network`, and a single
    /// unresponsive peer's dial can take 20–30s before timing out. On a
    /// young network (e.g. fresh testnet, NAT-simulated peers in 30% of
    /// the swarm) iterations average ~10s each — captured trace from
    /// STG-01 EWR-3 ant-node-1 just before a pre-fix timeout:
    ///
    /// ```text
    /// Iter 0: +0.0s | Iter 1: +0.2s | Iter 2: +6.6s | Iter 3: +13.1s
    /// Iter 4: +20.9s | Iter 5: +39.8s | Iter 6: +50.8s | [60s wall]
    /// ```
    ///
    /// 60s caps the lookup at ~7 iterations and rejects honest pools whose
    /// candidates only emerge after iteration 7. 240s gives ~1.2× headroom
    /// over the ~200s natural worst-case runtime on a 1k-node testnet.
    ///
    /// `DoS` amplification stays bounded at roughly one in-flight lookup
    /// per unique `pool_hash` under typical load, via
    /// [`closeness_pass_cache`] + [`inflight_closeness`]. The bound is
    /// "typical" because `inflight_closeness` is an LRU and a sustained
    /// flood of unique `pool_hash` entries can evict an in-flight slot,
    /// at which point a second leader can race for the same pool (see
    /// [`InflightGuard::drop`]). At steady state the pool cache and pool
    /// signature verification gate keep this rare in practice.
    const CLOSENESS_LOOKUP_TIMEOUT: std::time::Duration = std::time::Duration::from_mins(4);

    /// Width of the storer's authoritative network lookup, in peers.
    ///
    /// The client over-queries `2 * CANDIDATES_PER_POOL = 32` peers via
    /// `find_closest_peers(addr, 32)` (see
    /// `ant-client/ant-core/src/data/client/merkle.rs::get_merkle_candidate_pool`)
    /// and selects 16 valid responders by XOR distance — so truly-close
    /// peers that are slow, NAT'd, or briefly unreachable get filtered
    /// out and replaced by peers from positions 17–32 of the network's
    /// actual ranking. The storer must therefore verify against the same
    /// wider window: a pool containing peers from positions 17–32 is
    /// honest (those peers really exist in the network's closest-32 set),
    /// it's just that the client's quote-collection step couldn't reach
    /// the peers at positions <17 in time.
    ///
    /// Empirical effect on STG-01 (1k-node testnet, 30% NAT-simulated):
    /// widening from K=16 to K=32 dropped client-side closeness
    /// mismatches from ~115 to ~31 per 5 min, a 73% reduction.
    ///
    /// Performance note: `count` does not just truncate the lookup —
    /// `find_closest_nodes_network` keeps iterating until either
    /// `MAX_ITERATIONS` is reached or `best_nodes.len() >= count`. K=32
    /// can therefore extend lookups by a few iterations on sparse
    /// networks vs K=16, which reinforces (rather than undermines) the
    /// timeout bump above.
    ///
    /// Security: the pay-yourself attack still requires the attacker's
    /// fabricated `PeerId`s to land in the storer's authoritative top-K, so
    /// the dominant cost is Sybil-grinding midpoint addresses or running real
    /// nodes near the target. The leniency for honest divergence comes from
    /// the `CANDIDATE_CLOSENESS_REQUIRED` majority threshold, not from this
    /// window; widening the window further was measured as too heavy on the
    /// lookup path.
    const CLOSENESS_LOOKUP_WIDTH: usize = 2 * evmlib::merkle_payments::CANDIDATES_PER_POOL;

    /// Maximum waiter → leader retries when the leader's future was cancelled
    /// or panicked before publishing a result. Beyond this the waiter returns
    /// a visible error rather than spinning indefinitely through a
    /// cancellation cascade.
    ///
    /// Worst-case waiter wall-clock is `(MAX_LEADER_RETRIES + 1) *
    /// CLOSENESS_LOOKUP_TIMEOUT` (one wait per attempt). Kept low (1)
    /// because the only realistic trigger is leader future-cancellation,
    /// which should be extraordinarily rare; under sustained adversarial
    /// cancellation a higher cap doesn't add resilience, it just hides
    /// the symptom. With `CLOSENESS_LOOKUP_TIMEOUT = 240s` this caps a
    /// single user-visible verification at ~8 min worst case (vs ~20 min
    /// at the previous value of 4).
    const MAX_LEADER_RETRIES: usize = 1;

    /// Compute the storer's authoritative-lookup width for a candidate pool.
    ///
    /// Returns `max(CLOSENESS_LOOKUP_WIDTH, pool_len)`: matches the client's
    /// over-query width today, and scales with the pool if a future protocol
    /// bump grows pool size beyond `CLOSENESS_LOOKUP_WIDTH`. Truncating to
    /// `CLOSENESS_LOOKUP_WIDTH` in that future case would re-open the
    /// K-too-small failure mode (the storer would reject honest pools whose
    /// candidates legitimately span a wider XOR range than the storer
    /// fetched). Pinned by `closeness_lookup_count_uses_max_of_width_and_pool_len`.
    const fn closeness_lookup_count(pool_len: usize) -> usize {
        if Self::CLOSENESS_LOOKUP_WIDTH > pool_len {
            Self::CLOSENESS_LOOKUP_WIDTH
        } else {
            pool_len
        }
    }

    /// Verify that the candidate pool's `pub_keys` correspond to peers that
    /// are actually XOR-closest to the pool midpoint address, by querying
    /// the DHT for its closest peers to that address and requiring that a
    /// majority of the candidates match.
    ///
    /// **What this blocks**: the "pay yourself" attack. Candidate signatures
    /// only cover `(price, reward_address, timestamp)` and the `pub_key` bytes —
    /// nothing ties a candidate to a network-registered identity or to the
    /// pool neighbourhood. Without this check an attacker can generate 16
    /// ML-DSA keypairs locally, point all 16 `reward_address` fields at a
    /// single attacker-controlled wallet, submit the merkle payment, and drain
    /// their own payment back out.
    ///
    /// **How it blocks**: each candidate's `PeerId = BLAKE3(pub_key)`; the DHT
    /// is the authoritative source of "which peers exist at this XOR
    /// coordinate". If the attacker's 16 fabricated `PeerId`s are not among
    /// the peers the network actually lists as closest to the pool address,
    /// the pool is forged.
    ///
    /// **Scope**: a `MerklePaymentProof` carries exactly one `winner_pool`
    /// (the pool the smart contract selected for the batch). Every storing
    /// node that receives the proof independently re-runs this check against
    /// that same pool, so a forged pool is rejected at every node it
    /// reaches.
    ///
    /// **Known limitation — Sybil-grinding**: `midpoint_proof.address()` is a
    /// BLAKE3 hash of attacker-controllable inputs (leaf bytes, tree root,
    /// timestamp). A determined attacker who *also* runs Sybil DHT nodes can
    /// grind the midpoint until it lands in a region where a majority of
    /// their Sybil keys are the true network-closest — at which point this check
    /// passes for the attacker. Closing that gap requires binding the
    /// midpoint to an attacker-uncontrolled value (e.g. a block hash at
    /// payment time or an on-chain VRF) or a Sybil-resistant identity
    /// layer. This defence raises the attack cost from "free" to "run N
    /// Sybil nodes AND grind", which is a meaningful but not complete
    /// improvement.
    async fn verify_merkle_candidate_closeness(
        &self,
        pool: &evmlib::merkle_payments::MerklePaymentCandidatePool,
        pool_hash: PoolHash,
    ) -> Result<()> {
        // Fast path: this node already verified this pool successfully.
        // A batch of 256 chunks shares one winner_pool, so without this cache
        // we'd pay a Kademlia lookup per chunk.
        if self.closeness_pass_cache.lock().get(&pool_hash).is_some() {
            return Ok(());
        }

        // Single-flight: on each attempt, either claim leadership by
        // inserting a fresh `ClosenessSlot`, or wait on an existing leader
        // and read its published result. The leader holds an `Arc` to the
        // slot independent of the LruCache so waiters are still woken if
        // eviction pressure kicked the cache entry.
        //
        // The `notified_owned()` future snapshots the `notify_waiters`
        // counter at the moment of construction (while we hold the lock),
        // which makes the subsequent `.await` race-free: if the leader
        // calls `notify_waiters` between our construction and our poll, the
        // counter has advanced and the future resolves immediately on first
        // poll.
        //
        // Bounded retry: if we're a waiter and the leader gets cancelled or
        // panics (slot.result.get() == None after wake-up), we loop back to
        // claim leadership. `MAX_LEADER_RETRIES` bounds the attempts so
        // adversarial cancellation cascades cannot spin this indefinitely.
        for attempt in 0..=Self::MAX_LEADER_RETRIES {
            // Release the mutex guard explicitly before any await below.
            // Clippy wants `if let ... else` written as `map_or_else`, but
            // any such rewrite re-borrows the locked `inflight` inside the
            // closure and fails the borrow checker — so the lint is
            // silenced here.
            #[allow(clippy::option_if_let_else)]
            let (waiter_slot, leader_slot) = {
                let mut inflight = self.inflight_closeness.lock();
                let chosen = if let Some(existing) = inflight.get(&pool_hash) {
                    (Some(Arc::clone(existing)), None)
                } else {
                    let slot = Arc::new(ClosenessSlot::new());
                    inflight.put(pool_hash, Arc::clone(&slot));
                    (None, Some(slot))
                };
                drop(inflight);
                chosen
            };

            if let Some(slot) = waiter_slot {
                // Build the owned-notified future BEFORE awaiting, so it
                // snapshots the `notify_waiters` counter now. The slot
                // already existed when we locked, so the leader is either
                // running or finished; in both cases the snapshot + counter
                // check ensures we wake up correctly.
                let notified = slot.notified_owned();
                notified.await;

                // Leader published a result — use it directly.
                if let Some(result) = slot.result.get() {
                    return result.clone().map_err(Error::Payment);
                }
                // Leader disappeared without publishing (panic or
                // cancellation). Slot was cleared by the leader's drop
                // guard; loop to become the new leader — unless we've
                // hit the retry bound (see MAX_LEADER_RETRIES).
                if attempt == Self::MAX_LEADER_RETRIES {
                    return Err(Error::Payment(
                        "Merkle candidate pool rejected: closeness leader \
                         repeatedly failed to publish a result (likely \
                         repeated cancellation or panic)."
                            .into(),
                    ));
                }
                continue;
            }

            // Leader path. Drop guard clears the slot and wakes waiters on
            // every exit (success, failure, panic, cancellation).
            let Some(slot) = leader_slot else {
                // Unreachable by construction.
                return Err(Error::Payment(
                    "internal error: neither leader nor waiter in closeness check".into(),
                ));
            };
            let guard = InflightGuard {
                slot_cache: &self.inflight_closeness,
                pool_hash,
                slot,
            };

            let result = self.verify_merkle_candidate_closeness_inner(pool).await;
            guard.publish(&result);
            if result.is_ok() {
                self.closeness_pass_cache.lock().put(pool_hash, ());
            }
            return result;
        }
        // Unreachable: the for-loop body always either `return`s or `continue`s,
        // and the waiter branch's `continue` only runs when `attempt <
        // Self::MAX_LEADER_RETRIES`. The last iteration's waiter branch returns
        // via the retry-bound check; the leader branch always returns.
        Err(Error::Payment(
            "internal error: closeness retry loop exited without returning".into(),
        ))
    }

    /// Inner closeness check: the actual DHT lookup + set-membership test.
    /// Wrapped by [`verify_merkle_candidate_closeness`] with a pass-cache and
    /// single-flight guard so a batch of chunks and a storm of forged PUTs
    /// don't multiply the lookup cost.
    /// Derive each candidate's `PeerId` from its `pub_key` and reject the
    /// pool if any `PeerId` appears more than once.
    ///
    /// This is a pure-validation pre-check, runnable without a `P2PNode`:
    /// catches the case where one real peer's `pub_key` is repeated to
    /// inflate the closeness match count, without paying for a Kademlia
    /// lookup. An honest pool has [`evmlib::merkle_payments::CANDIDATES_PER_POOL`]
    /// distinct candidate `pub_keys` by construction.
    fn derive_distinct_candidate_peer_ids(
        pool: &evmlib::merkle_payments::MerklePaymentCandidatePool,
    ) -> Result<Vec<PeerId>> {
        let mut candidate_peer_ids = Vec::with_capacity(pool.candidate_nodes.len());
        let mut seen = std::collections::HashSet::with_capacity(pool.candidate_nodes.len());
        for candidate in &pool.candidate_nodes {
            let pid = peer_id_from_public_key_bytes(&candidate.pub_key).map_err(|e| {
                Error::Payment(format!(
                    "Invalid ML-DSA public key in merkle candidate: {e}"
                ))
            })?;
            if !seen.insert(pid) {
                return Err(Error::Payment(
                    "Merkle candidate pool rejected: duplicate candidate PeerId. An \
                     honest pool has 16 distinct candidate pub_keys; duplicates would \
                     let a single real peer satisfy the closeness threshold by being \
                     counted multiple times."
                        .into(),
                ));
            }
            candidate_peer_ids.push(pid);
        }
        Ok(candidate_peer_ids)
    }

    /// Pure-logic closeness check: given the pool's candidate peer IDs and
    /// the storer's authoritative network view (closest peers to the pool
    /// midpoint), decide whether the pool passes the
    /// `CANDIDATE_CLOSENESS_REQUIRED`-of-N threshold.
    ///
    /// A candidate counts only if its `PeerId` is one of the peers the
    /// storer's own network lookup returned (exact set membership). This is
    /// the property that makes the gate meaningful: a passing candidate must
    /// be a real, reachable peer the live DHT actually routes to and lists
    /// among the closest — it cannot be a key fabricated off-network. The
    /// leniency in this check is purely the lowered threshold (a majority
    /// rather than near-unanimity), which tolerates the closest-set
    /// divergence between two nodes' views without admitting fabricated keys.
    ///
    /// Extracted from `verify_merkle_candidate_closeness_inner` so tests
    /// can exercise the matching logic without standing up a real DHT.
    /// Mirrors the runtime path exactly: same sparse-network short-circuit,
    /// same set-membership check, same error strings.
    fn check_closeness_match(
        candidate_peer_ids: &[PeerId],
        network_peer_ids: &[PeerId],
        pool_address: &[u8; 32],
    ) -> Result<()> {
        // Sparse-network short-circuit: if the DHT itself returned fewer
        // peers than the closeness threshold, the proof can never pass —
        // not because the candidates are forged, but because we don't
        // have an authoritative view to compare against. Surface this
        // distinct cause so operators can tell "retry once the network
        // settles" apart from "this peer sent a forged pool".
        if network_peer_ids.len() < Self::CANDIDATE_CLOSENESS_REQUIRED {
            debug!(
                "Merkle closeness deferred: network lookup returned {} peers \
                 for pool midpoint {} (need at least {} to verify)",
                network_peer_ids.len(),
                hex::encode(pool_address),
                Self::CANDIDATE_CLOSENESS_REQUIRED,
            );
            return Err(Error::Payment(format!(
                "Merkle candidate pool rejected: authoritative DHT lookup returned \
                 only {} peers, less than the {} required to verify candidate \
                 closeness. Retry once the routing table populates further.",
                network_peer_ids.len(),
                Self::CANDIDATE_CLOSENESS_REQUIRED,
            )));
        }

        // Exact-match membership against the returned closest peers.
        // Candidate `PeerId`s are deduplicated upstream, so each match
        // corresponds to a distinct peer.
        let network_set: std::collections::HashSet<PeerId> =
            network_peer_ids.iter().copied().collect();
        let matched = candidate_peer_ids
            .iter()
            .filter(|pid| network_set.contains(pid))
            .count();

        if matched < Self::CANDIDATE_CLOSENESS_REQUIRED {
            debug!(
                "Merkle closeness rejected: {matched}/{} candidates match the DHT's closest peers \
                 for pool midpoint {} (required: {}, network returned {} peers)",
                candidate_peer_ids.len(),
                hex::encode(pool_address),
                Self::CANDIDATE_CLOSENESS_REQUIRED,
                network_peer_ids.len(),
            );
            return Err(Error::Payment(
                "Merkle candidate pool rejected: candidate pub_keys do not match the \
                 network's closest peers to the pool midpoint address. Pools must be \
                 collected from the pool-address close group, not fabricated off-network."
                    .into(),
            ));
        }

        debug!(
            "Merkle closeness passed: {matched}/{} candidates matched the DHT's closest peers \
             for pool midpoint {}",
            candidate_peer_ids.len(),
            hex::encode(pool_address),
        );
        Ok(())
    }

    #[allow(clippy::too_many_lines)]
    async fn verify_merkle_candidate_closeness_inner(
        &self,
        pool: &evmlib::merkle_payments::MerklePaymentCandidatePool,
    ) -> Result<()> {
        // Pre-check: catch malformed/hostile pools (duplicate candidate
        // PeerIds) before paying for the Kademlia lookup. Runs in unit
        // tests without a P2PNode too.
        let candidate_peer_ids = Self::derive_distinct_candidate_peer_ids(pool)?;

        // Release the RwLock guard before any await to avoid holding it
        // across an iterative Kademlia lookup.
        let attached = self.p2p_node.read().as_ref().map(Arc::clone);
        let Some(p2p_node) = attached else {
            // Production must call attach_p2p_node at startup. Fail CLOSED
            // to avoid silently disabling the defence if a startup path
            // regresses and loses the attach call. Unit-test builds that
            // construct a PaymentVerifier directly without exercising merkle
            // verification are opted-in via `test-utils` to fall back to
            // fail-open.
            #[cfg(any(test, feature = "test-utils"))]
            {
                crate::logging::warn!(
                    "PaymentVerifier: no P2PNode attached; merkle pay-yourself \
                     defence SKIPPED (test build). Production startup MUST call \
                     PaymentVerifier::attach_p2p_node."
                );
                return Ok(());
            }
            #[cfg(not(any(test, feature = "test-utils")))]
            {
                crate::logging::error!(
                    "PaymentVerifier: no P2PNode attached; rejecting merkle \
                     payment. This is a node-startup bug — \
                     PaymentVerifier::attach_p2p_node must be called before \
                     any PUT handler runs."
                );
                return Err(Error::Payment(
                    "Merkle candidate pool rejected: verifier is not wired to \
                     the P2P layer; cannot verify candidate closeness."
                        .into(),
                ));
            }
        };

        let pool_address = pool.midpoint_proof.address();
        // Match the client's over-query width. The client's
        // `get_merkle_candidate_pool` queries 2 × `CANDIDATES_PER_POOL` peers
        // and picks the 16 closest *valid responders* — so legitimate pools
        // routinely include peers from positions 17–32 of the network's true
        // ranking when the closer peers are slow or NAT-stuck. The storer
        // must look at the same window or it will reject honest pools with
        // no security benefit.
        //
        // `pool.candidate_nodes` is currently a fixed-size array of length
        // `CANDIDATES_PER_POOL` (= 16), so `.max(...)` always evaluates to
        // `CLOSENESS_LOOKUP_WIDTH` today. The compile-time
        // `const _: () = assert!(WIDTH >= CANDIDATES_PER_POOL)` in the test
        // module pins that invariant. The `.max(...)` form is belt-and-braces
        // for a hypothetical future protocol that grows pool size to a
        // `Vec`-typed candidate set: the storer would scale its lookup with
        // the pool rather than truncating, which would otherwise re-open the
        // K-too-small failure mode.
        let lookup_count = Self::closeness_lookup_count(pool.candidate_nodes.len());
        let network_lookup = p2p_node
            .dht_manager()
            .find_closest_nodes_network(&pool_address.0, lookup_count);
        let network_peers =
            match tokio::time::timeout(Self::CLOSENESS_LOOKUP_TIMEOUT, network_lookup).await {
                Ok(Ok(peers)) => peers,
                Ok(Err(e)) => {
                    debug!(
                        "Merkle closeness network-lookup failed for pool midpoint {}: {e}",
                        hex::encode(pool_address.0),
                    );
                    return Err(Error::Payment(
                        "Merkle candidate pool rejected: could not verify candidate \
                     closeness against the authoritative network view."
                            .into(),
                    ));
                }
                Err(_) => {
                    debug!(
                        "Merkle closeness network-lookup timeout ({:?}) for pool midpoint {}",
                        Self::CLOSENESS_LOOKUP_TIMEOUT,
                        hex::encode(pool_address.0),
                    );
                    return Err(Error::Payment(
                        "Merkle candidate pool rejected: authoritative network lookup \
                     timed out. Retry once the network lookup completes."
                            .into(),
                    ));
                }
            };

        let network_peer_ids: Vec<PeerId> = network_peers.iter().map(|n| n.peer_id).collect();
        Self::check_closeness_match(&candidate_peer_ids, &network_peer_ids, &pool_address.0)
    }

    /// Verify a merkle batch payment proof.
    ///
    /// This verification flow:
    /// 1. Deserialize the `MerklePaymentProof`
    /// 2. Check pool cache for previously verified pool hash
    /// 3. If not cached, query on-chain for payment info
    /// 4. Validate the proof against on-chain data
    /// 5. Cache the pool hash for subsequent chunk verifications in the same batch
    #[allow(clippy::too_many_lines)]
    async fn verify_merkle_payment(
        &self,
        target: &PaymentTarget,
        proof_bytes: &[u8],
        context: VerificationContext,
    ) -> Result<()> {
        // The proof names what was paid for, which for a pointer is its state
        // rather than its address.
        let xorname = target.content();

        // A merkle proof binds the paid address but carries no issuer-proximity
        // check, so it cannot express "paid at the state, quoted by the group
        // around the address". Accepting one for a pointer would check the
        // close group of a state identifier, which names no group at all.
        // Single-node proofs do carry that check, so pointers use those until
        // the merkle proof shape can say which group issued it.
        if target.routing() != target.content() {
            return Err(Error::Payment(format!(
                "a pointer update must be paid with a single-node proof: a merkle \
                 proof cannot bind the issuing close group of {} to the paid state {}",
                hex::encode(target.routing()),
                hex::encode(target.content())
            )));
        }
        if crate::logging::enabled!(crate::logging::Level::DEBUG) {
            debug!(
                "Verifying merkle payment for {} ({context:?})",
                hex::encode(xorname)
            );
        }

        // Deserialize the merkle proof
        let merkle_proof = deserialize_merkle_proof(proof_bytes)
            .map_err(|e| Error::Payment(format!("Failed to deserialize merkle proof: {e}")))?;

        // Verify the address in the proof matches the xorname being stored
        if merkle_proof.address.0 != *xorname {
            let proof_hex = hex::encode(merkle_proof.address.0);
            let store_hex = hex::encode(xorname);
            return Err(Error::Payment(format!(
                "Merkle proof address mismatch: proof is for {proof_hex}, but storing {store_hex}"
            )));
        }

        let pool_hash = merkle_proof.winner_pool_hash();

        // Run cheap local checks BEFORE expensive on-chain queries.
        // This prevents DoS via garbage proofs that trigger RPC lookups.
        for candidate in &merkle_proof.winner_pool.candidate_nodes {
            if !crate::payment::verify_merkle_candidate_signature(candidate) {
                return Err(Error::Payment(format!(
                    "Invalid ML-DSA-65 signature on merkle candidate node (reward: {})",
                    candidate.reward_address
                )));
            }
        }

        // ADR-0004: every storer re-runs the price-equals-formula-of-count
        // check on every merkle candidate, in every context, before median
        // reconstruction. Runs AFTER signature verification so observe-only
        // telemetry cannot be spoofed by unauthenticated senders. Reject-only
        // when enforcement is enabled; no trust evidence emitted in either
        // mode.
        Self::validate_merkle_candidate_arithmetic(&merkle_proof.winner_pool)?;
        Self::log_off_curve_merkle(&merkle_proof.winner_pool);

        // Pay-yourself defence: the candidate pub_keys must map to peers the
        // live DHT actually considers closest to the pool midpoint. Without
        // this, an attacker can point all 16 reward_address fields at a
        // self-owned wallet and drain their own payment. Every storing node
        // runs this check against the single `winner_pool` in the proof, so a
        // forged pool is rejected everywhere it lands. The pass cache and
        // single-flight keyed on pool_hash collapse the Kademlia lookup cost
        // within a batch and across concurrent PUTs for the same pool.
        //
        self.verify_merkle_candidate_closeness(&merkle_proof.winner_pool, pool_hash)
            .await?;

        // Check pool cache first
        let cached_info = {
            let mut pool_cache = self.pool_cache.lock();
            pool_cache.get(&pool_hash).cloned()
        };

        let payment_info = if let Some(info) = cached_info {
            debug!("Pool cache hit for hash {}", hex::encode(pool_hash));
            info
        } else {
            // Query on-chain for completed merkle payment
            let info =
                payment_vault::get_completed_merkle_payment(&self.config.evm.network, pool_hash)
                    .await;
            // V2-834: EVM RPC is counted per call (bodies live inside alloy).
            super::traffic::record(
                super::traffic::EvmRpcCall::CompletedMerklePayment,
                info.is_ok(),
            );
            let info = info.map_err(|e| {
                let pool_hex = hex::encode(pool_hash);
                Error::Payment(format!(
                    "Failed to query merkle payment info for pool {pool_hex}: {e}"
                ))
            })?;

            let paid_node_addresses: Vec<_> = info
                .paidNodeAddresses
                .iter()
                .map(|pna| (pna.rewardsAddress, usize::from(pna.poolIndex), pna.amount))
                .collect();

            let on_chain_info = OnChainPaymentInfo {
                depth: info.depth,
                merkle_payment_timestamp: info.merklePaymentTimestamp,
                paid_node_addresses,
            };

            // Cache the pool info for subsequent chunks in the same batch
            {
                let mut pool_cache = self.pool_cache.lock();
                pool_cache.put(pool_hash, on_chain_info.clone());
            }

            debug!(
                "Queried on-chain merkle payment info for pool {}: depth={}, timestamp={}, paid_nodes={}",
                hex::encode(pool_hash),
                on_chain_info.depth,
                on_chain_info.merkle_payment_timestamp,
                on_chain_info.paid_node_addresses.len()
            );

            on_chain_info
        };

        // Verify timestamp consistency (signatures already checked above before RPC).
        for candidate in &merkle_proof.winner_pool.candidate_nodes {
            if candidate.merkle_payment_timestamp != payment_info.merkle_payment_timestamp {
                return Err(Error::Payment(format!(
                    "Candidate timestamp mismatch: expected {}, got {} (reward: {})",
                    payment_info.merkle_payment_timestamp,
                    candidate.merkle_payment_timestamp,
                    candidate.reward_address
                )));
            }
        }

        // Get the root from the winner pool's midpoint proof
        let smart_contract_root = merkle_proof.winner_pool.midpoint_proof.root();

        // Verify the cryptographic merkle proofs (address belongs to tree,
        // midpoint belongs to tree, roots match, timestamps valid).
        evmlib::merkle_payments::verify_merkle_proof(
            &merkle_proof.address,
            &merkle_proof.data_proof,
            &merkle_proof.winner_pool.midpoint_proof,
            payment_info.depth,
            smart_contract_root,
            payment_info.merkle_payment_timestamp,
        )
        .map_err(|e| {
            let xorname_hex = hex::encode(xorname);
            Error::Payment(format!(
                "Merkle proof verification failed for {xorname_hex}: {e}"
            ))
        })?;

        // Verify paid node count matches depth
        let expected_depth = payment_info.depth as usize;
        let actual_paid = payment_info.paid_node_addresses.len();
        if actual_paid != expected_depth {
            return Err(Error::Payment(format!(
                "Wrong number of paid nodes: expected {expected_depth}, got {actual_paid}"
            )));
        }

        // Expected per-node payment from the contract formula. ADR-0008: which
        // multiplier is REQUIRED depends on when the receipt was stamped —
        // 3x from the parity boundary onward, the historic 1x before it, since
        // that money was already spent under the old rule. The expiry check
        // above bounds the legacy branch: no receipt older than a week is
        // valid, so the branch is unreachable one week past the boundary.
        let candidate_prices: Vec<Amount> = merkle_proof
            .winner_pool
            .candidate_nodes
            .iter()
            .map(|c| c.price)
            .collect();
        let parity_from = *self.merkle_parity_from.read();
        let required_multiplier =
            merkle_required_multiplier(payment_info.merkle_payment_timestamp, parity_from);
        let expected_per_node_bare =
            merkle_expected_per_node(&candidate_prices, payment_info.depth, 1)?;
        let expected_per_node_parity = merkle_expected_per_node(
            &candidate_prices,
            payment_info.depth,
            PAID_QUOTE_PAYMENT_MULTIPLIER,
        )?;
        let expected_per_node =
            merkle_expected_per_node(&candidate_prices, payment_info.depth, required_multiplier)?;

        // Verify paid node indices, addresses, and amounts against the candidate pool.
        //
        // Each paid node must:
        // 1. Have a valid index within the candidate pool
        // 2. Match the expected reward address at that index
        // 3. Have been paid at least `expected_per_node`, which is the contract
        //    formula `median16(prices) * 2^depth / depth` at the multiplier the
        //    receipt's own timestamp selects (3x from the parity boundary, the
        //    historic 1x before it). Do NOT re-assume the 1x formula here —
        //    read `expected_per_node` rather than recomputing it.
        //
        // Note: unlike single-node payments, merkle proofs are NOT bound to a
        // specific storing node. The contract pays `depth` random nodes from the
        // winner pool; the storing node is whichever close-group peer the client
        // routes the chunk to. There is no local-recipient check here because
        // any node that can verify the merkle proof is allowed to store the chunk.
        // Replay protection comes from the per-address proof binding (each proof
        // is for a specific XorName in the paid tree).
        for (addr, idx, paid_amount) in &payment_info.paid_node_addresses {
            let node = merkle_proof
                .winner_pool
                .candidate_nodes
                .get(*idx)
                .ok_or_else(|| {
                    Error::Payment(format!(
                        "Paid node index {idx} out of bounds for pool size {}",
                        merkle_proof.winner_pool.candidate_nodes.len()
                    ))
                })?;
            if node.reward_address != *addr {
                return Err(Error::Payment(format!(
                    "Paid node address mismatch at index {idx}: expected {addr}, got {}",
                    node.reward_address
                )));
            }
            if *paid_amount < expected_per_node {
                // An exact `required_multiplier`x shortfall is the signature of
                // a client that settles under the superseded rule: it applied
                // no multiplier at all. That is not a pricing dispute, it is an
                // outdated client, and the person reading this has already paid
                // and cannot get it back. Say so, and say what to do about it,
                // rather than leaving them with two integers to compare.
                //
                // This message is the only thing that reaches such a client.
                // The settlement-version gate refuses these uploads before they
                // pay, but only for clients new enough to declare a version,
                // which by construction excludes every client this branch
                // catches.
                let looks_like_stale_client = underpayment_looks_like_a_stale_client(
                    *paid_amount,
                    expected_per_node_bare,
                    required_multiplier,
                );
                let advice = if looks_like_stale_client {
                    " This is exactly the pre-parity settlement amount, so the paying client is \
                     too old to settle correctly. Run `ant update` to upgrade, or reinstall from \
                     https://github.com/WithAutonomi/ant-client/releases/latest. Note the payment \
                     that was already made cannot be recovered."
                } else {
                    ""
                };
                return Err(Error::Payment(format!(
                    "Underpayment for node at index {idx}: paid {paid_amount}, \
                     expected at least {expected_per_node} \
                     (median16 formula, depth={}, {required_multiplier}x required for a \
                     receipt stamped {} vs parity boundary {parity_from}).{advice}",
                    payment_info.depth, payment_info.merkle_payment_timestamp
                )));
            }
        }

        if crate::logging::enabled!(crate::logging::Level::INFO) {
            info!(
                "Merkle payment verified for {} (pool: {})",
                hex::encode(xorname),
                hex::encode(pool_hash)
            );
        }

        // ADR-0008 parity telemetry, emitted only now: every paid index, reward
        // address and amount above has been validated, so a proof that is
        // rejected later in this function can never enter the rollout signal.
        // Store admissions only, matching the cross-check below — a paid-list
        // receipt reprices no fresh economic decision and would otherwise
        // double-count a batch already measured at its store admission.
        // `log_merkle_parity` itself drops all but the first admission per
        // pool, so a 256-chunk batch contributes one line here, not 256.
        if context.is_store_admission() {
            self.log_merkle_parity(
                pool_hash,
                &payment_info,
                expected_per_node_bare,
                expected_per_node_parity,
                required_multiplier,
            );
        }

        // ADR-0004: route the merkle-batch candidates through the SAME
        // cross-check + first-audit funnel as single-node quotes, AFTER on-chain
        // verification has succeeded (so an unpaid pool cannot drive audits or
        // fetches). Store admissions only (direct PUT + immediate fresh
        // replication, the paths that previously verified under `ClientPut`) —
        // a paid-list receipt's pins have aged out.
        // Amendment 2: only the candidates the contract actually PAID
        // (`paid_node_addresses` indices, verified above) are nominated for
        // first audits; the rest of the pool merely established the median.
        if context.is_store_admission() {
            let paid_indices: std::collections::HashSet<usize> = payment_info
                .paid_node_addresses
                .iter()
                .map(|(_, idx, _)| *idx)
                .collect();
            self.cross_check_merkle_candidates(
                &merkle_proof.winner_pool,
                &merkle_proof.commitment_sidecars,
                &paid_indices,
            )
            .await;
        }

        Ok(())
    }

    /// ADR-0004 cross-check for the merkle-batch path: every candidate carries
    /// the same signed `(committed_key_count, commitment_pin)` binding as a
    /// single-node quote, so each non-baseline candidate is resolved against the
    /// gossip cache (or fetched) and routed into the deterministic first audit,
    /// exactly like [`Self::cross_check_quotes`]. The candidate's peer id is
    /// derived from its `pub_key` (`PeerId = BLAKE3(pub_key)`), matching how the
    /// network binds identities.
    async fn cross_check_merkle_candidates(
        &self,
        pool: &evmlib::merkle_payments::MerklePaymentCandidatePool,
        commitment_sidecars: &[Vec<u8>],
        paid_indices: &std::collections::HashSet<usize>,
    ) {
        let now = std::time::Instant::now();
        let ttl = crate::replication::commitment_state::GOSSIP_ANSWERABILITY_TTL;
        let p2p = self.p2p_node.read().as_ref().map(Arc::clone);
        let monetized_pin_tx = self.monetized_pin_tx.read().as_ref().cloned();
        let cache = self.commitment_cache.read().as_ref().map(Arc::clone);
        // ADR-0004 "the commitment arrived with the quote" for the merkle path:
        // validate sidecars exactly as the single-node path does.
        let sidecar_map = Self::index_valid_sidecars(commitment_sidecars);

        let mut unresolved: Vec<(PeerId, [u8; 32], u32, Vec<u8>)> = Vec::new();
        for (idx, candidate) in pool.candidate_nodes.iter().enumerate() {
            let Some(pin) = candidate.commitment_pin else {
                continue; // baseline candidate pins nothing
            };
            let peer_id = PeerId::from_bytes(*blake3::hash(&candidate.pub_key).as_bytes());

            // ADR-0004 Amendment 2: nominate only the candidates the contract
            // actually paid — the unpaid pool members earned nothing and stay
            // covered by the gossip-lottery audit path. Pre-amendment every
            // pool candidate (16) was nominated per verified proof.
            if paid_indices.contains(&idx) {
                if let Some(ref tx) = monetized_pin_tx {
                    // Bounded queue: drop on full (best-effort, penalty-free;
                    // the peer's next settled payment re-nominates it). Count a
                    // Full drop so ingress saturation is observable; a Closed
                    // channel just means the engine is shutting down.
                    if let Err(tokio::sync::mpsc::error::TrySendError::Full(_)) =
                        tx.try_send(crate::replication::MonetizedPinEvent {
                            peer: peer_id,
                            pin,
                            key_count: candidate.committed_key_count,
                            quote_ts: std::time::UNIX_EPOCH
                                .checked_add(std::time::Duration::from_secs(
                                    candidate.merkle_payment_timestamp,
                                ))
                                .unwrap_or(std::time::UNIX_EPOCH),
                        })
                    {
                        crate::replication::note_monetized_ingress_drop();
                    }
                }
            }

            let resolved = match sidecar_map.get(&(peer_id, pin)) {
                Some(c) => Some(c.clone()),
                None => Self::cache_resolve(cache.as_ref(), peer_id, pin, now, ttl).await,
            };
            match resolved {
                Some(commitment) => {
                    let artifact = rmp_serde::to_vec(candidate).unwrap_or_default();
                    Self::handle_cross_check(
                        &peer_id,
                        pin,
                        candidate.committed_key_count,
                        artifact,
                        &commitment,
                        p2p.as_ref(),
                    )
                    .await;
                }
                None => unresolved.push((
                    peer_id,
                    pin,
                    candidate.committed_key_count,
                    rmp_serde::to_vec(candidate).unwrap_or_default(),
                )),
            }
        }

        if unresolved.is_empty() {
            return;
        }
        let Some(p2p) = p2p else {
            return;
        };
        let neg_cache = Arc::clone(&self.pin_fetch_negative_cache);
        tokio::spawn(async move {
            Self::drain_unresolved_pin_fetches(&p2p, &neg_cache, unresolved).await;
        });
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use evmlib::merkle_payments::MerklePaymentCandidatePool;
    use evmlib::PaymentQuote;
    use saorsa_core::MlDsa65;
    use saorsa_pqc::pqc::types::MlDsaSecretKey;
    use saorsa_pqc::pqc::MlDsaOperations;
    use std::time::SystemTime;

    /// Create a verifier for unit tests. EVM is always on, but tests can
    /// pre-populate the cache to bypass on-chain verification.
    /// A chunk whose address equals a pointer's `state_id` must not be able to
    /// pay for that pointer's update.
    ///
    /// Reaching a pointer's `state_id` with a chunk now takes a BLAKE3
    /// preimage under one BLAKE3 mode for an output of the other — it is a
    /// `derive_key` output, and a chunk address is a plain hash. This is the second line: were the two ever to
    /// meet at one value, filing both "already paid" entries under it would let
    /// chunk price buy a pointer update, skipping issuer proximity, the price
    /// floor and the proof-shape rule with it. The typed key holds whatever the
    /// two hash modes do.
    #[test]
    fn a_chunk_cannot_pay_for_a_pointer_that_shares_its_address() {
        let state_id: XorName = [0x5Au8; 32];
        let pointer_address: XorName = [0xA5u8; 32];

        let chunk = PaymentTarget::same(state_id);
        let pointer = PaymentTarget::split(pointer_address, state_id);

        assert_eq!(
            chunk,
            PaymentTarget::Chunk(state_id),
            "a chunk keeps its bare address, so existing entries are untouched"
        );
        assert_ne!(
            pointer, chunk,
            "the pointer must not read the chunk's paid entry as its own"
        );

        // And two different pointers sharing a state cannot borrow either.
        let other = PaymentTarget::split([0x11u8; 32], state_id);
        assert_ne!(pointer, other);

        // The key is typed, not hashed: there is no 32-byte preimage a client
        // could put in a chunk's *content* to land on the pointer's entry,
        // because no chunk key is ever a `PointerState` variant.
        let cache = VerifiedCache::with_capacity(8);
        cache.insert_key(chunk);
        assert!(cache.contains_key(&chunk), "the chunk's own entry is there");
        assert!(
            !cache.contains_key(&pointer),
            "and it does not satisfy the pointer"
        );
        for crafted in [state_id, pointer_address, [0u8; 32], [0xFFu8; 32]] {
            assert!(
                !cache.contains_key(&PaymentTarget::split(pointer_address, state_id)),
                "no chunk address {crafted:?} can stand in for the pointer's entry"
            );
            cache.insert_key(PaymentTarget::Chunk(crafted));
        }
        assert!(
            !cache.contains_key(&pointer),
            "still no chunk address satisfies the pointer's typed entry"
        );
    }

    fn create_test_verifier() -> PaymentVerifier {
        let config = PaymentVerifierConfig {
            evm: EvmVerifierConfig::default(),
            cache_capacity: 100,
            close_group_size: CLOSE_GROUP_SIZE,
            local_rewards_address: RewardsAddress::new([1u8; 20]),
            price_floor: PriceFloorConfig::default(),
        };
        PaymentVerifier::new(config)
    }

    #[test]
    fn paid_quote_issuer_closeness_width_uses_k() {
        let issuer_closeness_width = PAID_QUOTE_ISSUER_CLOSENESS_WIDTH;
        let k_bucket_size = K_BUCKET_SIZE;
        let close_group_size = CLOSE_GROUP_SIZE;

        assert_eq!(issuer_closeness_width, k_bucket_size);
        assert!(issuer_closeness_width > close_group_size);
    }

    /// 16 candidate prices 100..=1600; the upper median (index 8) is 900.
    fn varied_candidate_prices() -> Vec<Amount> {
        (1..=16u64).map(|i| Amount::from(i * 100)).collect()
    }

    #[test]
    fn merkle_expected_per_node_reproduces_the_contract_formula() {
        // total = median16 x 2^depth = 900 x 16 = 14400, over depth=4 nodes.
        let per_node = merkle_expected_per_node(&varied_candidate_prices(), 4, 1)
            .expect("depth 4 pool is payable");
        assert_eq!(per_node, Amount::from(3_600u64));
    }

    /// 16 identical candidate prices, so the upper median is exactly `median`.
    fn prices_with_median(median: u64) -> Vec<Amount> {
        vec![Amount::from(median); 16]
    }

    /// The parity expectation is the floor of the multiplied total, which is
    /// **not** the bare expectation times the multiplier.
    ///
    /// `expected(m) = floor(m x median x 2^depth / depth)` sits in
    /// `[m x expected(1), m x expected(1) + (m - 1)]`: equal when `depth`
    /// divides the total, up to `m - 1` wei above it when it does not.
    /// Asserting exact equality would encode `floor(3x/d) == 3 x floor(x/d)`,
    /// which is false in general and only happened to hold for the
    /// evenly-dividing case the first version of this test used.
    #[test]
    fn merkle_expected_per_node_scales_within_the_division_remainder() {
        let multiplier = PAID_QUOTE_PAYMENT_MULTIPLIER;

        for median in [1u64, 899, 900, 901, 6_000, 1_000_003] {
            for depth in 1..=8u8 {
                let prices = prices_with_median(median);
                let bare = merkle_expected_per_node(&prices, depth, 1)
                    .expect("bare expectation is payable");
                let parity = merkle_expected_per_node(&prices, depth, multiplier)
                    .expect("parity expectation is payable");

                let scaled_bare = bare * Amount::from(multiplier);
                let slack = Amount::from(multiplier - 1);

                assert!(
                    parity >= scaled_bare && parity <= scaled_bare + slack,
                    "median {median} depth {depth}: parity {parity} must lie in \
                     [{scaled_bare}, {}]",
                    scaled_bare + slack
                );

                // And it is exactly the floor of the multiplied total.
                let leaves = 1u64 << u32::from(depth);
                let expected =
                    Amount::from(median * leaves * multiplier) / Amount::from(u64::from(depth));
                assert_eq!(parity, expected, "median {median} depth {depth}");
            }
        }
    }

    /// Regression for the arithmetic order: the helper multiplies the total and
    /// then divides once, as the contract does. Median 901 at depth 7 is a case
    /// where that differs from scaling a 1x result by one wei, so a refactor
    /// that divided first would fail here.
    #[test]
    fn merkle_expected_per_node_multiplies_before_dividing() {
        let prices = prices_with_median(901);
        let bare = merkle_expected_per_node(&prices, 7, 1).expect("bare expectation is payable");
        let parity = merkle_expected_per_node(&prices, 7, PAID_QUOTE_PAYMENT_MULTIPLIER)
            .expect("parity expectation is payable");

        assert_eq!(bare, Amount::from(16_475u64));
        assert_eq!(parity, Amount::from(49_426u64));
        assert_eq!(
            parity,
            bare * Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER) + Amount::from(1u64),
            "dividing before multiplying would lose this wei"
        );
    }

    /// The same non-linearity that test pins also breaks a naive check for
    /// "did this client apply no multiplier".
    ///
    /// Scaling the paid amount up by the multiplier and comparing against the
    /// parity expectation asks `49_426 == 49_425` at median 901 depth 7, which
    /// is false, so a genuinely unmultiplied settlement is not recognised and
    /// the payer never learns their client is too old. Comparing against the
    /// bare expectation asks the same arithmetic the expectation was built
    /// with, and holds at every depth.
    #[test]
    fn a_stale_client_is_recognised_where_depth_does_not_divide_its_leaves() {
        let prices = prices_with_median(901);
        let bare = merkle_expected_per_node(&prices, 7, 1).expect("bare expectation is payable");
        let parity = merkle_expected_per_node(&prices, 7, PAID_QUOTE_PAYMENT_MULTIPLIER)
            .expect("parity expectation is payable");

        // The scaled comparison this replaced would have missed it.
        assert_ne!(parity, bare * Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER));

        assert!(
            underpayment_looks_like_a_stale_client(bare, bare, PAID_QUOTE_PAYMENT_MULTIPLIER),
            "an exactly-1x settlement must be recognised at depth 7"
        );
        // A merely-cheap payment still is not blamed on the client's version.
        assert!(!underpayment_looks_like_a_stale_client(
            bare.saturating_sub(Amount::from(1u64)),
            bare,
            PAID_QUOTE_PAYMENT_MULTIPLIER
        ));
        // And under the legacy regime there is nothing to upgrade away from.
        assert!(!underpayment_looks_like_a_stale_client(bare, bare, 1));
    }

    /// The economic invariant ADR-0008 restores: a merkle **leaf** settles
    /// for what the single-node path settles — 3x the median quote — up to
    /// the contract's integer division.
    ///
    /// Leaf, not chunk: the tree pads to `2^ceil(log2(N))` and the contract
    /// charges for every leaf, so a batch whose size is not a power of two
    /// pays a padding premium on top. That premium is a property of the
    /// contract formula, not of the multiplier under test here.
    ///
    /// `amountPerNode = totalAmount / depth` discards a remainder whenever
    /// `depth` does not divide `median x 2^depth` (e.g. depth 7). The loss is
    /// strictly under one wei per paid node, spread across `2^depth` chunks,
    /// so it is economically irrelevant — but the invariant is "within
    /// rounding", not exact equality, and the test says so rather than
    /// picking depths that happen to divide evenly.
    #[test]
    fn merkle_parity_per_chunk_equals_single_node_settlement() {
        let prices = varied_candidate_prices();
        let median = Amount::from(900u64);

        for depth in 1..=8u8 {
            let per_node = merkle_expected_per_node(&prices, depth, PAID_QUOTE_PAYMENT_MULTIPLIER)
                .expect("every supported depth is payable");
            let required_total = per_node * Amount::from(u64::from(depth));

            let leaves = Amount::from(1u64 << u32::from(depth));
            let ideal_total = median * Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER) * leaves;

            assert!(
                required_total <= ideal_total,
                "at depth {depth} the required total {required_total} must not \
                 exceed the 3x-median ideal {ideal_total}"
            );
            assert!(
                ideal_total - required_total < Amount::from(u64::from(depth)),
                "at depth {depth} the shortfall {} must be pure division \
                 remainder (< depth wei), not a missing multiplier",
                ideal_total - required_total
            );
        }
    }

    #[test]
    fn merkle_expected_per_node_is_zero_at_depth_zero() {
        let per_node = merkle_expected_per_node(&varied_candidate_prices(), 0, 3)
            .expect("depth zero is a valid no-op, not an error");
        assert_eq!(per_node, Amount::ZERO);
    }

    #[test]
    fn merkle_expected_per_node_rejects_an_empty_pool() {
        let Err(err) = merkle_expected_per_node(&[], 4, 3) else {
            panic!("an empty candidate pool has no median and must not be payable");
        };
        let err = err.to_string();
        assert!(err.contains("empty candidate pool"), "got: {err}");
    }

    fn make_signed_quote(
        xorname: XorName,
        price: Amount,
        rewards_seed: u8,
    ) -> (evmlib::EncodedPeerId, PaymentQuote) {
        let ml_dsa = MlDsa65::new();
        let (public_key, secret_key) = ml_dsa.generate_keypair().expect("keygen");
        let pub_key_bytes = public_key.as_bytes().to_vec();
        let peer_id = encoded_peer_id_for_pub_key(&pub_key_bytes);
        // Derive the ADR-0004 binding from the price rather than hardcoding
        // `(0, None)`: a quote priced at `calculate_price(n)` must also claim
        // `committed_key_count = n`, or it charges as though it stored `n` keys
        // while committing to nothing and is rejected by the arithmetic gate
        // before the test's own assertion is reached. Prices that are not on
        // the curve at all (`ZERO`, small literals) keep the baseline shape and
        // stay off-curve, which is what the tests exercising bad prices want.
        let (committed_key_count, commitment_pin) =
            match PaymentVerifier::price_off_curve_diagnostics(&price) {
                None => {
                    let records = derive_records_stored_from_price(price);
                    let count = u32::try_from(records).unwrap_or(u32::MAX);
                    let pin = if count == 0 { None } else { Some([0xA5u8; 32]) };
                    (count, pin)
                }
                Some(_) => (0, None),
            };
        let mut quote = PaymentQuote {
            content: xor_name::XorName(xorname),
            timestamp: SystemTime::now(),
            price,
            rewards_address: RewardsAddress::new([rewards_seed; 20]),
            committed_key_count,
            commitment_pin,
            pub_key: pub_key_bytes,
            signature: Vec::new(),
        };
        let secret_key = MlDsaSecretKey::from_bytes(secret_key.as_bytes()).expect("secret key");
        quote.signature = ml_dsa
            .sign(&secret_key, &quote.bytes_for_sig())
            .expect("sign quote")
            .as_bytes()
            .to_vec();
        (peer_id, quote)
    }

    fn make_signed_legacy_bundle(
        xorname: XorName,
        prices: [Amount; CLOSE_GROUP_SIZE],
    ) -> Vec<(evmlib::EncodedPeerId, PaymentQuote)> {
        prices
            .into_iter()
            .enumerate()
            .map(|(index, price)| {
                let rewards_seed = u8::try_from(index + 1).expect("small test index");
                make_signed_quote(xorname, price, rewards_seed)
            })
            .collect()
    }

    fn price_at_records(records: usize) -> Amount {
        crate::payment::pricing::calculate_price(records)
    }

    fn unique_test_prices() -> [Amount; CLOSE_GROUP_SIZE] {
        [
            price_at_records(0),
            price_at_records(1),
            price_at_records(2),
            price_at_records(3),
            price_at_records(4),
            price_at_records(5),
            price_at_records(6),
        ]
    }

    fn tied_median_test_prices() -> [Amount; CLOSE_GROUP_SIZE] {
        [
            price_at_records(0),
            price_at_records(1),
            price_at_records(2),
            price_at_records(3),
            price_at_records(3),
            price_at_records(4),
            price_at_records(5),
        ]
    }

    fn median_test_candidates(
        peer_quotes: &[(evmlib::EncodedPeerId, PaymentQuote)],
    ) -> Vec<(evmlib::EncodedPeerId, PaymentQuote)> {
        let mut sorted_quotes: Vec<_> = peer_quotes.iter().collect();
        sorted_quotes.sort_by_key(|(_, quote)| quote.price);
        let median_index = median_quote_index(sorted_quotes.len());
        let median_price = sorted_quotes
            .get(median_index)
            .expect("median quote")
            .1
            .price;

        sorted_quotes
            .into_iter()
            .filter(|(_, quote)| quote.price == median_price)
            .map(|(peer_id, quote)| (peer_id.clone(), quote.clone()))
            .collect()
    }

    fn expected_median_payment(peer_quotes: &[(evmlib::EncodedPeerId, PaymentQuote)]) -> Amount {
        let median_price = median_test_candidates(peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .price;
        median_price * Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER)
    }

    fn mark_k_closest_paid_candidates(
        verifier: &PaymentVerifier,
        peer_quotes: &[(evmlib::EncodedPeerId, PaymentQuote)],
    ) {
        let k_closest_peers = median_test_candidates(peer_quotes)
            .iter()
            .map(|(peer_id, _)| *peer_id.as_bytes())
            .collect();
        verifier.set_paid_quote_k_closest_for_tests(k_closest_peers);
    }

    fn mark_candidate_paid(verifier: &PaymentVerifier, quote: &PaymentQuote, amount: Amount) {
        verifier.set_completed_payment_for_tests(quote.hash(), amount);
    }

    fn mark_all_median_candidates_unpaid(
        verifier: &PaymentVerifier,
        peer_quotes: &[(evmlib::EncodedPeerId, PaymentQuote)],
    ) {
        for (_, quote) in median_test_candidates(peer_quotes) {
            mark_candidate_paid(verifier, &quote, Amount::ZERO);
        }
    }

    #[test]
    fn test_payment_required_for_new_data() {
        let verifier = create_test_verifier();
        let xorname = [1u8; 32];

        // All uncached data requires payment
        let status = verifier.check_payment_required(&xorname, VerificationContext::ClientPut);
        assert_eq!(status, PaymentStatus::PaymentRequired);
    }

    #[test]
    fn test_cache_hit() {
        let verifier = create_test_verifier();
        let xorname = [1u8; 32];

        // Manually add to cache
        verifier.cache.insert(xorname);

        // Should return CachedAsVerified
        let status = verifier.check_payment_required(&xorname, VerificationContext::ClientPut);
        assert_eq!(status, PaymentStatus::CachedAsVerified);
    }

    #[tokio::test]
    async fn test_verify_payment_without_proof_rejected() {
        let verifier = create_test_verifier();
        let xorname = [1u8; 32];

        // No proof provided => should return an error (EVM is always on)
        let result = verifier
            .verify_payment(&xorname, None, VerificationContext::ClientPut)
            .await;
        assert!(
            result.is_err(),
            "Expected Err without proof, got: {result:?}"
        );
    }

    #[tokio::test]
    async fn test_verify_payment_cached() {
        let verifier = create_test_verifier();
        let xorname = [1u8; 32];

        // Add to cache — simulates previously-paid data
        verifier.cache.insert(xorname);

        // Should succeed without payment (cached)
        let result = verifier
            .verify_payment(&xorname, None, VerificationContext::ClientPut)
            .await;
        assert!(result.is_ok());
        assert_eq!(result.expect("cached"), PaymentStatus::CachedAsVerified);
    }

    #[tokio::test]
    async fn test_paid_list_cache_entry_does_not_satisfy_client_put() {
        let verifier = create_test_verifier();
        let xorname = [0xB8u8; 32];
        verifier.cache.insert_paid_list_verified(xorname);

        assert_eq!(
            verifier.check_payment_required(&xorname, VerificationContext::PaidListAdmission),
            PaymentStatus::CachedAsVerified,
            "paid-list lookups must hit a paid-list-verified entry"
        );
        assert_eq!(
            verifier.check_payment_required(&xorname, VerificationContext::ClientPut),
            PaymentStatus::PaymentRequired,
            "client PUT must not fast-path on a paid-list-verified entry"
        );

        let err = verifier
            .verify_payment(&xorname, None, VerificationContext::ClientPut)
            .await
            .expect_err("proof-less client PUT must not ride the paid-list entry");
        assert!(
            format!("{err}").contains("Payment required"),
            "client PUT must still demand payment: {err}"
        );
    }

    #[test]
    fn test_payment_status_can_store() {
        assert!(PaymentStatus::CachedAsVerified.can_store());
        assert!(PaymentStatus::PaymentVerified.can_store());
        assert!(!PaymentStatus::PaymentRequired.can_store());
    }

    #[test]
    fn test_payment_status_is_cached() {
        assert!(PaymentStatus::CachedAsVerified.is_cached());
        assert!(!PaymentStatus::PaymentVerified.is_cached());
        assert!(!PaymentStatus::PaymentRequired.is_cached());
    }

    #[tokio::test]
    async fn test_cache_preload_bypasses_evm() {
        let verifier = create_test_verifier();
        let xorname = [42u8; 32];

        // Not yet cached — should require payment
        assert_eq!(
            verifier.check_payment_required(&xorname, VerificationContext::ClientPut),
            PaymentStatus::PaymentRequired
        );

        // Pre-populate cache (simulates a previous successful payment)
        verifier.cache.insert(xorname);

        // Now the xorname should be cached
        assert_eq!(
            verifier.check_payment_required(&xorname, VerificationContext::ClientPut),
            PaymentStatus::CachedAsVerified
        );
    }

    #[tokio::test]
    async fn test_proof_too_small() {
        let verifier = create_test_verifier();
        let xorname = [1u8; 32];

        // Proof smaller than MIN_PAYMENT_PROOF_SIZE_BYTES
        let small_proof = vec![0u8; MIN_PAYMENT_PROOF_SIZE_BYTES - 1];
        let result = verifier
            .verify_payment(&xorname, Some(&small_proof), VerificationContext::ClientPut)
            .await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("too small"),
            "Error should mention 'too small': {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_proof_too_large() {
        let verifier = create_test_verifier();
        let xorname = [2u8; 32];

        // Proof larger than MAX_PAYMENT_PROOF_SIZE_BYTES
        let large_proof = vec![0u8; MAX_PAYMENT_PROOF_SIZE_BYTES + 1];
        let result = verifier
            .verify_payment(&xorname, Some(&large_proof), VerificationContext::ClientPut)
            .await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("too large"),
            "Error should mention 'too large': {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_proof_at_min_boundary_unknown_tag() {
        let verifier = create_test_verifier();
        let xorname = [3u8; 32];

        // Exactly MIN_PAYMENT_PROOF_SIZE_BYTES with unknown tag — rejected
        let boundary_proof = vec![0xFFu8; MIN_PAYMENT_PROOF_SIZE_BYTES];
        let result = verifier
            .verify_payment(
                &xorname,
                Some(&boundary_proof),
                VerificationContext::ClientPut,
            )
            .await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("Unknown payment proof type tag"),
            "Error should mention unknown tag: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_proof_at_max_boundary_unknown_tag() {
        let verifier = create_test_verifier();
        let xorname = [4u8; 32];

        // Exactly MAX_PAYMENT_PROOF_SIZE_BYTES with unknown tag — rejected
        let boundary_proof = vec![0xFFu8; MAX_PAYMENT_PROOF_SIZE_BYTES];
        let result = verifier
            .verify_payment(
                &xorname,
                Some(&boundary_proof),
                VerificationContext::ClientPut,
            )
            .await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("Unknown payment proof type tag"),
            "Error should mention unknown tag: {err_msg}"
        );
    }

    /// Regression pin for the DEV-01 (2026-07-06) outage: a merkle proof that
    /// ships all 16 ADR-0004 commitment sidecars serializes to ~342 KB, and the
    /// old 256 KB cap size-rejected every merkle PUT as soon as nodes carried
    /// live commitments. The cap must keep accepting that legacy client shape;
    /// a proof of that size must fail on content, never on size.
    #[tokio::test]
    async fn test_sidecar_bearing_merkle_proof_size_is_accepted() {
        const LEGACY_SIDECAR_PROOF_BYTES: usize = 342_171;

        let verifier = create_test_verifier();
        let xorname = [6u8; 32];

        let legacy_sized_proof = vec![0xFFu8; LEGACY_SIDECAR_PROOF_BYTES];
        let result = verifier
            .verify_payment(
                &xorname,
                Some(&legacy_sized_proof),
                VerificationContext::ClientPut,
            )
            .await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            !err_msg.contains("too large"),
            "A {LEGACY_SIDECAR_PROOF_BYTES}-byte proof must not be size-rejected \
             (sidecar-bearing merkle proofs from pre-fix clients): {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_malformed_single_node_proof() {
        let verifier = create_test_verifier();
        let xorname = [5u8; 32];

        // Valid tag (0x01) but garbage payload — should fail deserialization
        let mut garbage = vec![crate::ant_protocol::PROOF_TAG_SINGLE_NODE];
        garbage.extend_from_slice(&[0xAB; 63]);
        let result = verifier
            .verify_payment(&xorname, Some(&garbage), VerificationContext::ClientPut)
            .await;
        assert!(result.is_err());
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("deserialize") || err_msg.contains("Failed"),
            "Error should mention deserialization failure: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_legacy_paid_median_full_path_accepted() {
        let verifier = create_test_verifier();
        let xorname = [0xA1u8; 32];
        let peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("paid median should verify"),
            PaymentStatus::PaymentVerified
        );
    }

    /// ADR-0004 Amendment 2: a settlement recorded under the median quote's
    /// hash but redirected to a DIFFERENT rewards address must be rejected —
    /// otherwise a client could "pay" its own wallet while the issuer is
    /// treated (and first-audited) as paid without ever being compensated.
    #[tokio::test]
    async fn test_legacy_paid_median_redirected_settlement_rejected() {
        let verifier = create_test_verifier();
        let xorname = [0xA2u8; 32];
        let peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        // Full amount, but recorded for an address that is not the quote's.
        let attacker_address = RewardsAddress::new([0xEEu8; 20]);
        assert_ne!(attacker_address, paid_quote.rewards_address);
        verifier.set_completed_payment_with_address_for_tests(
            paid_quote.hash(),
            expected_amount,
            attacker_address,
        );

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        let err = result.expect_err("redirected settlement must be rejected");
        assert!(
            format!("{err}").contains("redirected"),
            "Error should mention the settlement redirect: {err}"
        );
    }

    #[tokio::test]
    async fn test_legacy_single_quote_proof_accepted() {
        let verifier = create_test_verifier();
        let xorname = [0xB1u8; 32];
        let (peer_id, quote) = make_signed_quote(xorname, price_at_records(0), 1);
        let peer_quotes = vec![(peer_id, quote.clone())];
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        mark_candidate_paid(&verifier, &quote, expected_median_payment(&peer_quotes));

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("single paid quote should verify"),
            PaymentStatus::PaymentVerified
        );
    }

    /// Fixed-count [`crate::payment::quote::CommitmentSource`] standing in for
    /// the receiver's live commitment state in price-floor tests.
    struct FixedFloorSource {
        key_count: u32,
    }

    impl crate::payment::quote::CommitmentSource for FixedFloorSource {
        fn current_binding_for_quote(&self) -> Option<crate::payment::quote::QuoteBinding> {
            self.current_binding_snapshot()
        }

        fn current_binding_snapshot(&self) -> Option<crate::payment::quote::QuoteBinding> {
            Some(crate::payment::quote::QuoteBinding {
                key_count: self.key_count,
                pin: [0u8; 32],
            })
        }

        fn commitment_blob_for_pin(&self, _pin: [u8; 32]) -> Option<Vec<u8>> {
            None
        }
    }

    /// Receiver commitment count for floor tests, chosen so the local price is
    /// comfortably more than double the baseline (the 50% floor then rejects a
    /// baseline-priced settlement). Asserted in each test so a pricing-curve
    /// change fails loudly instead of silently weakening the tests.
    const FLOOR_TEST_LOCAL_KEY_COUNT: u32 = 1_000_000;

    fn floor_test_verifier(enforce: bool) -> PaymentVerifier {
        let verifier = create_test_verifier();
        verifier.attach_local_commitment_source(Arc::new(FixedFloorSource {
            key_count: FLOOR_TEST_LOCAL_KEY_COUNT,
        }));
        verifier.set_price_floor_for_tests(PriceFloorConfig {
            enforce,
            tolerance_percent: PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT,
        });
        let local_price = price_at_records(FLOOR_TEST_LOCAL_KEY_COUNT as usize);
        assert!(
            local_price > price_at_records(0) * Amount::from(2u64),
            "floor tests need a local price above twice baseline, got {local_price}"
        );
        verifier
    }

    /// The settled amount the floor requires: 3x the tolerated fraction of the
    /// close group's median commitment-bound price.
    fn group_floor_required_amount() -> Amount {
        let tolerated = price_at_records(GROUP_TEST_MEDIAN_KEY_COUNT as usize)
            * Amount::from(PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT)
            / Amount::from(100u64);
        tolerated * Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER)
    }

    /// One cheap (baseline-priced) quote paid `settled`: the cheapest-of-K
    /// omission shape a modified client can always produce.
    fn cheap_single_quote_proof(
        verifier: &PaymentVerifier,
        xorname: XorName,
        settled: Amount,
    ) -> Vec<u8> {
        let (peer_id, quote) = make_signed_quote(xorname, price_at_records(0), 1);
        let peer_quotes = vec![(peer_id, quote.clone())];
        mark_k_closest_paid_candidates(verifier, &peer_quotes);
        mark_candidate_paid(verifier, &quote, settled);
        serialize_proof(peer_quotes)
    }

    /// Close-group commitment counts used by the group-median floor tests.
    ///
    /// Exactly [`PRICE_FLOOR_MIN_NEIGHBOUR_COMMITMENTS`] entries, so these tests
    /// sit on the gate rather than comfortably past it: drop one and the floor
    /// must skip. Spread widely enough that the cheapest sits below the tolerated
    /// fraction of the median (asserted in the rejection test), and kept in the
    /// low thousands because each entry builds a real signed merkle commitment.
    const GROUP_TEST_KEY_COUNTS: [u32; 15] = [
        500, 800, 1_100, 1_400, 1_700, 2_000, 2_300, 2_600, 2_900, 3_200, 3_500, 3_800, 4_100,
        4_400, 4_700,
    ];

    /// The LOWER median of [`GROUP_TEST_KEY_COUNTS`] plus this node's own
    /// [`FLOOR_TEST_LOCAL_KEY_COUNT`].
    ///
    /// Sixteen samples, so the lower median is the eighth smallest: `2_600`. The
    /// receiver's own count is an extreme outlier at the top and does not move
    /// it, which is exactly the robustness a median buys. An even sample is
    /// deliberate — it is the case where upper and lower median differ, and the
    /// lower one is what production safety depends on.
    const GROUP_TEST_MEDIAN_KEY_COUNT: u32 = 2_600;

    /// Build a gossip commitment cache holding one fresh commitment per entry of
    /// `key_counts`, attach it, and return the peer ids so the caller can put
    /// them in the K-closest set.
    ///
    /// `age` ages every record equally, for the staleness test.
    fn attach_group_commitments(
        verifier: &PaymentVerifier,
        key_counts: &[u32],
        age: std::time::Duration,
    ) -> Vec<[u8; 32]> {
        use crate::replication::commitment_state::PeerCommitmentRecord;

        let received_at = std::time::Instant::now()
            .checked_sub(age)
            .unwrap_or_else(std::time::Instant::now);
        let mut cache_map = HashMap::new();
        let mut peer_ids = Vec::with_capacity(key_counts.len());
        for (index, count) in key_counts.iter().enumerate() {
            let built = test_built_commitment(*count);
            let mut raw = [0u8; 32];
            // Distinct, stable id per neighbour; the id only has to match
            // between the cache key and the K-closest set.
            raw[0] = u8::try_from(index + 1).unwrap_or(u8::MAX);
            peer_ids.push(raw);
            cache_map.insert(
                PeerId::from_bytes(raw),
                PeerCommitmentRecord::from_verified(built.commitment().clone(), received_at),
            );
        }
        verifier.attach_commitment_cache(Arc::new(tokio::sync::RwLock::new(cache_map)));
        peer_ids
    }

    /// A one-quote proof priced at `quote_records`, settled at `3 x
    /// price(settle_records)`, with the K-closest set covering both the quote
    /// issuer and the supplied close-group neighbours.
    fn group_floor_proof(
        verifier: &PaymentVerifier,
        xorname: XorName,
        quote_records: usize,
        settled: Amount,
        neighbours: &[[u8; 32]],
    ) -> Vec<u8> {
        let (peer_id, quote) = make_signed_quote(xorname, price_at_records(quote_records), 1);
        let mut k_closest: Vec<[u8; 32]> = neighbours.to_vec();
        k_closest.push(*peer_id.as_bytes());
        let peer_quotes = vec![(peer_id, quote.clone())];
        verifier.set_paid_quote_k_closest_for_tests(k_closest);
        mark_candidate_paid(verifier, &quote, settled);
        serialize_proof(peer_quotes)
    }

    fn three_times(price: Amount) -> Amount {
        price * Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER)
    }

    /// Overwrite the signed key count on cached commitments for `peer_ids`.
    ///
    /// Models a modified node: the honest builder refuses to construct a
    /// commitment at these counts, and building real ones at the protocol cap
    /// costs about a second and 200 MB apiece. The signature no longer matches,
    /// which is fine — the floor reads the cached count and never re-verifies.
    ///
    /// Asserts that every requested mutation landed. Callers assert ADMISSION,
    /// so a helper that silently no-opped would make those tests pass without
    /// ever forging anything.
    async fn forge_key_counts(
        verifier: &PaymentVerifier,
        peer_ids: Option<&[[u8; 32]]>,
        key_count: u32,
    ) {
        use crate::replication::commitment_state::PeerCommitmentRecord;

        let peer_ids = peer_ids.expect("caller must supply a valid peer id slice");
        let cache = verifier
            .commitment_cache
            .read()
            .as_ref()
            .map(Arc::clone)
            .expect("commitment cache must be attached before forging counts");
        let mut mutated = 0usize;
        {
            let mut guard = cache.write().await;
            for raw in peer_ids {
                if let Some(record) = guard.get_mut(&PeerId::from_bytes(*raw)) {
                    if let Some(mut forged) = record.last_commitment().cloned() {
                        forged.key_count = key_count;
                        *record =
                            PeerCommitmentRecord::from_verified(forged, std::time::Instant::now());
                        mutated += 1;
                    }
                }
            }
        }
        assert_eq!(
            mutated,
            peer_ids.len(),
            "every forged count must land, or the test asserting admission proves nothing"
        );
    }

    #[test]
    fn store_admission_contexts_cover_puts_and_fresh_replication_only() {
        assert!(VerificationContext::ClientPut.is_store_admission());
        assert!(VerificationContext::FreshReplication.is_store_admission());
        assert!(!VerificationContext::PaidListAdmission.is_store_admission());
    }

    /// Pins the reference itself: the sample includes this node, the median is
    /// taken over the whole close group, and the receiver's own extreme count
    /// does not move it.
    #[tokio::test]
    async fn group_reference_price_is_the_close_group_median() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xD0u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        verifier.set_paid_quote_k_closest_for_tests(neighbours);

        let (reference, sample) = verifier.group_reference_price(&xorname).await;
        let reference = reference.expect("a fully populated group must produce a reference");

        assert_eq!(
            sample.neighbours,
            GROUP_TEST_KEY_COUNTS.len(),
            "the gated count is neighbours only"
        );
        assert_eq!(
            sample.total,
            GROUP_TEST_KEY_COUNTS.len() + 1,
            "the median is taken over the neighbours plus this node's own commitment"
        );
        assert!(
            sample.skip_reason.is_none(),
            "a fully populated group must not report a skip reason"
        );
        assert_eq!(
            reference,
            price_at_records(GROUP_TEST_MEDIAN_KEY_COUNT as usize),
            "the reference must be the group median price, not this node's own"
        );
        assert!(
            reference < price_at_records(FLOOR_TEST_LOCAL_KEY_COUNT as usize),
            "this node's own price is far above the median and must not be the reference"
        );
    }

    /// The production false-rejection case, encoded.
    ///
    /// This receiver holds far more than anyone else in its group, so its own
    /// price is enormous. An honest client paying 3x the GROUP MEDIAN must still
    /// be admitted. Pricing the floor against the receiver's own commitment
    /// rejected exactly this shape on ant-prod-01, 0.377% of honest stores, all
    /// of them on the fullest nodes.
    #[tokio::test]
    async fn price_floor_prices_against_the_group_median_not_the_receiver() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xD1u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);

        let median_price = price_at_records(GROUP_TEST_MEDIAN_KEY_COUNT as usize);
        let own_price = price_at_records(FLOOR_TEST_LOCAL_KEY_COUNT as usize);
        assert!(
            own_price > median_price * Amount::from(2u64),
            "this test is only meaningful when the receiver is far above the group median"
        );

        // Honest: the client paid 3x the median of the quotes it collected.
        let settled = three_times(median_price);
        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            GROUP_TEST_MEDIAN_KEY_COUNT as usize,
            settled,
            &neighbours,
        );

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("an honest median payment must clear the group-median floor"),
            PaymentStatus::PaymentVerified
        );
    }

    /// The attack the floor exists to stop: collect the neighbourhood's quotes,
    /// settle only the cheapest one, submit it as a one-quote proof.
    #[tokio::test]
    async fn price_floor_rejects_cheapest_of_group_payment() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xD2u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);

        let cheapest = GROUP_TEST_KEY_COUNTS.first().copied().unwrap_or(0) as usize;
        let cheapest_price = price_at_records(cheapest);
        let tolerated = price_at_records(GROUP_TEST_MEDIAN_KEY_COUNT as usize)
            * Amount::from(PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT)
            / Amount::from(100u64);
        assert!(
            cheapest_price < tolerated,
            "the cheapest group price must sit below the tolerated floor for this test to bite"
        );

        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            cheapest,
            three_times(cheapest_price),
            &neighbours,
        );

        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("settling only the cheapest group quote must not clear the floor");

        assert!(
            format!("{err}").contains("below the close-group price floor"),
            "Error should name the group price floor: {err}"
        );
    }

    /// The anti-manipulation property: the reference price is built from this
    /// node's own routing view and the neighbours' signed gossip, never from the
    /// bundle. Padding the bundle with cheap quotes (the median-padding shape)
    /// must not move the floor.
    #[tokio::test]
    async fn price_floor_reference_ignores_client_supplied_quotes() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xD3u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);

        // A bundle stuffed with baseline-priced quotes: the client's own median
        // is now the floor price, which is exactly what it would want.
        let cheapest = GROUP_TEST_KEY_COUNTS.first().copied().unwrap_or(0) as usize;
        let mut peer_quotes = Vec::new();
        let mut k_closest: Vec<[u8; 32]> = neighbours.clone();
        for seed in 1..=CLOSE_GROUP_SIZE {
            let seed_u8 = u8::try_from(seed).unwrap_or(u8::MAX);
            let (peer_id, quote) = make_signed_quote(xorname, price_at_records(cheapest), seed_u8);
            k_closest.push(*peer_id.as_bytes());
            peer_quotes.push((peer_id, quote));
        }
        verifier.set_paid_quote_k_closest_for_tests(k_closest);
        // Settle the padded median honestly for that padded bundle.
        for (_, quote) in &peer_quotes {
            mark_candidate_paid(&verifier, quote, three_times(price_at_records(cheapest)));
        }
        let proof_bytes = serialize_proof(peer_quotes);

        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("a bundle padded with cheap quotes must not lower this node's floor");

        assert!(
            format!("{err}").contains("below the close-group price floor"),
            "Error should name the group price floor: {err}"
        );
    }

    /// Too thin a local view must SKIP, never reject. A settled payment cannot
    /// be refunded, so guessing a reference from a handful of peers would burn
    /// user money on a startup or post-churn window.
    ///
    /// One neighbour short of the gate, and built from the DEAREST counts on
    /// purpose: if the gate were deleted, the surviving sample would price the
    /// floor ABOVE the settled amount and the payment would be rejected, so this
    /// test fails rather than passing vacuously. Building it from the cheapest
    /// counts (the obvious choice) would pass either way and prove nothing.
    #[tokio::test]
    async fn price_floor_skips_when_group_view_is_too_thin() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xD4u8; 32];
        let thin: Vec<u32> = GROUP_TEST_KEY_COUNTS
            .iter()
            .rev()
            .copied()
            .take(PRICE_FLOOR_MIN_NEIGHBOUR_COMMITMENTS - 1)
            .collect();
        assert_eq!(thin.len(), PRICE_FLOOR_MIN_NEIGHBOUR_COMMITMENTS - 1);
        let neighbours = attach_group_commitments(&verifier, &thin, std::time::Duration::ZERO);

        // Deep underpayment that this sample, if it were used, would reject.
        let settled = three_times(price_at_records(0));
        let would_be_median = thin.get(thin.len() / 2).copied().unwrap_or_default();
        assert!(
            settled
                < price_at_records(would_be_median as usize)
                    * Amount::from(PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT)
                    / Amount::from(100u64)
                    * Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER),
            "the thin sample must price ABOVE the settlement, so deleting the gate fails this test"
        );
        let proof_bytes = group_floor_proof(&verifier, xorname, 0, settled, &neighbours);

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("a thin group view must skip the floor, not reject"),
            PaymentStatus::PaymentVerified
        );
    }

    /// Exactly ON the gate must evaluate, so the boundary is pinned from both
    /// sides alongside the one-short test above.
    #[tokio::test]
    async fn price_floor_evaluates_at_exactly_the_minimum_sample() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xDAu8; 32];
        assert_eq!(
            GROUP_TEST_KEY_COUNTS.len(),
            PRICE_FLOOR_MIN_NEIGHBOUR_COMMITMENTS,
            "the fixture must sit exactly on the gate for this boundary test"
        );
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);

        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            0,
            three_times(price_at_records(0)),
            &neighbours,
        );

        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("exactly at the minimum sample the floor must evaluate and reject");
        assert!(format!("{err}").contains("below the close-group price floor"));
    }

    /// A receiver with no live commitment of its own must skip.
    ///
    /// Such a node prices its OWN quotes at baseline, so holding an incoming
    /// payment to its neighbours' higher median would reject settlements it
    /// would itself have quoted. The gossip cache is fully populated here, so
    /// only the missing own commitment can cause the skip.
    #[tokio::test]
    async fn price_floor_skips_when_this_node_has_no_commitment() {
        let verifier = create_test_verifier();
        verifier.set_price_floor_for_tests(PriceFloorConfig {
            enforce: true,
            tolerance_percent: PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT,
        });
        let xorname = [0xDBu8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);

        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            0,
            three_times(price_at_records(0)),
            &neighbours,
        );

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("a receiver with no commitment of its own must skip the floor"),
            PaymentStatus::PaymentVerified
        );
    }

    /// A neighbour gossiping a count no honest node could hold must be dropped,
    /// not priced against.
    ///
    /// Gossip ingest authenticates the sender but does not bound the count,
    /// while quote validation rejects anything above `MAX_COMMITMENT_KEY_COUNT`.
    /// Without the filter, one such commitment inflates the reference and the
    /// floor becomes a way to reject other people's settled payments.
    #[tokio::test]
    async fn price_floor_ignores_commitments_above_the_protocol_cap() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xDCu8; 32];
        // The honest builder refuses to construct an over-cap commitment, so
        // forge one the way a modified node would: build a valid commitment and
        // overwrite the signed count. Dropping it leaves the sample one short of
        // the gate, so the floor skips — that cardinality change is what this
        // test detects if the filter is removed. One over-cap value alone would
        // NOT move a 16-sample median; inflating the reference that way needs
        // about a quarter of the sample, which is the cardinality bound on
        // `group_reference_price` rather than something this filter fixes.
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        let over_cap = crate::replication::commitment::MAX_COMMITMENT_KEY_COUNT + 1;
        {
            let cache = verifier
                .commitment_cache
                .read()
                .as_ref()
                .map(Arc::clone)
                .expect("cache attached above");
            let mut guard = cache.write().await;
            let victim = neighbours.last().copied().unwrap_or_default();
            if let Some(record) = guard.get_mut(&PeerId::from_bytes(victim)) {
                let mut forged = record
                    .last_commitment()
                    .cloned()
                    .expect("fixture record holds a commitment");
                forged.key_count = over_cap;
                *record = crate::replication::commitment_state::PeerCommitmentRecord::from_verified(
                    forged,
                    std::time::Instant::now(),
                );
            }
        }

        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            0,
            three_times(price_at_records(0)),
            &neighbours,
        );

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("an over-cap gossiped count must be dropped, leaving too thin a sample"),
            PaymentStatus::PaymentVerified
        );
    }

    /// Pins the other KNOWN LIMIT: four of fifteen OVERSTATING neighbours push
    /// the reference up far enough to reject an honest payment sitting at the
    /// true group median.
    ///
    /// This is the griefing direction, and the more serious of the two: the
    /// attacker spends nothing and the victim's money is already settled and
    /// unrefundable. Like its understating sibling this test asserts a weakness
    /// deliberately. If a future change improves the bound it will fail and must
    /// be re-derived rather than deleted.
    #[tokio::test]
    async fn group_median_is_griefed_by_four_overstated_commitments() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xE0u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        forge_key_counts(&verifier, neighbours.get(..4), MAX_COMMITMENT_KEY_COUNT).await;

        // A completely honest payment, priced at the TRUE group median.
        let honest = three_times(price_at_records(GROUP_TEST_MEDIAN_KEY_COUNT as usize));
        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            GROUP_TEST_MEDIAN_KEY_COUNT as usize,
            honest,
            &neighbours,
        );

        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err(
                "KNOWN LIMIT: four overstated neighbours reject an honest at-median payment. \
                 If this now succeeds, the bound improved — re-derive it and update the docs.",
            );
        assert!(format!("{err}").contains("below the close-group price floor"));
    }

    /// Pins the KNOWN LIMIT, so it is a fact in the test suite rather than a
    /// claim in a comment: four of fifteen understating neighbours DISARM the
    /// floor, admitting the cheapest-of-K underpayment it exists to reject.
    ///
    /// This test asserts a weakness on purpose. It is the concrete reason
    /// enforcement stays off, and if a future change improves the bound this
    /// test will fail and must be re-derived rather than quietly deleted.
    #[tokio::test]
    async fn group_median_is_disarmed_by_four_understated_commitments() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xDFu8; 32];
        let mut counts: Vec<u32> = GROUP_TEST_KEY_COUNTS.to_vec();
        for slot in counts.iter_mut().rev().take(4) {
            *slot = 1;
        }
        let neighbours = attach_group_commitments(&verifier, &counts, std::time::Duration::ZERO);

        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            0,
            three_times(price_at_records(0)),
            &neighbours,
        );

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect(
                "KNOWN LIMIT: four understated neighbours disarm the floor. If this now \
                 rejects, the bound improved — re-derive it and update the docs."
            ),
            PaymentStatus::PaymentVerified
        );
    }

    /// The griefing direction: a small minority gossiping OVERSTATED
    /// commitments must not push the reference up far enough to make this node
    /// reject an honest, already settled payment.
    ///
    /// This is the dangerous direction of a false commitment. Understating only
    /// costs a missed underpayment; overstating destroys someone else's money,
    /// and the attacker spends nothing to do it.
    ///
    /// The bound is real but partial, and worth stating precisely rather than
    /// claiming immunity. Each liar shifts the order statistic up by one rank,
    /// so what protects the payment is the tolerance headroom, not the median
    /// itself. In this fixture three liars move the reference `2_600` ->
    /// `3_500` and an at-median payment still clears, by only 1.9%; four move it
    /// to `3_800`
    /// and the same payment is REJECTED (pinned by
    /// `group_median_is_griefed_by_four_overstated_commitments`). The griefing
    /// bound is therefore about a quarter of the sample, the same as the
    /// disarming one — NOT "roughly half", which was the incorrect claim this
    /// policy originally rested on.
    ///
    /// Estimator coverage: this case fails under a 25th percentile, and its
    /// disarming sibling fails under a mean and under the upper median, so the
    /// pair pins the estimator jointly. Neither does so alone.
    #[tokio::test]
    async fn group_median_resists_three_overstated_commitments() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xDDu8; 32];
        // Three of fifteen neighbours claim the protocol maximum. Forged by
        // overwriting the signed count rather than building real 1,000,000-leaf
        // commitments, which cost about a second and 200 MB each.
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        forge_key_counts(&verifier, neighbours.get(..3), MAX_COMMITMENT_KEY_COUNT).await;

        // An honest payment at the TRUE group median must still be admitted.
        let honest = three_times(price_at_records(GROUP_TEST_MEDIAN_KEY_COUNT as usize));
        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            GROUP_TEST_MEDIAN_KEY_COUNT as usize,
            honest,
            &neighbours,
        );

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("a minority of overstated commitments must not reject honest payments"),
            PaymentStatus::PaymentVerified
        );
    }

    /// Stale gossip is treated as unknown rather than as a low price, so a peer
    /// going quiet can neither drag the median down nor be counted toward the
    /// minimum sample.
    ///
    /// Every neighbour is stale, so the sample collapses below the gate and the
    /// floor skips instead of pricing against this node's own (huge) count.
    #[tokio::test]
    async fn price_floor_ignores_stale_group_commitments() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xD5u8; 32];
        let stale_age = crate::replication::commitment_state::GOSSIP_ANSWERABILITY_TTL
            + std::time::Duration::from_mins(1);
        let neighbours = attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, stale_age);

        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            0,
            three_times(price_at_records(0)),
            &neighbours,
        );

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("stale gossip must not be priced against"),
            PaymentStatus::PaymentVerified
        );
    }

    /// A MIXED cache must exclude only the stale entries and price against the
    /// fresh ones.
    ///
    /// The all-stale test above collapses to the sample gate, so it would still
    /// pass if the TTL filter let stale counts through whenever enough fresh
    /// ones existed. This one pins the filter itself, and asserts on the
    /// reference directly rather than end-to-end.
    ///
    /// Going through `verify_payment` would need enough stale entries to move
    /// the median past the tolerance — more neighbours than production can ever
    /// present, since the K-closest scan returns at most 20 including self. The
    /// reference and the gated neighbour count are the precise observations
    /// anyway: if the TTL filter were removed, both would change.
    #[tokio::test]
    async fn price_floor_excludes_only_the_stale_entries_from_a_mixed_cache() {
        use crate::replication::commitment_state::PeerCommitmentRecord;

        let verifier = floor_test_verifier(true);
        let xorname = [0xDEu8; 32];
        let stale_age = crate::replication::commitment_state::GOSSIP_ANSWERABILITY_TTL
            + std::time::Duration::from_mins(1);

        // Fresh: the full fixture, exactly on the gate.
        let fresh =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);

        // Stale: four cheap counts, keeping the total within the 19 neighbours a
        // real routing view can supply.
        let stale_counts = [1u32; 4];
        let stale_received = std::time::Instant::now()
            .checked_sub(stale_age)
            .expect("test clock must support the TTL offset");
        let mut all = fresh.clone();
        {
            let cache = verifier
                .commitment_cache
                .read()
                .as_ref()
                .map(Arc::clone)
                .expect("cache attached above");
            let mut guard = cache.write().await;
            for (index, count) in stale_counts.iter().enumerate() {
                let built = test_built_commitment(*count);
                let mut raw = [0u8; 32];
                raw[0] = 0xF0;
                raw[1] = u8::try_from(index).unwrap_or(u8::MAX);
                all.push(raw);
                guard.insert(
                    PeerId::from_bytes(raw),
                    PeerCommitmentRecord::from_verified(built.commitment().clone(), stale_received),
                );
            }
        }
        verifier.set_paid_quote_k_closest_for_tests(all);

        let (reference, sample) = verifier.group_reference_price(&xorname).await;

        assert_eq!(
            sample.neighbours,
            GROUP_TEST_KEY_COUNTS.len(),
            "stale entries must not count toward the gate"
        );
        assert_eq!(
            reference,
            Some(price_at_records(GROUP_TEST_MEDIAN_KEY_COUNT as usize)),
            "the reference must be the fresh-only median; counting the stale cheap \
             entries would drag it below"
        );
    }

    /// Understated commitments pull the reference DOWN and weaken the floor.
    ///
    /// Three adversarially-placed liars leave it standing; four disarm it. The
    /// placement matters and is easy to get wrong: understating a peer that was
    /// already below the median changes nothing, so a test that rewrites the
    /// CHEAPEST entries passes while testing nothing. These rewrite the DEAREST
    /// ones, which is where an attacker would sit, and the count is set to the
    /// largest that still holds rather than to an aspirational majority.
    #[tokio::test]
    async fn group_median_resists_three_understated_commitments() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xD6u8; 32];
        // Three of fifteen neighbours claim to store almost nothing. `1`, not
        // `0`: a commitment over an empty key set cannot be built at all, so one
        // key is the least a peer can honestly (or dishonestly) commit to.
        let mut counts: Vec<u32> = GROUP_TEST_KEY_COUNTS.to_vec();
        for slot in counts.iter_mut().rev().take(3) {
            *slot = 1;
        }
        let neighbours = attach_group_commitments(&verifier, &counts, std::time::Duration::ZERO);

        let cheapest_price = price_at_records(0);
        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            0,
            three_times(cheapest_price),
            &neighbours,
        );

        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("a minority of understated commitments must not disarm the floor");

        assert!(
            format!("{err}").contains("below the close-group price floor"),
            "Error should name the group price floor: {err}"
        );
    }

    #[tokio::test]
    async fn test_price_floor_shadow_mode_accepts_below_floor_settlement() {
        let verifier = floor_test_verifier(false);
        let xorname = [0xC1u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        let settled = three_times(price_at_records(0));
        assert!(
            settled < group_floor_required_amount(),
            "shadow mode is only meaningful on a settlement that WOULD be rejected"
        );
        let proof_bytes = group_floor_proof(&verifier, xorname, 0, settled, &neighbours);

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("shadow mode must never reject on the floor"),
            PaymentStatus::PaymentVerified
        );
    }

    #[tokio::test]
    async fn test_price_floor_enforced_rejects_cheapest_of_k_settlement() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xC2u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        let settled = three_times(price_at_records(0));
        let proof_bytes = group_floor_proof(&verifier, xorname, 0, settled, &neighbours);

        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("a 3x-baseline settlement must not clear the group-median floor");

        assert!(
            format!("{err}").contains("below the close-group price floor"),
            "Error should name the price floor: {err}"
        );
    }

    #[tokio::test]
    async fn test_price_floor_enforced_accepts_exactly_at_floor_overpayment() {
        // An honest client may OVERPAY a cheap quote to clear stricter
        // receivers: the floor compares the settled amount, not the quote
        // price, and exactly-at-floor must pass.
        let verifier = floor_test_verifier(true);
        let xorname = [0xC3u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        let proof_bytes = group_floor_proof(
            &verifier,
            xorname,
            0,
            group_floor_required_amount(),
            &neighbours,
        );

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("an exactly-at-floor settlement must pass"),
            PaymentStatus::PaymentVerified
        );
    }

    #[tokio::test]
    async fn test_price_floor_enforced_applies_to_fresh_replication() {
        // Fresh replication is the same fresh economic event as a direct PUT;
        // exempting it would let one cheap accepting node fan the proof out
        // around every other storer's floor.
        let verifier = floor_test_verifier(true);
        let xorname = [0xC4u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        let settled = three_times(price_at_records(0));
        let proof_bytes = group_floor_proof(&verifier, xorname, 0, settled, &neighbours);

        let err = verifier
            .verify_payment(
                &xorname,
                Some(&proof_bytes),
                VerificationContext::FreshReplication,
            )
            .await
            .expect_err("fresh replication must enforce the same floor as direct PUTs");

        assert!(
            format!("{err}").contains("below the close-group price floor"),
            "Error should name the price floor: {err}"
        );
    }

    #[tokio::test]
    async fn test_price_floor_never_reprices_paid_list_admission() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xC5u8; 32];
        let neighbours =
            attach_group_commitments(&verifier, &GROUP_TEST_KEY_COUNTS, std::time::Duration::ZERO);
        let settled = three_times(price_at_records(0));
        let proof_bytes = group_floor_proof(&verifier, xorname, 0, settled, &neighbours);

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&proof_bytes),
                VerificationContext::PaidListAdmission,
            )
            .await;

        assert_eq!(
            result.expect("paid-list admission reprices no fresh economic decision"),
            PaymentStatus::PaymentVerified
        );
    }

    /// A node that HAS its own commitment but no gossip cache must skip.
    ///
    /// This is the case that actually distinguishes the new behaviour from the
    /// old: under the withdrawn own-price rule this receiver priced the floor at
    /// its own enormous count and rejected the settlement. Now it has no group
    /// view, so it does not evaluate. Using a verifier with no commitment source
    /// at all (the obvious setup) would have passed under both rules and proved
    /// nothing.
    #[tokio::test]
    async fn test_price_floor_without_any_group_view_skips() {
        let verifier = floor_test_verifier(true);
        let xorname = [0xC6u8; 32];
        let settled = three_times(price_at_records(0));
        assert!(
            settled
                < price_at_records(FLOOR_TEST_LOCAL_KEY_COUNT as usize)
                    * Amount::from(PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT)
                    / Amount::from(100u64)
                    * Amount::from(PAID_QUOTE_PAYMENT_MULTIPLIER),
            "the old own-price rule would have rejected this settlement, so the skip is load-bearing"
        );
        let proof_bytes = cheap_single_quote_proof(&verifier, xorname, settled);

        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("no group view must skip the floor, never reject"),
            PaymentStatus::PaymentVerified
        );
    }

    /// `from_env` must fail CLOSED: a present-but-invalid tolerance can never
    /// leave enforcement on with a tolerance the operator did not specify.
    /// (This test owns the `PRICE_FLOOR_*` env vars — no other test reads them.)
    #[test]
    fn price_floor_from_env_fails_closed_on_invalid_tolerance() {
        // Defaults with nothing set.
        std::env::remove_var(PRICE_FLOOR_ENFORCE_ENV);
        std::env::remove_var(PRICE_FLOOR_TOLERANCE_ENV);
        let cfg = PriceFloorConfig::from_env();
        assert!(!cfg.enforce);
        assert_eq!(cfg.tolerance_percent, PRICE_FLOOR_DEFAULT_TOLERANCE_PERCENT);

        // Enforce on, valid tolerance: honoured.
        std::env::set_var(PRICE_FLOOR_ENFORCE_ENV, "1");
        std::env::set_var(PRICE_FLOOR_TOLERANCE_ENV, "80");
        let cfg = PriceFloorConfig::from_env();
        assert!(cfg.enforce);
        assert_eq!(cfg.tolerance_percent, 80);

        // Enforce on, out-of-range tolerance: enforcement DISABLED (fail closed).
        std::env::set_var(PRICE_FLOOR_TOLERANCE_ENV, "150");
        let cfg = PriceFloorConfig::from_env();
        assert!(
            !cfg.enforce,
            "an out-of-range tolerance must not silently enforce a default"
        );

        // Enforce on, unparseable tolerance: also disabled.
        std::env::set_var(PRICE_FLOOR_TOLERANCE_ENV, "loose");
        let cfg = PriceFloorConfig::from_env();
        assert!(!cfg.enforce);

        std::env::remove_var(PRICE_FLOOR_ENFORCE_ENV);
        std::env::remove_var(PRICE_FLOOR_TOLERANCE_ENV);
    }

    #[tokio::test]
    async fn test_legacy_single_quote_proof_requires_three_x_payment() {
        let verifier = create_test_verifier();
        let xorname = [0xB2u8; 32];
        let (peer_id, quote) = make_signed_quote(xorname, price_at_records(0), 1);
        let peer_quotes = vec![(peer_id, quote.clone())];
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        mark_candidate_paid(&verifier, &quote, quote.price);

        let proof_bytes = serialize_proof(peer_quotes);
        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("single quote paid less than 3x should be rejected");

        assert!(
            format!("{err}").contains("not paid enough"),
            "Error should mention underpayment: {err}"
        );
    }

    #[tokio::test]
    async fn test_legacy_too_many_quotes_rejected() {
        let verifier = create_test_verifier();
        let xorname = [0xB3u8; 32];
        let mut peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        peer_quotes.push(make_signed_quote(xorname, price_at_records(7), 8));

        let proof_bytes = serialize_proof(peer_quotes);
        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("proof with more than close-group quotes should be rejected");

        assert!(
            format!("{err}").contains("at most"),
            "Error should mention max quote count: {err}"
        );
    }

    #[tokio::test]
    async fn test_legacy_structural_majority_price_at_median_accepted() {
        let verifier = create_test_verifier();
        let xorname = [0xA2u8; 32];
        let peer_quotes = make_signed_legacy_bundle(
            xorname,
            [
                crate::payment::pricing::calculate_price(0),
                crate::payment::pricing::calculate_price(100),
                crate::payment::pricing::calculate_price(500),
                crate::payment::pricing::calculate_price(1000),
                crate::payment::pricing::calculate_price(2000),
                crate::payment::pricing::calculate_price(4000),
                crate::payment::pricing::calculate_price(6000),
            ],
        );
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("median-priced verifier should accept"),
            PaymentStatus::PaymentVerified
        );
    }

    #[tokio::test]
    async fn test_legacy_paid_median_issuer_k_closest_rejection() {
        let verifier = create_test_verifier();
        verifier.set_paid_quote_k_closest_for_tests(vec![rand::random()]);
        let xorname = [0xA4u8; 32];
        let peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("out-of-K paid issuer should be rejected");

        assert!(
            format!("{err}").contains("not among this node's local"),
            "Error should mention local K-closest peers: {err}"
        );
    }

    /// A zero-priced median must never be admitted.
    ///
    /// Which check rejects it depends on the ADR-0004 rollout gate. `Amount::ZERO`
    /// is not a point on the pricing curve (the curve's minimum is
    /// `calculate_price(0)`), so with
    /// [`crate::replication::config::QUOTE_ARITHMETIC_RECHECK_ENABLED`] on, the
    /// arithmetic gate rejects the bundle before median selection runs and the
    /// dedicated zero-price check is no longer reachable for single-node quotes.
    /// The gate subsumes it rather than replacing it, so both messages are
    /// accepted here and the invariant under test stays "this is rejected".
    #[tokio::test]
    async fn test_legacy_zero_price_median_rejected() {
        let verifier = create_test_verifier();
        let xorname = [0xA6u8; 32];
        let peer_quotes = make_signed_legacy_bundle(
            xorname,
            [
                Amount::ZERO,
                Amount::ZERO,
                Amount::ZERO,
                Amount::ZERO,
                Amount::from(1u64),
                Amount::from(2u64),
                Amount::from(3u64),
            ],
        );

        let proof_bytes = serialize_proof(peer_quotes);
        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("zero median must be rejected");

        let msg = format!("{err}");
        assert!(
            msg.contains("zero price") || msg.contains("off-curve quote rejected"),
            "Error must reject the zero-priced median, by either the zero-price \
             check or the ADR-0004 arithmetic gate: {msg}"
        );
    }

    #[tokio::test]
    async fn test_legacy_paid_quote_content_mismatch_rejected() {
        let verifier = create_test_verifier();
        let xorname = [0xA7u8; 32];
        let mut peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        let median_index = median_quote_index(peer_quotes.len());
        peer_quotes[median_index].1.content = xor_name::XorName([0xE7u8; 32]);
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);

        let proof_bytes = serialize_proof(peer_quotes);
        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("paid quote content mismatch should be rejected");

        assert!(
            format!("{err}").contains("content address mismatch"),
            "Error should mention content mismatch: {err}"
        );
    }

    #[tokio::test]
    async fn test_legacy_unpaid_quote_content_mismatch_accepted() {
        let verifier = create_test_verifier();
        let xorname = [0xA8u8; 32];
        let mut peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        peer_quotes[0].1.content = xor_name::XorName([0xE8u8; 32]);
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("unpaid content mismatch should be ignored"),
            PaymentStatus::PaymentVerified
        );
    }

    #[tokio::test]
    async fn test_legacy_paid_quote_bad_signature_rejected() {
        let verifier = create_test_verifier();
        let xorname = [0xA9u8; 32];
        let mut peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        let median_index = median_quote_index(peer_quotes.len());
        peer_quotes[median_index].1.signature.push(0xFF);
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let err = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await
            .expect_err("paid bad signature should be rejected");

        assert!(
            format!("{err}").contains("signature verification failed"),
            "Error should mention signature failure: {err}"
        );
    }

    #[tokio::test]
    async fn test_legacy_unpaid_quote_bad_signature_accepted() {
        let verifier = create_test_verifier();
        let xorname = [0xAAu8; 32];
        let mut peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        peer_quotes[0].1.signature.push(0xFF);
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("unpaid bad signature should be ignored"),
            PaymentStatus::PaymentVerified
        );
    }

    #[tokio::test]
    async fn test_legacy_unpaid_peer_binding_mismatch_accepted() {
        let verifier = create_test_verifier();
        let xorname = [0xABu8; 32];
        let mut peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        peer_quotes[0].0 = evmlib::EncodedPeerId::new(rand::random());
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("unpaid peer binding mismatch should be ignored"),
            PaymentStatus::PaymentVerified
        );
    }

    #[tokio::test]
    async fn test_legacy_median_tie_accepts_paid_candidate() {
        let verifier = create_test_verifier();
        let xorname = [0xACu8; 32];
        let peer_quotes = make_signed_legacy_bundle(xorname, tied_median_test_prices());
        mark_k_closest_paid_candidates(&verifier, &peer_quotes);
        mark_all_median_candidates_unpaid(&verifier, &peer_quotes);
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .get(1)
            .expect("second tied median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert_eq!(
            result.expect("one paid tied median candidate should verify"),
            PaymentStatus::PaymentVerified
        );
    }

    #[tokio::test]
    async fn test_legacy_paid_list_admission_enforces_issuer_k_closest() {
        let verifier = create_test_verifier();
        verifier.set_paid_quote_k_closest_for_tests(Vec::new());
        let xorname = [0xB5u8; 32];
        let peer_quotes = make_signed_legacy_bundle(xorname, unique_test_prices());
        let expected_amount = expected_median_payment(&peer_quotes);
        let paid_quote = median_test_candidates(&peer_quotes)
            .first()
            .expect("median candidate")
            .1
            .clone();
        mark_candidate_paid(&verifier, &paid_quote, expected_amount);

        let proof_bytes = serialize_proof(peer_quotes);
        let err = verifier
            .verify_payment(
                &xorname,
                Some(&proof_bytes),
                VerificationContext::PaidListAdmission,
            )
            .await
            .expect_err("paid-list admission must enforce the paid issuer K-closest check");

        assert!(
            format!("{err}").contains("not among this node's local"),
            "Error should mention local K-closest peers: {err}"
        );
    }

    #[test]
    fn test_cache_len_getter() {
        let verifier = create_test_verifier();
        assert_eq!(verifier.cache_len(), 0);

        verifier.cache.insert([10u8; 32]);
        assert_eq!(verifier.cache_len(), 1);

        verifier.cache.insert([20u8; 32]);
        assert_eq!(verifier.cache_len(), 2);
    }

    #[test]
    fn test_cache_stats_after_operations() {
        let verifier = create_test_verifier();
        let xorname = [7u8; 32];

        // Miss
        verifier.check_payment_required(&xorname, VerificationContext::ClientPut);
        let stats = verifier.cache_stats();
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hits, 0);

        // Insert and hit
        verifier.cache.insert(xorname);
        verifier.check_payment_required(&xorname, VerificationContext::ClientPut);
        let stats = verifier.cache_stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.additions, 1);
    }

    #[tokio::test]
    async fn test_concurrent_cache_lookups() {
        let verifier = std::sync::Arc::new(create_test_verifier());

        // Pre-populate cache for all 10 xornames
        for i in 0..10u8 {
            verifier.cache.insert([i; 32]);
        }

        let mut handles = Vec::new();
        for i in 0..10u8 {
            let v = verifier.clone();
            handles.push(tokio::spawn(async move {
                let xorname = [i; 32];
                v.verify_payment(&xorname, None, VerificationContext::ClientPut)
                    .await
            }));
        }

        for handle in handles {
            let result = handle.await.expect("task panicked");
            assert!(result.is_ok());
            assert_eq!(result.expect("cached"), PaymentStatus::CachedAsVerified);
        }

        assert_eq!(verifier.cache_len(), 10);
    }

    #[test]
    fn test_default_evm_config() {
        let _config = EvmVerifierConfig::default();
        // EVM is always on — default network is ArbitrumOne
    }

    #[test]
    fn test_real_ml_dsa_proof_size_within_limits() {
        use crate::payment::metrics::QuotingMetricsTracker;
        use crate::payment::proof::PaymentProof;
        use crate::payment::quote::{QuoteGenerator, XorName};
        use alloy::primitives::FixedBytes;
        use evmlib::{EncodedPeerId, RewardsAddress};
        use saorsa_core::MlDsa65;
        use saorsa_pqc::pqc::types::MlDsaSecretKey;
        use saorsa_pqc::pqc::MlDsaOperations;

        let ml_dsa = MlDsa65::new();
        let mut peer_quotes = Vec::new();

        for i in 0..5u8 {
            let (public_key, secret_key) = ml_dsa.generate_keypair().expect("keygen");

            let rewards_address = RewardsAddress::new([i; 20]);
            let metrics_tracker = QuotingMetricsTracker::new(0);
            let mut generator = QuoteGenerator::new(rewards_address, metrics_tracker);

            let pub_key_bytes = public_key.as_bytes().to_vec();
            let sk_bytes = secret_key.as_bytes().to_vec();
            generator.set_signer(pub_key_bytes, move |msg| {
                let sk = MlDsaSecretKey::from_bytes(&sk_bytes).expect("sk parse");
                let ml_dsa = MlDsa65::new();
                ml_dsa.sign(&sk, msg).expect("sign").as_bytes().to_vec()
            });

            let content: XorName = [i; 32];
            let quote = generator.create_quote(content, 4096, 0).expect("quote");

            peer_quotes.push((EncodedPeerId::new(rand::random()), quote));
        }

        let proof = PaymentProof {
            proof_of_payment: ProofOfPayment { peer_quotes },
            tx_hashes: vec![FixedBytes::from([0xABu8; 32])],
            commitment_sidecars: vec![],
        };

        let proof_bytes =
            crate::payment::proof::serialize_single_node_proof(&proof).expect("serialize");

        // 7 ML-DSA-65 quotes with ~1952-byte pub keys and ~3309-byte signatures
        // should produce a proof in the 30-80 KB range
        assert!(
            proof_bytes.len() > 20_000,
            "Real 7-quote ML-DSA proof should be > 20 KB, got {} bytes",
            proof_bytes.len()
        );
        assert!(
            proof_bytes.len() < MAX_PAYMENT_PROOF_SIZE_BYTES,
            "Real 7-quote ML-DSA proof ({} bytes) should fit within {} byte limit",
            proof_bytes.len(),
            MAX_PAYMENT_PROOF_SIZE_BYTES
        );
    }

    #[tokio::test]
    async fn test_content_address_mismatch_rejected() {
        use crate::payment::proof::{serialize_single_node_proof, PaymentProof};
        use evmlib::{EncodedPeerId, PaymentQuote, RewardsAddress};
        use std::time::SystemTime;

        let verifier = create_test_verifier();

        // The xorname we're trying to store
        let target_xorname = [0xAAu8; 32];

        // Create a quote for a DIFFERENT xorname
        let wrong_xorname = [0xBBu8; 32];
        let quote = PaymentQuote {
            content: xor_name::XorName(wrong_xorname),
            timestamp: SystemTime::now(),
            // Baseline-priced so the quote is on-curve: this test is about the
            // content binding, and an off-curve price would be rejected by the
            // ADR-0004 arithmetic gate first, never reaching that check.
            price: crate::payment::pricing::calculate_price(0),
            rewards_address: RewardsAddress::new([1u8; 20]),
            committed_key_count: 0,
            commitment_pin: None,
            pub_key: vec![0u8; 64],
            signature: vec![0u8; 64],
        };

        // Build CLOSE_GROUP_SIZE quotes with distinct peer IDs
        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            peer_quotes.push((EncodedPeerId::new(rand::random()), quote.clone()));
        }

        let proof = PaymentProof {
            proof_of_payment: ProofOfPayment { peer_quotes },
            tx_hashes: vec![],
            commitment_sidecars: vec![],
        };

        let proof_bytes = serialize_single_node_proof(&proof).expect("serialize proof");

        let result = verifier
            .verify_payment(
                &target_xorname,
                Some(&proof_bytes),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(result.is_err(), "Should reject mismatched content address");
        let err_msg = format!("{}", result.expect_err("should be error"));
        assert!(
            err_msg.contains("content address mismatch"),
            "Error should mention 'content address mismatch': {err_msg}"
        );
    }

    /// Helper: create a fake quote with the given xorname and timestamp.
    fn make_fake_quote(
        xorname: [u8; 32],
        timestamp: SystemTime,
        rewards_address: RewardsAddress,
    ) -> evmlib::PaymentQuote {
        use evmlib::PaymentQuote;

        PaymentQuote {
            content: xor_name::XorName(xorname),
            timestamp,
            // Baseline quote: `(committed_key_count = 0, commitment_pin = None)`
            // must be priced at `calculate_price(0)` to satisfy the ADR-0004
            // binding rule. An arbitrary placeholder price here is off-curve and
            // is rejected outright once
            // [`crate::replication::config::QUOTE_ARITHMETIC_RECHECK_ENABLED`] is
            // on, which would mask whatever a test actually means to assert.
            price: crate::payment::pricing::calculate_price(0),
            rewards_address,
            committed_key_count: 0,
            commitment_pin: None,
            pub_key: vec![0u8; 64],
            signature: vec![0u8; 64],
        }
    }

    /// Helper: create a fake quote priced on-curve at `records` stored records
    /// (price = `calculate_price(records)`), reusing [`make_fake_quote`] for the
    /// remaining fields. Used by the ADR-0004 arithmetic-gate tests.
    ///
    /// Sets the whole binding, not just the price: ADR-0004 requires `price ==
    /// calculate_price(committed_key_count)` AND the `(n > 0, Some(pin))` /
    /// `(0, None)` shape, so pricing at `records` while leaving the count at `0`
    /// produces a quote that claims to store nothing yet charges as though it
    /// stored `records` keys. That is exactly what the gate rejects.
    fn make_fake_quote_at_records(
        xorname: [u8; 32],
        timestamp: SystemTime,
        rewards_address: RewardsAddress,
        records: usize,
    ) -> evmlib::PaymentQuote {
        let mut quote = make_fake_quote(xorname, timestamp, rewards_address);
        quote.price = crate::payment::pricing::calculate_price(records);
        quote.committed_key_count = u32::try_from(records).unwrap_or(u32::MAX);
        quote.commitment_pin = if records == 0 {
            None
        } else {
            // Any stable non-zero pin: these fixtures never resolve it to a real
            // commitment, they only need the binding shape to be coherent.
            Some([0xA5u8; 32])
        };
        quote
    }

    /// Helper: wrap quotes into a tagged serialized `PaymentProof`.
    fn serialize_proof(peer_quotes: Vec<(evmlib::EncodedPeerId, evmlib::PaymentQuote)>) -> Vec<u8> {
        use crate::payment::proof::{serialize_single_node_proof, PaymentProof};

        let proof = PaymentProof {
            proof_of_payment: ProofOfPayment { peer_quotes },
            tx_hashes: vec![],
            commitment_sidecars: vec![],
        };
        serialize_single_node_proof(&proof).expect("serialize proof")
    }

    #[tokio::test]
    async fn test_old_quote_uses_storage_delta_not_timestamp() {
        use evmlib::{EncodedPeerId, RewardsAddress};
        use std::time::Duration;

        let verifier = create_test_verifier();
        let xorname = [0xCCu8; 32];
        let rewards_addr = RewardsAddress::new([1u8; 20]);

        // Create a quote that's 25 hours old (exceeds 24-hour max)
        let old_timestamp = SystemTime::now() - Duration::from_hours(25);
        let quote = make_fake_quote(xorname, old_timestamp, rewards_addr);

        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            peer_quotes.push((EncodedPeerId::new(rand::random()), quote.clone()));
        }

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        let err_msg = format!("{}", result.expect_err("should fail at later check"));
        assert!(
            !err_msg.contains("expired"),
            "Should not reject by timestamp age: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_future_quote_uses_storage_delta_not_timestamp() {
        use evmlib::{EncodedPeerId, RewardsAddress};
        use std::time::Duration;

        let verifier = create_test_verifier();
        let xorname = [0xDDu8; 32];
        let rewards_addr = RewardsAddress::new([1u8; 20]);

        // Create a quote with a timestamp 1 hour in the future
        let future_timestamp = SystemTime::now() + Duration::from_hours(1);
        let quote = make_fake_quote(xorname, future_timestamp, rewards_addr);

        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            peer_quotes.push((EncodedPeerId::new(rand::random()), quote.clone()));
        }

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        let err_msg = format!("{}", result.expect_err("should fail at later check"));
        assert!(
            !err_msg.contains("future"),
            "Should not reject by future timestamp: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_quote_within_clock_skew_tolerance_accepted() {
        use evmlib::{EncodedPeerId, RewardsAddress};
        use std::time::Duration;

        let verifier = create_test_verifier();
        let xorname = [0xD1u8; 32];
        let rewards_addr = RewardsAddress::new([1u8; 20]);

        // Quote 30 seconds in the future — well within 300s tolerance
        let future_timestamp = SystemTime::now() + Duration::from_secs(30);
        let quote = make_fake_quote(xorname, future_timestamp, rewards_addr);

        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            peer_quotes.push((EncodedPeerId::new(rand::random()), quote.clone()));
        }

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        // Should NOT fail at timestamp check (will fail later at pub_key binding)
        let err_msg = format!("{}", result.expect_err("should fail at later check"));
        assert!(
            !err_msg.contains("future"),
            "Should pass timestamp check (within tolerance), but got: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_quote_beyond_clock_skew_still_uses_storage_delta() {
        use evmlib::{EncodedPeerId, RewardsAddress};
        use std::time::Duration;

        let verifier = create_test_verifier();
        let xorname = [0xD2u8; 32];
        let rewards_addr = RewardsAddress::new([1u8; 20]);

        // Quote 360 seconds in the future — exceeds 300s tolerance
        let future_timestamp = SystemTime::now() + Duration::from_mins(6);
        let quote = make_fake_quote(xorname, future_timestamp, rewards_addr);

        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            peer_quotes.push((EncodedPeerId::new(rand::random()), quote.clone()));
        }

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        let err_msg = format!("{}", result.expect_err("should fail at later check"));
        assert!(
            !err_msg.contains("future"),
            "Should not reject by future timestamp: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_quote_23h_old_still_accepted() {
        use evmlib::{EncodedPeerId, RewardsAddress};
        use std::time::Duration;

        let verifier = create_test_verifier();
        let xorname = [0xD3u8; 32];
        let rewards_addr = RewardsAddress::new([1u8; 20]);

        // Quote 23 hours old — within 24h max age
        let old_timestamp = SystemTime::now() - Duration::from_hours(23);
        let quote = make_fake_quote(xorname, old_timestamp, rewards_addr);

        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            peer_quotes.push((EncodedPeerId::new(rand::random()), quote.clone()));
        }

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        // Should NOT fail at timestamp check (will fail later at pub_key binding)
        let err_msg = format!("{}", result.expect_err("should fail at later check"));
        assert!(
            !err_msg.contains("expired"),
            "Should pass expiry check (23h < 24h), but got: {err_msg}"
        );
    }

    /// Helper: build an `EncodedPeerId` that matches the BLAKE3 hash of an ML-DSA public key.
    fn encoded_peer_id_for_pub_key(pub_key: &[u8]) -> evmlib::EncodedPeerId {
        let ant_peer_id = peer_id_from_public_key_bytes(pub_key).expect("valid ML-DSA pub key");
        evmlib::EncodedPeerId::new(*ant_peer_id.as_bytes())
    }

    #[tokio::test]
    async fn test_wrong_peer_binding_rejected() {
        use evmlib::{EncodedPeerId, RewardsAddress};
        use saorsa_core::MlDsa65;
        use saorsa_pqc::pqc::MlDsaOperations;

        let verifier = create_test_verifier();
        let xorname = [0xFFu8; 32];
        let rewards_addr = RewardsAddress::new([1u8; 20]);

        // Generate a real ML-DSA keypair so pub_key is valid
        let ml_dsa = MlDsa65::new();
        let (public_key, _secret_key) = ml_dsa.generate_keypair().expect("keygen");
        let pub_key_bytes = public_key.as_bytes().to_vec();

        // Create a quote with a real pub_key but attach it to a random peer ID
        // whose identity multihash does NOT match BLAKE3(pub_key)
        let mut quote = make_fake_quote(xorname, SystemTime::now(), rewards_addr);
        quote.pub_key = pub_key_bytes;

        // Use random ed25519 peer IDs — they won't match BLAKE3(pub_key)
        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            peer_quotes.push((EncodedPeerId::new(rand::random()), quote.clone()));
        }

        let proof_bytes = serialize_proof(peer_quotes);
        let result = verifier
            .verify_payment(&xorname, Some(&proof_bytes), VerificationContext::ClientPut)
            .await;

        assert!(result.is_err(), "Should reject wrong peer binding");
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("pub_key does not belong to claimed peer"),
            "Error should mention binding mismatch: {err_msg}"
        );
    }

    // =========================================================================
    // VerificationContext tests — both contexts verify fresh proof admissions.
    // Later neighbour-sync repair has no proof-of-payment and is authorized by
    // closest-7 storage quorum or closest-K paid-list quorum instead.
    // =========================================================================

    /// Content binding is required for every fresh proof context. A receipt for
    /// chunk A cannot admit chunk B as either a direct/fresh store or a fresh
    /// paid-list update.
    #[tokio::test]
    async fn test_fresh_contexts_reject_content_mismatch() {
        let verifier = create_test_verifier();
        let stored_xorname = [0xD2u8; 32];
        let quoted_xorname = [0xD3u8; 32];
        let rewards = RewardsAddress::new([1u8; 20]);

        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            let quote = make_fake_quote(quoted_xorname, SystemTime::now(), rewards);
            peer_quotes.push((evmlib::EncodedPeerId::new(rand::random()), quote));
        }
        let proof_bytes = serialize_proof(peer_quotes);

        for context in [
            VerificationContext::ClientPut,
            VerificationContext::FreshReplication,
            VerificationContext::PaidListAdmission,
        ] {
            let err = verifier
                .verify_payment(&stored_xorname, Some(&proof_bytes), context)
                .await
                .expect_err("content binding must hold in every context");
            assert!(
                format!("{err}").contains("content address mismatch"),
                "{context:?} must reject a receipt for a different address: {err}"
            );
        }
    }

    /// The merkle pay-yourself closeness defence (including its duplicate-
    /// candidate pre-check, which runs without a `P2PNode`) applies to every
    /// proof verification context because every context is a fresh admission.
    #[tokio::test]
    async fn test_fresh_contexts_enforce_merkle_closeness() {
        let verifier = create_test_verifier();

        let (mut merkle_proof, _pool_hash, xorname, _timestamp) = make_valid_merkle_proof();

        // 16 copies of one real candidate: every self-signature is valid, but
        // the candidate PeerIds are duplicates — the closeness pre-check
        // rejects this pool on a client PUT.
        let shared = merkle_proof
            .winner_pool
            .candidate_nodes
            .first()
            .expect("candidates")
            .clone();
        for c in &mut merkle_proof.winner_pool.candidate_nodes {
            *c = shared.clone();
        }
        let tagged =
            crate::payment::proof::serialize_merkle_proof(&merkle_proof).expect("serialize");

        for context in [
            VerificationContext::ClientPut,
            VerificationContext::FreshReplication,
            VerificationContext::PaidListAdmission,
        ] {
            let err = verifier
                .verify_payment(&xorname, Some(&tagged), context)
                .await
                .expect_err("duplicate candidate PeerIds must fail fresh admission closeness");
            assert!(
                format!("{err}").contains("duplicate candidate PeerId"),
                "{context:?} must fail at the closeness pre-check: {err}"
            );
        }
    }

    // =========================================================================
    // Merkle-tagged proof tests
    // =========================================================================

    #[tokio::test]
    async fn test_merkle_tagged_proof_invalid_data_rejected() {
        use crate::ant_protocol::PROOF_TAG_MERKLE;

        let verifier = merkle_test_verifier();
        let xorname = [0xA1u8; 32];

        // Build a merkle-tagged proof with garbage body.
        // The tag byte is correct but the body is not valid msgpack.
        let mut merkle_garbage = Vec::with_capacity(64);
        merkle_garbage.push(PROOF_TAG_MERKLE);
        merkle_garbage.extend_from_slice(&[0xAB; 63]);

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&merkle_garbage),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(
            result.is_err(),
            "Should reject merkle proof with invalid body"
        );
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("deserialize") || err_msg.contains("merkle proof"),
            "Error should mention deserialization failure: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_single_node_tagged_proof_deserialization() {
        use crate::payment::proof::serialize_single_node_proof;
        use evmlib::{EncodedPeerId, RewardsAddress};

        let verifier = create_test_verifier();
        let xorname = [0xA2u8; 32];
        let rewards_addr = RewardsAddress::new([1u8; 20]);

        // Build a valid tagged single-node proof
        let quote = make_fake_quote(xorname, SystemTime::now(), rewards_addr);
        let mut peer_quotes = Vec::new();
        for _ in 0..CLOSE_GROUP_SIZE {
            peer_quotes.push((EncodedPeerId::new(rand::random()), quote.clone()));
        }

        let proof = crate::payment::proof::PaymentProof {
            proof_of_payment: ProofOfPayment {
                peer_quotes: peer_quotes.clone(),
            },
            tx_hashes: vec![],
            commitment_sidecars: vec![],
        };

        let tagged_bytes = serialize_single_node_proof(&proof).expect("serialize tagged proof");

        // detect_proof_type should identify it as SingleNode
        assert_eq!(
            crate::payment::proof::detect_proof_type(&tagged_bytes),
            Some(crate::payment::proof::ProofType::SingleNode)
        );

        // verify_payment should process it through the single-node path.
        // It will fail at quote validation (fake pub_key), but we verify
        // it passes the deserialization stage by checking the error type.
        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_bytes),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(result.is_err(), "Should fail at quote validation stage");
        let err_msg = format!("{}", result.expect_err("should fail"));
        // It should NOT be a deserialization error — it should get further
        assert!(
            !err_msg.contains("deserialize"),
            "Should pass deserialization but fail later: {err_msg}"
        );
    }

    #[test]
    fn test_pool_cache_insert_and_lookup() {
        use evmlib::merkle_batch_payment::PoolHash;

        // Verify the pool_cache field exists and works correctly.
        // Insert a pool hash, then verify it's present on lookup.
        let verifier = create_test_verifier();

        let pool_hash: PoolHash = [0xBBu8; 32];
        let payment_info = evmlib::merkle_payments::OnChainPaymentInfo {
            depth: 4,
            merkle_payment_timestamp: 1_700_000_000,
            paid_node_addresses: vec![],
        };

        // Insert into pool cache
        {
            let mut cache = verifier.pool_cache.lock();
            cache.put(pool_hash, payment_info);
        }

        // First lookup — should find it
        {
            let found = verifier.pool_cache.lock().get(&pool_hash).cloned();
            assert!(found.is_some(), "Pool hash should be in cache after insert");
            let info = found.expect("cached info");
            assert_eq!(info.depth, 4);
            assert_eq!(info.merkle_payment_timestamp, 1_700_000_000);
        }

        // Second lookup — same result (no double-query needed)
        {
            let found = verifier.pool_cache.lock().get(&pool_hash).cloned();
            assert!(
                found.is_some(),
                "Pool hash should still be in cache on second lookup"
            );
        }

        // Different pool hash — should NOT be found
        let other_hash: PoolHash = [0xCCu8; 32];
        {
            let found = verifier.pool_cache.lock().get(&other_hash).cloned();
            assert!(found.is_none(), "Unknown pool hash should not be in cache");
        }
    }

    #[tokio::test]
    async fn closeness_pass_cache_short_circuits_second_call() {
        // When a pool_hash is in the closeness_pass_cache, the outer
        // verify_merkle_candidate_closeness must return Ok(()) without
        // running the inner lookup — even if no P2PNode is attached.
        // That second half (no-p2p → would normally fail-closed in release)
        // is the proof the cache short-circuit ran first.
        let verifier = create_test_verifier();
        let pool_hash = [0xAAu8; 32];
        verifier.closeness_pass_cache.lock().put(pool_hash, ());

        // Construct a dummy pool — contents don't matter because the cache
        // hit means we never look at them.
        let pool = MerklePaymentCandidatePool {
            midpoint_proof: fake_midpoint_proof(),
            candidate_nodes: make_candidate_nodes(1_700_000_000),
        };

        let result = verifier
            .verify_merkle_candidate_closeness(&pool, pool_hash)
            .await;
        assert!(
            result.is_ok(),
            "cached pool hash must bypass the inner check and return Ok(()), got: {result:?}"
        );
    }

    #[tokio::test]
    async fn closeness_single_flight_concurrent_readers_share_one_verification() {
        // Two concurrent callers for the same pool_hash should produce the
        // same outcome, and the cache should end up populated exactly once.
        // We use the test-utils fail-open path to short-circuit the inner
        // DHT lookup; the purpose of this test is the single-flight
        // plumbing, not the lookup itself.
        let verifier = Arc::new(create_test_verifier());
        let pool_hash = [0x77u8; 32];
        let pool = MerklePaymentCandidatePool {
            midpoint_proof: fake_midpoint_proof(),
            candidate_nodes: make_candidate_nodes(1_700_000_000),
        };

        let v1 = Arc::clone(&verifier);
        let p1 = pool.clone();
        let v2 = Arc::clone(&verifier);
        let p2 = pool.clone();

        let (r1, r2) = tokio::join!(
            async move { v1.verify_merkle_candidate_closeness(&p1, pool_hash).await },
            async move { v2.verify_merkle_candidate_closeness(&p2, pool_hash).await },
        );

        assert_eq!(r1.is_ok(), r2.is_ok(), "concurrent callers must agree");
        assert!(
            r1.is_ok(),
            "both callers must succeed on the test-utils path"
        );
        assert!(
            verifier
                .closeness_pass_cache
                .lock()
                .get(&pool_hash)
                .is_some(),
            "success path must populate the pass cache"
        );
        assert!(
            verifier.inflight_closeness.lock().get(&pool_hash).is_none(),
            "inflight slot must be cleared after the leader finishes"
        );
    }

    #[tokio::test]
    async fn closeness_waiter_reads_leaders_published_failure() {
        // Prove the waiter path actually surfaces a failure published by a
        // concurrent leader, without running its own inner check. Insert a
        // slot, spawn a waiter (which will park on notified_owned), then
        // publish failure + notify from the outside — simulating what the
        // leader's `publish` + drop-guard pair does.
        let verifier = Arc::new(create_test_verifier());
        let pool_hash = [0x55u8; 32];
        let slot = Arc::new(ClosenessSlot::new());
        verifier
            .inflight_closeness
            .lock()
            .put(pool_hash, Arc::clone(&slot));

        let pool = MerklePaymentCandidatePool {
            midpoint_proof: fake_midpoint_proof(),
            candidate_nodes: make_candidate_nodes(1_700_000_000),
        };

        let verifier_c = Arc::clone(&verifier);
        let pool_c = pool.clone();
        let waiter = tokio::spawn(async move {
            verifier_c
                .verify_merkle_candidate_closeness(&pool_c, pool_hash)
                .await
        });

        // Yield so the waiter can run up to its `notified_owned().await`.
        // A few yields cover both single-threaded and multi-threaded tokio
        // runtimes regardless of scheduling.
        for _ in 0..5 {
            tokio::task::yield_now().await;
        }

        // Simulate the leader's `publish` + drop-guard: publish the result,
        // clear the slot, wake waiters.
        slot.result
            .set(Err("forged pool: not close enough".to_string()))
            .expect("set once");
        verifier.inflight_closeness.lock().pop(&pool_hash);
        slot.notify.notify_waiters();

        let result = waiter.await.expect("task panicked");
        let err = result.expect_err("waiter must return the leader's published failure");
        assert!(
            err.to_string().contains("forged pool"),
            "waiter must surface the leader's error message, got: {err}"
        );
    }

    #[tokio::test]
    async fn closeness_rejects_pool_with_duplicate_candidate_pub_keys() {
        // An attacker who submits 16 copies of the same real peer's pub_key
        // would otherwise satisfy the closeness threshold trivially:
        // that one peer's membership in the DHT-returned set would count
        // 16 times. The dedupe check in verify_merkle_candidate_closeness_inner
        // must reject the pool BEFORE the network lookup runs (so this test
        // works even with no P2PNode attached).
        let verifier = create_test_verifier();
        let pool_hash = [0xDDu8; 32];

        // Build a normal pool, then overwrite every candidate's pub_key
        // with a single shared key so all 16 derive to the same PeerId.
        let mut candidates = make_candidate_nodes(1_700_000_000);
        let shared_pub_key = candidates
            .first()
            .expect("make_candidate_nodes returns CANDIDATES_PER_POOL entries")
            .pub_key
            .clone();
        for c in &mut candidates {
            c.pub_key = shared_pub_key.clone();
        }
        let pool = MerklePaymentCandidatePool {
            midpoint_proof: fake_midpoint_proof(),
            candidate_nodes: candidates,
        };

        let result = verifier
            .verify_merkle_candidate_closeness(&pool, pool_hash)
            .await;
        let err = result.expect_err("duplicate candidate PeerIds must be rejected");
        let msg = err.to_string();
        assert!(
            msg.contains("duplicate candidate PeerId"),
            "rejection must be the duplicate-PeerId branch, got: {msg}"
        );
    }

    /// Build a deterministic but otherwise-unused `MidpointProof` so unit
    /// tests can construct a `MerklePaymentCandidatePool` without spinning
    /// up a real merkle tree. The closeness path only calls `.address()`
    /// on it, which is a pure BLAKE3 of the branch's leaf/root/timestamp —
    /// the values don't need to be tree-valid for these tests.
    fn fake_midpoint_proof() -> evmlib::merkle_payments::MidpointProof {
        // Build a minimal tree of two leaves so we get a real branch.
        let leaves = vec![xor_name::XorName([1u8; 32]), xor_name::XorName([2u8; 32])];
        let tree = evmlib::merkle_payments::MerkleTree::from_xornames(leaves).expect("tree");
        let candidates = tree.reward_candidates(1_700_000_000).expect("candidates");
        candidates.first().expect("at least one").clone()
    }

    // =========================================================================
    // Merkle verification unit tests
    // =========================================================================

    /// Helper: build 16 validly-signed ML-DSA-65 candidate nodes.
    fn make_candidate_nodes(
        timestamp: u64,
    ) -> [evmlib::merkle_payments::MerklePaymentCandidateNode;
           evmlib::merkle_payments::CANDIDATES_PER_POOL] {
        use evmlib::merkle_payments::{MerklePaymentCandidateNode, CANDIDATES_PER_POOL};
        use saorsa_core::MlDsa65;
        use saorsa_pqc::pqc::types::MlDsaSecretKey;
        use saorsa_pqc::pqc::MlDsaOperations;

        std::array::from_fn::<_, CANDIDATES_PER_POOL, _>(|i| {
            let ml_dsa = MlDsa65::new();
            let (pub_key, secret_key) = ml_dsa.generate_keypair().expect("keygen");
            // Baseline candidate: `(committed_key_count = 0, commitment_pin =
            // None)` below, so the price must be `calculate_price(0)` to satisfy
            // the ADR-0004 binding rule. A placeholder price is off-curve and is
            // rejected before the check each test actually exercises.
            let price = crate::payment::pricing::calculate_price(0);
            #[allow(clippy::cast_possible_truncation)]
            let reward_address = RewardsAddress::new([i as u8; 20]);
            let msg = MerklePaymentCandidateNode::bytes_to_sign(
                &price,
                &reward_address,
                timestamp,
                0,
                &None,
            );
            let sk = MlDsaSecretKey::from_bytes(secret_key.as_bytes()).expect("sk");
            let signature = ml_dsa.sign(&sk, &msg).expect("sign").as_bytes().to_vec();

            MerklePaymentCandidateNode {
                pub_key: pub_key.as_bytes().to_vec(),
                price,
                reward_address,
                merkle_payment_timestamp: timestamp,
                committed_key_count: 0,
                commitment_pin: None,
                signature,
            }
        })
    }

    /// Helper: build a valid `MerklePaymentProof` with real ML-DSA-65
    /// signatures. Returns the raw proof, pool hash, xorname, and timestamp.
    fn make_valid_merkle_proof() -> (
        evmlib::merkle_payments::MerklePaymentProof,
        evmlib::merkle_batch_payment::PoolHash,
        [u8; 32],
        u64,
    ) {
        use evmlib::merkle_payments::{MerklePaymentCandidatePool, MerklePaymentProof, MerkleTree};

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system time")
            .as_secs();

        let addresses: Vec<xor_name::XorName> = (0..4u8)
            .map(|i| xor_name::XorName::from_content(&[i]))
            .collect();
        let tree = MerkleTree::from_xornames(addresses.clone()).expect("tree");

        let candidate_nodes = make_candidate_nodes(timestamp);

        let reward_candidates = tree
            .reward_candidates(timestamp)
            .expect("reward candidates");
        let midpoint_proof = reward_candidates
            .first()
            .expect("at least one candidate")
            .clone();

        let pool = MerklePaymentCandidatePool {
            midpoint_proof,
            candidate_nodes,
        };

        let first_address = *addresses.first().expect("first address");
        let address_proof = tree
            .generate_address_proof(0, first_address)
            .expect("proof");

        let merkle_proof = MerklePaymentProof::new(first_address, address_proof, pool);
        let pool_hash = merkle_proof.winner_pool_hash();
        let xorname = first_address.0;

        (merkle_proof, pool_hash, xorname, timestamp)
    }

    /// ADR-0004 Amendment 2: only the PAID (settlement-verified) quote's pin is
    /// nominated for a deterministic first audit; the unpaid bundle quotes are
    /// cross-checked but never enqueue audits.
    #[tokio::test]
    async fn adr0004_first_audit_nominates_only_paid_single_node_quote() {
        use evmlib::{EncodedPeerId, RewardsAddress};

        let verifier = create_test_verifier();
        let (tx, mut rx) = tokio::sync::mpsc::channel(8);
        verifier.attach_monetized_pin_sender(tx);

        let ids: Vec<[u8; 32]> = (1..=3u8).map(|b| [b; 32]).collect();
        let payment = ProofOfPayment {
            peer_quotes: ids
                .iter()
                .enumerate()
                .map(|(i, id)| {
                    let pin_byte = u8::try_from(i).expect("small bundle") + 1;
                    let mut quote = make_fake_quote(
                        [0xD0; 32],
                        SystemTime::now(),
                        RewardsAddress::new([pin_byte; 20]),
                    );
                    quote.commitment_pin = Some([pin_byte; 32]);
                    quote.committed_key_count = 100;
                    (EncodedPeerId::new(*id), quote)
                })
                .collect(),
        };

        // The middle quote is the candidate whose on-chain settlement verified.
        let paid = ids.get(1).copied().expect("paid id");
        verifier.cross_check_quotes(&payment, &[], Some(paid)).await;

        let event = rx.try_recv().expect("the paid quote must be nominated");
        assert_eq!(event.peer, PeerId::from_bytes(paid));
        assert_eq!(event.pin, [2u8; 32]);
        assert!(
            rx.try_recv().is_err(),
            "unpaid bundle quotes must not be nominated for first audits"
        );
    }

    /// ADR-0004 Amendment 2: with no verified paid candidate, nothing is
    /// nominated (defensive: the caller never reaches the cross-check on a
    /// failed verification, but the emission itself must also fail closed).
    #[tokio::test]
    async fn adr0004_first_audit_nominates_nothing_without_paid_peer() {
        use evmlib::{EncodedPeerId, RewardsAddress};

        let verifier = create_test_verifier();
        let (tx, mut rx) = tokio::sync::mpsc::channel(8);
        verifier.attach_monetized_pin_sender(tx);

        let mut quote =
            make_fake_quote([0xD1; 32], SystemTime::now(), RewardsAddress::new([1; 20]));
        quote.commitment_pin = Some([1u8; 32]);
        let payment = ProofOfPayment {
            peer_quotes: vec![(EncodedPeerId::new([1u8; 32]), quote)],
        };

        verifier.cross_check_quotes(&payment, &[], None).await;
        assert!(rx.try_recv().is_err(), "no paid peer, no nomination");
    }

    /// ADR-0004 Amendment 2, merkle path: only the candidates at the
    /// contract-verified PAID indices are nominated; the rest of the pool
    /// merely established the median and must not enqueue audits.
    #[tokio::test]
    async fn adr0004_first_audit_nominates_only_paid_merkle_candidates() {
        use evmlib::merkle_payments::{MerklePaymentCandidatePool, MerkleTree};

        let verifier = create_test_verifier();
        let (tx, mut rx) = tokio::sync::mpsc::channel(8);
        verifier.attach_monetized_pin_sender(tx);

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system time")
            .as_secs();
        let mut candidate_nodes = make_candidate_nodes(timestamp);
        for (i, candidate) in candidate_nodes.iter_mut().enumerate() {
            candidate.commitment_pin = Some([u8::try_from(i & 0xFF).expect("byte"); 32]);
            candidate.committed_key_count = 50;
        }
        let addresses: Vec<xor_name::XorName> = (0..4u8)
            .map(|i| xor_name::XorName::from_content(&[i]))
            .collect();
        let tree = MerkleTree::from_xornames(addresses).expect("tree");
        let midpoint_proof = tree
            .reward_candidates(timestamp)
            .expect("reward candidates")
            .first()
            .expect("at least one candidate")
            .clone();
        let pool = MerklePaymentCandidatePool {
            midpoint_proof,
            candidate_nodes,
        };

        let paid_indices: std::collections::HashSet<usize> = [1usize, 3].into_iter().collect();
        verifier
            .cross_check_merkle_candidates(&pool, &[], &paid_indices)
            .await;

        let mut nominated = Vec::new();
        while let Ok(event) = rx.try_recv() {
            nominated.push(event);
        }
        assert_eq!(
            nominated.len(),
            2,
            "only the PAID candidates are nominated, not the whole pool"
        );
        for (event, idx) in nominated.iter().zip([1usize, 3]) {
            let candidate = pool.candidate_nodes.get(idx).expect("paid candidate");
            assert_eq!(
                event.peer,
                PeerId::from_bytes(*blake3::hash(&candidate.pub_key).as_bytes())
            );
            assert_eq!(event.pin, [u8::try_from(idx).expect("byte"); 32]);
        }
    }

    /// Helper: build a minimal valid `MerklePaymentProof` with real ML-DSA-65
    /// signatures. Returns `(xorname, serialized_tagged_proof, pool_hash, timestamp)`.
    /// Verifier for merkle admission tests, pinned to the **enforcing** side of
    /// the ADR-0008 parity boundary.
    ///
    /// Merkle receipts must be stamped within one week of now or they are
    /// expired, so these tests necessarily build near-`now` timestamps. Left on
    /// the production boundary they would sit in the legacy 1x window today and
    /// silently switch to the 3x rule when the wall clock crossed that date,
    /// changing what each assertion means. Pinning to `0` means "every receipt
    /// is post-boundary", which is the rule the release ships with.
    fn merkle_test_verifier() -> PaymentVerifier {
        let verifier = create_test_verifier();
        verifier.set_merkle_parity_from_for_tests(0);
        verifier
    }

    /// Per-node amount a depth-2 pool of baseline-priced candidates must settle
    /// under parity: `median * 2^2 * 3 / 2`, i.e. six times the candidate price.
    ///
    /// Derived from the curve rather than hardcoded: the candidates are priced
    /// at `calculate_price(0)` so they satisfy the ADR-0004 binding rule, and a
    /// literal would silently stop matching the formula if the curve moves.
    fn merkle_parity_per_node_depth2() -> Amount {
        crate::payment::pricing::calculate_price(0) * Amount::from(6u64)
    }

    /// The same pool's historic pre-parity amount: `median * 2^2 / 2`, i.e.
    /// twice the candidate price.
    fn merkle_legacy_per_node_depth2() -> Amount {
        crate::payment::pricing::calculate_price(0) * Amount::from(2u64)
    }

    fn make_valid_merkle_proof_bytes() -> (
        [u8; 32],
        Vec<u8>,
        evmlib::merkle_batch_payment::PoolHash,
        u64,
    ) {
        let (merkle_proof, pool_hash, xorname, timestamp) = make_valid_merkle_proof();
        let tagged = crate::payment::proof::serialize_merkle_proof(&merkle_proof)
            .expect("serialize merkle proof");
        (xorname, tagged, pool_hash, timestamp)
    }

    #[tokio::test]
    async fn test_merkle_address_mismatch_rejected() {
        let verifier = merkle_test_verifier();
        let (_correct_xorname, tagged_proof, _pool_hash, _ts) = make_valid_merkle_proof_bytes();

        // Use a DIFFERENT xorname than what the proof was built for
        let wrong_xorname = [0xFFu8; 32];

        let result = verifier
            .verify_payment(
                &wrong_xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(
            result.is_err(),
            "Should reject merkle proof address mismatch"
        );
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("address mismatch") || err_msg.contains("Merkle proof address"),
            "Error should mention address mismatch: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_merkle_malformed_body_rejected() {
        let verifier = merkle_test_verifier();
        let xorname = [0xA3u8; 32];

        // Valid merkle tag but truncated/corrupted msgpack body
        let mut bad_proof = vec![crate::ant_protocol::PROOF_TAG_MERKLE];
        bad_proof.extend_from_slice(&[0xDE, 0xAD, 0xBE, 0xEF]);
        bad_proof.extend_from_slice(&[0x00; 10]);
        // pad to minimum size
        while bad_proof.len() < MIN_PAYMENT_PROOF_SIZE_BYTES {
            bad_proof.push(0x00);
        }

        let result = verifier
            .verify_payment(&xorname, Some(&bad_proof), VerificationContext::ClientPut)
            .await;

        assert!(result.is_err(), "Should reject malformed merkle body");
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("deserialize") || err_msg.contains("Failed"),
            "Error should mention deserialization: {err_msg}"
        );
    }

    #[test]
    fn test_merkle_proof_serialized_size_within_limits() {
        let (_xorname, tagged_proof, _pool_hash, _ts) = make_valid_merkle_proof_bytes();

        // 16 ML-DSA-65 candidates (~1952 pub key + ~3309 sig each) ≈ 84 KB + tree data
        assert!(
            tagged_proof.len() >= MIN_PAYMENT_PROOF_SIZE_BYTES,
            "Merkle proof ({} bytes) should be >= min {} bytes",
            tagged_proof.len(),
            MIN_PAYMENT_PROOF_SIZE_BYTES
        );
        assert!(
            tagged_proof.len() <= MAX_PAYMENT_PROOF_SIZE_BYTES,
            "Merkle proof ({} bytes) should be <= max {} bytes",
            tagged_proof.len(),
            MAX_PAYMENT_PROOF_SIZE_BYTES
        );
    }

    #[test]
    fn test_merkle_proof_tag_is_correct() {
        let (_xorname, tagged_proof, _pool_hash, _ts) = make_valid_merkle_proof_bytes();

        assert_eq!(
            tagged_proof.first().copied(),
            Some(crate::ant_protocol::PROOF_TAG_MERKLE),
            "First byte must be the merkle tag"
        );
        assert_eq!(
            crate::payment::proof::detect_proof_type(&tagged_proof),
            Some(crate::payment::proof::ProofType::Merkle)
        );
    }

    #[test]
    fn test_pool_cache_eviction() {
        use evmlib::merkle_batch_payment::PoolHash;

        let config = PaymentVerifierConfig {
            evm: EvmVerifierConfig::default(),
            cache_capacity: 100,
            close_group_size: CLOSE_GROUP_SIZE,
            local_rewards_address: RewardsAddress::new([1u8; 20]),
            price_floor: PriceFloorConfig::default(),
        };
        let verifier = PaymentVerifier::new(config);

        // Fill the pool cache to capacity (DEFAULT_POOL_CACHE_CAPACITY = 1000)
        for i in 0..DEFAULT_POOL_CACHE_CAPACITY {
            let mut hash: PoolHash = [0u8; 32];
            // Write index bytes into the hash
            let idx_bytes = i.to_le_bytes();
            for (j, b) in idx_bytes.iter().enumerate() {
                if j < 32 {
                    hash[j] = *b;
                }
            }
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 4,
                merkle_payment_timestamp: 1_700_000_000,
                paid_node_addresses: vec![],
            };
            verifier.pool_cache.lock().put(hash, info);
        }

        assert_eq!(
            verifier.pool_cache.lock().len(),
            DEFAULT_POOL_CACHE_CAPACITY
        );

        // Insert one more — should evict the oldest
        let overflow_hash: PoolHash = [0xFFu8; 32];
        let info = evmlib::merkle_payments::OnChainPaymentInfo {
            depth: 8,
            merkle_payment_timestamp: 1_800_000_000,
            paid_node_addresses: vec![],
        };
        verifier.pool_cache.lock().put(overflow_hash, info);

        // Size should still be at capacity (not capacity + 1)
        assert_eq!(
            verifier.pool_cache.lock().len(),
            DEFAULT_POOL_CACHE_CAPACITY
        );

        // The new entry should be present
        let found = verifier.pool_cache.lock().get(&overflow_hash).cloned();
        assert!(
            found.is_some(),
            "Newly inserted pool hash should be present"
        );
        assert_eq!(found.expect("info").depth, 8);
    }

    #[test]
    fn test_pool_cache_concurrent_access() {
        use evmlib::merkle_batch_payment::PoolHash;
        use std::sync::Arc;

        let verifier = Arc::new(create_test_verifier());

        let mut handles = Vec::new();
        for i in 0..20u8 {
            let v = verifier.clone();
            handles.push(std::thread::spawn(move || {
                let hash: PoolHash = [i; 32];
                let info = evmlib::merkle_payments::OnChainPaymentInfo {
                    depth: i,
                    merkle_payment_timestamp: u64::from(i) * 1000,
                    paid_node_addresses: vec![],
                };
                v.pool_cache.lock().put(hash, info);

                // Read back
                let found = v.pool_cache.lock().get(&hash).cloned();
                assert!(found.is_some(), "Entry {i} should be readable after insert");
            }));
        }

        for handle in handles {
            handle.join().expect("thread panicked");
        }

        // All 20 entries should be present (well under 1000 capacity)
        assert_eq!(verifier.pool_cache.lock().len(), 20);
    }

    #[tokio::test]
    async fn test_merkle_tampered_candidate_signature_rejected() {
        let verifier = merkle_test_verifier();

        let (mut merkle_proof, _pool_hash, xorname, timestamp) = make_valid_merkle_proof();

        // Tamper the first candidate's signature
        if let Some(byte) = merkle_proof
            .winner_pool
            .candidate_nodes
            .first_mut()
            .and_then(|c| c.signature.first_mut())
        {
            *byte ^= 0xFF;
        }

        // Recompute pool hash after tampering (signature change alters the hash)
        let tampered_pool_hash = merkle_proof.winner_pool_hash();

        // Pre-populate pool cache so we skip the on-chain query
        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 4,
                merkle_payment_timestamp: timestamp,
                paid_node_addresses: vec![],
            };
            verifier.pool_cache.lock().put(tampered_pool_hash, info);
        }

        let tagged =
            crate::payment::proof::serialize_merkle_proof(&merkle_proof).expect("serialize");

        let result = verifier
            .verify_payment(&xorname, Some(&tagged), VerificationContext::ClientPut)
            .await;

        assert!(
            result.is_err(),
            "Should reject merkle proof with tampered candidate signature"
        );
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("Invalid ML-DSA-65 signature"),
            "Error should mention invalid signature: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_merkle_timestamp_mismatch_rejected() {
        let verifier = merkle_test_verifier();

        let (xorname, tagged, pool_hash, timestamp) = make_valid_merkle_proof_bytes();

        // Pre-populate pool cache with a DIFFERENT timestamp than the candidates
        {
            let mismatched_ts = timestamp + 9999;
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 4,
                merkle_payment_timestamp: mismatched_ts,
                paid_node_addresses: vec![],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(&xorname, Some(&tagged), VerificationContext::ClientPut)
            .await;

        assert!(
            result.is_err(),
            "Should reject merkle proof with timestamp mismatch"
        );
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("timestamp mismatch"),
            "Error should mention timestamp mismatch: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_merkle_paid_node_index_out_of_bounds_rejected() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        // The test tree has 4 addresses → depth 2. We must match the tree depth
        // so verify_merkle_proof passes the depth check, then the paid node
        // index out-of-bounds check fires.
        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 2,
                merkle_payment_timestamp: ts,
                paid_node_addresses: vec![
                    // First paid node: valid (matches candidate 0, amount clears
                    // the parity formula so the test reaches the index check)
                    (
                        RewardsAddress::new([0u8; 20]),
                        0,
                        merkle_parity_per_node_depth2(),
                    ),
                    // Second paid node: index 999 is way beyond CANDIDATES_PER_POOL (16)
                    (
                        RewardsAddress::new([1u8; 20]),
                        999,
                        merkle_parity_per_node_depth2(),
                    ),
                ],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(
            result.is_err(),
            "Should reject paid node index out of bounds"
        );
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("out of bounds"),
            "Error should mention out of bounds: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_merkle_paid_node_address_mismatch_rejected() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        // Tree has depth 2, so provide 2 paid node entries.
        // Both use valid indices but the second has a wrong reward address.
        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 2,
                merkle_payment_timestamp: ts,
                paid_node_addresses: vec![
                    // Index 0 with matching address [0x00; 20], paid enough to
                    // clear the parity formula so the test reaches index 1
                    (
                        RewardsAddress::new([0u8; 20]),
                        0,
                        merkle_parity_per_node_depth2(),
                    ),
                    // Index 1 with WRONG address — candidate 1's address is [0x01; 20]
                    (
                        RewardsAddress::new([0xFF; 20]),
                        1,
                        merkle_parity_per_node_depth2(),
                    ),
                ],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(result.is_err(), "Should reject paid node address mismatch");
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("address mismatch"),
            "Error should mention address mismatch: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_merkle_wrong_depth_rejected() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        // Pre-populate pool cache with depth=3 but only 1 paid node address
        // (depth must equal paid_node_addresses.len())
        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 3,
                merkle_payment_timestamp: ts,
                paid_node_addresses: vec![(
                    RewardsAddress::new([0u8; 20]),
                    0,
                    Amount::from(1024u64),
                )],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(
            result.is_err(),
            "Should reject mismatched depth vs paid node count"
        );
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("Wrong number of paid nodes")
                || err_msg.contains("verification failed"),
            "Error should mention depth/count mismatch: {err_msg}"
        );
    }

    #[tokio::test]
    async fn test_merkle_underpayment_rejected() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        // Tree depth=2, so 2 paid nodes required. Candidates all quote price=1024.
        // Expected per-node: median(1024) * 2^2 / 2 = 2048.
        // Pay only 1 wei per node — far below the expected amount.
        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 2,
                merkle_payment_timestamp: ts,
                paid_node_addresses: vec![
                    (RewardsAddress::new([0u8; 20]), 0, Amount::from(1u64)),
                    (RewardsAddress::new([1u8; 20]), 1, Amount::from(1u64)),
                ],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(
            result.is_err(),
            "Should reject merkle payment where paid amount < expected per-node amount"
        );
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(
            err_msg.contains("Underpayment"),
            "Error should mention underpayment: {err_msg}"
        );
    }

    /// A batch settled at the historic 1x is refused once its receipt is stamped
    /// at or after the parity boundary. This is the fix being active by default:
    /// no flag, no shadow mode.
    #[tokio::test]
    async fn merkle_legacy_1x_settlement_rejected_after_the_parity_boundary() {
        let verifier = merkle_test_verifier(); // boundary pinned at 0 = always enforcing
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 2,
                merkle_payment_timestamp: ts,
                paid_node_addresses: vec![
                    (
                        RewardsAddress::new([0u8; 20]),
                        0,
                        merkle_legacy_per_node_depth2(),
                    ),
                    (
                        RewardsAddress::new([1u8; 20]),
                        1,
                        merkle_legacy_per_node_depth2(),
                    ),
                ],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let err_msg = format!(
            "{}",
            verifier
                .verify_payment(
                    &xorname,
                    Some(&tagged_proof),
                    VerificationContext::ClientPut,
                )
                .await
                .expect_err("a 1x settlement must be refused past the boundary")
        );
        assert!(
            err_msg.contains("Underpayment") && err_msg.contains("3x required"),
            "Error should name the required multiplier: {err_msg}"
        );
        // An exact 1x settlement is an outdated client, not a pricing dispute.
        // The payer has already spent money they cannot recover, so the
        // rejection has to tell them what to do rather than hand them two
        // integers. This is the only message that reaches a client too old to
        // declare a settlement version at quote time.
        assert!(
            err_msg.contains("too old to settle correctly") && err_msg.contains("ant update"),
            "Error should tell the user to upgrade: {err_msg}"
        );
    }

    /// The upgrade advice is keyed on an EXACT multiplier shortfall, which is
    /// what an unmultiplied settlement looks like. A merely-cheap payment is a
    /// different fault and must not be blamed on the client's version, or the
    /// advice stops meaning anything.
    #[tokio::test]
    async fn a_partial_shortfall_is_not_blamed_on_an_outdated_client() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        // One wei under parity: short, but not the 1x signature.
        let almost = merkle_parity_per_node_depth2().saturating_sub(Amount::from(1u64));
        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 2,
                merkle_payment_timestamp: ts,
                paid_node_addresses: vec![
                    (RewardsAddress::new([0u8; 20]), 0, almost),
                    (RewardsAddress::new([1u8; 20]), 1, almost),
                ],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let err_msg = format!(
            "{}",
            verifier
                .verify_payment(
                    &xorname,
                    Some(&tagged_proof),
                    VerificationContext::ClientPut
                )
                .await
                .expect_err("a short settlement must be refused")
        );
        assert!(
            err_msg.contains("Underpayment"),
            "Error should still name the underpayment: {err_msg}"
        );
        assert!(
            !err_msg.contains("ant update"),
            "A partial shortfall must not be reported as an outdated client: {err_msg}"
        );
    }

    /// A receipt stamped BEFORE the boundary keeps its 1x price. The money was
    /// already spent under the old rule and cannot be refunded, so refusing it
    /// would destroy value. This is the compatibility half of the cutover.
    #[tokio::test]
    async fn merkle_legacy_1x_settlement_accepted_before_the_parity_boundary() {
        let verifier = create_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();
        // Boundary just after this receipt's stamp: the receipt is legacy.
        verifier.set_merkle_parity_from_for_tests(ts + 1);

        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 2,
                merkle_payment_timestamp: ts,
                paid_node_addresses: vec![
                    (
                        RewardsAddress::new([0u8; 20]),
                        0,
                        merkle_legacy_per_node_depth2(),
                    ),
                    (
                        RewardsAddress::new([1u8; 20]),
                        1,
                        merkle_legacy_per_node_depth2(),
                    ),
                ],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;
        assert!(
            result.is_ok(),
            "a receipt stamped before the boundary keeps the 1x rule: {result:?}"
        );
    }

    /// An upgraded client's 3x settlement is accepted on both sides of the
    /// boundary. This is what makes the client-first rollout work: the client
    /// pays 3x from the day it ships, and neither a node that predates the
    /// boundary nor one enforcing past it has any reason to refuse.
    #[tokio::test]
    async fn merkle_parity_settlement_accepted_in_both_regimes() {
        for parity_from in [0u64, u64::MAX] {
            let verifier = create_test_verifier();
            verifier.set_merkle_parity_from_for_tests(parity_from);
            let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

            {
                let info = evmlib::merkle_payments::OnChainPaymentInfo {
                    depth: 2,
                    merkle_payment_timestamp: ts,
                    paid_node_addresses: vec![
                        (
                            RewardsAddress::new([0u8; 20]),
                            0,
                            merkle_parity_per_node_depth2(),
                        ),
                        (
                            RewardsAddress::new([1u8; 20]),
                            1,
                            merkle_parity_per_node_depth2(),
                        ),
                    ],
                };
                verifier.pool_cache.lock().put(pool_hash, info);
            }

            let result = verifier
                .verify_payment(
                    &xorname,
                    Some(&tagged_proof),
                    VerificationContext::ClientPut,
                )
                .await;
            assert!(
                result.is_ok(),
                "3x must be accepted with boundary {parity_from}: {result:?}"
            );
        }
    }

    /// One chunk's payment proof as the verifier receives it: the stored
    /// address and the tagged, serialized proof bytes.
    type TaggedProof = ([u8; 32], Vec<u8>);

    /// Build valid proofs for several addresses of ONE tree, all resolving to
    /// the same winner pool hash — the shape a real batch upload has, and the
    /// case the ADR-0008 parity telemetry must collapse to a single sample.
    /// Returns `(Vec<(xorname, tagged_proof)>, pool_hash, timestamp)`.
    fn make_valid_merkle_proofs_for_one_pool(
        indices: &[usize],
    ) -> (
        Vec<TaggedProof>,
        evmlib::merkle_batch_payment::PoolHash,
        u64,
    ) {
        use evmlib::merkle_payments::{MerklePaymentCandidatePool, MerklePaymentProof, MerkleTree};

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system time")
            .as_secs();

        let addresses: Vec<xor_name::XorName> = (0..4u8)
            .map(|i| xor_name::XorName::from_content(&[i]))
            .collect();
        let tree = MerkleTree::from_xornames(addresses.clone()).expect("tree");

        let midpoint_proof = tree
            .reward_candidates(timestamp)
            .expect("reward candidates")
            .first()
            .expect("at least one candidate")
            .clone();
        let pool = MerklePaymentCandidatePool {
            midpoint_proof,
            candidate_nodes: make_candidate_nodes(timestamp),
        };

        let mut proofs = Vec::with_capacity(indices.len());
        let mut pool_hash = None;
        for &index in indices {
            let address = *addresses.get(index).expect("address index within the tree");
            let address_proof = tree
                .generate_address_proof(index, address)
                .expect("address proof");
            let proof = MerklePaymentProof::new(address, address_proof, pool.clone());
            let hash = proof.winner_pool_hash();
            match pool_hash {
                None => pool_hash = Some(hash),
                Some(first) => assert_eq!(
                    hash, first,
                    "every address in one tree must share one winner pool"
                ),
            }
            let tagged =
                crate::payment::proof::serialize_merkle_proof(&proof).expect("serialize proof");
            proofs.push((address.0, tagged));
        }

        (proofs, pool_hash.expect("at least one proof"), timestamp)
    }

    /// The depth-2 test pool's on-chain record, settled at 3x parity so the
    /// proof is admitted and the telemetry call site is actually reached.
    fn parity_settled_pool_info(ts: u64) -> evmlib::merkle_payments::OnChainPaymentInfo {
        evmlib::merkle_payments::OnChainPaymentInfo {
            depth: 2,
            merkle_payment_timestamp: ts,
            paid_node_addresses: vec![
                (
                    RewardsAddress::new([0u8; 20]),
                    0,
                    merkle_parity_per_node_depth2(),
                ),
                (
                    RewardsAddress::new([1u8; 20]),
                    1,
                    merkle_parity_per_node_depth2(),
                ),
            ],
        }
    }

    // =========================================================================
    // ADR-0008 parity telemetry: what may enter the rollout signal, and at what
    // cardinality. The signal is read to decide when the pre-boundary (1x)
    // compatibility window has drained, so a sample from a proof that was
    // rejected — or 256 samples from one settlement — would mislead that call.
    // =========================================================================

    /// A proof naming a paid index outside the pool is rejected in the paid-node
    /// loop, before the telemetry call site. Nothing may be measured.
    #[tokio::test]
    async fn parity_telemetry_silent_when_a_paid_index_is_invalid() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        {
            let mut info = parity_settled_pool_info(ts);
            // Second entry points past CANDIDATES_PER_POOL (16).
            if let Some(entry) = info.paid_node_addresses.get_mut(1) {
                entry.1 = 999;
            }
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(
            result.is_err(),
            "an out-of-bounds paid index must be refused"
        );
        assert_eq!(
            verifier.merkle_parity_emission_count(),
            0,
            "a refused proof must not enter the parity signal"
        );
    }

    /// A paid entry whose reward address does not match the candidate at that
    /// index is rejected before the telemetry call site.
    #[tokio::test]
    async fn parity_telemetry_silent_when_a_reward_address_is_wrong() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        {
            let mut info = parity_settled_pool_info(ts);
            // Candidate 1's address is [0x01; 20], not [0xFF; 20].
            if let Some(entry) = info.paid_node_addresses.get_mut(1) {
                entry.0 = RewardsAddress::new([0xFF; 20]);
            }
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(result.is_err(), "a redirected payee must be refused");
        assert_eq!(
            verifier.merkle_parity_emission_count(),
            0,
            "a refused proof must not enter the parity signal"
        );
    }

    /// An underpaid batch is the case the signal most obviously must not
    /// contain: it is rejected, so it never became a store.
    #[tokio::test]
    async fn parity_telemetry_silent_on_underpayment() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();

        {
            let info = evmlib::merkle_payments::OnChainPaymentInfo {
                depth: 2,
                merkle_payment_timestamp: ts,
                paid_node_addresses: vec![
                    (RewardsAddress::new([0u8; 20]), 0, Amount::from(1u64)),
                    (RewardsAddress::new([1u8; 20]), 1, Amount::from(1u64)),
                ],
            };
            verifier.pool_cache.lock().put(pool_hash, info);
        }

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::ClientPut,
            )
            .await;

        assert!(result.is_err(), "an underpaid batch must be refused");
        assert_eq!(
            verifier.merkle_parity_emission_count(),
            0,
            "a refused proof must not enter the parity signal"
        );
    }

    /// A paid-list admission verifies the same proof but reprices nothing. It
    /// must not be measured, or a batch already sampled at its store admission
    /// would be counted twice.
    #[tokio::test]
    async fn parity_telemetry_silent_for_paid_list_admission() {
        let verifier = merkle_test_verifier();
        let (xorname, tagged_proof, pool_hash, ts) = make_valid_merkle_proof_bytes();
        verifier
            .pool_cache
            .lock()
            .put(pool_hash, parity_settled_pool_info(ts));

        let result = verifier
            .verify_payment(
                &xorname,
                Some(&tagged_proof),
                VerificationContext::PaidListAdmission,
            )
            .await;

        assert!(result.is_ok(), "the proof itself is valid: {result:?}");
        assert_eq!(
            verifier.merkle_parity_emission_count(),
            0,
            "only store admissions may be measured"
        );
    }

    /// Two chunks of one batch are one economic event. The pool hash in the
    /// line makes the stream deduplicable; the first-emission cache is what
    /// keeps the emitted cardinality at one.
    #[tokio::test]
    async fn parity_telemetry_emits_once_for_two_chunks_of_one_pool() {
        let verifier = merkle_test_verifier();
        let (proofs, pool_hash, ts) = make_valid_merkle_proofs_for_one_pool(&[0, 1]);
        verifier
            .pool_cache
            .lock()
            .put(pool_hash, parity_settled_pool_info(ts));

        for (xorname, tagged) in &proofs {
            let result = verifier
                .verify_payment(xorname, Some(tagged), VerificationContext::ClientPut)
                .await;
            assert!(result.is_ok(), "both chunks must be admitted: {result:?}");
        }

        assert_eq!(
            verifier.merkle_parity_emission_count(),
            1,
            "one settlement is one sample, however many chunks it covers"
        );
    }

    /// Deduplication is per pool, not a global mute: a genuinely separate
    /// settlement is a separate sample.
    #[tokio::test]
    async fn parity_telemetry_emits_once_more_for_a_second_pool() {
        let verifier = merkle_test_verifier();

        let (first, first_pool, first_ts) = make_valid_merkle_proofs_for_one_pool(&[0, 1]);
        verifier
            .pool_cache
            .lock()
            .put(first_pool, parity_settled_pool_info(first_ts));
        for (xorname, tagged) in &first {
            verifier
                .verify_payment(xorname, Some(tagged), VerificationContext::ClientPut)
                .await
                .expect("first pool admitted");
        }
        assert_eq!(verifier.merkle_parity_emission_count(), 1);

        // A fresh batch: new candidate keypairs and leaf salts give it a
        // different pool hash. Its chunk is one the first batch did not carry,
        // so this exercises verification rather than the verified-address cache.
        let (second, second_pool, second_ts) = make_valid_merkle_proofs_for_one_pool(&[2]);
        let (second_xorname, second_tagged) = second.first().expect("one proof").clone();
        assert_ne!(first_pool, second_pool, "the second batch is a new pool");
        verifier
            .pool_cache
            .lock()
            .put(second_pool, parity_settled_pool_info(second_ts));
        verifier
            .verify_payment(
                &second_xorname,
                Some(&second_tagged),
                VerificationContext::ClientPut,
            )
            .await
            .expect("second pool admitted");

        assert_eq!(
            verifier.merkle_parity_emission_count(),
            2,
            "a second settlement contributes a second sample"
        );
    }

    /// Concurrent admissions of one batch — the normal case, since a client
    /// PUTs a batch's chunks in parallel — must still emit once. The
    /// check-and-insert is a single locked operation for exactly this reason.
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn parity_telemetry_emits_once_under_concurrent_admissions() {
        let verifier = Arc::new(merkle_test_verifier());
        let (proofs, pool_hash, ts) = make_valid_merkle_proofs_for_one_pool(&[0, 1, 2, 3]);
        verifier
            .pool_cache
            .lock()
            .put(pool_hash, parity_settled_pool_info(ts));

        let mut handles = Vec::new();
        for (xorname, tagged) in proofs {
            // Two racing admissions per address, as replication and the direct
            // PUT can both arrive for the same chunk.
            for _ in 0..2 {
                let verifier = Arc::clone(&verifier);
                let tagged = tagged.clone();
                handles.push(tokio::spawn(async move {
                    verifier
                        .verify_payment(&xorname, Some(&tagged), VerificationContext::ClientPut)
                        .await
                }));
            }
        }

        for handle in handles {
            let result = handle.await.expect("task panicked");
            assert!(
                result.is_ok(),
                "every concurrent admission must succeed: {result:?}"
            );
        }

        assert_eq!(
            verifier.merkle_parity_emission_count(),
            1,
            "concurrent admissions of one pool must not race into extra samples"
        );
    }

    /// The first-emission cache is bounded, so a node admitting an unbounded
    /// stream of distinct pools cannot grow it without limit.
    #[test]
    fn parity_telemetry_first_emission_cache_is_bounded() {
        let verifier = create_test_verifier();
        for i in 0..(MERKLE_PARITY_TELEMETRY_CACHE_CAPACITY + 50) {
            let mut hash: PoolHash = [0u8; 32];
            for (j, b) in i.to_le_bytes().iter().enumerate() {
                if let Some(slot) = hash.get_mut(j) {
                    *slot = *b;
                }
            }
            verifier.merkle_parity_logged.lock().put(hash, ());
        }

        assert_eq!(
            verifier.merkle_parity_logged.lock().len(),
            MERKLE_PARITY_TELEMETRY_CACHE_CAPACITY,
            "the telemetry cache must stay at its capacity"
        );
    }

    #[test]
    fn merkle_required_multiplier_switches_exactly_at_the_boundary() {
        let boundary = 1_785_855_600u64;

        assert_eq!(merkle_required_multiplier(boundary - 1, boundary), 1);
        assert_eq!(
            merkle_required_multiplier(boundary, boundary),
            PAID_QUOTE_PAYMENT_MULTIPLIER,
            "the boundary instant itself is enforcing"
        );
        assert_eq!(
            merkle_required_multiplier(boundary + 1, boundary),
            PAID_QUOTE_PAYMENT_MULTIPLIER
        );
    }

    /// The legacy branch cannot outlive the receipt lifetime, which is what
    /// makes the cutover self-completing.
    ///
    /// It bounds backdating rather than preventing it: mid-window, a stamp just
    /// before the boundary is both unexpired and eligible for 1x, so a modified
    /// client can take that route until it expires. Both halves are asserted
    /// here so neither claim drifts.
    #[test]
    fn merkle_legacy_window_closes_one_receipt_lifetime_after_the_boundary() {
        let boundary = 1_785_855_600u64;
        let expiry = evmlib::merkle_payments::MERKLE_PAYMENT_EXPIRATION;

        // Mid-window: a deliberately backdated stamp is still unexpired, and
        // still gets the 1x rule. This route is open for exactly one week.
        let mid_window = boundary + expiry / 2;
        let backdated = boundary - 1;
        assert!(
            mid_window - backdated < expiry,
            "the backdated stamp is still within its lifetime"
        );
        assert_eq!(
            merkle_required_multiplier(backdated, boundary),
            1,
            "backdating is bounded by expiry, not blocked outright"
        );

        // Once now is a full receipt lifetime past the boundary, the oldest
        // still-valid stamp is itself at or after the boundary.
        let now = boundary + expiry;
        let oldest_valid_stamp = now - expiry;
        assert_eq!(
            merkle_required_multiplier(oldest_valid_stamp, boundary),
            PAID_QUOTE_PAYMENT_MULTIPLIER,
            "no unexpired receipt can still claim the 1x rule"
        );
    }

    //
    // These constants are load-bearing for both correctness (the storer
    // must look at the same window the client picks from, otherwise honest
    // pools are rejected) and DoS resistance (the timeout caps lookup
    // amplification per forged pool_hash). Pinning them with tests gives
    // future patches a one-line failure if either is silently changed
    // without updating the security argument in the doc comments.
    //
    // Empirical justification, captured during STG-01 investigation on
    // 2026-05-01:
    //
    //   - 60s timeout cut iterative lookups off after ~7 of 20 iterations
    //     (trace from EWR-3 ant-node-1 in CLOSENESS_LOOKUP_TIMEOUT doc).
    //   - K=16 storer window vs K=32 client over-query produced 73%
    //     false-positive mismatch rejections under realistic load
    //     (115 → 31 client mismatches per 5min after K=32 deploy).
    // =========================================================================

    #[test]
    fn closeness_lookup_timeout_is_240s() {
        // Pin the timeout. If a future change drops it back to 60s the
        // failure mode from the trace in the doc comment will return.
        assert_eq!(
            PaymentVerifier::CLOSENESS_LOOKUP_TIMEOUT,
            std::time::Duration::from_mins(4),
            "CLOSENESS_LOOKUP_TIMEOUT must be 240s; if changing this, update \
             the iteration trace in the doc comment and re-validate on a \
             fresh testnet"
        );
    }

    #[test]
    fn closeness_lookup_width_is_32() {
        // Pin the storer's lookup width. Must equal the client's
        // over-query factor (CANDIDATES_PER_POOL * 2 = 32) so the storer
        // sees the same peers the client legitimately picks from.
        assert_eq!(
            PaymentVerifier::CLOSENESS_LOOKUP_WIDTH,
            2 * evmlib::merkle_payments::CANDIDATES_PER_POOL,
            "CLOSENESS_LOOKUP_WIDTH must equal 2 * CANDIDATES_PER_POOL to \
             match the client's over-query in get_merkle_candidate_pool"
        );
    }

    #[test]
    fn closeness_required_threshold_is_majority() {
        // Pin the threshold so a future change can't silently move it. This
        // is the security knob: a 9/16 majority tolerates closest-set
        // divergence between two nodes' views while still requiring most
        // candidates to be real peers the live DHT lists as closest.
        assert_eq!(
            PaymentVerifier::CANDIDATE_CLOSENESS_REQUIRED,
            9,
            "closeness threshold is a 9/16 majority"
        );
    }

    #[test]
    fn closeness_lookup_count_uses_max_of_width_and_pool_len() {
        // The honest case: a 16-candidate pool must trigger a 32-peer
        // network lookup. This is the K=16-rejects-honest-pool fix from
        // the STG-01 investigation — without it, the storer never
        // observes the peers at network-true positions 17–32 that the
        // client legitimately picks from.
        let standard =
            PaymentVerifier::closeness_lookup_count(evmlib::merkle_payments::CANDIDATES_PER_POOL);
        assert_eq!(
            standard, 32,
            "honest 16-candidate pool must trigger a 32-peer DHT lookup"
        );

        // Future-proof: if a protocol bump ever produces a pool larger
        // than CLOSENESS_LOOKUP_WIDTH, lookup_count must scale with the
        // pool — not truncate to WIDTH. Truncating would let an attacker
        // hide candidates by padding the pool past the storer's window.
        assert_eq!(
            PaymentVerifier::closeness_lookup_count(64),
            64,
            "lookup_count must scale up if pool exceeds CLOSENESS_LOOKUP_WIDTH"
        );

        // Lower bound (also covered by the const-assert below; pin the
        // runtime path too in case the const-assert is ever removed).
        assert_eq!(
            PaymentVerifier::closeness_lookup_count(1),
            PaymentVerifier::CLOSENESS_LOOKUP_WIDTH,
            "lookup_count must never drop below CLOSENESS_LOOKUP_WIDTH"
        );
    }

    // Compile-time invariant: the `closeness_lookup_count` formula relies
    // on WIDTH being ≥ CANDIDATES_PER_POOL so we never request fewer peers
    // than the pool itself contains.
    const _: () = assert!(
        PaymentVerifier::CLOSENESS_LOOKUP_WIDTH >= evmlib::merkle_payments::CANDIDATES_PER_POOL,
        "CLOSENESS_LOOKUP_WIDTH must be ≥ CANDIDATES_PER_POOL",
    );

    // =========================================================================
    // Closeness-match logic tests
    //
    // These tests use the extracted `check_closeness_match` helper to
    // exercise the matching logic directly with synthetic peer-ID sets,
    // without standing up a real DHT. They cover:
    //
    //   - the 9/16 majority threshold (accept at exactly 9, reject below);
    //   - that a candidate counts only via exact membership in the storer's
    //     returned closest peers, so off-network fabrications are rejected;
    //   - the sparse-network short-circuit.
    //
    // Synthetic PeerIds put the tag in `bytes[0]`, so a candidate is in or
    // out of the network's returned set purely by tag value.
    // =========================================================================

    /// Build a deterministic `PeerId` from a single byte tag.
    fn synthetic_peer_id(tag: u8) -> PeerId {
        let mut bytes = [0u8; 32];
        bytes[0] = tag;
        PeerId::from_bytes(bytes)
    }

    /// Build a vector of synthetic `PeerId`s tagged with bytes 1..=n.
    fn synthetic_peer_ids(n: u8) -> Vec<PeerId> {
        (1..=n).map(synthetic_peer_id).collect()
    }

    #[test]
    fn closeness_match_passes_when_all_16_candidates_in_top_16() {
        // Trivial case: every candidate is in the network's top-16.
        // Asserts the happy path still works after the refactor.
        let candidates = synthetic_peer_ids(16);
        let network = synthetic_peer_ids(16);
        let pool_address = [0u8; 32];
        let result = PaymentVerifier::check_closeness_match(&candidates, &network, &pool_address);
        assert!(result.is_ok(), "all-in-top-16 pool must pass: {result:?}");
    }

    #[test]
    fn closeness_match_passes_when_candidates_span_positions_1_to_15_and_17() {
        // The client's pool contains 16 candidates, 15 at network-true
        // positions 1..=15 plus one at position 17 (the position-16 peer was
        // unresponsive when the client over-queried). Under K=32 all 16 are
        // exact matches, comfortably ≥ the 9/16 majority.
        let candidates = synthetic_peer_ids(15)
            .into_iter()
            .chain(std::iter::once(synthetic_peer_id(17)))
            .collect::<Vec<_>>();
        // Lookup window = 32, includes position 17.
        let network: Vec<PeerId> = (1..=32).map(synthetic_peer_id).collect();
        let pool_address = [0u8; 32];
        let result = PaymentVerifier::check_closeness_match(&candidates, &network, &pool_address);
        assert!(
            result.is_ok(),
            "pool with one candidate at position 17 must pass: {result:?}"
        );
    }

    #[test]
    fn closeness_match_accepts_honest_skew_via_exact_matches() {
        // Honest skew: the client's 16 candidates span network-true positions
        // {1..=12, 17, 19, 21, 23}. The lookup window of 32 covers all of
        // them, so all 16 are exact matches — trivially ≥ the 9/16 majority.
        let candidates: Vec<PeerId> = (1..=12u8)
            .chain([17u8, 19, 21, 23])
            .map(synthetic_peer_id)
            .collect();
        let pool_address = [0u8; 32];
        let network: Vec<PeerId> = (1..=32).map(synthetic_peer_id).collect();

        let result = PaymentVerifier::check_closeness_match(&candidates, &network, &pool_address);
        assert!(
            result.is_ok(),
            "honest pool fully inside the lookup window must pass: {result:?}"
        );
    }

    #[test]
    fn closeness_match_rejects_forged_pool() {
        // Security floor: a fully-forged pool whose candidate PeerIds are
        // disjoint from the network's returned closest peers must be
        // rejected. The lowered majority threshold must NOT let off-network
        // fabrications pass — every counted candidate has to be a peer the
        // live DHT actually returned.
        let forged_candidates: Vec<PeerId> = (100..=115).map(synthetic_peer_id).collect();
        let network: Vec<PeerId> = (1..=32).map(synthetic_peer_id).collect();
        let pool_address = [0u8; 32];

        let result =
            PaymentVerifier::check_closeness_match(&forged_candidates, &network, &pool_address);
        match result {
            Err(Error::Payment(msg)) => {
                assert!(
                    msg.contains("candidate pub_keys do not match"),
                    "expected forged-pool rejection message, got: {msg}"
                );
            }
            other => {
                panic!("forged pool disjoint from the network set must be rejected: {other:?}")
            }
        }
    }

    #[test]
    fn closeness_match_rejects_pool_below_majority() {
        // Threshold sanity: 8 candidates are exact matches (tags 1..=8) and
        // the other 8 are off-network fabrications (tags 100..=107). 8 < 9
        // → reject.
        let mut candidates = synthetic_peer_ids(8);
        candidates.extend((100..=107).map(synthetic_peer_id)); // 8 fabrications
        let network: Vec<PeerId> = (1..=32).map(synthetic_peer_id).collect();
        let pool_address = [0u8; 32];

        let result = PaymentVerifier::check_closeness_match(&candidates, &network, &pool_address);
        assert!(
            result.is_err(),
            "8 matches < majority of 9/16 must reject: {result:?}"
        );
    }

    #[test]
    fn closeness_match_accepts_at_exactly_majority() {
        // Threshold sanity: exactly 9 candidates are exact matches (tags
        // 1..=9), the other 7 are off-network fabrications (tags 100..=106).
        // 9 ≥ 9 → accept.
        let mut candidates = synthetic_peer_ids(9);
        candidates.extend((100..=106).map(synthetic_peer_id)); // 7 fabrications
        let network: Vec<PeerId> = (1..=32).map(synthetic_peer_id).collect();
        let pool_address = [0u8; 32];

        let result = PaymentVerifier::check_closeness_match(&candidates, &network, &pool_address);
        assert!(
            result.is_ok(),
            "9/16 ≥ majority threshold must accept: {result:?}"
        );
    }

    #[test]
    fn closeness_match_returns_sparse_dht_error_when_lookup_too_small() {
        // The sparse-DHT short-circuit fires when the lookup returned
        // fewer peers than the threshold itself — even an all-matching
        // candidate set can't pass because the storer doesn't have an
        // authoritative view to compare against.
        let candidates = synthetic_peer_ids(16);
        let network = synthetic_peer_ids(8); // < CANDIDATE_CLOSENESS_REQUIRED (9)
        let pool_address = [0u8; 32];

        let result = PaymentVerifier::check_closeness_match(&candidates, &network, &pool_address);
        match result {
            Err(Error::Payment(msg)) => {
                assert!(
                    msg.contains("authoritative DHT lookup returned only 8"),
                    "expected sparse-DHT error message, got: {msg}"
                );
            }
            other => panic!("expected sparse-DHT rejection, got: {other:?}"),
        }
    }

    // ---------- ADR-0004: quote arithmetic re-check ----------

    /// Curve canonicality: any price produced by `calculate_price(n)` is
    /// on-curve by construction. We exercise a spread of `n` covering the
    /// baseline floor (n=0), small counts, the pricing-curve knee
    /// (`n=PRICING_DIVISOR=6000`), and a saturating-arithmetic regime.
    #[test]
    fn adr0004_on_curve_prices_round_trip() {
        for &n in &[0usize, 1, 2, 100, 5999, 6000, 6001, 50_000, 1_000_000] {
            let price = crate::payment::pricing::calculate_price(n);
            assert!(
                PaymentVerifier::quote_price_is_on_curve(&price),
                "calculate_price({n}) = {price} must be on-curve"
            );
        }
    }

    /// Off-curve canonicality: a price one wei above or below an on-curve
    /// point is between two adjacent curve values and must fail the
    /// canonicality predicate. The check IS exact equality, not a tolerance.
    #[test]
    fn adr0004_off_curve_prices_rejected_by_predicate() {
        // n=100 is well above baseline so price ± 1 is non-saturating.
        let on = crate::payment::pricing::calculate_price(100);
        let just_above = on + Amount::from(1u64);
        let just_below = on - Amount::from(1u64);
        assert!(
            !PaymentVerifier::quote_price_is_on_curve(&just_above),
            "price one wei above an on-curve point must be off-curve"
        );
        assert!(
            !PaymentVerifier::quote_price_is_on_curve(&just_below),
            "price one wei below an on-curve point must be off-curve"
        );
    }

    /// A price strictly below the baseline floor is off-curve: the formula's
    /// minimum value is `calculate_price(0) = BASELINE`, so any smaller value
    /// has no corresponding `n`.
    #[test]
    fn adr0004_sub_baseline_price_is_off_curve() {
        let baseline = crate::payment::pricing::calculate_price(0);
        let sub_baseline = baseline - Amount::from(1u64);
        assert!(
            !PaymentVerifier::quote_price_is_on_curve(&sub_baseline),
            "price strictly below baseline must be off-curve"
        );
    }

    /// ADR-0004 storer-side gate: a bundle in which every quote is on-curve
    /// passes the gate **and** the per-quote canonicality predicate. Runs in
    /// every context (no `ClientPut` split): the rule depends only on the
    /// bundle, not on per-peer state. The outer `validate_quote_arithmetic`
    /// short-circuits to `Ok` under the observe-only rollout const, so the
    /// per-quote diagnostics assertion is what proves the bundle is genuinely
    /// on-curve regardless of how the const ships.
    #[test]
    fn adr0004_validate_quote_arithmetic_passes_for_honest_bundle() {
        use evmlib::{EncodedPeerId, RewardsAddress};

        let payment = ProofOfPayment {
            peer_quotes: (0..crate::ant_protocol::CLOSE_GROUP_SIZE)
                .map(|i| {
                    let id: [u8; 32] = rand::random();
                    let byte = u8::try_from(i & 0xFF).unwrap_or(0);
                    let quote = make_fake_quote_at_records(
                        [0xC0u8; 32],
                        SystemTime::now(),
                        RewardsAddress::new([byte; 20]),
                        100 * (i + 1),
                    );
                    (EncodedPeerId::new(id), quote)
                })
                .collect(),
        };
        PaymentVerifier::validate_quote_arithmetic(&payment)
            .expect("honest on-curve bundle must pass the gate (any const value)");
        for (_, quote) in &payment.peer_quotes {
            assert!(
                PaymentVerifier::price_off_curve_diagnostics(&quote.price).is_none(),
                "every quote in honest bundle must be canonically on-curve"
            );
        }
    }

    /// Off-curve quote behaviour follows the rollout gate
    /// [`QUOTE_ARITHMETIC_RECHECK_ENABLED`]. The gate now ships enforcing, so
    /// an off-curve quote must be rejected; the observe-only branch is kept so
    /// the test still documents the semantics if the gate is ever rolled back.
    /// The rejection payload itself is exercised separately by
    /// `adr0004_off_curve_diagnostics_yields_reject_payload`.
    #[test]
    fn adr0004_off_curve_quote_follows_rollout_gate() {
        use evmlib::{EncodedPeerId, RewardsAddress};

        let mut quote = make_fake_quote_at_records(
            [0xC1u8; 32],
            SystemTime::now(),
            RewardsAddress::new([1u8; 20]),
            100,
        );
        // Bump one wei off the curve.
        quote.price += Amount::from(1u64);

        let id: [u8; 32] = rand::random();
        let payment = ProofOfPayment {
            peer_quotes: vec![(EncodedPeerId::new(id), quote)],
        };

        // Both branches of the const-gated split are asserted, so the test
        // keeps its meaning whichever way the gate is set rather than going
        // silently vacuous when it flips.
        if crate::replication::config::QUOTE_ARITHMETIC_RECHECK_ENABLED {
            assert!(
                PaymentVerifier::validate_quote_arithmetic(&payment).is_err(),
                "enforcing rollout must reject off-curve quotes"
            );
        } else {
            assert!(
                PaymentVerifier::validate_quote_arithmetic(&payment).is_ok(),
                "observe-only rollout must not reject off-curve quotes"
            );
        }
    }

    /// Enforcement-branch coverage: the rejection payload (peer id, candidate
    /// `n`, recomputed price) is produced for off-curve prices independently
    /// of the rollout const, so CI exercises the rejection code path even
    /// while [`QUOTE_ARITHMETIC_RECHECK_ENABLED`] ships as `false`. Flipping
    /// the const to `true` then merely wires this diagnostic into the outer
    /// `Err` return, which is what `validate_quote_arithmetic` does.
    #[test]
    fn adr0004_off_curve_diagnostics_yields_reject_payload() {
        let on = crate::payment::pricing::calculate_price(100);
        let off = on + Amount::from(1u64);

        let diag = PaymentVerifier::price_off_curve_diagnostics(&off)
            .expect("off-curve price must produce diagnostics");
        let (candidate_count, recomputed) = diag;
        assert_eq!(candidate_count, 100, "floor candidate must be n=100");
        assert_eq!(
            recomputed, on,
            "recomputed price must equal the floor curve point"
        );
        assert!(
            recomputed < off,
            "off-curve diagnostics' recomputed price must be strictly below the off-curve input"
        );

        // And an on-curve price must produce no diagnostics.
        assert!(
            PaymentVerifier::price_off_curve_diagnostics(&on).is_none(),
            "on-curve price must yield no off-curve diagnostics"
        );
    }

    /// Saturation regime: a price strictly above
    /// `calculate_price(u64::MAX-equivalent saturating ceiling)` is rejected.
    /// We do not have direct access to that ceiling, but `Amount::MAX` is
    /// guaranteed above it (since `calculate_price(usize::MAX)` saturates to
    /// some value strictly less than `Amount::MAX` due to the additive
    /// baseline). The gate must reject it.
    #[test]
    fn adr0004_amount_max_price_is_off_curve() {
        let price = Amount::MAX;
        assert!(
            !PaymentVerifier::quote_price_is_on_curve(&price),
            "Amount::MAX must not be a valid on-curve price"
        );
    }

    /// Merkle gate, predicate-level: the same canonicality rule applies to
    /// `MerklePaymentCandidateNode.price`. We don't construct a full merkle
    /// proof here (the test fixtures for that live elsewhere); we prove the
    /// underlying decision matches the single-node side, so the Merkle gate
    /// inherits the same correctness as `validate_quote_arithmetic`.
    #[test]
    fn adr0004_merkle_candidate_canonicality_matches_single_node() {
        // Every on-curve `n` produces a price the predicate accepts; one wei
        // off produces a price the predicate rejects. This is the entire
        // contract; the Merkle gate's outer wrapper enforces the same const
        // as the single-node gate, so the wrappers are mechanically
        // equivalent.
        for &n in &[0usize, 1, 100, 6000, 1_000_000] {
            let on = crate::payment::pricing::calculate_price(n);
            assert!(
                PaymentVerifier::quote_price_is_on_curve(&on),
                "merkle candidate price for n={n} must be on-curve"
            );
            if n > 0 {
                let off = on + Amount::from(1u64);
                assert!(
                    !PaymentVerifier::quote_price_is_on_curve(&off),
                    "merkle candidate price one wei above n={n} must be off-curve"
                );
            }
        }
    }

    /// Merkle gate, pool-level: build a real signed candidate pool, set every
    /// candidate's price to the same on-curve value, and assert the gate
    /// passes. Then bump one candidate's price one wei off-curve and assert
    /// the per-candidate diagnostics correctly identify it. We use the
    /// diagnostics predicate rather than the outer `validate_merkle_candidate_arithmetic`
    /// because the outer wrapper short-circuits to `Ok` under the observe-only
    /// rollout const; the diagnostics path is what carries the rejection
    /// information when enforcement flips on.
    #[test]
    fn adr0004_merkle_pool_off_curve_candidate_caught_by_diagnostics() {
        use evmlib::merkle_payments::MerklePaymentCandidatePool;

        let timestamp = 1_700_000_000u64;
        let mut candidates = make_candidate_nodes(timestamp);

        // Set every candidate price to calculate_price(500) so the pool is
        // honestly on-curve to start. The binding must move with the price:
        // ADR-0004 requires `price == calculate_price(committed_key_count)`, so
        // repricing at 500 records while leaving the count at 0 would make the
        // pool dishonest rather than honest.
        let on_curve = crate::payment::pricing::calculate_price(500);
        for c in &mut candidates {
            c.price = on_curve;
            c.committed_key_count = 500;
            c.commitment_pin = Some([0xA5u8; 32]);
        }
        let pool = MerklePaymentCandidatePool {
            midpoint_proof: fake_midpoint_proof(),
            candidate_nodes: candidates,
        };
        // The outer wrapper is rollout-gated, but a fully on-curve pool must
        // pass it under any const value because the loop finds no off-curve
        // candidate to reject.
        PaymentVerifier::validate_merkle_candidate_arithmetic(&pool)
            .expect("honest on-curve pool must pass merkle gate (any const value)");
        for c in &pool.candidate_nodes {
            assert!(
                PaymentVerifier::price_off_curve_diagnostics(&c.price).is_none(),
                "every honest candidate must be canonically on-curve"
            );
        }

        // Now bump exactly one candidate off-curve and check that the
        // diagnostics path catches it. (The outer wrapper still short-circuits
        // under observe-only; this proves the underlying detection works
        // independently of the rollout const, exercising the rejection-payload
        // path in CI.)
        let mut tampered = pool;
        tampered.candidate_nodes[3].price += Amount::from(1u64);
        let mut off_curve_seen = 0;
        for c in &tampered.candidate_nodes {
            if PaymentVerifier::price_off_curve_diagnostics(&c.price).is_some() {
                off_curve_seen += 1;
            }
        }
        assert_eq!(
            off_curve_seen, 1,
            "exactly one tampered candidate must register as off-curve"
        );
    }

    // === ADR-0004 binding-shape + cross-check unit tests ===

    use crate::payment::pricing::calculate_price as cp;

    /// Build a real signed commitment over `n` synthetic keys for tests.
    fn test_built_commitment(n: u32) -> crate::replication::commitment_state::BuiltCommitment {
        use saorsa_pqc::api::sig::ml_dsa_65;
        let (pk, sk) = ml_dsa_65().generate_keypair().expect("keypair");
        let pk_bytes = pk.to_bytes();
        let peer_id = blake3::hash(&pk_bytes);
        let entries: Vec<([u8; 32], [u8; 32])> = (0..n)
            .map(|i| {
                let mut k = [0u8; 32];
                k[..4].copy_from_slice(&i.to_le_bytes());
                let mut b = [1u8; 32];
                b[..4].copy_from_slice(&i.to_le_bytes());
                (k, b)
            })
            .collect();
        crate::replication::commitment_state::BuiltCommitment::build(
            entries,
            peer_id.as_bytes(),
            &sk,
            &pk_bytes,
        )
        .expect("build commitment")
    }

    #[test]
    fn binding_baseline_ok_only_at_baseline_price() {
        // (0, None) with calculate_price(0) is the valid baseline.
        assert!(PaymentVerifier::binding_violation(0, None, &cp(0)).is_none());
        // (0, None) with a non-baseline price is REJECTED — this is the BLOCKER
        // bypass the round-1 review found (unpinned quote priced above baseline).
        assert!(PaymentVerifier::binding_violation(0, None, &cp(500)).is_some());
    }

    #[test]
    fn binding_bound_ok_only_with_pin_and_exact_price() {
        let pin = [9u8; 32];
        // (n>0, Some(pin)) priced exactly is valid.
        assert!(PaymentVerifier::binding_violation(500, Some(pin), &cp(500)).is_none());
        // (n>0, Some(pin)) priced for a DIFFERENT count is rejected (on-curve
        // but wrong count — stronger than canonicality).
        assert!(PaymentVerifier::binding_violation(500, Some(pin), &cp(499)).is_some());
    }

    #[test]
    fn binding_rejects_incoherent_shapes() {
        let pin = [9u8; 32];
        // count > 0 but no pin: unauditable.
        assert!(PaymentVerifier::binding_violation(500, None, &cp(500)).is_some());
        // count 0 but a pin: incoherent baseline.
        assert!(PaymentVerifier::binding_violation(0, Some(pin), &cp(0)).is_some());
    }

    #[test]
    fn binding_rejects_count_above_cap() {
        let pin = [9u8; 32];
        let over = crate::replication::commitment::MAX_COMMITMENT_KEY_COUNT + 1;
        assert!(PaymentVerifier::binding_violation(over, Some(pin), &cp(over as usize)).is_some());
    }

    #[test]
    fn cross_check_match_when_pin_and_count_agree() {
        let built = test_built_commitment(12);
        let outcome = PaymentVerifier::cross_check_binding(12, built.hash(), built.commitment());
        assert_eq!(outcome, CrossCheck::Match);
    }

    #[test]
    fn cross_check_mismatch_when_count_inflated() {
        let built = test_built_commitment(12);
        // Quote claims 999 but the pinned commitment attests 12.
        let outcome = PaymentVerifier::cross_check_binding(999, built.hash(), built.commitment());
        assert_eq!(
            outcome,
            CrossCheck::Mismatch {
                quoted_key_count: 999,
                committed_key_count: 12,
            }
        );
    }

    #[test]
    fn cross_check_unresolved_when_pin_wrong() {
        let built = test_built_commitment(12);
        // Pin does not match the supplied commitment's hash: not evidence.
        let outcome = PaymentVerifier::cross_check_binding(12, [0xFFu8; 32], built.commitment());
        assert_eq!(outcome, CrossCheck::PinDoesNotResolve);
    }

    #[test]
    fn fresh_cached_commitment_honours_ttl_boundary() {
        use crate::replication::commitment_state::PeerCommitmentRecord;
        let built = test_built_commitment(5);
        let commitment = built.commitment().clone();
        let pin = built.hash();
        let ttl = std::time::Duration::from_hours(3);
        let now = std::time::Instant::now();

        // Fresh AND matching pin -> resolves to the commitment.
        let fresh = PeerCommitmentRecord::from_verified(commitment.clone(), now);
        assert!(
            PaymentVerifier::fresh_cached_commitment(&fresh, pin, now, ttl).is_some(),
            "a fresh cache entry whose hash matches the pin must resolve"
        );

        // Fresh but a DIFFERENT pin -> treated as not-cached (None), so the
        // caller falls through to fetch the actually-quoted pin instead of
        // mis-resolving against the peer's latest (different) commitment.
        assert!(
            PaymentVerifier::fresh_cached_commitment(&fresh, [0xEEu8; 32], now, ttl).is_none(),
            "a fresh cache entry for a DIFFERENT pin must not resolve (fetch fallback runs)"
        );

        // Stale: received older than the TTL -> treated as unknown (None), the
        // ADR-0004 false-positive guard against an aged cache entry.
        //
        // Advance the comparison clock PAST the TTL rather than subtracting the
        // TTL from `now`: on Windows the monotonic `Instant` epoch can be
        // younger than a multi-hour TTL, so `now.checked_sub(ttl + 1s)`
        // underflows to `None` and panics. `checked_add` from the receipt time
        // is always in range and is equivalent for the age comparison.
        let received_at = now;
        let now_after_ttl = received_at
            .checked_add(ttl + std::time::Duration::from_secs(1))
            .expect("instant in range");
        let stale = PeerCommitmentRecord::from_verified(commitment, received_at);
        assert!(
            PaymentVerifier::fresh_cached_commitment(&stale, pin, now_after_ttl, ttl).is_none(),
            "a cache entry older than the answerability TTL must be treated as unknown"
        );
    }

    #[test]
    fn fetched_commitment_must_be_bound_to_the_queried_peer() {
        // A fetched commitment is accepted only when it is bound to the peer we
        // asked (sender_peer_id == peer_id) and hashes to the requested pin.
        let built = test_built_commitment(8);
        let commitment = built.commitment().clone();
        let pin = built.hash();
        let owner = PeerId::from_bytes(commitment.sender_peer_id);

        // Correct owner + correct pin -> accepted.
        assert!(
            PaymentVerifier::fetched_commitment_is_valid(&commitment, &owner, pin),
            "a peer's own validly-signed commitment, hashing to the pin, must be accepted"
        );

        // Same (validly signed) commitment but attributed to a DIFFERENT peer ->
        // rejected. This is the MAJOR fix: a peer must not be able to answer with
        // someone else's commitment and have it pass as its own.
        let other = PeerId::from_bytes([0xABu8; 32]);
        assert!(
            !PaymentVerifier::fetched_commitment_is_valid(&commitment, &other, pin),
            "another peer's commitment must be rejected for the queried peer"
        );

        // Correct owner but wrong pin -> rejected.
        assert!(
            !PaymentVerifier::fetched_commitment_is_valid(&commitment, &owner, [0u8; 32]),
            "a commitment that does not hash to the requested pin must be rejected"
        );
    }

    #[tokio::test]
    async fn emit_mismatch_evidence_without_p2p_does_not_panic() {
        // Guards the "always best-effort" degrade path: with no P2P handle there
        // is no trust engine to act on, so `emit_mismatch_evidence` must be a
        // no-op that never panics or errors on the payment path. This is a
        // no-panic guard on the degraded path — it does NOT exercise the
        // evidence->action mapping (that needs a live trust engine).
        let built = test_built_commitment(12);
        let evidence = crate::replication::types::FailureEvidence::QuoteCommitmentMismatch {
            peer: PeerId::from_bytes([1u8; 32]),
            pinned_commitment: [2u8; 32],
            quoted_key_count: 999,
            committed_key_count: 12,
            quote_artifact: vec![0xAA; 16],
            commitment: Box::new(built.commitment().clone()),
        };
        // No P2P -> no trust event even if enforce were on; must not panic.
        PaymentVerifier::emit_mismatch_evidence(&evidence, None).await;
    }

    #[test]
    fn valid_sidecar_is_indexed_and_resolves_synchronously() {
        // A valid sidecar blob is indexed under its own (peer, pin) so the
        // cross-check resolves it synchronously — "the commitment arrived with
        // the quote", no gossip-cache hit or fetch needed.
        let built = test_built_commitment(9);
        let commitment = built.commitment().clone();
        let pin = built.hash();
        let owner = PeerId::from_bytes(commitment.sender_peer_id);
        let blob = rmp_serde::to_vec(&commitment).expect("serialize sidecar");

        let map = PaymentVerifier::index_valid_sidecars(std::slice::from_ref(&blob));
        assert!(
            map.contains_key(&(owner, pin)),
            "a valid sidecar must be indexed under its own (peer, pin)"
        );

        // A garbage blob is silently skipped (resolution falls back), never a
        // hard error.
        let map2 = PaymentVerifier::index_valid_sidecars(&[vec![0xFF; 8]]);
        assert!(map2.is_empty(), "an unparseable sidecar must be skipped");
    }
}