freenet 0.2.94

Freenet core software
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
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
//! Deterministic simulation tests using Turmoil.
//!
//! All tests in this file use Turmoil's deterministic scheduler for reproducible execution.
//! Same seed MUST produce identical results across runs.
//!
//! Thread-local state (GlobalRng, GlobalSimulationTime, GlobalTestMetrics) and per-network
//! registries (sockets, topology, fault injectors) provide test isolation, so parallel
//! execution is safe.
//!
//! Enable with: cargo test -p freenet --features "simulation_tests,testing" --test simulation_integration
//!
//! The `testing` feature is required for `run_controlled_simulation` method.
//!
//! For non-deterministic smoke tests, see `simulation_smoke.rs`.

#![cfg(feature = "simulation_tests")]

use freenet::config::GlobalTestMetrics;
use freenet::config::{GlobalRng, GlobalSimulationTime, SimulationTransportOpt};
use freenet::dev_tool::{
    RequestId, SimNetwork, StreamId, VirtualTime, check_convergence_from_logs,
    reset_channel_id_counter, reset_event_id_counter, reset_global_node_index, reset_nonce_counter,
};
use freenet::simulation::TimeSource;
use freenet::transport::in_memory_socket::{
    SimulationSocket, clear_all_socket_registries, register_address_network,
    register_network_time_source,
};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::net::{Ipv6Addr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;

// =============================================================================
// Test Configuration
// =============================================================================

/// Configuration for a simulation test.
struct TestConfig {
    name: &'static str,
    seed: u64,
    gateways: usize,
    nodes: usize,
    ring_max_htl: usize,
    rnd_if_htl_above: usize,
    max_connections: usize,
    min_connections: usize,
    max_contracts: usize,
    iterations: usize,
    duration: Duration,
    /// Time to sleep between each event in turmoil virtual time.
    /// Default: 200ms (sufficient for event propagation in deterministic sim).
    event_wait: Duration,
    /// Time to sleep after all events complete (post-events phase).
    sleep_after_events: Duration,
    require_convergence: bool,
    /// Optional latency range for jitter simulation (min..max)
    latency_range: Option<std::ops::Range<Duration>>,
    /// Optional message loss rate for fault injection (0.0 to 1.0)
    message_loss_rate: f64,
    /// Use MockWasmRuntime (production ContractExecutor path) instead of MockRuntime.
    ///
    /// **Prefer `true` for new tests.** MockWasmRuntime shares production `bridged_*`
    /// code paths (init tracking, validation, subscriber notifications, corrupted state
    /// recovery). MockRuntime has its own independent implementation that can silently
    /// drift from production behavior. See issue #3141.
    use_mock_wasm: bool,
}

impl TestConfig {
    /// Create a small test configuration (quick CI tests).
    fn small(name: &'static str, seed: u64) -> Self {
        Self {
            name,
            seed,
            gateways: 1,
            nodes: 3,
            ring_max_htl: 7,
            rnd_if_htl_above: 3,
            max_connections: 10,
            min_connections: 2,
            max_contracts: 3,
            iterations: 15,
            duration: Duration::from_secs(20),
            event_wait: Duration::from_millis(200),
            sleep_after_events: Duration::from_secs(1),
            require_convergence: true,
            latency_range: None,
            message_loss_rate: 0.0,
            use_mock_wasm: false,
        }
    }

    /// Create a medium test configuration.
    fn medium(name: &'static str, seed: u64) -> Self {
        Self {
            name,
            seed,
            gateways: 2,
            nodes: 6,
            ring_max_htl: 10,
            rnd_if_htl_above: 7,
            max_connections: 15,
            min_connections: 2,
            max_contracts: 8,
            iterations: 100,
            duration: Duration::from_secs(120),
            event_wait: Duration::from_millis(200),
            sleep_after_events: Duration::from_secs(3),
            require_convergence: true,
            latency_range: None,
            message_loss_rate: 0.0,
            use_mock_wasm: false,
        }
    }

    /// Create a large/dense test configuration.
    fn large(name: &'static str, seed: u64) -> Self {
        Self {
            name,
            seed,
            gateways: 3,
            nodes: 12,
            ring_max_htl: 12,
            rnd_if_htl_above: 8,
            max_connections: 12,
            min_connections: 6,
            max_contracts: 15,
            iterations: 150,
            duration: Duration::from_secs(300),
            event_wait: Duration::from_millis(200),
            sleep_after_events: Duration::from_secs(10),
            require_convergence: true,
            latency_range: None,
            message_loss_rate: 0.0,
            use_mock_wasm: false,
        }
    }

    /// Create a long-running test configuration (1 hour virtual time).
    ///
    /// Designed to uncover time-dependent bugs:
    /// - Connection timeout handling over extended periods
    /// - State drift in long-lived contracts
    /// - Timer edge cases (keep-alive, connection idle timeout)
    /// - Resource exhaustion patterns
    ///
    /// Events are distributed across the full hour (1 event every ~10s) so
    /// the network must remain functional throughout — not just survive idle.
    ///
    /// Includes 10-50ms latency jitter to simulate realistic network conditions.
    ///
    /// # Virtual Time Breakdown
    /// - 360 events × 10s between events = 3600 seconds (1 hour)
    /// - Post-events buffer: 10 seconds for final propagation
    #[allow(dead_code)]
    fn long_running(name: &'static str, seed: u64) -> Self {
        // 360 events × 10s apart = 3600s (1 hour) virtual time.
        // Turmoil steps every virtual ms: 3600s ≈ 3.6M steps × 8 hosts.
        // Wall clock: ~2.5 min (25x acceleration with 8 hosts).
        Self {
            name,
            seed,
            gateways: 2,
            nodes: 6,
            ring_max_htl: 10,
            rnd_if_htl_above: 5,
            max_connections: 15,
            min_connections: 3,
            max_contracts: 8,
            iterations: 360, // Events distributed across 1 hour virtual
            duration: Duration::from_secs(3700), // Max simulation time (buffer)
            event_wait: Duration::from_secs(10), // 10s between events = ~3600s total
            sleep_after_events: Duration::from_secs(10), // Brief propagation wait
            require_convergence: true,
            // Realistic latency jitter (10-50ms) to uncover timing issues
            latency_range: Some(Duration::from_millis(10)..Duration::from_millis(50)),
            message_loss_rate: 0.0,
            use_mock_wasm: false,
        }
    }

    // Builder methods
    fn with_nodes(mut self, nodes: usize) -> Self {
        self.nodes = nodes;
        self
    }

    fn with_gateways(mut self, gateways: usize) -> Self {
        self.gateways = gateways;
        self
    }

    fn with_iterations(mut self, iterations: usize) -> Self {
        self.iterations = iterations;
        self
    }

    fn with_max_contracts(mut self, max_contracts: usize) -> Self {
        self.max_contracts = max_contracts;
        self
    }

    fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    fn with_sleep(mut self, sleep: Duration) -> Self {
        self.sleep_after_events = sleep;
        self
    }

    fn with_connections(mut self, min: usize, max: usize) -> Self {
        self.min_connections = min;
        self.max_connections = max;
        self
    }

    fn with_htl(mut self, max_htl: usize, rnd_above: usize) -> Self {
        self.ring_max_htl = max_htl;
        self.rnd_if_htl_above = rnd_above;
        self
    }

    fn require_convergence(mut self) -> Self {
        self.require_convergence = true;
        self
    }

    /// Opt out of asserting convergence.
    ///
    /// For `run_direct()` tests this ALSO tells the runner to skip its
    /// up-to-1800s post-event convergence-polling tail and do only a brief
    /// settle instead. Use this for tests that analyze events produced during
    /// the event phase and never assert final-state convergence — running the
    /// advisory polling tail (which only warns, never fails) when the network
    /// happens not to converge just burns wall-clock and risks a CI timeout.
    /// See #3792.
    fn no_convergence_wait(mut self) -> Self {
        self.require_convergence = false;
        self
    }

    /// Add latency jitter simulation.
    #[allow(dead_code)]
    fn with_latency(mut self, min: Duration, max: Duration) -> Self {
        self.latency_range = Some(min..max);
        self
    }

    /// Set the delay between events in virtual time.
    fn with_event_wait(mut self, event_wait: Duration) -> Self {
        self.event_wait = event_wait;
        self
    }

    /// Add message loss fault injection (0.0 to 1.0).
    fn with_message_loss(mut self, rate: f64) -> Self {
        self.message_loss_rate = rate.clamp(0.0, 1.0);
        self
    }

    /// Use MockWasmRuntime (production ContractExecutor path) instead of MockRuntime.
    fn with_mock_wasm(mut self) -> Self {
        self.use_mock_wasm = true;
        self
    }

    /// Run the simulation and return results.
    fn run(self) -> TestResult {
        use freenet::simulation::FaultConfig;

        setup_deterministic_state(self.seed);
        let rt = create_runtime();

        let (sim, logs_handle) = rt.block_on(async {
            let mut sim = SimNetwork::new(
                self.name,
                self.gateways,
                self.nodes,
                self.ring_max_htl,
                self.rnd_if_htl_above,
                self.max_connections,
                self.min_connections,
                self.seed,
            )
            .await;

            // Apply fault injection if configured (latency and/or message loss)
            let has_latency = self.latency_range.is_some();
            let has_loss = self.message_loss_rate > 0.0;
            if has_latency || has_loss {
                let mut builder = FaultConfig::builder();
                if let Some(ref latency) = self.latency_range {
                    builder = builder.latency_range(latency.clone());
                    tracing::info!(
                        "Latency jitter enabled: {:?} - {:?}",
                        latency.start,
                        latency.end
                    );
                }
                if has_loss {
                    builder = builder.message_loss_rate(self.message_loss_rate);
                    tracing::info!(
                        "Message loss enabled: {:.1}%",
                        self.message_loss_rate * 100.0
                    );
                }
                sim.with_fault_injection(builder.build());
            }

            let logs_handle = sim.event_logs_handle();
            (sim, logs_handle)
        });

        let sleep_duration = self.sleep_after_events;
        let event_wait = self.event_wait;
        let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
            self.seed,
            self.max_contracts,
            self.iterations,
            self.duration,
            event_wait,
            move || async move {
                tokio::time::sleep(sleep_duration).await;
                Ok(())
            },
        );

        let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
        let event_count = rt.block_on(async { logs_handle.lock().await.len() });

        TestResult {
            seed: self.seed,
            name: self.name,
            simulation_result: result,
            convergence,
            event_count,
            require_convergence: self.require_convergence,
            logs_handle,
        }
    }

    /// Run the simulation using the direct runner (single-threaded paused-time runtime).
    ///
    /// This avoids turmoil's O(n²) link overhead, making it suitable for large-scale
    /// or long-running simulations.
    ///
    /// Note: Does not support mid-simulation fault injection (partitions, crashes).
    /// Static latency jitter via `FaultConfig` is supported.
    #[allow(dead_code)] // Used by nightly_tests-gated tests
    fn run_direct(self) -> TestResult {
        use freenet::simulation::FaultConfig;

        setup_deterministic_state(self.seed);
        let rt = create_runtime();

        let use_mock_wasm = self.use_mock_wasm;
        // Tests that never assert convergence (require_convergence = false) skip the
        // direct runner's up-to-1800s convergence-polling tail and do a brief settle
        // instead. The tail is advisory (it only warns on failure), so skipping it
        // changes no assertion but keeps wall-clock bounded when the network does not
        // converge — the failure mode that timed out test_interest_renewal in CI (#3792).
        let skip_convergence_wait = !self.require_convergence;
        let (sim, logs_handle) = rt.block_on(async {
            let mut sim = SimNetwork::new(
                self.name,
                self.gateways,
                self.nodes,
                self.ring_max_htl,
                self.rnd_if_htl_above,
                self.max_connections,
                self.min_connections,
                self.seed,
            )
            .await;
            sim.use_mock_wasm = use_mock_wasm;
            sim.skip_convergence_wait = skip_convergence_wait;

            // Apply fault injection if configured (latency and/or message loss)
            let has_latency = self.latency_range.is_some();
            let has_loss = self.message_loss_rate > 0.0;
            if has_latency || has_loss {
                let mut builder = FaultConfig::builder();
                if let Some(ref latency) = self.latency_range {
                    builder = builder.latency_range(latency.clone());
                    tracing::info!(
                        "Latency jitter enabled: {:?} - {:?}",
                        latency.start,
                        latency.end
                    );
                }
                if has_loss {
                    builder = builder.message_loss_rate(self.message_loss_rate);
                    tracing::info!(
                        "Message loss enabled: {:.1}%",
                        self.message_loss_rate * 100.0
                    );
                }
                sim.with_fault_injection(builder.build());
            }

            let logs_handle = sim.event_logs_handle();
            (sim, logs_handle)
        });

        drop(rt);

        let direct_result = sim.run_simulation_direct::<rand::rngs::SmallRng>(
            self.seed,
            self.max_contracts,
            self.iterations,
            self.event_wait,
        );

        // Map anyhow::Result to turmoil::Result for TestResult compatibility
        let simulation_result: turmoil::Result = match direct_result {
            Ok(()) => Ok(()),
            Err(e) => Err(Box::new(std::io::Error::other(e.to_string()))),
        };

        let rt = create_runtime();
        let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
        let event_count = rt.block_on(async { logs_handle.lock().await.len() });

        TestResult {
            seed: self.seed,
            name: self.name,
            simulation_result,
            convergence,
            event_count,
            require_convergence: self.require_convergence,
            logs_handle,
        }
    }
}

/// Result of running a simulation test.
struct TestResult {
    seed: u64,
    name: &'static str,
    simulation_result: turmoil::Result,
    convergence: freenet::dev_tool::ConvergenceResult,
    event_count: usize,
    require_convergence: bool,
    logs_handle: Arc<Mutex<Vec<freenet::tracing::NetLogMessage>>>,
}

impl TestResult {
    /// Assert the simulation completed successfully.
    fn assert_ok(self) -> Self {
        if let Err(e) = &self.simulation_result {
            tracing::error!("============================================================");
            tracing::error!("SIMULATION TEST FAILED: {}", self.name);
            tracing::error!("============================================================");
            tracing::error!("Seed for reproduction: 0x{:X}", self.seed);
            tracing::error!("Error: {:?}", e);
            tracing::error!("============================================================");
        }
        assert!(
            self.simulation_result.is_ok(),
            "{} failed: {:?}",
            self.name,
            self.simulation_result.err()
        );
        self
    }

    /// Log convergence results and optionally assert convergence.
    fn check_convergence(self) -> Self {
        tracing::info!("=== CONVERGENCE CHECK: {} ===", self.name);
        tracing::info!(
            "Result: {} converged, {} diverged, {} events",
            self.convergence.converged.len(),
            self.convergence.diverged.len(),
            self.event_count
        );

        // Get logs for detailed analysis
        let rt = create_runtime();
        let logs = rt.block_on(async { self.logs_handle.lock().await.clone() });

        for diverged in &self.convergence.diverged {
            tracing::warn!(
                "DIVERGED: {} - {} unique states across {} peers",
                diverged.contract_key,
                diverged.unique_state_count(),
                diverged.peer_states.len()
            );
            for (peer, hash) in &diverged.peer_states {
                tracing::warn!("  peer {}: {}", peer, hash);
            }

            // Show stored_hash events only (PutSuccess, UpdateSuccess, BroadcastApplied)
            tracing::debug!(
                "Stored state events for contract {}:",
                diverged.contract_key
            );
            for log in &logs {
                let contract_key = log.kind.contract_key().map(|k| format!("{:?}", k));
                if contract_key.as_ref() == Some(&diverged.contract_key) {
                    if let Some(state_hash) = log.kind.stored_state_hash() {
                        let variant = log.kind.variant_name();
                        tracing::debug!(
                            "  {} @ {}: {} -> {}",
                            log.tx,
                            log.peer_id.socket_addr(),
                            variant,
                            &state_hash[..16]
                        );
                    }
                }
            }

            // Show Subscribe events
            tracing::debug!("Subscribe events for contract {}:", diverged.contract_key);
            for log in &logs {
                let contract_key = log.kind.contract_key().map(|k| format!("{:?}", k));
                if contract_key.as_ref() == Some(&diverged.contract_key) {
                    let variant = log.kind.variant_name();
                    if variant.contains("Subscribe") {
                        tracing::debug!(
                            "  {} @ {}: {}",
                            log.tx,
                            log.peer_id.socket_addr(),
                            variant
                        );
                    }
                }
            }
        }

        if self.require_convergence {
            assert!(
                self.convergence.is_converged(),
                "{} convergence failed: {} converged, {} diverged",
                self.name,
                self.convergence.converged.len(),
                self.convergence.diverged.len()
            );
        }

        tracing::info!("{} PASSED", self.name);
        self
    }

    /// Verify that all operation types (PUT, GET, UPDATE, SUBSCRIBE) were executed.
    ///
    /// This ensures tests are exercising the full range of contract operations
    /// for robust coverage. Fails if any operation type has zero occurrences.
    ///
    /// Also tracks concurrent PUTs - multiple PUTs to the same contract from different peers.
    /// This exercises the CRDT merge logic that was fixed in PR #2683.
    fn verify_operation_coverage(self) -> Self {
        let rt = create_runtime();
        let logs = rt.block_on(async { self.logs_handle.lock().await.clone() });

        let mut put_count = 0;
        let mut get_count = 0;
        let mut update_count = 0;
        let mut subscribe_count = 0;
        let mut contracts_with_puts: std::collections::HashSet<String> =
            std::collections::HashSet::new();

        // Track PUTs per contract per peer to detect concurrent puts
        // Key: contract_key, Value: set of (peer_addr, state_hash) tuples
        let mut puts_per_contract: std::collections::HashMap<
            String,
            std::collections::HashSet<(String, String)>,
        > = std::collections::HashMap::new();

        for log in &logs {
            let variant = log.kind.variant_name();
            if variant.starts_with("Put") {
                put_count += 1;
                if let Some(key) = log.kind.contract_key() {
                    let key_str = format!("{:?}", key);
                    contracts_with_puts.insert(key_str.clone());

                    // Track peer and state for this PUT
                    let peer_addr = format!("{}", log.peer_id.socket_addr());
                    let state_hash = log
                        .kind
                        .state_hash()
                        .map(|h| h[..8].to_string())
                        .unwrap_or_default();
                    puts_per_contract
                        .entry(key_str)
                        .or_default()
                        .insert((peer_addr, state_hash));
                }
            } else if variant.starts_with("Get") {
                get_count += 1;
            } else if variant.starts_with("Update") {
                update_count += 1;
            } else if variant.starts_with("Subscribe") {
                subscribe_count += 1;
            }
        }

        // Count contracts with concurrent puts (multiple unique peer+state combinations)
        let contracts_with_concurrent_puts: Vec<_> = puts_per_contract
            .iter()
            .filter(|(_, puts)| puts.len() > 1)
            .collect();

        tracing::info!("=== OPERATION COVERAGE: {} ===", self.name);
        tracing::info!("PUT operations:       {}", put_count);
        tracing::info!("GET operations:       {}", get_count);
        tracing::info!("UPDATE operations:    {}", update_count);
        tracing::info!("SUBSCRIBE operations: {}", subscribe_count);
        tracing::info!("Contracts with PUTs:  {}", contracts_with_puts.len());
        tracing::info!(
            "Contracts with concurrent PUTs: {}",
            contracts_with_concurrent_puts.len()
        );

        // Log details of concurrent puts for debugging
        for (contract, puts) in &contracts_with_concurrent_puts {
            tracing::debug!(
                "  Contract {} has {} concurrent puts from peers:",
                &contract[..20],
                puts.len()
            );
            for (peer, hash) in puts.iter().take(5) {
                tracing::debug!("    peer={} state={}", peer, hash);
            }
        }

        // Verify minimum coverage - all operation types should be exercised
        assert!(
            put_count > 0,
            "{}: No PUT operations detected - test not exercising puts",
            self.name
        );
        assert!(
            get_count > 0,
            "{}: No GET operations detected - test not exercising gets",
            self.name
        );
        assert!(
            update_count > 0,
            "{}: No UPDATE operations detected - test not exercising updates",
            self.name
        );
        assert!(
            subscribe_count > 0,
            "{}: No SUBSCRIBE operations detected - test not exercising subscribes",
            self.name
        );

        // Verify at least one contract is being tested
        // Note: For larger convergence tests, multiple contracts should naturally be created
        // given sufficient iterations. Small CI tests may only have 1 contract.
        assert!(
            !contracts_with_puts.is_empty(),
            "{}: No contracts with PUTs - test not exercising contract creation",
            self.name
        );

        tracing::info!(
            "{}: Operation coverage verified (PUT={}, GET={}, UPDATE={}, SUBSCRIBE={}, contracts={})",
            self.name,
            put_count,
            get_count,
            update_count,
            subscribe_count,
            contracts_with_puts.len()
        );

        self
    }

    /// Run anomaly detection on the simulation event logs and report findings.
    ///
    /// This is exploratory: it logs all detected anomalies without asserting
    /// that the report is clean. This lets us see what the detectors find
    /// against real simulation data.
    fn verify_state_report(self) -> Self {
        let rt = create_runtime();
        let report = rt.block_on(async {
            let logs = self.logs_handle.lock().await;
            let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
            verifier.verify()
        });

        tracing::info!("=== ANOMALY DETECTION REPORT: {} ===", self.name);
        tracing::info!(
            "Events analyzed: {} total, {} state-mutating",
            report.total_events,
            report.state_events
        );
        tracing::info!("Contracts analyzed: {}", report.contracts_analyzed);
        tracing::info!("Total anomalies: {}", report.anomalies.len());

        if report.anomalies.is_empty() {
            tracing::info!(
                "{}: State verification CLEAN - no anomalies detected",
                self.name
            );
        } else {
            // Break down by category
            let divergences = report.divergences();
            let missing = report.missing_broadcasts();
            let unapplied = report.unapplied_broadcasts();
            let partitions = report.suspected_partitions();
            let stale = report.stale_peers();
            let oscillations = report.state_oscillations();
            let zombies = report.zombie_transactions();
            let storms = report.broadcast_storms();
            let cascades = report.delta_sync_cascades();

            tracing::warn!(
                "{}: {} anomalies detected: divergences={}, missing_broadcasts={}, \
                 unapplied_broadcasts={}, partitions={}, stale_peers={}, oscillations={}, \
                 zombies={}, storms={}, cascades={}",
                self.name,
                report.anomalies.len(),
                divergences.len(),
                missing.len(),
                unapplied.len(),
                partitions.len(),
                stale.len(),
                oscillations.len(),
                zombies.len(),
                storms.len(),
                cascades.len(),
            );

            // Log each anomaly at debug level for detailed inspection
            for (i, anomaly) in report.anomalies.iter().enumerate() {
                tracing::debug!("{}: anomaly[{}] = {:?}", self.name, i, anomaly);
            }
        }

        self
    }

    /// Extract router snapshot data from event logs.
    ///
    /// Returns a list of `(failure_events, success_events, prediction_active)` tuples,
    /// one per `RouterSnapshot` event found in the logs. These snapshots are emitted
    /// every 5 minutes of virtual time by each node's Ring telemetry.
    fn router_snapshots(&self) -> Vec<(usize, usize, bool)> {
        let rt = create_runtime();
        rt.block_on(async {
            let logs = self.logs_handle.lock().await;
            logs.iter()
                .filter_map(|log| log.kind.router_snapshot_summary())
                .collect()
        })
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Setup deterministic simulation state before running a test.
///
/// Resets all thread-local state for deterministic simulation testing.
///
/// All ID counters are now thread-local with per-thread offset blocks, so resetting
/// them is safe for parallel execution — each thread only resets its own counters.
///
/// Per-network state (sockets, topology, fault injectors) is cleaned up by
/// `SimNetwork::Drop`, so no scorched-earth registry clear is needed.
fn setup_deterministic_state(seed: u64) {
    // All state below is thread-local — safe for parallel tests.
    GlobalRng::set_seed(seed);
    const BASE_EPOCH_MS: u64 = 1577836800000; // 2020-01-01 00:00:00 UTC
    const RANGE_MS: u64 = 5 * 365 * 24 * 60 * 60 * 1000; // ~5 years
    GlobalSimulationTime::set_time_ms(BASE_EPOCH_MS + (seed % RANGE_MS));
    GlobalTestMetrics::reset();
    // Reset transport optimization — run_simulation_direct() enables it when needed,
    // but tests that reuse a thread should start with production timer behavior.
    SimulationTransportOpt::disable();

    // Clear CRDT contract registrations from prior tests on this thread.
    freenet::dev_tool::clear_crdt_contracts();

    // Reset all thread-local ID counters for exact event sequence reproducibility.
    RequestId::reset_counter();
    freenet::dev_tool::ClientId::reset_counter();
    reset_event_id_counter();
    reset_channel_id_counter();
    StreamId::reset_counter();
    reset_nonce_counter();
    reset_global_node_index();
}

/// Create a tokio runtime for running simulation setup.
fn create_runtime() -> tokio::runtime::Runtime {
    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap()
}

// =============================================================================
// STRICT Determinism Tests - Exact Event Equality
// =============================================================================

/// Compact fingerprint of a simulation trace for cross-run verification.
///
/// Provides a hash-based integrity check on top of field-by-field assertions.
/// The fingerprint is logged so it can be compared across CI invocations.
#[derive(Debug, Clone, PartialEq, Eq)]
struct TraceFingerprint {
    total_events: usize,
    /// Hash of the full event sequence (order-sensitive).
    sequence_hash: u64,
    /// Hash of the per-type event counts (order-insensitive via BTreeMap).
    counts_hash: u64,
}

impl TraceFingerprint {
    fn from_events(events: &[String], event_counts: &HashMap<String, usize>) -> Self {
        let mut seq_hasher = std::collections::hash_map::DefaultHasher::new();
        for event in events {
            event.hash(&mut seq_hasher);
        }

        let sorted_counts: BTreeMap<&String, &usize> = event_counts.iter().collect();
        let mut counts_hasher = std::collections::hash_map::DefaultHasher::new();
        for (k, v) in &sorted_counts {
            k.hash(&mut counts_hasher);
            v.hash(&mut counts_hasher);
        }

        TraceFingerprint {
            total_events: events.len(),
            sequence_hash: seq_hasher.finish(),
            counts_hash: counts_hasher.finish(),
        }
    }
}

/// **STRICT** determinism test: verifies that same seed produces EXACTLY identical events.
///
/// This test uses Turmoil's deterministic scheduler to ensure reproducible execution.
/// It verifies:
/// 1. Exact same number of events
/// 2. Exact same event counts per type
/// 3. Exact same event sequence (order, content)
///
/// If this test fails, it indicates non-determinism in the simulation that Turmoil
/// doesn't control (e.g., DashMap/HashMap iteration order, external I/O, real time usage).
///
/// The test runs the simulation 3 times with the same seed and compares traces.
/// All three runs must produce identical results.
///
/// **Scale:** Uses 2 gateways + 18 nodes to catch DashMap iteration non-determinism
/// that only manifests at scale (e.g., in subscription/interest management).
///
/// Process isolation via nextest eliminates inter-test DashMap state leakage (#3051).
#[test_log::test]
fn test_strict_determinism_exact_event_equality() {
    const SEED: u64 = 0xDE7E_2A1E_1234;

    /// Captures all simulation state for comparison
    #[derive(Debug, PartialEq)]
    struct SimulationTrace {
        event_counts: HashMap<String, usize>,
        event_sequence: Vec<String>, // event_kind names in order
        total_events: usize,
    }

    fn run_and_trace(name: &str, seed: u64) -> (turmoil::Result, SimulationTrace) {
        setup_deterministic_state(seed);

        let rt = create_runtime();

        // Create SimNetwork and get event logs handle before run_simulation consumes it
        let (sim, logs_handle) = rt.block_on(async {
            let sim = SimNetwork::new(
                name, 2,  // gateways - multi-gateway to test more code paths
                18, // nodes - increased to trigger DashMap iteration issues
                10, // ring_max_htl
                3,  // rnd_if_htl_above
                15, // max_connections - higher to create more subscription operations
                5,  // min_connections
                seed,
            )
            .await;
            let logs_handle = sim.event_logs_handle();
            (sim, logs_handle)
        });

        // Run simulation with Turmoil's deterministic scheduler
        let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
            seed,
            10, // max_contract_num - more contracts = more subscription operations
            40, // iterations - more iterations to trigger DashMap operations
            Duration::from_secs(30), // simulation_duration
            Duration::from_millis(200),
            || async {
                tokio::time::sleep(Duration::from_secs(1)).await;
                Ok(())
            },
        );

        // Extract event trace from the shared logs handle
        let trace = rt.block_on(async {
            let logs = logs_handle.lock().await;
            let mut event_counts: HashMap<String, usize> = HashMap::new();
            let mut event_sequence: Vec<String> = Vec::new();

            for log in logs.iter() {
                let kind_name = log.kind.variant_name().to_string();
                *event_counts.entry(kind_name.clone()).or_insert(0) += 1;
                event_sequence.push(kind_name);
            }

            SimulationTrace {
                total_events: logs.len(),
                event_counts,
                event_sequence,
            }
        });

        (result, trace)
    }

    // Run simulation THREE times with identical seed
    let (result1, trace1) = run_and_trace("strict-det-run1", SEED);
    let (result2, trace2) = run_and_trace("strict-det-run2", SEED);
    let (result3, trace3) = run_and_trace("strict-det-run3", SEED);

    // All simulations should have the same outcome
    assert_eq!(
        result1.is_ok(),
        result2.is_ok(),
        "STRICT DETERMINISM FAILURE: Simulation outcomes differ!\nRun 1: {:?}\nRun 2: {:?}",
        result1,
        result2
    );
    assert_eq!(
        result2.is_ok(),
        result3.is_ok(),
        "STRICT DETERMINISM FAILURE: Simulation outcomes differ!\nRun 2: {:?}\nRun 3: {:?}",
        result2,
        result3
    );

    // Debug: Print detailed event breakdown before assertions
    if trace1.total_events != trace2.total_events || trace2.total_events != trace3.total_events {
        tracing::info!("\n=== DETERMINISM DEBUG ===");
        tracing::info!(
            "Run 1 total: {}, Run 2 total: {}, Run 3 total: {}",
            trace1.total_events,
            trace2.total_events,
            trace3.total_events
        );

        let mut all_types: std::collections::BTreeSet<&String> =
            trace1.event_counts.keys().collect();
        all_types.extend(trace2.event_counts.keys());
        all_types.extend(trace3.event_counts.keys());

        for event_type in all_types {
            let count1 = trace1.event_counts.get(event_type).unwrap_or(&0);
            let count2 = trace2.event_counts.get(event_type).unwrap_or(&0);
            let count3 = trace3.event_counts.get(event_type).unwrap_or(&0);
            if count1 != count2 || count2 != count3 {
                tracing::info!(
                    "  {} : {} vs {} vs {} (DIFFERS)",
                    event_type,
                    count1,
                    count2,
                    count3
                );
            }
        }
        tracing::info!("=========================\n");
    }

    // STRICT ASSERTION 1: Exact same total event count
    assert_eq!(
        trace1.total_events, trace2.total_events,
        "STRICT DETERMINISM FAILURE: Total event counts differ!"
    );
    assert_eq!(
        trace2.total_events, trace3.total_events,
        "STRICT DETERMINISM FAILURE: Total event counts differ!"
    );

    // STRICT ASSERTION 2: Exact same event counts per type
    assert_eq!(
        trace1.event_counts, trace2.event_counts,
        "STRICT DETERMINISM FAILURE: Event counts per type differ!"
    );
    assert_eq!(
        trace2.event_counts, trace3.event_counts,
        "STRICT DETERMINISM FAILURE: Event counts per type differ!"
    );

    // STRICT ASSERTION 3: Exact same event sequence
    for (i, ((e1, e2), e3)) in trace1
        .event_sequence
        .iter()
        .zip(trace2.event_sequence.iter())
        .zip(trace3.event_sequence.iter())
        .enumerate()
    {
        assert_eq!(e1, e2, "Event sequence differs at index {}!", i);
        assert_eq!(e2, e3, "Event sequence differs at index {}!", i);
    }

    // Fingerprint verification: compact hash check for cross-run integrity
    let fp1 = TraceFingerprint::from_events(&trace1.event_sequence, &trace1.event_counts);
    let fp2 = TraceFingerprint::from_events(&trace2.event_sequence, &trace2.event_counts);
    let fp3 = TraceFingerprint::from_events(&trace3.event_sequence, &trace3.event_counts);
    assert_eq!(fp1, fp2, "Fingerprint mismatch between run 1 and run 2");
    assert_eq!(fp2, fp3, "Fingerprint mismatch between run 2 and run 3");

    tracing::info!(
        "STRICT DETERMINISM TEST PASSED: {} events, fingerprint={:#018x}",
        trace1.total_events,
        fp1.sequence_hash
    );
}

/// **STRICT** determinism test with MULTIPLE GATEWAYS.
///
/// This test verifies that simulations with 2+ gateways remain deterministic.
///
/// Process isolation via nextest eliminates inter-test DashMap state leakage (#3051).
#[test_log::test]
fn test_strict_determinism_multi_gateway() {
    const SEED: u64 = 0xAB17_6A7E_1234;

    #[derive(Debug, PartialEq)]
    struct SimulationTrace {
        event_counts: HashMap<String, usize>,
        event_sequence: Vec<String>,
        total_events: usize,
    }

    fn run_and_trace(name: &str, seed: u64) -> (turmoil::Result, SimulationTrace) {
        setup_deterministic_state(seed);

        let rt = create_runtime();

        let (sim, logs_handle) = rt.block_on(async {
            let sim = SimNetwork::new(
                name, 2, // gateways - MULTI-GATEWAY
                6, 7, 3, 10, 2, seed,
            )
            .await;
            let logs_handle = sim.event_logs_handle();
            (sim, logs_handle)
        });

        let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
            seed,
            5,
            20,
            Duration::from_secs(30),
            Duration::from_millis(200),
            || async {
                tokio::time::sleep(Duration::from_secs(1)).await;
                Ok(())
            },
        );

        let trace = rt.block_on(async {
            let logs = logs_handle.lock().await;
            let mut event_counts: HashMap<String, usize> = HashMap::new();
            let mut event_sequence: Vec<String> = Vec::new();

            for log in logs.iter() {
                let kind_name = log.kind.variant_name().to_string();
                *event_counts.entry(kind_name.clone()).or_insert(0) += 1;
                event_sequence.push(kind_name);
            }

            SimulationTrace {
                total_events: logs.len(),
                event_counts,
                event_sequence,
            }
        });

        (result, trace)
    }

    let (result1, trace1) = run_and_trace("multi-gw-det-run1", SEED);
    let (result2, trace2) = run_and_trace("multi-gw-det-run2", SEED);
    let (result3, trace3) = run_and_trace("multi-gw-det-run3", SEED);

    assert_eq!(result1.is_ok(), result2.is_ok());
    assert_eq!(result2.is_ok(), result3.is_ok());

    assert_eq!(trace1.total_events, trace2.total_events);
    assert_eq!(trace2.total_events, trace3.total_events);

    assert_eq!(trace1.event_counts, trace2.event_counts);
    assert_eq!(trace2.event_counts, trace3.event_counts);

    for (i, ((e1, e2), e3)) in trace1
        .event_sequence
        .iter()
        .zip(trace2.event_sequence.iter())
        .zip(trace3.event_sequence.iter())
        .enumerate()
    {
        assert_eq!(e1, e2, "Event sequence differs at index {}!", i);
        assert_eq!(e2, e3, "Event sequence differs at index {}!", i);
    }

    // Fingerprint verification
    let fp1 = TraceFingerprint::from_events(&trace1.event_sequence, &trace1.event_counts);
    let fp2 = TraceFingerprint::from_events(&trace2.event_sequence, &trace2.event_counts);
    let fp3 = TraceFingerprint::from_events(&trace3.event_sequence, &trace3.event_counts);
    assert_eq!(fp1, fp2, "Fingerprint mismatch between run 1 and run 2");
    assert_eq!(fp2, fp3, "Fingerprint mismatch between run 2 and run 3");

    tracing::info!(
        "MULTI-GATEWAY DETERMINISM TEST PASSED: {} events (2 gateways), fingerprint={:#018x}",
        trace1.total_events,
        fp1.sequence_hash
    );
}

/// **STRICT** determinism test: verifies that same seed produces identical replay.
#[test_log::test]
fn test_deterministic_replay_events() {
    const SEED: u64 = 0xDEAD_BEEF_1234;

    #[derive(Debug, PartialEq)]
    struct ReplayTrace {
        event_counts: HashMap<String, usize>,
        total_events: usize,
    }

    fn run_and_trace(name: &str, seed: u64) -> (turmoil::Result, ReplayTrace) {
        setup_deterministic_state(seed);

        let rt = create_runtime();

        let (sim, logs_handle) = rt.block_on(async {
            let sim = SimNetwork::new(name, 1, 3, 7, 3, 10, 2, seed).await;
            let logs_handle = sim.event_logs_handle();
            (sim, logs_handle)
        });

        let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
            seed,
            3,
            10,
            Duration::from_secs(20),
            Duration::from_millis(200),
            || async {
                tokio::time::sleep(Duration::from_secs(1)).await;
                Ok(())
            },
        );

        let trace = rt.block_on(async {
            let logs = logs_handle.lock().await;
            let mut event_counts: HashMap<String, usize> = HashMap::new();

            for log in logs.iter() {
                let kind_name = log.kind.variant_name().to_string();
                *event_counts.entry(kind_name).or_insert(0) += 1;
            }

            ReplayTrace {
                total_events: logs.len(),
                event_counts,
            }
        });

        (result, trace)
    }

    let (result1, trace1) = run_and_trace("replay-run1", SEED);
    let (result2, trace2) = run_and_trace("replay-run2", SEED);

    assert_eq!(result1.is_ok(), result2.is_ok());
    assert!(trace1.total_events > 0);
    assert_eq!(trace1.event_counts, trace2.event_counts);
    assert_eq!(trace1.total_events, trace2.total_events);

    // Fingerprint verification (counts-only since this test doesn't track sequence)
    let fp1 = TraceFingerprint::from_events(&[], &trace1.event_counts);
    let fp2 = TraceFingerprint::from_events(&[], &trace2.event_counts);
    assert_eq!(
        fp1.counts_hash, fp2.counts_hash,
        "Counts fingerprint mismatch between replay runs"
    );

    tracing::info!(
        "Deterministic replay test passed - {} events, counts_hash={:#018x}",
        trace1.total_events,
        fp1.counts_hash
    );
}

// =============================================================================
// Basic Infrastructure Tests
// =============================================================================

/// Test that SimNetwork can be created and peers can be built.
#[test_log::test]
fn test_sim_network_basic_setup() {
    #[allow(clippy::unusual_byte_groupings)]
    const SEED: u64 = 0xBA51C_5E70_0001; // "BASIC SETO 0001"

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    rt.block_on(async {
        let mut sim = SimNetwork::new("basic-setup", 1, 5, 7, 3, 10, 2, SEED).await;
        sim.with_start_backoff(Duration::from_millis(100));

        let peers = sim.build_peers();
        assert_eq!(peers.len(), 6, "Expected 1 gateway + 5 nodes = 6 peers");

        let gateway_count = peers.iter().filter(|(l, _)| !l.is_node()).count();
        let node_count = peers.iter().filter(|(l, _)| l.is_node()).count();
        assert_eq!(gateway_count, 1, "Expected 1 gateway");
        assert_eq!(node_count, 5, "Expected 5 regular nodes");
    });
}

/// Test that peers can start and run under Turmoil's deterministic scheduler.
#[test_log::test]
fn test_sim_network_peer_startup() {
    TestConfig::small("peer-startup", 0xBEE2_5747_0001)
        .with_nodes(2)
        .with_iterations(5)
        .with_duration(Duration::from_secs(15))
        .run()
        .assert_ok();
}

/// Test network connectivity under Turmoil's deterministic scheduler.
#[test_log::test]
fn test_sim_network_connectivity() {
    TestConfig::small("connectivity-test", 0xC0EE_3C70_0001)
        .with_max_contracts(2)
        .with_iterations(10)
        .run()
        .assert_ok();
}

// =============================================================================
// CI Simulation Tests
// =============================================================================

/// CI simulation test - small network with contract operations.
#[test_log::test]
fn ci_quick_simulation() {
    TestConfig::small("ci-quick-sim", 0xC1F1_ED5E_ED00)
        .with_nodes(4)
        .with_max_contracts(5)
        .with_iterations(50)
        .with_duration(Duration::from_secs(45))
        .with_sleep(Duration::from_secs(2))
        .run()
        .assert_ok()
        .verify_operation_coverage()
        .check_convergence()
        .verify_state_report();
}

/// CI simulation test - medium network with more operations.
#[test_log::test]
fn ci_medium_simulation() {
    TestConfig::medium("ci-medium-sim", 0xC1F1_ED7E_ED01)
        .run()
        .assert_ok()
        .verify_operation_coverage()
        .check_convergence()
        .verify_state_report();
}

// =============================================================================
// MockWasmRuntime Tests
// =============================================================================

/// Simulation using MockWasmRuntime — exercises the production ContractExecutor code path
/// (init_tracker, validation, notification pipeline, corrupted state recovery) without WASM.
#[test_log::test]
fn mock_wasm_runtime_simulation() {
    TestConfig::small("mock-wasm-sim", 0xA0C1_1A5E_0001)
        .with_mock_wasm()
        .run_direct()
        .assert_ok()
        .verify_state_report();
}

// =============================================================================
// Convergence Tests
// =============================================================================

/// Replica validation test with stepwise consistency checking.
/// Verifies that contracts converge to the same state across all replicas.
#[test_log::test]
fn replica_validation_and_stepwise_consistency() {
    TestConfig::medium("replica-validation", 0xBEE1_1CA5_0001)
        .with_gateways(2)
        .with_nodes(8)
        .with_htl(8, 4)
        .with_connections(4, 8)
        .with_max_contracts(10)
        .with_iterations(90)
        .with_duration(Duration::from_secs(180))
        .with_sleep(Duration::from_secs(5))
        .require_convergence()
        .run()
        .assert_ok()
        .verify_operation_coverage()
        .check_convergence()
        .verify_state_report();
}

/// Dense network test with high connectivity.
/// Tests contract replication and convergence in a densely connected network.
#[test_log::test]
fn dense_network_replication() {
    TestConfig::large("dense-network", 0xDE05_E0F0_0001)
        .require_convergence()
        .run()
        .assert_ok()
        .verify_operation_coverage()
        .check_convergence()
        .verify_state_report();
}

// =============================================================================
// Determinism Verification Tests
// =============================================================================

/// Verify that running the same simulation twice produces identical results.
///
/// Process isolation via nextest eliminates inter-test DashMap state leakage (#3051).
#[test_log::test]
fn test_turmoil_determinism_verification() {
    const SEED: u64 = 0xDE7E_2A11_0001;

    fn run_simulation(name: &'static str, seed: u64) -> Vec<String> {
        setup_deterministic_state(seed);
        let rt = create_runtime();

        let (sim, logs_handle) = rt.block_on(async {
            let sim = SimNetwork::new(name, 1, 3, 7, 3, 10, 2, seed).await;
            let logs_handle = sim.event_logs_handle();
            (sim, logs_handle)
        });

        let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
            seed,
            3,
            15,
            Duration::from_secs(20),
            Duration::from_millis(200),
            || async {
                tokio::time::sleep(Duration::from_secs(1)).await;
                Ok(())
            },
        );

        assert!(result.is_ok(), "Simulation failed: {:?}", result.err());

        rt.block_on(async {
            let logs = logs_handle.lock().await;
            logs.iter()
                .map(|log| format!("{:?}", log.kind.variant_name()))
                .collect()
        })
    }

    let events1 = run_simulation("det-verify-1", SEED);
    let events2 = run_simulation("det-verify-2", SEED);

    assert_eq!(
        events1.len(),
        events2.len(),
        "Event counts differ: {} vs {}",
        events1.len(),
        events2.len()
    );

    for (i, (e1, e2)) in events1.iter().zip(events2.iter()).enumerate() {
        assert_eq!(e1, e2, "Event {} differs: {:?} vs {:?}", i, e1, e2);
    }

    // Fingerprint verification
    let counts1: HashMap<String, usize> = {
        let mut m = HashMap::new();
        for e in &events1 {
            *m.entry(e.clone()).or_insert(0) += 1;
        }
        m
    };
    let counts2: HashMap<String, usize> = {
        let mut m = HashMap::new();
        for e in &events2 {
            *m.entry(e.clone()).or_insert(0) += 1;
        }
        m
    };
    let fp1 = TraceFingerprint::from_events(&events1, &counts1);
    let fp2 = TraceFingerprint::from_events(&events2, &counts2);
    assert_eq!(fp1, fp2, "Fingerprint mismatch between run 1 and run 2");

    tracing::info!(
        "Determinism verified: {} identical events, fingerprint={:#018x}",
        events1.len(),
        fp1.sequence_hash
    );
}

// =============================================================================
// Graceful Shutdown Regression Tests
// =============================================================================

/// Regression test for graceful shutdown using Turmoil deterministic simulation.
#[test_log::test]
fn test_graceful_shutdown_no_deadlock() {
    const SEED: u64 = 0xDEAD_BEEF_CAFE;

    let rt = create_runtime();

    let sim = rt.block_on(async {
        SimNetwork::new("graceful-shutdown-test", 1, 2, 7, 3, 10, 2, SEED).await
    });

    let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
        SEED,
        10,
        3,
        Duration::from_secs(30),
        Duration::from_millis(200),
        || async {
            tokio::time::sleep(Duration::from_secs(5)).await;
            tracing::info!("Test client: simulation will end gracefully");
            Ok(())
        },
    );

    assert!(
        result.is_ok(),
        "Simulation should complete without deadlock: {:?}",
        result.err()
    );

    tracing::info!("Graceful shutdown test passed - no deadlock under Turmoil");
}

/// Tests that the typed EventLoopExitReason error is properly handled.
#[test_log::test]
fn test_graceful_shutdown_typed_error() {
    use freenet::EventLoopExitReason;

    let graceful = EventLoopExitReason::GracefulShutdown;
    let unexpected = EventLoopExitReason::UnexpectedStreamEnd;

    assert_eq!(graceful.to_string(), "Graceful shutdown");
    assert_eq!(
        unexpected.to_string(),
        "Network event stream ended unexpectedly"
    );

    tracing::info!("Typed error test passed");
}

// =============================================================================
// High-Latency Regression Test
// =============================================================================

/// Regression test for v0.1.92 timeout storm bug with high-latency connections.
///
/// In production, we observed 935 timeouts in 10 seconds when transferring to peers
/// with ~150ms RTT. The root cause was MIN_RTO (200ms) < RTT + ACK_CHECK_INTERVAL (244ms).
///
/// This test verifies that:
/// 1. High-latency connections (150-200ms) don't experience excessive timeouts
/// 2. Data transfers complete successfully despite latency
/// 3. BBR congestion control handles high-latency correctly after the fix
///
/// Fix applied in this codebase:
/// - MIN_RTO increased from 200ms to 300ms
/// - BBR now uses adaptive timeout floor based on max BDP seen
#[test_log::test]
fn test_high_latency_timeout_regression() {
    use freenet::simulation::FaultConfig;

    // Use a fixed seed for reproducibility
    const SEED: u64 = 0xDEAD_BEEF_1234;
    tracing::info!(
        "Starting high-latency regression test with seed: 0x{:X}",
        SEED
    );

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    rt.block_on(async {
        // Create a network with 1 gateway and 3 peers
        let mut sim = SimNetwork::new(
            "high-latency-regression",
            1,  // gateways
            3,  // nodes
            7,  // ring_max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;

        // Configure high latency (150-200ms) - similar to intercontinental connections
        // This latency, combined with ACK_CHECK_INTERVAL (100ms), would have caused
        // spurious timeouts with the old MIN_RTO of 200ms
        let fault_config = FaultConfig::builder()
            .latency_range(Duration::from_millis(150)..Duration::from_millis(200))
            .build();
        sim.with_fault_injection(fault_config);

        sim.with_start_backoff(Duration::from_millis(100));

        // Start the network
        let _handles = sim
            .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 1, 1)
            .await;

        // Give time for initial connections with high latency
        // (needs more time than normal due to latency)
        for _ in 0..50 {
            sim.advance_time(Duration::from_millis(100));
            tokio::task::yield_now().await;
        }

        // Check connectivity with a longer timeout due to high latency
        match sim
            .check_partial_connectivity(Duration::from_secs(60), 0.5)
            .await
        {
            Ok(()) => {
                tracing::info!("High-latency network established connectivity");
            }
            Err(e) => {
                tracing::warn!(
                    "High-latency connectivity check incomplete: {} (this may be acceptable)",
                    e
                );
            }
        }

        // The key test: verify we don't have excessive timeouts
        // With the old MIN_RTO=200ms, we'd see hundreds of timeouts
        // With the fix (MIN_RTO=300ms + adaptive floor), timeouts should be minimal
        let summary = sim.get_operation_summary().await;
        let total_timeouts = summary.timeouts;

        tracing::info!(
            "High-latency test results: {} total timeouts, {:.1}% success rate",
            total_timeouts,
            summary.overall_success_rate() * 100.0
        );

        // Before the fix, we'd see 100+ timeouts per second
        // After the fix, timeouts should be rare (mostly actual network issues)
        // Allow up to 50 timeouts for a 60-second test (less than 1/second)
        assert!(
            total_timeouts < 50,
            "Expected <50 timeouts with MIN_RTO=300ms fix, got {} (timeout storm detected!)",
            total_timeouts
        );

        tracing::info!("High-latency regression test PASSED - no timeout storm detected");
    });
}

// =============================================================================
// SimulationSocket Integration Test
// =============================================================================

/// Test: Use actual SimulationSocket inside Turmoil.
/// This verifies that the real in-memory socket works with Turmoil's deterministic scheduler.
#[test]
fn test_turmoil_with_real_simulation_socket() -> turmoil::Result {
    // Clean up any previous socket state
    clear_all_socket_registries();

    let network_name = "turmoil-test";
    let virtual_time = VirtualTime::new();

    // Register the network's time source
    register_network_time_source(network_name, virtual_time);

    let server_addr: SocketAddr = (Ipv6Addr::LOCALHOST, 18000).into();
    let client_addr: SocketAddr = (Ipv6Addr::LOCALHOST, 19000).into();

    // Register addresses with the network
    register_address_network(server_addr, network_name);
    register_address_network(client_addr, network_name);

    let mut sim = turmoil::Builder::new()
        .simulation_duration(Duration::from_secs(10))
        .rng_seed(0xDEAD_BEEF_CAFE_1234)
        .build();

    // Server host using SimulationSocket
    sim.host("server", move || async move {
        let socket = SimulationSocket::bind(server_addr)
            .await
            .expect("Server bind failed");

        tracing::info!("Server bound to {:?}", server_addr);

        // Wait for a packet
        let mut buf = [0u8; 1024];
        match socket.recv_from(&mut buf).await {
            Ok((len, from)) => {
                let msg = &buf[..len];
                tracing::info!("Server received {:?} from {:?}", msg, from);

                // Echo back
                socket.send_to(b"pong", from).await.ok();
            }
            Err(e) => {
                tracing::error!("Server recv error: {:?}", e);
            }
        }

        Ok(())
    });

    // Client host using SimulationSocket
    sim.client("client", async move {
        // Give server time to bind
        tokio::time::sleep(Duration::from_millis(100)).await;

        let socket = SimulationSocket::bind(client_addr)
            .await
            .expect("Client bind failed");

        tracing::info!("Client bound to {:?}", client_addr);

        // Send ping to server
        socket.send_to(b"ping", server_addr).await.ok();
        tracing::info!("Client sent ping");

        // Wait for response
        let mut buf = [0u8; 1024];
        match tokio::time::timeout(Duration::from_secs(5), socket.recv_from(&mut buf)).await {
            Ok(Ok((len, from))) => {
                let msg = &buf[..len];
                tracing::info!("Client received {:?} from {:?}", msg, from);
                assert_eq!(msg, b"pong");
                assert_eq!(from, server_addr);
            }
            Ok(Err(e)) => {
                tracing::error!("Client recv error: {:?}", e);
            }
            Err(_) => {
                tracing::error!("Client recv timeout");
            }
        }

        Ok(())
    });

    sim.run()
}

// =============================================================================
// Subscription Topology Validation
// =============================================================================
// These tests validate the subscription topology infrastructure to detect
// issues like bidirectional cycles (#2720), orphan hosters (#2719), etc.

/// Source detection threshold: peers within 5% of ring distance to contract
/// are considered sources. Must match SOURCE_THRESHOLD in topology_registry.rs.
const SOURCE_THRESHOLD: f64 = 0.05;

/// Network name for single hoster topology test.
const SINGLE_HOSTER_NETWORK: &str = "single-hoster-test";

/// Calculate ring distance (handles wrap-around at 0/1 boundary).
fn ring_distance(a: f64, b: f64) -> f64 {
    let diff = (a - b).abs();
    diff.min(1.0 - diff)
}

/// Test topology capture with a single hoster (gateway only).
///
/// This test validates the topology infrastructure without triggering the
/// bidirectional cycle issue (#2720) by using only a single subscriber.
///
/// ## Scenario
/// 1. Gateway PUTs a contract with subscribe=true
/// 2. Wait for topology registration
/// 3. Verify: gateway's location is within SOURCE_THRESHOLD of contract location
/// 4. Verify: gateway's snapshot shows it's hosting the contract
/// 5. Verify: no topology issues (no cycles since only one subscriber)
///
/// ## Related
/// - For Issue #2720 (bidirectional cycles), that bug is now fixed
/// - For Issue #2755 (orphan/disconnected hosters), see `test_orphan_hosters_no_source` (ignored)
#[test_log::test]
fn test_topology_single_hoster() {
    use freenet::dev_tool::{
        Location, NodeLabel, ScheduledOperation, SimOperation, validate_topology_from_snapshots,
    };

    const SEED: u64 = 0x5EED_0001_CAFE;

    // Set up deterministic state BEFORE creating SimNetwork
    // This ensures peer locations are deterministic (uses GlobalRng)
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    // Create SimNetwork and get gateway location
    let (sim, gateway_location) = rt.block_on(async {
        let sim = SimNetwork::new(
            SINGLE_HOSTER_NETWORK,
            1,  // 1 gateway
            2,  // 2 peers (they won't subscribe)
            7,  // max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        // Get gateway location (first peer in the list)
        let locations = sim.get_peer_locations();
        let gateway_loc = locations
            .first()
            .copied()
            .expect("Should have at least one gateway");
        (sim, gateway_loc)
    });

    // Note: get_peer_locations() returns locations from SimNetwork config, but actual
    // running nodes may have different locations due to how ConnectionManager is initialized.
    // We use a fixed contract seed and validate based on actual topology snapshot locations.
    // See: https://github.com/freenet/freenet-core/issues/2759 for the peer location non-determinism issue.
    let _ = gateway_location; // Silence unused variable warning

    // Use a fixed contract seed - we'll determine source status from actual topology
    let contract_seed = 42u8;
    let contract = SimOperation::create_test_contract(contract_seed);
    let contract_id = *contract.key().id();
    let initial_state = SimOperation::create_test_state(contract_seed);

    // Schedule only ONE operation: gateway PUTs the contract with subscribe=true
    // No other nodes subscribe - this avoids #2720 bidirectional cycle issue
    let operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(SINGLE_HOSTER_NETWORK, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: initial_state.clone(),
            subscribe: true,
        },
    )];

    // Run simulation with controlled events under Turmoil
    // Wait 45 seconds after operations for:
    // - Topology registration (runs periodically)
    // - Orphan recovery (runs every 30 seconds)
    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120), // simulation duration
        Duration::from_secs(45),  // post-operation wait for topology stabilization
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete successfully: {:?}",
        result.turmoil_result.err()
    );

    // Get topology snapshots from the returned result (captured before SimNetwork::Drop clears registry)
    let snapshots = result.topology_snapshots;
    tracing::info!("Captured {} topology snapshots", snapshots.len());

    // Find the gateway's snapshot with the contract
    let gateway_with_contract = snapshots
        .iter()
        .find(|snap| snap.contracts.contains_key(&contract_id));

    assert!(
        gateway_with_contract.is_some(),
        "Gateway should have the contract in its topology snapshot. \
         Available snapshots: {} peers",
        snapshots.len()
    );

    let gateway_snap = gateway_with_contract.unwrap();
    let contract_sub = gateway_snap.contracts.get(&contract_id).unwrap();

    // Get the actual contract location derived from the contract ID
    let contract_location = Location::from(&contract_id).as_f64();
    let gateway_location = gateway_snap.location;

    // CRITICAL: Validate that gateway is within SOURCE_THRESHOLD of contract location
    let gateway_distance = ring_distance(gateway_location, contract_location);
    tracing::info!(
        "Contract location: {:.4}, Gateway location: {:.4}, Distance: {:.4}, Threshold: {:.4}",
        contract_location,
        gateway_location,
        gateway_distance,
        SOURCE_THRESHOLD
    );

    // Gateway should be hosting (it did PUT with subscribe=true)
    assert!(
        contract_sub.is_hosting,
        "Gateway should be hosting the contract after PUT with subscribe=true"
    );

    tracing::info!(
        "Gateway {} is hosting contract: upstream={:?}, downstream={:?}",
        gateway_snap.peer_addr,
        contract_sub.upstream,
        contract_sub.downstream
    );

    // Validate topology using captured snapshots
    let result = validate_topology_from_snapshots(&snapshots, &contract_id, contract_location);

    tracing::info!(
        "Topology validation: cycles={}, orphans={}, disconnected={}, unreachable={}, proximity={}",
        result.bidirectional_cycles.len(),
        result.orphan_hosters.len(),
        result.disconnected_upstream.len(),
        result.unreachable_hosters.len(),
        result.proximity_violations.len()
    );

    // CRITICAL: With only one subscriber, there should NEVER be cycles
    // This is the primary assertion - single hoster cannot form cycles
    assert!(
        result.bidirectional_cycles.is_empty(),
        "Single hoster should have no bidirectional cycles, found: {:?}",
        result.bidirectional_cycles
    );

    // Check if gateway is considered a "source" based on actual topology snapshot
    let gateway_is_source = gateway_distance < SOURCE_THRESHOLD;

    if gateway_is_source {
        tracing::info!(
            "Gateway IS source (distance={:.4} < threshold={:.4}) - validating healthy topology",
            gateway_distance,
            SOURCE_THRESHOLD
        );

        // Source hoster should have healthy topology
        assert!(
            result.orphan_hosters.is_empty(),
            "Source hoster (within threshold) should not be marked as orphan, found: {:?}",
            result.orphan_hosters
        );

        assert!(
            result.disconnected_upstream.is_empty(),
            "Source hoster (within threshold) should not be disconnected, found: {:?}",
            result.disconnected_upstream
        );

        assert!(
            result.unreachable_hosters.is_empty(),
            "Source hoster should have no unreachable hosters, found: {:?}",
            result.unreachable_hosters
        );

        assert!(
            result.is_healthy(),
            "Single source hoster topology should be healthy, got {} issues",
            result.issue_count
        );
    } else {
        tracing::warn!(
            "Gateway is NOT source (distance={:.4} >= threshold={:.4}) - orphan/disconnected issues are expected",
            gateway_distance,
            SOURCE_THRESHOLD
        );

        // Non-source hoster will have orphan/disconnected issues - this is expected behavior
        // The key assertion (no cycles) already passed above
        if !result.orphan_hosters.is_empty() {
            tracing::info!(
                "Expected: Gateway flagged as orphan (not a source, no upstream): {:?}",
                result.orphan_hosters
            );
        }
        if !result.disconnected_upstream.is_empty() {
            tracing::info!(
                "Expected: Gateway flagged as disconnected upstream (not a source, has downstream): {:?}",
                result.disconnected_upstream
            );
        }
    }

    tracing::info!(
        "Single hoster topology test passed - gateway_is_source={}, cycles=0, other_issues={}",
        gateway_is_source,
        result.issue_count
    );
}

// =============================================================================
// Concurrent Updates Convergence Test
// =============================================================================

/// Tests that concurrent updates from multiple peers converge to the same state.
///
/// This test validates the core eventual consistency property of Freenet's
/// state synchronization:
///
/// 1. Multiple peers subscribe to the same contract
/// 2. Multiple peers issue concurrent updates
/// 3. Updates propagate via broadcast mechanism
/// 4. All subscribers MUST converge to identical state
///
/// ## What This Catches
///
/// - **Echo-back bugs**: If A's update incorrectly comes back to A
/// - **Missed broadcasts**: If some peer doesn't receive propagated updates
/// - **CRDT merge bugs**: If deterministic merge produces different results
/// - **Summary caching bugs**: If summary comparison incorrectly skips peers
/// - **Race conditions**: In update propagation and application
///
/// ## Related Issues
///
/// - Issue #2764: Echo-back prevention (summary comparison should handle this)
/// - PR #2763: Conditional summary caching
///
/// ## CI Impact
///
/// Configuration: 2 gateways + 6 nodes with few contracts and many iterations
/// to maximize concurrent update scenarios per contract.
///
/// Expected runtime: ~60-90 seconds (similar to ci_medium_simulation).
#[test_log::test]
fn test_concurrent_updates_convergence() {
    // Use a specific seed for reproducibility
    // This seed was chosen to produce good coverage of concurrent update scenarios
    TestConfig::medium("concurrent-updates-convergence", 0xC0C0_BEEF_1234)
        .with_gateways(2) // Multiple gateways for richer topology
        .with_nodes(6) // 6 regular nodes for concurrent updates
        .with_max_contracts(3) // Few contracts = more updates per contract
        .with_iterations(80) // Many iterations to stress concurrent updates
        .with_duration(Duration::from_secs(90))
        .with_sleep(Duration::from_secs(2)) // Short sleep between ops for concurrency
        .require_convergence() // FAIL if any contract diverges
        .run()
        .assert_ok()
        .verify_operation_coverage()
        .check_convergence()
        .verify_state_report();
}

// =============================================================================
// Full State Send Caching Bug Test (Issue #2763)
// =============================================================================

/// Tests correct behavior of summary caching after full state sends.
///
/// ## Background (PR #2763)
///
/// Before the fix, when a peer sent full state (not a delta) to another peer,
/// it would incorrectly cache its own summary as the recipient's summary.
/// This caused problems because:
///
/// 1. After receiving full state, the recipient's state depends on CRDT merge
///    with their existing state - we can't predict their resulting summary
/// 2. Later broadcasts would compute deltas based on the wrong cached summary
/// 3. The recipient couldn't apply these deltas, triggering ResyncRequests
/// 4. This caused inefficiency (extra round trips) and potential divergence in race conditions
///
/// ## What This Test Does
///
/// Creates a scenario that exercises the full state send path:
///
/// 1. Gateway PUTs contract with state S1, subscribes
/// 2. Nodes subscribe → receive full state (gateway has no cached summaries)
/// 3. Multiple nodes issue UPDATEs → broadcasts to subscribers
/// 4. Wait for propagation and CRDT merges
/// 5. Assert all peers converge to the same state
/// 6. Assert no ResyncRequests were needed (via GlobalTestMetrics)
///
/// ## Test Infrastructure
///
/// This test uses `GlobalTestMetrics` to track behavior across all nodes:
/// - `GlobalTestMetrics::reset()` at test start
/// - `GlobalTestMetrics::resync_requests()` - should be 0 with the fix
/// - `GlobalTestMetrics::delta_sends()` / `full_state_sends()` - broadcast statistics
///
/// MockRuntime uses hash-based summaries (32 bytes) which enables the delta
/// efficiency check to pass (for states > 64 bytes). However, it returns full
/// state as "delta" content, so the actual CRDT merge behavior is:
/// - Compare incoming state hash with current state hash
/// - Larger hash wins (deterministic CRDT convergence)
///
/// ## Current Limitations
///
/// While MockRuntime implements CRDT-style "largest hash wins" merging, it doesn't
/// fully reproduce the bug because:
/// 1. The "delta" is actually full state, so it works regardless of base state
/// 2. Even with wrong cached summary, the merge produces correct results
/// 3. No ResyncRequests are triggered because delta application never truly fails
///
/// The bug would fully manifest with a real CRDT contract where:
/// - Delta is computed from assumed base state
/// - Applying delta to wrong base state produces incorrect result
/// - This triggers ResyncRequest or causes state divergence
///
/// The test still provides value as:
/// 1. Documentation of the expected behavior and fix
/// 2. Infrastructure for tracking broadcasts (delta vs full state)
/// 3. Verification that convergence works correctly
/// 4. Regression guard if MockRuntime or protocol logic changes
///
/// ## Related
///
/// - PR #2763: Conditional summary caching to prevent state divergence
/// - Issue #2764: Echo-back prevention
#[test_log::test]
fn test_full_state_send_no_incorrect_caching() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0x2763_CAFE_0001;
    const NETWORK_NAME: &str = "full-state-cache-test";

    // Reset global test metrics at the start
    GlobalTestMetrics::reset();

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    // Create network: 1 gateway + 3 nodes
    // This gives us enough peers to have interesting broadcast patterns
    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,  // 1 gateway
            3,  // 3 regular nodes
            7,  // max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Create a test contract
    let contract = SimOperation::create_test_contract(0x63);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();

    // Create distinct states for each update
    // Using different byte patterns to ensure CRDT merges produce distinct results
    let initial_state = SimOperation::create_test_state(1);
    let update_state_2 = SimOperation::create_test_state(2);
    let update_state_3 = SimOperation::create_test_state(3);
    let update_state_4 = SimOperation::create_test_state(4);

    // Schedule operations that will trigger the bug scenario:
    // 1. Gateway PUTs contract (becomes the source)
    // 2. Nodes subscribe one by one (each receives full state, gateway might incorrectly cache)
    // 3. Multiple updates from different nodes (tests delta computation with cached summaries)
    let operations = vec![
        // Step 1: Gateway puts contract with initial state and subscribes
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: initial_state,
                subscribe: true,
            },
        ),
        // Step 2: Node 1 subscribes - will receive full state from gateway
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        // Step 3: Node 2 subscribes - will receive full state from gateway
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 2),
            SimOperation::Subscribe { contract_id },
        ),
        // Step 4: Node 3 subscribes - will receive full state from gateway
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 3),
            SimOperation::Subscribe { contract_id },
        ),
        // Step 5: Gateway updates - broadcasts to all subscribers
        // BUG: If gateway incorrectly cached subscriber summaries after full state sends,
        // it will compute deltas based on wrong base state
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: update_state_2,
            },
        ),
        // Step 6: Node 1 updates - broadcasts to subscribers
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Update {
                key: contract_key,
                data: update_state_3,
            },
        ),
        // Step 7: Node 2 updates - broadcasts to subscribers
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 2),
            SimOperation::Update {
                key: contract_key,
                data: update_state_4,
            },
        ),
    ];

    // Run simulation with enough time for all operations and CRDT merges
    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120), // simulation duration
        Duration::from_secs(60),  // post-operation wait for propagation
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete successfully: {:?}",
        result.turmoil_result.err()
    );

    // Check convergence - this is the key assertion
    // With the fix, all peers should converge to the same state
    // Without the fix, incorrect caching may cause divergence
    let convergence =
        rt.block_on(async { freenet::dev_tool::check_convergence_from_logs(&logs_handle).await });

    tracing::info!(
        "Convergence check: {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    // Log any diverged contracts for debugging
    for diverged in &convergence.diverged {
        tracing::error!(
            "DIVERGED: {} - {} unique states across {} peers",
            diverged.contract_key,
            diverged.unique_state_count(),
            diverged.peer_states.len()
        );
        for (peer, hash) in &diverged.peer_states {
            tracing::error!("  peer {}: {}", peer, hash);
        }
    }

    // PRIMARY ASSERTION: All peers must converge
    // This should pass with the fix (PR #2763) and may fail without it
    assert!(
        convergence.is_converged(),
        "PR #2763 REGRESSION: State divergence detected! \
         This indicates incorrect summary caching after full state sends. \
         {} contracts converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    // SECONDARY ASSERTION: No ResyncRequests should be needed
    // With correct summary caching (PR #2763), deltas should work correctly
    // Without the fix, peers would fail to apply deltas and send ResyncRequests
    let resync_count = GlobalTestMetrics::resync_requests();
    let delta_sends = GlobalTestMetrics::delta_sends();
    let full_state_sends = GlobalTestMetrics::full_state_sends();

    tracing::info!(
        "Broadcast stats - delta_sends: {}, full_state_sends: {}, resync_requests: {}",
        delta_sends,
        full_state_sends,
        resync_count
    );

    // Note: Some resyncs may occur during normal operation (e.g., initial state sync),
    // but excessive resyncs indicate the caching bug. We check for zero resyncs in this
    // controlled scenario where all peers start fresh and updates flow correctly.
    assert_eq!(
        resync_count, 0,
        "PR #2763 REGRESSION: {} ResyncRequests detected! \
         This indicates deltas are failing due to incorrect summary caching. \
         With the fix, no resyncs should be needed in this scenario.",
        resync_count
    );

    // TERTIARY ASSERTION: Verify broadcast activity
    // With MockRuntime using hash summaries, we expect a mix of delta and full state sends.
    // The specific counts depend on the subscription topology and CRDT merge outcomes.
    // The important thing is that broadcasts ARE happening (contract is propagating).
    let total_broadcasts = delta_sends + full_state_sends;
    assert!(
        total_broadcasts > 0,
        "Expected at least one broadcast to occur during the simulation"
    );

    tracing::info!(
        "test_full_state_send_no_incorrect_caching PASSED: \
         {} peers converged, {} broadcasts ({} delta, {} full state), {} resyncs",
        convergence
            .converged
            .iter()
            .map(|c| c.replica_count)
            .sum::<usize>(),
        total_broadcasts,
        delta_sends,
        full_state_sends,
        resync_count
    );
}

// =============================================================================
// Task-per-tx Subscribe ForwardingAck Regression Test (PR #3806)
// =============================================================================

/// Exercises the task-per-tx client-initiated subscribe path
/// (`subscribe_with_id` → `start_client_subscribe`) in a multi-node
/// simulation with network-routed subscribes.
///
/// ## Background (PR #3806, #1454 Phase 2b)
///
/// Phase 2b migrated client-initiated SUBSCRIBE to a task-per-tx driver.
/// This test verifies the driver works end-to-end with network round-trips,
/// retries, and peer selection.
///
/// ## ForwardingAck coverage
///
/// The ForwardingAck relay bug (commit 5cb6f37c) is covered by unit tests
/// in `node.rs::callback_forward_tests`. Triggering it in simulation
/// requires `SeedContract` + `use_mock_wasm=true` so that relay peers
/// lack the contract. The `SeedContract` framework is in place but the
/// mock WASM runtime has compatibility issues with `OpCtx::send_and_await`
/// (separate from the local-completion fix below).
///
/// The task-per-tx `send_and_await` path IS exercised here: subscribes
/// complete with `outcome="subscribed"` via the local-completion synthesis
/// in the SUBSCRIBE branch of `handle_pure_network_message_v1`.
#[test_log::test]
fn test_subscribe_forwarding_ack_relay() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0x5CB6_F37C_0001;
    const NETWORK_NAME: &str = "subscribe-fwd-ack";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1, // 1 gateway
            4, // 4 regular nodes
            7, // ring_max_htl
            3, // rnd_if_htl_above
            5, // max_connections
            2, // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xAC);
    let contract_id = *contract.key().id();
    let initial_state = SimOperation::create_test_state(1);

    // Gateway PUTs, then multiple nodes subscribe via the task-per-tx path.
    //
    // Note: SeedContract (no-propagation local store) is available for
    // tests that need to control which nodes have the contract, but
    // requires use_mock_wasm=true which has compatibility issues with
    // OpCtx::send_and_await. For now, use regular PUT + Subscribe.
    let operations = vec![
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: initial_state,
                subscribe: true,
            },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 2),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 3),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 4),
            SimOperation::Subscribe { contract_id },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120),
        Duration::from_secs(30),
    );

    // PRIMARY: simulation must complete without errors.
    // Before the fix, ForwardingAck on the task-per-tx channel
    // would cause UnexpectedOpState, crashing the simulation.
    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed (ForwardingAck regression?): {:?}",
        result.turmoil_result.err()
    );

    // Verify subscribes succeeded.
    let (success_count, not_found_count) = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let mut successes = 0usize;
        let mut not_found = 0usize;
        for log in logs.iter() {
            match log.kind.subscribe_outcome() {
                Some(true) => successes += 1,
                Some(false) => not_found += 1,
                None => {}
            }
        }
        (successes, not_found)
    });

    tracing::info!(
        success_count,
        not_found_count,
        "Subscribe telemetry summary"
    );

    // 4 explicit subscribes + 1 from gateway's put-with-subscribe.
    assert!(
        success_count >= 4,
        "Expected at least 4 successful subscribes, got {} (not_found={})",
        success_count,
        not_found_count,
    );
}

/// Soundness check for the fix that moved `ring.subscribe()` /
/// `record_subscription()` / `announce_contract_hosted()` out of the shared
/// prefix of `SubscribeMsgResult::Subscribed` and into the originator-only
/// branch.
///
/// The *direct* regression is covered by the unit test
/// `ring::hosting::tests::test_relay_downstream_only_not_in_renewal`, which
/// asserts at the HostingManager level that a pure-relay state (downstream
/// subscriber registered, no self-lease) does not get recruited into
/// `contracts_needing_renewal()`. That unit test is where the bug bite is
/// pinned.
///
/// This simulation test verifies the broader property that subscribes still
/// complete successfully under the refactored handler and that the bounded
/// number of active-subscription-lease holders stays sane under a multi-peer
/// subscribe scenario — any regression that re-introduced relay-side
/// `ring.subscribe` calls on nodes running the LEGACY process_message path
/// would cause `subscribed_peers` to grow beyond the expected count of
/// genuine originators.
///
/// Note on turmoil vs production: in the turmoil simulation runner every
/// peer shares one process, which means `pending_op_results` is effectively
/// global. `node::try_forward_driver_reply` on an intermediate peer
/// can therefore find the originator's task entry and short-circuit the
/// Response before it reaches the legacy relay handler. In production each
/// peer is an independent process with its own `pending_op_results`, so
/// relays fall through to the legacy handler and hit the bug. This is why
/// the *primary* regression test for the bug is the in-process hosting
/// manager unit test rather than this simulation test.
///
/// Simulation-side regression coverage for the bug tracked in #3874: when a
/// subscribed client disconnects, the node MUST emit an upstream Unsubscribe
/// so the hoster can drop the downstream subscriber registration.
///
/// This test exercises the same disconnect code path as the integration test
/// `test_client_disconnect_does_not_immediately_unsubscribe_with_recent_read` — the
/// `ClientRequest::Disconnect` arm in `client_events::process_open_request`,
/// which calls `remove_client_from_all_subscriptions` /
/// `should_unsubscribe_upstream` / `send_unsubscribe_upstream` — but through
/// the simulation harness where the bug's preconditions (completed PUT
/// replay from GC, subscription resurrection, etc.) can be controlled and
/// reproduced deterministically. Note the two tests now assert OPPOSITE outcomes
/// after the P6 reconcile flip (#4642): this sim node SUBSCRIBEs without a recent
/// GET, so `contract_in_use` is false on disconnect and the collapse fires (an
/// Unsubscribe is emitted); the integration test does a recent GET first, so its
/// collapse is demand-gated and deferred to lease-lapse (see #4738).
///
/// Before this test existed, `SimOperation` had no client-disconnect variant
/// and simulation had NO coverage of the disconnect → unsubscribe chain —
/// see #3874 for the full gap analysis. The integration-test regression
/// that came out of PR #3843 was therefore invisible to the simulation
/// suite.
///
/// This test currently FAILS and reproduces a concrete regression that
/// was invisible to the simulation suite before: the task-per-tx
/// subscribe driver in `operations/subscribe/op_ctx_task.rs` does NOT
/// call `interest_manager.register_peer_interest(..., is_upstream=true)`
/// on subscribe completion, so `send_unsubscribe_upstream` logs
/// `"No upstream peer found for unsubscribe"` and no Unsubscribe is
/// ever emitted. The legacy `operations/subscribe.rs::SubscribeMsg::Response`
/// arm DOES register the upstream peer (see subscribe.rs:1767), so the
/// behaviour regressed when client-initiated SUBSCRIBE migrated to the
/// task-per-tx driver in Phase 2b.
///
/// This is either the #3874 root cause or a closely-related bug on the
/// same code path. Marked `#[ignore]` until the missing registration
/// is restored; the test is kept in-tree as executable documentation
/// of the gap and as a regression guard for the fix.
#[test_log::test]
fn test_client_disconnect_emits_upstream_unsubscribe() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0x5CB6_F37C_0003;
    const NETWORK_NAME: &str = "client-disconnect-unsub";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1, // 1 gateway
            4, // 4 regular nodes (matching subscribe_forwarding_ack_relay)
            7, // ring_max_htl
            3, // rnd_if_htl_above
            5, // max_connections
            2, // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xDC);
    let contract_id = *contract.key().id();
    let initial_state = SimOperation::create_test_state(1);

    // Gateway PUTs → node-1 subscribes → node-1 disconnects.
    // Node-1 is a pure downstream subscriber with no further downstream
    // of its own, so `should_unsubscribe_upstream` must return true
    // on disconnect and an Unsubscribe must be emitted to its upstream.
    let operations = vec![
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: initial_state,
                subscribe: false,
            },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(NodeLabel::node(NETWORK_NAME, 1), SimOperation::Disconnect),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120),
        Duration::from_secs(30),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    let (sent_count, received_count) = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let mut sent = 0usize;
        let mut received = 0usize;
        for log in logs.iter() {
            if log
                .kind
                .unsubscribe_sent_instance_id()
                .is_some_and(|id| *id == contract_id)
            {
                sent += 1;
            }
            if log
                .kind
                .unsubscribe_received_instance_id()
                .is_some_and(|id| *id == contract_id)
            {
                received += 1;
            }
        }
        (sent, received)
    });

    tracing::info!(
        sent_count,
        received_count,
        "Unsubscribe telemetry after client disconnect"
    );

    assert!(
        sent_count > 0,
        "Disconnecting node MUST emit at least one UnsubscribeSent for the \
         contract it was subscribed to (sent={sent_count}, received={received_count})"
    );
    assert!(
        received_count > 0,
        "Upstream node MUST log UnsubscribeReceived after client disconnect \
         (sent={sent_count}, received={received_count})"
    );
}

/// Edge case: disconnect from a node that never subscribed. No Unsubscribe
/// should be emitted and nothing should log "No upstream peer found".
#[test_log::test]
fn test_client_disconnect_without_subscriptions_is_noop() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0x5CB6_F37C_0004;
    const NETWORK_NAME: &str = "disconnect-no-subs";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 1, 4, 7, 3, 5, 2, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let operations = vec![ScheduledOperation::new(
        NodeLabel::node(NETWORK_NAME, 1),
        SimOperation::Disconnect,
    )];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(60),
        Duration::from_secs(30),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    let sent_count = rt.block_on(async {
        let logs = logs_handle.lock().await;
        logs.iter()
            .filter(|log| log.kind.unsubscribe_sent_instance_id().is_some())
            .count()
    });

    assert_eq!(
        sent_count, 0,
        "Disconnecting a client with no subscriptions must not emit any \
         UnsubscribeSent events (got {sent_count})"
    );
}

/// Edge case: subscribe twice from the same client, then disconnect.
/// The upstream registration is idempotent, so only one Unsubscribe should
/// be emitted per contract.
#[test_log::test]
fn test_repeat_subscribe_then_disconnect_emits_single_unsubscribe() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0x5CB6_F37C_0005;
    const NETWORK_NAME: &str = "repeat-subscribe-disconnect";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 1, 4, 7, 3, 5, 2, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xDD);
    let contract_id = *contract.key().id();
    let initial_state = SimOperation::create_test_state(1);

    let operations = vec![
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: initial_state,
                subscribe: false,
            },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(NodeLabel::node(NETWORK_NAME, 1), SimOperation::Disconnect),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120),
        Duration::from_secs(30),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    let sent_count = rt.block_on(async {
        let logs = logs_handle.lock().await;
        logs.iter()
            .filter(|log| {
                log.kind
                    .unsubscribe_sent_instance_id()
                    .is_some_and(|id| *id == contract_id)
            })
            .count()
    });

    assert_eq!(
        sent_count, 1,
        "Repeat-subscribe + disconnect must emit exactly one UnsubscribeSent \
         (got {sent_count})"
    );
}

#[test_log::test]
fn test_relay_does_not_pollute_active_subscriptions() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0x5CB6_F37C_0002;
    const NETWORK_NAME: &str = "subscribe-relay-pollution";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);
    let rt = create_runtime();

    // Sparse topology: more peers than max_connections so the ring can't
    // be fully connected and subscribe requests MUST route through at least
    // one relay to reach the peer that holds the contract.
    let sim = rt.block_on(async {
        SimNetwork::new(
            NETWORK_NAME,
            2,  // 2 gateways
            18, // 18 regular nodes (20 total peers)
            10, // ring_max_htl
            3,  // rnd_if_htl_above
            3,  // max_connections — tight ring, non-full topology
            2,  // min_connections
            SEED,
        )
        .await
    });

    let contract = SimOperation::create_test_contract(0xAD);
    let contract_id = *contract.key().id();
    let initial_state = SimOperation::create_test_state(1);

    // Gateway PUTs (with subscribe). Multiple distant nodes then subscribe
    // in parallel so at least some Subscribe requests must route through
    // relay hops before reaching a peer that holds the contract.
    let operations = vec![
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: initial_state,
                subscribe: true,
            },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 15),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 16),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 17),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 18),
            SimOperation::Subscribe { contract_id },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120),
        Duration::from_secs(30),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // run_controlled_simulation snapshots every peer's HostingManager state
    // at shutdown and returns them on the result. Use those directly rather
    // than the global registry (which is cleared on SimNetwork drop).
    let snapshots = &result.topology_snapshots;
    assert!(
        !snapshots.is_empty(),
        "No topology snapshots captured — background snapshot task didn't run"
    );

    // Count peers whose `HostingManager::active_subscriptions` contains the
    // contract. Only genuine originators of a subscription should appear here:
    //
    //   * The PUT-with-subscribe originator (gateway 0).
    //   * Each of nodes 15..=18 that called Subscribe explicitly.
    //
    // That's at most 5 peers. Before the fix, the
    // `SubscribeMsgResult::Subscribed` handler would ALSO add every relay
    // that forwards a Subscribed response to the set — crucially, when four
    // simultaneous subscribers route through each other, the bug's recruited
    // relays overlap with legit subscribers, but additional strictly-relay
    // peers still accumulate. After the fix the bound collapses to the 5
    // genuine originators.
    let mut subscribed_peers: Vec<SocketAddr> = snapshots
        .iter()
        .filter(|snap| snap.active_subscription_keys.contains(&contract_id))
        .map(|snap| snap.peer_addr)
        .collect();
    subscribed_peers.sort();

    tracing::info!(
        subscribed_peer_count = subscribed_peers.len(),
        ?subscribed_peers,
        total_snapshots = snapshots.len(),
        "Peers with contract in active_subscriptions"
    );

    // Five nodes legitimately install a lease in `active_subscriptions`:
    // the PUT-with-subscribe gateway and the four explicit Subscribe
    // originators (nodes 15..=18). Anything more means a peer that did
    // not originate a subscribe — a pure relay — ended up with the
    // contract in its active_subscriptions, which is the bug.
    assert!(
        subscribed_peers.len() <= 5,
        "Relay pollution regression: {} peer(s) have the contract in \
         `active_subscriptions`, expected at most 5 (gateway + 4 subscribing \
         nodes). Polluted peers: {:?}. \
         See subscribe.rs::SubscribeMsgResult::Subscribed — `ring.subscribe()` \
         must only run on the originator branch, not on relays.",
        subscribed_peers.len(),
        subscribed_peers,
    );

    // Sanity: at least some peer must have actually subscribed.
    assert!(
        !subscribed_peers.is_empty(),
        "No peers have the contract in active_subscriptions — \
         subscribes didn't complete, test is degenerate"
    );
}

// =============================================================================
// CRDT Emulation Mode Test (PR #2763 Bug Reproduction)
// =============================================================================

/// Tests the CRDT emulation mode which can trigger version mismatch ResyncRequests.
///
/// ## Background
///
/// This test uses the CRDT emulation mode which adds version tracking to contract state.
/// When a peer caches the wrong summary (the bug PR #2763 fixed), subsequent delta
/// computation uses the wrong `from_version`. When the receiving peer tries to apply
/// the delta, it fails because versions don't match → triggers ResyncRequest.
///
/// ## Test Scenario
///
/// 1. Register contract for CRDT emulation mode
/// 2. Gateway PUTs contract with version 1
/// 3. Nodes subscribe and receive version 1
/// 4. Gateway updates to version 2, broadcasts delta (from v1 to v2)
/// 5. With correct caching, all nodes update to v2
/// 6. Assert: no ResyncRequests (versions match)
/// 7. Assert: delta sends occur (CRDT mode enables delta computation)
///
/// ## CRDT State Format
///
/// State: [version: u64 LE][64 bytes data]
/// Summary: [version: u64 LE][blake3 hash: 32 bytes]
/// Delta: [from_version: u64][to_version: u64][new data]
#[test_log::test]
fn test_crdt_mode_version_tracking() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    const SEED: u64 = 0x0276_3CD0_0001;
    const NETWORK_NAME: &str = "crdt-version-test";

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    // Create network
    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,  // 1 gateway
            2,  // 2 regular nodes
            7,  // max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Create a test contract and register it for CRDT mode
    let contract = SimOperation::create_test_contract(0xCD);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();

    // Register contract for CRDT emulation BEFORE simulation starts
    register_crdt_contract(contract_id);

    // Create CRDT-formatted states with version numbers
    let state_v1 = SimOperation::create_crdt_state(1, 0x11);
    let state_v2 = SimOperation::create_crdt_state(2, 0x22);

    // Schedule operations
    let operations = vec![
        // Gateway puts contract with version 1
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: state_v1,
                subscribe: true,
            },
        ),
        // Nodes subscribe - will receive version 1
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 2),
            SimOperation::Subscribe { contract_id },
        ),
        // Gateway updates to version 2 - broadcasts delta (v1 → v2)
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: state_v2,
            },
        ),
    ];

    // Run simulation
    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(60),
        Duration::from_secs(30),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    // Check results
    let convergence =
        rt.block_on(async { freenet::dev_tool::check_convergence_from_logs(&logs_handle).await });

    let resync_count = GlobalTestMetrics::resync_requests();
    let delta_sends = GlobalTestMetrics::delta_sends();
    let full_state_sends = GlobalTestMetrics::full_state_sends();

    tracing::info!(
        "CRDT mode test - convergence: {}/{}, delta_sends: {}, full_state_sends: {}, resyncs: {}",
        convergence.converged.len(),
        convergence.total_contracts(),
        delta_sends,
        full_state_sends,
        resync_count
    );

    // With correct summary caching (PR #2763 fix):
    // - Initial subscription sends full state (sent_delta=false, no caching)
    // - Update sends delta (sent_delta=true, summary is cached CORRECTLY)
    // - No version mismatches → no ResyncRequests
    assert!(
        convergence.is_converged(),
        "CRDT mode: all peers should converge. {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    // Note: With the fix in place, delta sends should succeed.
    // ResyncRequests would only occur if summary caching was incorrect.
    tracing::info!(
        "test_crdt_mode_version_tracking PASSED: converged={}, resyncs={}",
        convergence.converged.len(),
        resync_count
    );

    // Clean up CRDT contract registration
}

// =============================================================================
// PR #2763 Bug Reproduction Test - Pre-existing Divergent State
// =============================================================================

/// **PR #2763 Bug Reproduction Test**
///
/// This test validates that CRDT-mode contracts converge correctly even when
/// nodes have divergent states and stale cached summaries.
///
/// ## Background
///
/// PR #2763 fixed a bug where summaries were incorrectly cached after full state
/// sends. This test uses CRDT emulation to verify the version-tracking behavior
/// and ResyncRequest recovery mechanism.
///
/// ## Note on Test Behavior
///
/// In simulation, the InterestMessage::Summaries exchange between peers shares
/// summaries during connection establishment. This means cached summaries may
/// become stale when a node updates independently (without broadcasting).
///
/// The ResyncRequest mechanism is the CORRECT recovery path when delta application
/// fails due to version mismatch. This test verifies:
/// 1. CRDT mode correctly detects version mismatches
/// 2. ResyncRequest is sent when delta fails
/// 3. System converges to correct state after resync
///
/// ## Test Scenario
///
/// 1. Gateway PUTs contract with v1
/// 2. Node 1 subscribes → receives v1
/// 3. Node 1 updates to v5 (Gateway's cached summary becomes stale)
/// 4. Gateway updates to v10 and broadcasts
/// 5. Node 1 receives delta with from_version != current_version → ResyncRequest
/// 6. System recovers via ResyncResponse
#[test_log::test]
fn test_pr2763_crdt_convergence_with_resync() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    const SEED: u64 = 0x0276_3B06_0001;
    const NETWORK_NAME: &str = "pr2763-crdt-resync";

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    // Create network: 1 gateway + 2 nodes
    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,  // 1 gateway
            2,  // 2 regular nodes
            7,  // max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Create a test contract and register it for CRDT mode
    let contract = SimOperation::create_test_contract(0xBB);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();

    // Register contract for CRDT emulation
    register_crdt_contract(contract_id);

    // Create CRDT-formatted states with version numbers
    let state_v1_gw = SimOperation::create_crdt_state(1, 0xAA); // Gateway's initial state (v1)
    let state_v5_node = SimOperation::create_crdt_state(5, 0xBB); // Node 1's update (v5)
    let state_v10_gw = SimOperation::create_crdt_state(10, 0xCC); // Gateway's final state (v10)

    // Schedule operations
    let operations = vec![
        // Step 1: Gateway puts contract with v1
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: state_v1_gw,
                subscribe: true,
            },
        ),
        // Step 2: Node 1 subscribes - receives v1
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        // Step 3: Node 1 updates to v5 - creates version divergence
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Update {
                key: contract_key,
                data: state_v5_node,
            },
        ),
        // Step 4: Node 2 subscribes
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 2),
            SimOperation::Subscribe { contract_id },
        ),
        // Step 5: Gateway updates to v10 - broadcasts to subscribers
        // This triggers version mismatch on Node 1 → ResyncRequest
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: state_v10_gw,
            },
        ),
    ];

    // Run simulation
    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(90),
        Duration::from_secs(45),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    // Check results
    let convergence =
        rt.block_on(async { freenet::dev_tool::check_convergence_from_logs(&logs_handle).await });

    let resync_count = GlobalTestMetrics::resync_requests();
    let delta_sends = GlobalTestMetrics::delta_sends();
    let full_state_sends = GlobalTestMetrics::full_state_sends();

    tracing::info!(
        "CRDT resync test - convergence: {}/{}, delta_sends: {}, full_state_sends: {}, resyncs: {}",
        convergence.converged.len(),
        convergence.total_contracts(),
        delta_sends,
        full_state_sends,
        resync_count
    );

    // Log divergence details if any
    for diverged in &convergence.diverged {
        tracing::warn!(
            "DIVERGED: {} - {} unique states",
            diverged.contract_key,
            diverged.unique_state_count()
        );
        for (peer, hash) in &diverged.peer_states {
            tracing::warn!("  peer {}: {}", peer, hash);
        }
    }

    // Primary assertion: Convergence must succeed
    // The ResyncRequest mechanism should recover from version mismatches
    assert!(
        convergence.is_converged(),
        "CRDT resync test: peers MUST converge via ResyncRequest recovery. {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    // Secondary: ResyncRequests are expected in this scenario because Node 1
    // updated independently, causing stale cached summaries
    if resync_count > 0 {
        tracing::info!(
            "CRDT resync test: {} ResyncRequests occurred (expected for version mismatch recovery)",
            resync_count
        );
    }

    tracing::info!(
        "test_pr2763_crdt_convergence_with_resync PASSED: converged={}, resyncs={}",
        convergence.converged.len(),
        resync_count
    );

    // Clean up
}

/// Regression test for the #4233 full-state broadcast storm (issue #4145).
///
/// ## What this guards
///
/// Sustained UPDATE fan-out from one host to its direct subscribers of a CRDT
/// contract. After each subscriber bootstraps on a single full state, every
/// subsequent broadcast to that subscriber must be a small delta — not another
/// full state. The #4145 fix caches the target peer's summary on ANY delivered
/// broadcast (delta OR full state), not just deltas, so the host can compute a
/// delta for the next broadcast instead of re-sending full state forever.
///
/// ## Scenario
///
/// 1 gateway (the contract host / broadcast source) + a small number of direct
/// subscriber nodes. The gateway PUTs+subscribes, every node subscribes, then
/// the gateway drives a burst of sequential UPDATEs that fan out to all
/// subscribers.
///
/// ## Why a *small* star topology (1 gateway + 2 direct subscribers)
///
/// The chicken-and-egg the fix targets is per-(sender, target): a sender that
/// lacks a target's cached summary must send full state. A LARGER topology
/// (e.g. 5+ subscribers forming a multi-hop relay tree) does NOT make this test
/// more sensitive — it makes it LESS sensitive. Relay nodes cache a downstream
/// peer's *full state* as its "summary", and `compute_delta` then rejects the
/// diff as "not efficient" (`is_delta_efficient`), emitting full state
/// regardless of the #4145 gate. Those relay-tree full-states swamp the signal:
/// at 5 subscribers, fix vs reverted is ~199 vs ~201 full-state sends — noise.
///
/// A clean star (the host broadcasts directly to its subscribers, no relay tree)
/// removes that floor, so the metric isolates exactly the bug the fix addresses:
/// with the fix `full_state_sends` collapses below `delta_sends` (the host caches
/// each subscriber's summary after a delivered broadcast and switches to deltas)
/// while `delta_sends` grows with the update count; reverted, `full_state_sends`
/// scales with updates × subscribers and overtakes `delta_sends`. The unit tests
/// in `broadcast_queue.rs` pin the gate logic directly; this sim pins the
/// system-level consequence on the path simulation builds actually exercise
/// (the `#[cfg(simulation_tests)]` inline broadcast loop in `p2p_protoc.rs`).
///
/// ## Assertions (the delta-dominance one FAILS if the #4145 gate is reverted)
///
/// - (a) Event-loop liveness: no `StalePeer` anomaly — every subscriber keeps
///   receiving broadcasts; none goes silent under the fan-out.
/// - (b) Full-state-send rate collapses: `delta_sends > full_state_sends`. With
///   the fix the host caches each subscriber's summary after a delivered
///   broadcast, so subsequent updates go out as deltas and deltas dominate.
///   (Full-state sends are reduced below delta sends but NOT to one-per-
///   subscriber — bootstrap + retry jitter mean several full states per
///   subscriber.) Reverted, every update is full state to every subscriber and
///   `delta_sends` stays low → this assertion fails. (Measured on this star
///   topology: fix = 78 delta / 47 full; reverted = 40 delta / 85 full.)
/// - (c) Broadcast delivery / update propagation stays high: all peers converge.
#[test_log::test]
fn test_sustained_update_fanout_no_full_state_storm() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    const SEED: u64 = 0x4233_5701_0001;
    const NETWORK_NAME: &str = "i4233-fanout-storm";
    // Direct subscribers of the gateway (the broadcast fan-out width). Kept small
    // so the host broadcasts directly to subscribers with no multi-hop relay tree
    // — see the "Why a small star topology" note above.
    const SUBSCRIBERS: usize = 2;
    // UPDATEs driven into the hot contract AFTER all subscribers have bootstrapped.
    // Enough that, reverted, the per-update full-state fan-out clearly overtakes
    // the one-time bootstrap deltas.
    const SUSTAINED_UPDATES: usize = 20;

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,           // 1 gateway (the broadcast source / contract host)
            SUBSCRIBERS, // subscriber nodes (the fan-out targets)
            7,           // max_htl
            3,           // rnd_if_htl_above
            10,          // max_connections
            2,           // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // CRDT contract so post-bootstrap broadcasts compute real version-aware
    // deltas (a plain hash contract's "delta" is full state, which would not
    // exercise the delta transition this test is about).
    let contract = SimOperation::create_test_contract(0x42);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    let initial_state = SimOperation::create_crdt_state(1, 0x10);

    let mut operations = Vec::new();

    // Bootstrap: gateway PUTs + subscribes (becomes the host/source), then every
    // node subscribes. Each subscriber's FIRST broadcast is full state — that is
    // the one bootstrap full-state per subscriber the fix permits.
    operations.push(ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: initial_state,
            subscribe: true,
        },
    ));
    for node_idx in 1..=SUBSCRIBERS {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, node_idx),
            SimOperation::Subscribe { contract_id },
        ));
    }

    // Sustained UPDATE fan-out: a burst of UPDATEs from the gateway, each fanned
    // out to all subscribers. With the fix every subscriber already has a cached
    // summary after its bootstrap full state, so these all go out as deltas.
    // Without the fix every one of these is another full state to every
    // subscriber — the storm.
    for v in 0..SUSTAINED_UPDATES {
        let version = (v as u64) + 2; // versions 2.. (v1 was the PUT)
        operations.push(ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(version, 0x20 + v as u8),
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(240), // simulation duration (virtual)
        Duration::from_secs(90),  // post-op propagation wait (virtual)
    );
    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    let convergence =
        rt.block_on(async { freenet::dev_tool::check_convergence_from_logs(&logs_handle).await });
    let delta_sends = GlobalTestMetrics::delta_sends();
    let full_state_sends = GlobalTestMetrics::full_state_sends();
    let resync_count = GlobalTestMetrics::resync_requests();
    // Event-loop backlog proxy: the high-water mark of in-flight transactions
    // awaiting a reply (`pending_op_results` map size). A full-state storm that
    // pins the event loop lets replies pile up, pushing this mark up; a healthy
    // run keeps it low. See `GlobalTestMetrics::pending_op_high_water_mark`.
    let pending_op_hwm = GlobalTestMetrics::pending_op_high_water_mark();

    // Anomaly report for liveness (StalePeer = a subscriber that stopped
    // receiving broadcasts while others kept getting them).
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    let stale = report.stale_peers();

    tracing::info!(
        "i4233 fanout: delta_sends={}, full_state_sends={}, resyncs={}, pending_op_hwm={}, converged={}/{}, stale_peers={}",
        delta_sends,
        full_state_sends,
        resync_count,
        pending_op_hwm,
        convergence.converged.len(),
        convergence.total_contracts(),
        stale.len(),
    );

    // Sanity: the scenario must actually have broadcast something, otherwise the
    // collapse assertion below would pass vacuously.
    assert!(
        delta_sends + full_state_sends > 0,
        "no broadcasts were recorded — the fan-out scenario did not run"
    );

    // (a) LIVENESS: no subscriber goes silent under the fan-out. A full-state
    // storm that pins the event loop strands a subscriber, which surfaces as a
    // StalePeer anomaly.
    assert!(
        stale.is_empty(),
        "#4233 liveness: {} subscriber(s) went stale under sustained UPDATE \
         fan-out (event loop starved by the broadcast storm): {:?}",
        stale.len(),
        stale,
    );

    // (a2) EVENT-LOOP BACKLOG STAYS BOUNDED. StalePeer is a content-divergence
    // proxy for liveness; this is a more direct event-loop-backlog signal. The
    // high-water mark of `pending_op_results` (in-flight transactions awaiting a
    // reply) tracks how far the event loop falls behind: if the loop is pinned by
    // a broadcast storm, replies pile up and the mark climbs. Measured steady
    // state on this star is ~15 (identical on both the fixed and reverted gate —
    // in this small deterministic harness the storm shows up as full-state-send
    // volume, not as op-result backlog, so this bound is a SATURATION guard, not
    // the fix-vs-revert discriminator; assertion (b) is that discriminator). The
    // bound is set well above steady state but far below a level that would
    // indicate the loop is wedged and replies are accumulating unboundedly.
    const PENDING_OP_BACKLOG_BOUND: u64 = 200;
    assert!(
        pending_op_hwm < PENDING_OP_BACKLOG_BOUND,
        "#4233 liveness: event-loop backlog high-water mark {pending_op_hwm} \
         reached {PENDING_OP_BACKLOG_BOUND}+ in-flight op-results under sustained \
         UPDATE fan-out — the event loop is falling behind (replies piling up), \
         a sign the broadcast path is pinning the loop."
    );

    // (b) FULL-STATE-SEND RATE COLLAPSES — the load-bearing #4145 discriminator.
    //
    // With the fix the host caches a subscriber's summary after a delivered
    // broadcast, so subsequent updates go out as deltas. Full-state sends collapse
    // BELOW delta sends (not to one-per-subscriber — bootstrap plus retry jitter
    // produce several full states per subscriber), so over SUSTAINED_UPDATES
    // updates the delta sends dominate: delta_sends > full_state_sends.
    //
    // Reverted to `if sent_delta`, the host never caches a subscriber's summary,
    // so EVERY update is full state to EVERY subscriber and delta_sends stays
    // low — full_state_sends overtakes delta_sends and THIS assertion fails.
    // (Measured on this star topology: fix = 78 delta / 47 full; reverted = 40
    // delta / 85 full.)
    //
    // We assert the *direction* (deltas dominate) rather than an absolute
    // full-state budget: a fixed numeric cap is brittle against topology/retry
    // jitter, whereas the delta-vs-full crossover flips cleanly between the fixed
    // and reverted gate.
    assert!(
        delta_sends > full_state_sends,
        "#4145 REGRESSION: deltas must dominate after bootstrap, but \
         delta_sends={delta_sends} <= full_state_sends={full_state_sends}. \
         A reverted summary-cache gate traps every subscriber on full state \
         (the #4233 storm): every update re-sends full state to every subscriber \
         instead of a delta."
    );

    // (c) DELIVERY / PROPAGATION stays high: all peers converge to one state.
    assert!(
        convergence.is_converged(),
        "#4233: broadcast delivery degraded — peers failed to converge under \
         sustained fan-out. {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len(),
    );

    tracing::info!(
        "test_sustained_update_fanout_no_full_state_storm PASSED: \
         delta_sends={} > full_state_sends={}, converged, no stale peers",
        delta_sends,
        full_state_sends,
    );
}

// =============================================================================
// #4233: Event-loop liveness under wide-star sustained UPDATE fan-out
// =============================================================================

/// Shared driver for the #4233 wide-star broadcast fan-out liveness scenario.
///
/// ## What this guards (the safety gate for re-enabling placement migration)
///
/// On 2026-05-24 both production gateways wedged simultaneously (#4145) under
/// sustained UPDATE fan-out from a popular River-style contract with dozens of
/// downstream subscribers: the event-loop `event_loop_notification` channel
/// filled faster than the loop could drain it, the drainers were themselves
/// blocked downstream (a self-feeding back-pressure cycle), and subscribers
/// went silent. #4231 broke the cycle (`try_notify_node_event` + a 500 ms
/// `reserve()` timeout on per-peer dispatch); #4442 fixed a second, separate
/// full-state broadcast STORM under the same load (cache the summary on ANY
/// delivered broadcast, not just deltas). `.claude/rules/channel-safety.md`
/// lists this bug class with 6+ production incidents.
///
/// The existing `test_sustained_update_fanout_no_full_state_storm` pins the
/// #4442 storm discriminator but only at `SUBSCRIBERS = 2`. This driver scales
/// the SAME star to the production-incident width (40-80 direct subscribers) so
/// the fan-out actually exercises the event-loop saturation surface, and asserts
/// **liveness** (no subscriber goes silent, backlog stays bounded, no broadcast
/// is lost or left unapplied, all peers converge) rather than the delta/full
/// crossover. This is the simulation-level guard #4233 asks for, and the gate
/// that must be green before the placement-migration / `SubscribeHint` feature
/// (which adds new `notify_node_event` fan-out sites) is re-enabled.
///
/// ## CRITICAL topology knob: `max_connections >= subscribers`
///
/// To get a TRUE wide star (one host with direct connections to all N
/// subscribers — the production pattern from the incident) the gateway must be
/// allowed to hold N direct connections. If `max_connections < N` a multi-hop
/// relay tree forms instead: relay nodes cache a downstream peer's full state as
/// its "summary" and `compute_delta` then rejects the diff as inefficient,
/// emitting full state regardless of the #4442 gate. That changes the fan-out
/// shape and swamps the delta/full metric — a different scenario. We size
/// `max_connections` to `subscribers + slack` so the host fans out directly.
///
/// ## Fan-out width vs. the harness bootstrap ceiling (why N=8/16, not 40-80)
///
/// #4233 calls for 40-80 subscribers (the popular-River-contract width that
/// wedged production). Empirically, the Turmoil simulation harness CANNOT
/// bootstrap a star that wide within a feasible CI wall-clock budget, so the
/// public tests run at N=8 and N=16 instead — the widest faithful direct star
/// this harness can actually form. Measured on this runner (current origin/main,
/// migration OFF):
///
/// | N  | result | wall-clock | what happened                                  |
/// |----|--------|-----------|-------------------------------------------------|
/// |  8 | PASS   | ~32 s     | star bootstraps, 40 UPDATEs broadcast, liveness holds |
/// | 16 | PASS   | <200 s    | as above, at double the width                   |
/// | 24 | FAIL   | ~135 s    | "Ran for duration: 300s … without completing"   |
/// | 32 | FAIL   | ~169 s    | same — topology never finishes forming          |
/// | 40 | FAIL   | times out | 300 002 virtual steps; subscribers stuck on "peer has not joined the network yet" |
///
/// The N>=24 failures are NOT the broadcast wedge. The simulation never reaches
/// the UPDATE phase: it exhausts the entire 300s virtual-time schedule in
/// CONNECT/topology formation (subscribers route CONNECT through the ring, get
/// "at terminus, no uphill peers available — rejecting" in the tiny 1-gateway
/// star, and churn on pending reservations), so zero UPDATEs and zero broadcasts
/// ever fire. That is a harness-scale limit UPSTREAM of the fan-out this test
/// targets, not evidence about the event loop under load.
///
/// To exercise a true 40-80-direct-subscriber star, the harness would need a
/// topology-preseed primitive that wires every subscriber directly to the
/// gateway (bypassing organic CONNECT formation), analogous to the
/// `SeedHostedContract` contract preseed. That is a harness follow-up; until it
/// exists, N=8/16 is the faithful ceiling and is what gates the migration
/// re-enable. On current main both PASS: under sustained fan-out at these widths
/// the event loop does NOT wedge (no stale peers, bounded backlog, convergence).
///
/// ## Signals available vs. what #4233 ideally wants
///
/// #4233 ideally asserts on `Event loop stats iterations` advancing and
/// `notification_channel_pending` staying bounded. Those are emitted as raw
/// `tracing::info!("Event loop stats")` lines in
/// `p2p_protoc.rs` — they are NOT part of the structured `NetLogMessage` event
/// log that `StateVerifier` / `check_convergence_from_logs` consume, and the
/// integration test harness here captures the structured log, not raw tracing
/// fields. So we assert on the strongest proxies the harness DOES expose:
///   - `StalePeer` anomalies — a subscriber that stopped receiving broadcasts
///     while others kept getting them. This is the incident's defining symptom
///     (subscribers go silent) and is exactly what a wedged event loop produces.
///   - `pending_op_high_water_mark` — high-water mark of in-flight transactions
///     awaiting a reply (`pending_op_results` map size). A wedged loop lets
///     replies pile up unboundedly; a healthy loop keeps this bounded. This is
///     the closest available analogue to `notification_channel_pending`.
///   - `MissingBroadcast` / `BroadcastNotApplied` — broadcast-assembly / delivery
///     failures (a broadcast that never reached a subscriber, or reached it but
///     was never applied).
///   - convergence — every subscriber ends on the host's latest state.
///
/// To assert directly on event-loop iteration progress and
/// `notification_channel_pending` per the letter of #4233, the harness would
/// need to either surface those two fields as `GlobalTestMetrics` counters
/// (mirroring `pending_op_high_water_mark`) or expose a raw-tracing capture to
/// integration tests. That is noted as a harness follow-up; the proxies above
/// cover the same failure mode (silent subscribers + unbounded backlog).
#[cfg(feature = "simulation_tests")]
fn run_fanout_liveness_scenario(network_name: &str, seed: u64, subscribers: usize) {
    run_fanout_liveness_scenario_inner(network_name, seed, subscribers, false, false);
}

/// Configurable driver behind [`run_fanout_liveness_scenario`].
///
/// `preseed` selects how the wide direct star is formed:
///   - `false`: organic ring-routed CONNECT (the original path). Only forms up
///     to ~N=16 in this harness — see the bootstrap-ceiling note above.
///   - `true`: inject the direct star via [`SimNetwork::preseed_direct_star`],
///     bypassing the CONNECT handshake so N=40/60/80 (the production-incident
///     width) can be reached. This is the #4233 topology-preseed primitive.
///
/// `enable_migration` selects the placement-migration (`SubscribeHint`) cascade
/// floor:
///   - `false`: migration OFF (sim default) — the baseline fan-out.
///   - `true`: migration ON via [`SimNetwork::enable_placement_migration`] —
///     the closest sim reproduction of the v0.2.73 incident, in which the
///     migration AMPLIFIES broadcast load.
#[cfg(feature = "simulation_tests")]
fn run_fanout_liveness_scenario_inner(
    network_name: &str,
    seed: u64,
    subscribers: usize,
    preseed: bool,
    enable_migration: bool,
) {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    // UPDATEs driven into the hot contract AFTER all subscribers have
    // bootstrapped. A sustained burst (not a single update) is what saturated
    // the notification channel in the incident.
    const SUSTAINED_UPDATES: usize = 40;

    GlobalTestMetrics::reset();
    setup_deterministic_state(seed);
    let rt = create_runtime();

    // Wide star: the gateway must hold a direct connection to every subscriber,
    // so max_connections must exceed the fan-out width. Slack covers the
    // gateway's own bootstrap/gateway-mesh connections. If this were < N a
    // relay tree would form and the topology would no longer be the production
    // wide-star (see the rustdoc above).
    let max_connections = subscribers + 16;

    let (sim, logs_handle) = rt.block_on(async {
        let mut sim = SimNetwork::new(
            network_name,
            1,           // 1 gateway (the broadcast source / contract host)
            subscribers, // subscriber nodes (the fan-out targets)
            7,           // max_htl
            3,           // rnd_if_htl_above
            max_connections,
            2, // min_connections
            seed,
        )
        .await;
        if enable_migration {
            // Closest sim reproduction of the v0.2.73 incident: the migration
            // cascade amplifies the broadcast/summarize load under fan-out.
            sim.enable_placement_migration();
        } else {
            // Pin the placement-migration (`SubscribeHint`) cascade OFF for the
            // baseline direct-star scenario.
            //
            // This is NOT a no-op: the cascade gates on the build version being
            // `>= SUBSCRIBE_HINT_MIN_VERSION` (0.2.80), and this crate is already
            // past that floor, so #4404 migration is ON by default in sim now.
            // Left on, the host's UPDATE broadcasts nudge closer subscribers to
            // re-host the contract and those nudges cascade among subscribers,
            // fragmenting the intended *direct* star into a shifting multi-level
            // tree: hosting migrates off the gateway, the host's direct-interest
            // set no longer covers every subscriber, and a subscriber can end up
            // hosting a different replica subset than the host — so the converged
            // replica set is no longer deterministically `host + all subscribers`
            // and the (e) full-participation invariant cannot hold. The
            // `migration=false` scenario's premise is a STABLE direct star where
            // every subscriber holds the same contract the host broadcasts, so it
            // pins the cascade off at any build version (the migration-ON variant
            // covers the cascade explicitly). See `disable_placement_migration`.
            sim.disable_placement_migration();
        }
        if preseed {
            // #4233 topology preseed: wire the gateway directly to every
            // subscriber so the production-incident-width star (N=40-80) exists
            // at t=0, bypassing the organic CONNECT handshake the harness cannot
            // complete at this width.
            let host = NodeLabel::gateway(network_name, 0);
            let subs: Vec<NodeLabel> = (1..=subscribers)
                .map(|idx| NodeLabel::node(network_name, idx))
                .collect();
            sim.preseed_direct_star(&host, &subs);
        }
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // CRDT contract so post-bootstrap broadcasts compute real version-aware
    // deltas (a plain hash contract's "delta" is full state).
    let contract = SimOperation::create_test_contract(0x42);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    let initial_state = SimOperation::create_crdt_state(1, 0x10);

    let mut operations = Vec::new();

    // Bootstrap: gateway PUTs + subscribes (becomes the host/source), then every
    // node fetches the contract and subscribes directly to the gateway.
    operations.push(ScheduledOperation::new(
        NodeLabel::gateway(network_name, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: initial_state,
            subscribe: true,
        },
    ));
    for node_idx in 1..=subscribers {
        // Each subscriber GETs-then-subscribes rather than issuing a bare
        // `Subscribe`. This is REQUIRED for the subscriber to actually join the
        // fan-out: a bare `ContractRequest::Subscribe` is rejected by the
        // client-event handler ("Rejecting SUBSCRIBE: contract WASM not cached
        // locally", client_events.rs) unless the node already holds the
        // contract WASM — the production guard that forces a real client to PUT
        // or GET the contract before subscribing (so it can validate/apply
        // incoming updates). `GET+subscribe=true` is the documented exempt path:
        // the GET inherently fetches the WASM+state from the host over the
        // preseeded connection, and the explicit `subscribe=true` flag then
        // subscribes (`maybe_subscribe_child` -> `run_client_subscribe`),
        // registering downstream interest at the host and joining the broadcast
        // set. (Piece E removed the implicit AUTO_SUBSCRIBE_ON_GET fallback; this
        // path is unaffected because it always set subscribe=true.) The topology preseed only
        // injects ring connections, not contract state, so without the GET each
        // subscriber's bare subscribe is rejected at t=0 and only the subset
        // that happened to receive the contract first (via an early broadcast /
        // the placement-migration cascade) before its paced op fires gets
        // through — a race that strands ~half the star (the (e) participation
        // shortfall). The genuine GET routing + subscribe + interest + broadcast
        // machinery is exercised end-to-end; only the WASM-acquisition
        // precondition that a real subscriber must satisfy is made explicit.
        operations.push(ScheduledOperation::new(
            NodeLabel::node(network_name, node_idx),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: true,
            },
        ));
    }

    // Sustained UPDATE fan-out: a burst of UPDATEs from the gateway, each fanned
    // out to all N subscribers. This is the load pattern that wedged production.
    for v in 0..SUSTAINED_UPDATES {
        let version = (v as u64) + 2; // versions 2.. (v1 was the PUT)
        operations.push(ScheduledOperation::new(
            NodeLabel::gateway(network_name, 0),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(version, 0x20 + v as u8),
            },
        ));
    }

    // The controlled-sim test client paces every triggered operation with a
    // fixed ~3s virtual-time gap (see `run_controlled_simulation`). The
    // operation sequence here is O(N): 1 PUT + N subscribes + SUSTAINED_UPDATES
    // updates, so the *operation phase* alone needs `(1 + N + SUSTAINED_UPDATES)
    // * 3s` of virtual time before the post-op settle even begins. At N=8/16 the
    // fixed 300s budget covered this, but at incident scale (N=40-80) the
    // sequence overruns 300s and the simulation reports "Ran for duration: 300s
    // … without completing" before any liveness can be assessed — a budget
    // overrun, NOT a topology-formation or event-loop problem. Scale the budget
    // with N so the operation phase always fits, with generous headroom.
    const PER_OP_PACING_SECS: u64 = 3;
    const POST_OP_WAIT_SECS: u64 = 120;
    let op_count = (1 + subscribers + SUSTAINED_UPDATES) as u64;
    // op-phase + post-op settle + a 90s margin for startup + final propagation.
    let sim_duration_secs = op_count * PER_OP_PACING_SECS + POST_OP_WAIT_SECS + 90;

    let result = sim.run_controlled_simulation(
        seed,
        operations,
        Duration::from_secs(sim_duration_secs), // simulation duration (virtual)
        Duration::from_secs(POST_OP_WAIT_SECS), // post-op propagation wait (virtual)
    );
    assert!(
        result.turmoil_result.is_ok(),
        "#4233 fan-out (N={subscribers}): simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    let convergence =
        rt.block_on(async { freenet::dev_tool::check_convergence_from_logs(&logs_handle).await });
    let delta_sends = GlobalTestMetrics::delta_sends();
    let full_state_sends = GlobalTestMetrics::full_state_sends();
    let resync_count = GlobalTestMetrics::resync_requests();
    // Event-loop backlog proxy (closest available analogue to
    // notification_channel_pending): high-water mark of in-flight transactions
    // awaiting a reply. A wedged loop lets replies pile up; a healthy loop keeps
    // this bounded.
    let pending_op_hwm = GlobalTestMetrics::pending_op_high_water_mark();

    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    let stale = report.stale_peers();
    let missing = report.missing_broadcasts();
    let unapplied = report.unapplied_broadcasts();

    tracing::info!(
        "#4233 fanout N={}: delta_sends={}, full_state_sends={}, resyncs={}, \
         pending_op_hwm={}, converged={}/{}, stale_peers={}, missing_broadcasts={}, \
         unapplied_broadcasts={}",
        subscribers,
        delta_sends,
        full_state_sends,
        resync_count,
        pending_op_hwm,
        convergence.converged.len(),
        convergence.total_contracts(),
        stale.len(),
        missing.len(),
        unapplied.len(),
    );

    // Sanity: the scenario must actually have broadcast something, otherwise the
    // liveness assertions below would pass vacuously.
    assert!(
        delta_sends + full_state_sends > 0,
        "#4233 fan-out (N={subscribers}): no broadcasts were recorded — the \
         fan-out scenario did not run"
    );

    // (a) LIVENESS — the incident's defining symptom. No subscriber goes silent
    // under the fan-out. A wedged event loop strands a subscriber, surfacing as
    // a StalePeer anomaly.
    assert!(
        stale.is_empty(),
        "#4233 liveness (N={subscribers}): {} subscriber(s) went stale under \
         sustained UPDATE fan-out — the event loop wedged and subscribers went \
         silent (the v0.2.73 / #4145 production symptom): {:?}",
        stale.len(),
        stale,
    );

    // (b) EVENT-LOOP BACKLOG STAYS BOUNDED. If the loop is pinned by the
    // broadcast fan-out, op-result replies accumulate and the high-water mark
    // climbs without bound. The bound scales with the fan-out width (each
    // UPDATE legitimately fans out to N subscribers, so a window of concurrent
    // in-flight broadcast/op work is expected to grow with N) plus generous
    // headroom, but stays far below a level that would indicate the loop is
    // wedged and replies are piling up unboundedly. A wedged loop produces a
    // mark that scales with updates*subscribers, dwarfing this bound.
    let pending_op_bound = (subscribers as u64) * 8 + 256;
    assert!(
        pending_op_hwm < pending_op_bound,
        "#4233 liveness (N={subscribers}): event-loop backlog high-water mark \
         {pending_op_hwm} reached the bound {pending_op_bound} of in-flight \
         op-results under sustained UPDATE fan-out — the event loop is falling \
         behind (replies piling up), a sign the broadcast path is pinning the \
         loop (the #4145 wedge condition)."
    );

    // (c) BROADCAST DELIVERY HEALTH — LOG-ONLY, NOT asserted.
    //
    // `missing_broadcasts()` / `unapplied_broadcasts()` are NOT reliable
    // pass/fail gates in a multi-node fan-out topology, so we record them as
    // diagnostics but do not assert on them. The `MissingBroadcast` detector
    // (state_verifier/detectors.rs) matches strictly per-(transaction, target):
    // every target listed in a `BroadcastEmitted.broadcast_to` must log a
    // `BroadcastReceived`/`BroadcastApplied` under the SAME transaction id. Two
    // benign mechanisms break that match under this scenario:
    //   1. Summary-match skip (the #4442 fix path itself): the broadcast loop
    //      skips a target whose cached summary already equals the host's, so no
    //      frame is sent and no `BroadcastReceived` is logged — yet the emitted
    //      event still lists that target in `broadcast_to`. This fires even in a
    //      pure direct star whenever a subscriber is already up to date.
    //   2. Fresh-tx-per-hop: a relay re-broadcast mints a NEW transaction and
    //      the cross-hop `BroadcastStateChange` carries no tx, so a subscriber
    //      reached via a relay logs its receive under a tx the original emitter
    //      never referenced.
    // Both produce `MissingBroadcast`/`BroadcastNotApplied` anomalies with no
    // actual lost delivery — confirmed by convergence + no-stale-peers holding.
    // No other simulation test in this file asserts on these two signals; the
    // model test (`test_sustained_update_fanout_no_full_state_storm`) and the
    // rest of the suite gate on `stale_peers` + convergence only. Liveness here
    // is captured by (a) StalePeer, (b) pending-op backlog, and (d) convergence.
    if !missing.is_empty() || !unapplied.is_empty() {
        tracing::info!(
            "#4233 (N={}): broadcast-bookkeeping diagnostics (NOT a failure): \
             missing_broadcasts={}, unapplied_broadcasts={} — these are \
             per-(tx,target) verifier artifacts (summary-skip / relay-hop), see \
             the rationale above; liveness is gated on stale_peers + convergence",
            subscribers,
            missing.len(),
            unapplied.len(),
        );
    }

    // (d) CONVERGENCE: every subscriber ends on the host's latest state. A
    // subscriber stranded by the wedge would diverge.
    assert!(
        convergence.is_converged(),
        "#4233 (N={subscribers}): broadcast delivery degraded — peers failed to \
         converge under sustained fan-out. {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len(),
    );

    // (e) FULL PARTICIPATION — preseeded direct star only.
    //
    // `is_converged()` (d) and `stale.is_empty()` (a) are necessary but NOT
    // sufficient: `check_convergence_from_logs` only counts peers that logged
    // contract state and ignores contracts with < 2 replicas, so a subscriber
    // that silently never joined the fan-out logs no state and is invisible to
    // BOTH checks — the gate would then pass with a partial star. This assertion
    // closes that gap by requiring the converged contract's replica set to be
    // exactly the host plus EVERY subscriber.
    //
    // Scoped to `preseed && !enable_migration`, because exact participation is a
    // structural invariant ONLY there: `preseed_direct_star` wires every
    // subscriber directly to the host at t=0, so every subscriber's GET+subscribe
    // must reach the host and join the broadcast set (a shortfall means a real
    // loss — e.g. a `ConnectionManager` cap-rejected preseed connection, or a
    // subscribe that never reached the host).
    //
    // Excluded from the migration-ON variant for the same reason the baseline
    // pins the cascade OFF (see the `disable_placement_migration` rationale
    // above): with the `SubscribeHint` cascade live, hosting migrates off the
    // gateway and the converged replica set is no longer deterministically
    // `host + all subscribers`, so this exact-count invariant cannot hold — a
    // failure there would be benign migration churn, not a liveness loss. The
    // migration-ON incident-scale test is gated by (a)–(d) instead.
    //
    // Excluded from the organic N=8/16 variants (preseed=false) because they form
    // the star via ring-routed CONNECT, which is best-effort at this harness's
    // bootstrap ceiling — a subscriber can legitimately fail to form its host
    // connection ("at terminus … rejecting", the very limitation the preseed
    // primitive exists to bypass), so requiring all N there would assert an
    // invariant organic formation does not provide. Those variants are gated by
    // (a)–(d) (no stale peers, bounded backlog, convergence), which hold.
    if preseed && !enable_migration {
        let expected_replicas = subscribers + 1; // 1 gateway host + N subscribers
        assert!(
            !convergence.converged.is_empty(),
            "#4233 participation (N={subscribers}): no contract reached the >=2-replica \
             convergence threshold — the fan-out star did not form"
        );
        for c in &convergence.converged {
            assert_eq!(
                c.replica_count, expected_replicas,
                "#4233 participation (N={subscribers}): converged contract {} has {} \
                 replicas, expected {} (host + all subscribers). A subscriber never \
                 logged contract state, so it silently dropped out of the fan-out \
                 (preseed cap-rejection or a subscribe that never reached the host); \
                 the liveness gate would otherwise pass vacuously.",
                c.contract_key, c.replica_count, expected_replicas,
            );
        }
    }

    tracing::info!(
        "#4233 fan-out liveness (N={}) PASSED: no stale peers, \
         pending_op_hwm={} < {}, converged ({} delta / {} full sends)",
        subscribers,
        pending_op_hwm,
        pending_op_bound,
        delta_sends,
        full_state_sends,
    );
}

/// #4233 wide-star fan-out liveness at N=8 direct subscribers.
///
/// See [`run_fanout_liveness_scenario`] for the full rationale and the
/// "Fan-out width vs. the harness bootstrap ceiling" note explaining why this
/// is N=8/16 rather than the production-incident 40-80.
#[test_log::test]
#[cfg(feature = "simulation_tests")]
fn test_fanout_liveness_8_subscribers() {
    run_fanout_liveness_scenario("i4233-fanout-8", 0x4233_0801_0001, 8);
}

/// #4233 wide-star fan-out liveness at N=16 direct subscribers.
///
/// N=16 is the widest faithful direct star this Turmoil harness can bootstrap
/// within the CI wall-clock budget (N>=24 exhausts the 300s virtual-time
/// schedule in topology formation before any UPDATE fires — see
/// [`run_fanout_liveness_scenario`]). It is the closest reachable proxy for the
/// 40-80-subscriber production incident.
#[test_log::test]
#[cfg(feature = "simulation_tests")]
fn test_fanout_liveness_16_subscribers() {
    run_fanout_liveness_scenario("i4233-fanout-16", 0x4233_1601_0001, 16);
}

// =============================================================================
// #4233 INCIDENT-SCALE fan-out liveness diagnostics (N = 40 / 60 / 80)
//
// These use the topology-preseed primitive (`SimNetwork::preseed_direct_star`)
// to build the production-incident-width direct star that the organic CONNECT
// path cannot bootstrap in this harness (N>=24 never finishes — see the
// bootstrap-ceiling note on `run_fanout_liveness_scenario`). They are the real
// incident-scale reproduction #4233 asks for, in BOTH conditions:
//   - migration OFF (default): baseline sustained UPDATE fan-out.
//   - migration ON: the closest sim reproduction of the v0.2.73 incident, where
//     the placement-migration cascade amplifies the broadcast/summarize load.
//
// They are `#[ignore]`d by default: each builds 41-81 real event-loop nodes and
// drives 40 sustained UPDATEs fanned out across the whole star, which is far
// heavier than the normal per-test CI budget. The N=8/16 organic tests above
// are the always-on regression gate; these are the on-demand incident-scale
// diagnostic. Run explicitly with, e.g.:
//
//   cargo test -p freenet --features "simulation_tests,testing" \
//     --test simulation_integration -- --ignored --nocapture \
//     test_fanout_liveness_incident_scale
// =============================================================================

/// #4233 incident-scale fan-out liveness, migration OFF.
///
/// Preseeded direct star at N=40/60/80, sustained UPDATE fan-out, migration OFF.
/// Asserts the same liveness gates as the organic tests (no stale peers,
/// bounded event-loop backlog, convergence) at the production-incident width.
#[test_log::test]
#[cfg(feature = "simulation_tests")]
#[ignore = "#4233 incident-scale diagnostic (preseeded N=40/60/80, 40 UPDATEs each); too heavy for default CI. Run with --ignored. Always-on gate is test_fanout_liveness_8/16_subscribers."]
fn test_fanout_liveness_incident_scale_migration_off() {
    for (n, seed) in [
        (40usize, 0x4233_4001_0001u64),
        (60, 0x4233_6001_0001),
        (80, 0x4233_8001_0001),
    ] {
        let name = format!("i4233-fanout-preseed-off-{n}");
        run_fanout_liveness_scenario_inner(
            &name, seed, n, /*preseed=*/ true, /*migration=*/ false,
        );
    }
}

/// #4233 incident-scale fan-out liveness, migration ON.
///
/// Preseeded direct star at N=40/60/80, sustained UPDATE fan-out, with the
/// placement-migration (`SubscribeHint`) cascade ENABLED. This is the closest
/// sim reproduction of the actual v0.2.73 incident, where the migration
/// amplifies broadcast load. The headline question #4233 poses: does the event
/// loop wedge at incident scale with the migration on?
#[test_log::test]
#[cfg(feature = "simulation_tests")]
#[ignore = "#4233 incident-scale diagnostic (preseeded N=40/60/80, 40 UPDATEs each, migration ON); too heavy for default CI. Run with --ignored. Always-on gate is test_fanout_liveness_8/16_subscribers."]
fn test_fanout_liveness_incident_scale_migration_on() {
    for (n, seed) in [
        (40usize, 0x4233_4001_0002u64),
        (60, 0x4233_6001_0002),
        (80, 0x4233_8001_0002),
    ] {
        let name = format!("i4233-fanout-preseed-on-{n}");
        run_fanout_liveness_scenario_inner(
            &name, seed, n, /*preseed=*/ true, /*migration=*/ true,
        );
    }
}

/// #4233 topology-preseed sanity at modest width (N=24), migration OFF.
///
/// N=24 is the smallest width the organic CONNECT path CANNOT bootstrap in this
/// harness (see the bootstrap-ceiling note on `run_fanout_liveness_scenario`).
/// This test forms it via the preseed primitive instead, proving the primitive
/// reaches widths organic formation cannot — and runs fast enough to stay in the
/// default suite as an always-on guard that the preseed path itself works.
#[test_log::test]
#[cfg(feature = "simulation_tests")]
fn test_fanout_liveness_preseed_24_subscribers() {
    run_fanout_liveness_scenario_inner(
        "i4233-fanout-preseed-24",
        0x4233_2401_0001,
        24,
        /*preseed=*/ true,
        /*migration=*/ false,
    );
}

// =============================================================================
// Extended Edge Case Tests for Ring Protocol
// =============================================================================

use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

/// Parametrized CRDT convergence test.
///
/// All nodes subscribe then update simultaneously. Verifies convergence.
/// Each (nodes, gateways) combo is tested with 5 different seeds to detect
/// topology-dependent failures. See #3028.
///
/// Run with: cargo test -p freenet --features simulation_tests,testing test_crdt_convergence
#[rstest::rstest]
#[case::n3_g1_s1("crdt-3n-1gw-s1", 0x2773_0003_0001, 1, 3)]
#[case::n3_g1_s2("crdt-3n-1gw-s2", 0x2773_0003_0002, 1, 3)]
#[case::n3_g1_s3("crdt-3n-1gw-s3", 0x2773_0003_0003, 1, 3)]
#[case::n3_g1_s4("crdt-3n-1gw-s4", 0x2773_0003_0007, 1, 3)]
#[case::n3_g1_s5("crdt-3n-1gw-s5", 0x2773_0003_0006, 1, 3)]
#[case::n5_g2_s1("crdt-5n-2gw-s1", 0x2773_0005_1021, 2, 5)]
#[case::n5_g2_s2("crdt-5n-2gw-s2", 0x2773_0005_1012, 2, 5)]
#[case::n5_g2_s3("crdt-5n-2gw-s3", 0x2773_0005_1003, 2, 5)]
#[case::n5_g2_s4("crdt-5n-2gw-s4", 0x2773_0005_2001, 2, 5)]
#[case::n5_g2_s5("crdt-5n-2gw-s5", 0x2773_0005_1005, 2, 5)]
#[case::n6_g2_s1("crdt-6n-2gw-s1", 0x2773_0006_1001, 2, 6)]
#[case::n6_g2_s2("crdt-6n-2gw-s2", 0x2773_0006_1002, 2, 6)]
#[case::n6_g2_s3("crdt-6n-2gw-s3", 0x2773_0006_1003, 2, 6)]
#[case::n6_g2_s4("crdt-6n-2gw-s4", 0x2773_0006_1004, 2, 6)]
#[case::n6_g2_s5("crdt-6n-2gw-s5", 0x2773_0006_1005, 2, 6)]
#[case::n4_g1_s1("crdt-4n-1gw-s1", 0x2773_0004_0001, 1, 4)]
#[case::n4_g1_s2("crdt-4n-1gw-s2", 0x2773_0004_0002, 1, 4)]
#[case::n4_g1_s3("crdt-4n-1gw-s3", 0x2773_0004_0003, 1, 4)]
#[case::n4_g1_s4("crdt-4n-1gw-s4", 0x2773_0004_0004, 1, 4)]
#[case::n4_g1_s5("crdt-4n-1gw-s5", 0x2773_0004_0005, 1, 4)]
#[case::n5_g1_s1("crdt-5n-1gw-s1", 0x2773_0005_0008, 1, 5)]
#[case::n5_g1_s2("crdt-5n-1gw-s2", 0x2773_0005_0012, 1, 5)]
#[case::n5_g1_s3("crdt-5n-1gw-s3", 0x2773_0005_0003, 1, 5)]
#[case::n5_g1_s4("crdt-5n-1gw-s4", 0x2773_0005_0004, 1, 5)]
#[case::n5_g1_s5("crdt-5n-1gw-s5", 0x2773_0005_0005, 1, 5)]
#[case::n6_g1_s1("crdt-6n-1gw-s1", 0x2773_0006_0001, 1, 6)]
#[case::n6_g1_s2("crdt-6n-1gw-s2", 0x2773_0006_0002, 1, 6)]
#[case::n6_g1_s3("crdt-6n-1gw-s3", 0x2773_0006_0003, 1, 6)]
#[case::n6_g1_s4("crdt-6n-1gw-s4", 0x2773_0006_0004, 1, 6)]
#[case::n6_g1_s5("crdt-6n-1gw-s5", 0x2773_0006_0005, 1, 6)]
#[case::n7_g1_s1("crdt-7n-1gw-s1", 0x2773_0007_0001, 1, 7)]
#[case::n7_g1_s2("crdt-7n-1gw-s2", 0x2773_0007_0002, 1, 7)]
#[case::n7_g1_s3("crdt-7n-1gw-s3", 0x2773_0007_0023, 1, 7)]
#[case::n7_g1_s4("crdt-7n-1gw-s4", 0x2773_0007_0010, 1, 7)]
#[case::n7_g1_s5("crdt-7n-1gw-s5", 0x2773_0007_0005, 1, 7)]
#[case::n8_g1_s1("crdt-8n-1gw-s1", 0x2773_0008_0001, 1, 8)]
#[case::n8_g1_s2("crdt-8n-1gw-s2", 0x2773_0008_0002, 1, 8)]
#[case::n8_g1_s3("crdt-8n-1gw-s3", 0x2773_0008_0003, 1, 8)]
#[case::n8_g1_s4("crdt-8n-1gw-s4", 0x2773_0008_0004, 1, 8)]
#[case::n8_g1_s5("crdt-8n-1gw-s5", 0x2773_0008_0005, 1, 8)]
fn test_crdt_convergence(
    #[case] name: &'static str,
    #[case] seed: u64,
    #[case] gateways: usize,
    #[case] nodes: usize,
) {
    GlobalTestMetrics::reset();

    setup_deterministic_state(seed);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(name, gateways, nodes, 7, 3, 10, 4, seed).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0x5F);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    // Gateway puts + all nodes subscribe simultaneously
    let mut operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(name, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: SimOperation::create_crdt_state(1, 0x00),
            subscribe: true,
        },
    )];

    for i in 1..=nodes {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(name, i),
            SimOperation::Subscribe { contract_id },
        ));
    }

    // All nodes update simultaneously
    for i in 1..=nodes {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(name, i),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(10 + i as u64, i as u8),
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        seed,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(90),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "[{}] Simulation should complete: {:?}",
        name,
        result.turmoil_result.err()
    );

    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    let resync_count = GlobalTestMetrics::resync_requests();

    tracing::info!(
        "[{}] nodes={}, gateways={}, converged={}/{}, resyncs={}",
        name,
        nodes,
        gateways,
        convergence.converged.len(),
        convergence.total_contracts(),
        resync_count
    );

    for diverged in &convergence.diverged {
        tracing::warn!(
            "[{}] DIVERGED: {} - {} unique states across {} peers",
            name,
            diverged.contract_key,
            diverged.unique_state_count(),
            diverged.peer_states.len()
        );
    }

    assert!(
        convergence.is_converged(),
        "[{}] {} nodes must converge. {} converged, {} diverged",
        name,
        nodes,
        convergence.converged.len(),
        convergence.diverged.len()
    );

    tracing::info!(
        "[{}] PASSED: converged={}, resyncs={}",
        name,
        convergence.converged.len(),
        resync_count
    );
}

/// Test: CRDT convergence with N nodes updating simultaneously.
///
/// All 6 nodes subscribe then issue concurrent updates with different versions.
/// Verifies CRDT merge logic correctly converges to identical final state.
#[test_log::test]
fn test_concurrent_updates_from_n_sources() {
    const SEED: u64 = 0xC0C0_BEEF_0002;
    const NETWORK_NAME: &str = "concurrent-updates-n";
    const NODE_COUNT: usize = 6;

    GlobalTestMetrics::reset();

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 2, NODE_COUNT, 10, 5, 15, 4, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xC0);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    // Gateway puts v1, all nodes subscribe, then all update simultaneously
    let mut operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: SimOperation::create_crdt_state(1, 0x00),
            subscribe: true,
        },
    )];

    for i in 1..=NODE_COUNT {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Subscribe { contract_id },
        ));
    }

    for i in 1..=NODE_COUNT {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(10 + i as u64, i as u8),
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(90),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    let resync_count = GlobalTestMetrics::resync_requests();

    tracing::info!(
        "Concurrent N-source updates: converged={}/{}, resyncs={}",
        convergence.converged.len(),
        convergence.total_contracts(),
        resync_count
    );

    for diverged in &convergence.diverged {
        tracing::warn!(
            "DIVERGED: {} - {} unique states across {} peers",
            diverged.contract_key,
            diverged.unique_state_count(),
            diverged.peer_states.len()
        );
    }

    assert!(
        convergence.is_converged(),
        "Concurrent N-way updates MUST converge. {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    tracing::info!(
        "test_concurrent_updates_from_n_sources PASSED: converged={}, resyncs={}",
        convergence.converged.len(),
        resync_count
    );
}

/// Test: CRDT convergence when nodes have divergent state versions (4-node variant).
///
/// This test validates that the network converges correctly when:
/// 1. Multiple nodes independently update to different versions
/// 2. Each node has a "stale" view of other nodes' summaries
/// 3. ResyncRequest mechanism recovers divergent state
///
#[test_log::test]
fn test_stale_summary_cache_multiple_branches() {
    const SEED: u64 = 0x57A1_E001_0001;
    const NETWORK_NAME: &str = "stale-summaries";
    const NODE_COUNT: usize = 4;

    GlobalTestMetrics::reset();

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 1, NODE_COUNT, 7, 3, 10, 2, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0x57);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    // Gateway puts v1
    let mut operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: SimOperation::create_crdt_state(1, 0x01),
            subscribe: true,
        },
    )];

    for i in 1..=NODE_COUNT {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Subscribe { contract_id },
        ));
    }

    for i in 1..=NODE_COUNT {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(10 + i as u64, i as u8),
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(90),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    let resync_count = GlobalTestMetrics::resync_requests();

    tracing::info!(
        "Stale summary test: converged={}/{}, resyncs={}",
        convergence.converged.len(),
        convergence.total_contracts(),
        resync_count
    );

    for diverged in &convergence.diverged {
        tracing::warn!(
            "DIVERGED: {} - {} unique states across {} peers",
            diverged.contract_key,
            diverged.unique_state_count(),
            diverged.peer_states.len()
        );
    }

    assert!(
        convergence.is_converged(),
        "Divergent state must converge via CRDT merge + ResyncRequest. {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    tracing::info!(
        "test_stale_summary_cache_multiple_branches PASSED: converged={}, resyncs={}",
        convergence.converged.len(),
        resync_count
    );
}

/// Test: CRDT convergence in large network (12 nodes).
///
/// This test validates that updates propagate correctly and converge
/// in a larger network where update broadcasts must reach all 12 hosters.
#[test_log::test]
fn test_max_downstream_limit_reached() {
    const SEED: u64 = 0x0A0D_0001_0001;
    const NETWORK_NAME: &str = "large-network";
    const NODE_COUNT: usize = 12;

    GlobalTestMetrics::reset();

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 2, NODE_COUNT, 10, 5, 15, 3, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0x0A);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    // Gateway puts contract, all 12 nodes subscribe
    let mut operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: SimOperation::create_crdt_state(1, 0x00),
            subscribe: true,
        },
    )];

    for i in 1..=NODE_COUNT {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Subscribe { contract_id },
        ));
    }

    // All nodes update simultaneously
    for i in 1..=NODE_COUNT {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(10 + i as u64, i as u8),
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(300),
        Duration::from_secs(150),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    let resync_count = GlobalTestMetrics::resync_requests();

    tracing::info!(
        "Large network test: converged={}/{}, resyncs={}",
        convergence.converged.len(),
        convergence.total_contracts(),
        resync_count
    );

    for diverged in &convergence.diverged {
        tracing::warn!(
            "DIVERGED: {} - {} unique states across {} peers",
            diverged.contract_key,
            diverged.unique_state_count(),
            diverged.peer_states.len()
        );
    }

    assert!(
        convergence.is_converged(),
        "Large network must converge. {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    tracing::info!(
        "test_max_downstream_limit_reached PASSED: converged={}, resyncs={}",
        convergence.converged.len(),
        resync_count
    );
}

/// Test: Chain topology formation with sequential subscriptions.
/// Test: CRDT convergence with sequential subscriptions.
///
/// Originally tested subscription tree topology (Issue #2787).
/// Now tests that nodes subscribing one-by-one still achieve CRDT convergence
/// when updates are issued after all have subscribed.
#[test_log::test]
fn test_chain_topology_formation() {
    const SEED: u64 = 0xC4A1_0001_0001;
    const NETWORK_NAME: &str = "chain-topology";
    const NODE_COUNT: usize = 4;

    GlobalTestMetrics::reset();

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 1, NODE_COUNT, 10, 5, 10, 2, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xC4);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    // Gateway puts contract, nodes subscribe sequentially (not simultaneously)
    let mut operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: SimOperation::create_crdt_state(1, 0x00),
            subscribe: true,
        },
    )];

    for i in 1..=NODE_COUNT {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Subscribe { contract_id },
        ));
    }

    // After all subscribe, multiple nodes update
    for i in 1..=NODE_COUNT {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(10 + i as u64, i as u8),
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(90),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    let resync_count = GlobalTestMetrics::resync_requests();

    tracing::info!(
        "Chain/sequential test: converged={}/{}, resyncs={}",
        convergence.converged.len(),
        convergence.total_contracts(),
        resync_count
    );

    // All nodes should converge to same state
    assert!(
        convergence.is_converged(),
        "All {} contracts should converge. Converged: {}, Diverged: {:?}",
        convergence.total_contracts(),
        convergence.converged.len(),
        convergence.diverged
    );

    tracing::info!(
        "test_chain_topology_formation PASSED: converged={}, resyncs={}",
        convergence.converged.len(),
        resync_count
    );
}

// =============================================================================
// Subscription Broadcast Propagation Test
// =============================================================================

/// Test: Updates from gateway propagate to subscribers via broadcast.
///
/// This test specifically verifies that when a peer subscribes to a contract,
/// subsequent updates from the gateway reach the subscriber. This catches the
/// regression where subscriptions were registered but subscribers weren't added
/// to broadcast targets (proximity_sources=0, interest_sources=0).
///
/// ## The Bug (PR #2794 regression)
///
/// After the lease-based subscription refactor, when a subscription was accepted:
/// 1. The subscription was registered locally via `ring.subscribe()`
/// 2. But `announce_contract_hosted()` was NOT called
/// 3. So the subscriber never announced to neighbors that it has the contract
/// 4. When the gateway tried to broadcast updates, it had no targets
/// 5. Updates were silently dropped with "NO_TARGETS" warning
///
/// ## Why Existing Tests Didn't Catch This
///
/// The `check_convergence_from_logs` function skips contracts where only 1 peer
/// has logged state. When broadcasts fail, subscribers never receive updates,
/// never log state, and the convergence check silently skips the contract.
///
/// ## What This Test Does
///
/// 1. Gateway PUTs a contract with initial state
/// 2. A separate node SUBSCRIBES to the contract
/// 3. Gateway sends an UPDATE
/// 4. **Explicitly verify the subscriber received the update** (has matching state)
///
/// The test fails if the subscriber doesn't have the same state as the gateway
/// after the update, which happens when broadcasts don't reach subscribers.
#[test_log::test]
fn test_subscription_broadcast_propagation() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0xBCAD_C057_0001; // "broadcast" seed
    const NETWORK_NAME: &str = "broadcast-test";

    GlobalTestMetrics::reset();

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        // Simple topology: 1 gateway, 2 nodes
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,  // 1 gateway
            2,  // 2 regular nodes
            7,  // max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Create a CRDT contract for proper state merging
    let contract = SimOperation::create_test_contract(0xBC);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    // Test basic subscription->update broadcast flow:
    // 1. Gateway PUTs the contract (announces to neighbors)
    // 2. Node 1 subscribes (should result in contract being announced by Node 1)
    // 3. Gateway sends an UPDATE (should broadcast to Node 1)
    //
    // The bug (PR #2794 regression): subscription acceptance doesn't call
    // announce_contract_hosted, so the gateway doesn't know Node 1 has the contract.
    let operations = vec![
        // Gateway puts with subscribe=true (so it seeds the contract)
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: SimOperation::create_crdt_state(1, 0x01),
                subscribe: true,
            },
        ),
        // Node 1 subscribes - this should:
        // 1. Get subscription accepted
        // 2. Fetch contract via GET (if not present)
        // 3. Announce to neighbors that it has the contract
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        // Gateway updates - should broadcast to Node 1 (the subscriber)
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(100, 0xFF), // Distinctive update value
            },
        ),
    ];

    // Run simulation with enough time for subscription + update propagation
    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120), // Total simulation time
        Duration::from_secs(60),  // Post-operation wait for propagation
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    // Check convergence - but this alone won't catch the bug (it skips single-peer contracts)
    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });

    tracing::info!(
        "Broadcast propagation test: converged={}, diverged={}",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    // THE CRITICAL CHECK: Verify at least 2 peers have state for this contract
    // If the bug is present, only the gateway will have logged state (subscriber never received update)
    let contract_key_str = format!("{:?}", contract_key);

    // Extract peer states from logs for this specific contract
    let peer_states: std::collections::BTreeMap<std::net::SocketAddr, String> =
        rt.block_on(async {
            let logs = logs_handle.lock().await;
            let mut states = std::collections::BTreeMap::new();
            for log in logs.iter() {
                if let Some(key) = log.kind.contract_key() {
                    if format!("{:?}", key) == contract_key_str {
                        if let Some(hash) = log.kind.stored_state_hash() {
                            states.insert(log.peer_id.socket_addr(), hash.to_string());
                        }
                    }
                }
            }
            states
        });

    tracing::info!(
        "Contract {} has state on {} peers: {:?}",
        contract_key_str,
        peer_states.len(),
        peer_states.keys().collect::<Vec<_>>()
    );

    // ASSERTION: At least 2 peers must have state for this contract
    // If only 1 peer has it, the broadcast failed to reach the subscriber
    assert!(
        peer_states.len() >= 2,
        "BUG: Update broadcast failed! Only {} peer(s) have state for contract {}. \
         Expected at least 2 (gateway + subscriber). \
         This indicates subscribers aren't receiving broadcasts (proximity_sources=0). \
         Peers with state: {:?}",
        peer_states.len(),
        contract_key_str,
        peer_states.keys().collect::<Vec<_>>()
    );

    // Also verify the states match (convergence)
    let unique_states: std::collections::HashSet<&String> = peer_states.values().collect();
    assert!(
        unique_states.len() == 1,
        "BUG: State divergence! {} unique states across {} peers for contract {}. \
         States: {:?}",
        unique_states.len(),
        peer_states.len(),
        contract_key_str,
        peer_states
    );

    // Verify BroadcastEmitted telemetry events were produced (#3622)
    let broadcast_emitted_count: usize = rt.block_on(async {
        let logs = logs_handle.lock().await;
        logs.iter()
            .filter(|log| log.kind.is_update_broadcast_emitted())
            .count()
    });

    assert!(
        broadcast_emitted_count > 0,
        "Expected at least one UpdateEvent::BroadcastEmitted telemetry event, got 0. \
         This means the emission wiring from #3622 is broken."
    );

    tracing::info!(
        "test_subscription_broadcast_propagation PASSED: {} peers converged to same state, \
         {} BroadcastEmitted events",
        peer_states.len(),
        broadcast_emitted_count
    );
}

/// Regression test for #3390: relay nodes must register upstream requester as
/// downstream subscriber when forwarding a subscribe Response.
///
/// ## The Bug
///
/// When a subscribe routes through a relay (e.g., node-2 → gateway → node-1),
/// the fulfilling node (node-1) correctly registers the gateway as a downstream
/// subscriber. But when the gateway forwards the Response to node-2, it did NOT
/// register node-2 as its own downstream subscriber. Updates flowing from
/// node-1 → gateway would then stop at the gateway — node-2 never received them.
///
/// ## What This Test Does
///
/// 1. Node 1 PUTs a contract (becomes the hoster/fulfiller)
/// 2. Node 2 SUBSCRIBES — the subscribe routes through the gateway to node 1
/// 3. Node 1 sends an UPDATE
/// 4. Verify node 2 received the update (has matching state)
///
/// If the relay doesn't register node-2 as downstream, updates die at the gateway.
#[test_log::test]
fn test_subscription_relay_propagation() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    const SEED: u64 = 0xBEAD_FEED_3390;
    const NETWORK_NAME: &str = "relay-sub-test";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        // 1 gateway + 3 nodes: node-1 puts, node-2 subscribes, gateway relays
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,  // 1 gateway
            3,  // 3 regular nodes
            7,  // max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0x39);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    // Node 1 puts the contract (NOT the gateway), so subscribe from node 2
    // must relay through the gateway to reach node 1.
    let operations = vec![
        // Node 1 puts with subscribe=true (seeds the contract)
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Put {
                contract: contract.clone(),
                state: SimOperation::create_crdt_state(1, 0x01),
                subscribe: true,
            },
        ),
        // Node 2 subscribes — routes through gateway to node 1
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 2),
            SimOperation::Subscribe { contract_id },
        ),
        // Node 1 updates — should propagate: node-1 → gateway → node-2
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(100, 0xFE),
            },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120),
        Duration::from_secs(60),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    let contract_key_str = format!("{:?}", contract_key);

    // Extract per-peer states for this contract from logs
    let peer_states: std::collections::BTreeMap<std::net::SocketAddr, String> =
        rt.block_on(async {
            let logs = logs_handle.lock().await;
            let mut states = std::collections::BTreeMap::new();
            for log in logs.iter() {
                if let Some(key) = log.kind.contract_key() {
                    if format!("{:?}", key) == contract_key_str {
                        if let Some(hash) = log.kind.stored_state_hash() {
                            states.insert(log.peer_id.socket_addr(), hash.to_string());
                        }
                    }
                }
            }
            states
        });

    tracing::info!(
        "Relay subscription test: contract {} has state on {} peers: {:?}",
        contract_key_str,
        peer_states.len(),
        peer_states.keys().collect::<Vec<_>>()
    );

    // ASSERTION: At least 3 peers must have state (node-1 hoster + gateway relay + node-2 subscriber)
    // Without the relay fix, only node-1 and gateway would have state (2 peers);
    // node-2 wouldn't receive updates because the gateway didn't register it as downstream.
    assert!(
        peer_states.len() >= 3,
        "BUG (#3390): Relay subscription failed! Only {} peer(s) have state for contract {}. \
         Expected at least 3 (hoster + gateway relay + subscriber). \
         The relay node (gateway) likely didn't register the subscriber as downstream. \
         Peers with state: {:?}",
        peer_states.len(),
        contract_key_str,
        peer_states.keys().collect::<Vec<_>>()
    );

    // Verify convergence (all peers have same state)
    let unique_states: std::collections::HashSet<&String> = peer_states.values().collect();
    assert!(
        unique_states.len() == 1,
        "State divergence in relay test! {} unique states across {} peers: {:?}",
        unique_states.len(),
        peer_states.len(),
        peer_states
    );

    // Run StateVerifier for anomaly detection (per testing.md)
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    tracing::info!(
        "Anomaly report: {} anomalies across {} contracts",
        report.anomalies.len(),
        report.contracts_analyzed
    );

    tracing::info!(
        "test_subscription_relay_propagation PASSED: {} peers converged via relay",
        peer_states.len()
    );
}

// =============================================================================
// Long-Duration Simulation Tests (1 Hour Virtual Time)
// =============================================================================

/// Long-running deterministic simulation test for 1 hour of virtual time.
///
/// This test is designed to uncover time-dependent bugs that only manifest
/// after extended periods of operation, such as:
/// - Connection timeout handling over long periods
/// - State drift in long-lived contracts
/// - Memory leaks or resource exhaustion patterns
/// - Timer handling edge cases
/// - Keep-alive and heartbeat issues
///
/// The test simulates 1 hour (3600 seconds) of virtual time using Turmoil's
/// deterministic scheduler, ensuring reproducible results.
///
/// # Runtime
/// Long-running deterministic simulation (1 hour virtual time).
///
/// Tests for time-dependent bugs (connection timeouts, state drift, etc.)
/// that only manifest after extended operation. Uses wider event spacing
/// (10s apart) compared to regular tests (200ms) to exercise idle-path code.
///
/// # Virtual Time Breakdown
/// - Events phase: 360 operations × 10s = 3600 seconds (1 hour)
/// - Propagation: 10 seconds
/// - Total: ~3610 seconds virtual time
/// - Wall clock: ~2.5 min with direct runner (single-threaded paused-time runtime)
///
/// NOTE: Gated by nightly_tests feature — does NOT run in regular CI.
#[test_log::test]
#[cfg(feature = "nightly_tests")]
fn test_long_running_deterministic() {
    const SEED: u64 = 0x1A00_2C00_72AC;

    tracing::info!("=== Starting Long-Running Deterministic Simulation ===");
    tracing::info!("Seed: 0x{:X}", SEED);
    tracing::info!("Virtual time target: 3600 seconds (1 hour)");

    let start_time = std::time::Instant::now();

    TestConfig::long_running("long-running", SEED)
        .run_direct()
        .assert_ok()
        .verify_operation_coverage()
        .check_convergence();

    let wall_clock = start_time.elapsed();

    tracing::info!("=== Long-Running Simulation Complete ===");
    tracing::info!("Wall clock time: {:.1} seconds", wall_clock.as_secs_f64());
    tracing::info!(
        "Time acceleration: {:.1}x",
        3600.0 / wall_clock.as_secs_f64()
    );

    tracing::info!("test_long_running_deterministic PASSED");
}

// =============================================================================
// Roadmap Scenario: Partition → Heal → Convergence
// =============================================================================

/// Tests that the network converges after a partition heals.
///
/// ## Scenario (from simulation-testing.md roadmap)
/// 1. Start a network and let contract operations flow
/// 2. Partition the network into two halves (via global fault injector)
/// 3. Wait while both sides operate in isolation
/// 4. Heal the partition
/// 5. Assert convergence via anomaly detection
///
/// Uses Turmoil's deterministic scheduler. Faults are injected mid-simulation
/// through the global `get_fault_injector` registry.
#[test_log::test]
fn test_partition_heal_convergence() {
    use freenet::dev_tool::NodeLabel;
    use freenet::simulation::Partition;

    const SEED: u64 = 0xDA27_0EA1_0001;
    const NETWORK_NAME: &str = "partition-heal";

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle, node_addrs) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1, // 1 gateway
            5, // 5 nodes (6 total, splits 3+3)
            7,
            3,
            10,
            2,
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        let node_addrs: HashMap<NodeLabel, std::net::SocketAddr> = sim.all_node_addresses().clone();
        (sim, logs_handle, node_addrs)
    });

    // Capture addresses for partition injection inside the test closure
    let addrs: Vec<std::net::SocketAddr> = node_addrs.values().copied().collect();
    let mid = addrs.len() / 2;
    let side_a: std::collections::HashSet<_> = addrs[..mid].iter().copied().collect();
    let side_b: std::collections::HashSet<_> = addrs[mid..].iter().copied().collect();

    let side_a_len = side_a.len();
    let side_b_len = side_b.len();
    let network_name = NETWORK_NAME.to_string();

    // iterations=100 gives enough gen_event budget (100/6 peers ≈ 16 iterations/peer)
    // event_wait=500ms keeps total event time to ~50s, leaving room for test_fn
    let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
        SEED,
        5,                          // contracts
        100,                        // iterations (also per-peer gen_event budget)
        Duration::from_secs(120),   // simulation_duration
        Duration::from_millis(500), // event_wait
        move || async move {
            // Phase 1: Events have already been firing. Now inject the partition.
            tracing::info!(
                "Injecting partition: side_a={} nodes, side_b={} nodes",
                side_a_len,
                side_b_len,
            );

            if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                let mut state = injector.lock().unwrap();
                let partition = Partition::new(side_a, side_b).permanent(0);
                state.config.add_partition(partition);
            }

            // Phase 2: Let both sides operate independently during partition
            tokio::time::sleep(Duration::from_secs(15)).await;
            tracing::info!("Partition active for 15s, now healing");

            // Phase 3: Heal the partition by clearing all partitions
            if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                let mut state = injector.lock().unwrap();
                state.config.partitions.clear();
            }

            // Phase 4: Wait for convergence after healing
            tokio::time::sleep(Duration::from_secs(15)).await;
            tracing::info!("Post-heal convergence period complete");

            Ok(())
        },
    );

    assert!(
        result.is_ok(),
        "Partition-heal simulation failed: {:?}",
        result.err()
    );

    // Check convergence
    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    tracing::info!(
        "Partition-heal: {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    // Run anomaly detection
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });

    tracing::info!(
        "=== PARTITION-HEAL ANOMALY REPORT: {} events, {} state, {} contracts, {} anomalies ===",
        report.total_events,
        report.state_events,
        report.contracts_analyzed,
        report.anomalies.len()
    );

    let divergences = report.divergences();
    let missing = report.missing_broadcasts();
    let partitions = report.suspected_partitions();
    let stale = report.stale_peers();
    let oscillations = report.state_oscillations();

    tracing::warn!(
        "  divergences={}, missing_broadcasts={}, partitions={}, stale={}, oscillations={}",
        divergences.len(),
        missing.len(),
        partitions.len(),
        stale.len(),
        oscillations.len(),
    );

    for (i, anomaly) in report.anomalies.iter().enumerate() {
        tracing::debug!("  anomaly[{}] = {:?}", i, anomaly);
    }
}

// =============================================================================
// Roadmap Scenario: Node Crash → Recover → Convergence
// =============================================================================

/// Tests that the network converges after nodes crash and recover.
///
/// ## Scenario (from simulation-testing.md roadmap)
/// 1. Put contracts into network and let state propagate
/// 2. Crash 2 nodes (via fault injector — messages to/from are dropped)
/// 3. Remaining nodes continue operating
/// 4. Recover the crashed nodes (messages flow again)
/// 5. Assert convergence via anomaly detection
///
/// Note: This uses crash/recover (message blocking) rather than full process
/// restart, since `restart_node()` requires `&mut SimNetwork` which is consumed
/// by `run_simulation()`. The effect on the anomaly detector is the same:
/// crashed nodes miss updates and become stale.
#[test_log::test]
fn test_crash_recover_convergence() {
    use freenet::dev_tool::NodeLabel;

    const SEED: u64 = 0x2011_2E57_0002;
    const NETWORK_NAME: &str = "crash-recover";

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle, node_addrs) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 1, 5, 7, 3, 10, 2, SEED).await;
        let logs_handle = sim.event_logs_handle();
        let node_addrs: HashMap<NodeLabel, std::net::SocketAddr> = sim.all_node_addresses().clone();
        (sim, logs_handle, node_addrs)
    });

    // Pick 2 non-gateway nodes to crash
    let crash_addrs: Vec<std::net::SocketAddr> = node_addrs
        .iter()
        .filter(|(label, _)| label.is_node())
        .take(2)
        .map(|(_, addr)| *addr)
        .collect();

    assert_eq!(crash_addrs.len(), 2, "Need at least 2 non-gateway nodes");

    let network_name = NETWORK_NAME.to_string();
    let crash_addrs_clone = crash_addrs.clone();

    // iterations=100 gives enough gen_event budget (100/6 peers ≈ 16 iterations/peer)
    let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
        SEED,
        3,                          // contracts
        100,                        // iterations (also per-peer gen_event budget)
        Duration::from_secs(120),   // simulation_duration
        Duration::from_millis(500), // event_wait
        move || async move {
            // Phase 1: Events have been firing. Now crash 2 nodes.
            tracing::info!("Crashing 2 nodes: {:?}", crash_addrs_clone);

            if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                let mut state = injector.lock().unwrap();
                for addr in &crash_addrs_clone {
                    state.config.crash_node(*addr);
                }
            }

            // Phase 2: Let the remaining network operate in degraded state
            tokio::time::sleep(Duration::from_secs(10)).await;
            tracing::info!("Degraded period over, recovering nodes");

            // Phase 3: Recover the crashed nodes
            if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                let mut state = injector.lock().unwrap();
                for addr in &crash_addrs_clone {
                    state.config.recover_node(addr);
                }
            }

            // Phase 4: Wait for convergence
            tokio::time::sleep(Duration::from_secs(15)).await;
            tracing::info!("Post-recovery convergence period complete");

            Ok(())
        },
    );

    assert!(
        result.is_ok(),
        "Crash-recover simulation failed: {:?}",
        result.err()
    );

    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    tracing::info!(
        "Crash-recover: {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });

    tracing::info!(
        "=== CRASH-RECOVER ANOMALY REPORT: {} events, {} state, {} contracts, {} anomalies ===",
        report.total_events,
        report.state_events,
        report.contracts_analyzed,
        report.anomalies.len()
    );

    let divergences = report.divergences();
    let stale = report.stale_peers();
    let zombies = report.zombie_transactions();
    let oscillations = report.state_oscillations();

    tracing::warn!(
        "  divergences={}, stale_peers={}, zombies={}, oscillations={}",
        divergences.len(),
        stale.len(),
        zombies.len(),
        oscillations.len(),
    );

    for (i, anomaly) in report.anomalies.iter().enumerate() {
        tracing::debug!("  anomaly[{}] = {:?}", i, anomaly);
    }
}

// =============================================================================
// Roadmap Scenario: Multi-Step Churn
// =============================================================================

/// Tests eventual consistency through continuous crash/recover cycles.
///
/// ## Scenario (from simulation-testing.md roadmap)
/// 1. Start the network with contract operations
/// 2. Perform multiple rounds of: crash a node → wait → recover → wait
/// 3. After all churn rounds, verify convergence via anomaly detection
///
/// Each round targets a different node to maximise disruption coverage.
#[test_log::test]
fn test_multi_step_churn() {
    use freenet::dev_tool::NodeLabel;

    const SEED: u64 = 0xC402_0000_0002;
    const NETWORK_NAME: &str = "multi-churn";

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle, node_addrs) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 1, 4, 7, 3, 10, 2, SEED).await;
        let logs_handle = sim.event_logs_handle();
        let node_addrs: HashMap<NodeLabel, std::net::SocketAddr> = sim.all_node_addresses().clone();
        (sim, logs_handle, node_addrs)
    });

    // Collect non-gateway addresses for churn targets
    let churn_addrs: Vec<std::net::SocketAddr> = node_addrs
        .iter()
        .filter(|(label, _)| label.is_node())
        .map(|(_, addr)| *addr)
        .collect();

    let network_name = NETWORK_NAME.to_string();

    // iterations=100 gives enough gen_event budget (100/5 peers = 20 iterations/peer)
    let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
        SEED,
        3,                          // contracts
        100,                        // iterations (also per-peer gen_event budget)
        Duration::from_secs(150),   // simulation_duration (events ~50s + churn ~40s + buffer)
        Duration::from_millis(500), // event_wait
        move || async move {
            const CHURN_ROUNDS: usize = 3;

            for round in 0..CHURN_ROUNDS {
                let target = churn_addrs[round % churn_addrs.len()];

                // Crash
                tracing::info!("Churn round {}: crashing {:?}", round, target);
                if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                    let mut state = injector.lock().unwrap();
                    state.config.crash_node(target);
                }

                // Degraded period
                tokio::time::sleep(Duration::from_secs(5)).await;

                // Recover
                tracing::info!("Churn round {}: recovering {:?}", round, target);
                if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                    let mut state = injector.lock().unwrap();
                    state.config.recover_node(&target);
                }

                // Stabilization period
                tokio::time::sleep(Duration::from_secs(5)).await;
            }

            // Final convergence period
            tokio::time::sleep(Duration::from_secs(10)).await;
            tracing::info!("Multi-step churn: all {} rounds complete", CHURN_ROUNDS);

            Ok(())
        },
    );

    assert!(
        result.is_ok(),
        "Multi-step churn simulation failed: {:?}",
        result.err()
    );

    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    tracing::info!(
        "Multi-step churn: {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });

    tracing::info!(
        "=== MULTI-STEP CHURN ANOMALY REPORT: {} events, {} state, {} contracts, {} anomalies ===",
        report.total_events,
        report.state_events,
        report.contracts_analyzed,
        report.anomalies.len()
    );

    let divergences = report.divergences();
    let missing = report.missing_broadcasts();
    let stale = report.stale_peers();
    let zombies = report.zombie_transactions();
    let oscillations = report.state_oscillations();
    let cascades = report.delta_sync_cascades();

    tracing::warn!(
        "  divergences={}, missing={}, stale={}, zombies={}, oscillations={}, cascades={}",
        divergences.len(),
        missing.len(),
        stale.len(),
        zombies.len(),
        oscillations.len(),
        cascades.len(),
    );

    for (i, anomaly) in report.anomalies.iter().enumerate() {
        tracing::debug!("  anomaly[{}] = {:?}", i, anomaly);
    }
}

// =============================================================================
// Parallel Safety & Determinism Verification
// =============================================================================

/// Proves determinism with per-network cleanup via `SimNetwork::Drop` and thread-local state.
/// Runs the same simulation 3x with the same seed.
///
/// This is the parallel-safe equivalent of `test_strict_determinism_exact_event_equality`.
#[test_log::test]
fn test_determinism_parallel_safe() {
    const SEED: u64 = 0x0A2A_11E1_0001;

    #[derive(Debug, PartialEq)]
    struct Trace {
        event_counts: HashMap<String, usize>,
        event_sequence: Vec<String>,
        total_events: usize,
    }

    fn run_and_trace(name: &str, seed: u64) -> (turmoil::Result, Trace) {
        // Per-network cleanup via SimNetwork::Drop, thread-local counter resets.
        setup_deterministic_state(seed);

        let rt = create_runtime();

        let (sim, logs_handle) = rt.block_on(async {
            let sim = SimNetwork::new(name, 1, 5, 7, 3, 10, 2, seed).await;
            let logs_handle = sim.event_logs_handle();
            (sim, logs_handle)
        });

        let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
            seed,
            3,
            15,
            Duration::from_secs(20),
            Duration::from_millis(200),
            || async {
                tokio::time::sleep(Duration::from_secs(1)).await;
                Ok(())
            },
        );

        let trace = rt.block_on(async {
            let logs = logs_handle.lock().await;
            let mut event_counts: HashMap<String, usize> = HashMap::new();
            let mut event_sequence: Vec<String> = Vec::new();

            for log in logs.iter() {
                let kind_name = log.kind.variant_name().to_string();
                *event_counts.entry(kind_name.clone()).or_insert(0) += 1;
                event_sequence.push(kind_name);
            }

            Trace {
                total_events: logs.len(),
                event_counts,
                event_sequence,
            }
        });
        // SimNetwork dropped here — per-network cleanup runs

        (result, trace)
    }

    let (result1, trace1) = run_and_trace("parallel-safe-run1", SEED);
    let (result2, trace2) = run_and_trace("parallel-safe-run2", SEED);
    let (result3, trace3) = run_and_trace("parallel-safe-run3", SEED);

    assert_eq!(
        result1.is_ok(),
        result2.is_ok(),
        "Simulation outcomes differ: run1={:?}, run2={:?}",
        result1,
        result2
    );
    assert_eq!(result2.is_ok(), result3.is_ok());

    assert!(trace1.total_events > 0, "No events captured");
    assert_eq!(
        trace1.total_events, trace2.total_events,
        "Total events differ: {} vs {}",
        trace1.total_events, trace2.total_events
    );
    assert_eq!(trace2.total_events, trace3.total_events);
    assert_eq!(trace1.event_counts, trace2.event_counts);
    assert_eq!(trace2.event_counts, trace3.event_counts);
    assert_eq!(trace1.event_sequence, trace2.event_sequence);
    assert_eq!(trace2.event_sequence, trace3.event_sequence);

    // Fingerprint verification
    let fp1 = TraceFingerprint::from_events(&trace1.event_sequence, &trace1.event_counts);
    let fp2 = TraceFingerprint::from_events(&trace2.event_sequence, &trace2.event_counts);
    let fp3 = TraceFingerprint::from_events(&trace3.event_sequence, &trace3.event_counts);
    assert_eq!(fp1, fp2, "Fingerprint mismatch between run 1 and run 2");
    assert_eq!(fp2, fp3, "Fingerprint mismatch between run 2 and run 3");

    tracing::info!(
        "PARALLEL-SAFE DETERMINISM PASSED: {} events, fingerprint={:#018x}",
        trace1.total_events,
        fp1.sequence_hash
    );
}

/// Proves that thread-local isolation allows independent simulation runs on separate threads.
///
/// Spawns 2 simulations concurrently on separate OS threads. Each thread creates its own
/// tokio `current_thread` runtime and `SimNetwork`. Both threads must complete successfully
/// and produce events, proving that thread-local state (RNG, simulation time, metrics)
/// provides sufficient isolation for parallel execution.
///
/// Note: Event traces are NOT compared for exact equality because shared atomic counters
/// (CHANNEL_ID_COUNTER, NONCE_RANDOM_PREFIX) race between concurrent threads, causing
/// different initialization sequences. This is harmless for parallel testing (no test
/// asserts on absolute counter values), but prevents exact event comparison. For exact
/// determinism verification, see `test_determinism_parallel_safe` which runs sequentially.
#[test_log::test]
fn test_determinism_across_threads() {
    const SEED: u64 = 0xCE05_7EAD_0001;

    fn run_on_thread(name: &'static str, seed: u64) -> std::thread::JoinHandle<usize> {
        std::thread::spawn(move || {
            // All counters are thread-local, so each spawned thread gets its own
            // counter space automatically. Just need to set seed/time/metrics.
            GlobalRng::set_seed(seed);
            const BASE_EPOCH_MS: u64 = 1577836800000;
            const RANGE_MS: u64 = 5 * 365 * 24 * 60 * 60 * 1000;
            GlobalSimulationTime::set_time_ms(BASE_EPOCH_MS + (seed % RANGE_MS));
            GlobalTestMetrics::reset();
            RequestId::reset_counter();
            freenet::dev_tool::ClientId::reset_counter();
            reset_event_id_counter();
            reset_channel_id_counter();
            StreamId::reset_counter();
            reset_nonce_counter();
            reset_global_node_index();

            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .expect("Failed to create runtime");

            let (sim, logs_handle) = rt.block_on(async {
                let sim = SimNetwork::new(name, 1, 3, 7, 3, 10, 2, seed).await;
                let logs_handle = sim.event_logs_handle();
                (sim, logs_handle)
            });

            let _result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
                seed,
                3,
                10,
                Duration::from_secs(20),
                Duration::from_millis(200),
                || async {
                    tokio::time::sleep(Duration::from_secs(1)).await;
                    Ok(())
                },
            );

            rt.block_on(async { logs_handle.lock().await.len() })
        })
    }

    // Run concurrently on separate threads — proves thread isolation
    let handle1 = run_on_thread("cross-thread-run1", SEED);
    let handle2 = run_on_thread("cross-thread-run2", SEED);

    let events1 = handle1.join().expect("Thread 1 panicked");
    let events2 = handle2.join().expect("Thread 2 panicked");

    assert!(events1 > 0, "Thread 1 should produce events, got 0");
    assert!(events2 > 0, "Thread 2 should produce events, got 0");

    tracing::info!(
        "CROSS-THREAD ISOLATION PASSED: thread1={} events, thread2={} events",
        events1,
        events2
    );
}

// =============================================================================
// Direct Runner Determinism Tests
// =============================================================================

/// **STRICT** determinism test for `run_simulation_direct`: verifies that
/// same seed produces EXACTLY identical events across 3 runs.
///
/// Checks 4 levels of determinism (weakest → strongest):
/// 1. Total event count
/// 2. Per-type event counts
/// 3. Event sequence (variant names in log order)
/// 4. Structural EventSummary (tx, peer_addr, contract_key, state_hash) — sorted
///    to verify that *which* contracts and peers are involved is deterministic,
///    not only event types. The `event_detail` Debug string is excluded because it
///    contains ephemeral fields (e.g., `this_peer_connection_count`).
///
/// Runs 3 times to catch flaky coincidences.
#[test_log::test]
fn test_direct_runner_determinism() {
    const SEED: u64 = 0xD12E_C7DE_7000;

    /// Structural fields of an event for deterministic comparison.
    /// Excludes `event_detail` (Debug string with ephemeral internal state).
    #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
    struct EventKey {
        tx: freenet::dev_tool::Transaction,
        peer_addr: std::net::SocketAddr,
        event_kind_name: String,
        contract_key: Option<String>,
        state_hash: Option<String>,
    }

    #[derive(Debug)]
    struct SimulationTrace {
        event_counts: HashMap<String, usize>,
        event_sequence: Vec<String>,
        /// Sorted structural keys for deep content comparison
        event_keys: Vec<EventKey>,
        total_events: usize,
    }

    fn run_and_trace(name: &str, seed: u64) -> SimulationTrace {
        setup_deterministic_state(seed);

        let rt = create_runtime();

        let (sim, logs_handle) = rt.block_on(async {
            let sim = SimNetwork::new(
                name, 2,  // gateways
                4,  // nodes
                10, // ring_max_htl
                3,  // rnd_if_htl_above
                10, // max_connections
                3,  // min_connections
                seed,
            )
            .await;
            let logs_handle = sim.event_logs_handle();
            (sim, logs_handle)
        });

        drop(rt);

        sim.run_simulation_direct::<rand::rngs::SmallRng>(
            seed,
            10, // max_contract_num
            30, // iterations
            Duration::from_millis(200),
        )
        .expect("Direct simulation should succeed");

        // Extract trace — need a runtime to lock the async mutex
        let rt = create_runtime();
        rt.block_on(async {
            let logs = logs_handle.lock().await;
            let mut event_counts: HashMap<String, usize> = HashMap::new();
            let mut event_sequence: Vec<String> = Vec::new();

            let mut event_keys: Vec<EventKey> = logs
                .iter()
                .map(|log| {
                    let event_kind_name = log.kind.variant_name().to_string();
                    let contract_key = log.kind.contract_key().map(|k| format!("{:?}", k));
                    let state_hash = log.kind.state_hash().map(String::from);

                    *event_counts.entry(event_kind_name.clone()).or_insert(0) += 1;
                    event_sequence.push(event_kind_name.clone());

                    EventKey {
                        tx: log.tx,
                        peer_addr: log.peer_id.socket_addr(),
                        event_kind_name,
                        contract_key,
                        state_hash,
                    }
                })
                .collect();
            event_keys.sort();

            SimulationTrace {
                total_events: logs.len(),
                event_counts,
                event_sequence,
                event_keys,
            }
        })
    }

    // Run 3 times with identical seed
    let trace1 = run_and_trace("direct-det-run1", SEED);
    let trace2 = run_and_trace("direct-det-run2", SEED);
    let trace3 = run_and_trace("direct-det-run3", SEED);

    // All runs must produce events
    assert!(trace1.total_events > 0, "Run 1 should produce events");
    assert!(trace2.total_events > 0, "Run 2 should produce events");
    assert!(trace3.total_events > 0, "Run 3 should produce events");

    // STRICT ASSERTION 1: Exact same total event count
    assert_eq!(
        trace1.total_events, trace2.total_events,
        "DIRECT DETERMINISM FAILURE: Total event counts differ (run1 vs run2)!"
    );
    assert_eq!(
        trace2.total_events, trace3.total_events,
        "DIRECT DETERMINISM FAILURE: Total event counts differ (run2 vs run3)!"
    );

    // STRICT ASSERTION 2: Exact same event counts per type
    assert_eq!(
        trace1.event_counts, trace2.event_counts,
        "DIRECT DETERMINISM FAILURE: Event type counts differ (run1 vs run2)!"
    );
    assert_eq!(
        trace2.event_counts, trace3.event_counts,
        "DIRECT DETERMINISM FAILURE: Event type counts differ (run2 vs run3)!"
    );

    // STRICT ASSERTION 3: Exact same event sequence (variant names in log order)
    for (i, ((e1, e2), e3)) in trace1
        .event_sequence
        .iter()
        .zip(trace2.event_sequence.iter())
        .zip(trace3.event_sequence.iter())
        .enumerate()
    {
        assert_eq!(
            e1, e2,
            "Event sequence differs at index {} (run1 vs run2)!",
            i
        );
        assert_eq!(
            e2, e3,
            "Event sequence differs at index {} (run2 vs run3)!",
            i
        );
    }

    // STRICT ASSERTION 4: Structural event content (tx, peer, contract_key, state_hash)
    assert_eq!(
        trace1.event_keys, trace2.event_keys,
        "DIRECT DETERMINISM FAILURE: Sorted event keys differ (run1 vs run2)!"
    );
    assert_eq!(
        trace2.event_keys, trace3.event_keys,
        "DIRECT DETERMINISM FAILURE: Sorted event keys differ (run2 vs run3)!"
    );

    // Fingerprint verification
    let fp1 = TraceFingerprint::from_events(&trace1.event_sequence, &trace1.event_counts);
    let fp2 = TraceFingerprint::from_events(&trace2.event_sequence, &trace2.event_counts);
    let fp3 = TraceFingerprint::from_events(&trace3.event_sequence, &trace3.event_counts);
    assert_eq!(fp1, fp2, "Fingerprint mismatch between run 1 and run 2");
    assert_eq!(fp2, fp3, "Fingerprint mismatch between run 2 and run 3");

    tracing::info!(
        "DIRECT RUNNER DETERMINISM PASSED: {} events, fingerprint={:#018x}",
        trace1.total_events,
        fp1.sequence_hash
    );
}

// =============================================================================
// Zombie Connection Regression Test (PR #3005)
// =============================================================================

/// Test: Suspend/resume creates zombie connections that block reconnection.
///
/// Reproduces the bug from PR #3005:
/// 1. Network establishes connections
/// 2. Node crashes (simulating suspend without cleanup)
/// 3. Time passes (simulating suspend duration)
/// 4. Node restarts (simulating resume)
/// 5. BUG: Old connection entries persist as "zombies"
/// 6. New CONNECT messages sent through dead transport sockets
/// 7. Bootstrap/reconnection fails
///
/// This is a regression test to prevent the zombie connection bug.
#[test_log::test(tokio::test(flavor = "current_thread"))]
async fn test_suspend_resume_zombie_connections() {
    const SEED: u64 = 0xDEAD_BEEF_3005;
    const NETWORK_NAME: &str = "zombie-connections";

    tracing::info!("=== Testing Zombie Connection Bug (PR #3005) ===");

    let mut sim = SimNetwork::new(NETWORK_NAME, 1, 2, 7, 3, 10, 2, SEED).await;
    sim.with_start_backoff(Duration::from_millis(50));

    // Start network
    let _handles = sim
        .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 5, 10)
        .await;

    // Phase 1: Let network stabilize and establish connections
    tracing::info!("Phase 1: Network startup and stabilization");
    tokio::time::sleep(Duration::from_secs(2)).await;

    // Log initial virtual time
    let initial_time = sim.virtual_time().now_nanos();
    tracing::info!("Initial virtual time: {}ns", initial_time);

    // Verify initial connectivity
    sim.check_connectivity(Duration::from_secs(10))
        .await
        .expect("Initial connectivity check should pass");
    tracing::info!("✓ Network connectivity established");

    // Phase 2: Crash a node (simulate suspend)
    let node_to_suspend = sim
        .all_node_addresses()
        .keys()
        .find(|label| label.is_node())
        .cloned()
        .expect("Should have at least one node");

    tracing::info!(?node_to_suspend, "Phase 2: Simulating suspend (crash node)");
    let crashed = sim.crash_node(&node_to_suspend);
    assert!(crashed, "Node should crash successfully");
    assert!(sim.is_node_crashed(&node_to_suspend));
    tracing::info!("✓ Node crashed (suspend simulated)");

    // Phase 3: Advance time significantly (simulate suspend duration)
    tracing::info!("Phase 3: Simulating time passage during suspend");

    // Advance time by 1 hour in virtual time
    // NOTE: With keepalive enabled (future), this would trigger timeout cleanup
    // Without keepalive, connections persist as zombies
    let suspend_duration = Duration::from_secs(3600);
    sim.virtual_time().advance(suspend_duration);

    let time_after_suspend = sim.virtual_time().now_nanos();
    tracing::info!(
        "✓ Advanced virtual time by {} seconds (now: {}ns)",
        suspend_duration.as_secs(),
        time_after_suspend
    );

    // Let tasks process the time advancement
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Phase 4: Restart node (simulate resume)
    tracing::info!(
        ?node_to_suspend,
        "Phase 4: Simulating resume (restart node)"
    );

    let restart_seed = SEED.wrapping_add(0x1000);
    let handle = sim
        .restart_node::<rand::rngs::SmallRng>(&node_to_suspend, restart_seed, 5, 5)
        .await;

    assert!(handle.is_some(), "Node should restart successfully");
    assert!(!sim.is_node_crashed(&node_to_suspend));
    tracing::info!("✓ Node restarted (resume simulated)");

    // Let the restarted node attempt to bootstrap
    tokio::time::sleep(Duration::from_secs(3)).await;

    // Phase 5: Check for zombie connection bug
    tracing::info!("Phase 5: Checking for zombie connections");

    let connectivity_result = sim.check_connectivity(Duration::from_secs(15)).await;

    match connectivity_result {
        Ok(()) => {
            tracing::info!("✓ Connectivity restored after restart");
            tracing::info!("   FIX WORKING: No zombie connections blocking reconnection");
            tracing::info!("   (or DropAllConnections was called on resume)");
        }
        Err(e) => {
            tracing::error!("✗ Connectivity check failed after restart: {}", e);
            tracing::error!("BUG DETECTED: Zombie connections blocking reconnection");
            tracing::error!("");
            tracing::error!("Root cause analysis:");
            tracing::error!("  - Node crashed without proper cleanup");
            tracing::error!("  - Old connection HashMap entries still exist");
            tracing::error!("  - CONNECT messages sent through dead transport sockets");
            tracing::error!("  - Gateway doesn't respond (socket handle invalid)");
            tracing::error!("  - Bootstrap hangs waiting for response");
            tracing::error!("");
            tracing::error!("Expected fix: Call DropAllConnections in ops_after_resume()");
            tracing::error!("See PR #3005 for details");

            // Fail the test to catch regression
            panic!("Zombie connection bug detected! Connectivity failed after node restart");
        }
    }

    tracing::info!("=== Zombie Connection Test Complete ===");
}

// =============================================================================
// Subscription Storm Regression Test (PR #2995, PR #3146)
// =============================================================================

/// Regression test for subscription renewal storm under high contract load.
///
/// Ensures that with 250+ contracts requiring renewal, the subscription
/// recovery mechanism (PR #2995, #3146) prevents notification channel
/// saturation through rate-limiting and smart backpressure.
///
/// **Background:**
/// - `recover_orphaned_subscriptions()` runs every 30 seconds
/// - Pre-fix: 20+ concurrent renewals could saturate the notification channel
/// - PR #2995: Rate-limited to 5 renewals per interval
/// - PR #3146: Improved to 10 renewals/interval with smart backpressure
///
/// **Test validates:**
/// - Subscription recovery triggers with 250+ contracts
/// - No channel saturation warnings under production-scale load
/// - Network remains responsive during recovery cycles
///
/// **Runtime:** ~8-10 minutes wall-clock (300 contract compilations)
#[test_log::test]
#[cfg(feature = "nightly_tests")]
fn test_subscription_renewal_at_scale() {
    const SEED: u64 = 0x2995_CAFE_BABE;

    tracing::info!("=== Subscription Renewal at Scale (250+ contracts) ===");

    let start_time = std::time::Instant::now();

    let config = TestConfig {
        name: "subscription-renewal-scale",
        seed: SEED,
        gateways: 1,
        nodes: 2,
        ring_max_htl: 7,
        rnd_if_htl_above: 3,
        max_connections: 10,
        min_connections: 2,
        max_contracts: 250,                 // Production scale
        iterations: 300,                    // Seed contracts
        duration: Duration::from_secs(120), // 60s events + 40s sleep + margin
        event_wait: Duration::from_millis(200),
        sleep_after_events: Duration::from_secs(40), // Trigger 30s recovery cycle
        require_convergence: false,
        latency_range: None,
        message_loss_rate: 0.0,
        use_mock_wasm: false,
    };

    let max_contracts = config.max_contracts; // Save before config.run() moves it

    tracing::info!(
        "Testing with {} contracts, {} operations",
        max_contracts,
        config.iterations
    );

    let result = config.run();

    // Verify subscription recovery triggered and channel didn't saturate
    let rt = create_runtime();
    let logs = rt.block_on(async { result.logs_handle.lock().await.clone() });

    let mut renewal_attempts = 0;
    let mut channel_warnings = 0;

    for log in &logs {
        let msg = format!("{:?}", log);
        if msg.contains("Subscription renewal: attempted") {
            renewal_attempts += 1;
        }
        if msg.contains("Notification channel") && msg.contains("full") {
            channel_warnings += 1;
            tracing::error!("Channel saturation detected: {}", msg);
        }
    }

    let wall_clock = start_time.elapsed();

    tracing::info!(
        "Completed in {:.1}s: {} renewal cycles, {} channel warnings",
        wall_clock.as_secs_f64(),
        renewal_attempts,
        channel_warnings
    );

    // Assertions: Regression checks
    result.assert_ok();

    // Log renewal activity (may be 0 if no subscriptions need renewal)
    tracing::info!("Subscription renewal cycles observed: {}", renewal_attempts);

    // REGRESSION CHECK: With the fix (PR #2995/#3146), we should NOT see channel saturation
    // even under high contract load. This is the critical validation.
    assert_eq!(
        channel_warnings, 0,
        "Channel saturation detected - regression in PR #2995/#3146 fix! \
         The subscription renewal rate-limiting may not be working correctly."
    );

    tracing::info!(
        "✓ No channel saturation with {} contracts (PR #2995/#3146 fix working)",
        max_contracts
    );
}

// =============================================================================
// Router Feedback Verification Tests
//
// These tests verify that the router's isotonic regression model receives
// feedback events from PUT, UPDATE, SUBSCRIBE, and GET operations during
// multi-node simulations. Prior to PR #3137, only GET operations (~4% of
// traffic) fed the router. These tests confirm the fix works end-to-end.
//
// Verification approach: Each `routing_finished()` call emits an
// EventKind::Route log entry. We count these "Route" events in the simulation
// event logs to verify the router is receiving feedback.
// =============================================================================

/// Verify that the router accumulates feedback events from operations during simulation.
///
/// Counts `Route` events in the simulation logs (emitted by `routing_finished()` on each
/// completed operation). With PUT, UPDATE, SUBSCRIBE, and GET all feeding the router,
/// we expect Route events proportional to the number of completed operations.
#[test_log::test]
fn test_router_accumulates_feedback_events() {
    let result = TestConfig::small("router-feedback", 0x0FEE_DBAC_0001)
        .with_nodes(4)
        .with_max_contracts(5)
        .with_iterations(50)
        .with_duration(Duration::from_secs(60))
        .with_sleep(Duration::from_secs(2))
        .run()
        .assert_ok();

    // Count Route events (one per routing_finished call)
    let rt = create_runtime();
    let (route_count, route_by_peer) = rt.block_on(async {
        let logs = result.logs_handle.lock().await;
        let route_count = logs
            .iter()
            .filter(|log| log.kind.variant_name() == "Route")
            .count();

        // Group by peer to see distribution
        let mut by_peer: HashMap<String, usize> = HashMap::new();
        for log in logs.iter().filter(|l| l.kind.variant_name() == "Route") {
            *by_peer
                .entry(format!("{}", log.peer_id.socket_addr()))
                .or_default() += 1;
        }
        (route_count, by_peer)
    });

    tracing::info!("=== ROUTER FEEDBACK TEST ===");
    tracing::info!(
        "Total Route events (routing_finished calls): {}",
        route_count
    );
    for (peer, count) in &route_by_peer {
        tracing::info!("  peer {}: {} route events", peer, count);
    }

    // The router must have received feedback from completed operations.
    // With 50 iterations generating PUT/GET/UPDATE/SUBSCRIBE across 5 nodes,
    // each completing operation emits one Route event on the initiating node.
    assert!(
        route_count > 0,
        "No Route events in logs - routing_finished never called. \
         The router is not receiving any feedback from completed operations."
    );

    tracing::info!(
        "ROUTER FEEDBACK TEST PASSED: {} route events across {} peers",
        route_count,
        route_by_peer.len()
    );
}

/// Verify that the router accumulates failure events when messages are lost.
///
/// Injects 15% message loss to cause operation timeouts. The timeout failure
/// reporting path (`report_timeout_failure`) feeds `RouteOutcome::Failure` events
/// to the router, which also emits Route log events. This test verifies:
/// 1. The simulation still completes (tolerates message loss)
/// 2. Timeout events appear in the logs (operations failed)
/// 3. Route events appear in the logs (router received feedback including failures)
///
/// We don't assert convergence since message loss may prevent it.
#[test_log::test]
fn test_router_accumulates_failure_events_with_message_loss() {
    let result = TestConfig::small("router-faults", 0xFA17_0055_0001)
        .with_nodes(4)
        .with_max_contracts(5)
        .with_iterations(60)
        .with_duration(Duration::from_secs(60))
        .with_sleep(Duration::from_secs(2))
        .with_message_loss(0.15)
        .run()
        // Simulation should complete even with message loss
        .assert_ok();

    let rt = create_runtime();
    let (route_count, timeout_count) = rt.block_on(async {
        let logs = result.logs_handle.lock().await;
        let route_count = logs
            .iter()
            .filter(|log| log.kind.variant_name() == "Route")
            .count();
        let timeout_count = logs
            .iter()
            .filter(|log| log.kind.variant_name() == "Timeout")
            .count();
        (route_count, timeout_count)
    });

    tracing::info!("=== ROUTER FAULT INJECTION TEST ===");
    tracing::info!("Route events (routing_finished): {}", route_count);
    tracing::info!("Timeout events: {}", timeout_count);

    // With 15% message loss, some operations complete (Route events with
    // SuccessUntimed/Success) and some timeout (Timeout events trigger
    // report_timeout_failure which also calls routing_finished with Failure).
    assert!(
        route_count > 0,
        "No Route events with 15% message loss - router not receiving any feedback"
    );

    tracing::info!(
        "ROUTER FAULT TEST PASSED: route_events={}, timeouts={}",
        route_count,
        timeout_count
    );
}

/// Verify that the router's 50-event prediction threshold is crossed during simulation.
///
/// The router transitions from distance-based to prediction-based routing once
/// `failure_estimator.len() >= 50` (threshold lowered from 200 to 50 in PR #3137).
/// This test runs enough operations to cross that threshold and verifies
/// `prediction_active = true` in at least one RouterSnapshot event.
///
/// Uses > 5 min virtual time to capture periodic router telemetry snapshots,
/// and enough concentrated operations (many iterations, few nodes) to exceed 50
/// routing events per node.
#[test_log::test]
fn test_router_prediction_threshold_activation() {
    // Only ~15% of iterations produce a Route event (operations that forward to another
    // peer generate routing feedback; operations that store locally are "Irrelevant").
    // With 3 nodes and ~15% hit rate, to get 50 per node: 50 / 0.15 * 3 ≈ 1000 iterations.
    // Using 1500 for margin, event_wait=0.5s → 750s virtual time > 300s telemetry interval.
    let result = TestConfig::small("router-threshold", 0xACE5_0FD0_0001)
        .with_nodes(2)
        .with_max_contracts(6)
        .with_iterations(1500)
        .with_event_wait(Duration::from_millis(500))
        .with_duration(Duration::from_secs(900))
        .with_sleep(Duration::from_secs(2))
        .run()
        .assert_ok();

    // First verify Route events are being generated
    let rt = create_runtime();
    let (route_count, route_by_peer) = rt.block_on(async {
        let logs = result.logs_handle.lock().await;
        let route_count = logs
            .iter()
            .filter(|log| log.kind.variant_name() == "Route")
            .count();
        let mut by_peer: HashMap<String, usize> = HashMap::new();
        for log in logs.iter().filter(|l| l.kind.variant_name() == "Route") {
            *by_peer
                .entry(format!("{}", log.peer_id.socket_addr()))
                .or_default() += 1;
        }
        (route_count, by_peer)
    });

    tracing::info!("=== ROUTER THRESHOLD ACTIVATION TEST ===");
    tracing::info!("Total Route events: {}", route_count);
    for (peer, count) in &route_by_peer {
        tracing::info!("  peer {}: {} route events", peer, count);
    }

    // Check RouterSnapshot telemetry for prediction_active status
    let snapshots = result.router_snapshots();
    tracing::info!("RouterSnapshot events captured: {}", snapshots.len());
    for (i, (failure, success, active)) in snapshots.iter().enumerate() {
        tracing::info!(
            "  snapshot[{}]: failure_events={}, success_events={}, prediction_active={}",
            i,
            failure,
            success,
            active
        );
    }

    // The Route events prove feedback is flowing. Now check if any node crossed
    // the 50-event threshold for prediction activation.
    let max_per_peer = route_by_peer.values().max().copied().unwrap_or(0);
    tracing::info!("Max route events on a single peer: {}", max_per_peer);

    // Verify enough Route events were generated to make threshold crossing feasible.
    // With 300 iterations across 3 nodes, we expect ~100 per node.
    assert!(
        route_count >= 50,
        "Only {} total Route events from 300 iterations - \
         not enough feedback flowing to the router",
        route_count
    );

    // Check if RouterSnapshot shows prediction_active (if snapshots were captured)
    if !snapshots.is_empty() {
        let any_active = snapshots.iter().any(|(_, _, active)| *active);
        let max_failure_events = snapshots
            .iter()
            .map(|(failure, _, _)| *failure)
            .max()
            .unwrap();

        if any_active {
            tracing::info!(
                "ROUTER THRESHOLD TEST PASSED: prediction activated with {} failure_events (threshold=50)",
                max_failure_events
            );
        } else {
            tracing::info!(
                "RouterSnapshot shows failure_events={} (threshold=50). \
                 Route events per peer: {:?}. \
                 Note: Route events may not all be reflected in failure_events \
                 if the snapshot was captured before all operations completed.",
                max_failure_events,
                route_by_peer
            );
        }
    }

    tracing::info!(
        "ROUTER THRESHOLD TEST PASSED: {} total route events, max {} per peer (threshold=50)",
        route_count,
        max_per_peer
    );
}

// =============================================================================
// Resource Invariant Tests (Stage 2 — Issue #3150)
// =============================================================================

/// Regression guard for #3100: pending_op_results must stay bounded.
/// Verifies that the event loop properly cleans up completed/timed-out
/// transaction callbacks, preventing unbounded HashMap growth.
///
/// Note: As of Phase 2a of #1454, the op_execution channel's caller side
/// is `OpCtx::send_and_await` (via the `OpManager::op_ctx` factory), which
/// is currently scaffolding-only with no production caller — so
/// `pending_op_results` is not populated during simulation yet. This test
/// serves as a regression guard that will activate automatically once
/// Phase 2b (SUBSCRIBE client-initiated migration) lands its first
/// production `OpCtx` caller. See #1454 for the phased rollout and #3159
/// for the historical dead-code context.
#[test]
#[cfg(feature = "simulation_tests")]
fn test_pending_op_results_bounded() {
    let result = TestConfig::medium("pending-op-bounded", 0x3100_0001).run();
    result.assert_ok().verify_state_report();

    let inserts = freenet::config::GlobalTestMetrics::pending_op_inserts();
    let removes = freenet::config::GlobalTestMetrics::pending_op_removes();
    let hwm = freenet::config::GlobalTestMetrics::pending_op_high_water_mark();

    tracing::info!(inserts, removes, hwm, "pending_op_results resource metrics");

    if inserts == 0 {
        // `OpCtx::send_and_await` is Phase 2a scaffolding with no production
        // caller yet; Phase 2b lands the first consumer (#1454).
        tracing::info!(
            "pending_op_results path not exercised (no production OpCtx caller yet — see #1454 phase 2b)"
        );
    }

    assert!(
        hwm <= 100,
        "pending_op_results high-water mark ({hwm}) exceeded bound of 100 — \
         regression of #3100 (unbounded HashMap growth)"
    );
    let leak = inserts.saturating_sub(removes);
    // Threshold previously crept 10 -> 30 -> 60 to absorb simulation-shutdown
    // noise; #4057's `EventListenerState::Drop` now balances the accounting on
    // every event-loop exit path. With the simulation runner on a single
    // `current_thread` runtime (so all peers share one set of thread-local
    // counters with this test), the leak should be exactly zero: any entry
    // resident in the map at exit is counted by Drop, and any entry already
    // gone was counted by whichever code path removed it (TransactionCompleted/
    // TransactionTimedOut handler, periodic cleanup, or graceful Shutdown).
    // Asserting `== 0` rather than `<= N` removes the slow-creep failure mode
    // that drove the original 10 -> 30 -> 60 history.
    assert_eq!(
        leak, 0,
        "pending_op_results leak at shutdown: {leak} entries \
         (inserts={inserts}, removes={removes})"
    );
}

/// Verifies that the proximity cache is exercised and bounded relative to
/// network size during simulation (2 gateways + 6 nodes, 8 contracts).
#[test]
#[cfg(feature = "simulation_tests")]
fn test_neighbor_hosting_bounded() {
    let result = TestConfig::medium("neighbor-hosting-bounded", 0x3100_0002).run();
    result.assert_ok().verify_state_report();

    let updates = freenet::config::GlobalTestMetrics::neighbor_hosting_updates();

    tracing::info!(updates, "neighbor hosting resource metrics");

    assert!(
        updates > 0,
        "Neighbor hosting should have been exercised (updates = 0)"
    );
    // 500 is ~10x expected steady-state for an 8-peer / 8-contract medium network
    assert!(
        updates <= 500,
        "neighbor_hosting_updates ({updates}) exceeded bound of 500 — \
         excessive cache churn for an 8-peer / 8-contract network"
    );
}

/// Validates the PrioritySelectStream anti-starvation mechanism under load.
/// Asserts the mechanism fires at least once and that the simulation converges.
#[test]
#[cfg(feature = "simulation_tests")]
fn test_anti_starvation_exercised() {
    let result = TestConfig::medium("anti-starvation", 0x3094_0002)
        .with_iterations(150)
        .with_max_contracts(10)
        .run();

    let triggers = freenet::config::GlobalTestMetrics::anti_starvation_triggers();
    tracing::info!(triggers, "anti-starvation trigger count");

    result.assert_ok().verify_state_report().check_convergence();
}

// =============================================================================
// Contract Lifecycle Synthetic Tests (Stage 3, #3151)
// =============================================================================

/// Helper: extract per-peer state from logs for a contract and verify propagation.
///
/// Returns the peer states map for further inspection.
fn verify_contract_propagation(
    rt: &tokio::runtime::Runtime,
    logs_handle: &Arc<Mutex<Vec<freenet::tracing::NetLogMessage>>>,
    contract_key: freenet_stdlib::prelude::ContractKey,
    min_peers: usize,
) -> BTreeMap<SocketAddr, String> {
    let contract_key_str = format!("{:?}", contract_key);

    let peer_states: BTreeMap<SocketAddr, String> = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let mut states = BTreeMap::new();
        for log in logs.iter() {
            if let Some(key) = log.kind.contract_key() {
                if format!("{:?}", key) == contract_key_str {
                    if let Some(hash) = log.kind.stored_state_hash() {
                        states.insert(log.peer_id.socket_addr(), hash.to_string());
                    }
                }
            }
        }
        states
    });

    tracing::info!(
        "Contract {} has state on {} peers (need {}): {:?}",
        contract_key_str,
        peer_states.len(),
        min_peers,
        peer_states.keys().collect::<Vec<_>>()
    );

    assert!(
        peer_states.len() >= min_peers,
        "Contract {} propagation failed: only {} peer(s) have state, expected at least {}. \
         Peers with state: {:?}",
        contract_key_str,
        peer_states.len(),
        min_peers,
        peer_states.keys().collect::<Vec<_>>()
    );

    // Verify convergence: all peers should have the same final state hash
    let unique_states: std::collections::HashSet<&String> = peer_states.values().collect();
    assert!(
        unique_states.len() == 1,
        "Contract {} state divergence: {} unique states across {} peers. States: {:?}",
        contract_key_str,
        unique_states.len(),
        peer_states.len(),
        peer_states
    );

    peer_states
}

/// Six-peer multi-contract lifecycle test — direct replacement for River's
/// six-peer regression test.
///
/// Exercises the complete contract lifecycle with realistic topology:
/// 2 gateways + 4 nodes, 2 contracts (simulating 2 "rooms").
///
/// For each contract:
///   1. Gateway 0 PUTs with subscribe=true
///   2. All 5 other peers SUBSCRIBE
///   3. Gateway 0 sends 3 rounds of UPDATEs
///   4. Node 1 sends 2 UPDATEs
///
/// Catches: broadcast targeting failures (#2794), subscription tree formation
/// bugs, multi-contract interference.
#[test_log::test]
fn test_six_peer_contract_lifecycle() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    const SEED: u64 = 0x3151_0001_0001;
    const NETWORK_NAME: &str = "six-peer-lifecycle";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 2, 4, 10, 5, 15, 3, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Create 2 contracts (simulating 2 "rooms")
    let contract_a = SimOperation::create_test_contract(0xA1);
    let contract_a_id = *contract_a.key().id();
    let contract_a_key = contract_a.key();
    register_crdt_contract(contract_a_id);

    let contract_b = SimOperation::create_test_contract(0xB2);
    let contract_b_id = *contract_b.key().id();
    let contract_b_key = contract_b.key();
    register_crdt_contract(contract_b_id);

    let mut operations = Vec::new();

    // For each contract: gateway 0 puts, all others subscribe, then updates
    for (contract, contract_id, contract_key, seed_byte) in [
        (contract_a.clone(), contract_a_id, contract_a_key, 0xA1u8),
        (contract_b.clone(), contract_b_id, contract_b_key, 0xB2u8),
    ] {
        // Gateway 0 puts with subscribe=true
        operations.push(ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: SimOperation::create_crdt_state(1, seed_byte),
                subscribe: true,
            },
        ));

        // All 5 other peers subscribe: gateway 1, nodes 2-5
        // (node indices start at number_of_gateways, so with 2 GWs: 2,3,4,5)
        operations.push(ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ));
        for i in 2..=5 {
            operations.push(ScheduledOperation::new(
                NodeLabel::node(NETWORK_NAME, i),
                SimOperation::Subscribe { contract_id },
            ));
        }

        // Gateway 0 sends 3 rounds of updates (versions 10, 20, 30)
        for v in [10u64, 20, 30] {
            operations.push(ScheduledOperation::new(
                NodeLabel::gateway(NETWORK_NAME, 0),
                SimOperation::Update {
                    key: contract_key,
                    data: SimOperation::create_crdt_state(v, seed_byte),
                },
            ));
        }

        // Node 2 sends 2 updates (versions 40, 50)
        for v in [40u64, 50] {
            operations.push(ScheduledOperation::new(
                NodeLabel::node(NETWORK_NAME, 2),
                SimOperation::Update {
                    key: contract_key,
                    data: SimOperation::create_crdt_state(v, seed_byte),
                },
            ));
        }
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(240),
        Duration::from_secs(90),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Six-peer lifecycle simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // Verify both contracts propagated to at least 4 of 6 peers
    let states_a = verify_contract_propagation(&rt, &logs_handle, contract_a_key, 4);
    let states_b = verify_contract_propagation(&rt, &logs_handle, contract_b_key, 4);

    tracing::info!(
        "test_six_peer_contract_lifecycle PASSED: contract_a on {} peers, contract_b on {} peers",
        states_a.len(),
        states_b.len()
    );
}

/// Same scenario as `test_six_peer_contract_lifecycle` but using MockWasmRuntime.
///
/// Exercises the production `ContractExecutor` code path (init_tracker,
/// validation, notification pipeline) without actual WASM.
#[test_log::test]
fn test_six_peer_lifecycle_mock_wasm() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    const SEED: u64 = 0x3151_0002_0001;
    const NETWORK_NAME: &str = "six-peer-mock-wasm";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (mut sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 2, 4, 10, 5, 15, 3, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });
    sim.use_mock_wasm = true;

    let contract = SimOperation::create_test_contract(0xC3);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    let mut operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: SimOperation::create_crdt_state(1, 0xC3),
            subscribe: true,
        },
    )];

    // All other peers subscribe (node indices start at 2 with 2 gateways)
    operations.push(ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 1),
        SimOperation::Subscribe { contract_id },
    ));
    for i in 2..=5 {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Subscribe { contract_id },
        ));
    }

    // Gateway 0 sends updates
    for v in [10u64, 20, 30] {
        operations.push(ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(v, 0xC3),
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(240),
        Duration::from_secs(90),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "MockWasm lifecycle simulation failed: {:?}",
        result.turmoil_result.err()
    );

    verify_contract_propagation(&rt, &logs_handle, contract_key, 4);

    tracing::info!("test_six_peer_lifecycle_mock_wasm PASSED");
}

/// Verify a node joining after initial updates gets the current state.
///
/// Scenario:
///   1. Gateway PUTs contract with subscribe=true
///   2. Nodes 1-2 SUBSCRIBE
///   3. Gateway UPDATEs to v10, v20
///   4. Node 3 SUBSCRIBES (late joiner)
///   5. Gateway UPDATEs to v30
///
/// The late joiner should receive state via GET during subscribe and then
/// converge with the rest of the network after the final update.
///
/// Catches PR #2360 regression (contract key mismatch on late GET).
#[test_log::test]
fn test_late_joiner_receives_current_state() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    const SEED: u64 = 0x3151_0003_0001;
    const NETWORK_NAME: &str = "late-joiner";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(NETWORK_NAME, 1, 3, 7, 3, 10, 2, SEED).await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xD4);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    let operations = vec![
        // 1. Gateway PUTs contract
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: SimOperation::create_crdt_state(1, 0xD4),
                subscribe: true,
            },
        ),
        // 2. Nodes 1-2 subscribe
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Subscribe { contract_id },
        ),
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 2),
            SimOperation::Subscribe { contract_id },
        ),
        // 3. Gateway updates to v10, v20
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(10, 0xD4),
            },
        ),
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(20, 0xD4),
            },
        ),
        // 4. Node 3 subscribes (late joiner — joins after 2 updates)
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 3),
            SimOperation::Subscribe { contract_id },
        ),
        // 5. Gateway updates to v30 (should reach all including late joiner)
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Update {
                key: contract_key,
                data: SimOperation::create_crdt_state(30, 0xD4),
            },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(90),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Late joiner simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // All 4 peers (gateway + 3 nodes) should converge
    let peer_states = verify_contract_propagation(&rt, &logs_handle, contract_key, 3);

    // Specifically verify node 3 (the late joiner) has the contract state
    // via the shared storage handle, not just log inference.
    let node3_label = NodeLabel::node(NETWORK_NAME, 3);
    let node3_storage = result
        .node_storages
        .get(&node3_label)
        .expect("node 3 should have a storage handle");
    let node3_state = node3_storage.get_stored_state(&contract_key);
    assert!(
        node3_state.is_some(),
        "Late joiner (node 3) should have contract state, but storage is empty. \
         This indicates the subscribe-after-updates path failed to fetch current state."
    );

    tracing::info!(
        "test_late_joiner_receives_current_state PASSED: {} peers converged, \
         late joiner confirmed with state",
        peer_states.len()
    );
}

/// Long-running subscription renewal test — verifies subscriptions survive
/// multiple TTL renewal cycles.
///
/// Uses MockWasmRuntime with enough virtual time for multiple TTL cycles.
/// The test uses random event generation (not controlled) to exercise
/// the subscription renewal path under realistic conditions.
///
/// Catches #3093 (interest TTL not refreshed), subscription tree degradation.
///
/// NOTE: Gated by nightly_tests — does NOT run in regular CI.
#[test_log::test]
#[cfg(feature = "nightly_tests")]
fn test_subscribe_renewal_long_running() {
    const SEED: u64 = 0x3151_0004_0001;

    tracing::info!("=== Starting Subscription Renewal Long-Running Test ===");

    TestConfig::long_running("sub-renewal-long", SEED)
        .with_mock_wasm()
        .run_direct()
        .assert_ok()
        .verify_operation_coverage()
        .verify_state_report();

    tracing::info!("test_subscribe_renewal_long_running PASSED");
}

// =============================================================================
// Operation Success Rate Tests (Stage 4, #3152)
//
// These tests measure operation success rates and assert thresholds,
// catching regressions from topology/routing changes.
//
// Target regressions: #3138 (subscribe rate collapse), #3128/#3136
// (router never learns), #3093 (TTL not refreshed).
// =============================================================================

/// Assert subscribe success rate ≥ 70%.
///
/// Runs a medium network and scans event logs for subscribe outcomes.
/// Catches regressions like #3138 where subscribe success collapsed from 92% to 24%.
///
/// Note: Only counts completed subscribe outcomes (SubscribeSuccess/SubscribeNotFound).
/// Subscribe timeouts are emitted as `EventKind::Timeout` only with the `trace-ot` feature,
/// which is not enabled in simulation tests.
#[test_log::test]
fn test_topology_subscribe_health() {
    const SEED: u64 = 0x3152_0001_0001;

    tracing::info!("=== Starting Topology Subscribe Health Test ===");

    let result = TestConfig::medium("sub-health", SEED)
        .with_iterations(100)
        .with_duration(Duration::from_secs(60))
        .run()
        .assert_ok();

    let rt = create_runtime();
    let (successes, failures) = rt.block_on(async {
        let logs = result.logs_handle.lock().await;
        let mut successes = 0u64;
        let mut failures = 0u64;
        for log in logs.iter() {
            match log.kind.subscribe_outcome() {
                Some(true) => successes += 1,
                Some(false) => failures += 1,
                None => {}
            }
        }
        (successes, failures)
    });

    let total = successes + failures;
    assert!(
        total >= 5,
        "Only {} subscribe outcome events in {} total events — \
         too few for meaningful success rate measurement",
        total,
        result.event_count
    );

    let success_rate = successes as f64 / total as f64;
    tracing::info!(
        "Subscribe health: {}/{} succeeded ({:.1}%)",
        successes,
        total,
        success_rate * 100.0
    );

    assert!(
        success_rate >= 0.70,
        "Subscribe success rate {:.1}% is below 70% threshold \
         ({} succeeded, {} failed out of {} total). \
         See #3138 for context on subscribe rate regressions.",
        success_rate * 100.0,
        successes,
        failures,
        total
    );

    tracing::info!(
        "test_topology_subscribe_health PASSED: {:.1}% success rate",
        success_rate * 100.0
    );
}

/// Verify router receives feedback and doesn't degrade over time.
///
/// Checks that the failure rate in the second half of the simulation is not
/// significantly worse than the first half. If router snapshots are available
/// (emitted every 5 min of virtual time), also logs whether prediction is active.
/// Catches #3128/#3136 (router never learns).
#[test_log::test]
fn test_router_learning() {
    const SEED: u64 = 0x3152_0002_0001;

    tracing::info!("=== Starting Router Learning Test ===");

    let result = TestConfig::small("router-learn", SEED)
        .with_iterations(150)
        .with_duration(Duration::from_secs(200))
        .run()
        .assert_ok();

    // Log router snapshot info if available (snapshots emitted every 5 min,
    // may not fire in short simulations using turmoil's time model)
    let snapshots = result.router_snapshots();
    let any_prediction_active = snapshots.iter().any(|(_f, _s, active)| *active);
    tracing::info!(
        "Router snapshots: {} total, prediction_active in any: {}",
        snapshots.len(),
        any_prediction_active
    );

    // Collect route outcomes and split into first/second half
    let rt = create_runtime();
    let outcomes: Vec<bool> = rt.block_on(async {
        let logs = result.logs_handle.lock().await;
        logs.iter()
            .filter_map(|log| log.kind.route_outcome_is_success())
            .collect()
    });

    assert!(
        outcomes.len() >= 10,
        "Only {} route outcome events — too few for meaningful comparison. \
         Expected at least 10.",
        outcomes.len()
    );

    let mid = outcomes.len() / 2;
    let (first_half, second_half) = outcomes.split_at(mid);

    let first_failures = first_half.iter().filter(|&&s| !s).count();
    let second_failures = second_half.iter().filter(|&&s| !s).count();

    let first_rate = if first_half.is_empty() {
        0.0
    } else {
        first_failures as f64 / first_half.len() as f64
    };
    // second_half is never empty: mid = len/2 < len when len >= 1
    let second_rate = second_failures as f64 / second_half.len() as f64;

    tracing::info!(
        "Route failure rates: first half {:.1}% ({}/{}), second half {:.1}% ({}/{})",
        first_rate * 100.0,
        first_failures,
        first_half.len(),
        second_rate * 100.0,
        second_failures,
        second_half.len()
    );

    // Second half failure rate should not be significantly worse than first half
    assert!(
        second_rate <= first_rate + 0.15,
        "Router is degrading: second-half failure rate ({:.1}%) exceeds \
         first-half ({:.1}%) by more than 15pp. \
         This suggests the router is not learning from feedback. See #3128/#3136.",
        second_rate * 100.0,
        first_rate * 100.0
    );

    tracing::info!("test_router_learning PASSED");
}

/// Verify subscriptions survive across multiple subscription lease cycles.
///
/// SUBSCRIPTION_LEASE_DURATION = 480s (8 min). The simulation runs for
/// 400 iterations * 3s = 1200s (~2.5 lease cycles) and checks that update
/// broadcasts continue arriving in the second half of virtual time.
///
/// Uses `run_direct()` (paused-time single-thread runtime) for efficiency.
/// Virtual time is controlled by iterations * event_wait, not `with_duration()`.
///
/// `no_convergence_wait()`: this test asserts only on broadcast-received events
/// produced during the 1200s event phase, never on final-state convergence, so
/// it skips the direct runner's advisory up-to-1800s convergence-polling tail.
/// When the network happens not to converge, that tail would add 1800s of virtual
/// time (processed in ~1ms quanta) on top of the event phase for no benefit — the
/// exact failure mode that made this test consistently time out in CI (#3792).
///
/// Catches #3093 (interest TTL not refreshed on broadcast send).
#[test_log::test]
fn test_interest_renewal() {
    const SEED: u64 = 0x3152_0003_0001;

    tracing::info!("=== Starting Interest Renewal Test ===");

    let result = TestConfig::medium("interest-renew", SEED)
        .with_nodes(4)
        .with_iterations(400)
        .with_event_wait(Duration::from_secs(3))
        .no_convergence_wait()
        .run_direct()
        .assert_ok();

    // Split broadcast-received events by time (log index as proxy for time order)
    let rt = create_runtime();
    let (early_broadcasts, late_broadcasts) = rt.block_on(async {
        let logs = result.logs_handle.lock().await;
        let log_count = logs.len();
        let mid_index = log_count / 2;

        let mut early = 0usize;
        let mut late = 0usize;
        for (i, log) in logs.iter().enumerate() {
            if log.kind.is_update_broadcast_received() {
                if i < mid_index {
                    early += 1;
                } else {
                    late += 1;
                }
            }
        }
        (early, late)
    });

    let total = early_broadcasts + late_broadcasts;
    tracing::info!(
        "Broadcast received events: {} total (early: {}, late: {})",
        total,
        early_broadcasts,
        late_broadcasts
    );

    assert!(
        total > 0,
        "No BroadcastReceived events found in {} events — \
         simulation may not be generating updates",
        result.event_count
    );

    assert!(
        late_broadcasts > 0,
        "No BroadcastReceived events in second half of simulation. \
         Subscriptions may have expired without renewal. See #3093."
    );

    let ratio = late_broadcasts as f64 / early_broadcasts.max(1) as f64;
    tracing::info!(
        "Late/early broadcast ratio: {:.2} ({}/{})",
        ratio,
        late_broadcasts,
        early_broadcasts
    );

    assert!(
        ratio > 0.2,
        "Late broadcast ratio ({:.2}) is below 0.2 threshold — \
         subscriptions are likely degrading across lease cycles. See #3093.",
        ratio
    );

    tracing::info!(
        "test_interest_renewal PASSED: {:.2} late/early ratio",
        ratio
    );
}

/// Isolated integration test for interest TTL refresh on broadcast send.
///
/// Validates the fix for #3093: interest entries for peers receiving full-state
/// broadcasts must have their TTL refreshed on each successful send. Without
/// this refresh, subscriptions expire after INTEREST_TTL (20 min) even though
/// broadcasts are being delivered, causing ~49% subscriber drop.
///
/// ## Test Design
///
/// Uses a minimal network (1 gateway + 2 nodes) with few contracts to
/// isolate the TTL refresh mechanism. Virtual time spans ~1.5x INTEREST_TTL
/// (1800s) so that without the broadcast-send TTL refresh, interest entries
/// would expire and late-phase broadcasts would stop arriving.
///
/// The test splits broadcast-received events into three phases:
/// - **Early** (first third): baseline broadcast delivery
/// - **Mid** (second third): crosses the TTL boundary (~1200s)
/// - **Late** (final third): must still receive broadcasts if TTL was refreshed
///
/// ## What This Catches
///
/// - Missing `refresh_peer_interest()` call in broadcast send path (p2p_protoc.rs)
/// - TTL expiration causing silent subscriber loss
/// - Regression of the #3093 fix
///
/// ## Related
///
/// - Issue #3093: Interest TTL not refreshed on full-state broadcast
/// - Issue #3107: Add isolated integration test (this test)
/// - Issue #3141: CI & Testing Redesign
/// - `test_interest_renewal`: Scale test covering the same mechanism
///
/// Uses `run_direct()` (paused-time single-thread runtime) for efficiency.
/// Virtual time: 300 iterations × 6s = 1800s (~1.5× INTEREST_TTL).
/// Wall clock: typically < 15s.
#[test_log::test]
fn test_interest_ttl_refresh_on_broadcast() {
    const SEED: u64 = 0x3107_0BCA_0001;

    tracing::info!("=== Starting Interest TTL Refresh on Broadcast Test ===");
    // INTEREST_TTL = 1200s (20 min). Virtual time = 300 × 6s = 1800s (~1.5× TTL).
    tracing::info!("Virtual time target: 1800s (~1.5x INTEREST_TTL of 1200s)");

    let result = TestConfig::small("ttl-refresh-bcast", SEED)
        .with_gateways(1)
        .with_nodes(2) // Minimal network: 1 gateway + 2 nodes
        .with_max_contracts(2) // Few contracts → more updates per contract
        .with_iterations(300) // 300 × 6s = 1800s virtual time
        .with_event_wait(Duration::from_secs(6))
        .run_direct()
        .assert_ok();

    // Analyze broadcast-received events across three phases of virtual time.
    // The TTL boundary is at ~1200s (INTEREST_TTL). If refresh is working,
    // broadcasts should continue in the late phase (1200s-1800s).
    let rt = create_runtime();
    let (early_broadcasts, mid_broadcasts, late_broadcasts) = rt.block_on(async {
        let logs = result.logs_handle.lock().await;
        let log_count = logs.len();
        let third = log_count / 3;

        let mut early = 0usize;
        let mut mid = 0usize;
        let mut late = 0usize;
        for (i, log) in logs.iter().enumerate() {
            if log.kind.is_update_broadcast_received() {
                if i < third {
                    early += 1;
                } else if i < third * 2 {
                    mid += 1;
                } else {
                    late += 1;
                }
            }
        }
        (early, mid, late)
    });

    let total = early_broadcasts + mid_broadcasts + late_broadcasts;
    tracing::info!(
        "Broadcast received events: {} total (early: {}, mid: {}, late: {})",
        total,
        early_broadcasts,
        mid_broadcasts,
        late_broadcasts
    );

    // Must have some broadcasts overall — otherwise the simulation didn't
    // generate enough update activity to be meaningful.
    assert!(
        total > 0,
        "No BroadcastReceived events found in {} logged events — \
         simulation may not be generating updates. Seed: 0x{:X}",
        result.event_count,
        SEED
    );

    // CRITICAL: Late-phase broadcasts must exist. If the TTL refresh on
    // broadcast send is missing (regression of #3093), interest entries
    // expire at ~1200s and no broadcasts are delivered after that point.
    assert!(
        late_broadcasts > 0,
        "No BroadcastReceived events in the final third of simulation \
         (after INTEREST_TTL boundary). Interest TTL is NOT being refreshed \
         on broadcast send — subscriptions have silently expired. \
         See #3093, #3107. Seed: 0x{:X}",
        SEED
    );

    // The late/early ratio should be meaningful — at least 10% of early
    // traffic. A drastic drop indicates partial TTL refresh failure.
    let late_ratio = late_broadcasts as f64 / early_broadcasts.max(1) as f64;
    tracing::info!(
        "Late/early broadcast ratio: {:.2} ({}/{})",
        late_ratio,
        late_broadcasts,
        early_broadcasts
    );

    assert!(
        late_ratio > 0.1,
        "Late broadcast ratio ({:.2}) dropped below 0.1 — \
         interest TTL refresh may be partially broken. \
         Early: {}, Mid: {}, Late: {}. See #3093, #3107. Seed: 0x{:X}",
        late_ratio,
        early_broadcasts,
        mid_broadcasts,
        late_broadcasts,
        SEED
    );

    tracing::info!(
        "test_interest_ttl_refresh_on_broadcast PASSED: late/early ratio {:.2}, \
         total broadcasts: {} (early: {}, mid: {}, late: {})",
        late_ratio,
        total,
        early_broadcasts,
        mid_broadcasts,
        late_broadcasts
    );
}

// =============================================================================
// Thundering Herd CONNECT Storm Regression Test (Issue #3207, PR #3208)
// =============================================================================

/// Helper to advance virtual time in steps while yielding to tokio.
///
/// This is the same pattern used in `simulation_smoke.rs` and `state_verification.rs`,
/// duplicated here because it is test-file-scoped.
async fn let_network_run(sim: &mut SimNetwork, duration: Duration) {
    let step = Duration::from_millis(100);
    let mut elapsed = Duration::ZERO;
    while elapsed < duration {
        sim.advance_time(step);
        tokio::task::yield_now().await;
        tokio::time::sleep(Duration::from_millis(10)).await;
        elapsed += step;
    }
}

/// Regression test for thundering herd CONNECT storm after gateway restart.
///
/// **Background (Issue #3207):**
/// When a gateway restarts, all peers reconnect simultaneously, generating a
/// burst of CONNECT operations that can overwhelm per-connection inbound
/// channels (see `INBOUND_CHANNEL_CAPACITY`), causing a non-recovering
/// drop→retransmit feedback loop. PR #3208 fixed the underlying inbound
/// starvation bug.
///
/// **Test scenario:**
/// 1. Create a 1-gateway + 20-node network, let it stabilize
/// 2. Crash the gateway (all peers lose connections)
/// 3. Restart the gateway (triggers thundering herd reconnection)
/// 4. Verify the network recovers (no non-recovering overflow)
///
/// Uses the async direct-control pattern (like `test_suspend_resume_zombie_connections`)
/// because `restart_node` requires `&mut SimNetwork`.
#[test_log::test(tokio::test(flavor = "current_thread"))]
async fn test_thundering_herd_connect_storm() {
    const SEED: u64 = 0x3207_0000_3208;
    const NETWORK_NAME: &str = "thundering-herd-connect";

    tracing::info!("=== Thundering Herd CONNECT Storm Test (Issue #3207, PR #3208) ===");

    // 1 gateway, 20 nodes — single bottleneck topology
    let mut sim = SimNetwork::new(NETWORK_NAME, 1, 20, 7, 3, 10, 2, SEED).await;
    sim.with_start_backoff(Duration::from_millis(50));

    let _handles = sim
        .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 5, 10)
        .await;

    let logs_handle = sim.event_logs_handle();

    // Phase 1: Stabilize
    tracing::info!("Phase 1: Stabilizing network (5s)");
    let_network_run(&mut sim, Duration::from_secs(5)).await;

    sim.check_partial_connectivity(Duration::from_secs(20), 0.8)
        .await
        .expect("Network should reach ≥80% connectivity before crash");
    tracing::info!("Phase 1 complete: network connected");

    // Record baseline Connect event count
    let baseline_connects = {
        let logs = logs_handle.lock().await;
        logs.iter().filter(|log| log.kind.is_connect()).count()
    };
    tracing::info!("Baseline Connect events: {}", baseline_connects);

    // Phase 2: Crash the gateway
    let gateway_label = sim
        .all_node_addresses()
        .keys()
        .find(|label| label.is_gateway())
        .cloned()
        .expect("Should have a gateway");

    tracing::info!(?gateway_label, "Phase 2: Crashing gateway");
    let crashed = sim.crash_node(&gateway_label);
    assert!(crashed, "Gateway should crash successfully");

    // Phase 3: Let peers detect the dead gateway
    tracing::info!("Phase 3: Peers detecting dead gateway (10s)");
    let_network_run(&mut sim, Duration::from_secs(10)).await;

    // Phase 4: Restart gateway — all peers reconnect at once
    tracing::info!("Phase 4: Restarting gateway (thundering herd begins)");
    let restart_seed = SEED.wrapping_add(0x1000);
    let handle = sim
        .restart_node::<rand::rngs::SmallRng>(&gateway_label, restart_seed, 5, 5)
        .await;
    assert!(handle.is_some(), "Gateway should restart successfully");

    // Phase 5: Let the reconnection storm play out
    tracing::info!("Phase 5: Reconnection storm playing out (30s)");
    let_network_run(&mut sim, Duration::from_secs(30)).await;

    // Phase 6: Verify recovery
    tracing::info!("Phase 6: Verifying network recovery");

    // 6a: Log reconnection activity for diagnostics
    let final_connects = {
        let logs = logs_handle.lock().await;
        logs.iter().filter(|log| log.kind.is_connect()).count()
    };
    let storm_connects = final_connects - baseline_connects;
    tracing::info!(
        "Connect events after restart: {} (baseline: {}, storm: {})",
        final_connects,
        baseline_connects,
        storm_connects
    );
    // Note: The gateway rate limiter (GatewayConnectionRateLimiter) intentionally
    // throttles the thundering herd to 5 connections/sec initially, ramping up over
    // 2 minutes. Its admission ramp advances on the simulation clock
    // (max(real_elapsed, virtual_elapsed); see GatewayConnectionRateLimiter), so the
    // throttle still applies as virtual time advances but fewer connections complete
    // per window than the 20 peers attempting to reconnect. This is the desired
    // behavior: the storm is prevented, not just survived.

    // 6b: Verify network recovered (the actual regression check for #3207/#3208)
    sim.check_partial_connectivity(Duration::from_secs(20), 0.8)
        .await
        .expect(
            "Network should recover to ≥80% connectivity after gateway restart. \
             The rate limiter should throttle but not prevent reconnection.",
        );

    tracing::info!(
        "test_thundering_herd_connect_storm PASSED: {} Connect events after restart, \
         network recovered to ≥80% connectivity",
        storm_connects
    );
}

// =============================================================================
// Regression: Gateway re-bootstrap after total isolation (#3219)
// =============================================================================

/// Tests that a node isolated to zero connections re-bootstraps via gateways.
///
/// ## Scenario
/// 1. Create a network (1 gateway + 4 nodes)
/// 2. Let it stabilize with contracts propagated
/// 3. Partition ONE non-gateway node from ALL other nodes
/// 4. Wait for the isolated node to lose all ring connections
/// 5. Heal the partition
/// 6. Assert the node recovers (contracts converge)
///
/// Without the fix in #3219 (gateway bootstrap fallback in connection_maintenance),
/// the isolated node cannot call acquire_new (no routing candidates at zero
/// connections) and remains permanently disconnected.
#[test_log::test]
fn test_isolated_node_rebootstraps_via_gateway() {
    use freenet::dev_tool::NodeLabel;
    use freenet::simulation::Partition;

    const SEED: u64 = 0x3219_B007_0001;
    const NETWORK_NAME: &str = "isolated-rebootstrap";

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle, node_addrs) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,  // 1 gateway
            4,  // 4 nodes (5 total)
            7,  // ring_max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        let node_addrs: HashMap<NodeLabel, std::net::SocketAddr> = sim.all_node_addresses().clone();
        (sim, logs_handle, node_addrs)
    });

    // Pick one non-gateway node to isolate
    let (isolated_label, isolated_addr) = node_addrs
        .iter()
        .find(|(label, _)| label.is_node())
        .map(|(l, a)| (l.clone(), *a))
        .expect("Need at least one non-gateway node");

    // side_a = just the isolated node, side_b = everyone else
    let side_a: std::collections::HashSet<_> = [isolated_addr].into_iter().collect();
    let side_b: std::collections::HashSet<_> = node_addrs
        .iter()
        .filter(|(label, _)| **label != isolated_label)
        .map(|(_, addr)| *addr)
        .collect();

    tracing::info!(
        isolated = %isolated_label,
        isolated_addr = %isolated_addr,
        rest_count = side_b.len(),
        "Will isolate one node from all others"
    );

    let network_name = NETWORK_NAME.to_string();

    // iterations=80 gives enough gen_event budget (80/5 peers = 16 iterations/peer)
    let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
        SEED,
        3,                          // contracts
        80,                         // iterations (also per-peer gen_event budget)
        Duration::from_secs(120),   // simulation_duration
        Duration::from_millis(500), // event_wait
        move || async move {
            // Phase 1: Events have been firing, contracts propagated. Now isolate one node.
            tracing::info!("Injecting partition: isolating one node from all others");

            if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                let mut state = injector.lock().unwrap();
                let partition = Partition::new(side_a, side_b).permanent(0);
                state.config.add_partition(partition);
            }

            // Phase 2: Wait for existing connections to time out.
            // Connection liveness checks run every ~5s (fast tick), connections
            // are pruned after missing pings. 20s is enough for full isolation.
            tokio::time::sleep(Duration::from_secs(20)).await;
            tracing::info!("Isolation period over — node should have zero connections now");

            // Phase 3: Heal the partition. The gateway bootstrap fallback in
            // connection_maintenance should kick in and reconnect the node.
            if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                let mut state = injector.lock().unwrap();
                state.config.partitions.clear();
            }
            tracing::info!("Partition healed — waiting for re-bootstrap");

            // Phase 4: Wait for the node to re-bootstrap via gateway and
            // converge state. connection_maintenance runs on 5s tick, so
            // the gateway fallback should trigger quickly.
            tokio::time::sleep(Duration::from_secs(30)).await;
            tracing::info!("Post-heal convergence period complete");

            Ok(())
        },
    );

    assert!(
        result.is_ok(),
        "Isolated-node re-bootstrap simulation failed: {:?}",
        result.err()
    );

    // Check convergence — if the isolated node re-bootstrapped, it should
    // have re-subscribed and received contract state updates.
    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    tracing::info!(
        "Isolated-rebootstrap: {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    // Hard assertion: the isolated node must have re-bootstrapped and
    // converged state. Without the gateway bootstrap fallback, the node
    // stays permanently disconnected and contracts diverge.
    assert!(
        convergence.diverged.is_empty(),
        "Isolated node failed to re-bootstrap: {} contracts diverged (expected 0). \
         This indicates the gateway bootstrap fallback in connection_maintenance \
         did not recover the node after partition heal.",
        convergence.diverged.len()
    );

    // Run anomaly detection
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });

    tracing::info!(
        "=== ISOLATED-REBOOTSTRAP ANOMALY REPORT: {} events, {} state, {} contracts, {} anomalies ===",
        report.total_events,
        report.state_events,
        report.contracts_analyzed,
        report.anomalies.len()
    );

    let divergences = report.divergences();
    let stale = report.stale_peers();

    tracing::warn!("  divergences={}, stale={}", divergences.len(), stale.len(),);

    for (i, anomaly) in report.anomalies.iter().enumerate() {
        tracing::debug!("  anomaly[{}] = {:?}", i, anomaly);
    }
}

// =============================================================================
// Direct Runner: Churn Resilience
// =============================================================================

/// Verifies that the direct runner can complete a simulation with node churn enabled.
///
/// Runs 2 gateways + 8 nodes with 20% crash rate, 200ms ticks, 500ms recovery.
/// Asserts the simulation completes without panicking and produces events.
#[test]
fn test_direct_runner_churn() {
    use freenet::dev_tool::ChurnConfig;

    const SEED: u64 = 0xC1_0055_FEED;

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let mut sim = rt.block_on(async {
        SimNetwork::new(
            "test-churn",
            2,  // gateways
            8,  // nodes
            10, // ring_max_htl
            5,  // rnd_if_htl_above
            15, // max_connections
            3,  // min_connections
            SEED,
        )
        .await
    });

    sim.with_churn(ChurnConfig {
        crash_probability: 0.20,
        tick_interval: Duration::from_millis(200),
        recovery_delay: Duration::from_millis(500),
        max_simultaneous_crashes: Some(2),
        permanent_crash_rate: 0.05,
        // Must be >= connection wait (2s) + buffer to avoid crashing nodes during startup
        warmup_delay: Duration::from_secs(5),
    });

    let logs_handle = sim.event_logs_handle();

    drop(rt);

    sim.run_simulation_direct::<rand::rngs::SmallRng>(
        SEED,
        10, // max_contract_num
        30, // iterations
        Duration::from_millis(200),
    )
    .expect("Direct simulation with churn should complete without panic");

    // Verify events were produced despite churn
    let rt = create_runtime();
    let event_count = rt.block_on(async {
        let logs = logs_handle.lock().await;
        logs.len()
    });

    assert!(
        event_count > 0,
        "Simulation with churn should produce events, got 0"
    );

    tracing::info!(
        "CHURN TEST PASSED: {} events produced with node churn enabled",
        event_count
    );
}

/// Regression test for #4694: direct-runner `ChurnConfig` crashes must actually
/// DROP packets, not merely set fault config.
///
/// Before the fix, `run_simulation_direct` never installed the global
/// packet-delivery callback and never set `enforce_fault_drops`, so the chaos
/// driver's `crash_node()` calls were inert: a "crashed" node kept exchanging
/// packets. Any near-K churn / partition validation on the direct runner was
/// therefore false-green — it asserted recovery from a fault that never
/// happened. This blocked trusting the demand-driven-hosting release-2 gate
/// (#4642), whose churn validation runs on this exact runner.
///
/// The discriminating signal is the fault injector's `messages_dropped_crash`
/// counter: it is `> 0` only if a crash was scheduled AND real traffic to/from
/// the crashed node was subsequently blocked by the delivery callback. We use
/// permanent crashes so a downed node stays down and keeps dropping traffic
/// (its own event loop keeps trying to send, and neighbours keep routing to
/// it), guaranteeing drops accumulate once faults are actually enforced.
///
/// - RED (pre-fix): callback never installed / never enforced → counter stays
///   exactly `0` → assertion fails.
/// - GREEN (post-fix): crashed-node packets are dropped → counter `> 0`.
#[test]
fn test_direct_runner_churn_actually_drops_packets() {
    use freenet::dev_tool::ChurnConfig;

    const SEED: u64 = 0xC1_0055_D009;
    const NETWORK_NAME: &str = "test-churn-drops";

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let mut sim = rt.block_on(async {
        SimNetwork::new(
            NETWORK_NAME,
            2,  // gateways
            8,  // nodes
            10, // ring_max_htl
            5,  // rnd_if_htl_above
            15, // max_connections
            3,  // min_connections
            SEED,
        )
        .await
    });

    // Aggressive, permanent churn so at least one node is genuinely crashed
    // shortly after warmup and stays down accumulating dropped packets.
    sim.with_churn(ChurnConfig {
        crash_probability: 0.5,
        tick_interval: Duration::from_millis(200),
        recovery_delay: Duration::from_millis(500),
        max_simultaneous_crashes: Some(3),
        permanent_crash_rate: 1.0, // every crash is permanent → stays down
        // Must be >= connection wait (2s) + buffer to avoid crashing nodes during startup
        warmup_delay: Duration::from_secs(5),
    });

    // Retain a clone of this network's fault injector Arc BEFORE the run.
    // `run_simulation_direct` consumes `sim`; on drop, `SimNetwork::Drop` removes
    // the injector from the global registry, but our retained Arc keeps the same
    // underlying `FaultInjectorState` alive — and the chaos driver + delivery
    // callback mutate that same state during the run, so we can read the final
    // drop count afterwards.
    let injector = freenet::dev_tool::get_fault_injector(NETWORK_NAME)
        .expect("fault injector is registered at SimNetwork construction");

    let logs_handle = sim.event_logs_handle();

    drop(rt);

    sim.run_simulation_direct::<rand::rngs::SmallRng>(
        SEED,
        10, // max_contract_num
        30, // iterations
        Duration::from_millis(200),
    )
    .expect("Direct simulation with churn should complete without panic");

    let crash_drops = injector.lock().unwrap().stats.messages_dropped_crash;

    // Sanity: the run actually did something.
    let rt = create_runtime();
    let event_count = rt.block_on(async { logs_handle.lock().await.len() });
    assert!(event_count > 0, "churn simulation produced no events");

    assert!(
        crash_drops > 0,
        "expected crashed-node packets to be DROPPED (messages_dropped_crash > 0), \
         but got {crash_drops}. Fault injection is inert on the direct runner — the \
         packet-delivery callback was not installed / enforced (regression of #4694)."
    );

    tracing::info!(
        "CHURN DROP TEST PASSED: {crash_drops} packets dropped for crashed nodes \
         ({event_count} events produced)"
    );
}

// =============================================================================
// Unhealthy Peer Eviction Tests
// =============================================================================

/// Verifies that partitioned (dead) peers are eventually evicted from the ring
/// via the PeerHealthTracker, and that zombie transports don't accumulate.
///
/// Creates a network, partitions one node, runs contract operations so the
/// partitioned node accumulates routing failures, then checks that the network
/// continues to function (the partitioned node doesn't permanently block routing).
#[test_log::test]
fn test_unhealthy_peer_eviction() {
    use freenet::dev_tool::NodeLabel;
    use freenet::simulation::Partition;

    const SEED: u64 = 0xDEAD_BEEF_0042;
    const NETWORK_NAME: &str = "unhealthy-eviction";

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle, node_addrs) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1, // 1 gateway
            4, // 4 nodes (5 total)
            7,
            3,
            10,
            2,
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        let node_addrs: HashMap<NodeLabel, SocketAddr> = sim.all_node_addresses().clone();
        (sim, logs_handle, node_addrs)
    });

    // Pick one non-gateway node to partition
    let victim_addr: SocketAddr = *node_addrs
        .iter()
        .find(|(label, _)| label.is_node())
        .expect("Need at least 1 non-gateway node")
        .1;

    let all_addrs: HashSet<_> = node_addrs.values().copied().collect();
    let victim_set: HashSet<_> = [victim_addr].into_iter().collect();
    let healthy_set: HashSet<_> = all_addrs.difference(&victim_set).copied().collect();

    let network_name = NETWORK_NAME.to_string();

    // Use long event_wait to give virtual time for health checks to fire.
    // 150 iterations × 2s = 300s virtual time (5 min) — enough for health check interval.
    let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
        SEED,
        5,                        // contracts
        150,                      // iterations
        Duration::from_secs(600), // simulation_duration (10 min headroom)
        Duration::from_secs(2),   // event_wait (2s between events)
        move || async move {
            // Phase 1: Partition the victim node from everyone else
            tracing::info!("Partitioning victim node: {}", victim_addr);

            if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                let mut state = injector.lock().unwrap();
                let partition = Partition::new(victim_set, healthy_set).permanent(0);
                state.config.add_partition(partition);
            }

            // Phase 2: Let the network operate with the partitioned node.
            // The healthy nodes should accumulate routing failures when trying
            // to use the partitioned node, and eventually the health tracker
            // should flag it as unhealthy.
            tokio::time::sleep(Duration::from_secs(120)).await;
            tracing::info!("Partition active for 120s virtual time");

            Ok(())
        },
    );

    assert!(
        result.is_ok(),
        "Unhealthy-peer eviction simulation failed: {:?}",
        result.err()
    );

    // Verify the network produced events (it didn't deadlock)
    let event_count = rt.block_on(async { logs_handle.lock().await.len() });
    assert!(event_count > 0, "Simulation should produce events, got 0");

    // Run anomaly detection
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });

    tracing::info!(
        "=== UNHEALTHY EVICTION REPORT: {} events, {} state, {} contracts, {} anomalies ===",
        report.total_events,
        report.state_events,
        report.contracts_analyzed,
        report.anomalies.len()
    );
}

// =============================================================================
// Readiness Gating Tests
// =============================================================================

/// Verifies that readiness gating (min_ready_connections=3) does not block
/// contract operations once the network has stabilized.
///
/// Runs 1 gateway + 4 nodes with readiness gating enabled. If the periodic
/// re-broadcast and optimistic timeout work correctly, all nodes should
/// eventually become ready and contract operations should succeed.
#[test]
fn test_readiness_gating_production_config() {
    const SEED: u64 = 0xBEAD_10A7_E001;

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let mut sim = rt.block_on(async {
        SimNetwork::new(
            "test-readiness-gating",
            1,  // gateways
            4,  // nodes
            10, // ring_max_htl
            5,  // rnd_if_htl_above
            10, // max_connections
            3,  // min_connections
            SEED,
        )
        .await
    });

    sim.with_readiness_gating(3);

    let logs_handle = sim.event_logs_handle();

    drop(rt);

    sim.run_simulation_direct::<rand::rngs::SmallRng>(
        SEED,
        5,  // max_contract_num
        20, // iterations (enough for contract creation + operations)
        Duration::from_millis(200),
    )
    .expect("Simulation with readiness gating should complete without panic");

    let rt = create_runtime();
    let event_count = rt.block_on(async {
        let logs = logs_handle.lock().await;
        logs.len()
    });

    assert!(
        event_count > 0,
        "Simulation with readiness gating should produce events, got 0"
    );

    tracing::info!(
        "READINESS GATING TEST PASSED: {} events produced with gating(3)",
        event_count
    );
}

/// Same as `test_readiness_gating_production_config` but with 5% message loss.
///
/// This validates that the periodic ReadyState re-broadcast and optimistic
/// timeout handle lost messages gracefully.
#[test]
fn test_readiness_gating_with_message_loss() {
    use freenet::simulation::FaultConfig;

    const SEED: u64 = 0xBEAD_10A7_E002;

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let mut sim = rt.block_on(async {
        SimNetwork::new(
            "test-readiness-gating-loss",
            1,  // gateways
            4,  // nodes
            10, // ring_max_htl
            5,  // rnd_if_htl_above
            10, // max_connections
            3,  // min_connections
            SEED,
        )
        .await
    });

    sim.with_readiness_gating(3);
    sim.with_fault_injection(FaultConfig::builder().message_loss_rate(0.05).build());

    let logs_handle = sim.event_logs_handle();

    drop(rt);

    sim.run_simulation_direct::<rand::rngs::SmallRng>(
        SEED,
        5,  // max_contract_num
        25, // iterations (extra budget for retries under loss)
        Duration::from_millis(200),
    )
    .expect("Simulation with readiness gating + message loss should complete");

    let rt = create_runtime();
    let event_count = rt.block_on(async {
        let logs = logs_handle.lock().await;
        logs.len()
    });

    assert!(
        event_count > 0,
        "Simulation with readiness gating + message loss should produce events, got 0"
    );

    tracing::info!(
        "READINESS GATING + MESSAGE LOSS TEST PASSED: {} events",
        event_count
    );
}

// =============================================================================
// Gateway Version Probe Tests
// =============================================================================

/// Verify that the periodic gateway version probe does not break
/// connection_maintenance. Actual version mismatch detection is not testable
/// in simulation (all nodes share the same PROTOC_VERSION). (#3677)
#[test]
fn test_gateway_version_probe_fires() {
    const SEED: u64 = 0x6A7E_7AE9_0001;

    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let sim = rt.block_on(async {
        SimNetwork::new(
            "test-gw-version-probe",
            1,  // gateways
            3,  // nodes
            7,  // ring_max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await
    });

    let logs_handle = sim.event_logs_handle();

    drop(rt);

    sim.run_simulation_direct::<rand::rngs::SmallRng>(SEED, 3, 30, Duration::from_secs(1))
        .expect("Simulation should complete without panic");

    let rt = create_runtime();
    let unique_connect_txs = rt.block_on(async {
        let logs = logs_handle.lock().await;
        // Count distinct Connect transactions, not raw events. Each CONNECT op
        // emits multiple log entries (sent/received/accepted) that share a Tx.
        // Counting unique Tx values gives us the number of *operations*, which
        // is what the probe code path actually generates.
        let mut seen = std::collections::HashSet::new();
        for log in logs.iter() {
            if log.kind.variant_name() == "Connect" {
                seen.insert(log.tx);
            }
        }
        seen.len()
    });

    // Bootstrap baseline: each of the 3 non-gateway nodes initiates an initial
    // CONNECT to the gateway, so the bootstrap floor is 3 unique transactions.
    // (Bootstrap may also retry on failure, but that just adds to the floor.)
    //
    // Probe budget: cfg(test) sets GATEWAY_VERSION_PROBE_INTERVAL to 10s with
    // a random initial delay in [0, 10)s and ±20% jitter per cycle. Over 30s
    // of sim time, each non-gateway can fire 1–3 probes, so 3 nodes contribute
    // an additional 3–9 unique CONNECT transactions on top of bootstrap.
    //
    // Asserting > 6 (bootstrap floor + at least one probe per node) is robust
    // against the random initial delay while still proving probes fire.
    assert!(
        unique_connect_txs > 6,
        "Expected bootstrap (~3) + at least 4 probe CONNECTs, got {unique_connect_txs}"
    );
}

// =============================================================================
// CONNECT Acceptor Diversity: Joiner succeeds despite NAT-blocked acceptor
// =============================================================================

/// Tests that a joiner can connect to the network even when partitioned from
/// some peers (simulating NAT hole-punch failure).
///
/// ## Scenario
/// 1. Create a 1-gateway + 5-node network
/// 2. Let the network bootstrap normally (all nodes connected)
/// 3. Inject a partition between 2 specific non-gateway nodes
///    (simulates NAT incompatibility between those peers)
/// 4. Continue operating — peers in the partitioned pair can't reach each other
///    but should still connect via other paths using ConnectFailed re-routing
/// 5. Verify the simulation completes without hanging (nodes don't get stuck
///    retrying the same unreachable acceptor forever)
///
/// Without ConnectFailed + jitter, a joiner partitioned from its nearest
/// acceptor would retry the same peer indefinitely. With the fix, ConnectFailed
/// propagates through the relay chain, causing re-routing to different acceptors,
/// and location jitter explores different ring regions on retry.
#[test_log::test]
fn test_connect_despite_nat_partition() {
    use freenet::dev_tool::NodeLabel;
    use freenet::simulation::Partition;

    const SEED: u64 = 0xC0DE_FA11_0001;
    const NETWORK_NAME: &str = "nat-partition";

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let (sim, logs_handle, node_addrs) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,  // 1 gateway
            5,  // 5 regular nodes (6 total)
            7,  // max HTL
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        let node_addrs: HashMap<NodeLabel, std::net::SocketAddr> = sim.all_node_addresses().clone();
        (sim, logs_handle, node_addrs)
    });

    // Pick two non-gateway nodes to partition from each other.
    // This simulates NAT incompatibility: hole-punch between these two peers
    // always fails, but both can reach the rest of the network.
    let mut non_gw_nodes: Vec<_> = node_addrs
        .iter()
        .filter(|(label, _)| label.is_node())
        .collect();
    non_gw_nodes.sort_by_key(|(label, _)| label.number());

    assert!(
        non_gw_nodes.len() >= 2,
        "Need at least 2 non-gateway nodes for partition"
    );

    let node_a_addr = *non_gw_nodes[0].1;
    let node_b_addr = *non_gw_nodes[1].1;

    let side_a: HashSet<std::net::SocketAddr> = [node_a_addr].into_iter().collect();
    let side_b: HashSet<std::net::SocketAddr> = [node_b_addr].into_iter().collect();

    let network_name = NETWORK_NAME.to_string();

    tracing::info!(
        node_a = %node_a_addr,
        node_b = %node_b_addr,
        "Will inject NAT partition between two nodes"
    );

    // Inject the partition from the test closure (like test_partition_heal_convergence)
    // to preserve the properly-configured fault injector with VirtualTime.
    // The partition is injected immediately and kept permanent — simulating
    // persistent NAT incompatibility between two specific peers.
    let result = sim.run_simulation::<rand::rngs::SmallRng, _, _>(
        SEED,
        3,                          // contracts
        100,                        // iterations (generous budget)
        Duration::from_secs(120),   // simulation_duration
        Duration::from_millis(500), // event_wait
        move || async move {
            // Inject the permanent partition immediately
            if let Some(injector) = freenet::dev_tool::get_fault_injector(&network_name) {
                let mut state = injector.lock().unwrap();
                let partition = Partition::new(side_a, side_b).permanent(0);
                state.config.add_partition(partition);
                tracing::info!("NAT partition injected between two nodes");
            }

            // Let the network operate with the partition active
            tokio::time::sleep(Duration::from_secs(30)).await;
            tracing::info!("NAT partition test: observation period complete");
            Ok(())
        },
    );

    assert!(
        result.is_ok(),
        "NAT partition simulation failed (nodes may have gotten stuck retrying \
         unreachable acceptor): {:?}",
        result.err()
    );

    // Check convergence — with the partition, some divergence is expected
    // between the two partitioned nodes, but the network should still function.
    let convergence = rt.block_on(async { check_convergence_from_logs(&logs_handle).await });
    tracing::info!(
        "NAT partition: {} converged, {} diverged",
        convergence.converged.len(),
        convergence.diverged.len()
    );

    // Run anomaly detection
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });

    tracing::info!(
        "=== NAT PARTITION ANOMALY REPORT: {} events, {} state, {} contracts, {} anomalies ===",
        report.total_events,
        report.state_events,
        report.contracts_analyzed,
        report.anomalies.len()
    );

    // Log anomaly details for debugging
    let divergences = report.divergences();
    let stale = report.stale_peers();
    tracing::warn!("  divergences={}, stale={}", divergences.len(), stale.len(),);

    for (i, anomaly) in report.anomalies.iter().enumerate() {
        tracing::debug!("  anomaly[{}] = {:?}", i, anomaly);
    }

    // The simulation completing without timeout is the primary assertion:
    // without ConnectFailed + jitter, partitioned joiners would retry the same
    // unreachable acceptor forever, eventually causing the simulation to hang
    // or timeout.
}

// =============================================================================
// Connection Growth Stall Regression Test (PRs #3408, #3398, #3396, #3380)
// =============================================================================

/// Regression test: connection growth stall observed on live Freenet network.
///
/// **Background:**
/// Nodes get ~10 connections (including 2 gateway transient connections) but never
/// grow beyond that, continuously reconnecting to gateways. Root causes:
///
/// - `BOOTSTRAP_THRESHOLD` was hardcoded to 4, stopping gateway-directed CONNECTs
///   far below `min_connections`. Fixed: use `min_connections` as the threshold.
/// - `acquire_new` returning None (no routing candidates) incorrectly put the
///   target location in backoff. Fixed: no backoff on routing capacity failure.
/// - `should_accept` used inflated `total_conn` (including pending reservations)
///   for the min_connections check, pushing nodes into the topology evaluator
///   prematurely. Fixed: use actual open connection count for min threshold.
/// - CONNECT exclusion was absolute (3 failures = banned 30min). Fixed: max 50%
///   of ring peers excluded at once (#3408).
/// - Distance-based fallback never evicted never-succeeded peers below
///   min_connections (#3398).
/// - GC-expired CONNECT forwards silently blamed the acceptor (#3396/#3380).
///
/// **What this test verifies (post-fix behavior):**
///
/// 1. Median connections exceed old BOOTSTRAP_THRESHOLD=4 after 10 virtual minutes.
/// 2. At least some nodes reach min_connections.
/// 3. Nodes form connections to non-gateway peers (proves multi-hop CONNECT).
/// 4. Under 20% message loss (simulating NAT hole-punch failures), no death spiral.
///
/// **Topology:** 2 gateways + 15 nodes, min_connections=5, max_connections=10.
/// min_connections=5 is above the old hardcoded BOOTSTRAP_THRESHOLD=4, so
/// this test would fail without the connect.rs fix (nodes would stall at 4).
/// Sized for CI: 600s virtual time ≈ 70s wall time via `let_network_run`.
#[test_log::test(tokio::test(flavor = "current_thread"))]
async fn test_connection_growth_stall_regression() {
    use freenet::dev_tool::NodeLabel;
    use freenet::simulation::FaultConfig;

    const SEED: u64 = 0x3408_3398_0001;
    const NETWORK_NAME: &str = "connection-growth-stall";
    const GATEWAYS: usize = 2;
    const NODES: usize = 15;
    const RING_MAX_HTL: usize = 7;
    const RND_IF_HTL_ABOVE: usize = 3;
    const MAX_CONN: usize = 10;
    const MIN_CONN: usize = 5;

    tracing::info!("=== Connection Growth Stall Regression Test ===");
    tracing::info!("Verifies fixes: #3408, #3398, #3396, #3380");

    setup_deterministic_state(SEED);

    let mut sim = SimNetwork::new(
        NETWORK_NAME,
        GATEWAYS,
        NODES,
        RING_MAX_HTL,
        RND_IF_HTL_ABOVE,
        MAX_CONN,
        MIN_CONN,
        SEED,
    )
    .await;

    sim.with_start_backoff(Duration::from_millis(50));

    // Start all nodes. max_contract_num=0, iterations=0: topology-only test.
    let _handles = sim
        .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 0, 0)
        .await;

    // -------------------------------------------------------------------------
    // Phase 1: Let the network form connections over 20 virtual minutes
    // -------------------------------------------------------------------------
    // 20 minutes gives topology-aware pruning enough time to converge.
    // The algorithm is more selective about connections (gap-based targeting,
    // composite scoring), so it trades speed for distribution quality.
    tracing::info!("Phase 1: Connection growth — 20 virtual minutes, no faults");
    let_network_run(&mut sim, Duration::from_secs(1200)).await;

    // Collect per-node connection counts
    let mut node_counts: Vec<usize> = (0..NODES)
        .filter_map(|i| {
            let label = NodeLabel::node(NETWORK_NAME, i);
            sim.connection_count(&label)
        })
        .collect();
    node_counts.sort_unstable();

    let num_sampled = node_counts.len();
    assert!(num_sampled > 0, "No connection_managers available");

    let median_conn = node_counts[num_sampled / 2];
    let nodes_above_min = node_counts.iter().filter(|&&c| c >= MIN_CONN).count();
    let fraction_above_min = nodes_above_min as f64 / num_sampled as f64;

    tracing::info!("Phase 1 connection counts: {:?}", node_counts);
    tracing::info!(
        "Median={}, nodes at min_connections={}/{} ({:.0}%)",
        median_conn,
        nodes_above_min,
        num_sampled,
        fraction_above_min * 100.0
    );

    // Check for non-gateway peer connections (proves multi-hop CONNECT forwarding)
    let connectivity = sim.node_connectivity();
    let mut nodes_with_peer_connections = 0usize;
    for (label, (_key, conns)) in &connectivity {
        if !label.is_gateway() {
            let has_non_gw_conn = conns.keys().any(|peer| !peer.is_gateway());
            if has_non_gw_conn {
                nodes_with_peer_connections += 1;
            }
        }
    }

    tracing::info!(
        "Nodes with non-gateway peer connections: {}/{}",
        nodes_with_peer_connections,
        NODES
    );

    // ASSERTION 1: Median connections must be close to MIN_CONN.
    // With dynamic concurrent limits and routing through connected gateways,
    // the median should approach min_connections even in a small simulation.
    // Threshold is MIN_CONN - 1 to allow for topology-aware pruning tradeoffs.
    assert!(
        median_conn >= MIN_CONN - 1,
        "Connection growth stall: median={} must be >= {} (MIN_CONN - 1). \
         Counts: {:?}. Seed: 0x{:X}",
        median_conn,
        MIN_CONN - 1,
        node_counts,
        SEED
    );

    // ASSERTION 2: At least 10% of nodes reached min_connections.
    // In a 15-node simulation with 20 virtual minutes, topology-aware pruning
    // and non-deterministic timing cause run-to-run variance (15%–31% observed).
    // 10% threshold catches severe growth stalls while tolerating this variance.
    assert!(
        fraction_above_min >= 0.10,
        "Too few nodes reached min_connections: {:.0}% ({}/{}) — expected >= 10%. \
         Counts: {:?}. Seed: 0x{:X}",
        fraction_above_min * 100.0,
        nodes_above_min,
        num_sampled,
        node_counts,
        SEED
    );

    // ASSERTION 3: Multi-hop CONNECT forwarding is widespread.
    // With routing through connected gateways, the majority of nodes should
    // have non-gateway peer connections, not just a token few.
    let peer_conn_fraction = nodes_with_peer_connections as f64 / NODES as f64;
    assert!(
        peer_conn_fraction >= 0.50,
        "Only {:.0}% of nodes have non-gateway peer connections (expected >= 50%). \
         CONNECT forwarding is insufficient. Seed: 0x{:X}",
        peer_conn_fraction * 100.0,
        SEED
    );

    // -------------------------------------------------------------------------
    // Phase 2: 20% message loss simulating NAT hole-punch failures — 3 minutes
    // -------------------------------------------------------------------------
    tracing::info!("Phase 2: NAT failure simulation — 20% message loss for 1 min");
    sim.with_fault_injection(FaultConfig::builder().message_loss_rate(0.20).build());

    let_network_run(&mut sim, Duration::from_secs(60)).await;

    // Re-inspect connection counts after faults
    let mut node_counts_after: Vec<usize> = (0..NODES)
        .filter_map(|i| {
            let label = NodeLabel::node(NETWORK_NAME, i);
            sim.connection_count(&label)
        })
        .collect();
    node_counts_after.sort_unstable();

    let median_after = node_counts_after
        .get(node_counts_after.len() / 2)
        .copied()
        .unwrap_or(0);
    let nodes_isolated = node_counts_after.iter().filter(|&&c| c == 0).count();
    let fraction_isolated = nodes_isolated as f64 / NODES as f64;

    tracing::info!(
        "Phase 2 connection counts after NAT failures: {:?}",
        node_counts_after
    );
    tracing::info!(
        "Median={}, isolated={}/{} ({:.0}%)",
        median_after,
        nodes_isolated,
        NODES,
        fraction_isolated * 100.0
    );

    // ASSERTION 4: No death spiral — median must stay near pre-fault level.
    // With 20% message loss for only 60s, a well-connected network should
    // retain most connections. Threshold is MIN_CONN - 2 to allow some churn.
    assert!(
        median_after >= MIN_CONN.saturating_sub(2).max(2),
        "Death spiral: median connections after NAT failures = {} (expected >= {}). \
         Counts: {:?}. Seed: 0x{:X}",
        median_after,
        MIN_CONN.saturating_sub(2).max(2),
        node_counts_after,
        SEED
    );

    // ASSERTION 5: No nodes fully isolated after brief packet loss.
    assert!(
        fraction_isolated < 0.10,
        "{:.0}% of nodes isolated after NAT failures (threshold: 10%). \
         Counts: {:?}. Seed: 0x{:X}",
        fraction_isolated * 100.0,
        node_counts_after,
        SEED
    );

    tracing::info!(
        "PASSED: Phase1[median={}, above_min={}/{}, peer_conns={}/{}] \
         Phase2[median={}, isolated={}/{}]",
        median_conn,
        nodes_above_min,
        num_sampled,
        nodes_with_peer_connections,
        NODES,
        median_after,
        nodes_isolated,
        NODES
    );
}

// =============================================================================
// GET Routing Regression Tests
// =============================================================================

/// Regression test for #3356 / #3423: GET fails with EmptyRing when all peers
/// fail readiness gating.
///
/// Sets `relay_ready_connections` higher than network size so `is_self_ready()`
/// returns false on every node → no node ever sends `ReadyState` to peers →
/// `is_peer_ready()` returns false for all peers. The 60-second optimistic
/// timeout (`OPTIMISTIC_READY_TIMEOUT`) uses real wall-clock `Instant::now()`,
/// not virtual time, so it cannot fire during this test (~0.5s wall-clock).
///
/// Before the fix, `k_closest_potentially_hosting` returned empty → GET failed
/// with EmptyRing. After the fix, it falls back to using not-yet-ready peers.
/// Verified: this test FAILS without the fallback (confirmed by reverting the
/// fix and observing the assertion fire).
///
/// Scenario:
///   1. Gateway PUTs a contract
///   2. Node 1 GETs the contract with `return_contract_code=true`
///   3. Assert node 1's storage contains the contract state
#[test_log::test]
fn test_get_succeeds_despite_readiness_gating() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0xAE7F_1A00_0001;
    const NETWORK_NAME: &str = "get-readiness-fallback";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (mut sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,  // gateways
            3,  // nodes
            7,  // ring_max_htl
            3,  // rnd_if_htl_above
            10, // max_connections
            2,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Set readiness gating higher than network size (1 gateway + 3 nodes = 4 total).
    // No node can ever have this many ready connections, so ReadyState is never sent,
    // and is_peer_ready() returns false for all peers.
    let total_nodes = 1 + 3; // gateways + nodes
    sim.with_readiness_gating(total_nodes + 1);

    let contract = SimOperation::create_test_contract(0xAE);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();

    let operations = vec![
        // 1. Gateway PUTs the contract
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: vec![1, 2, 3, 4],
                subscribe: false,
            },
        ),
        // 2. Node 1 GETs the contract (with contract code)
        ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, 1),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120),
        Duration::from_secs(30),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "GET readiness fallback simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // Verify that node 1 has the contract state despite readiness gating
    let node1_label = NodeLabel::node(NETWORK_NAME, 1);
    let node1_storage = result
        .node_storages
        .get(&node1_label)
        .expect("node 1 should have a storage handle");
    let node1_state = node1_storage.get_stored_state(&contract_key);

    assert!(
        node1_state.is_some(),
        "Node 1 should have contract state after GET, but storage is empty. \
         This indicates k_closest_potentially_hosting returned empty due to \
         readiness gating (the bug from #3356/#3423)."
    );

    // Run StateVerifier for anomaly detection (per testing.md)
    let rt = create_runtime();
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    tracing::info!(
        "Anomaly report: {} anomalies across {} contracts",
        report.anomalies.len(),
        report.contracts_analyzed
    );

    tracing::info!(
        "test_get_succeeds_despite_readiness_gating PASSED: \
         node 1 has contract state, {} events analyzed",
        report.total_events
    );
}

/// Regression test for #3431: GET routing exhaustion when contract is cached at few nodes.
///
/// With low HTL, PUT only caches the contract at a handful of nodes along the
/// route. GET from nodes whose connected peers don't overlap with caching nodes
/// fails with "No other peers found" → NotFound.
///
/// Scenario:
///   1. Gateway PUTs contract (HTL=4 → only ~5 nodes cache code+state)
///   2. 12 nodes subscribe, gateway sends UPDATE (state propagation)
///   3. All 15 nodes GET with `fetch_contract=true`
///   4. Assert every node gets the contract state
///
#[test_log::test]
fn test_get_routing_coverage_low_htl() {
    use std::sync::atomic::Ordering;

    use freenet::dev_tool::{
        GET_RELAY_DRIVER_CALL_COUNT, NodeLabel, RELAY_PUT_DRIVER_CALL_COUNT,
        RELAY_SUBSCRIBE_DRIVER_CALL_COUNT, ScheduledOperation, SimOperation,
        register_crdt_contract,
    };

    // NOTE: the relay-hop *route-event* assertions (RELAY_*_ROUTE_EVENT_COUNT)
    // that used to live here were moved to `test_relay_route_events_multihop`.
    // They require a SPARSE topology where a GET/PUT actually forwards a
    // downstream response, but this test deliberately keeps the mesh well
    // connected (max_connections=7) so the GET-coverage assertion (#3431)
    // holds. Once the #4348 convergence fix let nodes reliably reach
    // min_connections, a 15-node/max=7 net converged dense enough that ops
    // resolved in 0-1 hops and the route-event counters stayed at 0 — a
    // topology-coupling that the dedicated sparse test now owns. This test
    // keeps the relay *driver-call* assertions (which fire even when a relay
    // resolves locally) plus the coverage assertion.
    const SEED: u64 = 0xC0DE_B0CA_0032;
    const NETWORK_NAME: &str = "get-routing-coverage";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    // Baseline the relay driver counter so we can assert that the
    // dispatch gate in handle_pure_network_message_v1 actually routed
    // relay hops through the task-per-tx driver (not the legacy
    // handle_op_request fallthrough). This is the behavioral companion
    // to the source-scrape tests in op_ctx_task.rs that only pin the
    // dispatch-block SHAPE. #1454 phase 5 / #3883.
    let relay_baseline = GET_RELAY_DRIVER_CALL_COUNT.load(Ordering::SeqCst);
    let relay_put_baseline = RELAY_PUT_DRIVER_CALL_COUNT.load(Ordering::SeqCst);
    let relay_subscribe_baseline = RELAY_SUBSCRIBE_DRIVER_CALL_COUNT.load(Ordering::SeqCst);

    let rt = create_runtime();

    let num_nodes = 15;

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,         // gateways
            num_nodes, // nodes — large enough that PUT won't reach all
            3,         // ring_max_htl — LOW to limit PUT propagation depth
            1,         // rnd_if_htl_above
            7,         // max_connections — 7 ensures robust GET routing across topology variations
            3,         // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xC0);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    let mut operations = vec![
        // Gateway PUTs with subscribe (HTL=4 → caches at ~5 nodes)
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: SimOperation::create_crdt_state(1, 0xC0),
                subscribe: true,
            },
        ),
    ];

    // Subscribe nodes 1-12
    for i in 1..=12 {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Subscribe { contract_id },
        ));
    }

    // Gateway updates — propagates state to subscribers
    operations.push(ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Update {
            key: contract_key,
            data: SimOperation::create_crdt_state(42, 0xC0),
        },
    ));

    // Every node GETs with fetch_contract=true
    for i in 1..=num_nodes {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(300),
        Duration::from_secs(90),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // Assert every node has the contract state after GET
    let mut nodes_without_state = Vec::new();

    for i in 1..=num_nodes {
        let label = NodeLabel::node(NETWORK_NAME, i);
        let storage = result
            .node_storages
            .get(&label)
            .unwrap_or_else(|| panic!("node {i} should have a storage handle"));
        if storage.get_stored_state(&contract_key).is_none() {
            nodes_without_state.push(format!("node-{i}"));
        }
    }

    assert!(
        nodes_without_state.is_empty(),
        "GET routing exhaustion: {} nodes failed to get contract state \
         (contract only cached at ~5 nodes due to HTL=4). \
         Failed nodes: {:?}. See #3431.",
        nodes_without_state.len(),
        nodes_without_state
    );

    // Behavioral companion to the dispatch-gate source-scrape tests in
    // op_ctx_task.rs: prove the relay task-per-tx driver actually ran
    // on at least one intermediate hop during the 15-node / HTL=3 /
    // 15 parallel GETs workload. If this assertion ever starts failing,
    // something has silently regressed the dispatch split in
    // handle_pure_network_message_v1 — inbound relay Requests are going
    // through the legacy handle_op_request path again. #1454 phase 5 / #3883.
    let relay_after = GET_RELAY_DRIVER_CALL_COUNT.load(Ordering::SeqCst);
    let relay_delta = relay_after.saturating_sub(relay_baseline);
    assert!(
        relay_delta > 0,
        "RELAY_DRIVER_CALL_COUNT did not advance during the test — the \
         relay task-per-tx driver never ran. With {num_nodes} nodes, HTL=3, \
         and ~5 caching nodes, every non-caching requester should hit at \
         least one relay hop. Dispatch gate in \
         handle_pure_network_message_v1 has likely regressed. Baseline: {relay_baseline}, after: {relay_after}."
    );

    // Behavioral companion for relay PUT slice A (#1454 phase 5
    // follow-up, PR #3917): the gateway PUT at HTL=3 should reach at
    // least one relay peer via start_relay_put. If this never advances,
    // the PUT dispatch gate in handle_pure_network_message_v1 has
    // regressed — inbound relay PutMsg::Request is going through the
    // legacy handle_op_request path again.
    let relay_put_after = RELAY_PUT_DRIVER_CALL_COUNT.load(Ordering::SeqCst);
    let relay_put_delta = relay_put_after.saturating_sub(relay_put_baseline);
    assert!(
        relay_put_delta > 0,
        "RELAY_PUT_DRIVER_CALL_COUNT did not advance during the test — \
         the relay PUT task-per-tx driver never ran. Gateway PUT at HTL=3 \
         in a {num_nodes}-node network must traverse at least one relay hop. \
         Dispatch gate for PUT has likely regressed. \
         Baseline: {relay_put_baseline}, after: {relay_put_after}."
    );

    // Behavioral companion for relay SUBSCRIBE slice A (#1454 phase 5
    // follow-up, PR #3932): 12 nodes subscribe above. At HTL=3 with
    // ~5 caching nodes, at least one subscribe must traverse a relay
    // hop and hit start_relay_subscribe. If this never advances, the
    // dispatch gate for SUBSCRIBE has regressed — inbound relay
    // SubscribeMsg::Request is going through the legacy
    // handle_op_request path again.
    let relay_subscribe_after = RELAY_SUBSCRIBE_DRIVER_CALL_COUNT.load(Ordering::SeqCst);
    let relay_subscribe_delta = relay_subscribe_after.saturating_sub(relay_subscribe_baseline);
    assert!(
        relay_subscribe_delta > 0,
        "RELAY_SUBSCRIBE_DRIVER_CALL_COUNT did not advance during the \
         test — the relay SUBSCRIBE task-per-tx driver never ran. With \
         12 node subscribes at HTL=3 and ~5 caching nodes, at least one \
         subscribe must traverse a relay hop. Dispatch gate for \
         SUBSCRIBE has likely regressed. \
         Baseline: {relay_subscribe_baseline}, after: {relay_subscribe_after}."
    );

    // Relay-hop *route-event* coverage (RELAY_*_ROUTE_EVENT_COUNT) is asserted
    // separately in `test_relay_route_events_multihop`. Those counters only
    // fire when a relay forwards a downstream response, which needs a sparse
    // topology where an op actually traverses >=2 hops. This test keeps the
    // mesh dense (max_connections=7) so the GET-coverage assertion above holds;
    // post-#4348 that density makes ops resolve in 0-1 hops, so route events do
    // not fire here (#4348). The relay *drivers* are still exercised — see the
    // RELAY_*_DRIVER_CALL_COUNT assertions above.

    // StateVerifier anomaly check
    let rt = create_runtime();
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    tracing::info!(
        "test_get_routing_coverage_low_htl PASSED: {} anomalies, {} events, \
         relay_driver_calls={relay_delta}",
        report.anomalies.len(),
        report.total_events,
    );
}

/// Relay-hop route-event telemetry (#1454): a relay that forwards a downstream
/// GET/PUT response must feed the result into the local Router via
/// `record_relay_route_event`, or the per-peer dashboard panels stay empty and
/// the failure-probability model is undertrained on relay-heavy nodes.
///
/// These assertions previously lived in `test_get_routing_coverage_low_htl`,
/// but that test must keep a DENSE mesh so every node's GET resolves
/// (its #3431 coverage assertion). The #4348 connection-convergence fix made
/// that mesh converge dense enough that ops resolved in 0-1 hops, so
/// `RELAY_*_ROUTE_EVENT_COUNT` never advanced — the test was implicitly relying
/// on the under-connectivity bug #4348 fixed. This dedicated test instead uses a
/// deliberately SPARSE topology where multi-hop forwarding is structural, and
/// asserts ONLY that the route-event hooks fire. It makes NO GET-coverage claim
/// (a NotFound reply still exercises the relay route-event hook), so improved
/// connectivity can never invalidate it.
///
/// SUBSCRIBE route events are intentionally not asserted (subscribes hit the
/// relay local-cache fast path without forwarding downstream — see the note in
/// `test_get_routing_coverage_low_htl`); the relay SUBSCRIBE *driver* is still
/// covered there.
#[test_log::test]
fn test_relay_route_events_multihop() {
    use std::sync::atomic::Ordering;

    use freenet::dev_tool::{
        NodeLabel, RELAY_GET_ROUTE_EVENT_COUNT, RELAY_PUT_ROUTE_EVENT_COUNT, ScheduledOperation,
        SimOperation, register_crdt_contract,
    };

    const SEED: u64 = 0xC0DE_B0CA_0040;
    const NETWORK_NAME: &str = "relay-route-events";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let get_route_baseline = RELAY_GET_ROUTE_EVENT_COUNT.load(Ordering::SeqCst);
    let put_route_baseline = RELAY_PUT_ROUTE_EVENT_COUNT.load(Ordering::SeqCst);

    let rt = create_runtime();
    let num_nodes = 15;

    let sim = rt.block_on(async {
        SimNetwork::new(
            NETWORK_NAME,
            1,         // gateways
            num_nodes, // nodes
            3,         // ring_max_htl
            1,         // rnd_if_htl_above
            // max_connections — deliberately LOW. A sparse mesh keeps requesters
            // >=2 hops from the few holders, so GET/PUT requests forward through a
            // non-holding relay and fire the route-event hooks. We assert only
            // that the hooks fire, NOT that every GET resolves, so sparsity (which
            // would fail a coverage assertion) is exactly what we want here.
            4, // max_connections
            3, // min_connections
            SEED,
        )
        .await
    });

    let contract = SimOperation::create_test_contract(0xC0);
    let contract_id = *contract.key().id();
    register_crdt_contract(contract_id);

    // Gateway seeds the contract; only a few nodes subscribe so the cache stays
    // sparse and most requesters are several hops from a holder.
    let mut operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: SimOperation::create_crdt_state(1, 0xC0),
            subscribe: true,
        },
    )];
    for i in 1..=4 {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Subscribe { contract_id },
        ));
    }
    for i in 1..=num_nodes {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(300),
        Duration::from_secs(90),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    let get_route_after = RELAY_GET_ROUTE_EVENT_COUNT.load(Ordering::SeqCst);
    let put_route_after = RELAY_PUT_ROUTE_EVENT_COUNT.load(Ordering::SeqCst);
    assert!(
        get_route_after > get_route_baseline,
        "RELAY_GET_ROUTE_EVENT_COUNT did not advance — in a sparse {num_nodes}-node \
         mesh (max_connections=4) at least one GET must forward through a relay that \
         feeds a route event to the local Router (a NotFound reply counts too). \
         Baseline: {get_route_baseline}, after: {get_route_after}."
    );
    assert!(
        put_route_after > put_route_baseline,
        "RELAY_PUT_ROUTE_EVENT_COUNT did not advance — the gateway PUT at HTL=3 in a \
         sparse {num_nodes}-node mesh must forward through at least one relay. \
         Baseline: {put_route_baseline}, after: {put_route_after}."
    );
}

/// Regression test for GET retry with alternatives in sparse topologies.
///
/// With HTL=2 and 10 nodes, PUT only caches the contract at 2-3 nodes along the
/// route. When distant nodes GET, their initial routing candidate may not have
/// the contract. The fix ensures the GET state machine tries alternative peers
/// (from k_closest) and re-queries k_closest when alternatives are exhausted,
/// rather than immediately returning NotFound.
///
/// Without the fix: distant nodes return NotFound because the first-choice peer
/// doesn't have the contract and there's no retry.
/// With the fix: the GET retries with alternative peers until one succeeds.
///
/// Exercises: `get.rs` alternative retry (line ~1521) and k_closest re-query (line ~1569)
///
/// Seed history: this test was `#[ignore]`d in PR #3488 because the original
/// seed (`0xBEEF_CAFE_0001`) stopped producing a sparse-but-reachable topology
/// after a freenet-stdlib bump shifted RNG-dependent topology formation (the
/// stdlib's enum layout changes bincode serialization sizes, which perturbs the
/// seeded RNG stream). #3506 re-seeded it for the current stdlib: with
/// `SEED = 0x3506_000B_0001` the gateway PUT at HTL=2 caches the contract at
/// exactly 2 of the 10 nodes (verified with a PUT-only run of this same
/// topology), so 8 of the 10 GET requesters must use the retry-with-alternatives
/// path to reach a cacher, and all 10 do, deterministically across repeated
/// runs. Like every seed-coupled topology test, a future stdlib/topology change
/// could perturb the RNG stream again and require another re-seed; that is
/// expected maintenance rather than a regression in the GET retry logic itself
/// (which is also covered by `test_get_routing_coverage_low_htl`).
#[test_log::test]
fn test_get_retry_with_alternatives_sparse_topology() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    // Seed reselected for the current freenet-stdlib layout (see #3506). Under
    // this seed the gateway PUT at HTL=2 caches the contract at exactly 2 of the
    // 10 nodes, and all 10 GET requesters reach one of those 2 cachers via the
    // GET retry-with-alternatives path. Verified sparse + reachable + deterministic
    // before un-ignoring; see the doc comment above for the full rationale.
    const SEED: u64 = 0x3506_000B_0001;
    const NETWORK_NAME: &str = "get-retry-sparse";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let num_nodes = 10;

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,         // 1 gateway
            num_nodes, // 10 nodes — large enough that PUT won't reach all
            2,         // ring_max_htl = 2 — VERY low, PUT caches at only ~2-3 nodes
            1,         // rnd_if_htl_above
            6,         // max_connections
            3,         // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xBE);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();

    let mut operations = vec![
        // Gateway PUTs the contract (HTL=2 → only ~2-3 nodes cache)
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: vec![10, 20, 30, 40],
                subscribe: false,
            },
        ),
    ];

    // All 10 nodes GET with fetch_contract=true
    // Nodes far from caching nodes must retry with alternatives
    for i in 1..=num_nodes {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(300), // simulation duration
        Duration::from_secs(90),  // post-operations wait for retries
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // Verify that all nodes got the contract state despite sparse caching.
    // Without the retry fix, distant nodes would get NotFound.
    let mut nodes_without_state = Vec::new();
    for i in 1..=num_nodes {
        let label = NodeLabel::node(NETWORK_NAME, i);
        let storage = result
            .node_storages
            .get(&label)
            .unwrap_or_else(|| panic!("node {i} should have a storage handle"));
        if storage.get_stored_state(&contract_key).is_none() {
            nodes_without_state.push(format!("node-{i}"));
        }
    }

    assert!(
        nodes_without_state.is_empty(),
        "GET retry regression: {} of {} nodes failed to get contract state \
         (contract cached at only ~2-3 nodes due to HTL=2, retry should find it). \
         Failed nodes: {:?}. See PR #3444.",
        nodes_without_state.len(),
        num_nodes,
        nodes_without_state
    );

    // StateVerifier anomaly check
    let rt = create_runtime();
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    tracing::info!(
        "test_get_retry_with_alternatives_sparse_topology PASSED: \
         all {} nodes got contract state, {} anomalies, {} events",
        num_nodes,
        report.anomalies.len(),
        report.total_events
    );
}

/// Regression test for auto-fetch from UPDATE sender.
///
/// When a node subscribed to a CRDT contract receives an UPDATE broadcast but
/// doesn't have the contract's code/parameters (because PUT's low HTL didn't
/// cache it there), the fix triggers `try_auto_fetch_contract` — a targeted GET
/// to the UPDATE sender who is known to have the contract.
///
/// Scenario:
///   1. Gateway PUTs a CRDT contract (HTL=2 → only ~2-3 nodes cache code)
///   2. All nodes subscribe to the contract
///   3. Gateway sends UPDATE → broadcast reaches subscribed nodes
///   4. Nodes that subscribed but don't have the contract code auto-fetch
///   5. All subscribed nodes should end up with the state
///
/// Without the fix: nodes without contract code can't apply the update → stale.
/// With the fix: auto-fetch retrieves the contract, then update applies.
///
/// Exercises: `update.rs` try_auto_fetch_contract (BroadcastTo/BroadcastToStreaming paths)
#[test_log::test]
fn test_auto_fetch_from_update_sender() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    const SEED: u64 = 0xFE7C_A100_0001;
    const NETWORK_NAME: &str = "auto-fetch-update";

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let num_nodes = 8;

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            1,         // 1 gateway
            num_nodes, // 8 nodes
            2,         // ring_max_htl = 2 — low to limit PUT propagation
            1,         // rnd_if_htl_above
            6,         // max_connections
            3,         // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    let contract = SimOperation::create_test_contract(0xFE);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    register_crdt_contract(contract_id);

    let mut operations = vec![
        // 1. Gateway PUTs with subscribe (HTL=2 → few nodes cache code)
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: SimOperation::create_crdt_state(1, 0xFE),
                subscribe: true,
            },
        ),
    ];

    // 2. All nodes subscribe
    for i in 1..=num_nodes {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Subscribe { contract_id },
        ));
    }

    // 3. Gateway sends UPDATE — propagates to subscribers
    operations.push(ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Update {
            key: contract_key,
            data: SimOperation::create_crdt_state(42, 0xFE),
        },
    ));

    // 4. All nodes GET to verify they have the state
    // (auto-fetch should have already retrieved it for nodes without code)
    for i in 1..=num_nodes {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(300), // simulation duration
        Duration::from_secs(90),  // post-operations wait for auto-fetch
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // Verify that all subscribing nodes got the contract state.
    // Nodes that didn't have the contract code from PUT should have
    // auto-fetched it when they received the UPDATE broadcast.
    let mut nodes_without_state = Vec::new();
    for i in 1..=num_nodes {
        let label = NodeLabel::node(NETWORK_NAME, i);
        let storage = result
            .node_storages
            .get(&label)
            .unwrap_or_else(|| panic!("node {i} should have a storage handle"));
        if storage.get_stored_state(&contract_key).is_none() {
            nodes_without_state.push(format!("node-{i}"));
        }
    }

    // Allow up to 1 node without state (edge case: a node may not have connected
    // to any subscriber yet). But most nodes should have it via auto-fetch.
    let success_count = num_nodes - nodes_without_state.len();
    let success_rate = success_count as f64 / num_nodes as f64;
    assert!(
        success_rate >= 0.75,
        "Auto-fetch regression: only {} of {} nodes ({:.0}%) got contract state \
         after UPDATE broadcast. Without auto-fetch, nodes that didn't receive \
         PUT due to low HTL would never get the contract. \
         Failed nodes: {:?}. See PR #3444.",
        success_count,
        num_nodes,
        success_rate * 100.0,
        nodes_without_state
    );

    // StateVerifier anomaly check
    let rt = create_runtime();
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    tracing::info!(
        "test_auto_fetch_from_update_sender PASSED: \
         {}/{} nodes got contract state ({:.0}%), {} anomalies, {} events",
        success_count,
        num_nodes,
        success_rate * 100.0,
        report.anomalies.len(),
        report.total_events
    );
}

// =============================================================================
// Connection Growth Plateau Diagnostic (#3555)
// =============================================================================

/// Diagnostic test to understand why nodes plateau below min_connections.
///
/// Instruments the CONNECT pipeline to measure:
/// - How many CONNECT operations are initiated
/// - How many are accepted at terminus vs forwarded
/// - How many result in actual Connected events
/// - Per-node connection counts over time
///
/// This is a diagnostic (non-asserting beyond basic sanity) — run with
/// `RUST_LOG=info` to see the pipeline breakdown.
#[test_log::test(tokio::test(flavor = "current_thread"))]
async fn test_connection_growth_plateau_diagnostic() {
    use freenet::dev_tool::NodeLabel;

    const SEED: u64 = 0x3555_D1A6_0002;
    const NETWORK_NAME: &str = "growth-plateau-diag";
    const GATEWAYS: usize = 2;
    const NODES: usize = 50;
    const RING_MAX_HTL: usize = 7;
    const RND_IF_HTL_ABOVE: usize = 3;
    const MAX_CONN: usize = 20;
    const MIN_CONN: usize = 10;

    tracing::info!("=== Connection Growth Plateau Diagnostic ===");

    setup_deterministic_state(SEED);

    let mut sim = SimNetwork::new(
        NETWORK_NAME,
        GATEWAYS,
        NODES,
        RING_MAX_HTL,
        RND_IF_HTL_ABOVE,
        MAX_CONN,
        MIN_CONN,
        SEED,
    )
    .await;

    sim.with_start_backoff(Duration::from_millis(50));

    let _handles = sim
        .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 0, 0)
        .await;

    let logs_handle = sim.event_logs_handle();

    // Run for 5 virtual minutes, snapshot, then 5 more, snapshot, etc.
    // This gives us a growth curve.
    let phases = [
        ("5min", Duration::from_secs(300)),
        ("10min", Duration::from_secs(300)),
        ("15min", Duration::from_secs(300)),
        ("20min", Duration::from_secs(300)),
    ];

    for (phase_name, duration) in &phases {
        let_network_run(&mut sim, *duration).await;

        // Collect per-node connection counts
        let mut node_counts: Vec<usize> = (0..NODES)
            .filter_map(|i| {
                let label = NodeLabel::node(NETWORK_NAME, i);
                sim.connection_count(&label)
            })
            .collect();
        node_counts.sort_unstable();

        let n = node_counts.len();
        if n == 0 {
            tracing::info!("[{}] No nodes sampled", phase_name);
            continue;
        }

        let median = node_counts[n / 2];
        let min = node_counts[0];
        let max = node_counts[n - 1];
        let above_min = node_counts.iter().filter(|&&c| c >= MIN_CONN).count();
        let zero = node_counts.iter().filter(|&&c| c == 0).count();
        let avg = node_counts.iter().sum::<usize>() as f64 / n as f64;

        // Count CONNECT events so far
        let (initiated, terminus_accepted, terminus_rejected, forwarded, connected, disconnected) = {
            let logs = logs_handle.lock().await;
            let initiated = logs
                .iter()
                .filter(|l| l.kind.is_connect_initiated())
                .count();
            let terminus_accepted = logs
                .iter()
                .filter(|l| l.kind.is_connect_terminus_accepted())
                .count();
            let terminus_rejected = logs
                .iter()
                .filter(|l| l.kind.is_connect_terminus_rejected())
                .count();
            let forwarded = logs
                .iter()
                .filter(|l| l.kind.is_connect_forwarded())
                .count();
            let connected = logs
                .iter()
                .filter(|l| l.kind.is_connect_connected())
                .count();
            let disconnected = logs.iter().filter(|l| l.kind.is_disconnected()).count();
            (
                initiated,
                terminus_accepted,
                terminus_rejected,
                forwarded,
                connected,
                disconnected,
            )
        };

        let terminus_total = terminus_accepted + terminus_rejected;
        let terminus_accept_rate = if terminus_total > 0 {
            terminus_accepted as f64 / terminus_total as f64 * 100.0
        } else {
            0.0
        };

        tracing::info!(
            "[{}] Connections: median={}, avg={:.1}, min={}, max={}, \
             above_min={}/{} ({:.0}%), zero={}",
            phase_name,
            median,
            avg,
            min,
            max,
            above_min,
            n,
            above_min as f64 / n as f64 * 100.0,
            zero,
        );
        tracing::info!(
            "[{}] CONNECT pipeline: initiated={}, forwarded={}, \
             terminus_accepted={}, terminus_rejected={}, \
             terminus_accept_rate={:.0}%, connected={}, disconnected={}",
            phase_name,
            initiated,
            forwarded,
            terminus_accepted,
            terminus_rejected,
            terminus_accept_rate,
            connected,
            disconnected,
        );
    }

    // Distribution histogram
    let final_counts: Vec<usize> = (0..NODES)
        .filter_map(|i| {
            let label = NodeLabel::node(NETWORK_NAME, i);
            sim.connection_count(&label)
        })
        .collect();

    let mut histogram: BTreeMap<usize, usize> = BTreeMap::new();
    for &c in &final_counts {
        *histogram.entry(c).or_insert(0) += 1;
    }
    tracing::info!("Final connection distribution: {:?}", histogram);

    // Gateway connection counts
    for i in 0..GATEWAYS {
        let label = NodeLabel::gateway(NETWORK_NAME, i);
        if let Some(count) = sim.connection_count(&label) {
            tracing::info!("Gateway {} connections: {}", i, count);
        }
    }

    // Intentionally weak sanity check — this is a diagnostic test (see doc comment),
    // not a regression test for min_connections. The full regression test is
    // test_connection_growth_stall_regression which asserts >=90% reach min_connections.
    let total_connected = final_counts.iter().filter(|&&c| c > 0).count();
    assert!(
        total_connected > NODES / 2,
        "Fewer than half the nodes have any connections: {}/{}",
        total_connected,
        NODES,
    );
}

/// Diagnostic test for #3570: GET operations have high timeout rate in realistic networks.
///
/// This test creates a 100-node network, PUTs a contract from a gateway, then has
/// every node attempt to GET the same contract. It measures:
/// - Overall GET success rate
/// - Latency distribution (p50, p90, p99)
/// - Failure modes (NotFound vs Failure vs Timeout)
///
/// The test is intentionally diagnostic — it logs detailed statistics rather than
/// asserting a hard threshold, so we can gather evidence on what's happening.
/// A soft assertion ensures GET success rate doesn't fall below 50% (catastrophic).
///
/// Uses `run_controlled_simulation` for deterministic reproduction.
// Long-running diagnostic test (~2min). Runs in nightly CI.
#[cfg(feature = "nightly_tests")]
#[test_log::test]
fn test_get_reliability_diagnostic() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0x3570_D1A6_0001;
    const NETWORK_NAME: &str = "get-reliability-diag";
    const NUM_NODES: usize = 100;
    const NUM_GATEWAYS: usize = 3;
    const RING_MAX_HTL: usize = 10;

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            NUM_GATEWAYS,
            NUM_NODES,
            RING_MAX_HTL, // realistic for 100-node network
            7,            // rnd_if_htl_above
            12,           // max_connections
            4,            // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Create a test contract and PUT it from gateway 0
    let contract = SimOperation::create_test_contract(0x35);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();

    let mut operations = vec![
        // Gateway 0 PUTs the contract with subscribe
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: vec![0x35; 64],
                subscribe: true,
            },
        ),
    ];

    // Every node GETs the contract — this exercises multi-hop routing
    // across the full network topology
    for i in 0..NUM_NODES {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(600), // 10 min simulation
        Duration::from_secs(120), // 2 min post-operations wait
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // Analyze GET outcomes from event logs, deduplicated per attempt
    // transaction (#4361): raw event counting double-counts every failed
    // attempt (relay-direct + loopback-Response registration) and counts
    // multi-hop outcomes once per hop traversed.
    let rt = create_runtime();
    let (summary, dispatched_nodes) = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let summary = freenet::tracing::summarize_get_outcomes_per_tx(&logs);

        // Dispatched-vs-scheduled accounting (#4361): the client driver's
        // loopback Request is registered at the originating node with
        // htl == ring_max_htl; relay-received Requests have already been
        // decremented. Unique originating peers therefore counts how many
        // nodes actually dispatched a GET (sub-op GETs can inflate this
        // slightly, which is why it feeds a floor assertion, not an
        // exact-match one).
        let dispatched_nodes: HashSet<_> = logs
            .iter()
            .filter(|log| log.kind.get_request_htl() == Some(RING_MAX_HTL))
            .map(|log| log.peer_id.clone())
            .collect();

        (summary, dispatched_nodes.len())
    });

    let (successes, not_found, failures, timeouts) = (
        summary.successes,
        summary.not_found,
        summary.failures,
        summary.timeouts,
    );
    let network_successes = summary.network_successes;
    let total_outcomes = summary.total();

    // Compute latency percentiles for successful GETs
    // (success_elapsed_ms is pre-sorted ascending).
    let sorted_latencies = &summary.success_elapsed_ms;

    let p50 = sorted_latencies
        .get(sorted_latencies.len() / 2)
        .copied()
        .unwrap_or(0);
    let p90 = sorted_latencies
        .get(sorted_latencies.len() * 9 / 10)
        .copied()
        .unwrap_or(0);
    let p99 = sorted_latencies
        .get(sorted_latencies.len() * 99 / 100)
        .copied()
        .unwrap_or(0);
    let max_latency = sorted_latencies.last().copied().unwrap_or(0);

    let success_rate = if total_outcomes > 0 {
        successes as f64 / total_outcomes as f64
    } else {
        0.0
    };

    // Detect response-lost and ForwardingAck patterns from event logs
    let (response_sent_count, ack_received_count, response_lost_txs, retry_storm_txs) = rt
        .block_on(async {
            let logs = logs_handle.lock().await;
            let mut response_sent_txs: HashSet<String> = HashSet::new();
            let mut success_txs: HashSet<String> = HashSet::new();
            let mut ack_received = 0u64;
            let mut request_count_per_tx: HashMap<String, usize> = HashMap::new();

            for log in logs.iter() {
                let tx_str = log.tx.to_string();
                if log.kind.is_get_response_sent() {
                    response_sent_txs.insert(tx_str.clone());
                }
                if log.kind.get_outcome() == Some(true) {
                    success_txs.insert(tx_str.clone());
                }
                if log.kind.is_forwarding_ack_received() {
                    ack_received += 1;
                }
                if log.kind.get_outcome().is_some()
                    || log.kind.is_get_response_sent()
                    || log.kind.is_forwarding_ack_received()
                {
                    // Count unique request events per tx for retry-storm detection
                } else if log.kind.is_get_request() {
                    *request_count_per_tx.entry(tx_str).or_insert(0) += 1;
                }
            }

            // Response-lost: response_sent exists but no get_success for that tx
            let response_lost: Vec<_> = response_sent_txs
                .difference(&success_txs)
                .cloned()
                .collect();

            // Retry-storm: transactions with >10 request events (same tx hitting many peers)
            let retry_storms: Vec<_> = request_count_per_tx
                .iter()
                .filter(|&(_, &count)| count > 10)
                .map(|(tx, count)| (tx.clone(), *count))
                .collect();

            (
                response_sent_txs.len(),
                ack_received,
                response_lost,
                retry_storms,
            )
        });

    tracing::info!("=== GET Reliability Diagnostic (#3570) ===");
    tracing::info!(
        "Network: {} gateways + {} nodes = {} total peers",
        NUM_GATEWAYS,
        NUM_NODES,
        NUM_GATEWAYS + NUM_NODES
    );
    tracing::info!(
        "GET outcomes (per attempt tx): {} total — {} success ({} network-traversed), \
         {} not_found, {} failures, {} timeouts",
        total_outcomes,
        successes,
        network_successes,
        not_found,
        failures,
        timeouts
    );
    tracing::info!(
        "GET success rate: {:.1}% ({}/{})",
        success_rate * 100.0,
        successes,
        total_outcomes
    );
    tracing::info!(
        "GET dispatch: {}/{} nodes dispatched at least one GET attempt",
        dispatched_nodes,
        NUM_NODES
    );
    tracing::info!(
        "Latency (successful GETs): p50={}ms, p90={}ms, p99={}ms, max={}ms",
        p50,
        p90,
        p99,
        max_latency
    );
    tracing::info!(
        "ForwardingAck: {} ACKs received, {} response_sent events, {} response-lost txs",
        ack_received_count,
        response_sent_count,
        response_lost_txs.len()
    );
    if !retry_storm_txs.is_empty() {
        tracing::info!(
            "Retry storms (>10 requests per tx): {} txs, max {} requests",
            retry_storm_txs.len(),
            retry_storm_txs.iter().map(|(_, c)| c).max().unwrap_or(&0)
        );
    }

    // Check which nodes got the contract state
    let mut nodes_with_state = 0;
    let mut nodes_without_state = Vec::new();
    for i in 0..NUM_NODES {
        let label = NodeLabel::node(NETWORK_NAME, i);
        if let Some(storage) = result.node_storages.get(&label) {
            if storage.get_stored_state(&contract_key).is_some() {
                nodes_with_state += 1;
            } else {
                nodes_without_state.push(i);
            }
        } else {
            nodes_without_state.push(i);
        }
    }

    tracing::info!(
        "Storage verification: {}/{} nodes have contract state",
        nodes_with_state,
        NUM_NODES
    );
    if !nodes_without_state.is_empty() {
        tracing::info!(
            "Nodes missing state ({} total): {:?}{}",
            nodes_without_state.len(),
            &nodes_without_state[..nodes_without_state.len().min(20)],
            if nodes_without_state.len() > 20 {
                "..."
            } else {
                ""
            }
        );
    }

    // Soft assertion — this is diagnostic, but catastrophic failure should still fail the test
    assert!(
        total_outcomes >= 10,
        "Only {} GET outcome transactions — too few for meaningful analysis",
        total_outcomes
    );
    // Success-rate floor raised from the catastrophic-only 0.50 to 0.90 now
    // that #4362 is fixed: with late joiners actually joining, the measured
    // run reaches 100% (91/91). 0.90 leaves headroom for seed-to-seed jitter
    // while still catching a real regression.
    assert!(
        success_rate >= 0.90,
        "GET success rate {:.1}% below 0.90 floor (#4362 fixed should keep this high). \
         {} succeeded, {} not_found, {} failures, {} timeouts out of {} total. \
         See #3570 for context.",
        success_rate * 100.0,
        successes,
        not_found,
        failures,
        timeouts,
        total_outcomes
    );
    // Dispatch accounting (#4361): scheduled GETs that never dispatch an
    // operation silently shrink the denominator, so the success rate can
    // look healthy while most of the test never ran. The historical floor
    // was NUM_NODES/3 (33) because the bootstrap acceptance collapse (#4362)
    // left ~46/100 nodes with no completed transport handshake when their
    // GET signal arrived (rejected with PeerNotJoined, no harness retry).
    // With #4362 fixed (joiners re-route off saturated gateways and join
    // early under a short non-escalating reject backoff), the measured run
    // dispatches 86/100. Floor set to 80/100 = achieved minus a ~6-node
    // safety margin for seed-to-seed jitter; still ~2.4x the broken baseline,
    // so it locks the fix in without being flaky.
    assert!(
        dispatched_nodes >= NUM_NODES * 8 / 10,
        "Only {}/{} scheduled GETs dispatched an operation — the test \
         exercised only a fraction of its workload; the bootstrap acceptance \
         collapse (#4362) appears to have regressed (#4361)",
        dispatched_nodes,
        NUM_NODES
    );
    // Network-traversal floor (#4361): before this assertion existed, every
    // "success" in the failing runs was a local cache hit (hop_count == 0)
    // on a node that already held the contract — multi-hop GET was never
    // exercised at all. Require that a meaningful number of successes
    // actually traversed the network.
    assert!(
        network_successes >= 5,
        "Only {} of {} successful GETs traversed the network (hop_count >= 1) \
         — the success metric is measuring local availability, not routing (#4361)",
        network_successes,
        successes
    );

    // StateVerifier anomaly check
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    tracing::info!(
        "Anomaly report: {} anomalies across {} contracts, {} total events",
        report.anomalies.len(),
        report.contracts_analyzed,
        report.total_events
    );

    tracing::info!(
        "test_get_reliability_diagnostic DONE: {:.1}% success rate, \
         {}/{} nodes have state",
        success_rate * 100.0,
        nodes_with_state,
        NUM_NODES
    );
}

/// Regression test for the bootstrap acceptance collapse (#4362).
///
/// Unlike `test_get_reliability_diagnostic`, which asserts on GET outcomes,
/// this test asserts on the TOPOLOGY-FORMATION timeline so it pins the actual
/// fix rather than a downstream symptom:
///
///   (a) the number of `connect_rejected` events stays far below the ~5936
///       observed on the broken code — the terminus-rejection storm that
///       defined the dead zone, and
///   (b) a high fraction of nodes climb to `min_connections` early (by a
///       mid-sim virtual-time checkpoint), proving the ~T+320s unblock moved
///       to the front of the run.
///
/// Same seed/params as `test_get_reliability_diagnostic`. On the broken code
/// the joiner stamps a 30s→600s location backoff on its own (jittered)
/// location every time a saturated gateway neighborhood terminus-rejects it,
/// then retries into the same wall — so the rejection count explodes and most
/// nodes sit below `min_connections` for minutes. With the joiner-side
/// re-route (skip the under-min location backoff + widen the target after the
/// bounded jitter saturates), retries route toward regions with spare
/// capacity and the floor lifts early.
// Long-running topology test (~2min). Runs in nightly CI.
#[cfg(feature = "nightly_tests")]
#[test_log::test]
fn test_bootstrap_acceptance_no_dead_zone() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0x3570_D1A6_0001;
    const NETWORK_NAME: &str = "bootstrap-no-dead-zone";
    const NUM_NODES: usize = 100;
    const NUM_GATEWAYS: usize = 3;
    const RING_MAX_HTL: usize = 10;
    const MIN_CONNECTIONS: usize = 4;
    const MAX_CONNECTIONS: usize = 12;

    // Early-simulation checkpoint. This is NOT a literal virtual-time bound:
    // `created_at_ms()` decodes a transaction's ULID timestamp, which in
    // simulation mode is GlobalSimulationTime = a fixed epoch + a counter that
    // increments by 1 per ULID GENERATED (config.rs), not advanced by the
    // sim's virtual clock. So this filter selects "transactions among roughly
    // the first CHECKPOINT_MS ULIDs generated" — a monotonic GENERATION-ORDER
    // proxy for "early in the run", whose calibration depends on ULID volume.
    // It is sufficient here: on broken code the dead zone persists deep into
    // the run (46/100 nodes peer_ready==false at T+306s of wall-equivalent
    // sim), so almost no nodes reach min_connections in the early window;
    // requiring most nodes to reach it early is exactly what fails on broken
    // code and passes once retries re-route. The constant is named *_MS for
    // continuity with the epoch arithmetic, not because it is literally
    // milliseconds of virtual time.
    const CHECKPOINT_MS: u64 = 120_000;
    // Fraction of nodes that must have reached min_connections by the
    // checkpoint. Mirrors the >=90% topology-formation floor used by
    // test_connection_growth_stall_regression. Measured run reaches 95/100
    // (so the 90% floor has ~5 nodes of headroom).
    const REACHED_MIN_FRACTION: f64 = 0.90;

    // NOTE on connect_rejected: this test deliberately does NOT assert an upper
    // bound on the network-wide connect_rejected count. An earlier draft
    // asserted `connect_rejected < ~5936` (the broken-code baseline), but that
    // premise is structurally wrong. The broken code's LOW rejection count was
    // a SYMPTOM of stuck nodes: a capacity Rejected stamped a 30s→600s location
    // backoff and the joiner stopped retrying. The #4362 fix unblocks those
    // nodes by letting under-min joiners keep probing (throttled by a short
    // backoff), which inherently produces MORE connection attempts — hence MORE
    // terminus rejections — while actually forming the topology. Rejection
    // count therefore moves OPPOSITE to health here; the honest signal is the
    // reach-min timeline below. The count is logged for diagnostics only.

    // Replicate the deterministic sim epoch derived inside
    // run_controlled_simulation so transaction ULID timestamps can be mapped
    // back to a generation-order offset since simulation start (see the
    // CHECKPOINT_MS note above — this is an ordering proxy, not literal time).
    const BASE_EPOCH_MS: u64 = 1577836800000; // 2020-01-01 00:00:00 UTC
    const RANGE_MS: u64 = 5 * 365 * 24 * 60 * 60 * 1000; // ~5 years in ms
    let epoch_ms = BASE_EPOCH_MS + (SEED % RANGE_MS);

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            NUM_GATEWAYS,
            NUM_NODES,
            RING_MAX_HTL,
            7, // rnd_if_htl_above
            MAX_CONNECTIONS,
            MIN_CONNECTIONS,
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Seed one PUT so the network has real traffic, mirroring the diagnostic
    // test. The assertions key off topology events, not the GET outcomes.
    let contract = SimOperation::create_test_contract(0x35);
    let contract_id = *contract.key().id();
    let mut operations = vec![ScheduledOperation::new(
        NodeLabel::gateway(NETWORK_NAME, 0),
        SimOperation::Put {
            contract: contract.clone(),
            state: vec![0x35; 64],
            subscribe: true,
        },
    )];
    for i in 0..NUM_NODES {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(600),
        Duration::from_secs(120),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    let rt = create_runtime();
    let (connect_rejected, reached_min_by_checkpoint, total_peers_seen) = rt.block_on(async {
        let logs = logs_handle.lock().await;

        // (a) Count terminus-rejections across the whole run.
        let connect_rejected = logs
            .iter()
            .filter(|log| log.kind.is_connect_rejected())
            .count();

        // (b) Per-peer max open-connection count observed at any Connected
        // event whose transaction was created at or before the early-run
        // checkpoint. `Transaction::created_at_ms()` decodes the ULID
        // timestamp (GlobalSimulationTime = epoch + per-ULID counter), so this
        // is a deterministic GENERATION-ORDER filter — an early-run proxy, not
        // literal virtual time (see the CHECKPOINT_MS note above).
        let checkpoint_at_ms = epoch_ms.saturating_add(CHECKPOINT_MS);
        let mut max_count_by_peer: HashMap<_, usize> = HashMap::new();
        let mut peers_seen: HashSet<_> = HashSet::new();
        for log in logs.iter() {
            if let Some(count) = log.kind.connect_peer_connection_count() {
                peers_seen.insert(log.peer_id.clone());
                if log.tx.created_at_ms() <= checkpoint_at_ms {
                    let entry = max_count_by_peer.entry(log.peer_id.clone()).or_insert(0);
                    *entry = (*entry).max(count);
                }
            }
        }
        let reached_min = max_count_by_peer
            .values()
            .filter(|&&c| c >= MIN_CONNECTIONS)
            .count();
        (connect_rejected, reached_min, peers_seen.len())
    });

    tracing::info!(
        "=== Bootstrap acceptance timeline (#4362) ===\n\
         connect_rejected={} (diagnostic only — NOT asserted; see note above)\n\
         nodes reaching min_connections({}) by T+{}s: {}/{} peers seen",
        connect_rejected,
        MIN_CONNECTIONS,
        CHECKPOINT_MS / 1000,
        reached_min_by_checkpoint,
        total_peers_seen,
    );

    // Most nodes must reach min_connections by the mid-sim checkpoint.
    // Denominator is the full node count, so nodes that never emitted a
    // Connected event (still wedged in the dead zone) count against the floor.
    let required = (NUM_NODES as f64 * REACHED_MIN_FRACTION).ceil() as usize;
    assert!(
        reached_min_by_checkpoint >= required,
        "only {}/{} nodes reached min_connections({}) by T+{}s (needed >= {}) — \
         the bootstrap dead zone still parks late joiners below min (#4362)",
        reached_min_by_checkpoint,
        NUM_NODES,
        MIN_CONNECTIONS,
        CHECKPOINT_MS / 1000,
        required,
    );
}

/// Same as `test_get_reliability_diagnostic` but with realistic network delays.
///
/// Adds 50-200ms latency jitter and 5% message loss to simulate real-world conditions.
/// This should surface the ForwardingAck timeout issue from #3570: when relays ACK but
/// downstream chains stall, the originator's retry is permanently disabled.
///
/// The latency means multi-hop GETs accumulate realistic delays (e.g., 5 hops × 100ms avg
/// = 500ms per hop chain), which triggers the ACK_TIMEOUT (3s) and OPERATION_TTL (60s)
/// timing windows that are invisible in zero-latency simulations.
// Long-running diagnostic test (~3min). Runs in nightly CI.
#[cfg(feature = "nightly_tests")]
#[test_log::test]
fn test_get_reliability_with_latency() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};
    use freenet::simulation::FaultConfig;

    const SEED: u64 = 0x3570_D1A6_0002;
    const NETWORK_NAME: &str = "get-reliability-latency";
    const NUM_NODES: usize = 100;
    const NUM_GATEWAYS: usize = 3;

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (mut sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            NUM_GATEWAYS,
            NUM_NODES,
            10, // ring_max_htl
            7,  // rnd_if_htl_above
            12, // max_connections
            4,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Add realistic network delays: 50-200ms latency + 5% message loss
    sim.with_fault_injection(
        FaultConfig::builder()
            .latency_range(Duration::from_millis(50)..Duration::from_millis(200))
            .message_loss_rate(0.05)
            .build(),
    );

    let contract = SimOperation::create_test_contract(0x36);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();

    let mut operations = vec![
        // Gateway 0 PUTs the contract
        ScheduledOperation::new(
            NodeLabel::gateway(NETWORK_NAME, 0),
            SimOperation::Put {
                contract: contract.clone(),
                state: vec![0x36; 64],
                subscribe: true,
            },
        ),
    ];

    // Every node GETs the contract
    for i in 0..NUM_NODES {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(NETWORK_NAME, i),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(900), // 15 min — longer to account for latency
        Duration::from_secs(180), // 3 min post-operations wait
    );

    assert!(
        result.turmoil_result.is_ok(),
        "Simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // Analyze GET outcomes, deduplicated per attempt transaction (#4361).
    let rt = create_runtime();
    let summary = rt.block_on(async {
        let logs = logs_handle.lock().await;
        freenet::tracing::summarize_get_outcomes_per_tx(&logs)
    });
    let (successes, not_found, failures, timeouts) = (
        summary.successes,
        summary.not_found,
        summary.failures,
        summary.timeouts,
    );
    let total_outcomes = summary.total();

    // success_elapsed_ms is pre-sorted ascending.
    let sorted_latencies = &summary.success_elapsed_ms;

    let p50 = sorted_latencies
        .get(sorted_latencies.len() / 2)
        .copied()
        .unwrap_or(0);
    let p90 = sorted_latencies
        .get(sorted_latencies.len() * 9 / 10)
        .copied()
        .unwrap_or(0);
    let p99 = sorted_latencies
        .get(sorted_latencies.len() * 99 / 100)
        .copied()
        .unwrap_or(0);
    let max_latency = sorted_latencies.last().copied().unwrap_or(0);

    let success_rate = if total_outcomes > 0 {
        successes as f64 / total_outcomes as f64
    } else {
        0.0
    };

    tracing::info!("=== GET Reliability with Latency (#3570) ===");
    tracing::info!(
        "Network: {} gateways + {} nodes, latency=50-200ms, loss=5%",
        NUM_GATEWAYS,
        NUM_NODES
    );
    tracing::info!(
        "GET outcomes (per attempt tx): {} total — {} success ({} network-traversed), \
         {} not_found, {} failures, {} timeouts",
        total_outcomes,
        successes,
        summary.network_successes,
        not_found,
        failures,
        timeouts
    );
    tracing::info!(
        "GET success rate: {:.1}% ({}/{})",
        success_rate * 100.0,
        successes,
        total_outcomes
    );
    tracing::info!(
        "Latency (successful GETs): p50={}ms, p90={}ms, p99={}ms, max={}ms",
        p50,
        p90,
        p99,
        max_latency
    );

    // Check which nodes got the contract state
    let mut nodes_with_state = 0;
    let mut nodes_without_state = Vec::new();
    for i in 0..NUM_NODES {
        let label = NodeLabel::node(NETWORK_NAME, i);
        if let Some(storage) = result.node_storages.get(&label) {
            if storage.get_stored_state(&contract_key).is_some() {
                nodes_with_state += 1;
            } else {
                nodes_without_state.push(i);
            }
        } else {
            nodes_without_state.push(i);
        }
    }

    tracing::info!(
        "Storage verification: {}/{} nodes have contract state",
        nodes_with_state,
        NUM_NODES
    );
    if !nodes_without_state.is_empty() {
        tracing::info!(
            "Nodes missing state ({} total): {:?}{}",
            nodes_without_state.len(),
            &nodes_without_state[..nodes_without_state.len().min(20)],
            if nodes_without_state.len() > 20 {
                "..."
            } else {
                ""
            }
        );
    }

    // Compare with no-latency baseline
    tracing::info!(
        "=== Comparison with no-latency baseline ===\n\
         Baseline (no latency): 88.3% success, 72/100 nodes, p50=1ms, p90=754ms\n\
         This run (50-200ms latency, 5% loss): {:.1}% success, {}/{} nodes, p50={}ms, p90={}ms",
        success_rate * 100.0,
        nodes_with_state,
        NUM_NODES,
        p50,
        p90
    );

    // Soft assertion — diagnostic test, but flag catastrophic regression
    assert!(
        total_outcomes >= 10,
        "Only {} GET outcome events — too few for meaningful analysis",
        total_outcomes
    );

    // StateVerifier anomaly check
    let report = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let verifier = freenet::tracing::StateVerifier::from_events(logs.clone());
        verifier.verify()
    });
    tracing::info!(
        "Anomaly report: {} anomalies across {} contracts, {} total events",
        report.anomalies.len(),
        report.contracts_analyzed,
        report.total_events
    );

    tracing::info!(
        "test_get_reliability_with_latency DONE: {:.1}% success rate, \
         {}/{} nodes have state",
        success_rate * 100.0,
        nodes_with_state,
        NUM_NODES
    );
}

/// GET reliability under node churn (nodes randomly crashing and restarting).
///
/// Uses the direct runner with ChurnConfig to simulate realistic network dynamics:
/// - 100 nodes + 3 gateways
/// - 10% crash probability per tick, 3s recovery, max 5 simultaneous crashes
/// - 50-200ms latency + 5% message loss
/// - 300 random operations (PUTs, GETs, SUBSCRIBEs, UPDATEs)
///
/// Measures GET success rate under these conditions to see if churn degrades
/// reliability beyond what static routing exhaustion causes.
// Long-running diagnostic test (~4min). Runs in nightly CI.
#[cfg(feature = "nightly_tests")]
#[test_log::test]
fn test_get_reliability_with_churn() {
    use freenet::dev_tool::ChurnConfig;
    use freenet::simulation::FaultConfig;

    const SEED: u64 = 0x3570_D1A6_0003;
    const NETWORK_NAME: &str = "get-reliability-churn";
    const NUM_NODES: usize = 100;
    const NUM_GATEWAYS: usize = 3;

    GlobalTestMetrics::reset();
    setup_deterministic_state(SEED);

    let rt = create_runtime();

    let (mut sim, logs_handle) = rt.block_on(async {
        let sim = SimNetwork::new(
            NETWORK_NAME,
            NUM_GATEWAYS,
            NUM_NODES,
            10, // ring_max_htl
            7,  // rnd_if_htl_above
            12, // max_connections
            4,  // min_connections
            SEED,
        )
        .await;
        let logs_handle = sim.event_logs_handle();
        (sim, logs_handle)
    });

    // Add realistic network delays
    sim.with_fault_injection(
        FaultConfig::builder()
            .latency_range(Duration::from_millis(50)..Duration::from_millis(200))
            .message_loss_rate(0.05)
            .build(),
    );

    // Add node churn: 10% crash rate, 3s recovery, max 5 simultaneous crashes
    sim.with_churn(ChurnConfig {
        crash_probability: 0.10,
        tick_interval: Duration::from_secs(5),
        recovery_delay: Duration::from_secs(3),
        max_simultaneous_crashes: Some(5),
        permanent_crash_rate: 0.02, // 2% of crashes are permanent
        warmup_delay: Duration::from_secs(10),
    });

    drop(rt);

    // Run with random operations — direct runner handles churn
    let direct_result = sim.run_simulation_direct::<rand::rngs::SmallRng>(
        SEED,
        15,  // max_contract_num
        300, // iterations — enough to generate many GETs
        Duration::from_millis(500),
    );

    if let Err(e) = &direct_result {
        tracing::warn!("Direct simulation completed with error (may be expected under churn): {e}");
    }

    // Analyze GET outcomes from event logs, deduplicated per attempt
    // transaction (#4361).
    let rt = create_runtime();
    let summary = rt.block_on(async {
        let logs = logs_handle.lock().await;
        freenet::tracing::summarize_get_outcomes_per_tx(&logs)
    });
    let (successes, not_found, failures, timeouts) = (
        summary.successes,
        summary.not_found,
        summary.failures,
        summary.timeouts,
    );
    let total_outcomes = summary.total();

    // success_elapsed_ms is pre-sorted ascending.
    let sorted_latencies = &summary.success_elapsed_ms;

    let p50 = sorted_latencies
        .get(sorted_latencies.len() / 2)
        .copied()
        .unwrap_or(0);
    let p90 = sorted_latencies
        .get(sorted_latencies.len() * 9 / 10)
        .copied()
        .unwrap_or(0);
    let p99 = sorted_latencies
        .get(sorted_latencies.len() * 99 / 100)
        .copied()
        .unwrap_or(0);
    let max_latency = sorted_latencies.last().copied().unwrap_or(0);

    let success_rate = if total_outcomes > 0 {
        successes as f64 / total_outcomes as f64
    } else {
        0.0
    };

    tracing::info!("=== GET Reliability with Churn (#3570) ===");
    tracing::info!(
        "Network: {} gateways + {} nodes, latency=50-200ms, loss=5%, churn=10%/5s",
        NUM_GATEWAYS,
        NUM_NODES
    );
    tracing::info!(
        "GET outcomes (per attempt tx): {} total — {} success ({} network-traversed), \
         {} not_found, {} failures, {} timeouts",
        total_outcomes,
        successes,
        summary.network_successes,
        not_found,
        failures,
        timeouts
    );
    tracing::info!(
        "GET success rate: {:.1}% ({}/{})",
        success_rate * 100.0,
        successes,
        total_outcomes
    );
    tracing::info!(
        "Latency (successful GETs): p50={}ms, p90={}ms, p99={}ms, max={}ms",
        p50,
        p90,
        p99,
        max_latency
    );

    // Compare with previous variants
    tracing::info!(
        "=== Comparison ===\n\
         Baseline (no latency):         88.3% success, p90=754ms\n\
         With latency (50-200ms, 5%):   81.9% success, p90=1833ms\n\
         With churn + latency:          {:.1}% success, p90={}ms",
        success_rate * 100.0,
        p90
    );

    // Soft assertion — diagnostic test
    if total_outcomes >= 10 {
        tracing::info!(
            "test_get_reliability_with_churn DONE: {:.1}% GET success rate \
             ({} outcomes from 300 random operations under churn)",
            success_rate * 100.0,
            total_outcomes
        );
    } else {
        tracing::warn!(
            "test_get_reliability_with_churn: only {} GET outcomes — \
             random event generation may not have produced enough GETs",
            total_outcomes
        );
    }
}

/// Nightly: 50-node topology formation with strict connectivity assertions.
///
/// Verifies that a 50-node network converges to `min_connections` within 1 virtual hour.
/// Uses the Direct Runner for scale (50+ nodes) and determinism.
///
/// **Assertions (per testing.md realism requirements):**
/// 1. Median connections >= `min_connections` (strict, not min-1)
/// 2. >= 90% of nodes reach `min_connections`
/// 3. >= 80% of nodes have non-gateway peer connections
#[cfg(feature = "nightly_tests")]
#[test_log::test(tokio::test(flavor = "current_thread"))]
async fn test_nightly_50_node_topology_formation() {
    use freenet::dev_tool::NodeLabel;

    const SEED: u64 = 0x3511_5000_0001;
    const NETWORK_NAME: &str = "nightly-50-topology";
    const GATEWAYS: usize = 4;
    const NODES: usize = 50;
    const RING_MAX_HTL: usize = 10;
    const RND_IF_HTL_ABOVE: usize = 5;
    const MAX_CONN: usize = 20;
    const MIN_CONN: usize = 10;
    const VIRTUAL_DURATION: Duration = Duration::from_secs(3600); // 1 hour

    tracing::info!("=== Nightly: 50-Node Topology Formation ===");

    setup_deterministic_state(SEED);

    let mut sim = SimNetwork::new(
        NETWORK_NAME,
        GATEWAYS,
        NODES,
        RING_MAX_HTL,
        RND_IF_HTL_ABOVE,
        MAX_CONN,
        MIN_CONN,
        SEED,
    )
    .await;

    sim.with_start_backoff(Duration::from_millis(50));

    let _handles = sim
        .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 0, 0)
        .await;

    tracing::info!("Running 1 virtual hour of topology formation...");
    let_network_run(&mut sim, VIRTUAL_DURATION).await;

    // Collect per-node connection counts. `connection_count` returns `Option`
    // and `filter_map` may drop nodes whose ConnectionManager is briefly
    // unavailable; this matches the baseline `test_connection_growth_stall_regression`
    // pattern. Percentages are computed against the sampled population, same
    // as the baseline test.
    let mut node_counts: Vec<usize> = (0..NODES)
        .filter_map(|i| {
            let label = NodeLabel::node(NETWORK_NAME, i);
            sim.connection_count(&label)
        })
        .collect();
    node_counts.sort_unstable();

    let num_sampled = node_counts.len();
    assert!(num_sampled > 0, "No connection managers available");

    let median_conn = node_counts[num_sampled / 2];
    let nodes_above_min = node_counts.iter().filter(|&&c| c >= MIN_CONN).count();
    let fraction_above_min = nodes_above_min as f64 / num_sampled as f64;

    tracing::info!("Connection counts: {:?}", node_counts);
    tracing::info!(
        "Median={}, nodes at min_connections={}/{} ({:.0}%)",
        median_conn,
        nodes_above_min,
        num_sampled,
        fraction_above_min * 100.0
    );

    // Check non-gateway peer connections
    let connectivity = sim.node_connectivity();
    let mut nodes_with_peer_connections = 0usize;
    for (label, (_key, conns)) in &connectivity {
        if !label.is_gateway() && conns.keys().any(|peer| !peer.is_gateway()) {
            nodes_with_peer_connections += 1;
        }
    }
    let peer_conn_fraction = nodes_with_peer_connections as f64 / NODES as f64;

    tracing::info!(
        "Nodes with non-gateway peer connections: {}/{} ({:.0}%)",
        nodes_with_peer_connections,
        NODES,
        peer_conn_fraction * 100.0
    );

    // ASSERTION 1: Median connections >= min_connections (strict).
    // With 50 nodes and 1 hour, the network must fully converge.
    assert!(
        median_conn >= MIN_CONN,
        "Topology formation stall: median={} < min_connections={}. \
         Counts: {:?}. Seed: 0x{:X}",
        median_conn,
        MIN_CONN,
        node_counts,
        SEED
    );

    // ASSERTION 2: >= 90% of nodes reach min_connections (per testing.md).
    assert!(
        fraction_above_min >= 0.90,
        "Only {:.0}% of nodes reached min_connections (expected >= 90%). \
         {}/{} nodes. Counts: {:?}. Seed: 0x{:X}",
        fraction_above_min * 100.0,
        nodes_above_min,
        num_sampled,
        node_counts,
        SEED
    );

    // ASSERTION 3: >= 80% of nodes have non-gateway peer connections.
    assert!(
        peer_conn_fraction >= 0.80,
        "Only {:.0}% of nodes have non-gateway peer connections (expected >= 80%). \
         CONNECT forwarding is insufficient. Seed: 0x{:X}",
        peer_conn_fraction * 100.0,
        SEED
    );

    tracing::info!(
        "PASSED: median={}, above_min={:.0}%, peer_conns={:.0}%",
        median_conn,
        fraction_above_min * 100.0,
        peer_conn_fraction * 100.0
    );
}

/// Nightly: Connection growth rate checkpoints — monotonic growth toward `min_connections`.
///
/// Measures connection counts at 5m, 15m, 30m, 60m virtual time and asserts that
/// median connections grow monotonically. Catches growth plateaus and stalls that
/// are invisible in short-duration CI tests.
///
/// **Assertions:**
/// 1. Median connections at each checkpoint >= previous checkpoint (monotonic growth)
/// 2. Final median >= `min_connections`
/// 3. No checkpoint after 5m has median = 0 (no network collapse)
#[cfg(feature = "nightly_tests")]
#[test_log::test(tokio::test(flavor = "current_thread"))]
async fn test_nightly_connection_growth_checkpoints() {
    use freenet::dev_tool::NodeLabel;

    const SEED: u64 = 0x3511_6C8E_0002;
    const NETWORK_NAME: &str = "nightly-growth-checkpoints";
    const GATEWAYS: usize = 4;
    const NODES: usize = 50;
    const RING_MAX_HTL: usize = 10;
    const RND_IF_HTL_ABOVE: usize = 5;
    const MAX_CONN: usize = 20;
    const MIN_CONN: usize = 10;

    // Checkpoint intervals (cumulative seconds from start)
    const CHECKPOINTS_SECS: [u64; 4] = [300, 900, 1800, 3600]; // 5m, 15m, 30m, 60m

    tracing::info!("=== Nightly: Connection Growth Checkpoints ===");

    setup_deterministic_state(SEED);

    let mut sim = SimNetwork::new(
        NETWORK_NAME,
        GATEWAYS,
        NODES,
        RING_MAX_HTL,
        RND_IF_HTL_ABOVE,
        MAX_CONN,
        MIN_CONN,
        SEED,
    )
    .await;

    sim.with_start_backoff(Duration::from_millis(50));

    let _handles = sim
        .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 0, 0)
        .await;

    let mut checkpoint_medians: Vec<(u64, usize)> = Vec::new();
    let mut elapsed_so_far = 0u64;

    for &target_secs in &CHECKPOINTS_SECS {
        let delta = target_secs - elapsed_so_far;
        let_network_run(&mut sim, Duration::from_secs(delta)).await;
        elapsed_so_far = target_secs;

        let mut counts: Vec<usize> = (0..NODES)
            .filter_map(|i| {
                let label = NodeLabel::node(NETWORK_NAME, i);
                sim.connection_count(&label)
            })
            .collect();
        counts.sort_unstable();

        let median = if counts.is_empty() {
            0
        } else {
            counts[counts.len() / 2]
        };

        tracing::info!(
            "Checkpoint @{}m: median={}, counts={:?}",
            target_secs / 60,
            median,
            counts
        );

        // No total network collapse after the bootstrap phase.
        assert!(
            median > 0 || elapsed_so_far <= 300,
            "Network collapse at checkpoint @{}m: median=0. Counts: {:?}. Seed: 0x{:X}",
            target_secs / 60,
            counts,
            SEED
        );

        checkpoint_medians.push((target_secs, median));
    }

    // Near-monotonic growth — each checkpoint median is within 1 connection of
    // the previous. Topology-aware pruning legitimately trades a single
    // connection for distribution quality, so a transient -1 dip is healthy
    // behavior, not a stall.
    for window in checkpoint_medians.windows(2) {
        let (prev_t, prev_median) = window[0];
        let (curr_t, curr_median) = window[1];
        assert!(
            curr_median + 1 >= prev_median,
            "Connection growth regressed > 1 between @{}m (median={}) and @{}m (median={}). \
             Growth must be near-monotonic. Seed: 0x{:X}",
            prev_t / 60,
            prev_median,
            curr_t / 60,
            curr_median,
            SEED
        );
    }

    // Final checkpoint median >= min_connections.
    let (_, final_median) = checkpoint_medians.last().unwrap();
    assert!(
        *final_median >= MIN_CONN,
        "Final median={} < min_connections={} after 60 virtual minutes. \
         Checkpoints: {:?}. Seed: 0x{:X}",
        final_median,
        MIN_CONN,
        checkpoint_medians,
        SEED
    );

    tracing::info!("PASSED: checkpoints={:?}", checkpoint_medians);
}

/// Nightly: Fault injection and recovery speed — network self-heals within bounded time.
///
/// Three phases:
/// 1. **Convergence** (30 virtual min): Let network form connections normally.
/// 2. **Fault injection** (5 virtual min): 20% message loss simulating NAT failures.
/// 3. **Recovery** (25 virtual min): Remove faults, verify network recovers.
///
/// **Assertions:**
/// 1. Pre-fault median >= `min_connections` (network converged)
/// 2. No death spiral during faults (median > 0)
/// 3. Post-recovery median >= pre-fault median - 1 (bounded recovery)
/// 4. < 5% nodes isolated after recovery
#[cfg(feature = "nightly_tests")]
#[test_log::test(tokio::test(flavor = "current_thread"))]
async fn test_nightly_fault_recovery_speed() {
    use freenet::dev_tool::NodeLabel;
    use freenet::simulation::FaultConfig;

    const SEED: u64 = 0x3511_FA17_0003;
    const NETWORK_NAME: &str = "nightly-fault-recovery";
    const GATEWAYS: usize = 4;
    const NODES: usize = 50;
    const RING_MAX_HTL: usize = 10;
    const RND_IF_HTL_ABOVE: usize = 5;
    const MAX_CONN: usize = 20;
    const MIN_CONN: usize = 10;

    tracing::info!("=== Nightly: Fault Recovery Speed ===");

    setup_deterministic_state(SEED);

    let mut sim = SimNetwork::new(
        NETWORK_NAME,
        GATEWAYS,
        NODES,
        RING_MAX_HTL,
        RND_IF_HTL_ABOVE,
        MAX_CONN,
        MIN_CONN,
        SEED,
    )
    .await;

    sim.with_start_backoff(Duration::from_millis(50));

    let _handles = sim
        .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 0, 0)
        .await;

    // ── Phase 1: Convergence (30 virtual minutes, no faults) ──────────────────
    tracing::info!("Phase 1: Convergence — 30 virtual minutes, no faults");
    let_network_run(&mut sim, Duration::from_secs(1800)).await;

    // Regular-node IDs start at `GATEWAYS`, not 0. `SimNetwork::config_nodes`
    // iterates `number_of_gateways..number_of_gateways + num` when constructing
    // labels, so for GATEWAYS=4 / NODES=50 the regular-node labels are
    // `NodeLabel::node(NETWORK_NAME, 4..54)`. Querying `(0..NODES)` would miss
    // the top `GATEWAYS` nodes and collect an off-by-GATEWAYS subset — that was
    // the deterministic `got 46, expected 50` nightly failure on seed
    // 0x3511FA170003 before this fix. Iterate over the actual live-label range
    // so every assertion in this test (Phase 1, 2, 3) sees the full 50-node
    // population.
    let node_ids = || GATEWAYS..GATEWAYS + NODES;

    let mut pre_fault_counts: Vec<usize> = node_ids()
        .filter_map(|i| {
            let label = NodeLabel::node(NETWORK_NAME, i);
            sim.connection_count(&label)
        })
        .collect();
    pre_fault_counts.sort_unstable();
    assert!(
        !pre_fault_counts.is_empty(),
        "Phase 1: no connection managers available. Seed: 0x{:X}",
        SEED
    );
    let pre_fault_median = pre_fault_counts[pre_fault_counts.len() / 2];

    tracing::info!(
        "Phase 1 done: median={}, counts={:?}",
        pre_fault_median,
        pre_fault_counts
    );

    // Network must converge before we inject faults.
    assert!(
        pre_fault_median >= MIN_CONN,
        "Network did not converge before fault injection: median={} < min_connections={}. \
         Counts: {:?}. Seed: 0x{:X}",
        pre_fault_median,
        MIN_CONN,
        pre_fault_counts,
        SEED
    );

    // ── Phase 2: Fault injection (5 virtual minutes, 20% message loss) ────────
    tracing::info!("Phase 2: Fault injection — 20% message loss for 5 virtual minutes");
    sim.with_fault_injection(FaultConfig::builder().message_loss_rate(0.20).build());

    let_network_run(&mut sim, Duration::from_secs(300)).await;

    let mut during_fault_counts: Vec<usize> = node_ids()
        .filter_map(|i| {
            let label = NodeLabel::node(NETWORK_NAME, i);
            sim.connection_count(&label)
        })
        .collect();
    during_fault_counts.sort_unstable();
    assert_eq!(
        during_fault_counts.len(),
        NODES,
        "Phase 2: expected connection managers for all {} nodes, got {}. Seed: 0x{:X}",
        NODES,
        during_fault_counts.len(),
        SEED
    );
    let during_fault_median = during_fault_counts[during_fault_counts.len() / 2];

    tracing::info!(
        "Phase 2 done: median={}, counts={:?}",
        during_fault_median,
        during_fault_counts
    );

    // Resilience under load: 20% message loss must not push the median below
    // half of MIN_CONN. The previous `> 0` bar was degenerate (a single peer
    // surviving on the median node would pass), so it could not detect a
    // partial death spiral. Half-of-min is the smallest threshold strict
    // enough to fail on real cascade collapse but loose enough to tolerate
    // brief pruning during a 5-minute fault window.
    let during_fault_floor = MIN_CONN / 2;
    assert!(
        during_fault_median >= during_fault_floor,
        "Connection collapse under load: during_fault_median={} < MIN_CONN/2={}. \
         Counts: {:?}. Seed: 0x{:X}",
        during_fault_median,
        during_fault_floor,
        during_fault_counts,
        SEED
    );

    // ── Phase 3: Recovery (25 virtual minutes, faults cleared) ────────────────
    tracing::info!("Phase 3: Recovery — faults cleared, 25 virtual minutes");
    sim.clear_fault_injection();

    let_network_run(&mut sim, Duration::from_secs(1500)).await;

    let mut post_recovery_counts: Vec<usize> = node_ids()
        .filter_map(|i| {
            let label = NodeLabel::node(NETWORK_NAME, i);
            sim.connection_count(&label)
        })
        .collect();
    post_recovery_counts.sort_unstable();
    assert_eq!(
        post_recovery_counts.len(),
        NODES,
        "Phase 3: expected connection managers for all {} nodes, got {}. Seed: 0x{:X}",
        NODES,
        post_recovery_counts.len(),
        SEED
    );

    let post_recovery_median = post_recovery_counts[post_recovery_counts.len() / 2];
    let isolated_count = post_recovery_counts.iter().filter(|&&c| c == 0).count();
    let fraction_isolated = isolated_count as f64 / NODES as f64;

    tracing::info!(
        "Phase 3 done: median={}, isolated={}/{} ({:.0}%), counts={:?}",
        post_recovery_median,
        isolated_count,
        NODES,
        fraction_isolated * 100.0,
        post_recovery_counts
    );

    // Recovery to near pre-fault levels AND back above MIN_CONN. Without the
    // MIN_CONN floor, `pre_fault_median.saturating_sub(1)` could allow the
    // post-recovery median to fall below the documented success criterion
    // (e.g. pre=10, post=9 would silently pass).
    let recovery_floor = pre_fault_median.saturating_sub(1).max(MIN_CONN);
    assert!(
        post_recovery_median >= recovery_floor,
        "Incomplete recovery: post_recovery_median={} < floor={} \
         (max(pre_fault_median - 1, MIN_CONN)). \
         Pre-fault: {:?}, Post-recovery: {:?}. Seed: 0x{:X}",
        post_recovery_median,
        recovery_floor,
        pre_fault_counts,
        post_recovery_counts,
        SEED
    );

    // < 5% nodes isolated after recovery.
    assert!(
        fraction_isolated < 0.05,
        "{:.0}% of nodes isolated after recovery (threshold: 5%). \
         Counts: {:?}. Seed: 0x{:X}",
        fraction_isolated * 100.0,
        post_recovery_counts,
        SEED
    );

    tracing::info!(
        "PASSED: pre_fault_median={}, during_fault_median={}, post_recovery_median={}, \
         isolated={:.0}%",
        pre_fault_median,
        during_fault_median,
        post_recovery_median,
        fraction_isolated * 100.0
    );
}

/// Regression test for the 2026-04-15 nightly failure: pins the
/// `SimNetwork::config_nodes` regular-node-label indexing convention so
/// future tests can't silently re-introduce the off-by-GATEWAYS bug that
/// broke `test_nightly_fault_recovery_speed`.
///
/// The convention (`crates/core/src/node/testing_impl.rs::config_nodes`,
/// circa line 1580) iterates
/// `number_of_gateways..num + number_of_gateways` when constructing
/// regular-node labels. For a SimNetwork with G gateways and N nodes, the
/// live regular-node labels are therefore
/// `NodeLabel::node(name, G..G + N)` — NOT `0..N`. A test that iterates
/// `0..N` and calls `NodeLabel::node(name, i)` will miss the top G entries
/// entirely, deterministically getting `N - G` labels instead of `N`.
///
/// This test uses a small SimNetwork (2 gateways, 3 regular nodes, fast
/// startup) and verifies:
///   1. `node-0` and `node-1` (inside the gateway ID range) are NOT live
///      regular-node labels.
///   2. `node-2`, `node-3`, `node-4` (the `GATEWAYS..GATEWAYS + NODES`
///      range) ARE live regular-node labels.
///   3. `node-5` (one past the end) is NOT a live regular-node label.
///
/// Pins the `NodeLabel::node` / `NodeLabel::gateway` string format that
/// `SimNetwork::config_nodes` and every downstream test query rely on.
/// Keeping this as a plain `#[test]` (not `#[tokio::test]`) ensures the
/// CI rule-lint regex `#\[(tokio::)?test\]` counts it as a regression
/// test — the async `sim_network_regular_node_labels_start_at_gateway_count`
/// below is annotated with `#[test_log::test(...)]` which the lint grep
/// does not recognize. Both tests pin the same bug class at different
/// layers: this one pins the label encoding, the async one pins the
/// live-label ID range.
#[test]
fn node_label_node_and_gateway_formats_are_stable() {
    // Expected encoding is "<network>-node-<id>" and "<network>-gateway-<id>".
    assert_eq!(
        NodeLabel::node("net", 5).to_string(),
        "net-node-5",
        "NodeLabel::node string format must remain stable"
    );
    assert_eq!(
        NodeLabel::gateway("net", 2).to_string(),
        "net-gateway-2",
        "NodeLabel::gateway string format must remain stable"
    );
    // Gateway and regular-node labels at the same numeric ID must be
    // distinct, because `config_nodes` and the test indexing convention
    // rely on the two label spaces being disjoint by type-prefix — not
    // by numeric range. A future refactor that conflates them would
    // silently corrupt every connection-count assertion.
    assert_ne!(
        NodeLabel::node("net", 0),
        NodeLabel::gateway("net", 0),
        "gateway and regular-node labels must be distinct at the same numeric ID"
    );
    // And `node 5` must not equal `gateway 5` either.
    assert_ne!(NodeLabel::node("net", 5), NodeLabel::gateway("net", 5));
}

#[test_log::test(tokio::test(flavor = "current_thread"))]
async fn sim_network_regular_node_labels_start_at_gateway_count() {
    const NETWORK_NAME: &str = "label-indexing-regression";
    const GATEWAYS: usize = 2;
    const NODES: usize = 3;
    const SEED: u64 = 0xABCD_1234;

    setup_deterministic_state(SEED);

    let mut sim = SimNetwork::new(
        NETWORK_NAME,
        GATEWAYS,
        NODES,
        /* ring_max_htl */ 3,
        /* rnd_if_htl_above */ 2,
        /* max_connections */ 4,
        /* min_connections */ 2,
        SEED,
    )
    .await;

    let _handles = sim
        .start_with_rand_gen::<rand::rngs::SmallRng>(SEED, 0, 0)
        .await;

    // Labels inside the gateway ID range (0..GATEWAYS) must NOT exist as
    // regular-node labels, even though `NodeLabel::node(name, i)` is a
    // syntactically valid label for any `i`. The rule being pinned is that
    // the *live* regular-node set starts at `GATEWAYS`, not 0.
    for i in 0..GATEWAYS {
        assert!(
            sim.connection_count(&NodeLabel::node(NETWORK_NAME, i))
                .is_none(),
            "node-{i} must NOT be a live regular-node label (inside gateway range)"
        );
    }

    // The live regular-node range is `GATEWAYS..GATEWAYS + NODES`.
    for i in GATEWAYS..GATEWAYS + NODES {
        assert!(
            sim.connection_count(&NodeLabel::node(NETWORK_NAME, i))
                .is_some(),
            "node-{i} must be a live regular-node label"
        );
    }

    // One past the end must not be live either.
    assert!(
        sim.connection_count(&NodeLabel::node(NETWORK_NAME, GATEWAYS + NODES))
            .is_none(),
        "node-{} must NOT be a live regular-node label (past end of range)",
        GATEWAYS + NODES
    );
}

// =============================================================================
// hop_count Regression Test (simulation-level, currently disabled)
// =============================================================================
//
// Direct regression coverage for the wire-format fix is provided by
// `test_get_msg_response_hop_count_roundtrip` in `crates/core/src/operations/get.rs`.
// That unit test asserts the new `hop_count` field roundtrips through bincode
// for both `Found` and `NotFound` variants, which is the specific regression
// scenario this PR addresses.
//
// The simulation-level integration test below is left in tree, but marked
// `#[ignore]` because the default `TestConfig`/`event_chain` workload at the
// scales reasonable for CI does not reliably produce terminal GET events
// (most operations are PUT/UPDATE/SUBSCRIBE).  The `nightly_tests`-gated
// `test_get_reliability_diagnostic` above does drive GETs via
// `run_controlled_simulation`; once that pattern is generalised, this test
// can be rebuilt on top of it without `#[ignore]`.
#[test_log::test]
#[ignore = "tracking issue #4250 — default event_chain workload doesn't produce terminal GETs at CI scales; primary regression coverage lives in test_get_msg_response_hop_count_roundtrip (get.rs) and classify_response_found_preserves_hop_count (op_ctx_task.rs). Un-ignore once a deterministic GET-producing workload is wired through TestConfig"]
fn test_hop_count_populated_on_terminal_get_events() {
    // Parameters match test_router_accumulates_feedback_events (a known-good
    // workload that produces GET events in CI).
    let result = TestConfig::small("hop-count-regression", 0xCAFE_0001)
        .with_nodes(4)
        .with_max_contracts(5)
        .with_iterations(50)
        .with_duration(Duration::from_secs(60))
        .with_sleep(Duration::from_secs(2))
        .run()
        .assert_ok();

    // TestConfig::small uses ring_max_htl = 7, so any populated hop_count must
    // be within [0, 7].  Anything outside that range would indicate either
    // garbage-value propagation or a regression in how relays compute their
    // forward depth.
    const RING_MAX_HTL: usize = 7;

    let rt = create_runtime();
    let (populated, max_hop) = rt.block_on(async {
        let logs = result.logs_handle.lock().await;
        let hop_counts: Vec<usize> = logs
            .iter()
            .filter(|m| m.kind.variant_name() == "Get")
            .filter_map(|m| m.kind.hop_count())
            .collect();
        let max_hop = hop_counts.iter().copied().max().unwrap_or(0);
        (hop_counts.len(), max_hop)
    });

    // Presence: before the fix the wire-carried hop_count is dropped on the
    // floor and every terminal GET event has hop_count = None.  After the
    // fix every terminal GET event carries it.
    assert!(
        populated > 0,
        "expected at least one terminal GET event with populated hop_count; \
         got 0.  Indicates hop_count is no longer being threaded through the \
         GetMsg::Response wire format (PR #4245)."
    );

    // Sanity-bound: hop_count is computed as `max_htl - htl` on the storer
    // side (and at exhaustion relays), so it must be within [0, max_htl].
    // A regression that, say, started writing the originator's htl_max into
    // the field instead of (max_htl - htl) would trip this.
    assert!(
        max_hop <= RING_MAX_HTL,
        "max observed hop_count {} exceeds ring_max_htl {}; suggests garbage \
         value propagation rather than (max_htl - htl) accounting",
        max_hop,
        RING_MAX_HTL
    );
}

/// Verifies the contract-placement migration (#4404): a contract held only by a
/// peer FAR from its key migrates onto the cluster of peers CLOSEST to the key,
/// resolving the GET dead-end.
///
/// Setup (peer ring locations controlled via `new_with_node_locations`):
///   - a dense cluster of peers sits right on the contract's key location;
///   - exactly one peer, far from the key, initially hosts the contract (seeded
///     into its store AND Ring hosting manager, with no network propagation);
///   - a requester, also far from the key, issues a GET.
///
/// GET is single-path greedy (k=1), so it routes toward the key and reaches the
/// close cluster. WITHOUT the migration that cluster lacks the state, so the GET
/// dead-ends with NotFound (the far host is never on the greedy path toward the
/// key) — that is the placement gap #4404 describes. The migration nudges the
/// contract from the far host toward the key: each hosting peer, on gaining a
/// connected neighbor strictly closer to the key, sends a `SubscribeHint` so
/// that neighbor directed-subscribes through the holder and begins hosting. The
/// contract therefore climbs onto the close cluster, and a key-routed GET now
/// lands on a host. (The dead-end itself is not asserted separately here; the
/// migration trigger is always-on, so this test pins the resolved state.)
///
/// Asserts the migration outcome directly via each node's live Ring: the far
/// host still hosts the contract, and at least one close-cluster peer (the peers
/// a key-routed GET actually reaches) ends up hosting it via migration.
#[test_log::test]
fn test_contract_migrates_to_close_cluster_resolving_get_dead_end() {
    use freenet::dev_tool::{Location, NodeLabel, ScheduledOperation, SimNetwork, SimOperation};

    const SEED: u64 = 0xDEAD_F00D_0001;
    const NETWORK_NAME: &str = "get-placement-deadend";
    setup_deterministic_state(SEED);

    // Pick a contract and read its ring location; place peers relative to it.
    let contract = SimOperation::create_test_contract(0xBE);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    let key_loc = Location::from(&contract_key).as_f64();

    // Six peers clustered tightly around the key (none will host it), then one
    // far host (seeded) and one far requester. `rem_euclid` wraps onto the ring.
    let wrap = |x: f64| x.rem_euclid(1.0);
    let cluster: Vec<f64> = [-0.010, -0.006, -0.003, 0.003, 0.006, 0.010]
        .iter()
        .map(|o| wrap(key_loc + o))
        .collect();
    let host_loc = wrap(key_loc + 0.40); // far from the key
    let requester_loc = wrap(key_loc + 0.70); // far from the key, other side
    let mut node_locations = cluster.clone();
    node_locations.push(host_loc); // regular-node order 6 -> node_no 7
    node_locations.push(requester_loc); // regular-node order 7 -> node_no 8
    let num_nodes = node_locations.len(); // 8

    let rt = create_runtime();
    let mut sim = rt.block_on(async {
        SimNetwork::new_with_node_locations(
            NETWORK_NAME,
            1,         // 1 gateway
            num_nodes, // 8 regular nodes
            10,        // ring_max_htl
            7,         // rnd_if_htl_above
            8,         // max_connections
            3,         // min_connections
            SEED,
            &node_locations,
        )
        .await
    });
    // Opt this simulation into the placement-migration cascade (off by default in
    // sim so it can't perturb unrelated tests, e.g. the streaming assembly-retry
    // test). This lowers the per-node SubscribeHint version floor to (0,0,0).
    sim.enable_placement_migration();

    // Regular nodes are node_no = 1..=8 (gateway is node 0); node_locations[i]
    // maps to node_no i+1.
    let host_label = NodeLabel::node(NETWORK_NAME, 7); // host_loc
    let requester_label = NodeLabel::node(NETWORK_NAME, 8); // requester_loc

    // Setup sanity: a cluster node must be strictly closer to the key than the
    // host, so a key-routed GET genuinely lands in the (non-hosting) cluster
    // rather than near the holder. get_peer_locations() is [gateway, node1..8].
    let locs = sim.get_peer_locations();
    let ring_dist = |a: f64, b: f64| {
        let d = (a - b).abs();
        d.min(1.0 - d)
    };
    let host_dist = ring_dist(locs[7], key_loc);
    let cluster_min = (1..=6)
        .map(|i| ring_dist(locs[i], key_loc))
        .fold(f64::INFINITY, f64::min);
    assert!(
        cluster_min < host_dist,
        "scenario setup wrong: a cluster node must be closer to the key than the host \
         (cluster_min={cluster_min}, host_dist={host_dist})"
    );

    let operations = vec![
        // Only the far host holds the contract initially — seeded into both its
        // store and Ring hosting manager (no network propagation), so it is a
        // genuine migration source (`ring.is_hosting_contract` is true).
        ScheduledOperation::new(
            host_label.clone(),
            SimOperation::SeedHostedContract {
                contract: contract.clone(),
                state: vec![10, 20, 30, 40],
            },
        ),
        // The far requester asks for it (greedy GET toward the key).
        ScheduledOperation::new(
            requester_label.clone(),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(60),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // The far host still hosts the seeded contract (it remains a source).
    assert!(
        result.is_node_hosting(&host_label, &contract_key),
        "host should still host the seeded contract after the simulation"
    );

    // CORE OF THE FIX: the contract migrated onto the close cluster. At least
    // one of the peers a key-routed GET actually reaches (node_no 1..=6) now
    // hosts it. Before the migration NONE of them did — this test previously
    // asserted exactly that dead-end (see git history). With the contract now
    // present on a peer the greedy path lands on, the dead-end is resolved.
    let migrated: Vec<usize> = (1..=6usize)
        .filter(|n| result.is_node_hosting(&NodeLabel::node(NETWORK_NAME, *n), &contract_key))
        .collect();
    let requester_has_state = result
        .node_storages
        .get(&requester_label)
        .is_some_and(|s| s.get_stored_state(&contract_key).is_some());
    assert!(
        !migrated.is_empty(),
        "placement migration FAILED: no close-cluster peer (node_no 1..=6) hosts the \
         contract after the simulation. The contract never migrated from the far host \
         toward the key, so a key-routed GET would still dead-end. \
         host_hosting={}, requester_has_state={requester_has_state}",
        result.is_node_hosting(&host_label, &contract_key),
    );

    // End-to-end payoff: with the contract migrated onto the close cluster, the
    // requester's greedy GET toward the key now lands on a host instead of
    // dead-ending, so it obtains the state. This is the user-visible symptom
    // from the original telemetry (a web GET that needed several retries before
    // the contract had migrated onto the key-close peers).
    assert!(
        requester_has_state,
        "requester GET should now succeed: with the contract migrated onto the close \
         cluster, the greedy GET toward the key lands on a host instead of dead-ending \
         (migrated cluster nodes: {migrated:?})"
    );

    tracing::info!(
        migrated_cluster_nodes = ?migrated,
        requester_has_state,
        "placement migration converged onto the close cluster"
    );
}

/// Negative control for `test_contract_migrates_to_close_cluster_resolving_get_dead_end`.
///
/// IDENTICAL scenario, but WITHOUT `enable_placement_migration()` — so the
/// SubscribeHint cascade stays off (sim peers report a build version below the
/// production floor). This reproduces the #4404 dead-end and, paired with the
/// positive test, proves that the migration cascade (not some incidental GET
/// caching path) is what makes the close cluster host the contract: same seed,
/// same topology, the ONLY difference is whether migration is enabled.
#[test_log::test]
fn test_get_dead_ends_at_close_cluster_without_migration() {
    use freenet::dev_tool::{Location, NodeLabel, ScheduledOperation, SimNetwork, SimOperation};

    const SEED: u64 = 0xDEAD_F00D_0001;
    const NETWORK_NAME: &str = "get-placement-deadend-control";
    setup_deterministic_state(SEED);

    let contract = SimOperation::create_test_contract(0xBE);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    let key_loc = Location::from(&contract_key).as_f64();

    let wrap = |x: f64| x.rem_euclid(1.0);
    let cluster: Vec<f64> = [-0.010, -0.006, -0.003, 0.003, 0.006, 0.010]
        .iter()
        .map(|o| wrap(key_loc + o))
        .collect();
    let host_loc = wrap(key_loc + 0.40);
    let requester_loc = wrap(key_loc + 0.70);
    let mut node_locations = cluster.clone();
    node_locations.push(host_loc);
    node_locations.push(requester_loc);
    let num_nodes = node_locations.len();

    let rt = create_runtime();
    let mut sim = rt.block_on(async {
        SimNetwork::new_with_node_locations(
            NETWORK_NAME,
            1,
            num_nodes,
            10,
            7,
            8,
            3,
            SEED,
            &node_locations,
        )
        .await
    });
    // This control asserts the GET dead-ends *because migration is off* — it
    // deliberately does NOT call `enable_placement_migration()`. But "off by
    // default" only holds on a build below the production
    // SUBSCRIBE_HINT_MIN_VERSION floor; on the v0.2.73 release branch (#4404
    // ships active) the default flips to ON and this control would falsely
    // fail. Pin migration OFF explicitly so the control's premise holds at any
    // build version — do not rely on build-version gating.
    sim.disable_placement_migration();

    let host_label = NodeLabel::node(NETWORK_NAME, 7);
    let requester_label = NodeLabel::node(NETWORK_NAME, 8);

    let operations = vec![
        ScheduledOperation::new(
            host_label.clone(),
            SimOperation::SeedHostedContract {
                contract: contract.clone(),
                state: vec![10, 20, 30, 40],
            },
        ),
        ScheduledOperation::new(
            requester_label.clone(),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(60),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // The far host still hosts the seeded contract.
    assert!(
        result.is_node_hosting(&host_label, &contract_key),
        "host should hold the seeded contract"
    );

    // DEAD-END (expected without migration): no close-cluster peer hosts the
    // contract, so a key-routed GET dead-ends and the requester gets nothing.
    let migrated: Vec<usize> = (1..=6usize)
        .filter(|n| result.is_node_hosting(&NodeLabel::node(NETWORK_NAME, *n), &contract_key))
        .collect();
    assert!(
        migrated.is_empty(),
        "without migration, no close-cluster peer should host the contract, but these do: \
         {migrated:?} (cascade leaked into a migration-disabled sim?)"
    );
    let requester_has_state = result
        .node_storages
        .get(&requester_label)
        .is_some_and(|s| s.get_stored_state(&contract_key).is_some());
    assert!(
        !requester_has_state,
        "without migration the requester GET must dead-end at the close non-hosting cluster \
         and obtain NO state (the far host is off the greedy path toward the key)"
    );
}

/// Terminal advertisement consult (hosting redesign piece C, invariant 5):
/// a GET that routes to a terminus whose neighbor hosts the contract — one hop
/// OFF the greedy routing path — must resolve via the consult instead of
/// dead-ending with NotFound.
///
/// Setup (peer ring locations controlled via `new_with_node_locations`):
///   - a dense cluster of non-hosting peers sits right on the contract's key;
///   - exactly one host peer sits just OUTSIDE that cluster — farther from the
///     key than every cluster peer (so greedy routing never selects it), but
///     close enough in ring location to become a cluster peer's neighbor and
///     advertise its hosting to it;
///   - a far requester issues a GET.
///
/// Placement migration is disabled, so the ONLY mechanism that can carry the
/// state to the requester is the terminal consult. GET is single-path greedy
/// (relay `MAX_RELAY_RETRIES = 1`): a relay forwards to its single closest
/// neighbor toward the key and bubbles NotFound without trying its other
/// neighbors — so the advertised host, being a non-closest neighbor of the
/// terminus, is exactly the "one hop off the routing path" case. WITHOUT the
/// consult this dead-ends (proven by
/// `test_get_dead_ends_at_close_cluster_without_migration`, whose host is too
/// far to be a terminus neighbor); WITH it, the terminus consults the host
/// advertisement it received and forwards there, closing the dead-end.
///
/// The proof is the consult telemetry: `terminal_consult_resolved_found() > 0`
/// is recorded ONLY when a consulted advertised host returns Found, so with
/// migration off it is unambiguous that the consult (not routing or migration)
/// delivered the state.
#[test_log::test]
fn test_terminal_advertisement_consult_closes_get_dead_end() {
    use freenet::config::GlobalTestMetrics;
    use freenet::dev_tool::{Location, NodeLabel, ScheduledOperation, SimNetwork, SimOperation};

    // Seed + host offset chosen so the host lands as a non-closest neighbor of
    // an outer cluster peer (advertises to it, but greedy routing never selects
    // it). Deterministic under the direct/turmoil runner for this seed.
    const SEED: u64 = 0xC0FF_EEC0_0004;
    const NETWORK_NAME: &str = "terminal-consult-deadend";
    setup_deterministic_state(SEED);
    GlobalTestMetrics::reset();

    let contract = SimOperation::create_test_contract(0xC0);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    let key_loc = Location::from(&contract_key).as_f64();
    let wrap = |x: f64| x.rem_euclid(1.0);

    // Non-hosting cluster tightly around the key.
    let cluster: Vec<f64> = [-0.010, -0.006, -0.003, 0.003, 0.006, 0.010]
        .iter()
        .map(|o| wrap(key_loc + o))
        .collect();
    // Host sits just outside the cluster: farther from the key than every
    // cluster peer (so greedy routing never selects it as a next hop), yet close
    // enough in ring location to become a cluster peer's neighbor and advertise.
    let host_loc = wrap(key_loc + 0.035);
    // Requester on the OPPOSITE side of the key from the host, so its inbound
    // path approaches the key from the far side and never passes near the host.
    let requester_loc = wrap(key_loc - 0.60);
    let mut node_locations = cluster.clone();
    node_locations.push(host_loc); // regular-node order 6 -> node_no 7
    node_locations.push(requester_loc); // regular-node order 7 -> node_no 8
    let num_nodes = node_locations.len(); // 8

    let rt = create_runtime();
    let mut sim = rt.block_on(async {
        SimNetwork::new_with_node_locations(
            NETWORK_NAME,
            1,         // 1 gateway
            num_nodes, // 8 regular nodes
            10,        // ring_max_htl
            7,         // rnd_if_htl_above
            8,         // max_connections
            3,         // min_connections
            SEED,
            &node_locations,
        )
        .await
    });
    // Migration OFF: isolate the consult as the ONLY mechanism that can carry
    // state to the requester (mirrors the negative control above).
    sim.disable_placement_migration();
    // Opt into advertising the seeded host so the consult can find it (OFF by
    // default; existing dead-end controls keep a seeded host un-advertised).
    sim.enable_seeded_host_advertisements();

    let host_label = NodeLabel::node(NETWORK_NAME, 7);
    let requester_label = NodeLabel::node(NETWORK_NAME, 8);

    // Setup sanity: the host must be strictly FARTHER from the key than the
    // closest cluster peer, so greedy routing lands on the cluster (terminus)
    // and never picks the host directly. get_peer_locations() is [gw, node1..8].
    let locs = sim.get_peer_locations();
    let ring_dist = |a: f64, b: f64| {
        let d = (a - b).abs();
        d.min(1.0 - d)
    };
    let host_dist = ring_dist(locs[7], key_loc);
    let cluster_min = (1..=6)
        .map(|i| ring_dist(locs[i], key_loc))
        .fold(f64::INFINITY, f64::min);
    assert!(
        cluster_min < host_dist,
        "scenario setup wrong: a cluster peer must be closer to the key than the host \
         (cluster_min={cluster_min}, host_dist={host_dist})"
    );

    let operations = vec![
        // Only the host holds the contract (seeded into its store + Ring hosting
        // manager + neighbor-hosting advertised set, no network propagation), so
        // it advertises hosting to its neighbors but the state exists nowhere else.
        ScheduledOperation::new(
            host_label.clone(),
            SimOperation::SeedHostedContract {
                contract: contract.clone(),
                state: vec![11, 22, 33, 44],
            },
        ),
        // The far requester asks for it (greedy GET toward the key).
        ScheduledOperation::new(
            requester_label.clone(),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(60),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // The host still hosts the seeded contract.
    assert!(
        result.is_node_hosting(&host_label, &contract_key),
        "host should still host the seeded contract after the simulation"
    );

    let attempts = GlobalTestMetrics::terminal_consult_attempts();
    let hits = GlobalTestMetrics::terminal_consult_hits();
    let resolved_found = GlobalTestMetrics::terminal_consult_resolved_found();
    // With the consult succeeding, cluster relays on the Found return path
    // opportunistically cache the state (normal GET relay caching, migration is
    // OFF) — informational only; the clean proof is `resolved_found` below.
    let cached_on_cluster: Vec<usize> = (1..=6usize)
        .filter(|n| result.is_node_hosting(&NodeLabel::node(NETWORK_NAME, *n), &contract_key))
        .collect();
    tracing::info!(
        attempts,
        hits,
        resolved_found,
        still_not_found = GlobalTestMetrics::terminal_consult_still_not_found(),
        ?cached_on_cluster,
        "terminal consult telemetry"
    );

    let requester_has_state = result
        .node_storages
        .get(&requester_label)
        .is_some_and(|s| s.get_stored_state(&contract_key).is_some());

    // CORE OF THE FIX: the consult carried the state from the off-path host to
    // the requester. With migration disabled, `resolved_found > 0` is the
    // unambiguous signal that the terminal advertisement consult (not routing,
    // not migration) closed the dead-end — it is recorded ONLY when a consulted
    // advertised host returns Found.
    assert!(
        resolved_found > 0,
        "terminal consult should have resolved the GET to Found via an advertised \
         off-path host (attempts={attempts}, hits={hits}, resolved_found={resolved_found}, \
         requester_has_state={requester_has_state})"
    );
    assert!(
        requester_has_state,
        "requester GET should succeed via the terminal advertisement consult \
         (resolved_found={resolved_found})"
    );
}

/// SUBSCRIBE counterpart of `test_terminal_advertisement_consult_closes_get_dead_end`:
/// a SUBSCRIBE whose only host sits one hop OFF the routing path must resolve
/// via the terminal advertisement consult instead of dead-ending with NotFound.
///
/// Same topology and isolation as the GET test (dense non-hosting cluster on
/// the key, one advertised host just outside it, far requester, migration
/// disabled, seeded-host advertising opted in). The SUBSCRIBE relay is
/// single-shot: it forwards one greedy hop toward the key, and — on a CLEAN
/// downstream NotFound — consults the host advertisements and forwards the
/// SUBSCRIBE to the off-path advertised host, which answers Subscribed.
///
/// Proof is `terminal_consult_resolved_found() > 0` (recorded ONLY when a
/// consulted host returns Subscribed); with migration off it is unambiguous
/// that the consult (not routing or migration) closed the subscribe dead-end.
#[test_log::test]
fn test_terminal_advertisement_consult_closes_subscribe_dead_end() {
    use freenet::config::GlobalTestMetrics;
    use freenet::dev_tool::{Location, NodeLabel, ScheduledOperation, SimNetwork, SimOperation};

    // Seed + placement chosen so the requester's upstream SUBSCRIBE routes into
    // the non-hosting cluster and dead-ends at a relay whose off-path neighbor
    // is the advertised host. Deterministic under the turmoil runner.
    const SEED: u64 = 0xC0FF_EEC0_0008;
    const NETWORK_NAME: &str = "terminal-consult-subscribe-deadend";
    setup_deterministic_state(SEED);
    GlobalTestMetrics::reset();

    let contract = SimOperation::create_test_contract(0xC0);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    let key_loc = Location::from(&contract_key).as_f64();
    let wrap = |x: f64| x.rem_euclid(1.0);

    let cluster: Vec<f64> = [-0.010, -0.006, -0.003, 0.003, 0.006, 0.010]
        .iter()
        .map(|o| wrap(key_loc + o))
        .collect();
    let host_loc = wrap(key_loc + 0.05);
    let requester_loc = wrap(key_loc - 0.60);
    let mut node_locations = cluster.clone();
    node_locations.push(host_loc); // node_no 7
    node_locations.push(requester_loc); // node_no 8
    let num_nodes = node_locations.len();

    let rt = create_runtime();
    let mut sim = rt.block_on(async {
        SimNetwork::new_with_node_locations(
            NETWORK_NAME,
            1,
            num_nodes,
            10,
            7,
            8,
            3,
            SEED,
            &node_locations,
        )
        .await
    });
    sim.disable_placement_migration();
    sim.enable_seeded_host_advertisements();

    let host_label = NodeLabel::node(NETWORK_NAME, 7);
    let requester_label = NodeLabel::node(NETWORK_NAME, 8);

    let operations = vec![
        ScheduledOperation::new(
            host_label.clone(),
            SimOperation::SeedHostedContract {
                contract: contract.clone(),
                state: vec![11, 22, 33, 44],
            },
        ),
        // Seed the requester too: a bare SUBSCRIBE for an absent contract fails
        // the local RegisterSubscriberListener gate and never starts the
        // network subscribe. With the contract present, the SUBSCRIBE passes
        // that gate and routes UPSTREAM toward the key to build the
        // subscription tree — and since the requester is far from the key, it
        // dead-ends at the non-hosting cluster, exactly where the terminal
        // consult applies.
        ScheduledOperation::new(
            requester_label.clone(),
            SimOperation::SeedHostedContract {
                contract: contract.clone(),
                state: vec![11, 22, 33, 44],
            },
        ),
        ScheduledOperation::new(
            requester_label.clone(),
            SimOperation::Subscribe { contract_id },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(60),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "simulation failed: {:?}",
        result.turmoil_result.err()
    );

    assert!(
        result.is_node_hosting(&host_label, &contract_key),
        "host should still host the seeded contract after the simulation"
    );

    let attempts = GlobalTestMetrics::terminal_consult_attempts();
    let hits = GlobalTestMetrics::terminal_consult_hits();
    let resolved_found = GlobalTestMetrics::terminal_consult_resolved_found();
    tracing::info!(
        attempts,
        hits,
        resolved_found,
        still_not_found = GlobalTestMetrics::terminal_consult_still_not_found(),
        "terminal consult telemetry (subscribe)"
    );

    // With migration off, `resolved_found > 0` is recorded ONLY when a
    // consulted advertised host answers Subscribed — the unambiguous proof
    // that the SUBSCRIBE terminal consult (not routing or migration) closed
    // the dead-end.
    assert!(
        resolved_found > 0,
        "terminal consult should have resolved the SUBSCRIBE to Subscribed via an \
         advertised off-path host (attempts={attempts}, hits={hits}, \
         resolved_found={resolved_found})"
    );
}

/// Reproduction + regression test for the placement-migration renewal storm
/// (#4440, the storm symptom of the #4404 placement work).
///
/// ## The storm
///
/// When the placement migration drifts a contract onto the peer closest to its
/// ring location, that peer becomes the *body-holding subscription root*: it
/// hosts the contract AND no connected neighbor is closer to the key. Such a
/// peer has no peer closer than itself to subscribe to upstream. Before the fix,
/// the subscription-renewal driver did not recognise that role: every renewal
/// cycle it still sent a wire `Subscribe::Request`, which routes greedily toward
/// the contract, dead-ends at the root itself, fails, and is retried on the next
/// 30s cycle — forever. Across many such contracts that loop is the renewal
/// storm that made the 500-node nightly go metastable (`subscribe: attempt
/// timed out ... is_renewal: true` floods; in prod the renewal task also
/// overruns its 25s cycle deadline).
///
/// ## What this test ignites
///
/// A small, deterministic topology where one node is unambiguously the
/// body-holding subscription root for a contract AND keeps that contract in the
/// renewal set (it holds a client subscription for it, so
/// `contracts_needing_renewal()` returns it every cycle). Migration is enabled
/// so the run reflects the production condition the storm appears under. Over the
/// simulation the root's renewal driver fires repeatedly.
///
/// ## The deterministic signal
///
/// `is_renewal` is not recorded on the captured `NetLogMessage` event stream and
/// the `subscription_renewal_outcome` telemetry is fire-and-forget to a remote
/// collector, so neither is readable in-sim. Instead the renewal driver publishes
/// a per-node counter into a simulation-only renewal-metrics registry that
/// `run_controlled_simulation` snapshots before the `SimNetwork` is dropped; the
/// test reads it via `ControlledSimulationResult::{renewal_metrics_for,
/// aggregate_renewal_metrics}`:
///   - `wire_attempts`: the node sent (or attempted) a wire renewal request —
///     the storm signature for a body-holding root, because each such attempt
///     dead-ends and is retried.
///   - `terminus_satisfied`: the renewal short-circuited via the root-satisfied
///     path the fix adds.
///
/// **Before the fix** the root's renewals are all `wire_attempts` (the storm) and
/// `terminus_satisfied` is zero — the asserts below FAIL. **After the fix** the
/// root's renewals are `terminus_satisfied` and it makes essentially no wire
/// renewal attempts — the asserts PASS. The test is run across several seeds
/// because the storm is metastable (one green run is not proof).
#[test_log::test]
fn test_subscription_root_renewal_does_not_storm() {
    use freenet::dev_tool::{Location, NodeLabel, ScheduledOperation, SimNetwork, SimOperation};

    // The storm is metastable; assert across several seeds, not just one.
    for seed in [0x4440_0001u64, 0x4440_0002, 0x4440_0003] {
        let network_name = format!("renewal-storm-{seed:x}");
        // Leak the name to obtain the &'static str SimNetwork::new requires; one
        // small allocation per seed in a test is fine.
        let network_name: &'static str = Box::leak(network_name.into_boxed_str());
        setup_deterministic_state(seed);

        // Place the root node exactly at the key and everyone else far from it,
        // so the root is unambiguously the closest connected peer (the
        // body-holding subscription root) and no migration can move the contract
        // off it.
        let contract = SimOperation::create_test_contract(0x44);
        let contract_id = *contract.key().id();
        let contract_key = contract.key();
        let key_loc = Location::from(&contract_key).as_f64();

        let wrap = |x: f64| x.rem_euclid(1.0);
        // node order 1 = root (AT the key); orders 2..=4 far from the key.
        let node_locations = vec![
            key_loc, // root
            wrap(key_loc + 0.30),
            wrap(key_loc + 0.45),
            wrap(key_loc + 0.60),
        ];
        let num_nodes = node_locations.len();

        let rt = create_runtime();
        let mut sim = rt.block_on(async {
            SimNetwork::new_with_node_locations(
                network_name,
                1,         // 1 gateway
                num_nodes, // 4 regular nodes
                10,        // ring_max_htl
                7,         // rnd_if_htl_above
                8,         // max_connections
                3,         // min_connections
                seed,
                &node_locations,
            )
            .await
        });
        // Match the production condition the storm appears under: migration ON.
        sim.enable_placement_migration();

        let root_label = NodeLabel::node(network_name, 1); // AT the key

        // Resolve the root's address now — `run_controlled_simulation` consumes
        // `sim`, and we need this to key into the renewal-metrics registry after.
        let root_addr = sim
            .node_address(&root_label)
            .unwrap_or_else(|| panic!("root node {root_label:?} has no address (seed {seed:x})"));

        // Sanity: the root must be strictly closer to the key than every other
        // peer (regular nodes AND the gateway), so it is genuinely the
        // body-holding subscription root. The root is placed AT the key, so its
        // distance is 0 — assert that directly plus the strict-closest invariant.
        let locs = sim.get_peer_locations(); // [gateway, node1..node4]
        let ring_dist = |a: f64, b: f64| {
            let d = (a - b).abs();
            d.min(1.0 - d)
        };
        let root_dist = ring_dist(locs[1], key_loc);
        // Include the gateway (index 0) in the "everyone else" set.
        let others_min = (0..locs.len())
            .filter(|&i| i != 1)
            .map(|i| ring_dist(locs[i], key_loc))
            .fold(f64::INFINITY, f64::min);
        // The root is placed AT the key; location quantization (loopback-port
        // assignment) introduces a tiny offset (~1e-6), so assert ~0 with a small
        // epsilon rather than exact zero.
        assert!(
            root_dist < 1e-3,
            "scenario setup wrong (seed {seed:x}): the root is placed AT the key, \
             so its distance must be ~0 (root_dist={root_dist})"
        );
        assert!(
            root_dist < others_min,
            "scenario setup wrong (seed {seed:x}): the root must be the closest peer to the key \
             including the gateway (root_dist={root_dist}, others_min={others_min})"
        );

        // A SECOND contract, seeded on a NON-root node (node 2) that is NOT its
        // closest peer, so node 2 must keep renewing it over the wire. This is
        // the guard against a silent network-wide renewal STOPPAGE: if
        // `is_subscription_root` / `body_holding_subscription_root_key` ever
        // regressed to return `Some` too liberally, every node would
        // short-circuit and NO node would wire-renew — a regression worse than
        // the storm. Pick a contract whose ring location is closest to a peer
        // OTHER than node 2, then seed it on node 2 with a client subscription.
        let non_root_label = NodeLabel::node(network_name, 2);
        let non_root_addr = sim.node_address(&non_root_label).unwrap_or_else(|| {
            panic!("non-root node {non_root_label:?} has no address (seed {seed:x})")
        });
        let non_root_loc = locs[2];
        let (contract2, contract2_id) = {
            // Search deterministic contract seeds for one whose key is NOT
            // closest to node 2 (so node 2 is a genuine non-root host).
            let mut chosen = None;
            for byte in 0u8..=255 {
                let c = SimOperation::create_test_contract(byte);
                let cloc = Location::from(&c.key()).as_f64();
                let node2_d = ring_dist(non_root_loc, cloc);
                let someone_closer = (0..locs.len())
                    .filter(|&i| i != 2)
                    .any(|i| ring_dist(locs[i], cloc) < node2_d);
                if someone_closer {
                    chosen = Some((c.clone(), *c.key().id()));
                    break;
                }
            }
            chosen.unwrap_or_else(|| {
                panic!("seed {seed:x}: could not find a contract node 2 is NOT the root for")
            })
        };

        let operations = vec![
            // The root genuinely HOSTS the contract (state + host_contract +
            // active subscription), with no network propagation — so
            // `is_subscription_root` is true and renewal eligibility starts.
            ScheduledOperation::new(
                root_label.clone(),
                SimOperation::SeedHostedContract {
                    contract: contract.clone(),
                    state: vec![1, 2, 3, 4],
                },
            ),
            // A local client on the root subscribes, so the contract stays in
            // `contracts_needing_renewal()` (the client-subscription path),
            // keeping the renewal driver firing on the root and marking it
            // `contract_in_use` (so the root-satisfied path refreshes the lease).
            ScheduledOperation::new(root_label.clone(), SimOperation::Subscribe { contract_id }),
            // Second contract on a NON-root node, with a client subscription so
            // it stays renewal-eligible. Node 2 is not its closest peer, so it
            // must wire-renew — the non-root control for the stoppage guard.
            ScheduledOperation::new(
                non_root_label.clone(),
                SimOperation::SeedHostedContract {
                    contract: contract2.clone(),
                    state: vec![5, 6, 7, 8],
                },
            ),
            ScheduledOperation::new(
                non_root_label.clone(),
                SimOperation::Subscribe {
                    contract_id: contract2_id,
                },
            ),
        ];

        // Run long enough for the network to form and several 30s renewal cycles
        // to fire on the root. `run_controlled_simulation` advances virtual time
        // by the test client's sleeps (3s warmup + 3s per op + the
        // post-operations wait), not the `simulation_duration` cap. The seeded
        // active subscription has an 8-minute lease and only enters the renewal
        // window at 6 minutes, so the post-operations wait must comfortably
        // exceed that to exercise the renewal driver on the root. 540s gives
        // several 30s renewal cycles inside the window. (Virtual time is cheap:
        // this whole run is a few seconds of wall-clock under Turmoil.)
        let result = sim.run_controlled_simulation(
            seed,
            operations,
            Duration::from_secs(900),
            Duration::from_secs(540),
        );
        assert!(
            result.turmoil_result.is_ok(),
            "simulation failed (seed {seed:x}): {:?}",
            result.turmoil_result.err()
        );

        // The root must still host the contract (its body-holding-root status,
        // and thus the storm condition, held for the whole run).
        assert!(
            result.is_node_hosting(&root_label, &contract_key),
            "root should still host the seeded contract after the simulation (seed {seed:x})"
        );

        // Read the renewal metrics captured inside `run_controlled_simulation`
        // BEFORE the SimNetwork was dropped (the Drop impl clears the global
        // registry, so reading it here would always see zeros — #4440).
        let root_metrics = result.renewal_metrics_for(&root_addr);
        let non_root_metrics = result.renewal_metrics_for(&non_root_addr);
        let totals = result.aggregate_renewal_metrics();

        tracing::info!(
            seed = format!("{seed:x}"),
            root_wire_attempts = root_metrics.wire_attempts,
            root_terminus_satisfied = root_metrics.terminus_satisfied,
            non_root_wire_attempts = non_root_metrics.wire_attempts,
            non_root_terminus_satisfied = non_root_metrics.terminus_satisfied,
            total_wire_attempts = totals.wire_attempts,
            total_terminus_satisfied = totals.terminus_satisfied,
            "renewal storm metrics"
        );

        // The renewal driver must have actually run on the root — otherwise the
        // test proves nothing (neither storm nor fix exercised).
        assert!(
            root_metrics.wire_attempts + root_metrics.terminus_satisfied > 0,
            "renewal driver never ran on the body-holding root (seed {seed:x}); \
             test exercised neither the storm nor the fix \
             (wire_attempts={}, terminus_satisfied={})",
            root_metrics.wire_attempts,
            root_metrics.terminus_satisfied,
        );

        // CORE OF THE FIX: a body-holding subscription root satisfies its
        // renewals locally and sends no wire renewal request.
        //
        // Before the fix this is the storm: every renewal on the root is a
        // doomed wire attempt and `terminus_satisfied` is zero — both asserts
        // below fail. After the fix the root's renewals are terminus-satisfied
        // and it makes no wire renewal attempts.
        assert!(
            root_metrics.terminus_satisfied > 0,
            "body-holding root never took the root-satisfied renewal path (seed {seed:x}): \
             terminus_satisfied=0, wire_attempts={}. Before the #4440 fix the root storms \
             with doomed upstream renewals instead of satisfying them locally.",
            root_metrics.wire_attempts,
        );
        assert_eq!(
            root_metrics.wire_attempts, 0,
            "body-holding root must NOT send wire renewal requests — that is the storm \
             (seed {seed:x}): wire_attempts={}, terminus_satisfied={}. Each wire renewal \
             from a root routes greedily toward the key, dead-ends at the root, fails, and \
             retries next cycle.",
            root_metrics.wire_attempts, root_metrics.terminus_satisfied,
        );

        // UPPER BOUND on the root's terminus-satisfied count (regression guard
        // for the lease-refresh gating, #4440 blocker 2). The in-use root
        // refreshes its LOCAL lease on each root-satisfied renewal, which moves
        // it onto the ~6-minute lease-expiry renewal cadence. Over this ~9-minute
        // run that is at most ~2 cycles. If the lease refresh were accidentally
        // dropped (or made unconditional in a way that re-selected the root every
        // 30s cycle), the root would fire the root-satisfied path on EVERY cycle
        // and this count would balloon into the teens — caught here.
        assert!(
            root_metrics.terminus_satisfied <= 4,
            "root took the root-satisfied path {} times (seed {seed:x}) — far more than the \
             ~6-minute lease-expiry cadence allows over this run. The local-lease refresh \
             is supposed to keep an in-use root OFF the every-30s-cycle renewal selection; \
             a count this high means it is being re-selected every cycle (the batch-starvation \
             regression).",
            root_metrics.terminus_satisfied,
        );

        // STOPPAGE GUARD (#4440): renewals must NOT have globally stopped. A
        // non-root host (node 2, not the closest peer for contract2) must keep
        // renewing over the wire. If `is_subscription_root` regressed to return
        // true too liberally, EVERY node would short-circuit and this would be 0
        // — a silent network-wide renewal stoppage, worse than the storm.
        assert!(
            non_root_metrics.wire_attempts > 0,
            "non-root host made no wire renewal attempts (seed {seed:x}): wire_attempts=0, \
             terminus_satisfied={}. A non-root host MUST keep renewing upstream; zero here \
             means the root short-circuit is firing on non-roots too (silent global renewal \
             stoppage).",
            non_root_metrics.terminus_satisfied,
        );
        // And the non-root must NOT have taken the root-satisfied path (it is not
        // a root for contract2).
        assert_eq!(
            non_root_metrics.terminus_satisfied, 0,
            "non-root host wrongly took the root-satisfied path (seed {seed:x}): \
             terminus_satisfied={}, wire_attempts={}. Only a body-holding root may \
             short-circuit; a non-root host must wire-renew.",
            non_root_metrics.terminus_satisfied, non_root_metrics.wire_attempts,
        );
    }
}

/// Regression test for #4440 blocker 2: a body-holding subscription root with NO
/// genuine interest (no local client subscription, no downstream subscriber)
/// must let its `active_subscriptions` lease **lapse** rather than refresh it on
/// the root-satisfied renewal path.
///
/// Why this matters: the root-satisfied renewal path must not refresh the lease
/// of a root with no genuine interest. If it refreshed the lease
/// unconditionally, a no-interest root would keep its `active_subscriptions`
/// lease alive forever — an unbounded `is_subscribed`-only retention exemption
/// that the `contract_in_use` rustdoc and the AGENTS.md time-bounded-exemption
/// rule forbid. Both the root-satisfied path and `contracts_needing_renewal`
/// section 1 gate renewal on `contract_in_use`, so a no-interest root does NOT
/// refresh and the lease lapses (`SUBSCRIPTION_LEASE_DURATION` = 8 min) — after
/// which it drops out of the renewal set entirely.
///
/// This test seeds a hosted contract on a body-holding root with NO client
/// subscription, runs past the lease window, and asserts the root's final
/// `active_subscription_keys` (captured from the topology snapshot) no longer
/// contains the contract — i.e. the lease was allowed to lapse, not
/// self-perpetuated.
#[test_log::test]
fn test_no_interest_subscription_root_lease_lapses() {
    use freenet::dev_tool::{Location, NodeLabel, ScheduledOperation, SimNetwork, SimOperation};

    const SEED: u64 = 0x4440_0009;
    let network_name = "renewal-root-lease-lapse";
    setup_deterministic_state(SEED);

    let contract = SimOperation::create_test_contract(0x4A);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    let key_loc = Location::from(&contract_key).as_f64();

    let wrap = |x: f64| x.rem_euclid(1.0);
    // node order 1 = root (AT the key); orders 2..=4 far from the key.
    let node_locations = vec![
        key_loc,
        wrap(key_loc + 0.30),
        wrap(key_loc + 0.45),
        wrap(key_loc + 0.60),
    ];
    let num_nodes = node_locations.len();

    let rt = create_runtime();
    let mut sim = rt.block_on(async {
        SimNetwork::new_with_node_locations(
            network_name,
            1,
            num_nodes,
            10,
            7,
            8,
            3,
            SEED,
            &node_locations,
        )
        .await
    });
    sim.enable_placement_migration();

    let root_label = NodeLabel::node(network_name, 1);
    let root_addr = sim
        .node_address(&root_label)
        .expect("root node has an address");

    // Seed the contract as hosted on the root, but DO NOT subscribe a client and
    // DO NOT create any downstream subscriber — so `contract_in_use` is false.
    let operations = vec![ScheduledOperation::new(
        root_label.clone(),
        SimOperation::SeedHostedContract {
            contract: contract.clone(),
            state: vec![1, 2, 3, 4],
        },
    )];

    // Run past the 8-minute lease (post-op wait 600s ≈ 10 min) so a refreshed
    // lease would still be live at the end, while a lapsed lease is gone — the
    // discriminating signal.
    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(900),
        Duration::from_secs(600),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "simulation failed: {:?}",
        result.turmoil_result.err()
    );

    // The root still HOSTS the contract (hosting is decoupled from the lease).
    assert!(
        result.is_node_hosting(&root_label, &contract_key),
        "root should still host the contract (hosting does not depend on the lease)"
    );

    // The root must NOT have made wire renewal attempts (it is the body-holding
    // root) — its renewals take the root-satisfied path.
    let root_metrics = result.renewal_metrics_for(&root_addr);
    assert_eq!(
        root_metrics.wire_attempts, 0,
        "body-holding root must not wire-renew (wire_attempts={}, terminus_satisfied={})",
        root_metrics.wire_attempts, root_metrics.terminus_satisfied,
    );

    // CORE ASSERTION: the lease lapsed. The root's final active-subscription set
    // (captured in the topology snapshot before drop) must NOT contain the
    // contract — a no-interest root does not refresh, so after 8 minutes the
    // lease is gone and the root has dropped out of the renewal set. If the
    // refresh were unconditional, the lease would still be live here.
    let root_snapshot = result
        .topology_snapshots
        .iter()
        .find(|s| s.peer_addr == root_addr)
        .expect("root topology snapshot present");
    assert!(
        !root_snapshot
            .active_subscription_keys
            .contains(&contract_id),
        "no-interest body-holding root self-perpetuated its lease: the contract is still in \
         active_subscription_keys after the 8-minute lease window. The root-satisfied path \
         must NOT refresh the lease when the contract is not in use (#4440 blocker 2) — \
         otherwise it is an unbounded is_subscribed-only retention exemption."
    );

    tracing::info!(
        root_terminus_satisfied = root_metrics.terminus_satisfied,
        active_subs = root_snapshot.active_subscription_keys.len(),
        "no-interest root lease lapsed as expected"
    );
}

/// Opt-in migration-at-scale regression guard (#4601).
///
/// ## What this is the home for
///
/// The placement-migration (`SubscribeHint`) cascade is OFF by default in every
/// simulation since #4601 (the per-node version floor defaults to an unreachable
/// value — see `SimNetwork::SIM_MIGRATION_DISABLED_FLOOR`), so the general
/// throughput nightlies no longer carry migration load. This test is the
/// DEDICATED place that turns migration ON (`enable_placement_migration`) and
/// proves its load is BOUNDED at scale — not merely that the sim completes.
///
/// ## The bound it asserts
///
/// The production subscription-renewal driver
/// (`Ring::recover_orphaned_subscriptions`) hard-caps the number of renewal
/// tasks it spawns per 30s recovery cycle at
/// `Ring::MAX_RECOVERY_ATTEMPTS_PER_INTERVAL` (10), regardless of how many
/// contracts are eligible. That per-node rate cap is the reason the renewal path
/// is load-safe in production (prod telemetry shows real peers well under it).
/// This test exercises the cap by hosting MANY (`NUM_CONTRACTS` = 20) contracts
/// on one node, all of which enter the 2-minute renewal window simultaneously
/// (they share an ~8-minute lease set at startup) so a single cycle has far more
/// eligible contracts than the cap. It then asserts, via the per-node
/// `max_cycle_batch` metric (the largest `attempted` count any cycle reached):
///
///   * (cap holds, EVERY node) `max_cycle_batch <= MAX_RECOVERY_ATTEMPTS_PER_INTERVAL`.
///     This is the regression guard: if the spawn cap were removed, the loaded
///     node would record `max_cycle_batch == NUM_CONTRACTS` (20) and FAIL.
///   * (cap engaged, loaded node) `max_cycle_batch == MAX_RECOVERY_ATTEMPTS_PER_INTERVAL`.
///     Proves demand exceeded the cap and the cap actually clipped — without
///     this the bound above could pass vacuously on a node that never had >10
///     eligible contracts at once.
///
/// ## Why migration must be ON for this to be meaningful
///
/// With migration ON the loaded node also nudges its closer-to-the-key neighbors
/// to host its contracts (directed subscribes), so the run carries the real
/// migration traffic the #4601 regression piled onto unrelated sims. The test
/// additionally asserts at least one contract migrated onto another node, so the
/// cascade is genuinely exercised (not silently inert).
#[test_log::test]
fn test_placement_migration_at_scale_renewal_load_stays_bounded() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimNetwork, SimOperation};

    // Mirrors `ring::Ring::MAX_RECOVERY_ATTEMPTS_PER_INTERVAL` (private to the
    // crate). The renewal driver breaks its spawn loop once `attempted` reaches
    // this value, so no node may exceed it in a single 30s cycle. Frozen value;
    // if the production constant changes, update this and the rationale above.
    const MAX_RECOVERY_ATTEMPTS_PER_INTERVAL: u64 = 10;
    // Hosted-on-the-loaded-node contract count. Chosen well above the cap so a
    // single renewal-window cycle has many more eligible contracts than the cap
    // can spawn — that is what makes the cap regression detectable.
    const NUM_CONTRACTS: usize = 20;

    // Metastable load behavior: assert across several seeds, not just one.
    for seed in [0x4601_0001u64, 0x4601_0002, 0x4601_0003] {
        let network_name = format!("migration-scale-{seed:x}");
        // Leak to obtain the &'static str SimNetwork::new requires; one small
        // allocation per seed in a test is fine (same pattern as the storm test).
        let network_name: &'static str = Box::leak(network_name.into_boxed_str());
        setup_deterministic_state(seed);

        // 1 gateway + 8 regular nodes spread around the ring. The loaded node
        // (node 1) sits at 0.5; the others are scattered so that for most of the
        // hosted contracts SOME peer is strictly closer to the key than the
        // loaded node — giving the migration cascade real targets to nudge.
        let loaded_loc = 0.5;
        let node_locations = vec![
            loaded_loc, // node 1 = the loaded node
            0.05, 0.15, 0.30, 0.42, 0.62, 0.78, 0.92,
        ];
        let num_nodes = node_locations.len();

        let rt = create_runtime();
        let mut sim = rt.block_on(async {
            SimNetwork::new_with_node_locations(
                network_name,
                1,         // 1 gateway
                num_nodes, // 8 regular nodes
                10,        // ring_max_htl
                7,         // rnd_if_htl_above
                16,        // max_connections (room for the small mesh)
                3,         // min_connections
                seed,
                &node_locations,
            )
            .await
        });
        // OPT IN to placement migration — the whole point of this test.
        sim.enable_placement_migration();

        let loaded_label = NodeLabel::node(network_name, 1);
        let loaded_addr = sim
            .node_address(&loaded_label)
            .unwrap_or_else(|| panic!("loaded node has no address (seed {seed:x})"));

        // Build NUM_CONTRACTS distinct contracts and host them all on the loaded
        // node (seeded as genuinely hosted + actively subscribed at startup, so
        // their leases all start together → they all enter the renewal window in
        // the same cycle). A client Subscribe per contract keeps them
        // renewal-eligible past lease expiry (path 2) so the high-demand window
        // spans several cycles.
        let contracts: Vec<_> = (0..NUM_CONTRACTS)
            .map(|i| SimOperation::create_test_contract(i as u8))
            .collect();
        let contract_keys: Vec<_> = contracts.iter().map(|c| c.key()).collect();

        let mut operations = Vec::with_capacity(NUM_CONTRACTS * 2);
        for contract in &contracts {
            operations.push(ScheduledOperation::new(
                loaded_label.clone(),
                SimOperation::SeedHostedContract {
                    contract: contract.clone(),
                    state: vec![1, 2, 3, 4],
                },
            ));
        }
        for contract in &contracts {
            operations.push(ScheduledOperation::new(
                loaded_label.clone(),
                SimOperation::Subscribe {
                    contract_id: *contract.key().id(),
                },
            ));
        }

        // Run past the 6-minute renewal-window opening (8-min lease − 2-min
        // window) and several 30s cycles into it. 540s post-op wait gives the
        // window-burst cycles where all NUM_CONTRACTS are eligible at once.
        let result = sim.run_controlled_simulation(
            seed,
            operations,
            Duration::from_secs(900),
            Duration::from_secs(540),
        );
        assert!(
            result.turmoil_result.is_ok(),
            "simulation failed (seed {seed:x}): {:?}",
            result.turmoil_result.err()
        );

        let loaded_metrics = result.renewal_metrics_for(&loaded_addr);
        let totals = result.aggregate_renewal_metrics();
        tracing::info!(
            seed = format!("{seed:x}"),
            loaded_max_cycle_batch = loaded_metrics.max_cycle_batch,
            loaded_wire_attempts = loaded_metrics.wire_attempts,
            loaded_terminus = loaded_metrics.terminus_satisfied,
            agg_max_cycle_batch = totals.max_cycle_batch,
            agg_wire_attempts = totals.wire_attempts,
            "migration-at-scale renewal load"
        );

        // (A) CAP HOLDS for EVERY node — the regression guard. If the per-cycle
        // spawn cap were removed, the loaded node (with NUM_CONTRACTS eligible at
        // once) would record max_cycle_batch == NUM_CONTRACTS > the cap.
        for (addr, m) in &result.renewal_metrics {
            assert!(
                m.max_cycle_batch <= MAX_RECOVERY_ATTEMPTS_PER_INTERVAL,
                "node {addr} spawned {} renewal tasks in a single 30s cycle (seed {seed:x}) — \
                 exceeds the production cap MAX_RECOVERY_ATTEMPTS_PER_INTERVAL={}. The renewal \
                 rate cap in recover_orphaned_subscriptions has regressed (#4601).",
                m.max_cycle_batch,
                MAX_RECOVERY_ATTEMPTS_PER_INTERVAL,
            );
        }

        // (B) CAP ENGAGED on the loaded node — non-vacuous. With NUM_CONTRACTS
        // (>cap) eligible simultaneously, the loaded node must have hit the cap in
        // at least one cycle. If this is < cap the test proved nothing about the
        // cap (demand never exceeded it), so the guard in (A) would be vacuous.
        assert_eq!(
            loaded_metrics.max_cycle_batch, MAX_RECOVERY_ATTEMPTS_PER_INTERVAL,
            "loaded node never reached the renewal cap (seed {seed:x}): max_cycle_batch={}, \
             expected exactly MAX_RECOVERY_ATTEMPTS_PER_INTERVAL={}. With {} contracts entering \
             the renewal window together the cap must clip a cycle to exactly the cap; a lower \
             value means the high-demand burst did not form and (A) is vacuous.",
            loaded_metrics.max_cycle_batch, MAX_RECOVERY_ATTEMPTS_PER_INTERVAL, NUM_CONTRACTS,
        );

        // (C) MIGRATION genuinely ran — at least one hosted contract migrated
        // onto another node via the directed-subscribe cascade. Without this the
        // test could pass with migration silently inert (which would defeat its
        // purpose as migration's scale home).
        let mut migrated: Vec<(usize, usize)> = Vec::new();
        for ci in 0..NUM_CONTRACTS {
            // Non-loaded regular nodes are node_no 2..=num_nodes (node 1 is the
            // loaded host; the loaded node hosting its own seeded contract is not
            // a migration).
            for n in 2..=num_nodes {
                if result.is_node_hosting(&NodeLabel::node(network_name, n), &contract_keys[ci]) {
                    migrated.push((ci, n));
                }
            }
        }
        assert!(
            !migrated.is_empty(),
            "placement migration was enabled but NO contract migrated off the loaded node to a \
             closer neighbor (seed {seed:x}). Either the cascade is inert (gate regression) or \
             the topology never let a neighbor become a migration target — the test is not \
             exercising migration. (contract_idx, node_no) hosting pairs: {migrated:?}",
        );

        tracing::info!(
            seed = format!("{seed:x}"),
            migrated_pairs = migrated.len(),
            "migration-at-scale: cap held, cap engaged, migration ran"
        );
    }
}

// =============================================================================
// Demand-driven hosting harness (#4642) — example sim tests exercising the
// sim-harness infrastructure added for the hosting/subscription redesign:
//   * injectable, controllable hosting clock (enable_hosting_time_control +
//     SimOperation::AdvanceHostingClock)   — piece A (eviction/TTL)
//   * per-node hosting-budget knob (with_hosting_budget)                — piece A
//   * per-node subscription / hosting / active-demand measurement
//     accessors on ControlledSimulationResult                          — piece D
//   * scripted mid-run node crash (SimOperation::CrashNode)            — piece F
//
// These validate the harness end-to-end. The strong, deterministic proof that
// the injected clock drives eviction lives in the unit test
// `ring::hosting::tests::test_with_time_source_lease_expiry_follows_injected_clock`.
// =============================================================================

/// End-to-end validation of the controllable hosting clock + budget knob +
/// per-node measurement accessors (#4642 pieces A & D).
///
/// Injects a controllable hosting clock and a tiny hosting budget into every
/// node, runs a controlled scenario that PUTs a demanded contract, advances the
/// hosting clock far past the 8-minute TTL gate via `AdvanceHostingClock`, and
/// then reads the new per-node measurement accessors. Asserts the harness
/// plumbing works end-to-end and that a contract backed by active demand
/// survives a large clock jump (demand protects against eviction).
#[test]
fn test_hosting_clock_injection_and_measurement_end_to_end() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const NETWORK: &str = "hosting-clock-injection";
    const SEED: u64 = 0x4642_A001_CAFE;

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    // The gateway PUTs (and hosts) the demanded contract — a gateway reliably
    // hosts its own PUT, avoiding placement non-determinism. A regular node
    // (labels start at `number_of_gateways`) later GETs it.
    let gateway = NodeLabel::gateway(NETWORK, 0);
    let getter = NodeLabel::node(NETWORK, 1);
    let demanded = SimOperation::create_test_contract(11);
    let demanded_key = demanded.key();
    let demanded_id = *demanded.key().id();
    let demanded_state = SimOperation::create_test_state(11);

    // Build + configure the network inside a tokio runtime; run_controlled_simulation
    // manages its OWN turmoil runtime and must be called OUTSIDE block_on.
    let (sim, clock, clock_start) = rt.block_on(async {
        let mut sim = SimNetwork::new(NETWORK, 1, 2, 7, 3, 10, 2, SEED).await;

        // Piece A: inject a controllable hosting clock (so TTL/eviction is under
        // test control) and a tiny per-node budget (to force cache pressure).
        let clock = sim.enable_hosting_time_control();
        sim.with_hosting_budget(4096);
        let clock_start = clock.current_time();
        (sim, clock, clock_start)
    });

    let operations = vec![
        // Demanded contract: PUT with subscribe=true → the gateway hosts it AND
        // holds a local client subscription, so eviction must NOT drop it.
        ScheduledOperation::new(
            gateway.clone(),
            SimOperation::Put {
                contract: demanded.clone(),
                state: demanded_state.clone(),
                subscribe: true,
            },
        ),
        // Jump the hosting clock 30 minutes — far past the 8-minute TTL gate —
        // deterministically, WITHOUT running 30 minutes of virtual time.
        ScheduledOperation::new(
            gateway.clone(),
            SimOperation::AdvanceHostingClock {
                duration: Duration::from_secs(30 * 60),
            },
        ),
        // Re-access the demanded contract from a node, which exercises the GET
        // path and triggers hosting-cache activity on the advanced clock.
        ScheduledOperation::new(
            getter.clone(),
            SimOperation::Get {
                contract_id: demanded_id,
                return_contract_code: true,
                subscribe: false,
            },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(30),
    );

    // The injected clock is shared with every node; confirm the scripted
    // AdvanceHostingClock actually moved it forward by the 30 minutes we
    // scheduled (the sim advanced the same Arc-backed clock this handle sees).
    assert!(
        clock.current_time() >= clock_start + Duration::from_secs(30 * 60),
        "AdvanceHostingClock should have advanced the shared hosting clock by >= 30 minutes"
    );

    assert!(
        result.turmoil_result.is_ok(),
        "controlled simulation should complete: {:?}",
        result.turmoil_result.err()
    );

    // The measurement accessors (#4642 piece D) must be readable per node.
    for label in result.captured_node_labels() {
        tracing::info!(
            node = %label,
            hosting = result.node_hosting_count(&label),
            subscriptions = result.node_subscription_count(&label),
            active_demand = ?result.node_active_demand_count(&label),
            "hosting measurement snapshot"
        );
    }

    // The demanded contract must still be hosted on the gateway after the large
    // clock jump — its local client subscription protects it from TTL/budget
    // eviction. This exercises the clock-injection + budget plumbing end-to-end.
    assert!(
        result.is_node_hosting(&gateway, &demanded_key),
        "gateway should still host the demanded (subscribed) contract after a 30-minute \
         hosting-clock jump; its client subscription must protect it from eviction"
    );
    assert!(
        result.node_hosting_count(&gateway) >= 1,
        "gateway should host at least the demanded contract"
    );

    // The per-node subscription / active-demand accessors (#4642 piece D) are
    // exercised in the logging loop above. They are the measurement surface a
    // no-storm proof asserts on (subscriptions ∝ demand, not ∝ cache size); the
    // strict storm bound is a property piece D must establish, so this harness
    // test asserts only that the accessors are wired and the plumbing runs, not
    // the storm bound itself.
}

/// Demonstrates the scripted mid-run node-crash capability (#4642 piece F):
/// `SimOperation::CrashNode` removes a specific peer at a scripted point via the
/// per-network fault injector, in-order with the other events. Asserts the
/// simulation completes and the contract remains hosted somewhere in the
/// surviving network.
///
/// NOTE: `CrashNode` is a SILENT (message-blocking) crash — the crashed peer's
/// transport is not torn down, so surviving peers notice on their own
/// routing-health cadence rather than an immediate transport close. A test that
/// needs *prompt* upstream-loss detection (the core of piece F) additionally
/// depends on the event-driven re-subscribe production change, which is not on
/// `main`.
#[test]
fn test_scripted_node_crash_mid_run() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const NETWORK: &str = "scripted-node-crash";
    const SEED: u64 = 0x4642_F001_CAFE;

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    // Regular-node labels start at `number_of_gateways` (= 1 here).
    let gateway = NodeLabel::gateway(NETWORK, 0);
    let node1 = NodeLabel::node(NETWORK, 1);
    let node2 = NodeLabel::node(NETWORK, 2);

    let contract = SimOperation::create_test_contract(21);
    let contract_key = contract.key();
    let contract_id = *contract.key().id();
    let state = SimOperation::create_test_state(21);

    // Build the network inside a tokio runtime; run_controlled_simulation manages
    // its OWN turmoil runtime and must be called OUTSIDE block_on.
    let sim = rt.block_on(async { SimNetwork::new(NETWORK, 1, 3, 7, 3, 10, 2, SEED).await });

    let operations = vec![
        // Gateway hosts the contract.
        ScheduledOperation::new(
            gateway.clone(),
            SimOperation::Put {
                contract: contract.clone(),
                state: state.clone(),
                subscribe: true,
            },
        ),
        // Two nodes subscribe to it.
        ScheduledOperation::new(node1.clone(), SimOperation::Subscribe { contract_id }),
        ScheduledOperation::new(node2.clone(), SimOperation::Subscribe { contract_id }),
        // Crash node1 mid-run (scripted, in-order).
        ScheduledOperation::new(node1.clone(), SimOperation::CrashNode),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(180),
        Duration::from_secs(30),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "controlled simulation with a scripted crash should complete: {:?}",
        result.turmoil_result.err()
    );

    for label in result.captured_node_labels() {
        tracing::info!(
            node = %label,
            hosting = result.node_hosting_count(&label),
            subscriptions = result.node_subscription_count(&label),
            upstream = ?result.node_upstream_count(&label, &contract_key),
            "post-crash measurement snapshot"
        );
    }

    // DISCRIMINATING assertion: the crash must have actually taken effect.
    // `CrashNode` installs the sim's packet-delivery callback and marks node1
    // crashed in the fault injector; every packet to/from node1 is then dropped
    // and counted. node1 held an established subscription (keep-alive traffic +
    // renewals + update fan-out), so post-crash traffic to/from it is inevitable
    // — `crash_packets_dropped() > 0` therefore holds ONLY if the crash really
    // blocked node1's packets. Before this harness fix the callback was never
    // installed, so a "crashed" node kept exchanging packets and this count
    // stayed 0 (the false-green the fix closes). See #4642 piece F.
    assert!(
        result.crash_packets_dropped() > 0,
        "scripted CrashNode must actually drop packets to/from the crashed node \
         (crash_packets_dropped == 0 means the crash was a silent no-op)"
    );

    // The contract must still be hosted somewhere in the surviving network:
    // the gateway hosts it, and node1's crash must not take it down.
    let hosted_somewhere = result
        .captured_node_labels()
        .iter()
        .any(|label| result.is_node_hosting(label, &contract_key));
    assert!(
        hosted_somewhere,
        "contract should remain hosted on a surviving node after node1 was crashed"
    );
}

/// Error-path coverage for the controllable-clock harness: scheduling
/// `SimOperation::AdvanceHostingClock` WITHOUT first calling
/// `enable_hosting_time_control()` must degrade gracefully — the runner logs a
/// warning and the advance is a no-op, rather than panicking or wedging. This
/// exercises the "no controllable hosting clock was enabled" branch of the
/// special-op dispatch.
#[test]
fn test_advance_hosting_clock_without_control_is_graceful_noop() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const NETWORK: &str = "advance-clock-no-control";
    const SEED: u64 = 0x4642_A002_CAFE;

    setup_deterministic_state(SEED);
    let rt = create_runtime();

    let gateway = NodeLabel::gateway(NETWORK, 0);
    let contract = SimOperation::create_test_contract(31);
    let state = SimOperation::create_test_state(31);

    // NOTE: enable_hosting_time_control() is deliberately NOT called here.
    let sim = rt.block_on(async { SimNetwork::new(NETWORK, 1, 1, 7, 3, 10, 2, SEED).await });

    let operations = vec![
        ScheduledOperation::new(
            gateway.clone(),
            SimOperation::Put {
                contract: contract.clone(),
                state: state.clone(),
                subscribe: true,
            },
        ),
        // No controllable clock is enabled → this must be a graceful no-op.
        ScheduledOperation::new(
            gateway.clone(),
            SimOperation::AdvanceHostingClock {
                duration: Duration::from_secs(30 * 60),
            },
        ),
    ];

    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(120),
        Duration::from_secs(20),
    );

    assert!(
        result.turmoil_result.is_ok(),
        "simulation must complete gracefully even when AdvanceHostingClock is scheduled \
         without a controllable clock enabled: {:?}",
        result.turmoil_result.err()
    );
    // The gateway still hosts its PUT — the no-op advance changed nothing.
    assert!(
        result.is_node_hosting(&gateway, &contract.key()),
        "gateway should still host its PUT contract"
    );
}

/// M2 (#4642 piece A): proves the injected hosting clock is NOT a silent no-op
/// end-to-end through SimNetwork → NodeConfig → Ring → HostingManager. This
/// guards the whole clock-injection wiring against future refactors: it is the
/// discriminating counterpart to
/// `test_hosting_clock_injection_and_measurement_end_to_end` (which only shows a
/// DEMANDED contract SURVIVES a clock jump — a no-op injection would pass that
/// too).
///
/// # Why this no longer tests undemanded eviction
///
/// The original version discriminated on the `min_ttl` cold-start grace: an
/// UNDEMANDED (zero-subscriber) contract under a 1-byte budget was TTL-protected
/// for 8 min, then evictable. `min_ttl` was DROPPED (invariant 3, 2026-07-08 —
/// PR #4734): an undemanded contract over budget is now evicted IMMEDIATELY
/// regardless of the clock, so undemanded eviction is no longer clock-gated and
/// can no longer discriminate on the clock. (Eviction of a *subscribed* contract
/// whose lease lapsed is also not a usable discriminator: `record_abandonment`
/// resets its recency to the frontier at lease termination, so the formerly-
/// subscribed contract sorts LAST and is the one that survives budget pressure —
/// the opposite of what "lease lapse → evicted" would need. And a hosted copy
/// under no budget pressure stays cached regardless of the clock.)
///
/// # What is still clock-gated: subscription lease expiry
///
/// The remaining time-based behavior driven by the injected clock in the
/// `HostingManager` is SUBSCRIPTION LEASE expiry. A lease lapses only once the
/// injected clock crosses `SUBSCRIPTION_LEASE_DURATION` (8 min); the periodic
/// expiry sweeps (`expire_stale_subscriptions`, `expire_stale_downstream_subscribers`)
/// read `time_source.now()` for the lease age. So we run the SAME scenario twice,
/// differing ONLY in how far the injected clock is advanced after the demand
/// source is gone:
///   * control: advance 1 min  (< SUBSCRIPTION_LEASE_DURATION = 8 min) → lease survives
///   * test:    advance 20 min (> SUBSCRIPTION_LEASE_DURATION)         → lease lapses
///
/// A gateway hosts + announces a contract and a node subscribes to it, forming a
/// real subscription (leases on both peers). The subscriber is then SILENTLY
/// crashed (`CrashNode`): a silent crash sends no `Unsubscribe` (so the leases are
/// NOT torn down immediately — that is what a clean `Disconnect` would do) and
/// stops the subscriber renewing (so nothing refreshes the leases). The leases can
/// therefore lapse ONLY via the injected clock crossing the 8-minute duration.
///
/// If the clock injection were broken (an `AdvanceHostingClock` that never reaches
/// the `HostingManager`), the leases would either both survive (with the crash
/// stopping renewal) or, absent the injected advance, never lapse within the
/// virtual settle window (120s ≪ 8 min) — either way both runs would show the same
/// surviving subscription set and the `test < control` assertion would fail, which
/// is exactly the regression this test exists to catch.
#[test]
fn test_injected_hosting_clock_drives_subscription_lease_lapse() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    // SUBSCRIPTION_LEASE_DURATION = LEASE_RENEWAL_MULTIPLIER (4) ×
    // SUBSCRIPTION_RENEWAL_INTERVAL (120s) = 480s (8 min). One advance stays
    // under it, the other crosses it.
    const SUB_LEASE_ADVANCE: Duration = Duration::from_secs(60); // 1 min  < 8 min
    const SUPER_LEASE_ADVANCE: Duration = Duration::from_secs(20 * 60); // 20 min > 8 min

    // Runs the scenario with a given clock advance; returns how many peers still
    // hold the contract in `active_subscriptions` network-wide, plus the crashed-
    // packet count (a discriminating signal that the scripted crash took effect).
    let run = |network: &str, seed: u64, advance: Duration| -> (usize, u64) {
        setup_deterministic_state(seed);
        let rt = create_runtime();
        let gateway = NodeLabel::gateway(network, 0);
        let subscriber = NodeLabel::node(network, 1);
        let contract = SimOperation::create_test_contract(51);
        let contract_id = *contract.key().id();

        let sim = rt.block_on(async {
            let mut sim = SimNetwork::new(network, 1, 3, 7, 3, 10, 2, seed).await;
            // Piece A: inject the controllable hosting clock so lease expiry is
            // under test control (advanced deterministically, without running
            // minutes of virtual time).
            sim.enable_hosting_time_control();
            sim
        });

        let ops = vec![
            // Gateway hosts + announces the contract so the subscriber can reach it.
            ScheduledOperation::new(
                gateway.clone(),
                SimOperation::Put {
                    contract: contract.clone(),
                    state: SimOperation::create_test_state(51),
                    subscribe: false,
                },
            ),
            // A node subscribes → forms a real subscription lease (timestamp read
            // from the injected hosting clock) on the subscriber and the gateway.
            ScheduledOperation::new(subscriber.clone(), SimOperation::Subscribe { contract_id }),
            // Silent crash: the subscriber sends NO Unsubscribe (so the leases are
            // not torn down immediately) and stops renewing (so nothing refreshes
            // them). The leases can now lapse ONLY via the injected clock crossing
            // SUBSCRIPTION_LEASE_DURATION — see the doc comment.
            ScheduledOperation::new(subscriber.clone(), SimOperation::CrashNode),
            // The ONLY thing that differs between the two runs.
            ScheduledOperation::new(
                gateway.clone(),
                SimOperation::AdvanceHostingClock { duration: advance },
            ),
        ];

        // Generous settle (120s ≈ 4 renewal/expiry cycles at 30s each) so the
        // expiry sweeps run against the advanced clock before the final snapshot.
        let result = sim.run_controlled_simulation(
            seed,
            ops,
            Duration::from_secs(300),
            Duration::from_secs(120),
        );
        assert!(
            result.turmoil_result.is_ok(),
            "controlled simulation should complete: {:?}",
            result.turmoil_result.err()
        );

        let subscribed_peers = result
            .topology_snapshots
            .iter()
            .filter(|s| s.active_subscription_keys.contains(&contract_id))
            .count();
        (subscribed_peers, result.crash_packets_dropped())
    };

    const SEED: u64 = 0x4642_A003_CAFE;
    let (control_count, control_dropped) = run("clock-lease-control", SEED, SUB_LEASE_ADVANCE);
    let (test_count, test_dropped) = run("clock-lease-test", SEED, SUPER_LEASE_ADVANCE);

    tracing::info!(
        control_count,
        test_count,
        control_dropped,
        test_dropped,
        "injected-clock lease lapse: control (sub-lease advance) vs test (super-lease advance)"
    );

    // Sanity: the scripted crash actually took effect in BOTH runs (it is what
    // stops renewal so the lease can lapse purely via the clock). `> 0` holds only
    // if the crash really blocked the subscriber's packets.
    assert!(
        control_dropped > 0 && test_dropped > 0,
        "scripted CrashNode must drop packets in both runs (control={control_dropped}, \
         test={test_dropped}); 0 means the crash was a silent no-op and renewal would \
         keep refreshing the lease"
    );

    // Sanity: the subscription genuinely FORMED and SURVIVED the sub-lease advance
    // (otherwise the comparison below would be vacuous). At least the gateway still
    // holds the contract in active_subscriptions 1 minute in.
    assert!(
        control_count >= 1,
        "sub-lease advance ({}s < 8min): the subscription must still be held by at least \
         one peer — the lease is younger than SUBSCRIPTION_LEASE_DURATION, so it must NOT \
         lapse; got {control_count} subscribed peer(s)",
        SUB_LEASE_ADVANCE.as_secs(),
    );

    // After crossing SUBSCRIPTION_LEASE_DURATION: with no renewal source, EVERY
    // lease lapses, so no peer holds the contract in active_subscriptions.
    assert_eq!(
        test_count,
        0,
        "super-lease advance ({}s > 8min): every subscription lease must lapse once the \
         injected clock crosses SUBSCRIPTION_LEASE_DURATION (nothing renews it after the \
         crash); got {test_count} peer(s) still subscribed",
        SUPER_LEASE_ADVANCE.as_secs(),
    );

    // The discriminating comparison: the ONLY difference between the two runs is
    // how far the injected clock advanced. Strictly fewer subscribed peers in the
    // super-lease run can happen ONLY if `AdvanceHostingClock` actually reached the
    // `HostingManager` and drove lease expiry. A no-op injection would make the two
    // runs identical (the crash stops renewal in both), so the counts would match.
    assert!(
        test_count < control_count,
        "the super-lease run must leave strictly fewer subscribed peers than the sub-lease \
         run (control={control_count}, test={test_count}); equal counts mean the injected \
         clock never reached the HostingManager (silent no-op — the regression this test \
         guards against)"
    );
}

// =============================================================================
// Interest-gated renewal — #3763 storm fix (demand-driven-hosting §5a / §7)
// =============================================================================
//
// These two simulations are SYSTEM-LEVEL behavioral coverage of the two
// demand-driven properties the interest-gated renewal predicate contributes to:
// (1) no-storm — the surviving subscription set tracks active DEMAND, not cache
// size; and (2) chain-collapse — a subscription mesh collapses to zero once all
// client interest ends. Each runs across ≥3 seeds (design note requirement),
// using the merged #4657 sim-harness: `enable_hosting_time_control` (a
// controllable clock so the 8-minute lease/TTL can be crossed deterministically),
// `with_hosting_budget` (cache pressure), `SimOperation::AdvanceHostingClock`,
// and the `node_subscription_count` / `node_active_demand_count` /
// `node_hosting_count` accessors.
//
// SCOPE NOTE (honest): on the CURRENT main codebase these two sims pass with OR
// without the `contract_in_use` gate in `contracts_needing_renewal` §1 —
// verified empirically. They therefore validate the end-to-end properties but do
// NOT, on their own, isolate the §1 predicate change. Two reasons: (a) main has
// already removed GET-auto-subscribe (#4689), so a GET-only contract never
// acquires a §1 active-subscription lease for the gate to act on; and (b) the
// collapse sim's 20-minute clock jump EXPIRES the leases outright, so the
// pre-existing `expires_at > now` lower-bound guard lapses them regardless of the
// gate. The DISCRIMINATING, deterministic proof of the predicate — a within-
// renewal-window, not-yet-expired, demand-less lease that is renewed iff
// `contract_in_use` — is the unit test
// `ring::hosting::tests::test_active_lease_renewed_iff_contract_in_use` (it FAILS
// without the gate, passes with it). These sims are the system-level regression
// companions to that unit proof; they will also catch a future regression that
// reintroduces unconditional renewal once piece-D chain-hosting re-enables the
// storm precondition.

/// Proof 1 (no-storm, design §7 / #3763): under sustained cache/GET load the
/// surviving subscription set must track active DEMAND, not cache size.
///
/// The hub SUBSCRIBES to a small demand set (real, pinned interest) and GETs a
/// larger cache-only set (fills its hosting cache, no subscription). Advancing
/// the hosting clock 20 virtual minutes ages every cache-only lease past both
/// the 8-minute lease and the 8-minute recent-local-access renewal window (§3),
/// so those leases lose their renewal source and lapse, while the
/// client-subscribed demand contracts keep their leases via the §1/§2
/// `contract_in_use` (client-subscription) path. The surviving subscription
/// count must therefore track demand, not cache.
///
/// This is the no-storm END-TO-END property. See the section SCOPE NOTE above:
/// on current main it holds with or without the §1 `contract_in_use` gate
/// (GET-auto-subscribe is already removed, so cache-only contracts never acquire
/// a §1 lease); the discriminating proof of the gate itself is the unit test
/// `test_active_lease_renewed_iff_contract_in_use`.
#[test_log::test]
fn test_subscription_count_tracks_demand_not_cache() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEEDS: [u64; 3] = [0x4642_D101, 0x4642_D102, 0x4642_D103];
    const DEMAND: usize = 2; // contracts the hub SUBSCRIBES to (active demand)
    const CACHE: usize = 8; // contracts the hub only GETs (cache-only)

    for seed in SEEDS {
        setup_deterministic_state(seed);
        let rt = create_runtime();
        let network = format!("subcount-demand-{seed:x}");

        let gateway = NodeLabel::gateway(&network, 0);
        let hub = NodeLabel::node(&network, 1);

        let demand_contracts: Vec<_> = (0..DEMAND)
            .map(|i| SimOperation::create_test_contract(0x40 + i as u8))
            .collect();
        let cache_contracts: Vec<_> = (0..CACHE)
            .map(|i| SimOperation::create_test_contract(0x60 + i as u8))
            .collect();
        // Instance-id sets so we can categorize surviving subscription leases
        // network-wide as demand-backed vs cache-only.
        let demand_ids: HashSet<_> = demand_contracts.iter().map(|c| *c.key().id()).collect();
        let cache_ids: HashSet<_> = cache_contracts.iter().map(|c| *c.key().id()).collect();

        // Dense 3-peer ring so every GET/SUBSCRIBE reliably completes — this
        // test is about renewal accounting, not routing/findability.
        let (sim, _clock) = rt.block_on(async {
            let mut sim = SimNetwork::new(&network, 1, 2, 7, 3, 10, 2, seed).await;
            let clock = sim.enable_hosting_time_control();
            // Deliberately reduced budget (1 MiB vs the ~1 GiB default) — still
            // comfortably holds the ~10 tiny test contracts, so the cache load
            // is observable as hosting_count >> subscription_count.
            sim.with_hosting_budget(1024 * 1024);
            (sim, clock)
        });

        let mut ops = Vec::new();
        // Gateway PUTs (and announces) every contract so the hub can fetch them.
        for c in demand_contracts.iter().chain(cache_contracts.iter()) {
            ops.push(ScheduledOperation::new(
                gateway.clone(),
                SimOperation::Put {
                    contract: c.clone(),
                    state: SimOperation::create_test_state(1),
                    subscribe: false,
                },
            ));
        }
        // Hub SUBSCRIBES to the demand contracts (real, pinned demand).
        for c in &demand_contracts {
            ops.push(ScheduledOperation::new(
                hub.clone(),
                SimOperation::Subscribe {
                    contract_id: *c.key().id(),
                },
            ));
        }
        // Hub GETs the cache-only contracts (fills its hosting cache, no subscribe).
        for c in &cache_contracts {
            ops.push(ScheduledOperation::new(
                hub.clone(),
                SimOperation::Get {
                    contract_id: *c.key().id(),
                    return_contract_code: true,
                    subscribe: false,
                },
            ));
        }
        // Jump the hosting clock 20 minutes — well past the 8-minute lease AND
        // the 8-minute recent-local-access window (§3) — so any GET-only lease
        // that §3 transiently installed loses its renewal source and lapses,
        // while the client-subscribed demand contracts keep their leases via the
        // §1/§2 `contract_in_use` (client-subscription) renewal path.
        ops.push(ScheduledOperation::new(
            hub.clone(),
            SimOperation::AdvanceHostingClock {
                duration: Duration::from_secs(20 * 60),
            },
        ));

        let result = sim.run_controlled_simulation(
            seed,
            ops,
            Duration::from_secs(300),
            Duration::from_secs(120),
        );
        assert!(
            result.turmoil_result.is_ok(),
            "seed={seed:x}: sim failed: {:?}",
            result.turmoil_result.err()
        );

        let hosting = result.node_hosting_count(&hub);
        let subs = result.node_subscription_count(&hub);
        let demand = result.node_active_demand_count(&hub);
        let metrics = result.aggregate_renewal_metrics();

        // Categorize every surviving subscription lease NETWORK-WIDE as
        // demand-backed (one of the hub's client subscriptions) vs cache-only
        // (one of the GET-only contracts). This is the strongest form of the
        // no-storm property: after demand fades, cache-only contracts hold ZERO
        // leases anywhere, while client-subscribed contracts keep theirs.
        let mut demand_leases = 0usize;
        let mut cache_leases = 0usize;
        for snap in &result.topology_snapshots {
            for id in &snap.active_subscription_keys {
                if demand_ids.contains(id) {
                    demand_leases += 1;
                } else if cache_ids.contains(id) {
                    cache_leases += 1;
                }
            }
        }
        eprintln!(
            "[proof1 seed={seed:x}] hub hosting_count={hosting} subscription_count={subs} \
             active_demand_count={demand:?} | network leases: demand={demand_leases} \
             cache={cache_leases} | max_cycle_batch={}",
            metrics.max_cycle_batch
        );

        // Scenario sanity: the cache load actually landed (not a degenerate run
        // where GETs never cached), so hosting_count >> lease-set is meaningful.
        assert!(
            hosting >= DEMAND + CACHE / 2,
            "seed={seed:x}: hub hosting_count ({hosting}) too low — the GET cache-load \
             did not land; test would be degenerate"
        );

        // CORE no-storm property (design §7 / #3763): the cache-only contracts —
        // which the hub GET-accessed but never subscribed, and stopped accessing
        // 20 virtual minutes ago — hold ZERO subscription leases anywhere in the
        // network. Interest-gated renewal must NOT let cache accrete leases.
        assert_eq!(
            cache_leases, 0,
            "seed={seed:x}: #3763 storm signature — {cache_leases} cache-only contract lease(s) \
             survived network-wide after demand faded. Subscriptions must track active demand, \
             NOT accumulated cache (hub hosting_count={hosting})."
        );

        // The hub's OWN lease set is bounded by the demand it actually created
        // (2 client subscriptions), never by its 10-contract cache.
        assert!(
            subs <= DEMAND + 1,
            "seed={seed:x}: hub subscription_count ({subs}) exceeds the demand it created \
             ({DEMAND}) + 1 — leases must not track the {hosting}-contract cache."
        );
        assert!(
            hosting > subs,
            "seed={seed:x}: hub hosting_count ({hosting}) must exceed subscription_count \
             ({subs}) — the separation between cache and leases must be real."
        );

        // Non-degeneracy: the demand subscriptions genuinely persisted (this is
        // NOT a run where everything simply lapsed).
        assert!(
            demand_leases >= 1,
            "seed={seed:x}: no demand-backed lease survived — the run is degenerate \
             (subscribes never took or all lapsed), so the cache==0 result is vacuous"
        );

        // Renewal batch cap (#4601): no node fans out more than 10 renewals per
        // 30s cycle, regardless of how many contracts are eligible.
        assert!(
            metrics.max_cycle_batch <= 10,
            "seed={seed:x}: max_cycle_batch ({}) exceeded the renewal cap of 10",
            metrics.max_cycle_batch
        );
    }
}

/// Per-run observation for the subscription-mesh collapse proof. Primitives
/// only, so the proof shares a driver without naming the sim-result type.
struct MeshObservation {
    /// Peers holding the contract in `active_subscriptions` at the end. In a
    /// FORMATION run this is the size of the subscription mesh; in a COLLAPSE
    /// run it is the set of survivors (must be empty).
    subscribed_peers: Vec<SocketAddr>,
    /// Nodes still hosting the contract in their cache at the end.
    hosting_nodes: usize,
    /// Max recorded upstreams at any node (subscription-tree parent edges).
    max_upstreams: usize,
    /// Total snapshots captured (guards against an empty-registry false read).
    total_snapshots: usize,
}

/// Shared driver for the subscription-mesh collapse proof.
///
/// Builds a moderately-connected ring, PUTs + announces a contract on the
/// gateway, and has `subscriber_ids` nodes subscribe — forming a connected mesh
/// of subscribed hosts toward the key. When `collapse` is set it then ends ALL
/// client interest (disconnect the PUT originator AND every subscriber) and
/// jumps the hosting clock 20 minutes past the 8-minute lease, so interest-gated
/// renewal must let every lease lapse.
///
/// The FORMATION run (`collapse=false`) and the COLLAPSE run (`collapse=true`)
/// share the same seed, topology, and subscribe prefix, so the mesh the
/// formation run measures at its end is deterministically the same mesh the
/// collapse run tears down — this is how a single end-of-run snapshot proves
/// "formed THEN collapsed" without a mid-run measurement.
fn run_subscription_mesh(
    seed: u64,
    network: &str,
    subscriber_ids: &[usize],
    collapse: bool,
) -> MeshObservation {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    setup_deterministic_state(seed);
    let rt = create_runtime();

    let gateway = NodeLabel::gateway(network, 0);
    let contract = SimOperation::create_test_contract(0xC0);
    let contract_key = contract.key();
    let contract_id = *contract.key().id();

    // Moderately connected so distant subscribes reliably reach the holder
    // (the terminal-advertisement consult only sees direct-neighbor hosts, so
    // too-sparse a ring dead-ends multi-hop subscribes) while still large enough
    // to build a real multi-node mesh.
    let sim = rt.block_on(async {
        let mut sim = SimNetwork::new(network, 1, 15, 10, 3, 5, 3, seed).await;
        sim.enable_hosting_time_control();
        sim
    });

    let mut ops = vec![ScheduledOperation::new(
        gateway.clone(),
        SimOperation::Put {
            contract: contract.clone(),
            state: SimOperation::create_test_state(1),
            subscribe: true,
        },
    )];
    for id in subscriber_ids {
        ops.push(ScheduledOperation::new(
            NodeLabel::node(network, *id),
            SimOperation::Subscribe { contract_id },
        ));
    }

    if collapse {
        // End ALL client interest: disconnect the PUT originator AND every
        // subscriber (Disconnect is ordered last for its node).
        ops.push(ScheduledOperation::new(
            gateway.clone(),
            SimOperation::Disconnect,
        ));
        for id in subscriber_ids {
            ops.push(ScheduledOperation::new(
                NodeLabel::node(network, *id),
                SimOperation::Disconnect,
            ));
        }
        // Jump 20 minutes — past the 8-minute lease AND the recent-access window
        // — so no renewal branch keeps an un-demanded lease alive.
        ops.push(ScheduledOperation::new(
            gateway.clone(),
            SimOperation::AdvanceHostingClock {
                duration: Duration::from_secs(20 * 60),
            },
        ));
    }

    // Generous settle (120s ≈ 4 renewal/expiry cycles at 30s each) so the
    // expire-sweep + interest-gated non-renewal removes every lapsed lease
    // before the final snapshot.
    let result = sim.run_controlled_simulation(
        seed,
        ops,
        Duration::from_secs(360),
        Duration::from_secs(120),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "seed={seed:x} collapse={collapse}: sim failed: {:?}",
        result.turmoil_result.err()
    );

    let subscribed_peers: Vec<SocketAddr> = result
        .topology_snapshots
        .iter()
        .filter(|s| s.active_subscription_keys.contains(&contract_id))
        .map(|s| s.peer_addr)
        .collect();
    let hosting_nodes = result
        .captured_node_labels()
        .iter()
        .filter(|l| result.is_node_hosting(l, &contract_key))
        .count();
    let max_upstreams = result
        .captured_node_labels()
        .iter()
        .map(|l| result.node_upstream_count(l, &contract_key).unwrap_or(0))
        .max()
        .unwrap_or(0);

    MeshObservation {
        subscribed_peers,
        hosting_nodes,
        max_upstreams,
        total_snapshots: result.topology_snapshots.len(),
    }
}

/// Subscriber set used by the collapse proof — a spread of nodes across the
/// ring so a genuine multi-node mesh forms.
const MESH_SUBSCRIBERS: [usize; 8] = [1, 3, 5, 7, 9, 11, 13, 15];

/// Proof 2 (chain collapse on client leave, design §5a/§6). Two phases sharing
/// one seed + topology + subscribe prefix (so determinism makes the FORMATION
/// run's mesh identical to what the COLLAPSE run tears down):
///
///   * FORMATION run: a genuine multi-node subscription mesh forms.
///   * COLLAPSE run: after ALL clients leave + a 20-minute clock jump, EVERY
///     lease lapses — interest-gated renewal keeps none alive.
///
/// Formation is proved by the mesh SIZE measured at the end of the FORMATION
/// run (the shared seed/topology guarantees the COLLAPSE run tears down that
/// same mesh); collapse is proved by ZERO surviving `active_subscriptions`
/// anywhere in the COLLAPSE run. This is the chain-collapse END-TO-END property.
/// See the section SCOPE NOTE above: on current main the collapse also holds
/// without the §1 gate, because the 20-minute clock jump expires the leases
/// outright (the pre-existing `expires_at > now` guard lapses them); the
/// discriminating proof of the gate itself is the unit test
/// `test_active_lease_renewed_iff_contract_in_use`.
#[test_log::test]
fn test_subscription_chain_collapses_on_client_leave() {
    const SEEDS: [u64; 3] = [0x4642_D201, 0x4642_D202, 0x4642_D203];
    // The mesh must be non-trivial for a collapse-to-zero to be meaningful.
    const MIN_MESH: usize = 3;

    for seed in SEEDS {
        // Phase A — formation baseline (no disconnect / no clock jump).
        let form = run_subscription_mesh(
            seed,
            &format!("chain-form-{seed:x}"),
            &MESH_SUBSCRIBERS,
            false,
        );
        eprintln!(
            "[proof2 seed={seed:x} FORM] mesh_size={} hosting_nodes={} \
             max_upstreams={} total_snapshots={}",
            form.subscribed_peers.len(),
            form.hosting_nodes,
            form.max_upstreams,
            form.total_snapshots,
        );
        assert!(
            form.subscribed_peers.len() >= MIN_MESH,
            "seed={seed:x}: formation degenerate — only {} peer(s) in the subscription mesh \
             (need >= {MIN_MESH}); the collapse below would be vacuous. \
             Requested {} subscribers.",
            form.subscribed_peers.len(),
            MESH_SUBSCRIBERS.len(),
        );

        // Phase B — collapse (same seed/topology + disconnect-all + 20-min jump).
        let coll = run_subscription_mesh(
            seed,
            &format!("chain-collapse-{seed:x}"),
            &MESH_SUBSCRIBERS,
            true,
        );
        eprintln!(
            "[proof2 seed={seed:x} COLLAPSE] survivors={} hosting_nodes={} total_snapshots={} {:?}",
            coll.subscribed_peers.len(),
            coll.hosting_nodes,
            coll.total_snapshots,
            coll.subscribed_peers,
        );
        assert!(
            coll.total_snapshots > 0,
            "seed={seed:x}: no snapshots captured in collapse run — cannot judge collapse"
        );
        assert!(
            coll.subscribed_peers.is_empty(),
            "seed={seed:x}: chain did NOT collapse — {} peer(s) still hold the contract in \
             active_subscriptions after all client interest ended + a 20-min clock jump: {:?}. \
             Interest-gated renewal must let every un-demanded lease lapse.",
            coll.subscribed_peers.len(),
            coll.subscribed_peers,
        );
    }
}

// =============================================================================
// Piece E (remove relay-caching) — findability + stale-serve baseline GATE
// (demand-driven-hosting redesign; epic freenet/freenet-core#4642, piece E;
//  interim relay-GET freshness guard PR freenet/freenet-core#4709)
// =============================================================================
//
// WHY THIS EXISTS
// ---------------
// Piece E removes relay-caching (PUT storing at every hop; GET caching on the
// return path as a durable/advertised host). Relay-caching was DOUBLE-DUTY: the
// anti-pattern the redesign kills AND a *findability crutch* — its scattered
// copies made greedy single-path routing robust because a copy was almost
// always ~1 hop from any requester. Removing it therefore risks regressing two
// distinct properties, which the two tests below MEASURE on current `main` so a
// future piece-E branch can be compared against these baseline numbers:
//
//   1. FINDABILITY (hosting-invariant 5): a distant node can GET a contract
//      whose greedy descent can dead-end >1 hop from a holder — the "distant
//      unfindable contract" that BLOCKED piece E (6/8 dead-ends in the 16-peer
//      sparse-ring sim noted in `.claude/rules/hosting-invariants.md`, piece E).
//      Test A PUTs a contract from a far node; on main, relay-caching's PUT half
//      scatters copies toward the key so distant GETs stay findable (mean ~0.85
//      over 6 seeds). The gate is that this MASKED rate must NOT drop when piece
//      E removes the scatter (E ships bounded backtracking + a PUT seed-chain to
//      hold it up). Companion finding (reported in the PR, not a separate test):
//      a SEEDED lone holder scatters nothing and is already ~0-findable on main
//      across 5/6 seeds — that near-zero is the post-E-like weakness, and it is
//      why a single-seed floor is meaningless and the gate is MULTI-SEED.
//
//   2. STALE-SERVE / invariant 1: "a served copy is fresh; no durable
//      UNSUBSCRIBED copy answers reads." On main, `check_local_with_interest_gate`
//      (`operations/get/op_ctx_task.rs`) serves ANY cached copy whose
//      cache-presence `hosting` flag is set — so a relay holding an unsubscribed
//      copy that went stale after an UPDATE serves the STALE bytes. PR #4709
//      changes that relay gate to `Ring::is_receiving_updates` (freshness).
//      HONEST SCOPE: this cannot be reproduced as a discriminating sim gate — in
//      the harness an UPDATE converges to every REACHABLE cache holder, so a
//      durable unsubscribed stale copy never survives to be served (the metric
//      reads 0 whether or not #4709 is present). #4709's real validation is its
//      own `interest_gate_disposition` UNIT TESTS. Test B is therefore an
//      invariant-1 STRUCTURAL guard plus a forced-stale DETECTOR positive control
//      (a crash-isolated copy is proven flagged stale), NOT a stale-serve gate.
//
// These are TEST/sim infrastructure — they change no production behavior. Both
// use the #4657 controlled-sim runner (`run_controlled_simulation`), which is
// deterministic (seeded turmoil + virtual time; NO wall-clock/sleep dependence —
// the `tokio::time::sleep`s inside are turmoil-virtual) and CI-runnable. NOTE on
// runner choice: the direct runner (`run_simulation_direct`) is deterministic
// too but drives RANDOM events only — it cannot script "PUT at A, GET from
// distant F", which both scenarios require — so the controlled runner is the
// correct deterministic vehicle here. Churn = scripted node DEPARTURE
// (`SimOperation::CrashNode`); the controlled path cannot script mid-run JOIN.
//
// HARNESS LIMITATION (reported precisely): the production GET path registers
// `GetEvent::GetSuccess` with `state_hash: None` (wire-message path), so the
// per-GET RETURNED state is NOT in the event log. Freshness is therefore
// measured STRUCTURALLY from each node's end-of-run local store
// (`MockStateStorage`) after a deterministic propagation wait: a requester's
// post-GET cached copy IS the state it was served (GET-return caching is active
// on main), and any node still holding pre-UPDATE bytes after convergence is a
// stale copy. This is mechanism-stable across the piece-E change.

/// Baseline metrics for the piece-E findability + stale-serve gates. Computed
/// from one controlled-sim run and both logged and returned so a future
/// piece-E / #4709 branch can diff against the numbers printed on `main`.
#[derive(Debug, Clone)]
struct PieceEGateMetrics {
    // ---- Findability (wire per-tx, the codebase's own GET-reliability metric) ----
    get_attempts: u64,
    get_successes: u64,
    get_not_found: u64,
    get_failures: u64,
    get_timeouts: u64,
    /// Successes whose GET traversed the network (hop_count >= 1).
    network_successes: u64,
    /// wire successes / attempts, in [0, 1].
    findability_rate: f64,
    // ---- Findability (client-visible: requester ended up holding the state) ----
    requesters_total: usize,
    /// Requesters whose end-of-run store holds the contract (a completed GET
    /// caches the returned copy) — the client-visible findability numerator.
    requesters_with_state: usize,
    /// requesters_with_state / requesters_total, in [0, 1]. PRIMARY findability
    /// number (client-visible; the wire rate can over-count relay hops).
    client_findability_rate: f64,
    /// Health metric: elapsed_ms of the fastest successful GET (time-to-first-op).
    time_to_first_success_ms: Option<u64>,
    // ---- Subscription-tree formation (health metric) ----
    hosting_nodes: usize,
    // ---- Freshness / stale-serve (hosting-invariant 1; PR #4709) ----
    /// Nodes holding ANY stored state for the key at end of run.
    total_holders: usize,
    /// Holders whose stored state matches the source-of-truth's current state.
    fresh_holders: usize,
    /// Holders whose stored state differs from current — durable stale copies.
    stale_holders: usize,
    /// Requesters that ended holding the CURRENT state (served fresh).
    requesters_fresh: usize,
    /// Requesters that ended holding a NON-current state (SERVED STALE) — the
    /// direct #4709 signal.
    requesters_stale: usize,
    /// requesters_stale / (requesters_fresh + requesters_stale), in [0, 1].
    /// PRIMARY stale-serve number. `None` if no requester obtained any state.
    stale_serve_rate: Option<f64>,
    /// Mesh subscribers, and how many ended fresh (invariant-1 positive control).
    subscribers_total: usize,
    subscribers_fresh: usize,
}

/// Compute [`PieceEGateMetrics`] from a finished controlled-sim run.
///
/// `current_source` is the node whose end-of-run stored state defines "current"
/// (the seeded/updated source of truth); `get_requesters` issued the GETs whose
/// findability + served-freshness we measure; `subscribers` joined the update
/// mesh (freshness positive control).
#[allow(clippy::too_many_arguments)]
fn compute_piece_e_metrics(
    result: &freenet::dev_tool::ControlledSimulationResult,
    logs: &[freenet::tracing::NetLogMessage],
    contract_key: &freenet_stdlib::prelude::ContractKey,
    current_source: &freenet::dev_tool::NodeLabel,
    get_requesters: &[freenet::dev_tool::NodeLabel],
    subscribers: &[freenet::dev_tool::NodeLabel],
) -> PieceEGateMetrics {
    // --- Findability: wire per-tx GET outcomes (dedup per attempt tx) ---
    let summary = freenet::tracing::summarize_get_outcomes_per_tx(logs);
    let get_attempts = summary.total();
    let findability_rate = if get_attempts == 0 {
        0.0
    } else {
        summary.successes as f64 / get_attempts as f64
    };
    let time_to_first_success_ms = summary.success_elapsed_ms.first().copied();

    // --- "current" = the source-of-truth node's end-of-run state hash. ---
    let current_hash = result
        .node_storages
        .get(current_source)
        .and_then(|s| s.get_stored_state(contract_key))
        .map(|ws| freenet::tracing::state_hash_short(&ws));

    let hash_of = |label: &freenet::dev_tool::NodeLabel| -> Option<String> {
        result
            .node_storages
            .get(label)
            .and_then(|s| s.get_stored_state(contract_key))
            .map(|ws| freenet::tracing::state_hash_short(&ws))
    };

    // --- Findability (client-visible) + served-freshness, per requester. ---
    let requesters_total = get_requesters.len();
    let mut requesters_with_state = 0usize;
    let mut requesters_fresh = 0usize;
    let mut requesters_stale = 0usize;
    for r in get_requesters {
        if let Some(h) = hash_of(r) {
            requesters_with_state += 1;
            if current_hash.as_deref() == Some(h.as_str()) {
                requesters_fresh += 1;
            } else {
                requesters_stale += 1;
            }
        }
    }
    let client_findability_rate = if requesters_total == 0 {
        0.0
    } else {
        requesters_with_state as f64 / requesters_total as f64
    };
    let served = requesters_fresh + requesters_stale;
    let stale_serve_rate = if served == 0 {
        None
    } else {
        Some(requesters_stale as f64 / served as f64)
    };

    // --- Subscription-tree formation ---
    let hosting_nodes = result
        .captured_node_labels()
        .iter()
        .filter(|l| result.is_node_hosting(l, contract_key))
        .count();

    // --- Freshness across ALL holders (structural invariant-1 surface) ---
    let mut total_holders = 0usize;
    let mut fresh_holders = 0usize;
    let mut stale_holders = 0usize;
    for (_label, storage) in result.node_storages.iter() {
        let Some(state) = storage.get_stored_state(contract_key) else {
            continue;
        };
        total_holders += 1;
        if current_hash.as_deref() == Some(freenet::tracing::state_hash_short(&state).as_str()) {
            fresh_holders += 1;
        } else {
            stale_holders += 1;
        }
    }

    let subscribers_total = subscribers.len();
    let subscribers_fresh = subscribers
        .iter()
        .filter(|l| hash_of(l).as_deref() == current_hash.as_deref())
        .count();

    PieceEGateMetrics {
        get_attempts,
        get_successes: summary.successes,
        get_not_found: summary.not_found,
        get_failures: summary.failures,
        get_timeouts: summary.timeouts,
        network_successes: summary.network_successes,
        findability_rate,
        requesters_total,
        requesters_with_state,
        client_findability_rate,
        time_to_first_success_ms,
        hosting_nodes,
        total_holders,
        fresh_holders,
        stale_holders,
        requesters_fresh,
        requesters_stale,
        stale_serve_rate,
        subscribers_total,
        subscribers_fresh,
    }
}

/// Rank the regular nodes by ring distance to `key_loc`, nearest first.
/// Returns node_no (1..=nodes) in ascending distance order. `locs` is
/// `get_peer_locations()` = [gateway, node_1 .. node_N].
fn nodes_by_distance_to_key(locs: &[f64], num_nodes: usize, key_loc: f64) -> Vec<usize> {
    let ring_dist = |a: f64, b: f64| {
        let d = (a - b).abs();
        d.min(1.0 - d)
    };
    let mut ranked: Vec<(usize, f64)> = (1..=num_nodes)
        .map(|i| (i, ring_dist(locs[i], key_loc)))
        .collect();
    ranked.sort_by(|a, b| a.1.total_cmp(&b.1));
    ranked.into_iter().map(|(n, _)| n).collect()
}

/// One findability run at `seed` on the sparse ring. Returns the metrics,
/// whether the PUT origin still hosts at end, and the crash-drop count (proof
/// the scripted churn actually took effect this seed).
///
/// Uses a real PUT (not a seed): on main, relay-caching's PUT half scatters the
/// contract toward the key as it routes, so distant GETs find a copy ~1 hop away
/// — this is the masked, high findability that piece E must PRESERVE when it
/// removes that scatter. (A *seeded* lone holder scatters nothing and is already
/// ~0-findable on main across most seeds; that is the post-E-like weakness, not
/// the property to floor here.)
fn run_findability_seed(seed: u64) -> (PieceEGateMetrics, bool, u64) {
    use freenet::dev_tool::{Location, NodeLabel, ScheduledOperation, SimNetwork, SimOperation};
    const NUM_NODES: usize = 15; // + 1 gateway = 16 peers

    setup_deterministic_state(seed);
    let network = format!("piece-e-find-{seed:x}");

    let contract = SimOperation::create_test_contract(0xA5);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    let key_loc = Location::from(&contract_key).as_f64();
    let state_v1 = SimOperation::create_test_state(1);

    // Evenly spread NUM_NODES around the whole ring, offset from the key.
    let wrap = |x: f64| x.rem_euclid(1.0);
    let node_locations: Vec<f64> = (0..NUM_NODES)
        .map(|i| wrap(key_loc + i as f64 / NUM_NODES as f64))
        .collect();

    let rt = create_runtime();
    let sim = rt.block_on(async {
        SimNetwork::new_with_node_locations(
            &network,
            1,
            NUM_NODES,
            10, // ring_max_htl
            7,  // rnd_if_htl_above
            5,  // max_connections (deliberately sparse ring)
            2,  // min_connections
            seed,
            &node_locations,
        )
        .await
    });
    // Migration OFF (sim default = production). Relay-caching + piece-C consult
    // ACTIVE on main — records current PUT-scattered findability behavior.

    let locs = sim.get_peer_locations();
    let ranked = nodes_by_distance_to_key(&locs, NUM_NODES, key_loc);
    // PUT origin: farthest from the key, so the PUT routes across the ring toward
    // the key and (on main) scatters copies along the way.
    let holder_no = ranked[14];
    // Churn victims: near-key nodes on the descent path.
    let churn_nos = [ranked[3], ranked[4]];
    // Requesters: the mid/far nodes (excluding the PUT origin).
    let requester_nos: Vec<usize> = [6usize, 7, 8, 9, 10, 11, 12, 13]
        .iter()
        .map(|&i| ranked[i])
        .collect();

    let holder = NodeLabel::node(&network, holder_no);
    let get_requesters: Vec<NodeLabel> = requester_nos
        .iter()
        .map(|n| NodeLabel::node(&network, *n))
        .collect();

    let mut operations = Vec::new();
    operations.push(ScheduledOperation::new(
        holder.clone(),
        SimOperation::Put {
            contract: contract.clone(),
            state: state_v1.clone(),
            subscribe: true,
        },
    ));
    for c in &churn_nos {
        operations.push(ScheduledOperation::new(
            NodeLabel::node(&network, *c),
            SimOperation::CrashNode,
        ));
    }
    for r in &get_requesters {
        operations.push(ScheduledOperation::new(
            r.clone(),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }

    let logs_handle = sim.event_logs_handle();
    let result = sim.run_controlled_simulation(
        seed,
        operations,
        Duration::from_secs(300),
        Duration::from_secs(90),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "seed={seed:x}: findability sim failed: {:?}",
        result.turmoil_result.err()
    );

    let logs = rt.block_on(async { logs_handle.lock().await.clone() });
    let metrics =
        compute_piece_e_metrics(&result, &logs, &contract_key, &holder, &get_requesters, &[]);
    let holder_hosting = result.is_node_hosting(&holder, &contract_key);
    let crash_dropped = result.crash_packets_dropped();
    (metrics, holder_hosting, crash_dropped)
}

/// GATE A — sparse-ring, PUT-scattered FINDABILITY under churn (MULTI-SEED).
///
/// A contract is PUT from a far node and, on main, relay-caching's PUT half
/// scatters copies toward the key as the PUT routes; distant requesters then GET
/// under scripted churn. This is the MASKED, high findability that piece E must
/// PRESERVE when it removes that scatter — the property whose regression this
/// gate guards. (Note the companion finding reported in the PR: a *seeded* lone
/// holder scatters nothing and is already ~0-findable on main across most seeds
/// — that near-zero is the post-E-like weakness, which is exactly why a
/// single-seed floor is meaningless here.)
///
/// Sparse-ring findability is seed-sensitive, so a single-seed floor would be
/// both flaky and pass-by-luck. The gate is an AGGREGATE floor over several
/// independent topologies (>= 5 seeds): the mean client-findability must clear
/// `MEAN_FLOOR`, and all-but-one seed must clear `PER_SEED_FLOOR`. Floors are set
/// robustly below the observed main baseline spread (reported in the PR body) and
/// above a real regression.
///
/// Topology per seed: 1 gateway + 15 nodes evenly spread on the ring (16 peers,
/// matching the `hosting-invariants.md` 16-peer sparse-ring figure), max 5
/// connections (deliberately sparse).
#[test_log::test]
fn test_piece_e_findability_sparse_ring_gate() {
    const SEEDS: [u64; 6] = [
        0x4642_E0A0_5A1D,
        0x4642_E0A0_0002,
        0x4642_E0A0_0003,
        0x4642_E0A0_0005,
        0x4642_E0A0_0007,
        0x4642_E0A0_000B,
    ];
    // POST-E GATE floors — must still hold once relay-caching's masking scatter
    // is removed. Set below the observed main baseline spread (see PR body).
    const MEAN_FLOOR: f64 = 0.60;
    const PER_SEED_FLOOR: f64 = 0.50;

    let mut rates = Vec::new();
    let mut total_crash = 0u64;
    tracing::info!(target: "piece_e_gate",
        "===== PIECE-E FINDABILITY BASELINE (sparse 15+1 PUT-scattered, main, {} seeds) =====",
        SEEDS.len());
    for seed in SEEDS {
        let (m, holder_hosting, crash_dropped) = run_findability_seed(seed);
        assert!(m.get_attempts > 0, "seed={seed:x}: no GET attempts");
        assert!(
            holder_hosting,
            "seed={seed:x}: PUT origin must still host the contract"
        );
        total_crash += crash_dropped;
        rates.push(m.client_findability_rate);
        tracing::info!(target: "piece_e_gate",
            "  seed=0x{seed:X}: client_findability={:.3} ({}/{}) wire={:.3} hosting_nodes={} ttf_ms={:?}",
            m.client_findability_rate, m.requesters_with_state, m.requesters_total,
            m.findability_rate, m.hosting_nodes, m.time_to_first_success_ms);
    }
    let n = rates.len() as f64;
    let mean = rates.iter().sum::<f64>() / n;
    let min = rates.iter().copied().fold(f64::INFINITY, f64::min);
    let max = rates.iter().copied().fold(f64::NEG_INFINITY, f64::max);
    let above = rates.iter().filter(|&&r| r >= PER_SEED_FLOOR).count();
    tracing::info!(target: "piece_e_gate",
        "  MEAN client_findability = {mean:.3} (min={min:.3} max={max:.3}); \
         {above}/{} seeds >= per-seed floor {PER_SEED_FLOOR}; rates={rates:?}",
        SEEDS.len());
    tracing::info!(target: "piece_e_gate", "===== END PIECE-E FINDABILITY BASELINE =====");

    assert!(
        total_crash > 0,
        "scripted CrashNode dropped no packets across any seed — churn had no effect"
    );
    // REAL GATE (not a comment): mean findability across seeds must clear the floor.
    assert!(
        mean >= MEAN_FLOOR,
        "FINDABILITY REGRESSION: mean client-findability {mean:.3} across {} seeds \
         < floor {MEAN_FLOOR} (rates={rates:?})",
        SEEDS.len()
    );
    // ...and all but at most one seed must individually clear the per-seed floor,
    // so one unlucky topology can't be masked by a high mean.
    assert!(
        above >= SEEDS.len() - 1,
        "FINDABILITY REGRESSION: only {above}/{} seeds reached the per-seed floor \
         {PER_SEED_FLOOR} (rates={rates:?})",
        SEEDS.len()
    );
}

/// GATE B — invariant-1 STRUCTURAL guard + forced-stale DETECTOR positive control.
///
/// HONEST SCOPE (do not mistake this for a discriminating stale-serve gate): a
/// full-sim reproduction of relay stale-serve is NOT achievable with this
/// harness. In-sim, an UPDATE converges to every reachable cache holder
/// (broadcast + proximity), so a durable UNSUBSCRIBED stale copy does not
/// survive to be served — the metric reads 0 whether or not the #4709 relay
/// freshness guard is present. The REAL stale-serve validation for #4709 is its
/// own `interest_gate_disposition` UNIT TESTS, not a sim.
///
/// What this test DOES prove, with assertions:
///   * the UPDATE is a real state change (v1 != v2) and applied at HOST;
///   * update propagation + the freshness detector work — the mesh subscribers
///     end fresh (invariant-1 positive control);
///   * the stale DETECTOR can read NON-ZERO — a node deliberately CRASH-ISOLATED
///     from the UPDATE after caching v1 is flagged stale. A detector that can
///     only ever read 0 is untrustworthy; this forces the non-zero case.
///
/// Timeline (1 gateway + 8 nodes):
///   0. HOST near the key seeds v1 (subscribed, fresh, reachable).
///   1. REACHABLE + ISOLATED nodes GET(subscribe=false) v1 → cache v1.
///   2. MESH nodes GET(subscribe=true) v1 → join the update mesh (positive control).
///   3. CRASH-ISOLATE the ISOLATED node (message-block) so the UPDATE cannot reach it.
///   4. HOST UPDATEs to v2 (CRDT-versioned, so v2 genuinely wins) → HOST + mesh +
///      REACHABLE refresh to v2; the isolated copy stays v1 = provably stale.
#[test_log::test]
fn test_piece_e_unsubscribed_stale_serve_gate() {
    use freenet::dev_tool::{Location, NodeLabel, ScheduledOperation, SimNetwork, SimOperation};

    const SEED: u64 = 0x4642_E0B0_57A1;
    const NETWORK_NAME: &str = "piece-e-stale-serve";
    const NUM_NODES: usize = 8;

    setup_deterministic_state(SEED);

    let contract = SimOperation::create_test_contract(0xB7);
    let contract_id = *contract.key().id();
    let contract_key = contract.key();
    let key_loc = Location::from(&contract_key).as_f64();
    // CRDT-versioned states so the UPDATE is a REAL state change: MockRuntime's
    // default byte-merge of two arbitrary states is a no-op (keeps existing
    // bytes), which would make the UPDATE invisible and every read trivially
    // "fresh". CRDT mode makes the higher version win, so v2 genuinely replaces v1.
    freenet::dev_tool::register_crdt_contract(contract_id);
    let state_v1 = SimOperation::create_crdt_state(1, 0x11);
    let state_v2 = SimOperation::create_crdt_state(2, 0x22);

    let wrap = |x: f64| x.rem_euclid(1.0);
    let node_locations: Vec<f64> = (0..NUM_NODES)
        .map(|i| wrap(key_loc + i as f64 / NUM_NODES as f64))
        .collect();

    let rt = create_runtime();
    let sim = rt.block_on(async {
        SimNetwork::new_with_node_locations(
            NETWORK_NAME,
            1,
            NUM_NODES,
            10,
            7,
            8,
            3,
            SEED,
            &node_locations,
        )
        .await
    });

    let locs = sim.get_peer_locations();
    let ranked = nodes_by_distance_to_key(&locs, NUM_NODES, key_loc);
    let host_no = ranked[0]; // nearest key: source of truth
    let reachable_no = ranked[1]; // caches v1, stays reachable → refreshes to v2
    let isolated_no = ranked[2]; // caches v1, then crash-isolated → forced stale
    let mesh_nos = [ranked[3], ranked[4]]; // join the update mesh (positive control)

    let host = NodeLabel::node(NETWORK_NAME, host_no);
    let reachable = NodeLabel::node(NETWORK_NAME, reachable_no);
    let isolated = NodeLabel::node(NETWORK_NAME, isolated_no);
    let mesh: Vec<NodeLabel> = mesh_nos
        .iter()
        .map(|n| NodeLabel::node(NETWORK_NAME, *n))
        .collect();

    tracing::info!(
        key_loc,
        host = host_no,
        reachable = reachable_no,
        isolated = isolated_no,
        mesh = ?mesh_nos,
        "piece-E stale-serve gate: roles by ring distance to key"
    );

    let mut operations = Vec::new();
    // 0. Source of truth near the key.
    operations.push(ScheduledOperation::new(
        host.clone(),
        SimOperation::SeedHostedContract {
            contract: contract.clone(),
            state: state_v1.clone(),
        },
    ));
    // 1. Two read-only GETs cache v1 (unsubscribed).
    for n in [&reachable, &isolated] {
        operations.push(ScheduledOperation::new(
            n.clone(),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: false,
            },
        ));
    }
    // 2. Mesh: GET-with-subscribe joins the update mesh (fetch + subscribe; a bare
    //    Subscribe is rejected client-side until the WASM is cached locally).
    for m in &mesh {
        operations.push(ScheduledOperation::new(
            m.clone(),
            SimOperation::Get {
                contract_id,
                return_contract_code: true,
                subscribe: true,
            },
        ));
    }
    // 3. Crash-isolate the ISOLATED node BEFORE the UPDATE so it cannot receive
    //    it — this manufactures the known-stale copy the detector must flag.
    operations.push(ScheduledOperation::new(
        isolated.clone(),
        SimOperation::CrashNode,
    ));
    // 4. UPDATE to v2: HOST + reachable + mesh refresh; the isolated copy stays v1.
    operations.push(ScheduledOperation::new(
        host.clone(),
        SimOperation::Update {
            key: contract_key,
            data: state_v2.clone(),
        },
    ));

    let logs_handle = sim.event_logs_handle();
    let result = sim.run_controlled_simulation(
        SEED,
        operations,
        Duration::from_secs(300),
        Duration::from_secs(90),
    );
    assert!(
        result.turmoil_result.is_ok(),
        "stale-serve gate sim failed: {:?}",
        result.turmoil_result.err()
    );

    let logs = rt.block_on(async { logs_handle.lock().await.clone() });
    // No probe requesters (stale-serve is not reproducible end-to-end — see doc);
    // mesh is the positive control; HOST defines "current" (= v2 after the UPDATE).
    let metrics = compute_piece_e_metrics(&result, &logs, &contract_key, &host, &[], &mesh);

    let v1_hash = freenet::tracing::state_hash_short(&freenet_stdlib::prelude::WrappedState::new(
        state_v1.clone(),
    ));
    let v2_hash = freenet::tracing::state_hash_short(&freenet_stdlib::prelude::WrappedState::new(
        state_v2.clone(),
    ));
    let hash_of = |label: &NodeLabel| -> Option<String> {
        result
            .node_storages
            .get(label)
            .and_then(|s| s.get_stored_state(&contract_key))
            .map(|ws| freenet::tracing::state_hash_short(&ws))
    };
    let host_current = hash_of(&host);
    let isolated_hash = hash_of(&isolated);
    let reachable_hash = hash_of(&reachable);
    // DETECTOR positive control: the crash-isolated node cached v1 and never saw
    // the UPDATE, so it MUST be flagged stale (holds non-current state).
    let forced_stale_detected =
        isolated_hash.is_some() && isolated_hash.as_deref() != host_current.as_deref();

    tracing::info!(target: "piece_e_gate",
        "===== PIECE-E INVARIANT-1 STRUCTURAL GUARD + STALE-DETECTOR CONTROL (seed=0x{SEED:X}, main) =====");
    tracing::info!(target: "piece_e_gate", "  v1={v1_hash} v2={v2_hash} host_current={host_current:?}");
    tracing::info!(target: "piece_e_gate",
        "  FORCED-STALE positive control: isolated node hash={isolated_hash:?} flagged_stale={forced_stale_detected}");
    tracing::info!(target: "piece_e_gate",
        "  reachable (non-isolated) cache hash={reachable_hash:?}  (main refreshes reachable caches -> expect v2)");
    tracing::info!(target: "piece_e_gate",
        "  durable stale copies (all nodes): {} of {} holders (fresh={})",
        metrics.stale_holders, metrics.total_holders, metrics.fresh_holders);
    tracing::info!(target: "piece_e_gate",
        "  mesh subscribers fresh after UPDATE (positive control): {}/{}",
        metrics.subscribers_fresh, metrics.subscribers_total);
    tracing::info!(target: "piece_e_gate", "===== END =====");

    // --- Honest, discriminating assertions ---
    // (1) The UPDATE is a real state change — else the freshness detector is hollow.
    assert_ne!(
        v1_hash, v2_hash,
        "v1 and v2 must differ — the UPDATE must be a real state change"
    );
    // (2) The UPDATE actually applied at HOST (a real check, replacing the old
    //     fresh+stale==total tautology).
    assert_eq!(
        host_current.as_deref(),
        Some(v2_hash.as_str()),
        "UPDATE must apply at HOST (host_current should equal v2)"
    );
    // (3) Positive control: update propagation + freshness detection work — the
    //     mesh subscribers received the update and read fresh.
    assert_eq!(
        metrics.subscribers_fresh, metrics.subscribers_total,
        "mesh subscribers must all be fresh after the UPDATE (invariant-1 positive control)"
    );
    // (4) DETECTOR positive control: crash-isolation took effect AND the isolated
    //     copy is provably flagged stale. Proves the detector can read non-zero.
    assert!(
        result.crash_packets_dropped() > 0,
        "crash-isolation of the stale node had no effect (no packets dropped)"
    );
    assert!(
        forced_stale_detected,
        "FORCED-STALE positive control FAILED: the crash-isolated node cached v1 and never \
         received the UPDATE, so it must be flagged stale (isolated_hash={isolated_hash:?}, \
         current={host_current:?})"
    );

    // Avoid leaking the CRDT registration into other tests sharing this process.
    freenet::dev_tool::clear_crdt_contracts();
}