summaryrefslogtreecommitdiff
path: root/network/wsNetwork_test.go
blob: ef5769bcff45c196a0f51b26de1d2e5d46360062 (plain)
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
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package network

import (
	"bytes"
	"context"
	"encoding/base64"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"io"
	"math/rand"
	"net"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"regexp"
	"runtime"
	"sort"
	"strings"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"github.com/algorand/go-algorand/internal/rapidgen"
	"pgregory.net/rapid"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/algorand/go-deadlock"
	"github.com/algorand/websocket"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/crypto"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/logging/telemetryspec"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/algorand/go-algorand/util"
	"github.com/algorand/go-algorand/util/metrics"
)

const sendBufferLength = 1000

const genesisID = "go-test-network-genesis"

func TestMain(m *testing.M) {
	logging.Base().SetLevel(logging.Debug)
	os.Exit(m.Run())
}

func debugMetrics(t *testing.T) {
	if t.Failed() {
		var buf strings.Builder
		metrics.DefaultRegistry().WriteMetrics(&buf, "")
		t.Log(buf.String())
	}
}

type emptyPhonebook struct{}

func (e *emptyPhonebook) GetAddresses(n int) []string {
	return []string{}
}

func (e *emptyPhonebook) UpdateRetryAfter(addr string, retryAfter time.Time) {
}

var emptyPhonebookSingleton = &emptyPhonebook{}

type oneEntryPhonebook struct {
	addr       string
	retryAfter time.Time
}

func (e *oneEntryPhonebook) GetAddresses(n int) []string {
	return []string{e.addr}
}

func (e *oneEntryPhonebook) UpdateRetryAfter(addr string, retryAfter time.Time) {
	if e.addr == addr {
		e.retryAfter = retryAfter
	}
}

func (e *oneEntryPhonebook) GetConnectionWaitTime(addr string) (addrInPhonebook bool,
	waitTime time.Duration, provisionalTime time.Time) {
	var t time.Time
	return false, 0, t
}

func (e *oneEntryPhonebook) UpdateConnectionTime(addr string, t time.Time) bool {
	return false
}

var defaultConfig config.Local

func init() {
	defaultConfig = config.GetDefaultLocal()
	defaultConfig.Archival = false
	defaultConfig.GossipFanout = 4
	defaultConfig.NetAddress = "127.0.0.1:0"
	defaultConfig.BaseLoggerDebugLevel = uint32(logging.Debug)
	defaultConfig.DNSBootstrapID = ""
	defaultConfig.MaxConnectionsPerIP = 30
}

func makeTestWebsocketNodeWithConfig(t testing.TB, conf config.Local, opts ...testWebsocketOption) *WebsocketNetwork {
	log := logging.TestingLog(t)
	log.SetLevel(logging.Warn)
	wn := &WebsocketNetwork{
		log:       log,
		config:    conf,
		phonebook: MakePhonebook(1, 1*time.Millisecond),
		GenesisID: genesisID,
		NetworkID: config.Devtestnet,
	}
	// apply options to newly-created WebsocketNetwork, if provided
	for _, opt := range opts {
		opt.applyOpt(wn)
	}

	wn.setup()
	wn.eventualReadyDelay = time.Second
	return wn
}

// interface for providing extra options to makeTestWebsocketNode
type testWebsocketOption interface {
	applyOpt(wn *WebsocketNetwork)
}

// option to add KV to wn base logger
type testWebsocketLogNameOption struct{ logName string }

func (o testWebsocketLogNameOption) applyOpt(wn *WebsocketNetwork) {
	if o.logName != "" {
		wn.log = wn.log.With("name", o.logName)
	}
}

func makeTestWebsocketNode(t testing.TB, opts ...testWebsocketOption) *WebsocketNetwork {
	return makeTestWebsocketNodeWithConfig(t, defaultConfig, opts...)
}

type messageCounterHandler struct {
	target  int
	limit   int
	count   int
	lock    deadlock.Mutex
	done    chan struct{}
	t       testing.TB
	action  ForwardingPolicy
	verbose bool

	// For deterministically simulating slow handlers, block until test code says to go.
	release    sync.Cond
	shouldWait atomic.Int32
	waitcount  int
}

func (mch *messageCounterHandler) Handle(message IncomingMessage) OutgoingMessage {
	mch.lock.Lock()
	defer mch.lock.Unlock()
	if mch.verbose && len(message.Data) == 8 {
		now := time.Now().UnixNano()
		sent := int64(binary.LittleEndian.Uint64(message.Data))
		dnanos := now - sent
		mch.t.Logf("msg trans time %dns", dnanos)
	}
	if mch.shouldWait.Load() > 0 {
		mch.waitcount++
		mch.release.Wait()
		mch.waitcount--
	}
	mch.count++
	//mch.t.Logf("msg %d %#v", mch.count, message)
	if mch.target != 0 && mch.done != nil && mch.count >= mch.target {
		//mch.t.Log("mch target")
		close(mch.done)
		mch.done = nil
	}
	if mch.limit > 0 && mch.done != nil && mch.count > mch.limit {
		close(mch.done)
		mch.done = nil
	}
	return OutgoingMessage{Action: mch.action}
}

func (mch *messageCounterHandler) numWaiters() int {
	mch.lock.Lock()
	defer mch.lock.Unlock()
	return mch.waitcount
}
func (mch *messageCounterHandler) Count() int {
	mch.lock.Lock()
	defer mch.lock.Unlock()
	return mch.count
}
func (mch *messageCounterHandler) Signal() {
	mch.lock.Lock()
	defer mch.lock.Unlock()
	mch.release.Signal()
}
func (mch *messageCounterHandler) Broadcast() {
	mch.lock.Lock()
	defer mch.lock.Unlock()
	mch.release.Broadcast()
}

func newMessageCounter(t testing.TB, target int) *messageCounterHandler {
	return &messageCounterHandler{target: target, done: make(chan struct{}), t: t}
}

type messageMatcherHandler struct {
	lock deadlock.Mutex

	target   [][]byte
	received [][]byte
	done     chan struct{}
}

func (mmh *messageMatcherHandler) Handle(message IncomingMessage) OutgoingMessage {
	mmh.lock.Lock()
	defer mmh.lock.Unlock()

	mmh.received = append(mmh.received, message.Data)
	if len(mmh.target) > 0 && mmh.done != nil && len(mmh.received) >= len(mmh.target) {
		close(mmh.done)
		mmh.done = nil
	}

	return OutgoingMessage{Action: Ignore}
}

func (mmh *messageMatcherHandler) Match() bool {
	if len(mmh.target) != len(mmh.received) {
		return false
	}

	sort.Slice(mmh.target, func(i, j int) bool { return bytes.Compare(mmh.target[i], mmh.target[j]) == -1 })
	sort.Slice(mmh.received, func(i, j int) bool { return bytes.Compare(mmh.received[i], mmh.received[j]) == -1 })

	for i := 0; i < len(mmh.target); i++ {
		if !bytes.Equal(mmh.target[i], mmh.received[i]) {
			return false
		}
	}
	return true
}

func newMessageMatcher(t testing.TB, target [][]byte) *messageMatcherHandler {
	return &messageMatcherHandler{target: target, done: make(chan struct{})}
}

func TestWebsocketNetworkStartStop(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t)
	netA.Start()
	netA.Stop()
}

func waitReady(t testing.TB, wn *WebsocketNetwork, timeout <-chan time.Time) bool {
	select {
	case <-wn.Ready():
		return true
	case <-timeout:
		_, file, line, _ := runtime.Caller(1)
		t.Fatalf("%s:%d timeout waiting for ready", file, line)
		return false
	}
}

func netStop(t testing.TB, wn *WebsocketNetwork, name string) {
	t.Logf("stopping %s", name)
	wn.Stop()
	time.Sleep(time.Millisecond) // Stop is imperfect and some worker threads can log an error after Stop and that causes a testing error
	t.Logf("%s done", name)
}

func setupWebsocketNetworkAB(t *testing.T, countTarget int) (*WebsocketNetwork, *WebsocketNetwork, *messageCounterHandler, func()) {
	return setupWebsocketNetworkABwithLogger(t, countTarget, nil)
}
func setupWebsocketNetworkABwithLogger(t *testing.T, countTarget int, log logging.Logger) (*WebsocketNetwork, *WebsocketNetwork, *messageCounterHandler, func()) {
	success := false

	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	if log != nil {
		netA.log = log
	}
	netA.Start()
	defer func() {
		if !success {
			netStop(t, netA, "A")
		}
	}()
	netB := makeTestWebsocketNode(t)
	if log != nil {
		netB.log = log
	}
	netB.config.GossipFanout = 1
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer func() {
		if !success {
			netStop(t, netB, "B")
		}
	}()
	counter := newMessageCounter(t, countTarget)
	netB.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: counter}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	t.Log("a ready")
	waitReady(t, netB, readyTimeout.C)
	t.Log("b ready")

	success = true
	closeFunc := func() {
		netStop(t, netB, "B")
		netStop(t, netB, "A")
	}
	return netA, netB, counter, closeFunc
}

// Set up two nodes, test that a.Broadcast is received by B
func TestWebsocketNetworkBasic(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA, _, counter, closeFunc := setupWebsocketNetworkAB(t, 2)
	defer closeFunc()
	counterDone := counter.done
	netA.Broadcast(context.Background(), protocol.TxnTag, []byte("foo"), false, nil)
	netA.Broadcast(context.Background(), protocol.TxnTag, []byte("bar"), false, nil)

	select {
	case <-counterDone:
	case <-time.After(2 * time.Second):
		t.Errorf("timeout, count=%d, wanted 2", counter.count)
	}
}

type mutexBuilder struct {
	logOutput strings.Builder
	mu        deadlock.Mutex
}

func (lw *mutexBuilder) Write(p []byte) (n int, err error) {
	lw.mu.Lock()
	defer lw.mu.Unlock()
	return lw.logOutput.Write(p)
}
func (lw *mutexBuilder) String() string {
	lw.mu.Lock()
	defer lw.mu.Unlock()
	return lw.logOutput.String()
}

// Set up two nodes, test that the connection between A and B is not established.
func TestWebsocketNetworkBasicInvalidTags(t *testing.T) { // nolint:paralleltest // changes global variable defaultSendMessageTags
	partitiontest.PartitionTest(t)
	defaultSendMessageTagsOriginal := defaultSendMessageTags
	defaultSendMessageTags = map[protocol.Tag]bool{"XX": true, "MI": true}
	defer func() {
		defaultSendMessageTags = defaultSendMessageTagsOriginal
	}()
	var logOutput mutexBuilder
	log := logging.TestingLog(t)
	log.SetOutput(&logOutput)
	log.SetLevel(logging.Level(logging.Debug))
	netA, netB, counter, closeFunc := setupWebsocketNetworkABwithLogger(t, 0, log)

	defer closeFunc()
	// register a handler that should never get called, because the message will never be delivered
	netB.RegisterHandlers([]TaggedMessageHandler{
		{Tag: "XX", MessageHandler: HandlerFunc(func(msg IncomingMessage) OutgoingMessage {
			require.Fail(t, "MessageHandler for out-of-protocol tag should not be called")
			return OutgoingMessage{}
		})}})
	// send a message with an invalid tag which is in defaultSendMessageTags.
	// it should not go through because the defaultSendMessageTags should not be accepted
	// and the connection should be dropped dropped
	netA.Broadcast(context.Background(), "XX", []byte("foo"), false, nil)
	for p := 0; p < 100; p++ {
		if strings.Contains(logOutput.String(), "wsPeer handleMessageOfInterest: could not unmarshall message from") {
			break
		}
		time.Sleep(20 * time.Millisecond)
	}
	require.Contains(t, logOutput.String(), "wsPeer handleMessageOfInterest: could not unmarshall message from")
	require.Equal(t, 0, counter.count)
}

// Set up two nodes, send proposal
func TestWebsocketProposalPayloadCompression(t *testing.T) {
	partitiontest.PartitionTest(t)

	type testDef struct {
		netASupProto []string
		netAProto    string
		netBSupProto []string
		netBProto    string
	}

	var tests []testDef = []testDef{
		// two old nodes
		{[]string{"2.1"}, "2.1", []string{"2.1"}, "2.1"},

		// two new nodes with overwritten config
		{[]string{"2.2"}, "2.2", []string{"2.2"}, "2.2"},

		// old node + new node
		{[]string{"2.1"}, "2.1", []string{"2.2", "2.1"}, "2.2"},
		{[]string{"2.2", "2.1"}, "2.2", []string{"2.1"}, "2.1"},

		// combinations
		{[]string{"2.2", "2.1"}, "2.1", []string{"2.2", "2.1"}, "2.1"},
		{[]string{"2.2", "2.1"}, "2.2", []string{"2.2", "2.1"}, "2.1"},
		{[]string{"2.2", "2.1"}, "2.1", []string{"2.2", "2.1"}, "2.2"},
		{[]string{"2.2", "2.1"}, "2.2", []string{"2.2", "2.1"}, "2.2"},
	}

	for _, test := range tests {
		t.Run(fmt.Sprintf("A_%s(%s)+B_%s(%s)", test.netASupProto, test.netAProto, test.netBSupProto, test.netBProto), func(t *testing.T) {
			netA := makeTestWebsocketNode(t)
			netA.config.GossipFanout = 1
			netA.protocolVersion = test.netAProto
			netA.supportedProtocolVersions = test.netASupProto
			netA.Start()
			defer netStop(t, netA, "A")
			netB := makeTestWebsocketNode(t)
			netB.config.GossipFanout = 1
			netB.protocolVersion = test.netBProto
			netA.supportedProtocolVersions = test.netBSupProto
			addrA, postListen := netA.Address()
			require.True(t, postListen)
			t.Log(addrA)
			netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
			netB.Start()
			defer netStop(t, netB, "B")
			messages := [][]byte{
				[]byte("foo"),
				[]byte("bar"),
			}
			matcher := newMessageMatcher(t, messages)
			counterDone := matcher.done
			netB.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.ProposalPayloadTag, MessageHandler: matcher}})

			readyTimeout := time.NewTimer(2 * time.Second)
			waitReady(t, netA, readyTimeout.C)
			t.Log("a ready")
			waitReady(t, netB, readyTimeout.C)
			t.Log("b ready")

			for _, msg := range messages {
				netA.Broadcast(context.Background(), protocol.ProposalPayloadTag, msg, false, nil)
			}

			select {
			case <-counterDone:
			case <-time.After(2 * time.Second):
				t.Errorf("timeout, count=%d, wanted %d", len(matcher.received), len(messages))
			}

			require.True(t, matcher.Match())
		})
	}
}

// Repeat basic, but test a unicast
func TestWebsocketNetworkUnicast(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA, _, counter, closeFunc := setupWebsocketNetworkAB(t, 2)
	defer closeFunc()
	counterDone := counter.done

	require.Equal(t, 1, len(netA.peers))
	require.Equal(t, 1, len(netA.GetPeers(PeersConnectedIn)))
	peerB := netA.peers[0]
	err := peerB.Unicast(context.Background(), []byte("foo"), protocol.TxnTag)
	assert.NoError(t, err)
	err = peerB.Unicast(context.Background(), []byte("bar"), protocol.TxnTag)
	assert.NoError(t, err)

	select {
	case <-counterDone:
	case <-time.After(2 * time.Second):
		t.Errorf("timeout, count=%d, wanted 2", counter.count)
	}
}

// Like a basic test, but really we just want to have SetPeerData()/GetPeerData()
func TestWebsocketPeerData(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA, _, _, closeFunc := setupWebsocketNetworkAB(t, 2)
	defer closeFunc()

	require.Equal(t, 1, len(netA.peers))
	require.Equal(t, 1, len(netA.GetPeers(PeersConnectedIn)))
	peerB := netA.peers[0]

	require.Equal(t, nil, netA.GetPeerData(peerB, "not there"))
	netA.SetPeerData(peerB, "foo", "bar")
	require.Equal(t, "bar", netA.GetPeerData(peerB, "foo"))
	netA.SetPeerData(peerB, "foo", "qux")
	require.Equal(t, "qux", netA.GetPeerData(peerB, "foo"))
	netA.SetPeerData(peerB, "foo", nil)
	require.Equal(t, nil, netA.GetPeerData(peerB, "foo"))
}

// Test sending array of messages
func TestWebsocketNetworkArray(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA, _, counter, closeFunc := setupWebsocketNetworkAB(t, 3)
	defer closeFunc()
	counterDone := counter.done

	tags := []protocol.Tag{protocol.TxnTag, protocol.TxnTag, protocol.TxnTag}
	data := [][]byte{[]byte("foo"), []byte("bar"), []byte("algo")}
	netA.broadcaster.BroadcastArray(context.Background(), tags, data, false, nil)

	select {
	case <-counterDone:
	case <-time.After(2 * time.Second):
		t.Errorf("timeout, count=%d, wanted 2", counter.count)
	}
}

// Test cancelling message sends
func TestWebsocketNetworkCancel(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA, _, counter, closeFunc := setupWebsocketNetworkAB(t, 100)
	defer closeFunc()
	counterDone := counter.done

	tags := make([]protocol.Tag, 100)
	data := make([][]byte, 100)
	for i := range data {
		tags[i] = protocol.TxnTag
		data[i] = []byte(string(rune(i)))
	}

	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	// try calling BroadcastArray
	netA.broadcaster.BroadcastArray(ctx, tags, data, true, nil)

	select {
	case <-counterDone:
		t.Errorf("All messages were sent, send not cancelled")
	case <-time.After(2 * time.Second):
	}
	assert.Equal(t, 0, counter.Count())

	// try calling innerBroadcast
	request := broadcastRequest{tags: tags, data: data, enqueueTime: time.Now(), ctx: ctx}
	peers, _ := netA.peerSnapshot([]*wsPeer{})
	netA.broadcaster.innerBroadcast(request, true, peers)

	select {
	case <-counterDone:
		t.Errorf("All messages were sent, send not cancelled")
	case <-time.After(2 * time.Second):
	}
	assert.Equal(t, 0, counter.Count())

	// try calling writeLoopSend
	msgs := make([]sendMessage, 0, len(data))
	enqueueTime := time.Now()
	for i, msg := range data {
		tbytes := []byte(tags[i])
		mbytes := make([]byte, len(tbytes)+len(msg))
		copy(mbytes, tbytes)
		copy(mbytes[len(tbytes):], msg)
		msgs = append(msgs, sendMessage{data: mbytes, enqueued: time.Now(), peerEnqueued: enqueueTime, hash: crypto.Hash(mbytes), ctx: context.Background()})
	}

	msgs[50].ctx = ctx

	for _, peer := range peers {
		peer.sendBufferHighPrio <- sendMessages{msgs: msgs}
	}

	select {
	case <-counterDone:
		t.Errorf("All messages were sent, send not cancelled")
	case <-time.After(2 * time.Second):
	}
	assert.Equal(t, 50, counter.Count())
}

// Set up two nodes, test that a.Broadcast is received by B, when B has no address.
func TestWebsocketNetworkNoAddress(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.Start()
	defer netStop(t, netA, "A")

	noAddressConfig := defaultConfig
	noAddressConfig.NetAddress = ""
	// enable services even though NetAddress is not set (to assert they don't override NetAddress)
	noAddressConfig.EnableGossipService = true
	noAddressConfig.EnableBlockService = true
	noAddressConfig.EnableLedgerService = true
	netB := makeTestWebsocketNodeWithConfig(t, noAddressConfig)
	netB.config.GossipFanout = 1
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")

	// assert addrB is not listening
	addrB, postListenB := netB.Address()
	require.False(t, postListenB)
	require.Empty(t, addrB)

	counter := newMessageCounter(t, 2)
	counterDone := counter.done
	netB.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: counter}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	t.Log("a ready")
	waitReady(t, netB, readyTimeout.C)
	t.Log("b ready")

	netA.Broadcast(context.Background(), protocol.TxnTag, []byte("foo"), false, nil)
	netA.Broadcast(context.Background(), protocol.TxnTag, []byte("bar"), false, nil)

	select {
	case <-counterDone:
	case <-time.After(2 * time.Second):
		t.Errorf("timeout, count=%d, wanted 2", counter.count)
	}
}

func TestWebsocketNetworkNoGossipService(t *testing.T) {
	partitiontest.PartitionTest(t)

	config := defaultConfig
	config.EnableGossipService = false
	netA := makeTestWebsocketNodeWithConfig(t, config)
	netA.Start()
	defer netStop(t, netA, "A")

	// assert that the network was started and is listening
	addrA, postListen := netA.Address()
	require.True(t, postListen)

	// make HTTP request to gossip service and assert 404
	var resp *http.Response
	require.Eventually(t, func() bool {
		var err error
		resp, err = http.Get(fmt.Sprintf("%s/v1/%s/gossip", addrA, genesisID))
		return err == nil
	}, 2*time.Second, 100*time.Millisecond)
	require.Equal(t, http.StatusNotFound, resp.StatusCode)
}

func lineNetwork(t *testing.T, numNodes int) (nodes []*WebsocketNetwork, counters []messageCounterHandler) {
	nodes = make([]*WebsocketNetwork, numNodes)
	counters = make([]messageCounterHandler, numNodes)
	for i := range nodes {
		nodes[i] = makeTestWebsocketNode(t)
		nodes[i].log = nodes[i].log.With("node", i)
		nodes[i].config.GossipFanout = 2
		if i == 0 || i == len(nodes)-1 {
			nodes[i].config.GossipFanout = 1
		}
		if i > 0 {
			addrPrev, postListen := nodes[i-1].Address()
			require.True(t, postListen)
			nodes[i].phonebook.ReplacePeerList([]string{addrPrev}, "default", PhoneBookEntryRelayRole)
			nodes[i].RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: &counters[i]}})
		}
		nodes[i].Start()
		counters[i].t = t
		counters[i].action = Broadcast
	}
	return
}

func closeNodeWG(node *WebsocketNetwork, wg *sync.WaitGroup) {
	node.Stop()
	wg.Done()
}

func closeNodes(nodes []*WebsocketNetwork) {
	wg := sync.WaitGroup{}
	wg.Add(len(nodes))
	for _, node := range nodes {
		go closeNodeWG(node, &wg)
	}
	wg.Wait()
}

func waitNodesReady(t *testing.T, nodes []*WebsocketNetwork, timeout time.Duration) {
	tc := time.After(timeout)
	for i, node := range nodes {
		select {
		case <-node.Ready():
		case <-tc:
			t.Fatalf("node[%d] not ready at timeout", i)
		}
	}
}

const lineNetworkLength = 20
const lineNetworkNumMessages = 5

// Set up a network where each node connects to the previous; test that .Broadcast from one end gets to the other.
// Bonus! Measure how long that takes.
// TODO: also make a Benchmark version of this that reports per-node broadcast hop speed.
func TestLineNetwork(t *testing.T) {
	partitiontest.PartitionTest(t)

	nodes, counters := lineNetwork(t, lineNetworkLength)
	t.Logf("line network length: %d", lineNetworkLength)
	waitNodesReady(t, nodes, 2*time.Second)
	t.Log("ready")
	defer closeNodes(nodes)
	counter := &counters[len(counters)-1]
	counter.target = lineNetworkNumMessages
	counter.done = make(chan struct{})
	counterDone := counter.done
	counter.verbose = true
	for i := 0; i < lineNetworkNumMessages; i++ {
		sendTime := time.Now().UnixNano()
		var timeblob [8]byte
		binary.LittleEndian.PutUint64(timeblob[:], uint64(sendTime))
		nodes[0].Broadcast(context.Background(), protocol.TxnTag, timeblob[:], true, nil)
	}
	select {
	case <-counterDone:
	case <-time.After(20 * time.Second):
		t.Errorf("timeout, count=%d, wanted %d", counter.Count(), lineNetworkNumMessages)
		for ci := range counters {
			t.Errorf("count[%d]=%d", ci, counters[ci].Count())
		}
	}
	debugMetrics(t)
}

func addrtest(t *testing.T, wn *WebsocketNetwork, expected, src string) {
	actual, err := wn.addrToGossipAddr(src)
	require.NoError(t, err)
	assert.Equal(t, expected, actual)
}

func TestAddrToGossipAddr(t *testing.T) {
	partitiontest.PartitionTest(t)

	wn := &WebsocketNetwork{}
	wn.GenesisID = "test genesisID"
	wn.log = logging.Base()
	addrtest(t, wn, "ws://r7.algodev.network.:4166/v1/test%20genesisID/gossip", "r7.algodev.network.:4166")
	addrtest(t, wn, "ws://r7.algodev.network.:4166/v1/test%20genesisID/gossip", "http://r7.algodev.network.:4166")
	addrtest(t, wn, "wss://r7.algodev.network.:4166/v1/test%20genesisID/gossip", "https://r7.algodev.network.:4166")
}

type nopConn struct{}

func (nc *nopConn) RemoteAddr() net.Addr                        { return nil }
func (nc *nopConn) RemoteAddrString() string                    { return "" }
func (nc *nopConn) NextReader() (int, io.Reader, error)         { return 0, nil, nil }
func (nc *nopConn) WriteMessage(int, []byte) error              { return nil }
func (nc *nopConn) WriteControl(int, []byte, time.Time) error   { return nil }
func (nc *nopConn) CloseWithMessage([]byte, time.Time) error    { return nil }
func (nc *nopConn) SetReadLimit(limit int64)                    {}
func (nc *nopConn) CloseWithoutFlush() error                    { return nil }
func (nc *nopConn) SetPingHandler(h func(appData string) error) {}
func (nc *nopConn) SetPongHandler(h func(appData string) error) {}
func (nc *nopConn) UnderlyingConn() net.Conn                    { return nil }

var nopConnSingleton = nopConn{}

// What happens when all the read message handler threads get busy?
func TestSlowHandlers(t *testing.T) {
	partitiontest.PartitionTest(t)

	slowTag := protocol.Tag("sl")
	fastTag := protocol.Tag("fa")
	slowCounter := messageCounterHandler{}
	slowCounter.shouldWait.Store(1)
	slowCounter.release.L = &slowCounter.lock
	fastCounter := messageCounterHandler{target: incomingThreads}
	fastCounter.done = make(chan struct{})
	fastCounterDone := fastCounter.done
	slowHandler := TaggedMessageHandler{Tag: slowTag, MessageHandler: &slowCounter}
	fastHandler := TaggedMessageHandler{Tag: fastTag, MessageHandler: &fastCounter}
	node := makeTestWebsocketNode(t)
	node.RegisterHandlers([]TaggedMessageHandler{slowHandler, fastHandler})
	node.Start()
	defer node.Stop()
	injectionPeers := make([]wsPeer, incomingThreads*2)
	for i := range injectionPeers {
		injectionPeers[i].closing = make(chan struct{})
		injectionPeers[i].net = node
		injectionPeers[i].conn = &nopConnSingleton
		node.addPeer(&injectionPeers[i])
	}
	ipi := 0
	// start slow handler calls that will block all handler threads
	for i := 0; i < incomingThreads; i++ {
		data := []byte{byte(i)}
		node.handler.readBuffer <- IncomingMessage{Sender: &injectionPeers[ipi], Tag: slowTag, Data: data, Net: node}
		ipi++
	}
	defer slowCounter.Broadcast()

	// start fast handler calls that won't get to run
	for i := 0; i < incomingThreads; i++ {
		data := []byte{byte(i)}
		node.handler.readBuffer <- IncomingMessage{Sender: &injectionPeers[ipi], Tag: fastTag, Data: data, Net: node}
		ipi++
	}
	ok := false
	lastnw := -1
	totalWait := 0
	for i := 0; i < 7; i++ {
		waitTime := int(1 << uint64(i))
		time.Sleep(time.Duration(waitTime) * time.Millisecond)
		totalWait += waitTime
		nw := slowCounter.numWaiters()
		if nw == incomingThreads {
			ok = true
			break
		}
		if lastnw != nw {
			t.Logf("%dms %d waiting", totalWait, nw)
			lastnw = nw
		}
	}
	if !ok {
		t.Errorf("timeout waiting for %d threads to block on slow handler, have %d", incomingThreads, lastnw)
	}
	require.Equal(t, 0, fastCounter.Count())

	// release one slow request, all the other requests should process on that one handler thread
	slowCounter.Signal()

	select {
	case <-fastCounterDone:
	case <-time.After(time.Second):
		t.Errorf("timeout waiting for %d blocked events to be handled, have %d", incomingThreads, fastCounter.Count())
	}
	// checks that above .Signal() did in fact release just one waiting slow handler
	require.Equal(t, 1, slowCounter.Count())

	// we don't care about counting how things finish
	debugMetrics(t)
}

// one peer sends waaaayy too much slow-to-handle traffic. everything else should run fine.
func TestFloodingPeer(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Skip("flaky test")
	slowTag := protocol.Tag("sl")
	fastTag := protocol.Tag("fa")
	slowCounter := messageCounterHandler{}
	slowCounter.shouldWait.Store(1)
	slowCounter.release.L = &slowCounter.lock
	fastCounter := messageCounterHandler{}
	slowHandler := TaggedMessageHandler{Tag: slowTag, MessageHandler: &slowCounter}
	fastHandler := TaggedMessageHandler{Tag: fastTag, MessageHandler: &fastCounter}
	node := makeTestWebsocketNode(t)
	node.RegisterHandlers([]TaggedMessageHandler{slowHandler, fastHandler})
	node.Start()
	defer node.Stop()
	injectionPeers := make([]wsPeer, incomingThreads*2)
	for i := range injectionPeers {
		injectionPeers[i].closing = make(chan struct{})
		injectionPeers[i].net = node
		injectionPeers[i].conn = &nopConnSingleton
		node.addPeer(&injectionPeers[i])
	}
	ipi := 0
	const numBadPeers = 1
	// start slow handler calls that will block some threads
	ctx, cancel := context.WithCancel(context.Background())
	for i := 0; i < numBadPeers; i++ {
		myI := i
		myIpi := ipi
		go func() {
			processed := make(chan struct{}, 1)
			processed <- struct{}{}

			for qi := 0; qi < incomingThreads*2; qi++ {
				data := []byte{byte(myI), byte(qi)}
				select {
				case <-processed:
				case <-ctx.Done():
					return
				}

				select {
				case node.handler.readBuffer <- IncomingMessage{Sender: &injectionPeers[myIpi], Tag: slowTag, Data: data, Net: node, processing: processed}:
				case <-ctx.Done():
					return
				}
			}
		}()
		ipi++
	}
	defer cancel()
	defer func() {
		t.Log("release slow handlers")
		slowCounter.shouldWait.Store(0)
		slowCounter.Broadcast()
	}()

	// start fast handler calls that will run on other reader threads
	numFast := 0
	fastCounter.target = len(injectionPeers) - ipi
	fastCounter.done = make(chan struct{})
	fastCounterDone := fastCounter.done
	for ipi < len(injectionPeers) {
		data := []byte{byte(ipi)}
		node.handler.readBuffer <- IncomingMessage{Sender: &injectionPeers[ipi], Tag: fastTag, Data: data, Net: node}
		numFast++
		ipi++
	}
	require.Equal(t, numFast, fastCounter.target)
	select {
	case <-fastCounterDone:
	case <-time.After(time.Second):
		t.Errorf("timeout waiting for %d fast handlers, got %d", fastCounter.target, fastCounter.Count())
	}

	// we don't care about counting how things finish
}

func peerIsClosed(peer *wsPeer) bool {
	return peer.didInnerClose.Load() != 0
}

func avgSendBufferHighPrioLength(wn *WebsocketNetwork) float64 {
	wn.peersLock.Lock()
	defer wn.peersLock.Unlock()
	sum := 0
	for _, peer := range wn.peers {
		sum += len(peer.sendBufferHighPrio)
	}
	return float64(sum) / float64(len(wn.peers))
}

// TestSlowOutboundPeer tests what happens when one outbound peer is slow and the rest are fine. Current logic is to disconnect the one slow peer when its outbound channel is full.
//
// This is a deeply invasive test that reaches into the guts of WebsocketNetwork and wsPeer. If the implementation chainges consider throwing away or totally reimplementing this test.
func TestSlowOutboundPeer(t *testing.T) {
	partitiontest.PartitionTest(t)

	t.Skip() // todo - update this test to reflect the new implementation.
	xtag := protocol.ProposalPayloadTag
	node := makeTestWebsocketNode(t)
	destPeers := make([]wsPeer, 5)
	for i := range destPeers {
		destPeers[i].closing = make(chan struct{})
		destPeers[i].net = node
		destPeers[i].sendBufferHighPrio = make(chan sendMessages, sendBufferLength)
		destPeers[i].sendBufferBulk = make(chan sendMessages, sendBufferLength)
		destPeers[i].conn = &nopConnSingleton
		destPeers[i].rootURL = fmt.Sprintf("fake %d", i)
		node.addPeer(&destPeers[i])
	}
	node.Start()
	tctx, cf := context.WithTimeout(context.Background(), 5*time.Second)
	for i := 0; i < sendBufferLength; i++ {
		t.Logf("broadcast %d", i)
		sent := node.Broadcast(tctx, xtag, []byte{byte(i)}, true, nil)
		require.NoError(t, sent)
	}
	cf()
	ok := false
	for i := 0; i < 10; i++ {
		time.Sleep(time.Millisecond)
		aoql := avgSendBufferHighPrioLength(node)
		if aoql == sendBufferLength {
			ok = true
			break
		}
		t.Logf("node.avgOutboundQueueLength() %f", aoql)
	}
	require.True(t, ok)
	for p := range destPeers {
		if p == 0 {
			continue
		}
		for j := 0; j < sendBufferLength; j++ {
			// throw away a message as if sent
			<-destPeers[p].sendBufferHighPrio
		}
	}
	aoql := avgSendBufferHighPrioLength(node)
	if aoql > (sendBufferLength / 2) {
		t.Fatalf("avgOutboundQueueLength=%f wanted <%f", aoql, sendBufferLength/2.0)
		return
	}
	// it shouldn't have closed for just sitting on the limit of full
	require.False(t, peerIsClosed(&destPeers[0]))

	// function context just to contain defer cf()
	func() {
		timeout, cf := context.WithTimeout(context.Background(), time.Second)
		defer cf()
		sent := node.Broadcast(timeout, xtag, []byte{byte(42)}, true, nil)
		assert.NoError(t, sent)
	}()

	// and now with the rest of the peers well and this one slow, we closed the slow one
	require.True(t, peerIsClosed(&destPeers[0]))
}

func makeTestFilterWebsocketNode(t *testing.T, nodename string) *WebsocketNetwork {
	dc := defaultConfig
	dc.EnableIncomingMessageFilter = true
	dc.EnableOutgoingNetworkMessageFiltering = true
	dc.IncomingMessageFilterBucketCount = 5
	dc.IncomingMessageFilterBucketSize = 512
	dc.OutgoingMessageFilterBucketCount = 3
	dc.OutgoingMessageFilterBucketSize = 128
	wn := &WebsocketNetwork{
		log:       logging.TestingLog(t).With("node", nodename),
		config:    dc,
		phonebook: MakePhonebook(1, 1*time.Millisecond),
		GenesisID: genesisID,
		NetworkID: config.Devtestnet,
	}
	require.True(t, wn.config.EnableIncomingMessageFilter)
	wn.setup()
	wn.eventualReadyDelay = time.Second
	require.True(t, wn.config.EnableIncomingMessageFilter)
	return wn
}

func TestDupFilter(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestFilterWebsocketNode(t, "a")
	netA.config.GossipFanout = 1
	netA.Start()
	defer netStop(t, netA, "A")
	netB := makeTestFilterWebsocketNode(t, "b")
	netB.config.GossipFanout = 2
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")
	counter := &messageCounterHandler{t: t, limit: 1, done: make(chan struct{})}
	netB.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.AgreementVoteTag, MessageHandler: counter}})
	debugTag2 := protocol.ProposalPayloadTag
	counter2 := &messageCounterHandler{t: t, limit: 1, done: make(chan struct{})}
	netB.RegisterHandlers([]TaggedMessageHandler{{Tag: debugTag2, MessageHandler: counter2}})

	addrB, postListen := netB.Address()
	require.True(t, postListen)
	netC := makeTestFilterWebsocketNode(t, "c")
	netC.config.GossipFanout = 1
	netC.phonebook.ReplacePeerList([]string{addrB}, "default", PhoneBookEntryRelayRole)
	netC.Start()
	defer netC.Stop()

	makeMsg := func(n int) []byte {
		// We cannot harcode the msgSize to messageFilterSize + 1 because max allowed AV message is smaller  than that.
		// We also cannot use maxSize for PP since it's a compressible tag but trying to compress random data will expand it.
		if messageFilterSize+1 < n {
			n = messageFilterSize + 1
		}
		msg := make([]byte, n)
		rand.Read(msg)
		return msg
	}

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	t.Log("a ready")
	waitReady(t, netB, readyTimeout.C)
	t.Log("b ready")
	waitReady(t, netC, readyTimeout.C)
	t.Log("c ready")

	// TODO: this test has two halves that exercise inbound de-dup and outbound non-send due to received hash. But it doesn't properly _test_ them as it doesn't measure _why_ it receives each message exactly once. The second half below could actually be because of the same inbound de-dup as this first half. You can see the actions of either in metrics.
	// algod_network_duplicate_message_received_total{} 2
	// algod_outgoing_network_message_filtered_out_total{} 2
	// Maybe we should just .Set(0) those counters and use them in this test?

	// This exercise inbound dup detection.
	avMsg := makeMsg(int(protocol.AgreementVoteTag.MaxMessageSize()))
	netA.Broadcast(context.Background(), protocol.AgreementVoteTag, avMsg, true, nil)
	netA.Broadcast(context.Background(), protocol.AgreementVoteTag, avMsg, true, nil)
	netA.Broadcast(context.Background(), protocol.AgreementVoteTag, avMsg, true, nil)
	t.Log("A dup send done")

	select {
	case <-counter.done:
		// probably a failure, but let it fall through to the equal check
	case <-time.After(time.Second):
	}
	counter.lock.Lock()
	assert.Equal(t, 1, counter.count)
	counter.lock.Unlock()

	// new message
	debugTag2Msg := makeMsg(int(debugTag2.MaxMessageSize()))
	t.Logf("debugTag2Msg len %d", len(debugTag2Msg))
	t.Log("A send, C non-dup-send")
	netA.Broadcast(context.Background(), debugTag2, debugTag2Msg, true, nil)
	// B should broadcast its non-desire to receive the message again
	time.Sleep(500 * time.Millisecond)

	// C should now not send these
	netC.Broadcast(context.Background(), debugTag2, debugTag2Msg, true, nil)
	netC.Broadcast(context.Background(), debugTag2, debugTag2Msg, true, nil)

	select {
	case <-counter2.done:
		// probably a failure, but let it fall through to the equal check
	case <-time.After(time.Second):
	}
	assert.Equal(t, 1, counter2.count)

	debugMetrics(t)
}

func TestGetPeers(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.Start()
	defer netA.Stop()
	netB := makeTestWebsocketNode(t)
	netB.config.GossipFanout = 1
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	phbMulti := MakePhonebook(1, 1*time.Millisecond)
	phbMulti.ReplacePeerList([]string{addrA}, "phba", PhoneBookEntryRelayRole)
	netB.phonebook = phbMulti
	netB.Start()
	defer netB.Stop()

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	t.Log("a ready")
	waitReady(t, netB, readyTimeout.C)
	t.Log("b ready")

	phbMulti.ReplacePeerList([]string{"a", "b", "c"}, "ph", PhoneBookEntryRelayRole)

	//addrB, _ := netB.Address()

	// A has only an inbound connection from B
	aPeers := netA.GetPeers(PeersConnectedOut)
	assert.Equal(t, 0, len(aPeers))

	// B's connection to A is outgoing
	bPeers := netB.GetPeers(PeersConnectedOut)
	assert.Equal(t, 1, len(bPeers))
	assert.Equal(t, addrA, bPeers[0].(HTTPPeer).GetAddress())

	// B also knows about other peers not connected to
	bPeers = netB.GetPeers(PeersPhonebookRelays)
	assert.Equal(t, 4, len(bPeers))
	peerAddrs := make([]string, len(bPeers))
	for pi, peer := range bPeers {
		peerAddrs[pi] = peer.(HTTPPeer).GetAddress()
	}
	sort.Strings(peerAddrs)
	expectAddrs := []string{addrA, "a", "b", "c"}
	sort.Strings(expectAddrs)
	assert.Equal(t, expectAddrs, peerAddrs)

	// For now, PeersPhonebookArchivalNodes and PeersPhonebookRelays will return the same set of nodes
	bPeers2 := netB.GetPeers(PeersPhonebookArchivalNodes)
	peerAddrs2 := make([]string, len(bPeers2))
	for pi2, peer2 := range bPeers2 {
		peerAddrs2[pi2] = peer2.(HTTPPeer).GetAddress()
	}
	sort.Strings(peerAddrs2)
	assert.Equal(t, expectAddrs, peerAddrs2)

}

// confirms that if the config PublicAddress is set to "testing",
// PublicAddress is loaded when possible with the value of Address()
func TestTestingPublicAddress(t *testing.T) {
	partitiontest.PartitionTest(t)
	t.Parallel()

	netA := makeTestWebsocketNode(t)
	netA.config.PublicAddress = "testing"
	netA.config.GossipFanout = 1

	netA.Start()

	time.Sleep(100 * time.Millisecond)

	// check that "testing" has been overloaded
	addr, ok := netA.Address()
	addr = hostAndPort(addr)
	require.True(t, ok)
	require.NotEqual(t, "testing", netA.PublicAddress())
	require.Equal(t, addr, netA.PublicAddress())
}

// mock an identityTracker
type mockIdentityTracker struct {
	isOccupied  bool
	setCount    int
	insertCount int
	removeCount int
	lock        deadlock.Mutex
	realTracker identityTracker
}

func newMockIdentityTracker(realTracker identityTracker) *mockIdentityTracker {
	return &mockIdentityTracker{
		isOccupied:  false,
		setCount:    0,
		insertCount: 0,
		removeCount: 0,
		realTracker: realTracker,
	}
}

func (d *mockIdentityTracker) setIsOccupied(b bool) {
	d.lock.Lock()
	defer d.lock.Unlock()
	d.isOccupied = b
}
func (d *mockIdentityTracker) removeIdentity(p *wsPeer) {
	d.lock.Lock()
	defer d.lock.Unlock()
	d.removeCount++
	d.realTracker.removeIdentity(p)
}
func (d *mockIdentityTracker) getInsertCount() int {
	d.lock.Lock()
	defer d.lock.Unlock()
	return d.insertCount
}
func (d *mockIdentityTracker) getRemoveCount() int {
	d.lock.Lock()
	defer d.lock.Unlock()
	return d.removeCount
}
func (d *mockIdentityTracker) getSetCount() int {
	d.lock.Lock()
	defer d.lock.Unlock()
	return d.setCount
}
func (d *mockIdentityTracker) setIdentity(p *wsPeer) bool {
	d.lock.Lock()
	defer d.lock.Unlock()
	d.setCount++
	// isOccupied is true, meaning we're overloading the "ok" return to false
	if d.isOccupied {
		return false
	}
	ret := d.realTracker.setIdentity(p)
	if ret {
		d.insertCount++
	}
	return ret
}

func hostAndPort(u string) string {
	url, err := url.Parse(u)
	if err == nil {
		return fmt.Sprintf("%s:%s", url.Hostname(), url.Port())
	}
	return ""
}

// TestPeeringWithIdentityChallenge tests the happy path of connecting with identity challenge:
// - both peers have correctly set PublicAddress
// - both should exchange identities and verify
// - both peers should be able to deduplicate connections
func TestPeeringWithIdentityChallenge(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
	netA.identityTracker = newMockIdentityTracker(netA.identityTracker)
	netA.config.PublicAddress = "testing"
	netA.config.GossipFanout = 1

	netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
	netB.identityTracker = newMockIdentityTracker(netB.identityTracker)
	netB.config.PublicAddress = "testing"
	netB.config.GossipFanout = 1

	netA.Start()
	defer netA.Stop()
	netB.Start()
	defer netB.Stop()

	addrA, ok := netA.Address()
	require.True(t, ok)

	addrB, ok := netB.Address()
	require.True(t, ok)
	gossipB, err := netB.addrToGossipAddr(addrB)
	require.NoError(t, err)

	// set addresses to just host:port to match phonebook/dns format
	addrA = hostAndPort(addrA)
	addrB = hostAndPort(addrB)

	// first connection should work just fine
	if _, ok := netA.tryConnectReserveAddr(addrB); ok {
		netA.wg.Add(1)
		netA.tryConnect(addrB, gossipB)
		// let the tryConnect go forward
		assert.Eventually(t, func() bool {
			return len(netA.GetPeers(PeersConnectedOut)) == 1
		}, time.Second, 50*time.Millisecond)
	}
	// just one A->B connection
	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedOut)))

	// confirm identity map was added to for both hosts
	assert.Equal(t, 1, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 1, netA.identityTracker.(*mockIdentityTracker).getInsertCount())

	// netB has to wait for a final verification message over WS Handler, so pause a moment
	assert.Eventually(t, func() bool {
		return netB.identityTracker.(*mockIdentityTracker).getSetCount() == 1
	}, time.Second, 50*time.Millisecond)

	assert.Equal(t, 1, netB.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 1, netB.identityTracker.(*mockIdentityTracker).getInsertCount())

	// bi-directional connection from B should not proceed
	_, ok = netB.tryConnectReserveAddr(addrA)
	assert.False(t, ok)

	// still just one A->B connection
	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedOut)))
	// netA never attempts to set identity as it never sees a verified identity
	assert.Equal(t, 1, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	// no connecton => netB does attepmt to add the identity to the tracker
	// and it would not end up being added
	assert.Equal(t, 1, netB.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 1, netB.identityTracker.(*mockIdentityTracker).getInsertCount())

	// Check deduplication again, this time from A
	// the "ok" from tryConnectReserveAddr is overloaded here because isConnectedTo
	// will prevent this connection from attempting in the first place
	// in the real world, that isConnectedTo doesn't always trigger, if the hosts are behind
	// a load balancer or other NAT
	_, ok = netA.tryConnectReserveAddr(addrB)
	assert.False(t, ok)
	netA.wg.Add(1)
	old := networkPeerIdentityDisconnect.GetUint64Value()
	netA.tryConnect(addrB, gossipB)
	// let the tryConnect go forward
	assert.Eventually(t, func() bool {
		new := networkPeerIdentityDisconnect.GetUint64Value()
		return new > old
	}, time.Second, 50*time.Millisecond)

	// netB never tries to add a new identity, since the connection gets abandoned before it is verified
	assert.Equal(t, 1, netB.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 1, netB.identityTracker.(*mockIdentityTracker).getInsertCount())
	// still just one A->B connection
	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 2, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 1, netA.identityTracker.(*mockIdentityTracker).getInsertCount())
	// it is possible for NetB to be in the process of doing addPeer while
	// the underlying connection is being closed. In this case, the read loop
	// on the peer will detect and close the peer. Since this is asynchronous,
	// we wait and check regularly to allow the connection to settle
	assert.Eventually(t, func() bool {
		return len(netB.GetPeers(PeersConnectedIn)) == 1
	}, time.Second, 50*time.Millisecond)

	// Now have A connect to node C, which has the same PublicAddress as B (e.g., because it shares the
	// same public load balancer endpoint). C will have a different identity keypair and so will not be
	// considered a duplicate.
	netC := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netC"})
	netC.identityTracker = newMockIdentityTracker(netC.identityTracker)
	netC.config.PublicAddress = addrB
	netC.config.GossipFanout = 1

	netC.Start()
	defer netC.Stop()

	addrC, ok := netC.Address()
	require.True(t, ok)
	gossipC, err := netC.addrToGossipAddr(addrC)
	require.NoError(t, err)

	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	// A connects to C (but uses addrB here to simulate case where B & C have the same PublicAddress)
	netA.wg.Add(1)
	netA.tryConnect(addrB, gossipC)
	// let the tryConnect go forward
	assert.Eventually(t, func() bool {
		return len(netA.GetPeers(PeersConnectedOut)) == 2
	}, time.Second, 50*time.Millisecond)

	// A->B and A->C both open
	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 2, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netC.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedOut)))

	// confirm identity map was added to for both hosts
	assert.Equal(t, 3, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 2, netA.identityTracker.(*mockIdentityTracker).getInsertCount())

	// netC has to wait for a final verification message over WS Handler, so pause a moment
	assert.Eventually(t, func() bool {
		return netC.identityTracker.(*mockIdentityTracker).getSetCount() == 1
	}, time.Second, 50*time.Millisecond)

	assert.Equal(t, 1, netC.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 1, netC.identityTracker.(*mockIdentityTracker).getInsertCount())

}

// TestPeeringSenderIdentityChallengeOnly will confirm that if only the Sender
// Uses Identity, no identity exchange happens in the connection
func TestPeeringSenderIdentityChallengeOnly(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
	netA.identityTracker = newMockIdentityTracker(netA.identityTracker)
	netA.config.PublicAddress = "testing"
	netA.config.GossipFanout = 1

	netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
	netB.identityTracker = newMockIdentityTracker(netB.identityTracker)
	//netB.config.PublicAddress = "testing"
	netB.config.GossipFanout = 1

	netA.Start()
	defer netA.Stop()
	netB.Start()
	defer netB.Stop()

	addrA, ok := netA.Address()
	require.True(t, ok)

	addrB, ok := netB.Address()
	require.True(t, ok)
	gossipB, err := netB.addrToGossipAddr(addrB)
	require.NoError(t, err)

	// set addresses to just host:port to match phonebook/dns format
	addrA = hostAndPort(addrA)
	addrB = hostAndPort(addrB)

	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedIn)))

	// first connection should work just fine
	if _, ok := netA.tryConnectReserveAddr(addrB); ok {
		netA.wg.Add(1)
		netA.tryConnect(addrB, gossipB)
		assert.Eventually(t, func() bool {
			return len(netA.GetPeers(PeersConnectedOut)) == 1
		}, time.Second, 50*time.Millisecond)
	}
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))

	// confirm identity map was not added to for either host
	assert.Equal(t, 0, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 0, netB.identityTracker.(*mockIdentityTracker).getSetCount())

	// bi-directional connection does not work because netA advertises its public address
	_, ok = netB.tryConnectReserveAddr(addrA)
	assert.False(t, ok)

	// no redundant connections
	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedOut)))
	// confirm identity map was not added to for either host
	assert.Equal(t, 0, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 0, netB.identityTracker.(*mockIdentityTracker).getSetCount())
}

// TestPeeringReceiverIdentityChallengeOnly will confirm that if only the Receiver
// Uses Identity, no identity exchange happens in the connection
func TestPeeringReceiverIdentityChallengeOnly(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
	netA.identityTracker = newMockIdentityTracker(netA.identityTracker)
	//netA.config.PublicAddress = "testing"
	netA.config.GossipFanout = 1

	netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
	netB.identityTracker = newMockIdentityTracker(netB.identityTracker)
	netB.config.PublicAddress = "testing"
	netB.config.GossipFanout = 1

	netA.Start()
	defer netA.Stop()
	netB.Start()
	defer netB.Stop()

	addrA, ok := netA.Address()
	require.True(t, ok)
	gossipA, err := netA.addrToGossipAddr(addrA)
	require.NoError(t, err)

	addrB, ok := netB.Address()
	require.True(t, ok)
	gossipB, err := netB.addrToGossipAddr(addrB)
	require.NoError(t, err)

	// set addresses to just host:port to match phonebook/dns format
	addrA = hostAndPort(addrA)
	addrB = hostAndPort(addrB)

	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedOut)))
	// first connection should work just fine
	if _, ok := netA.tryConnectReserveAddr(addrB); ok {
		netA.wg.Add(1)
		netA.tryConnect(addrB, gossipB)
		// let the tryConnect go forward
		assert.Eventually(t, func() bool {
			return len(netA.GetPeers(PeersConnectedOut)) == 1
		}, time.Second, 50*time.Millisecond)
	}
	// single A->B connection
	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedOut)))

	// confirm identity map was not added to for either host
	assert.Equal(t, 0, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 0, netB.identityTracker.(*mockIdentityTracker).getSetCount())

	// bi-directional connection should also work
	if _, ok := netB.tryConnectReserveAddr(addrA); ok {
		netB.wg.Add(1)
		netB.tryConnect(addrA, gossipA)
		// let the tryConnect go forward
		assert.Eventually(t, func() bool {
			return len(netB.GetPeers(PeersConnectedOut)) == 1
		}, time.Second, 50*time.Millisecond)
	}
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedOut)))
	// confirm identity map was not added to for either host
	assert.Equal(t, 0, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 0, netB.identityTracker.(*mockIdentityTracker).getSetCount())
}

// TestPeeringIncorrectDeduplicationName confirm that if the reciever can't match
// the Address in the challenge to its PublicAddress, identities aren't exchanged, but peering continues
func TestPeeringIncorrectDeduplicationName(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
	netA.identityTracker = newMockIdentityTracker(netA.identityTracker)
	netA.config.PublicAddress = "testing"
	netA.config.GossipFanout = 1

	netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
	netB.identityTracker = newMockIdentityTracker(netB.identityTracker)
	netB.config.PublicAddress = "no:3333"
	netB.config.GossipFanout = 1

	netA.Start()
	defer netA.Stop()
	netB.Start()
	defer netB.Stop()

	addrA, ok := netA.Address()
	require.True(t, ok)
	gossipA, err := netA.addrToGossipAddr(addrA)
	require.NoError(t, err)

	addrB, ok := netB.Address()
	require.True(t, ok)
	gossipB, err := netB.addrToGossipAddr(addrB)
	require.NoError(t, err)

	// set addresses to just host:port to match phonebook/dns format
	addrA = hostAndPort(addrA)
	addrB = hostAndPort(addrB)

	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedOut)))
	// first connection should work just fine
	if _, ok := netA.tryConnectReserveAddr(addrB); ok {
		netA.wg.Add(1)
		netA.tryConnect(addrB, gossipB)
		// let the tryConnect go forward
		assert.Eventually(t, func() bool {
			return len(netA.GetPeers(PeersConnectedOut)) == 1
		}, time.Second, 50*time.Millisecond)
	}
	// single A->B connection
	assert.Equal(t, 0, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 0, len(netB.GetPeers(PeersConnectedOut)))

	// confirm identity map was not added to for either host
	// nor was "set" called at all
	assert.Equal(t, 0, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 0, netB.identityTracker.(*mockIdentityTracker).getSetCount())

	// bi-directional connection would now work since netB detects to be connected to netA in tryConnectReserveAddr,
	// so force it.
	// this second connection should set identities, because the reciever address matches now
	_, ok = netB.tryConnectReserveAddr(addrA)
	assert.False(t, ok)
	netB.wg.Add(1)
	netB.tryConnect(addrA, gossipA)
	// let the tryConnect go forward
	assert.Eventually(t, func() bool {
		return len(netB.GetPeers(PeersConnectedOut)) == 1
	}, time.Second, 50*time.Millisecond)

	// confirm that at this point the identityTracker was called once per network
	//	and inserted once per network
	assert.Equal(t, 1, netA.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 1, netB.identityTracker.(*mockIdentityTracker).getSetCount())
	assert.Equal(t, 1, netA.identityTracker.(*mockIdentityTracker).getInsertCount())
	assert.Equal(t, 1, netB.identityTracker.(*mockIdentityTracker).getInsertCount())
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netA.GetPeers(PeersConnectedOut)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedIn)))
	assert.Equal(t, 1, len(netB.GetPeers(PeersConnectedOut)))
}

// make a mockIdentityScheme which can accept overloaded behavior
// use this over the next few tests to check that when one peer misbehaves, peering continues/halts as expected
type mockIdentityScheme struct {
	t                       *testing.T
	realScheme              *identityChallengePublicKeyScheme
	attachChallenge         func(attach http.Header, addr string) identityChallengeValue
	verifyAndAttachResponse func(attach http.Header, h http.Header) (identityChallengeValue, crypto.PublicKey, error)
	verifyResponse          func(t *testing.T, h http.Header, c identityChallengeValue) (crypto.PublicKey, []byte, error)
}

func newMockIdentityScheme(t *testing.T) *mockIdentityScheme {
	return &mockIdentityScheme{t: t, realScheme: NewIdentityChallengeScheme("any")}
}
func (i mockIdentityScheme) AttachChallenge(attach http.Header, addr string) identityChallengeValue {
	if i.attachChallenge != nil {
		return i.attachChallenge(attach, addr)
	}
	return i.realScheme.AttachChallenge(attach, addr)
}
func (i mockIdentityScheme) VerifyRequestAndAttachResponse(attach http.Header, h http.Header) (identityChallengeValue, crypto.PublicKey, error) {
	if i.verifyAndAttachResponse != nil {
		return i.verifyAndAttachResponse(attach, h)
	}
	return i.realScheme.VerifyRequestAndAttachResponse(attach, h)
}
func (i mockIdentityScheme) VerifyResponse(h http.Header, c identityChallengeValue) (crypto.PublicKey, []byte, error) {
	if i.verifyResponse != nil {
		return i.verifyResponse(i.t, h, c)
	}
	return i.realScheme.VerifyResponse(h, c)
}

// when the identity challenge is misconstructed in various ways, peering should behave as expected
func TestPeeringWithBadIdentityChallenge(t *testing.T) {
	partitiontest.PartitionTest(t)

	type testCase struct {
		name            string
		attachChallenge func(attach http.Header, addr string) identityChallengeValue
		totalInA        int
		totalOutA       int
		totalInB        int
		totalOutB       int
	}

	testCases := []testCase{
		// when identityChallenge is not included, peering continues as normal
		{
			name:            "not included",
			attachChallenge: func(attach http.Header, addr string) identityChallengeValue { return identityChallengeValue{} },
			totalInA:        0,
			totalOutA:       1,
			totalInB:        1,
			totalOutB:       0,
		},
		// when the identityChallenge is malformed B64, peering halts
		{
			name: "malformed b64",
			attachChallenge: func(attach http.Header, addr string) identityChallengeValue {
				attach.Add(IdentityChallengeHeader, "this does not decode!")
				return newIdentityChallengeValue()
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
		// when the identityChallenge can't be unmarshalled, peering halts
		{
			name: "not msgp decodable",
			attachChallenge: func(attach http.Header, addr string) identityChallengeValue {
				attach.Add(IdentityChallengeHeader, base64.StdEncoding.EncodeToString([]byte("Bad!Data!")))
				return newIdentityChallengeValue()
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
		// when the incorrect address is used, peering continues
		{
			name: "incorrect address",
			attachChallenge: func(attach http.Header, addr string) identityChallengeValue {
				s := NewIdentityChallengeScheme("does not matter") // make a scheme to use its keys
				c := identityChallenge{
					Key:           s.identityKeys.SignatureVerifier,
					Challenge:     newIdentityChallengeValue(),
					PublicAddress: []byte("incorrect address!"),
				}
				attach.Add(IdentityChallengeHeader, c.signAndEncodeB64(s.identityKeys))
				return c.Challenge
			},
			totalInA:  0,
			totalOutA: 1,
			totalInB:  1,
			totalOutB: 0,
		},
		// when the challenge is incorrectly signed, peering halts
		{
			name: "bad signature",
			attachChallenge: func(attach http.Header, addr string) identityChallengeValue {
				s := NewIdentityChallengeScheme("does not matter") // make a scheme to use its keys
				c := identityChallenge{
					Key:           s.identityKeys.SignatureVerifier,
					Challenge:     newIdentityChallengeValue(),
					PublicAddress: []byte("incorrect address!"),
				}.Sign(s.identityKeys)
				c.Msg.Challenge = newIdentityChallengeValue() // change the challenge after signing the message, so the signature check fails
				enc := protocol.Encode(&c)
				b64enc := base64.StdEncoding.EncodeToString(enc)
				attach.Add(IdentityChallengeHeader, b64enc)
				return c.Msg.Challenge
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
	}

	for _, tc := range testCases {
		t.Logf("Running Peering with Identity Challenge Test: %s", tc.name)
		netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
		netA.identityTracker = newMockIdentityTracker(netA.identityTracker)
		netA.config.PublicAddress = "testing"
		netA.config.GossipFanout = 1

		scheme := newMockIdentityScheme(t)
		scheme.attachChallenge = tc.attachChallenge
		netA.identityScheme = scheme

		netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
		netB.identityTracker = newMockIdentityTracker(netB.identityTracker)
		netB.config.PublicAddress = "testing"
		netB.config.GossipFanout = 1

		netA.Start()
		defer netA.Stop()
		netB.Start()
		defer netB.Stop()

		addrB, ok := netB.Address()
		require.True(t, ok)
		gossipB, err := netB.addrToGossipAddr(addrB)
		require.NoError(t, err)

		// set addresses to just host:port to match phonebook/dns format
		addrB = hostAndPort(addrB)

		if _, ok := netA.tryConnectReserveAddr(addrB); ok {
			netA.wg.Add(1)
			netA.tryConnect(addrB, gossipB)
			// let the tryConnect go forward
			time.Sleep(250 * time.Millisecond)
		}
		assert.Equal(t, tc.totalInA, len(netA.GetPeers(PeersConnectedIn)))
		assert.Equal(t, tc.totalOutA, len(netA.GetPeers(PeersConnectedOut)))
		assert.Equal(t, tc.totalInB, len(netB.GetPeers(PeersConnectedIn)))
		assert.Equal(t, tc.totalOutB, len(netB.GetPeers(PeersConnectedOut)))
	}

}

// when the identity challenge response is misconstructed in various way, confirm peering behaves as expected
func TestPeeringWithBadIdentityChallengeResponse(t *testing.T) {
	partitiontest.PartitionTest(t)

	type testCase struct {
		name                    string
		verifyAndAttachResponse func(attach http.Header, h http.Header) (identityChallengeValue, crypto.PublicKey, error)
		totalInA                int
		totalOutA               int
		totalInB                int
		totalOutB               int
	}

	testCases := []testCase{
		// when there is no response to the identity challenge, peering should continue without ID
		{
			name: "not included",
			verifyAndAttachResponse: func(attach http.Header, h http.Header) (identityChallengeValue, crypto.PublicKey, error) {
				return identityChallengeValue{}, crypto.PublicKey{}, nil
			},
			totalInA:  0,
			totalOutA: 1,
			totalInB:  1,
			totalOutB: 0,
		},
		// when the response is malformed, do not peer
		{
			name: "malformed b64",
			verifyAndAttachResponse: func(attach http.Header, h http.Header) (identityChallengeValue, crypto.PublicKey, error) {
				attach.Add(IdentityChallengeHeader, "this does not decode!")
				return identityChallengeValue{}, crypto.PublicKey{}, nil
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
		// when the response is malformed, do not peer
		{
			name: "not msgp decodable",
			verifyAndAttachResponse: func(attach http.Header, h http.Header) (identityChallengeValue, crypto.PublicKey, error) {
				attach.Add(IdentityChallengeHeader, base64.StdEncoding.EncodeToString([]byte("Bad!Data!")))
				return identityChallengeValue{}, crypto.PublicKey{}, nil
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
		// when the original challenge isn't included, do not peer
		{
			name: "incorrect original challenge",
			verifyAndAttachResponse: func(attach http.Header, h http.Header) (identityChallengeValue, crypto.PublicKey, error) {
				s := NewIdentityChallengeScheme("does not matter") // make a scheme to use its keys
				// decode the header to an identityChallenge
				msg, _ := base64.StdEncoding.DecodeString(h.Get(IdentityChallengeHeader))
				idChal := identityChallenge{}
				protocol.Decode(msg, &idChal)
				// make the response object, with an incorrect challenge encode it and attach it to the header
				r := identityChallengeResponse{
					Key:               s.identityKeys.SignatureVerifier,
					Challenge:         newIdentityChallengeValue(),
					ResponseChallenge: newIdentityChallengeValue(),
				}
				attach.Add(IdentityChallengeHeader, r.signAndEncodeB64(s.identityKeys))
				return r.ResponseChallenge, idChal.Key, nil
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
		// when the message is incorrectly signed, do not peer
		{
			name: "bad signature",
			verifyAndAttachResponse: func(attach http.Header, h http.Header) (identityChallengeValue, crypto.PublicKey, error) {
				s := NewIdentityChallengeScheme("does not matter") // make a scheme to use its keys
				// decode the header to an identityChallenge
				msg, _ := base64.StdEncoding.DecodeString(h.Get(IdentityChallengeHeader))
				idChal := identityChallenge{}
				protocol.Decode(msg, &idChal)
				// make the response object, then change the signature and encode and attach
				r := identityChallengeResponse{
					Key:               s.identityKeys.SignatureVerifier,
					Challenge:         newIdentityChallengeValue(),
					ResponseChallenge: newIdentityChallengeValue(),
				}.Sign(s.identityKeys)
				r.Msg.ResponseChallenge = newIdentityChallengeValue() // change the challenge after signing the message
				enc := protocol.Encode(&r)
				b64enc := base64.StdEncoding.EncodeToString(enc)
				attach.Add(IdentityChallengeHeader, b64enc)
				return r.Msg.ResponseChallenge, idChal.Key, nil
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
	}

	for _, tc := range testCases {
		t.Logf("Running Peering with Identity Challenge Response Test: %s", tc.name)
		netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
		netA.identityTracker = newMockIdentityTracker(netA.identityTracker)
		netA.config.PublicAddress = "testing"
		netA.config.GossipFanout = 1

		netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
		netB.identityTracker = newMockIdentityTracker(netB.identityTracker)
		netB.config.PublicAddress = "testing"
		netB.config.GossipFanout = 1

		scheme := newMockIdentityScheme(t)
		scheme.verifyAndAttachResponse = tc.verifyAndAttachResponse
		netB.identityScheme = scheme

		netA.Start()
		defer netA.Stop()
		netB.Start()
		defer netB.Stop()

		addrB, ok := netB.Address()
		require.True(t, ok)
		gossipB, err := netB.addrToGossipAddr(addrB)
		require.NoError(t, err)

		// set addresses to just host:port to match phonebook/dns format
		addrB = hostAndPort(addrB)

		if _, ok := netA.tryConnectReserveAddr(addrB); ok {
			netA.wg.Add(1)
			netA.tryConnect(addrB, gossipB)
			// let the tryConnect go forward
			time.Sleep(250 * time.Millisecond)
		}
		assert.Equal(t, tc.totalInA, len(netA.GetPeers(PeersConnectedIn)))
		assert.Equal(t, tc.totalOutA, len(netA.GetPeers(PeersConnectedOut)))
		assert.Equal(t, tc.totalOutB, len(netB.GetPeers(PeersConnectedOut)))
		// it is possible for NetB to be in the process of doing addPeer while
		// the underlying connection is being closed. In this case, the read loop
		// on the peer will detect and close the peer. Since this is asynchronous,
		// we wait and check regularly to allow the connection to settle
		assert.Eventually(
			t,
			func() bool { return len(netB.GetPeers(PeersConnectedIn)) == tc.totalInB },
			5*time.Second,
			100*time.Millisecond)
	}

}

// when the identity challenge verification is misconstructed in various ways, peering should behave as expected
func TestPeeringWithBadIdentityVerification(t *testing.T) {
	partitiontest.PartitionTest(t)

	type testCase struct {
		name           string
		verifyResponse func(t *testing.T, h http.Header, c identityChallengeValue) (crypto.PublicKey, []byte, error)
		totalInA       int
		totalOutA      int
		totalInB       int
		totalOutB      int
		occupied       bool
	}

	testCases := []testCase{
		// in a totally unmodified scenario, the two peers stay connected even after the verification timeout
		{
			name:      "happy path",
			totalInA:  0,
			totalOutA: 1,
			totalInB:  1,
			totalOutB: 0,
		},
		// if the peer does not send a final message, the peers stay connected
		{
			name: "not included",
			verifyResponse: func(t *testing.T, h http.Header, c identityChallengeValue) (crypto.PublicKey, []byte, error) {
				return crypto.PublicKey{}, []byte{}, nil
			},
			totalInA:  0,
			totalOutA: 1,
			totalInB:  1,
			totalOutB: 0,
		},
		// when the identityVerification can't be unmarshalled, peer is disconnected
		{
			name: "not msgp decodable",
			verifyResponse: func(t *testing.T, h http.Header, c identityChallengeValue) (crypto.PublicKey, []byte, error) {
				message := append([]byte(protocol.NetIDVerificationTag), []byte("Bad!Data!")[:]...)
				return crypto.PublicKey{}, message, nil
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
		{
			// when the verification signature doesn't match the peer's expectation (the previously exchanged identity), peer is disconnected
			name: "bad signature",
			verifyResponse: func(t *testing.T, h http.Header, c identityChallengeValue) (crypto.PublicKey, []byte, error) {
				headerString := h.Get(IdentityChallengeHeader)
				require.NotEmpty(t, headerString)
				msg, err := base64.StdEncoding.DecodeString(headerString)
				require.NoError(t, err)
				resp := identityChallengeResponseSigned{}
				err = protocol.Decode(msg, &resp)
				require.NoError(t, err)
				s := NewIdentityChallengeScheme("does not matter") // make a throwaway key
				ver := identityVerificationMessageSigned{
					// fill in correct ResponseChallenge field
					Msg:       identityVerificationMessage{ResponseChallenge: resp.Msg.ResponseChallenge},
					Signature: s.identityKeys.SignBytes([]byte("bad bytes for signing")),
				}
				message := append([]byte(protocol.NetIDVerificationTag), protocol.Encode(&ver)[:]...)
				return crypto.PublicKey{}, message, nil
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
		{
			// when the verification signature doesn't match the peer's expectation (the previously exchanged identity), peer is disconnected
			name: "bad signature",
			verifyResponse: func(t *testing.T, h http.Header, c identityChallengeValue) (crypto.PublicKey, []byte, error) {
				s := NewIdentityChallengeScheme("does not matter") // make a throwaway key
				ver := identityVerificationMessageSigned{
					// fill in wrong ResponseChallenge field
					Msg:       identityVerificationMessage{ResponseChallenge: newIdentityChallengeValue()},
					Signature: s.identityKeys.SignBytes([]byte("bad bytes for signing")),
				}
				message := append([]byte(protocol.NetIDVerificationTag), protocol.Encode(&ver)[:]...)
				return crypto.PublicKey{}, message, nil
			},
			totalInA:  0,
			totalOutA: 0,
			totalInB:  0,
			totalOutB: 0,
		},
		{
			// when the identity is already in use, peer is disconnected
			name:           "identity occupied",
			verifyResponse: nil,
			totalInA:       0,
			totalOutA:      0,
			totalInB:       0,
			totalOutB:      0,
			occupied:       true,
		},
	}

	for _, tc := range testCases {
		t.Logf("Running Peering with Identity Verification Test: %s", tc.name)
		netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
		netA.identityTracker = newMockIdentityTracker(netA.identityTracker)
		netA.config.PublicAddress = "testing"
		netA.config.GossipFanout = 1

		scheme := newMockIdentityScheme(t)
		scheme.verifyResponse = tc.verifyResponse
		netA.identityScheme = scheme

		netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
		netB.identityTracker = newMockIdentityTracker(netB.identityTracker)
		netB.config.PublicAddress = "testing"
		netB.config.GossipFanout = 1
		// if the key is occupied, make the tracker fail to insert the peer
		if tc.occupied {
			netB.identityTracker = newMockIdentityTracker(netB.identityTracker)
			netB.identityTracker.(*mockIdentityTracker).setIsOccupied(true)
		}

		netA.Start()
		defer netA.Stop()
		netB.Start()
		defer netB.Stop()

		addrB, ok := netB.Address()
		require.True(t, ok)
		gossipB, err := netB.addrToGossipAddr(addrB)
		require.NoError(t, err)

		// set addresses to just host:port to match phonebook/dns format
		addrB = hostAndPort(addrB)

		if _, ok := netA.tryConnectReserveAddr(addrB); ok {
			netA.wg.Add(1)
			netA.tryConnect(addrB, gossipB)
			// let the tryConnect go forward
			time.Sleep(250 * time.Millisecond)
		}

		assert.Equal(t, tc.totalInA, len(netA.GetPeers(PeersConnectedIn)))
		assert.Equal(t, tc.totalOutA, len(netA.GetPeers(PeersConnectedOut)))
		assert.Equal(t, tc.totalOutB, len(netB.GetPeers(PeersConnectedOut)))
		// it is possible for NetB to be in the process of doing addPeer while
		// the underlying connection is being closed. In this case, the read loop
		// on the peer will detect and close the peer. Since this is asynchronous,
		// we wait and check regularly to allow the connection to settle
		assert.Eventually(
			t,
			func() bool { return len(netB.GetPeers(PeersConnectedIn)) == tc.totalInB },
			5*time.Second,
			100*time.Millisecond)
	}
}

type benchmarkHandler struct {
	returns chan uint64
}

func (bh *benchmarkHandler) Handle(message IncomingMessage) OutgoingMessage {
	i := binary.LittleEndian.Uint64(message.Data)
	bh.returns <- i
	return OutgoingMessage{}
}

// Set up two nodes, test that a.Broadcast is received by B
func BenchmarkWebsocketNetworkBasic(t *testing.B) {
	deadlock.Opts.Disable = true
	const msgSize = 200
	const inflight = 90
	t.Logf("%s %d", t.Name(), t.N)
	t.StopTimer()
	t.ResetTimer()
	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.Start()
	defer netStop(t, netA, "A")
	netB := makeTestWebsocketNode(t)
	netB.config.GossipFanout = 1
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")
	returns := make(chan uint64, 100)
	bhandler := benchmarkHandler{returns}
	netB.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: &bhandler}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	t.Log("a ready")
	waitReady(t, netB, readyTimeout.C)
	t.Log("b ready")
	var ireturned uint64

	t.StartTimer()
	timeoutd := (time.Duration(t.N) * 100 * time.Microsecond) + (2 * time.Second)
	timeout := time.After(timeoutd)
	for i := 0; i < t.N; i++ {
		for uint64(i) > ireturned+inflight {
			select {
			case ireturned = <-returns:
			case <-timeout:
				t.Errorf("timeout in send at %d", i)
				return
			}
		}
		msg := make([]byte, msgSize)
		binary.LittleEndian.PutUint64(msg, uint64(i))
		err := netA.Broadcast(context.Background(), protocol.TxnTag, msg, true, nil)
		if err != nil {
			t.Errorf("error on broadcast: %v", err)
			return
		}
	}
	netA.Broadcast(context.Background(), protocol.Tag("-1"), []byte("derp"), true, nil)
	t.Logf("sent %d", t.N)

	for ireturned < uint64(t.N-1) {
		select {
		case ireturned = <-returns:
		case <-timeout:
			t.Errorf("timeout, count=%d, wanted %d", ireturned, t.N)
			buf := strings.Builder{}
			networkMessageReceivedTotal.WriteMetric(&buf, "")
			networkMessageSentTotal.WriteMetric(&buf, "")
			networkBroadcasts.WriteMetric(&buf, "")
			duplicateNetworkMessageReceivedTotal.WriteMetric(&buf, "")
			outgoingNetworkMessageFilteredOutTotal.WriteMetric(&buf, "")
			networkBroadcastsDropped.WriteMetric(&buf, "")
			t.Errorf(
				"a out queue=%d, metric: %s",
				len(netA.broadcaster.broadcastQueueBulk),
				buf.String(),
			)
			return
		}
	}
	t.StopTimer()
	t.Logf("counter done")
}

// Check that priority is propagated from B to A
func TestWebsocketNetworkPrio(t *testing.T) {
	partitiontest.PartitionTest(t)

	prioA := netPrioStub{}
	netA := makeTestWebsocketNode(t)
	netA.SetPrioScheme(&prioA)
	netA.config.GossipFanout = 1
	netA.prioResponseChan = make(chan *wsPeer, 10)
	netA.Start()
	defer netStop(t, netA, "A")

	prioB := netPrioStub{}
	crypto.RandBytes(prioB.addr[:])
	prioB.prio = crypto.RandUint64()
	netB := makeTestWebsocketNode(t)
	netB.SetPrioScheme(&prioB)
	netB.config.GossipFanout = 1
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")

	// Wait for response message to propagate from B to A
	select {
	case <-netA.prioResponseChan:
	case <-time.After(time.Second):
		t.Errorf("timeout on netA.prioResponseChan")
	}
	waitReady(t, netA, time.After(time.Second))

	// Peek at A's peers
	netA.peersLock.RLock()
	defer netA.peersLock.RUnlock()
	require.Equal(t, len(netA.peers), 1)

	require.Equal(t, netA.peers[0].prioAddress, prioB.addr)
	require.Equal(t, netA.peers[0].prioWeight, prioB.prio)
}

// Check that priority is propagated from B to A
func TestWebsocketNetworkPrioLimit(t *testing.T) {
	partitiontest.PartitionTest(t)

	limitConf := defaultConfig
	limitConf.BroadcastConnectionsLimit = 1

	prioA := netPrioStub{}
	netA := makeTestWebsocketNodeWithConfig(t, limitConf)
	netA.SetPrioScheme(&prioA)
	netA.config.GossipFanout = 2
	netA.prioResponseChan = make(chan *wsPeer, 10)
	netA.Start()
	defer netStop(t, netA, "A")
	addrA, postListen := netA.Address()
	require.True(t, postListen)

	counterB := newMessageCounter(t, 1)
	counterBdone := counterB.done
	prioB := netPrioStub{}
	crypto.RandBytes(prioB.addr[:])
	prioB.prio = 100
	netB := makeTestWebsocketNode(t)
	netB.SetPrioScheme(&prioB)
	netB.config.GossipFanout = 1
	netB.config.NetAddress = ""
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: counterB}})
	netB.Start()
	defer netStop(t, netB, "B")

	counterC := newMessageCounter(t, 1)
	counterCdone := counterC.done
	prioC := netPrioStub{}
	crypto.RandBytes(prioC.addr[:])
	prioC.prio = 10
	netC := makeTestWebsocketNode(t)
	netC.SetPrioScheme(&prioC)
	netC.config.GossipFanout = 1
	netC.config.NetAddress = ""
	netC.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netC.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: counterC}})
	netC.Start()
	defer func() { t.Log("stopping C"); netC.Stop(); t.Log("C done") }()

	// Wait for response messages to propagate from B+C to A
	select {
	case peer := <-netA.prioResponseChan:
		netA.peersLock.RLock()
		require.Subset(t, []uint64{prioB.prio, prioC.prio}, []uint64{peer.prioWeight})
		netA.peersLock.RUnlock()
	case <-time.After(time.Second):
		t.Errorf("timeout on netA.prioResponseChan 1")
	}
	select {
	case peer := <-netA.prioResponseChan:
		netA.peersLock.RLock()
		require.Subset(t, []uint64{prioB.prio, prioC.prio}, []uint64{peer.prioWeight})
		netA.peersLock.RUnlock()
	case <-time.After(time.Second):
		t.Errorf("timeout on netA.prioResponseChan 2")
	}
	waitReady(t, netA, time.After(time.Second))

	firstPeer := netA.peers[0]
	netA.Broadcast(context.Background(), protocol.TxnTag, nil, true, nil)

	failed := false
	select {
	case <-counterBdone:
	case <-time.After(time.Second):
		t.Errorf("timeout, B did not receive message")
		failed = true
	}

	select {
	case <-counterCdone:
		t.Errorf("C received message")
		failed = true
	case <-time.After(time.Second):
	}

	if failed {
		t.Errorf("NetA had the following two peers priorities : [0]:%s=%d [1]:%s=%d", netA.peers[0].rootURL, netA.peers[0].prioWeight, netA.peers[1].rootURL, netA.peers[1].prioWeight)
		t.Errorf("first peer before broadcasting was %s", firstPeer.rootURL)
	}
}

// Create many idle connections, to see if we have excessive CPU utilization.
func TestWebsocketNetworkManyIdle(t *testing.T) {
	partitiontest.PartitionTest(t)

	// This test is meant to be run manually, as:
	//
	//   IDLETEST=x go test -v . -run=ManyIdle -count=1
	//
	// and examining the reported CPU time use.

	if os.Getenv("IDLETEST") == "" {
		t.Skip("Skipping; IDLETEST not set")
	}

	deadlock.Opts.Disable = true

	numClients := 1000
	relayConf := defaultConfig
	relayConf.BaseLoggerDebugLevel = uint32(logging.Error)
	relayConf.MaxConnectionsPerIP = numClients

	relay := makeTestWebsocketNodeWithConfig(t, relayConf)
	relay.config.GossipFanout = numClients
	relay.Start()
	defer relay.Stop()
	relayAddr, postListen := relay.Address()
	require.True(t, postListen)

	clientConf := defaultConfig
	clientConf.BaseLoggerDebugLevel = uint32(logging.Error)
	clientConf.BroadcastConnectionsLimit = 0
	clientConf.NetAddress = ""

	var clients []*WebsocketNetwork
	for i := 0; i < numClients; i++ {
		client := makeTestWebsocketNodeWithConfig(t, clientConf)
		client.config.GossipFanout = 1
		client.phonebook.ReplacePeerList([]string{relayAddr}, "default", PhoneBookEntryRelayRole)
		client.Start()
		defer client.Stop()

		clients = append(clients, client)
	}

	readyTimeout := time.NewTimer(30 * time.Second)
	waitReady(t, relay, readyTimeout.C)

	for i := 0; i < numClients; i++ {
		waitReady(t, clients[i], readyTimeout.C)
	}

	var r0utime, r1utime int64
	var r0stime, r1stime int64

	r0utime, r0stime, _ = util.GetCurrentProcessTimes()
	time.Sleep(10 * time.Second)
	r1utime, r1stime, _ = util.GetCurrentProcessTimes()

	t.Logf("Background CPU use: user %v, system %v\n",
		time.Duration(r1utime-r0utime),
		time.Duration(r1stime-r0stime))
}

// TODO: test both sides of http-header setting and checking?
// TODO: test request-disconnect-reconnect?
// TODO: test server handling of various malformed clients?
// TODO? disconnect a node in the middle of a line and test that messages _don't_ get through?
// TODO: test self-connect rejection
// TODO: test funcion when some message handler is slow?

func TestWebsocketNetwork_getCommonHeaders(t *testing.T) {
	partitiontest.PartitionTest(t)

	header := http.Header{}
	expectedTelemetryGUID := "123"
	expectedInstanceName := "456"
	expectedPublicAddr := "789"
	header.Set(TelemetryIDHeader, expectedTelemetryGUID)
	header.Set(InstanceNameHeader, expectedInstanceName)
	header.Set(AddressHeader, expectedPublicAddr)
	otherTelemetryGUID, otherInstanceName, otherPublicAddr := getCommonHeaders(header)
	require.Equal(t, expectedTelemetryGUID, otherTelemetryGUID)
	require.Equal(t, expectedInstanceName, otherInstanceName)
	require.Equal(t, expectedPublicAddr, otherPublicAddr)
}

func TestWebsocketNetwork_checkServerResponseVariables(t *testing.T) {
	partitiontest.PartitionTest(t)

	wn := makeTestWebsocketNode(t)
	wn.GenesisID = "genesis-id1"
	wn.RandomID = "random-id1"
	header := http.Header{}
	header.Set(ProtocolVersionHeader, ProtocolVersion)
	header.Set(NodeRandomHeader, wn.RandomID+"tag")
	header.Set(GenesisHeader, wn.GenesisID)
	responseVariableOk, matchingVersion := wn.checkServerResponseVariables(header, "addressX")
	require.Equal(t, true, responseVariableOk)
	require.Equal(t, matchingVersion, ProtocolVersion)

	noVersionHeader := http.Header{}
	noVersionHeader.Set(NodeRandomHeader, wn.RandomID+"tag")
	noVersionHeader.Set(GenesisHeader, wn.GenesisID)
	responseVariableOk, matchingVersion = wn.checkServerResponseVariables(noVersionHeader, "addressX")
	require.Equal(t, false, responseVariableOk)

	noRandomHeader := http.Header{}
	noRandomHeader.Set(ProtocolVersionHeader, ProtocolVersion)
	noRandomHeader.Set(GenesisHeader, wn.GenesisID)
	responseVariableOk, _ = wn.checkServerResponseVariables(noRandomHeader, "addressX")
	require.Equal(t, false, responseVariableOk)

	sameRandomHeader := http.Header{}
	sameRandomHeader.Set(ProtocolVersionHeader, ProtocolVersion)
	sameRandomHeader.Set(NodeRandomHeader, wn.RandomID)
	sameRandomHeader.Set(GenesisHeader, wn.GenesisID)
	responseVariableOk, _ = wn.checkServerResponseVariables(sameRandomHeader, "addressX")
	require.Equal(t, false, responseVariableOk)

	differentGenesisIDHeader := http.Header{}
	differentGenesisIDHeader.Set(ProtocolVersionHeader, ProtocolVersion)
	differentGenesisIDHeader.Set(NodeRandomHeader, wn.RandomID+"tag")
	differentGenesisIDHeader.Set(GenesisHeader, wn.GenesisID+"tag")
	responseVariableOk, _ = wn.checkServerResponseVariables(differentGenesisIDHeader, "addressX")
	require.Equal(t, false, responseVariableOk)
}

func (wn *WebsocketNetwork) broadcastWithTimestamp(tag protocol.Tag, data []byte, when time.Time) error {
	msgArr := make([][]byte, 1, 1)
	msgArr[0] = data
	tagArr := make([]protocol.Tag, 1, 1)
	tagArr[0] = tag
	request := broadcastRequest{tags: tagArr, data: msgArr, enqueueTime: when, ctx: context.Background()}

	broadcastQueue := wn.broadcaster.broadcastQueueBulk
	if highPriorityTag(tagArr) {
		broadcastQueue = wn.broadcaster.broadcastQueueHighPrio
	}
	// no wait
	select {
	case broadcastQueue <- request:
		return nil
	default:
		return errBcastQFull
	}
}

func TestDelayedMessageDrop(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.Start()
	defer netStop(t, netA, "A")

	noAddressConfig := defaultConfig
	noAddressConfig.NetAddress = ""
	netB := makeTestWebsocketNodeWithConfig(t, noAddressConfig)
	netB.config.GossipFanout = 1
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")
	counter := newMessageCounter(t, 5)
	counterDone := counter.done
	netB.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: counter}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)

	currentTime := time.Now()
	for i := 0; i < 10; i++ {
		err := netA.broadcastWithTimestamp(protocol.TxnTag, []byte("foo"), currentTime.Add(time.Hour*time.Duration(i-5)))
		require.NoErrorf(t, err, "No error was expected")
	}

	select {
	case <-counterDone:
	case <-time.After(maxMessageQueueDuration):
		require.Equalf(t, 5, counter.count, "One or more messages failed to reach destination network")
	}
}

func TestSlowPeerDisconnection(t *testing.T) {
	partitiontest.PartitionTest(t)

	log := logging.TestingLog(t)
	log.SetLevel(logging.Info)
	wn := &WebsocketNetwork{
		log:       log,
		config:    defaultConfig,
		phonebook: MakePhonebook(1, 1*time.Millisecond),
		GenesisID: genesisID,
		NetworkID: config.Devtestnet,
	}
	wn.setup()
	wn.broadcaster.slowWritingPeerMonitorInterval = time.Millisecond * 50
	wn.eventualReadyDelay = time.Second
	wn.messagesOfInterest = nil // clear this before starting the network so that we won't be sending a MOI upon connection.

	netA := wn
	netA.config.GossipFanout = 1
	netA.Start()
	defer netStop(t, netA, "A")

	noAddressConfig := defaultConfig
	noAddressConfig.NetAddress = ""
	netB := makeTestWebsocketNodeWithConfig(t, noAddressConfig)
	netB.config.GossipFanout = 1
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)

	var peers []*wsPeer
	peers, _ = netA.peerSnapshot(peers)
	require.Equalf(t, len(peers), 1, "Expected number of peers should be 1")
	peer := peers[0]
	// On connection may send a MOI message, wait for it to go out
	now := time.Now()
	expire := now.Add(5 * time.Second)
	for {
		time.Sleep(10 * time.Millisecond)
		if len(peer.sendBufferHighPrio)+len(peer.sendBufferBulk) == 0 {
			break
		}
		now = time.Now()
		if now.After(expire) {
			t.Errorf("wait for empty peer outbound queue expired")
		}
	}
	// modify the peer on netA and
	beforeLoopTime := time.Now()
	peer.intermittentOutgoingMessageEnqueueTime.Store(beforeLoopTime.Add(-maxMessageQueueDuration).Add(time.Second).UnixNano())
	// wait up to 10 seconds for the monitor to figure out it needs to disconnect.
	expire = beforeLoopTime.Add(2 * slowWritingPeerMonitorInterval)
	for {
		peers, _ = netA.peerSnapshot(peers)
		if len(peers) == 0 || peers[0] != peer {
			// make sure it took more than 1 second, and less than 5 seconds.
			waitTime := time.Since(beforeLoopTime)
			require.LessOrEqual(t, int64(time.Second), int64(waitTime))
			require.GreaterOrEqual(t, int64(5*time.Second), int64(waitTime))
			break
		}
		if time.Now().After(expire) {
			require.Fail(t, "Slow peer was not disconnected")
		}
		time.Sleep(time.Millisecond * 5)
	}
}

func TestForceMessageRelaying(t *testing.T) {
	partitiontest.PartitionTest(t)

	log := logging.TestingLog(t)
	log.SetLevel(logging.Level(defaultConfig.BaseLoggerDebugLevel))
	wn := &WebsocketNetwork{
		log:       log,
		config:    defaultConfig,
		phonebook: MakePhonebook(1, 1*time.Millisecond),
		GenesisID: genesisID,
		NetworkID: config.Devtestnet,
	}
	wn.setup()
	wn.eventualReadyDelay = time.Second

	netA := wn
	netA.config.GossipFanout = 1

	defer netStop(t, netA, "A")

	counter := newMessageCounter(t, 5)
	counterDone := counter.done
	netA.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: counter}})
	netA.Start()
	addrA, postListen := netA.Address()
	require.Truef(t, postListen, "Listening network failed to start")

	noAddressConfig := defaultConfig
	noAddressConfig.NetAddress = ""
	netB := makeTestWebsocketNodeWithConfig(t, noAddressConfig)
	netB.config.GossipFanout = 1
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")

	noAddressConfig.ForceRelayMessages = true
	netC := makeTestWebsocketNodeWithConfig(t, noAddressConfig)
	netC.config.GossipFanout = 1
	netC.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netC.Start()
	defer func() { t.Log("stopping C"); netC.Stop(); t.Log("C done") }()

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)
	waitReady(t, netC, readyTimeout.C)

	// send 5 messages from both netB and netC to netA
	for i := 0; i < 5; i++ {
		err := netB.Relay(context.Background(), protocol.TxnTag, []byte{1, 2, 3}, true, nil)
		require.NoError(t, err)
		err = netC.Relay(context.Background(), protocol.TxnTag, []byte{1, 2, 3}, true, nil)
		require.NoError(t, err)
	}

	select {
	case <-counterDone:
	case <-time.After(2 * time.Second):
		if counter.count < 5 {
			require.Failf(t, "One or more messages failed to reach destination network", "%d > %d", 5, counter.count)
		} else if counter.count > 5 {
			require.Failf(t, "One or more messages that were expected to be dropped, reached destination network", "%d < %d", 5, counter.count)
		}
	}
	netA.ClearHandlers()
	counter = newMessageCounter(t, 10)
	counterDone = counter.done
	netA.RegisterHandlers([]TaggedMessageHandler{{Tag: protocol.TxnTag, MessageHandler: counter}})

	// hack the relayMessages on the netB so that it would start sending messages.
	netB.relayMessages = true
	// send additional 10 messages from netB
	for i := 0; i < 10; i++ {
		err := netB.Relay(context.Background(), protocol.TxnTag, []byte{1, 2, 3}, true, nil)
		require.NoError(t, err)
	}

	select {
	case <-counterDone:
	case <-time.After(2 * time.Second):
		require.Failf(t, "One or more messages failed to reach destination network", "%d > %d", 10, counter.count)
	}

}

func TestSetUserAgentHeader(t *testing.T) {
	partitiontest.PartitionTest(t)

	headers := http.Header{}
	SetUserAgentHeader(headers)
	require.Equal(t, 1, len(headers))
	t.Log(headers)
}

func TestCheckProtocolVersionMatch(t *testing.T) {
	partitiontest.PartitionTest(t)

	log := logging.TestingLog(t)
	log.SetLevel(logging.Level(defaultConfig.BaseLoggerDebugLevel))
	wn := &WebsocketNetwork{
		log:       log,
		config:    defaultConfig,
		phonebook: MakePhonebook(1, 1*time.Millisecond),
		GenesisID: genesisID,
		NetworkID: config.Devtestnet,
	}
	wn.setup()
	wn.supportedProtocolVersions = []string{"2", "1"}

	header1 := make(http.Header)
	header1.Add(ProtocolAcceptVersionHeader, "1")
	header1.Add(ProtocolVersionHeader, "3")
	matchingVersion, otherVersion := wn.checkProtocolVersionMatch(header1)
	require.Equal(t, "1", matchingVersion)
	require.Equal(t, "", otherVersion)

	header2 := make(http.Header)
	header2.Add(ProtocolAcceptVersionHeader, "3")
	header2.Add(ProtocolAcceptVersionHeader, "4")
	header2.Add(ProtocolVersionHeader, "1")
	matchingVersion, otherVersion = wn.checkProtocolVersionMatch(header2)
	require.Equal(t, "1", matchingVersion)
	require.Equal(t, "1", otherVersion)

	header3 := make(http.Header)
	header3.Add(ProtocolVersionHeader, "3")
	matchingVersion, otherVersion = wn.checkProtocolVersionMatch(header3)
	require.Equal(t, "", matchingVersion)
	require.Equal(t, "3", otherVersion)

	header4 := make(http.Header)
	header4.Add(ProtocolVersionHeader, "5\n")
	matchingVersion, otherVersion = wn.checkProtocolVersionMatch(header4)
	require.Equal(t, "", matchingVersion)
	require.Equal(t, "5"+unprintableCharacterGlyph, otherVersion)
}

func handleTopicRequest(msg IncomingMessage) (out OutgoingMessage) {

	topics, err := UnmarshallTopics(msg.Data)
	if err != nil {
		return
	}

	val1b, f := topics.GetValue("val1")
	if !f {
		return
	}
	val2b, f := topics.GetValue("val2")
	if !f {
		return
	}
	val1 := int(val1b[0])
	val2 := int(val2b[0])

	respTopics := Topics{
		Topic{
			key:  "value",
			data: []byte{byte(val1 + val2)},
		},
	}
	return OutgoingMessage{
		Action: Respond,
		Tag:    protocol.TopicMsgRespTag,
		Topics: respTopics,
	}
}

// Set up two nodes, test topics send/receive is working
func TestWebsocketNetworkTopicRoundtrip(t *testing.T) {
	partitiontest.PartitionTest(t)

	var topicMsgReqTag Tag = protocol.UniEnsBlockReqTag
	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.Start()
	defer netStop(t, netA, "A")
	netB := makeTestWebsocketNode(t)
	netB.config.GossipFanout = 1
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")

	netB.RegisterHandlers([]TaggedMessageHandler{
		{
			Tag:            topicMsgReqTag,
			MessageHandler: HandlerFunc(handleTopicRequest),
		},
	})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	t.Log("a ready")
	waitReady(t, netB, readyTimeout.C)
	t.Log("b ready")

	peerA := netA.peers[0]

	topics := Topics{
		Topic{
			key:  "command",
			data: []byte("add"),
		},
		Topic{
			key:  "val1",
			data: []byte{1},
		},
		Topic{
			key:  "val2",
			data: []byte{4},
		},
	}

	resp, err := peerA.Request(context.Background(), topicMsgReqTag, topics)
	assert.NoError(t, err)

	sum, found := resp.Topics.GetValue("value")
	assert.Equal(t, true, found)
	assert.Equal(t, 5, int(sum[0]))
}

func waitPeerInternalChanQuiet(t *testing.T, netA *WebsocketNetwork) {
	// okay, but now we need to wait for asynchronous thread within netA to _apply_ the MOI to its peer for netB...
	timeout := time.Now().Add(100 * time.Millisecond)
	waiting := true
	for waiting {
		time.Sleep(1 * time.Millisecond)
		peers := netA.GetPeers(PeersConnectedIn)
		for _, pg := range peers {
			wp := pg.(*wsPeer)
			if len(wp.sendBufferHighPrio)+len(wp.sendBufferBulk) == 0 {
				waiting = false
				break
			}
		}
		if time.Now().After(timeout) {
			for _, pg := range peers {
				wp := pg.(*wsPeer)
				if len(wp.sendBufferHighPrio)+len(wp.sendBufferBulk) == 0 {
					t.Fatalf("netA peer buff empty timeout len(high)=%d, len(bulk)=%d", len(wp.sendBufferHighPrio), len(wp.sendBufferBulk))
				}
			}
		}
	}
}

func waitForMOIRefreshQuiet(netB *WebsocketNetwork) {
	for {
		// wait for async messagesOfInterestRefresh
		time.Sleep(time.Millisecond)
		if len(netB.messagesOfInterestRefresh) == 0 {
			break
		}
	}
}

// Set up two nodes, have one of them request a certain message tag mask, and verify the other follow that.
func TestWebsocketNetworkMessageOfInterest(t *testing.T) {
	partitiontest.PartitionTest(t)
	var (
		ft1 = protocol.Tag("AV")
		ft2 = protocol.Tag("pj")
		ft3 = protocol.Tag("NI")
		ft4 = protocol.Tag("TX")

		testTags = []protocol.Tag{ft1, ft2, ft3, ft4}
	)
	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.config.EnablePingHandler = false

	netA.Start()
	defer netStop(t, netA, "A")
	netB := makeTestWebsocketNode(t)
	netB.config.GossipFanout = 1
	netB.config.EnablePingHandler = false
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Logf("netA %s", addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)

	// have netB asking netA to send it ft2, deregister ping handler to make sure that we aren't exceeding the maximum MOI messagesize
	// Max MOI size is calculated by encoding all of the valid tags, since we are using a custom tag here we must deregister one in the default set.
	netB.DeregisterMessageInterest(protocol.PingTag)
	netB.registerMessageInterest(ft2)

	netB.Start()
	defer netStop(t, netB, "B")
	addrB, _ := netB.Address()
	t.Logf("netB %s", addrB)

	incomingMsgSync := deadlock.Mutex{}
	msgCounters := make(map[protocol.Tag]int)
	expectedCounts := make(map[protocol.Tag]int)
	expectedCounts[ft2] = 5
	var failed atomic.Uint32
	messageArriveWg := sync.WaitGroup{}
	msgHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		t.Logf("A->B %s", msg.Tag)
		incomingMsgSync.Lock()
		defer incomingMsgSync.Unlock()
		expected := expectedCounts[msg.Tag]
		if expected < 1 {
			failed.Store(1)
			t.Logf("UNEXPECTED A->B %s", msg.Tag)
			return
		}
		msgCounters[msg.Tag] = msgCounters[msg.Tag] + 1
		messageArriveWg.Done()
		return
	}
	messageFilterArriveWg := sync.WaitGroup{}
	messageFilterArriveWg.Add(1)
	waitMessageArriveHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		messageFilterArriveWg.Done()
		return
	}

	// register all the handlers.
	taggedHandlers := []TaggedMessageHandler{}
	for _, tag := range testTags {
		taggedHandlers = append(taggedHandlers, TaggedMessageHandler{
			Tag:            tag,
			MessageHandler: HandlerFunc(msgHandler),
		})
	}
	netB.RegisterHandlers(taggedHandlers)
	netA.RegisterHandlers([]TaggedMessageHandler{
		{
			Tag:            protocol.VoteBundleTag,
			MessageHandler: HandlerFunc(waitMessageArriveHandler),
		}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)

	// have netB asking netA to send it only AgreementVoteTag and ProposalPayloadTag
	netB.registerMessageInterest(ft2)
	netB.DeregisterMessageInterest(ft1)
	netB.DeregisterMessageInterest(ft3)
	netB.DeregisterMessageInterest(ft4)

	// send another message which we can track, so that we'll know that the first message was delivered.
	netB.Broadcast(context.Background(), protocol.VoteBundleTag, []byte{0, 1, 2, 3, 4}, true, nil)
	messageFilterArriveWg.Wait()
	waitPeerInternalChanQuiet(t, netA)

	messageArriveWg.Add(5) // we're expecting exactly 5 messages.
	// send 5 messages of few types.
	for i := 0; i < 5; i++ {
		if failed.Load() != 0 {
			t.Errorf("failed")
			break
		}
		netA.Broadcast(context.Background(), ft1, []byte{0, 1, 2, 3, 4}, true, nil) // NOT in MOI
		netA.Broadcast(context.Background(), ft3, []byte{0, 1, 2, 3, 4}, true, nil) // NOT in MOI
		netA.Broadcast(context.Background(), ft2, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), ft4, []byte{0, 1, 2, 3, 4}, true, nil) // NOT in MOI
	}
	if failed.Load() != 0 {
		t.Errorf("failed")
	}
	// wait until all the expected messages arrive.
	messageArriveWg.Wait()
	incomingMsgSync.Lock()
	defer incomingMsgSync.Unlock()
	require.Equal(t, 1, len(msgCounters))
	for tag, count := range msgCounters {
		if failed.Load() != 0 {
			t.Errorf("failed")
			break
		}
		if tag == ft1 || tag == ft2 {
			require.Equal(t, 5, count)
		} else {
			require.Equal(t, 0, count)
		}
	}
}

// Set up two nodes, have one of them work through TX gossip message-of-interest logic
// test:
// * wn.config.ForceFetchTransactions
// * wn.config.ForceRelayMessages
// * NodeInfo.IsParticipating() + WebsocketNetwork.OnNetworkAdvance()
func TestWebsocketNetworkTXMessageOfInterestRelay(t *testing.T) {
	// Tests that A->B follows MOI
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.config.EnablePingHandler = false

	netA.Start()
	defer netStop(t, netA, "A")
	bConfig := defaultConfig
	bConfig.NetAddress = ""
	bConfig.ForceRelayMessages = true
	netB := makeTestWebsocketNodeWithConfig(t, bConfig)
	netB.config.GossipFanout = 1
	netB.config.EnablePingHandler = false
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")

	incomingMsgSync := deadlock.Mutex{}
	msgCounters := make(map[protocol.Tag]int)
	messageArriveWg := sync.WaitGroup{}
	msgHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		t.Logf("A->B %s", msg.Tag)
		incomingMsgSync.Lock()
		defer incomingMsgSync.Unlock()
		msgCounters[msg.Tag] = msgCounters[msg.Tag] + 1
		messageArriveWg.Done()
		return
	}
	messageFilterArriveWg := sync.WaitGroup{}
	messageFilterArriveWg.Add(1)
	waitMessageArriveHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		messageFilterArriveWg.Done()
		return
	}

	// register all the handlers.
	taggedHandlers := []TaggedMessageHandler{}
	for tag := range defaultSendMessageTags {
		taggedHandlers = append(taggedHandlers, TaggedMessageHandler{
			Tag:            tag,
			MessageHandler: HandlerFunc(msgHandler),
		})
	}
	netB.RegisterHandlers(taggedHandlers)
	netA.RegisterHandlers([]TaggedMessageHandler{
		{
			Tag:            protocol.AgreementVoteTag,
			MessageHandler: HandlerFunc(waitMessageArriveHandler),
		}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)

	netB.OnNetworkAdvance()
	waitForMOIRefreshQuiet(netB)
	// send another message which we can track, so that we'll know that the first message was delivered.
	netB.Broadcast(context.Background(), protocol.AgreementVoteTag, []byte{0, 1, 2, 3, 4}, true, nil)
	messageFilterArriveWg.Wait()

	messageArriveWg.Add(5 * 4) // we're expecting exactly 20 messages.
	// send 5 messages of few types.
	for i := 0; i < 5; i++ {
		netA.Broadcast(context.Background(), protocol.AgreementVoteTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.TxnTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.ProposalPayloadTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.VoteBundleTag, []byte{0, 1, 2, 3, 4}, true, nil)
	}
	// wait until all the expected messages arrive.
	messageArriveWg.Wait()
	incomingMsgSync.Lock()
	require.Equal(t, 4, len(msgCounters))
	for _, count := range msgCounters {
		require.Equal(t, 5, count)
	}
	incomingMsgSync.Unlock()
}

func TestWebsocketNetworkTXMessageOfInterestForceTx(t *testing.T) {
	// Tests that A->B follows MOI
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.config.EnablePingHandler = false

	netA.Start()
	defer netStop(t, netA, "A")
	bConfig := defaultConfig
	bConfig.NetAddress = ""
	bConfig.ForceFetchTransactions = true
	netB := makeTestWebsocketNodeWithConfig(t, bConfig)
	netB.config.GossipFanout = 1
	netB.config.EnablePingHandler = false
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")

	incomingMsgSync := deadlock.Mutex{}
	msgCounters := make(map[protocol.Tag]int)
	messageArriveWg := sync.WaitGroup{}
	msgHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		t.Logf("A->B %s", msg.Tag)
		incomingMsgSync.Lock()
		defer incomingMsgSync.Unlock()
		msgCounters[msg.Tag] = msgCounters[msg.Tag] + 1
		messageArriveWg.Done()
		return
	}
	messageFilterArriveWg := sync.WaitGroup{}
	messageFilterArriveWg.Add(1)
	waitMessageArriveHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		messageFilterArriveWg.Done()
		return
	}

	// register all the handlers.
	taggedHandlers := []TaggedMessageHandler{}
	for tag := range defaultSendMessageTags {
		taggedHandlers = append(taggedHandlers, TaggedMessageHandler{
			Tag:            tag,
			MessageHandler: HandlerFunc(msgHandler),
		})
	}
	netB.RegisterHandlers(taggedHandlers)
	netA.RegisterHandlers([]TaggedMessageHandler{
		{
			Tag:            protocol.AgreementVoteTag,
			MessageHandler: HandlerFunc(waitMessageArriveHandler),
		}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)

	netB.OnNetworkAdvance()
	waitForMOIRefreshQuiet(netB)
	// send another message which we can track, so that we'll know that the first message was delivered.
	netB.Broadcast(context.Background(), protocol.AgreementVoteTag, []byte{0, 1, 2, 3, 4}, true, nil)
	messageFilterArriveWg.Wait()

	messageArriveWg.Add(5 * 4) // we're expecting exactly 20 messages.
	// send 5 messages of few types.
	for i := 0; i < 5; i++ {
		netA.Broadcast(context.Background(), protocol.AgreementVoteTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.TxnTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.ProposalPayloadTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.VoteBundleTag, []byte{0, 1, 2, 3, 4}, true, nil)
	}
	// wait until all the expected messages arrive.
	messageArriveWg.Wait()
	incomingMsgSync.Lock()
	require.Equal(t, 4, len(msgCounters))
	for _, count := range msgCounters {
		require.Equal(t, 5, count)
	}
	incomingMsgSync.Unlock()
}
func TestWebsocketNetworkTXMessageOfInterestNPN(t *testing.T) {
	// Tests that A->B follows MOI
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.config.EnablePingHandler = false
	netA.Start()
	defer netStop(t, netA, "A")

	bConfig := defaultConfig
	bConfig.NetAddress = ""
	netB := makeTestWebsocketNodeWithConfig(t, bConfig)
	netB.config.GossipFanout = 1
	netB.config.EnablePingHandler = false
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")
	require.False(t, netB.relayMessages)
	require.Equal(t, uint32(wantTXGossipUnk), netB.wantTXGossip.Load())

	incomingMsgSync := deadlock.Mutex{}
	msgCounters := make(map[protocol.Tag]int)
	messageArriveWg := sync.WaitGroup{}
	msgHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		t.Logf("A->B %s", msg.Tag)
		incomingMsgSync.Lock()
		defer incomingMsgSync.Unlock()
		msgCounters[msg.Tag] = msgCounters[msg.Tag] + 1
		messageArriveWg.Done()
		return
	}
	messageFilterArriveWg := sync.WaitGroup{}
	messageFilterArriveWg.Add(1)
	waitMessageArriveHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		messageFilterArriveWg.Done()
		return
	}

	// register all the handlers.
	taggedHandlers := []TaggedMessageHandler{}
	for tag := range defaultSendMessageTags {
		taggedHandlers = append(taggedHandlers, TaggedMessageHandler{
			Tag:            tag,
			MessageHandler: HandlerFunc(msgHandler),
		})
	}
	netB.RegisterHandlers(taggedHandlers)
	netA.RegisterHandlers([]TaggedMessageHandler{
		{
			Tag:            protocol.AgreementVoteTag,
			MessageHandler: HandlerFunc(waitMessageArriveHandler),
		}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)

	netB.OnNetworkAdvance()
	waitForMOIRefreshQuiet(netB)
	for i := 0; i < 100; i++ {
		if netB.wantTXGossip.Load() == uint32(wantTXGossipNo) {
			break
		}
		time.Sleep(10 * time.Millisecond)
	}
	require.Equal(t, uint32(wantTXGossipNo), netB.wantTXGossip.Load())
	// send another message which we can track, so that we'll know that the first message was delivered.
	netB.Broadcast(context.Background(), protocol.AgreementVoteTag, []byte{0, 1, 2, 3, 4}, true, nil)
	messageFilterArriveWg.Wait()
	waitPeerInternalChanQuiet(t, netA)

	messageArriveWg.Add(5 * 3) // we're expecting exactly 15 messages.
	// send 5 messages of few types.
	for i := 0; i < 5; i++ {
		netA.Broadcast(context.Background(), protocol.AgreementVoteTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.TxnTag, []byte{0, 1, 2, 3, 4}, true, nil) // THESE WILL BE DROPPED
		netA.Broadcast(context.Background(), protocol.ProposalPayloadTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.VoteBundleTag, []byte{0, 1, 2, 3, 4}, true, nil)
	}
	// wait until all the expected messages arrive.
	messageArriveWg.Wait()
	incomingMsgSync.Lock()
	require.Equal(t, 3, len(msgCounters), msgCounters)
	for tag, count := range msgCounters {
		if tag == protocol.TxnTag {
			require.Equal(t, 0, count)
		} else {
			require.Equal(t, 5, count)
		}
	}
	incomingMsgSync.Unlock()
}

type participatingNodeInfo struct {
}

func (nnni *participatingNodeInfo) IsParticipating() bool {
	return true
}

func TestWebsocketNetworkTXMessageOfInterestPN(t *testing.T) {
	// Tests that A->B follows MOI
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.config.EnablePingHandler = false
	netA.Start()
	defer netStop(t, netA, "A")

	bConfig := defaultConfig
	bConfig.NetAddress = ""
	netB := makeTestWebsocketNodeWithConfig(t, bConfig)
	netB.nodeInfo = &participatingNodeInfo{}
	netB.config.GossipFanout = 1
	netB.config.EnablePingHandler = false
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")
	require.False(t, netB.relayMessages)
	require.Equal(t, uint32(wantTXGossipUnk), netB.wantTXGossip.Load())

	incomingMsgSync := deadlock.Mutex{}
	msgCounters := make(map[protocol.Tag]int)
	messageArriveWg := sync.WaitGroup{}
	msgHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		t.Logf("A->B %s", msg.Tag)
		incomingMsgSync.Lock()
		defer incomingMsgSync.Unlock()
		msgCounters[msg.Tag] = msgCounters[msg.Tag] + 1
		messageArriveWg.Done()
		return
	}
	messageFilterArriveWg := sync.WaitGroup{}
	messageFilterArriveWg.Add(1)
	waitMessageArriveHandler := func(msg IncomingMessage) (out OutgoingMessage) {
		messageFilterArriveWg.Done()
		return
	}

	// register all the handlers.
	taggedHandlers := []TaggedMessageHandler{}
	for tag := range defaultSendMessageTags {
		taggedHandlers = append(taggedHandlers, TaggedMessageHandler{
			Tag:            tag,
			MessageHandler: HandlerFunc(msgHandler),
		})
	}
	netB.RegisterHandlers(taggedHandlers)
	netA.RegisterHandlers([]TaggedMessageHandler{
		{
			Tag:            protocol.AgreementVoteTag,
			MessageHandler: HandlerFunc(waitMessageArriveHandler),
		}})

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)

	netB.OnNetworkAdvance()
	waitForMOIRefreshQuiet(netB)
	for i := 0; i < 100; i++ {
		if netB.wantTXGossip.Load() == uint32(wantTXGossipYes) {
			break
		}
		time.Sleep(10 * time.Millisecond)
	}
	require.Equal(t, uint32(wantTXGossipYes), netB.wantTXGossip.Load())
	// send another message which we can track, so that we'll know that the first message was delivered.
	netB.Broadcast(context.Background(), protocol.AgreementVoteTag, []byte{0, 1, 2, 3, 4}, true, nil)
	messageFilterArriveWg.Wait()

	messageArriveWg.Add(5 * 4) // we're expecting exactly 20 messages.
	// send 5 messages of few types.
	for i := 0; i < 5; i++ {
		netA.Broadcast(context.Background(), protocol.AgreementVoteTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.TxnTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.ProposalPayloadTag, []byte{0, 1, 2, 3, 4}, true, nil)
		netA.Broadcast(context.Background(), protocol.VoteBundleTag, []byte{0, 1, 2, 3, 4}, true, nil)
	}
	// wait until all the expected messages arrive.
	messageArriveWg.Wait()
	incomingMsgSync.Lock()
	require.Equal(t, 4, len(msgCounters))
	for tag, count := range msgCounters {
		if tag == protocol.TxnTag {
			require.Equal(t, 5, count)
		} else {
			require.Equal(t, 5, count)
		}
	}
	incomingMsgSync.Unlock()
}

// Set up two nodes, have one of them disconnect from the other, and monitor disconnection error on the side that did not issue the disconnection.
// Plan:
// Network A will be sending messages to network B.
// Network B will respond with another message for the first 4 messages. When it receive the 5th message, it would close the connection.
func TestWebsocketDisconnection(t *testing.T) {
	partitiontest.PartitionTest(t)

	// We want to get an event with disconnectRequestReceived from netA
	testWebsocketDisconnection(t, func(wn *WebsocketNetwork, _ *OutgoingMessage) {
		wn.DisconnectPeers()
	}, nil)

	// We want to get an event with the default reason from netB
	defaultReason := disconnectBadData
	testWebsocketDisconnection(t, func(_ *WebsocketNetwork, out *OutgoingMessage) {
		out.Action = Disconnect
	}, &defaultReason)

	// We want to get an event with the provided reason from netB
	customReason := disconnectReason("MyCustomDisconnectReason")
	testWebsocketDisconnection(t, func(_ *WebsocketNetwork, out *OutgoingMessage) {
		out.Action = Disconnect
		out.reason = customReason
	}, &customReason)
}

func testWebsocketDisconnection(t *testing.T, disconnectFunc func(wn *WebsocketNetwork, out *OutgoingMessage), expectedNetBReason *disconnectReason) {
	netA := makeTestWebsocketNode(t)
	netA.config.GossipFanout = 1
	netA.config.EnablePingHandler = false
	dlNetA := eventsDetailsLogger{Logger: logging.TestingLog(t), eventReceived: make(chan interface{}, 1), eventIdentifier: telemetryspec.DisconnectPeerEvent}
	netA.log = dlNetA

	netA.Start()
	defer netStop(t, netA, "A")
	netB := makeTestWebsocketNode(t)
	netB.config.GossipFanout = 1
	netB.config.EnablePingHandler = false
	dlNetB := eventsDetailsLogger{Logger: logging.TestingLog(t), eventReceived: make(chan interface{}, 1), eventIdentifier: telemetryspec.DisconnectPeerEvent}
	netB.log = dlNetB

	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer netStop(t, netB, "B")

	msgHandlerA := func(msg IncomingMessage) (out OutgoingMessage) {
		// if we received a message, send a message back.
		if msg.Data[0]%10 == 2 {
			netA.Broadcast(context.Background(), protocol.ProposalPayloadTag, []byte{msg.Data[0] + 8}, true, nil)
		}
		return
	}

	var msgCounterNetB atomic.Uint32
	msgHandlerB := func(msg IncomingMessage) (out OutgoingMessage) {
		if msgCounterNetB.Add(1) == 5 {
			// disconnect
			disconnectFunc(netB, &out)
		} else {
			// if we received a message, send a message back.
			netB.Broadcast(context.Background(), protocol.ProposalPayloadTag, []byte{msg.Data[0] + 1}, true, nil)
			netB.Broadcast(context.Background(), protocol.ProposalPayloadTag, []byte{msg.Data[0] + 2}, true, nil)
		}
		return
	}

	// register all the handlers.
	taggedHandlersA := []TaggedMessageHandler{
		{
			Tag:            protocol.ProposalPayloadTag,
			MessageHandler: HandlerFunc(msgHandlerA),
		},
	}
	netA.ClearHandlers()
	netA.RegisterHandlers(taggedHandlersA)

	taggedHandlersB := []TaggedMessageHandler{
		{
			Tag:            protocol.ProposalPayloadTag,
			MessageHandler: HandlerFunc(msgHandlerB),
		},
	}
	netB.ClearHandlers()
	netB.RegisterHandlers(taggedHandlersB)

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)
	netA.Broadcast(context.Background(), protocol.ProposalPayloadTag, []byte{0}, true, nil)
	// wait until the peers disconnect.
	for {
		peers := netA.GetPeers(PeersConnectedIn)
		if len(peers) == 0 {
			break
		}
		time.Sleep(100 * time.Millisecond)
	}

	select {
	case eventDetails := <-dlNetA.eventReceived:
		switch disconnectPeerEventDetails := eventDetails.(type) {
		case telemetryspec.DisconnectPeerEventDetails:
			require.Equal(t, string(disconnectRequestReceived), disconnectPeerEventDetails.Reason)
		default:
			require.FailNow(t, "Unexpected event was send : %v", eventDetails)
		}

	default:
		require.FailNow(t, "The NetA DisconnectPeerEvent was missing")
	}

	if expectedNetBReason != nil {
		select {
		case eventDetails := <-dlNetB.eventReceived:
			switch disconnectPeerEventDetails := eventDetails.(type) {
			case telemetryspec.DisconnectPeerEventDetails:
				require.Equal(t, string(*expectedNetBReason), disconnectPeerEventDetails.Reason)
			default:
				require.FailNow(t, "Unexpected event was send : %v", eventDetails)
			}

		default:
			require.FailNow(t, "The NetB DisconnectPeerEvent was missing")
		}
	}
}

// TestASCIIFiltering tests the behaviour of filterASCII by feeding it with few known inputs and verifying the expected outputs.
func TestASCIIFiltering(t *testing.T) {
	partitiontest.PartitionTest(t)

	testUnicodePrintableStrings := []struct {
		testString     string
		expectedString string
	}{
		{"abc", "abc"},
		{"", ""},
		{"אבג", unprintableCharacterGlyph + unprintableCharacterGlyph + unprintableCharacterGlyph},
		{"\u001b[31mABC\u001b[0m", unprintableCharacterGlyph + "[31mABC" + unprintableCharacterGlyph + "[0m"},
		{"ab\nc", "ab" + unprintableCharacterGlyph + "c"},
	}
	for _, testElement := range testUnicodePrintableStrings {
		outString := filterASCII(testElement.testString)
		require.Equalf(t, testElement.expectedString, outString, "test string:%s", testElement.testString)
	}
}

type callbackLogger struct {
	logging.Logger
	InfoCallback  func(...interface{})
	InfofCallback func(string, ...interface{})
	WarnCallback  func(...interface{})
	WarnfCallback func(string, ...interface{})
}

func (cl callbackLogger) Info(args ...interface{}) {
	cl.InfoCallback(args...)
}
func (cl callbackLogger) Infof(s string, args ...interface{}) {
	cl.InfofCallback(s, args...)
}

func (cl callbackLogger) Warn(args ...interface{}) {
	cl.WarnCallback(args...)
}
func (cl callbackLogger) Warnf(s string, args ...interface{}) {
	cl.WarnfCallback(s, args...)
}

// TestMaliciousCheckServerResponseVariables test the checkServerResponseVariables to ensure it doesn't print the a malicious input without being filtered to the log file.
func TestMaliciousCheckServerResponseVariables(t *testing.T) {
	partitiontest.PartitionTest(t)

	wn := makeTestWebsocketNode(t)
	wn.GenesisID = "genesis-id1"
	wn.RandomID = "random-id1"
	wn.log = callbackLogger{
		Logger: wn.log,
		InfoCallback: func(args ...interface{}) {
			s := fmt.Sprint(args...)
			require.NotContains(t, s, "א")
		},
		InfofCallback: func(s string, args ...interface{}) {
			s = fmt.Sprintf(s, args...)
			require.NotContains(t, s, "א")
		},
		WarnCallback: func(args ...interface{}) {
			s := fmt.Sprint(args...)
			require.NotContains(t, s, "א")
		},
		WarnfCallback: func(s string, args ...interface{}) {
			s = fmt.Sprintf(s, args...)
			require.NotContains(t, s, "א")
		},
	}

	header1 := http.Header{}
	header1.Set(ProtocolVersionHeader, ProtocolVersion+"א")
	header1.Set(NodeRandomHeader, wn.RandomID+"tag")
	header1.Set(GenesisHeader, wn.GenesisID)
	responseVariableOk, matchingVersion := wn.checkServerResponseVariables(header1, "addressX")
	require.Equal(t, false, responseVariableOk)
	require.Equal(t, "", matchingVersion)

	header2 := http.Header{}
	header2.Set(ProtocolVersionHeader, ProtocolVersion)
	header2.Set("א", "א")
	header2.Set(GenesisHeader, wn.GenesisID)
	responseVariableOk, matchingVersion = wn.checkServerResponseVariables(header2, "addressX")
	require.Equal(t, false, responseVariableOk)
	require.Equal(t, "", matchingVersion)

	header3 := http.Header{}
	header3.Set(ProtocolVersionHeader, ProtocolVersion)
	header3.Set(NodeRandomHeader, wn.RandomID+"tag")
	header3.Set(GenesisHeader, wn.GenesisID+"א")
	responseVariableOk, matchingVersion = wn.checkServerResponseVariables(header3, "addressX")
	require.Equal(t, false, responseVariableOk)
	require.Equal(t, "", matchingVersion)
}

func BenchmarkVariableTransactionMessageBlockSizes(t *testing.B) {
	netA := makeTestWebsocketNode(t)
	netA.log.SetLevel(logging.Warn)
	netA.config.GossipFanout = 1
	netA.config.EnablePingHandler = false
	netA.Start()
	defer func() { netA.Stop() }()

	netB := makeTestWebsocketNode(t)
	netB.log.SetLevel(logging.Warn)
	netB.config.GossipFanout = 1
	netB.config.EnablePingHandler = false
	addrA, postListen := netA.Address()
	require.True(t, postListen)
	t.Log(addrA)
	netB.phonebook.ReplacePeerList([]string{addrA}, "default", PhoneBookEntryRelayRole)
	netB.Start()
	defer func() { netB.Stop() }()

	const txnSize = 250
	var msgProcessed chan struct{}

	msgHandlerA := func(msg IncomingMessage) (out OutgoingMessage) {
		// spend some time, linear to the size of the message -
		txnCount := len(msg.Data) / txnSize
		time.Sleep(time.Nanosecond * time.Duration(10000*txnCount))
		msgProcessed <- struct{}{}
		return
	}
	// register all the handlers.
	taggedHandlersA := []TaggedMessageHandler{
		{
			Tag:            protocol.TxnTag,
			MessageHandler: HandlerFunc(msgHandlerA),
		},
	}
	netA.ClearHandlers()
	netA.RegisterHandlers(taggedHandlersA)

	netB.ClearHandlers()

	readyTimeout := time.NewTimer(2 * time.Second)
	waitReady(t, netA, readyTimeout.C)
	waitReady(t, netB, readyTimeout.C)

	highestRate := float64(1)
	sinceHighestRate := 0
	rate := float64(0)
	for txnCount := 1; txnCount < 1024; {
		t.Run(fmt.Sprintf("%d-TxnPerMessage", txnCount), func(t *testing.B) {
			msgProcessed = make(chan struct{}, t.N/txnCount)
			dataBuffer := make([]byte, txnSize*txnCount)
			crypto.RandBytes(dataBuffer[:])
			t.ResetTimer()
			startTime := time.Now()
			for i := 0; i < t.N/txnCount; i++ {
				netB.Broadcast(context.Background(), protocol.TxnTag, dataBuffer, true, nil)
				<-msgProcessed
			}
			deltaTime := time.Since(startTime)
			rate = float64(t.N) * float64(time.Second) / float64(deltaTime)
			t.ReportMetric(rate, "txn/sec")
		})
		if rate > highestRate {
			highestRate = rate
			sinceHighestRate = 0
			txnCount += txnCount/10 + 1
			continue
		}
		sinceHighestRate++
		if sinceHighestRate > 4 {
			break
		}
		txnCount += txnCount/4 + 1
	}
}

func TestPreparePeerData(t *testing.T) {
	partitiontest.PartitionTest(t)

	// no compression
	req := broadcastRequest{
		tags: []protocol.Tag{protocol.AgreementVoteTag, protocol.ProposalPayloadTag},
		data: [][]byte{[]byte("test"), []byte("data")},
	}

	peers := []*wsPeer{}
	wn := WebsocketNetwork{}
	data, comp, digests, seenPrioPPTag := wn.broadcaster.preparePeerData(req, false, peers)
	require.NotEmpty(t, data)
	require.Empty(t, comp)
	require.NotEmpty(t, digests)
	require.Equal(t, len(req.data), len(digests))
	require.Equal(t, len(data), len(digests))
	require.False(t, seenPrioPPTag)

	for i := range data {
		require.Equal(t, append([]byte(req.tags[i]), req.data[i]...), data[i])
	}

	// compression
	peer1 := wsPeer{
		features: 0,
	}
	peer2 := wsPeer{
		features: pfCompressedProposal,
	}
	peers = []*wsPeer{&peer1, &peer2}
	data, comp, digests, seenPrioPPTag = wn.broadcaster.preparePeerData(req, true, peers)
	require.NotEmpty(t, data)
	require.NotEmpty(t, comp)
	require.NotEmpty(t, digests)
	require.Equal(t, len(req.data), len(digests))
	require.Equal(t, len(data), len(digests))
	require.Equal(t, len(comp), len(digests))
	require.True(t, seenPrioPPTag)

	for i := range data {
		require.Equal(t, append([]byte(req.tags[i]), req.data[i]...), data[i])
	}

	for i := range comp {
		if req.tags[i] != protocol.ProposalPayloadTag {
			require.Equal(t, append([]byte(req.tags[i]), req.data[i]...), comp[i])
			require.Equal(t, data[i], comp[i])
		} else {
			require.NotEqual(t, data[i], comp[i])
			require.Equal(t, append([]byte(req.tags[i]), zstdCompressionMagic[:]...), comp[i][:len(req.tags[i])+len(zstdCompressionMagic)])
		}
	}
}

func TestWebsocketNetworkTelemetryTCP(t *testing.T) {
	partitiontest.PartitionTest(t)

	if strings.ToUpper(os.Getenv("CIRCLECI")) == "TRUE" {
		t.Skip("Flaky on CIRCLECI")
	}

	// start two networks and send 2 messages from A to B
	closed := false
	netA, netB, counter, closeFunc := setupWebsocketNetworkAB(t, 2)
	defer func() {
		if !closed {
			closeFunc()
		}
	}()
	counterDone := counter.done
	netA.Broadcast(context.Background(), protocol.TxnTag, []byte("foo"), false, nil)
	netA.Broadcast(context.Background(), protocol.TxnTag, []byte("bar"), false, nil)

	select {
	case <-counterDone:
	case <-time.After(2 * time.Second):
		t.Errorf("timeout, count=%d, wanted 2", counter.count)
	}

	// get RTT from both ends and assert nonzero
	var peersA, peersB []*wsPeer
	peersA, _ = netA.peerSnapshot(peersA)
	detailsA := netA.getPeerConnectionTelemetryDetails(time.Now(), peersA)
	peersB, _ = netB.peerSnapshot(peersB)
	detailsB := netB.getPeerConnectionTelemetryDetails(time.Now(), peersB)
	require.Len(t, detailsA.IncomingPeers, 1)
	assert.NotZero(t, detailsA.IncomingPeers[0].TCP.RTT)
	require.Len(t, detailsB.OutgoingPeers, 1)
	assert.NotZero(t, detailsB.OutgoingPeers[0].TCP.RTT)

	pcdA, err := json.Marshal(detailsA)
	assert.NoError(t, err)
	pcdB, err := json.Marshal(detailsB)
	assert.NoError(t, err)
	t.Log("detailsA", string(pcdA))
	t.Log("detailsB", string(pcdB))

	// close connections
	closeFunc()
	closed = true
	// open more FDs by starting 2 more networks
	_, _, _, closeFunc2 := setupWebsocketNetworkAB(t, 2)
	defer closeFunc2()
	//  use stale peers snapshot from closed networks to get telemetry
	// *net.OpError "use of closed network connection" err results in 0 rtt values
	detailsA = netA.getPeerConnectionTelemetryDetails(time.Now(), peersA)
	detailsB = netB.getPeerConnectionTelemetryDetails(time.Now(), peersB)
	require.Len(t, detailsA.IncomingPeers, 1)
	assert.Zero(t, detailsA.IncomingPeers[0].TCP.RTT)
	require.Len(t, detailsB.OutgoingPeers, 1)
	assert.Zero(t, detailsB.OutgoingPeers[0].TCP.RTT)

	pcdA, err = json.Marshal(detailsA)
	assert.NoError(t, err)
	pcdB, err = json.Marshal(detailsB)
	assert.NoError(t, err)
	t.Log("closed detailsA", string(pcdA))
	t.Log("closed detailsB", string(pcdB))
}

type mockServer struct {
	*httptest.Server
	URL string

	waitForClientClose bool
}

type mockHandler struct {
	*testing.T
	s *mockServer
}

var mockUpgrader = websocket.Upgrader{
	ReadBufferSize:    1024,
	WriteBufferSize:   1024,
	EnableCompression: true,
	Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
		http.Error(w, reason.Error(), status)
	},
}

func buildWsResponseHeader() http.Header {
	h := http.Header{}
	h.Add(ProtocolVersionHeader, ProtocolVersion)
	h.Add(GenesisHeader, genesisID)
	h.Add(NodeRandomHeader, "randomHeader")
	return h
}

func (t mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Set the required headers to successfully establish a connection
	ws, err := mockUpgrader.Upgrade(w, r, buildWsResponseHeader())
	if err != nil {
		t.Logf("Upgrade: %v", err)
		return
	}
	defer ws.Close()
	// Send a message of interest immediately after the connection is established
	wr, err := ws.NextWriter(websocket.BinaryMessage)
	if err != nil {
		t.Logf("NextWriter: %v", err)
		return
	}

	bytes := MarshallMessageOfInterest([]protocol.Tag{protocol.AgreementVoteTag})
	msgBytes := append([]byte(protocol.MsgOfInterestTag), bytes...)
	_, err = wr.Write(msgBytes)
	if err != nil {
		t.Logf("Error writing MessageOfInterest: %v", err)
		return
	}
	wr.Close()

	for {
		// echo a message back to the client
		_, _, err := ws.NextReader()
		if err != nil {
			if _, ok := err.(*websocket.CloseError); ok && t.s.waitForClientClose {
				t.Log("got client close")
				return
			}
			return
		}
	}
}

func makeWsProto(s string) string {
	return "ws" + strings.TrimPrefix(s, "http")
}

func newServer(t *testing.T) *mockServer {
	var s mockServer
	s.Server = httptest.NewServer(mockHandler{t, &s})
	s.Server.URL += ""
	s.URL = makeWsProto(s.Server.URL)
	return &s
}

func TestMaxHeaderSize(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
	netA.config.GossipFanout = 1

	netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
	netB.config.GossipFanout = 1

	netA.Start()
	defer netA.Stop()
	netB.Start()
	defer netB.Stop()

	addrB, ok := netB.Address()
	require.True(t, ok)
	gossipB, err := netB.addrToGossipAddr(addrB)
	require.NoError(t, err)

	// First make sure that the regular connection with default max header size works
	netA.wsMaxHeaderBytes = wsMaxHeaderBytes
	netA.wg.Add(1)
	netA.tryConnect(addrB, gossipB)
	require.Eventually(t, func() bool { return netA.NumPeers() == 1 }, 500*time.Millisecond, 25*time.Millisecond)

	netA.removePeer(netA.peers[0], disconnectReasonNone)
	assert.Zero(t, len(netA.peers))

	// Now try to connect with a max header size that is too small
	logBuffer := bytes.NewBuffer(nil)
	netA.log.SetOutput(logBuffer)

	netA.wsMaxHeaderBytes = 128
	netA.wg.Add(1)
	netA.tryConnect(addrB, gossipB)
	lg := logBuffer.String()
	logBuffer.Reset()
	time.Sleep(250 * time.Millisecond)
	assert.Contains(t, lg, fmt.Sprintf("ws connect(%s) fail:", gossipB))
	assert.Zero(t, len(netA.peers))

	// Test that setting 0 disables the max header size check
	netA.wsMaxHeaderBytes = 0
	netA.wg.Add(1)
	netA.tryConnect(addrB, gossipB)
	require.Eventually(t, func() bool { return netA.NumPeers() == 1 }, 500*time.Millisecond, 25*time.Millisecond)
}

func TestTryConnectEarlyWrite(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
	netA.config.GossipFanout = 1

	s := newServer(t)
	s.waitForClientClose = true
	defer s.Close()

	netA.Start()
	defer netA.Stop()

	dialer := websocket.Dialer{}
	mconn, resp, _ := dialer.Dial(s.URL, nil)
	expectedHeader := buildWsResponseHeader()
	for k, v := range expectedHeader {
		assert.Equal(t, v[0], resp.Header.Get(k))
	}

	// Fixed overhead of the full status line "HTTP/1.1 101 Switching Protocols" (32) + 4 bytes for two instance of CRLF
	// one after the status line and one to separate headers from the body
	minValidHeaderSize := 36
	for k, v := range resp.Header {
		minValidHeaderSize += len(k) + len(v[0]) + 4 // + 4 is for the ": " and CRLF
	}
	mconn.Close()

	// Setting the max header size to 1 byte less than the minimum header size should fail
	netA.wsMaxHeaderBytes = int64(minValidHeaderSize) - 1
	netA.wg.Add(1)
	netA.tryConnect(s.URL, s.URL)
	time.Sleep(250 * time.Millisecond)
	assert.Len(t, netA.peers, 0)

	// Now set the max header size to the minimum header size and it should succeed
	netA.wsMaxHeaderBytes = int64(minValidHeaderSize)
	netA.wg.Add(1)
	netA.tryConnect(s.URL, s.URL)
	p := netA.peers[0]
	var messageCount uint64
	for x := 0; x < 1000; x++ {
		messageCount = p.miMessageCount.Load()
		if messageCount == 1 {
			break
		}
		time.Sleep(2 * time.Millisecond)
	}

	// Confirm that we successfuly received a message of interest
	assert.Len(t, netA.peers, 1)
	fmt.Printf("MI Message Count: %v\n", netA.peers[0].miMessageCount.Load())
	assert.Equal(t, uint64(1), netA.peers[0].miMessageCount.Load())
}

// Test functionality that allows a node to discard a block response that it did not request or that arrived too late.
// Both cases are tested here by having A send unexpected, late responses to nodes B and C respectively.
func TestDiscardUnrequestedBlockResponse(t *testing.T) {
	partitiontest.PartitionTest(t)

	netA := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netA"})
	netA.config.GossipFanout = 1

	netB := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netB"})
	netB.config.GossipFanout = 1

	netC := makeTestWebsocketNode(t, testWebsocketLogNameOption{"netC"})
	netC.config.GossipFanout = 1

	netA.Start()
	defer netA.Stop()
	netB.Start()
	defer netB.Stop()

	addrB, ok := netB.Address()
	require.True(t, ok)
	gossipB, err := netB.addrToGossipAddr(addrB)
	require.NoError(t, err)

	netA.wg.Add(1)
	netA.tryConnect(addrB, gossipB)
	require.Eventually(t, func() bool { return netA.NumPeers() == 1 }, 500*time.Millisecond, 25*time.Millisecond)

	// send an unrequested block response
	msg := make([]sendMessage, 1)
	msg[0] = sendMessage{
		data:         append([]byte(protocol.TopicMsgRespTag), []byte("foo")...),
		enqueued:     time.Now(),
		peerEnqueued: time.Now(),
		ctx:          context.Background(),
	}
	netA.peers[0].sendBufferBulk <- sendMessages{msgs: msg}
	require.Eventually(t,
		func() bool {
			return networkConnectionsDroppedTotal.GetUint64ValueForLabels(map[string]string{"reason": "unrequestedTS"}) == 1
		},
		1*time.Second,
		50*time.Millisecond,
	)

	// Stop and confirm that we hit the case of disconnecting a peer for sending an unrequested block response
	require.Zero(t, netB.NumPeers())

	netC.Start()
	defer netC.Stop()

	addrC, ok := netC.Address()
	require.True(t, ok)
	gossipC, err := netC.addrToGossipAddr(addrC)
	require.NoError(t, err)

	netA.wg.Add(1)
	netA.tryConnect(addrC, gossipC)
	require.Eventually(t, func() bool { return netA.NumPeers() == 1 }, 500*time.Millisecond, 25*time.Millisecond)

	ctx, cancel := context.WithCancel(context.Background())
	topics := Topics{
		MakeTopic("requestDataType",
			[]byte("a")),
		MakeTopic(
			"blockData",
			[]byte("b")),
	}
	// Send a request for a block and cancel it after the handler has been registered
	go func() {
		netC.peers[0].Request(ctx, protocol.UniEnsBlockReqTag, topics)
	}()
	require.Eventually(
		t,
		func() bool {
			netC.peersLock.RLock()
			defer netC.peersLock.RUnlock()
			require.NotEmpty(t, netC.peers)
			return netC.peers[0].lenResponseChannels() > 0
		},
		1*time.Second,
		50*time.Millisecond,
	)
	cancel()

	// confirm that the request was cancelled but that we have registered that we have sent a request
	require.Eventually(
		t,
		func() bool { return netC.peers[0].lenResponseChannels() == 0 },
		500*time.Millisecond,
		20*time.Millisecond,
	)
	require.Equal(t, netC.peers[0].outstandingTopicRequests.Load(), int64(1))

	// Create a buffer to monitor log output from netC
	logBuffer := bytes.NewBuffer(nil)
	netC.log.SetOutput(logBuffer)

	// send a late TS response from A -> C
	netA.peers[0].sendBufferBulk <- sendMessages{msgs: msg}
	require.Eventually(
		t,
		func() bool { return netC.peers[0].outstandingTopicRequests.Load() == int64(0) },
		500*time.Millisecond,
		20*time.Millisecond,
	)

	// Stop and confirm that we hit the case of disconnecting a peer for sending a stale block response
	netC.Stop()
	lg := logBuffer.String()
	require.Contains(t, lg, "wsPeer readLoop: received a TS response for a stale request ")
}

func customNetworkIDGen(networkID protocol.NetworkID) *rapid.Generator[protocol.NetworkID] {
	return rapid.Custom(func(t *rapid.T) protocol.NetworkID {
		// Unused/satisfying rapid requirement
		rapid.String().Draw(t, "networkIDGen")
		return networkID
	})
}

// The hardcoded network IDs just make testing this function more difficult with no confidence gain (the custom logic
// is already exercised well in the dnsbootstrap parsing tests).
func nonHardcodedNetworkIDGen() *rapid.Generator[protocol.NetworkID] {
	return rapid.OneOf(customNetworkIDGen(config.Testnet), customNetworkIDGen(config.Mainnet),
		customNetworkIDGen(config.Devtestnet))
}

/*
Basic exercise of the refreshRelayArchivePhonebookAddresses function, uses base / expected cases, relying  on neighboring
unit tests to cover the merge and phonebook update logic.
*/
func TestRefreshRelayArchivePhonebookAddresses(t *testing.T) {
	partitiontest.PartitionTest(t)
	var netA *WebsocketNetwork
	var refreshRelayDNSBootstrapID = "<network>.algorand.network?backup=<network>.algorand.net&dedup=<name>.algorand-<network>.(network|net)"

	refreshTestConf := defaultConfig

	rapid.Check(t, func(t1 *rapid.T) {
		refreshTestConf.DNSBootstrapID = refreshRelayDNSBootstrapID
		netA = makeTestWebsocketNodeWithConfig(t, refreshTestConf)
		netA.NetworkID = nonHardcodedNetworkIDGen().Draw(t1, "network")

		primarySRVBootstrap := strings.Replace("<network>.algorand.network", "<network>", string(netA.NetworkID), -1)
		backupSRVBootstrap := strings.Replace("<network>.algorand.net", "<network>", string(netA.NetworkID), -1)
		var primaryRelayResolvedRecords []string
		var secondaryRelayResolvedRecords []string
		var primaryArchiveResolvedRecords []string
		var secondaryArchiveResolvedRecords []string

		for _, record := range []string{"r1.algorand-<network>.network",
			"r2.algorand-<network>.network", "r3.algorand-<network>.network"} {
			var recordSub = strings.Replace(record, "<network>", string(netA.NetworkID), -1)
			primaryRelayResolvedRecords = append(primaryRelayResolvedRecords, recordSub)
			secondaryRelayResolvedRecords = append(secondaryRelayResolvedRecords, strings.Replace(recordSub, "network", "net", -1))
		}

		for _, record := range []string{"r1archive.algorand-<network>.network",
			"r2archive.algorand-<network>.network", "r3archive.algorand-<network>.network"} {
			var recordSub = strings.Replace(record, "<network>", string(netA.NetworkID), -1)
			primaryArchiveResolvedRecords = append(primaryArchiveResolvedRecords, recordSub)
			secondaryArchiveResolvedRecords = append(secondaryArchiveResolvedRecords, strings.Replace(recordSub, "network", "net", -1))
		}

		// Mock the SRV record lookup
		netA.resolveSRVRecords = func(ctx context.Context, service string, protocol string, name string, fallbackDNSResolverAddress string,
			secure bool) (addrs []string, err error) {
			if service == "algobootstrap" && protocol == "tcp" && name == primarySRVBootstrap {
				return primaryRelayResolvedRecords, nil
			} else if service == "algobootstrap" && protocol == "tcp" && name == backupSRVBootstrap {
				return secondaryRelayResolvedRecords, nil
			}

			if service == "archive" && protocol == "tcp" && name == primarySRVBootstrap {
				return primaryArchiveResolvedRecords, nil
			} else if service == "archive" && protocol == "tcp" && name == backupSRVBootstrap {
				return secondaryArchiveResolvedRecords, nil
			}

			return
		}

		relayPeers := netA.GetPeers(PeersPhonebookRelays)
		assert.Equal(t, 0, len(relayPeers))

		archivePeers := netA.GetPeers(PeersPhonebookArchivers)
		assert.Equal(t, 0, len(archivePeers))

		netA.refreshRelayArchivePhonebookAddresses()

		relayPeers = netA.GetPeers(PeersPhonebookRelays)

		assert.Equal(t, 3, len(relayPeers))
		relayAddrs := make([]string, 0, len(relayPeers))
		for _, peer := range relayPeers {
			relayAddrs = append(relayAddrs, peer.(HTTPPeer).GetAddress())
		}

		assert.ElementsMatch(t, primaryRelayResolvedRecords, relayAddrs)

		archivePeers = netA.GetPeers(PeersPhonebookArchivers)

		// TODO: For the time being, we do not dedup resolved archive nodes
		assert.Equal(t, len(primaryArchiveResolvedRecords)+len(secondaryArchiveResolvedRecords), len(archivePeers))

		archiveAddrs := make([]string, 0, len(archivePeers))
		for _, peer := range archivePeers {
			archiveAddrs = append(archiveAddrs, peer.(HTTPPeer).GetAddress())
		}

		assert.ElementsMatch(t, append(primaryArchiveResolvedRecords, secondaryArchiveResolvedRecords...), archiveAddrs)

	})
}

/*
Exercises the updatePhonebookAddresses function, notably with different variations of valid relay and
archival addresses.
*/
func TestUpdatePhonebookAddresses(t *testing.T) {
	partitiontest.PartitionTest(t)
	var netA *WebsocketNetwork

	rapid.Check(t, func(t1 *rapid.T) {
		netA = makeTestWebsocketNode(t)
		relayPeers := netA.GetPeers(PeersPhonebookRelays)
		assert.Equal(t, 0, len(relayPeers))

		archivePeers := netA.GetPeers(PeersPhonebookArchivers)
		assert.Equal(t, 0, len(archivePeers))

		domainGen := rapidgen.Domain()

		// Generate between 0 and N examples - if no dups, should end up in phonebook
		relayDomainsGen := rapid.SliceOfN(domainGen, 0, 200)

		relayDomains := relayDomainsGen.Draw(t1, "relayDomains")

		// Dont overlap with relays, duplicates between them not stored in phonebook as of this writing
		archiveDomainsGen := rapid.SliceOfN(rapidgen.DomainOf(253, 63, "", relayDomains), 0, 200)
		archiveDomains := archiveDomainsGen.Draw(t1, "archiveDomains")
		netA.updatePhonebookAddresses(relayDomains, archiveDomains)

		// Check that entries are in fact in phonebook less any duplicates
		dedupedRelayDomains := removeDuplicateStr(relayDomains, false)
		dedupedArchiveDomains := removeDuplicateStr(archiveDomains, false)

		relayPeers = netA.GetPeers(PeersPhonebookRelays)
		assert.Equal(t, len(dedupedRelayDomains), len(relayPeers))

		relayAddrs := make([]string, 0, len(relayPeers))
		for _, peer := range relayPeers {
			relayAddrs = append(relayAddrs, peer.(HTTPPeer).GetAddress())
		}

		assert.ElementsMatch(t, dedupedRelayDomains, relayAddrs)

		archivePeers = netA.GetPeers(PeersPhonebookArchivers)
		assert.Equal(t, len(dedupedArchiveDomains), len(archivePeers))

		archiveAddrs := make([]string, 0, len(archivePeers))
		for _, peer := range archivePeers {
			archiveAddrs = append(archiveAddrs, peer.(HTTPPeer).GetAddress())
		}

		assert.ElementsMatch(t, dedupedArchiveDomains, archiveAddrs)

		// Generate fresh set of addresses with a duplicate from original batch if warranted,
		// assert phonebook reflects fresh list / prior peers other than selected duplicate
		// are not present
		var priorRelayDomains = relayDomains

		// Dont overlap with archive nodes previously specified, duplicates between them not stored in phonebook as of this writing
		relayDomainsGen = rapid.SliceOfN(rapidgen.DomainOf(253, 63, "", archiveDomains), 0, 200)
		relayDomains = relayDomainsGen.Draw(t1, "relayDomains")

		// Randomly select a prior relay domain
		if len(priorRelayDomains) > 0 {
			priorIdx := rapid.IntRange(0, len(priorRelayDomains)-1).Draw(t1, "")
			relayDomains = append(relayDomains, priorRelayDomains[priorIdx])
		}

		netA.updatePhonebookAddresses(relayDomains, nil)

		// Check that entries are in fact in phonebook less any duplicates
		dedupedRelayDomains = removeDuplicateStr(relayDomains, false)

		relayPeers = netA.GetPeers(PeersPhonebookRelays)
		assert.Equal(t, len(dedupedRelayDomains), len(relayPeers))

		relayAddrs = nil
		for _, peer := range relayPeers {
			relayAddrs = append(relayAddrs, peer.(HTTPPeer).GetAddress())
		}

		assert.ElementsMatch(t, dedupedRelayDomains, relayAddrs)

		archivePeers = netA.GetPeers(PeersPhonebookArchivers)
		assert.Equal(t, len(dedupedArchiveDomains), len(archivePeers))

		archiveAddrs = nil
		for _, peer := range archivePeers {
			archiveAddrs = append(archiveAddrs, peer.(HTTPPeer).GetAddress())
		}

		assert.ElementsMatch(t, dedupedArchiveDomains, archiveAddrs)
	})
}

func removeDuplicateStr(strSlice []string, lowerCase bool) []string {
	allKeys := make(map[string]bool)
	var dedupStrSlice = make([]string, 0)
	for _, item := range strSlice {
		if lowerCase {
			item = strings.ToLower(item)
		}
		if _, exists := allKeys[item]; !exists {
			allKeys[item] = true
			dedupStrSlice = append(dedupStrSlice, item)
		}
	}
	return dedupStrSlice
}

func replaceAllIn(strSlice []string, strToReplace string, newStr string) []string {
	var subbedStrSlice = make([]string, 0)
	for _, item := range strSlice {
		item = strings.ReplaceAll(item, strToReplace, newStr)
		subbedStrSlice = append(subbedStrSlice, item)
	}

	return subbedStrSlice
}

func supportedNetworkGen() *rapid.Generator[string] {
	return rapid.OneOf(rapid.StringMatching(string(config.Testnet)), rapid.StringMatching(string(config.Mainnet)),
		rapid.StringMatching(string(config.Devnet)), rapid.StringMatching(string(config.Betanet)),
		rapid.StringMatching(string(config.Alphanet)), rapid.StringMatching(string(config.Devtestnet)))
}

func TestMergePrimarySecondaryRelayAddressListsMinOverlap(t *testing.T) {
	partitiontest.PartitionTest(t)
	var netA *WebsocketNetwork

	rapid.Check(t, func(t1 *rapid.T) {
		netA = makeTestWebsocketNode(t)

		network := supportedNetworkGen().Draw(t1, "network")
		dedupExp := regexp.MustCompile(strings.Replace(
			`(algorand-<network>.(network|net))`, "<network>", network, -1))
		domainPortGen := rapidgen.DomainWithPort()

		// Generate between 0 and N examples - if no dups, should end up in phonebook
		domainsGen := rapid.SliceOfN(domainPortGen, 0, 200)

		primaryRelayAddresses := domainsGen.Draw(t1, "primaryRelayAddresses")
		secondaryRelayAddresses := domainsGen.Draw(t1, "secondaryRelayAddresses")

		mergedRelayAddresses := netA.mergePrimarySecondaryRelayAddressSlices(protocol.NetworkID(network),
			primaryRelayAddresses, secondaryRelayAddresses, dedupExp)

		expectedRelayAddresses := removeDuplicateStr(append(primaryRelayAddresses, secondaryRelayAddresses...), true)

		assert.ElementsMatch(t, expectedRelayAddresses, mergedRelayAddresses)
	})
}

func alphaNumStr(n int) string {
	var chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321")
	str := make([]rune, n)
	for i := range str {
		str[i] = chars[rand.Intn(len(chars))]
	}
	return string(str)
}

func TestMergePrimarySecondaryRelayAddressListsPartialOverlap(t *testing.T) {
	partitiontest.PartitionTest(t)

	networks := []protocol.NetworkID{config.Testnet, config.Mainnet, config.Devnet, config.Betanet,
		config.Alphanet, config.Devtestnet}
	var netA *WebsocketNetwork

	for _, network := range networks {
		dedupExp := regexp.MustCompile(strings.Replace(
			"(algorand-<network>.(network|net))", "<network>", string(network), -1))
		primaryRelayAddresses := make([]string, 0)
		secondaryRelayAddresses := make([]string, 0)
		extraSecondaryRelayAddresses := make([]string, 0)
		for i := 0; i < 100; i++ {
			relayID := alphaNumStr(2)
			primaryRelayAddresses = append(primaryRelayAddresses, fmt.Sprintf("r-%s.algorand-%s.network",
				relayID, network))
			secondaryRelayAddresses = append(secondaryRelayAddresses, fmt.Sprintf("r-%s.algorand-%s.net",
				relayID, network))
		}
		for i := 0; i < 20; i++ {
			relayID := alphaNumStr(2) + "-" + alphaNumStr(1)
			primaryRelayAddresses = append(primaryRelayAddresses, fmt.Sprintf("relay-%s.algorand-%s.network",
				relayID, network))
			secondaryRelayAddresses = append(secondaryRelayAddresses, fmt.Sprintf("relay-%s.algorand-%s.net",
				relayID, network))
		}
		// Add additional secondary ones that intentionally do not duplicate primary ones
		for i := 0; i < 10; i++ {
			relayID := alphaNumStr(2) + "-" + alphaNumStr(1)
			extraSecondaryRelayAddresses = append(extraSecondaryRelayAddresses, fmt.Sprintf("noduprelay-%s.algorand-%s.net",
				relayID, network))
		}
		secondaryRelayAddresses = append(secondaryRelayAddresses, extraSecondaryRelayAddresses...)

		mergedRelayAddresses := netA.mergePrimarySecondaryRelayAddressSlices(network,
			primaryRelayAddresses, secondaryRelayAddresses, dedupExp)

		// We expect the primary addresses to take precedence over a "matching" secondary address, extra non-duplicate
		// secondary addresses should be present in the merged slice
		expectedRelayAddresses := removeDuplicateStr(append(primaryRelayAddresses, extraSecondaryRelayAddresses...), true)

		assert.ElementsMatch(t, expectedRelayAddresses, mergedRelayAddresses)
	}

}

// Case where a "backup" network is specified, but no dedup expression is provided. Technically possible,
// but there is little benefit vs specifying them as separate `;` separated addresses in DNSBootrstrapID.
func TestMergePrimarySecondaryRelayAddressListsNoDedupExp(t *testing.T) {
	partitiontest.PartitionTest(t)
	var netA *WebsocketNetwork

	rapid.Check(t, func(t1 *rapid.T) {
		netA = makeTestWebsocketNode(t)

		network := supportedNetworkGen().Draw(t1, "network")
		primaryDomainSuffix := strings.Replace(
			`algorand-<network>.net`, "<network>", network, -1)

		// Generate hosts for a primary network domain
		primaryNetworkDomainGen := rapidgen.DomainWithSuffixAndPort(primaryDomainSuffix, nil)
		primaryDomainsGen := rapid.SliceOfN(primaryNetworkDomainGen, 0, 200)

		primaryRelayAddresses := primaryDomainsGen.Draw(t1, "primaryRelayAddresses")

		secondaryDomainSuffix := strings.Replace(
			`algorand-<network>.network`, "<network>", network, -1)
		// Generate these addresses from primary ones, find/replace domain suffix appropriately
		secondaryRelayAddresses := replaceAllIn(primaryRelayAddresses, primaryDomainSuffix, secondaryDomainSuffix)
		// Add some generated addresses to secondary list - to simplify verification further down
		// (substituting suffixes, etc), we don't want the generated addresses to duplicate any of
		// the replaced secondary ones
		secondaryNetworkDomainGen := rapidgen.DomainWithSuffixAndPort(secondaryDomainSuffix, secondaryRelayAddresses)
		secondaryDomainsGen := rapid.SliceOfN(secondaryNetworkDomainGen, 0, 200)
		generatedSecondaryRelayAddresses := secondaryDomainsGen.Draw(t1, "secondaryRelayAddresses")
		secondaryRelayAddresses = append(secondaryRelayAddresses, generatedSecondaryRelayAddresses...)

		mergedRelayAddresses := netA.mergePrimarySecondaryRelayAddressSlices(protocol.NetworkID(network),
			primaryRelayAddresses, secondaryRelayAddresses, nil)

		// We expect non deduplication, so all addresses _should_ be present (note that no lower casing happens either)
		expectedRelayAddresses := append(primaryRelayAddresses, secondaryRelayAddresses...)

		assert.ElementsMatch(t, expectedRelayAddresses, mergedRelayAddresses)
	})
}

// TestSendMessageCallbacks tests that the SendMessage callbacks are called correctly. These are currently used for
// decrementing the number of bytes considered currently in flight for blockservice memcaps.
func TestSendMessageCallbacks(t *testing.T) {
	partitiontest.PartitionTest(t)
	netA, netB, _, closeFunc := setupWebsocketNetworkAB(t, 2)
	defer closeFunc()

	var counter atomic.Uint64
	require.NotZero(t, netA.NumPeers())

	// peerB is netA's representation of netB and vice versa
	peerB := netA.peers[0]
	peerA := netB.peers[0]

	// Need to create a channel so that TS messages sent by netA don't get filtered out in the readLoop
	peerA.makeResponseChannel(1)

	// The for loop simulates netA receiving 100 UE block requests from netB
	// and goes through the actual response code path to generate and send TS responses to netB
	for i := 0; i < 100; i++ {
		randInt := crypto.RandUint64()%(128) + 1
		counter.Add(randInt)
		topic := MakeTopic("val", []byte("blah"))
		callback := func() {
			counter.Add(^uint64(randInt - 1))
		}
		msg := IncomingMessage{Sender: peerB, Tag: protocol.UniEnsBlockReqTag}
		peerB.Respond(context.Background(), msg, OutgoingMessage{OnRelease: callback, Topics: Topics{topic}})
	}
	// Confirm that netB's representation netA peerB has received some requests and decremented the counter
	// of outstanding TS requests below 0. This will be true because we never made any UE block requests, we only
	// simulated them by manually creating a IncomingMessage with the UE tag in the loop above
	require.Eventually(t,
		func() bool { return peerA.outstandingTopicRequests.Load() < 0 },
		500*time.Millisecond,
		25*time.Millisecond,
	)

	// confirm that the test counter decrements down to zero correctly through callbacks
	require.Eventually(t,
		func() bool { return counter.Load() == uint64(0) },
		500*time.Millisecond,
		25*time.Millisecond,
	)
}

func TestSendMessageCallbackDrain(t *testing.T) {
	partitiontest.PartitionTest(t)

	node := makeTestWebsocketNode(t)
	destPeer := wsPeer{
		closing:            make(chan struct{}),
		sendBufferHighPrio: make(chan sendMessages, sendBufferLength),
		sendBufferBulk:     make(chan sendMessages, sendBufferLength),
		conn:               &nopConnSingleton,
	}
	node.addPeer(&destPeer)
	node.Start()
	defer node.Stop()

	var target, counter uint64
	// send messages to the peer that won't read them so they will sit in the sendQueue
	for i := 0; i < 10; i++ {
		randInt := crypto.RandUint64()%(128) + 1
		target += randInt
		topic := MakeTopic("val", []byte("blah"))
		callback := func() {
			counter += randInt
		}
		msg := IncomingMessage{Sender: node.peers[0], Tag: protocol.UniEnsBlockReqTag}
		destPeer.Respond(context.Background(), msg, OutgoingMessage{OnRelease: callback, Topics: Topics{topic}})
	}
	require.Len(t, destPeer.sendBufferBulk, 10)
	require.Zero(t, counter)
	require.Positive(t, target)
	// close the peer to trigger draining of the queue callbacks
	destPeer.Close(time.Now().Add(time.Second))

	require.Eventually(t,
		func() bool { return target == counter },
		2*time.Second,
		50*time.Millisecond,
	)
}