freenet 0.2.42

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
//! 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
    }

    /// 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;
        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;

            // 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,
    );
}

// =============================================================================
// 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
}

// =============================================================================
// 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);
    assert!(
        leak <= 10,
        "pending_op_results leak at shutdown: {leak} entries \
         (inserts={inserts}, removes={removes}) — significant leak detected"
    );
}

/// 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()`.
///
/// 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))
        .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 fast_channel
/// (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. In simulation, RealTime-based rate limiting means fewer connections
    // complete 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
    );
}

// =============================================================================
// 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 freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation, register_crdt_contract};

    // Seed updated: per-peer acceptor reliability scoring replaces binary
    // exclusion (PR #3659), changing the CONNECT routing code path and
    // Turmoil scheduling. Previous seed: 0xC0DE_B0CA_0031 (PR #3621).
    const SEED: u64 = 0xC0DE_B0CA_0032;
    const NETWORK_NAME: &str = "get-routing-coverage";

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

    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
    );

    // 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",
        report.anomalies.len(),
        report.total_events
    );
}

/// 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)
///
/// Ignored: freenet-stdlib 0.2.2 adds the StreamChunk variant to ClientRequest,
/// changing bincode serialization sizes and altering RNG-dependent topology formation.
/// The test's fixed seed produces a different topology under 0.2.2 where node-10 cannot
/// reach caching nodes. The GET retry logic itself is unchanged and covered by
/// test_get_routing_coverage_low_htl. See PR #3488.
#[ignore]
#[test_log::test]
fn test_get_retry_with_alternatives_sparse_topology() {
    use freenet::dev_tool::{NodeLabel, ScheduledOperation, SimOperation};

    const SEED: u64 = 0xBEEF_CAFE_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;

    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,
            10, // 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
    let rt = create_runtime();
    let (successes, not_found, failures, timeouts, elapsed_ms_list) = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let mut successes = 0u64;
        let mut not_found = 0u64;
        let mut failures = 0u64;
        let mut timeouts = 0u64;
        let mut elapsed_list = Vec::new();

        for log in logs.iter() {
            match log.kind.get_outcome() {
                Some(true) => {
                    successes += 1;
                    if let Some(ms) = log.kind.get_elapsed_ms() {
                        elapsed_list.push(ms);
                    }
                }
                Some(false) => {
                    if let Some(ms) = log.kind.get_elapsed_ms() {
                        if ms >= 55_000 {
                            // Likely a timeout (close to OPERATION_TTL of 60s)
                            timeouts += 1;
                        } else {
                            not_found += 1;
                        }
                    } else {
                        failures += 1;
                    }
                }
                None => {}
            }
        }
        (successes, not_found, failures, timeouts, elapsed_list)
    });

    let total_outcomes = successes + not_found + failures + timeouts;

    // Compute latency percentiles for successful GETs
    let mut sorted_latencies = elapsed_ms_list.clone();
    sorted_latencies.sort();

    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: {} total — {} success, {} not_found, {} failures, {} timeouts",
        total_outcomes,
        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
    );
    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 events — too few for meaningful analysis",
        total_outcomes
    );
    assert!(
        success_rate >= 0.50,
        "GET success rate {:.1}% is catastrophically low (below 50%). \
         {} succeeded, {} not_found, {} failures, {} timeouts out of {} total. \
         See #3570 for context.",
        success_rate * 100.0,
        successes,
        not_found,
        failures,
        timeouts,
        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_diagnostic DONE: {:.1}% success rate, \
         {}/{} nodes have state",
        success_rate * 100.0,
        nodes_with_state,
        NUM_NODES
    );
}

/// 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
    let rt = create_runtime();
    let (successes, not_found, failures, timeouts, elapsed_ms_list) = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let mut successes = 0u64;
        let mut not_found = 0u64;
        let mut failures = 0u64;
        let mut timeouts = 0u64;
        let mut elapsed_list = Vec::new();

        for log in logs.iter() {
            match log.kind.get_outcome() {
                Some(true) => {
                    successes += 1;
                    if let Some(ms) = log.kind.get_elapsed_ms() {
                        elapsed_list.push(ms);
                    }
                }
                Some(false) => {
                    if let Some(ms) = log.kind.get_elapsed_ms() {
                        if ms >= 55_000 {
                            timeouts += 1;
                        } else {
                            not_found += 1;
                        }
                    } else {
                        failures += 1;
                    }
                }
                None => {}
            }
        }
        (successes, not_found, failures, timeouts, elapsed_list)
    });

    let total_outcomes = successes + not_found + failures + timeouts;

    let mut sorted_latencies = elapsed_ms_list.clone();
    sorted_latencies.sort();

    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: {} total — {} success, {} not_found, {} failures, {} timeouts",
        total_outcomes,
        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
    let rt = create_runtime();
    let (successes, not_found, failures, timeouts, elapsed_ms_list) = rt.block_on(async {
        let logs = logs_handle.lock().await;
        let mut successes = 0u64;
        let mut not_found = 0u64;
        let mut failures = 0u64;
        let mut timeouts = 0u64;
        let mut elapsed_list = Vec::new();

        for log in logs.iter() {
            match log.kind.get_outcome() {
                Some(true) => {
                    successes += 1;
                    if let Some(ms) = log.kind.get_elapsed_ms() {
                        elapsed_list.push(ms);
                    }
                }
                Some(false) => {
                    if let Some(ms) = log.kind.get_elapsed_ms() {
                        if ms >= 55_000 {
                            timeouts += 1;
                        } else {
                            not_found += 1;
                        }
                    } else {
                        failures += 1;
                    }
                }
                None => {}
            }
        }
        (successes, not_found, failures, timeouts, elapsed_list)
    });

    let total_outcomes = successes + not_found + failures + timeouts;

    let mut sorted_latencies = elapsed_ms_list.clone();
    sorted_latencies.sort();

    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: {} total — {} success, {} not_found, {} failures, {} timeouts",
        total_outcomes,
        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;

    let mut pre_fault_counts: Vec<usize> = (0..NODES)
        .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> = (0..NODES)
        .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> = (0..NODES)
        .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
    );
}