package octez-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file gossipsub_automaton.ml

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
(*****************************************************************************)
(*                                                                           *)
(* SPDX-License-Identifier: MIT                                              *)
(* Copyright (c) 2023 Nomadic Labs. <contact@nomadic-labs.com>               *)
(*                                                                           *)
(*****************************************************************************)

(* FIXME https://gitlab.com/tezos/tezos/-/issues/4966

   We should decide whether we want to implement a tracing mechanism
   as in the go implementation.
*)

open Gossipsub_intf

module Make (C : AUTOMATON_CONFIG) :
  AUTOMATON
    with type Time.t = C.Time.t
     and module Span = C.Span
     and module Peer = C.Subconfig.Peer
     and module Topic = C.Subconfig.Topic
     and module Message_id = C.Subconfig.Message_id
     and module Message = C.Subconfig.Message = struct
  module Peer = C.Subconfig.Peer
  module Topic = C.Subconfig.Topic
  module Message_id = C.Subconfig.Message_id
  module Message = C.Subconfig.Message
  module Span = C.Span
  module Time = C.Time
  module Score = Peers_score.Make (Span) (Time) (Topic)

  type message = Message.t

  type time = Time.t

  type span = Time.span

  type nonrec limits = (Topic.t, Peer.t, Message_id.t, span) limits

  type nonrec parameters = (Peer.t, Message_id.t) parameters

  type add_peer = {direct : bool; outbound : bool; peer : Peer.t}

  type remove_peer = {peer : Peer.t}

  type ihave = {peer : Peer.t; topic : Topic.t; message_ids : Message_id.t list}

  type iwant = {peer : Peer.t; message_ids : Message_id.t list}

  type graft = {peer : Peer.t; topic : Topic.t}

  type prune = {
    peer : Peer.t;
    topic : Topic.t;
    px : Peer.t Seq.t;
    backoff : span;
  }

  type publish_message = {
    topic : Topic.t;
    message_id : Message_id.t;
    message : message;
  }

  type receive_message = {
    sender : Peer.t;
    topic : Topic.t;
    message_id : Message_id.t;
    message : message;
  }

  type join = {topic : Topic.t}

  type leave = {topic : Topic.t}

  type subscribe = {topic : Topic.t; peer : Peer.t}

  type unsubscribe = {topic : Topic.t; peer : Peer.t}

  type set_application_score = {peer : Peer.t; score : float}

  (* FIXME not sure subtyping for output is useful. If it is, it is
     probably for few ouputs and could be removed. *)
  type _ output =
    | Ihave_from_peer_with_low_score : {
        score : Score.t;
        threshold : float;
      }
        -> [`IHave] output
    | Too_many_recv_ihave_messages : {count : int; max : int} -> [`IHave] output
    | Too_many_sent_iwant_messages : {count : int; max : int} -> [`IHave] output
    | Message_topic_not_tracked : [`IHave] output
    | Message_requested_message_ids : Message_id.t list -> [`IHave] output
    | Invalid_message_id : [`IHave] output
    | Iwant_from_peer_with_low_score : {
        score : Score.t;
        threshold : float;
      }
        -> [`IWant] output
    | On_iwant_messages_to_route : {
        routed_message_ids :
          [`Ignored | `Not_found | `Too_many_requests | `Message of message]
          Message_id.Map.t;
      }
        -> [`IWant] output
    | Peer_filtered : [`Graft] output
    | Unsubscribed_topic : [`Graft] output
    | Peer_already_in_mesh : [`Graft] output
    | Grafting_direct_peer : [`Graft] output
    | Unexpected_grafting_peer : [`Graft] output
    | Grafting_peer_with_negative_score : [`Graft] output
    | Grafting_successfully : [`Graft] output
    | Peer_backed_off : [`Graft] output
    | Mesh_full : [`Graft] output
    | Prune_topic_not_tracked : [`Prune] output
    | Peer_not_in_mesh : [`Prune] output
    | Ignore_PX_score_too_low : Score.t -> [`Prune] output
    | No_PX : [`Prune] output
    | PX : Peer.Set.t -> [`Prune] output
    | Publish_message : {to_publish : Peer.Set.t} -> [`Publish_message] output
    | Already_published : [`Publish_message] output
    | Route_message : {to_route : Peer.Set.t} -> [`Receive_message] output
    | Already_received : [`Receive_message] output
    | Not_subscribed : [`Receive_message] output
    | Invalid_message : [`Receive_message] output
    | Unknown_validity : [`Receive_message] output
    | Already_joined : [`Join] output
    | Joining_topic : {to_graft : Peer.Set.t} -> [`Join] output
    | Not_joined : [`Leave] output
    | Leaving_topic : {
        to_prune : Peer.Set.t;
        noPX_peers : Peer.Set.t;
      }
        -> [`Leave] output
    | Heartbeat : {
        to_graft : Topic.Set.t Peer.Map.t;
        to_prune : Topic.Set.t Peer.Map.t;
        noPX_peers : Peer.Set.t;
      }
        -> [`Heartbeat] output
    | Peer_added : [`Add_peer] output
    | Peer_already_known : [`Add_peer] output
    | Removing_peer : [`Remove_peer] output
    | Subscribed : [`Subscribe] output
    | Subscribe_to_unknown_peer : [`Subscribe] output
    | Unsubscribed : [`Unsubscribe] output
    | Unsubscribe_from_unknown_peer : [`Unsubscribe] output
    | Set_application_score : [`Set_application_score] output

  type connection = {
    topics : Topic.Set.t;  (** The set of topics the peer subscribed to. *)
    direct : bool;
        (** A direct (aka explicit) connection is a connection to which we
            forward all the messages. *)
    outbound : bool;
        (** Intuitively, an outbound connection is a connection we
            initiated. But, the application layer can refine, relax or redefine
            this notion to fit its needs. *)
  }

  (** [Connections] implements a bidirectional map from peers to connections and from
      topics to peers.

      Invariant:
        forall (c : Connections.t),
        forall (p : Peer.t),
        forall (t : Topic.t),
        t \in (Connections.find p c).topics <=> p \in (Connections.peers_in_topic topic c)
  *)
  module Connections : sig
    type t

    val empty : t

    val bindings : t -> (Peer.t * connection) list

    val find : Peer.t -> t -> connection option

    val mem : Peer.t -> t -> bool

    val add_peer :
      Peer.t ->
      direct:bool ->
      outbound:bool ->
      t ->
      [`added of t | `already_known]

    val subscribe : Peer.t -> Topic.t -> t -> [`unknown_peer | `subscribed of t]

    val unsubscribe :
      Peer.t -> Topic.t -> t -> [`unknown_peer | `unsubscribed of t]

    val remove : Peer.t -> t -> t

    val fold : (Peer.t -> connection -> 'b -> 'b) -> t -> 'b -> 'b

    val iter : (Peer.t -> connection -> unit) -> t -> unit

    val peers_in_topic : Topic.t -> t -> Peer.Set.t

    val peers_per_topic_map : t -> Peer.Set.t Topic.Map.t
  end = struct
    type t = {
      peer_to_conn : connection Peer.Map.t;
      topic_to_peers : Peer.Set.t Topic.Map.t;
    }

    let empty =
      {peer_to_conn = Peer.Map.empty; topic_to_peers = Topic.Map.empty}

    let bindings map = Peer.Map.bindings map.peer_to_conn

    let find peer map = Peer.Map.find peer map.peer_to_conn

    let mem peer map = Peer.Map.mem peer map.peer_to_conn

    let add_peer peer ~direct ~outbound map =
      if mem peer map then `already_known
      else
        let peer_to_conn =
          Peer.Map.add
            peer
            {topics = Topic.Set.empty; direct; outbound}
            map.peer_to_conn
        in
        `added {map with peer_to_conn}

    let subscribe peer topic map =
      match find peer map with
      | None -> `unknown_peer
      | Some connection ->
          (* We could detect that [peer] is already subscribed to [topic]. *)
          let connection =
            {connection with topics = Topic.Set.add topic connection.topics}
          in
          let peer_to_conn = Peer.Map.add peer connection map.peer_to_conn in
          let topic_to_peers =
            Topic.Map.update
              topic
              (function
                | None -> Some (Peer.Set.singleton peer)
                | Some peers_in_topic -> Some (Peer.Set.add peer peers_in_topic))
              map.topic_to_peers
          in
          `subscribed {peer_to_conn; topic_to_peers}

    let unsubscribe peer topic map =
      match find peer map with
      | None -> `unknown_peer
      | Some connection ->
          let connection =
            {connection with topics = Topic.Set.remove topic connection.topics}
          in
          let peer_to_conn = Peer.Map.add peer connection map.peer_to_conn in
          let topic_to_peers =
            Topic.Map.update
              topic
              (function
                | None -> None
                | Some peers_in_topic ->
                    Some (Peer.Set.remove peer peers_in_topic))
              map.topic_to_peers
          in
          `unsubscribed {peer_to_conn; topic_to_peers}

    let remove peer map =
      match find peer map with
      | None -> map
      | Some conn ->
          let topics = conn.topics in
          let peer_to_conn = Peer.Map.remove peer map.peer_to_conn in
          let topic_to_peers =
            Topic.Set.fold
              (fun topic topic_to_peers ->
                Topic.Map.update
                  topic
                  (function
                    | None ->
                        (* Should never happen by invariant on type [t]. *)
                        None
                    | Some peers_in_topic ->
                        Some (Peer.Set.remove peer peers_in_topic))
                  topic_to_peers)
              topics
              map.topic_to_peers
          in
          {peer_to_conn; topic_to_peers}

    let fold f map acc = Peer.Map.fold f map.peer_to_conn acc

    let iter f map = Peer.Map.iter f map.peer_to_conn

    let peers_in_topic topic map =
      Topic.Map.find topic map.topic_to_peers
      |> Option.value ~default:Peer.Set.empty

    let peers_per_topic_map t = t.topic_to_peers
  end

  type fanout_peers = {peers : Peer.Set.t; last_published_time : time}

  module Message_cache = Message_cache.Make (C.Subconfig) (Time)

  type state = {
    limits : limits;  (** Statically known parameters of the algorithm. *)
    parameters : parameters;  (** Other parameters of the algorithm. *)
    connections : Connections.t;
        (** [connections] is the set of active connections. A connection is added
            through the `Add_peer` message and removed through the `Remove_peer`
            message. *)
    scores : Score.t Peer.Map.t;
        (** The scores are used to drive peer selection mechanisms. Scores are kept
            for at least [retain_duration] after a connection is removed, hence they
            can't be stored in {!connections}. Any peer having an active connection
            is associated to a score. *)
    ihave_per_heartbeat : int Peer.Map.t;
        (** Mapping tracking for each peer the number of IHave messages received from
            that peer between two heartbeats. *)
    iwant_per_heartbeat : int Peer.Map.t;
        (** Mapping tracking for each peer the number of messages ids sent in
            IWant messages to that peer between two heartbeats. *)
    mesh : Peer.Set.t Topic.Map.t;
        (** The mesh for a topic is a random subset of the connected peers
            subscribed to that topic and that have non-negative score, are not
            backed-off, and are not direct peers. The local peer routes
            full-messages to these peers. A topic is in the domain of the mesh
            iff the local peer has joined the topic (we also say that it tracks
            the topic). *)
    fanout : fanout_peers Topic.Map.t;
        (** The fanout for a topic is a random subset of the connected peers
            subscribed to that topic and that are not direct and have a score
            above some given threshold. The local peer routes full-messages to
            these peers. In contrast to the mesh, the fanout map contains topics
            the local peer has not joined. *)
    backoff : time Peer.Map.t Topic.Map.t;
        (** The backoff times associated to a topic and a peer. When a backoff is
             set, we refuse any `graft` from this peer. As a consequence, if a
             backoff is set for a peer and a topic, this peer is not expected
             to be in the mesh of the given topic.  *)
    message_cache : Message_cache.t;
        (** A sliding window cache that stores published messages and their first
        seen time. *)
    rng : Random.State.t;  (** The state of the PRNG algorithm. *)
    heartbeat_ticks : int64;
        (** A counter of the number of elapsed heartbeat ticks.  *)
  }
  (* Invariants:

     - Forall t, Topic.Map.mem t mesh <=> not (Map.mem t fanout)

     - Forall p c t, Peer.Map.find connections p = Some ({ connection = Expires _}) ->
         Topic.Map.find t mesh = None &&
         Topic.Map.find t fanout = None

     - Forall p t, Peer.Map.find connections p = None ->
         Topic.Map.find t mesh = None &&
         Topic.Map.find t fanout = None

     - Forall p. Peer.Map.mem connections p -> Peer.Map.mem scores p

     - Forall p, Peer.Map.mem connections p <-> Peer.Map.find scores p = Some {peer_status = Connected; _}
  *)

  (* FIXME https://gitlab.com/tezos/tezos/-/issues/4984

     Test the those invariants
  *)
  module Monad = struct
    type 'a t = (state, 'a) State_monad.t

    type ('pass, 'fail) check = (state, 'pass, 'fail) State_monad.check

    include State_monad.M
  end

  let assert_in_unit_interval v = assert (v >= 0.0 && v <= 1.0)

  let check_per_topic_score_limits tsp =
    assert (tsp.time_in_mesh_weight >= 0.0) ;
    assert (tsp.time_in_mesh_cap >= 0.0) ;
    assert (Span.(tsp.time_in_mesh_quantum > zero)) ;
    assert (tsp.first_message_deliveries_weight >= 0.0) ;
    assert (tsp.first_message_deliveries_cap >= 0) ;
    assert_in_unit_interval tsp.first_message_deliveries_decay ;
    assert (tsp.mesh_message_deliveries_weight <= 0.0) ;
    assert (Span.(tsp.mesh_message_deliveries_activation >= of_int_s 1)) ;
    assert (tsp.mesh_message_deliveries_cap >= 0) ;
    assert (tsp.mesh_message_deliveries_threshold > 0) ;
    assert_in_unit_interval tsp.mesh_message_deliveries_decay ;
    assert (tsp.mesh_failure_penalty_weight <= 0.0) ;
    assert_in_unit_interval tsp.mesh_failure_penalty_decay ;
    assert (tsp.invalid_message_deliveries_weight <= 0.0) ;
    assert_in_unit_interval tsp.invalid_message_deliveries_decay

  let check_score_limits (sp : _ score_limits) =
    (match sp.topics with
    | Topic_score_limits_single tp -> check_per_topic_score_limits tp
    | Topic_score_limits_family {all_topics; parameters; weights = _} ->
        Seq.map parameters all_topics |> Seq.iter check_per_topic_score_limits) ;
    Option.iter (fun cap -> assert (cap >= 0.0)) sp.topic_score_cap ;
    assert (sp.behaviour_penalty_weight <= 0.0) ;
    assert (sp.behaviour_penalty_threshold >= 0.0) ;
    assert_in_unit_interval sp.behaviour_penalty_decay ;
    assert (sp.decay_zero >= 0.0)

  let check_limits l =
    (* TODO: https://gitlab.com/tezos/tezos/-/issues/5129
       Replace the asserts by something more informative. *)
    assert (l.degree_low >= 0) ;
    assert (l.degree_out >= 0) ;
    assert (l.degree_score >= 0) ;
    assert (l.degree_low <= l.degree_optimal) ;
    assert (l.degree_high >= l.degree_optimal) ;
    assert (l.backoff_cleanup_ticks > 0) ;
    assert (l.score_cleanup_ticks > 0) ;
    (* TODO: https://gitlab.com/tezos/tezos/-/issues/5052
       This requirement is not imposed in the spec/Go implementation. Relax this
       requirement or delete the todo. *)
    assert (l.degree_score + l.degree_out <= l.degree_optimal) ;
    assert (l.degree_out <= l.degree_low) ;
    assert (l.degree_out <= l.degree_optimal / 2) ;
    assert (l.history_gossip_length > 0) ;
    assert (l.history_gossip_length <= l.history_length) ;
    check_score_limits l.score_limits

  let make : Random.State.t -> limits -> parameters -> state =
   fun rng limits parameters ->
    check_limits limits ;
    {
      limits;
      parameters;
      connections = Connections.empty;
      scores = Peer.Map.empty;
      ihave_per_heartbeat = Peer.Map.empty;
      iwant_per_heartbeat = Peer.Map.empty;
      mesh = Topic.Map.empty;
      fanout = Topic.Map.empty;
      backoff = Topic.Map.empty;
      message_cache =
        Message_cache.create
          ~history_slots:limits.history_length
          ~gossip_slots:limits.history_gossip_length
          ~seen_message_slots:limits.seen_history_length;
      rng;
      heartbeat_ticks = 0L;
    }

  module Helpers = struct
    let fail_if cond output_err =
      let open Monad.Syntax in
      if cond then fail output_err else unit

    let fail_if_not cond output_err = fail_if (not cond) output_err

    (* These projections enable let-punning. *)
    let max_recv_ihave_per_heartbeat state =
      state.limits.max_recv_ihave_per_heartbeat

    let max_sent_iwant_per_heartbeat state =
      state.limits.max_sent_iwant_per_heartbeat

    let degree_optimal state = state.limits.degree_optimal

    let publish_threshold state = state.limits.publish_threshold

    let do_px state = state.limits.do_px

    let peers_to_px state = state.limits.peers_to_px

    let accept_px_threshold state = state.limits.accept_px_threshold

    let prune_backoff state = state.limits.prune_backoff

    let unsubscribe_backoff state = state.limits.unsubscribe_backoff

    let graft_flood_threshold state = state.limits.graft_flood_threshold

    let retain_duration state = state.limits.retain_duration

    let fanout_ttl state = state.limits.fanout_ttl

    let heartbeat_interval state = state.limits.heartbeat_interval

    let backoff_cleanup_ticks state = state.limits.backoff_cleanup_ticks

    let score_cleanup_ticks state = state.limits.score_cleanup_ticks

    let degree_low state = state.limits.degree_low

    let degree_high state = state.limits.degree_high

    let degree_score state = state.limits.degree_score

    let degree_out state = state.limits.degree_out

    let max_gossip_retransmission state = state.limits.max_gossip_retransmission

    let gossip_threshold state = state.limits.gossip_threshold

    let opportunistic_graft_ticks state = state.limits.opportunistic_graft_ticks

    let opportunistic_graft_peers state = state.limits.opportunistic_graft_peers

    let opportunistic_graft_threshold state =
      state.limits.opportunistic_graft_threshold

    let mesh state = state.mesh

    let fanout state = state.fanout

    let backoff state = state.backoff

    let connections state = state.connections

    let scores state = state.scores

    let peer_filter state = state.parameters.peer_filter

    let message_cache state = state.message_cache

    let rng state = state.rng

    let score_limits state = state.limits.score_limits

    let update ?(delta = 1) key map =
      Peer.Map.update
        key
        (function None -> Some delta | Some n -> Some (n + delta))
        map

    let update_and_get ?(delta = 1) key map =
      let res = ref delta in
      Peer.Map.update
        key
        (function
          | None -> Some delta
          | Some n ->
              let value = n + delta in
              res := value ;
              Some value)
        map
      |> fun x -> (x, !res)

    let update_and_get_ihave_per_heartbeat ?delta key state =
      let ihave_per_heartbeat, res =
        update_and_get ?delta key state.ihave_per_heartbeat
      in
      let state = {state with ihave_per_heartbeat} in
      (state, res)

    let update_iwant_per_heartbeat ?delta key state =
      let iwant_per_heartbeat = update ?delta key state.iwant_per_heartbeat in
      let state = {state with iwant_per_heartbeat} in
      (state, ())

    let find ?(default = 0) key map =
      match Peer.Map.find key map with None -> default | Some n -> n

    let find_iwant_per_heartbeat ?default key state =
      find ?default key state.iwant_per_heartbeat

    let reset_ihave_per_heartbeat state =
      ({state with ihave_per_heartbeat = Peer.Map.empty}, ())

    let reset_iwant_per_heartbeat state =
      ({state with iwant_per_heartbeat = Peer.Map.empty}, ())

    let heartbeat_ticks state = state.heartbeat_ticks

    let set_heartbeat_ticks heartbeat_ticks state =
      ({state with heartbeat_ticks}, ())

    let set_connections connections state = ({state with connections}, ())

    let set_scores scores state = ({state with scores}, ())

    let topic_is_tracked topic state =
      let {mesh; _} = state in
      match Topic.Map.find topic mesh with None -> false | Some _ -> true

    let set_message_cache message_cache state = ({state with message_cache}, ())

    let get_scores_score scores peer = Peer.Map.find peer scores

    let get_scores_score_or_zero scores peer =
      get_scores_score scores peer
      |> Option.fold ~none:Score.zero ~some:Score.value

    let get_score peer state = get_scores_score state.scores peer

    let get_score_or_zero peer state =
      get_scores_score_or_zero state.scores peer

    let peer_has_outbound_connection peers ~default peer =
      Connections.find peer peers
      |> Option.map (fun connection -> connection.outbound)
      |> Option.value ~default

    let select_connections_peers connections scores rng topic ~filter ~max =
      Connections.peers_in_topic topic connections
      |> Peer.Set.to_seq
      |> Seq.filter_map (fun peer ->
             match Connections.find peer connections with
             | None -> assert false
             | Some connection ->
                 let score = get_scores_score_or_zero scores peer in
                 if filter peer connection score then Some peer else None)
      |> List.of_seq |> List.shuffle ~rng |> List.take_n max

    let select_peers topic ~filter ~max =
      let open Monad.Syntax in
      let*! connections in
      let*! scores in
      let*! rng in
      select_connections_peers connections scores rng topic ~filter ~max
      |> Peer.Set.of_list |> return

    let get_direct_peers topic =
      (* TODO: https://gitlab.com/tezos/tezos/-/issues/5010

         Have a dedicated structure for direct peers? *)
      let filter _peer {direct; _} _score = direct in
      select_peers topic ~filter ~max:max_int

    let set_mesh_topic topic peers state =
      let state = {state with mesh = Topic.Map.add topic peers state.mesh} in
      (state, ())

    let set_mesh mesh state =
      let state = {state with mesh} in
      (state, ())

    let find_mesh topic state = Topic.Map.find topic state.mesh

    let find_fanout topic state = Topic.Map.find topic state.fanout

    let set_fanout_topic topic last_published_time peers state =
      if Peer.Set.is_empty peers then (state, ())
      else
        let state =
          {
            state with
            fanout =
              Topic.Map.add topic {peers; last_published_time} state.fanout;
          }
        in
        (state, ())

    let set_fanout fanout state =
      let state = {state with fanout} in
      (state, ())

    let delete_mesh topic state =
      let state = {state with mesh = Topic.Map.remove topic state.mesh} in
      (state, ())

    let delete_fanout topic state =
      let state = {state with fanout = Topic.Map.remove topic state.fanout} in
      (state, ())

    let put_message_in_cache message_id message topic state =
      let state =
        {
          state with
          message_cache =
            Message_cache.add_message
              message_id
              message
              topic
              state.message_cache;
        }
      in
      (state, ())

    let get_backoff topic peer backoff =
      Option.bind (Topic.Map.find topic backoff) (fun per_peer_backoff ->
          Peer.Map.find peer per_peer_backoff)

    let exists_backoff topic peer backoff =
      get_backoff topic peer backoff |> Option.is_some

    let set_backoff backoff state =
      let state = {state with backoff} in
      (state, ())

    let add_backoff_for_peer delay topic peer backoffs =
      let now = Time.now () in
      let backoff_expire = Time.add now delay in
      Topic.Map.update
        topic
        (function
          | None -> Some (Peer.Map.singleton peer backoff_expire)
          | Some peer_backoff ->
              Peer.Map.update
                peer
                (function
                  | None -> Some backoff_expire
                  | Some old_backoff ->
                      if Time.(old_backoff < backoff_expire) then
                        Some backoff_expire
                      else Some old_backoff)
                peer_backoff
              |> Option.some)
        backoffs

    let set_backoff_for_peer delay topic peer =
      let open Monad.Syntax in
      let*! backoff in
      add_backoff_for_peer delay topic peer backoff |> set_backoff

    let update_scores_score peer f scores =
      Peer.Map.update peer (Option.map f) scores

    let update_score peer f =
      let open Monad.Syntax in
      let*! scores in
      let scores = update_scores_score peer f scores in
      set_scores scores

    let with_score (opt : Score.t option) f =
      let open Monad.Syntax in
      match opt with None -> pass () | Some v -> f v

    let add_score peer score = update_score peer (Fun.const score)

    let check_score peer threshold ~on_failure =
      let open Monad.Syntax in
      let*! peer_score_opt = get_score peer in
      with_score peer_score_opt @@ fun peer_score ->
      fail_if Score.(value peer_score < threshold) @@ on_failure peer_score
  end

  include Helpers

  module Subscribe = struct
    let handle topic peer =
      let open Monad.Syntax in
      let*! connections in
      match Connections.subscribe peer topic connections with
      | `unknown_peer -> return Subscribe_to_unknown_peer
      | `subscribed connections ->
          let* () = set_connections connections in
          (* TODO: https://gitlab.com/tezos/tezos/-/issues/5143
             rust-libp2p adds the peer to the mesh if needed here. *)
          return Subscribed
  end

  let handle_subscribe : subscribe -> [`Subscribe] output Monad.t =
   fun {topic; peer} -> Subscribe.handle topic peer

  module Unsubscribe = struct
    let remove_peer_from_mesh topic peer =
      let open Monad.Syntax in
      let* () = update_score peer (fun s -> Score.prune s topic) in
      let*! mesh in
      let mesh =
        Topic.Map.update
          topic
          (Option.map (fun peers -> Peer.Set.remove peer peers))
          mesh
      in
      set_mesh mesh

    let remove_peer_from_fanout topic peer =
      let open Monad.Syntax in
      let*! fanout in
      let fanout =
        Topic.Map.update
          topic
          (Option.map (fun fanout_peers ->
               {
                 fanout_peers with
                 peers = Peer.Set.remove peer fanout_peers.peers;
               }))
          fanout
      in
      set_fanout fanout

    let handle topic peer =
      let open Monad.Syntax in
      let*! connections in
      match Connections.unsubscribe peer topic connections with
      | `unknown_peer -> return @@ Unsubscribe_from_unknown_peer
      | `unsubscribed connections ->
          let* () = set_connections connections in
          (* Remove the peer from mesh as done in the rust implementation
             but not go implementation. *)
          let* () = remove_peer_from_mesh topic peer in
          (* Remove the peer from fanout. This is not done in go/rust implementation but
             we do it here as it is more natural and consistent with the above mesh cleaning. *)
          let* () = remove_peer_from_fanout topic peer in
          return Unsubscribed
  end

  let handle_unsubscribe : unsubscribe -> [`Unsubscribe] output Monad.t =
   fun {topic; peer} -> Unsubscribe.handle topic peer

  module IHave = struct
    let check_too_many_recv_ihave_message count =
      let open Monad.Syntax in
      let*! max_recv_ihave_per_heartbeat in
      fail_if (count > max_recv_ihave_per_heartbeat)
      @@ Too_many_recv_ihave_messages
           {count; max = max_recv_ihave_per_heartbeat}

    (* FIXME https://gitlab.com/tezos/tezos/-/issues/5016

       This check is not correct if the distant peer uses a different
       value for [max_recv_ihave_per_heartbeat] then our value for
       [max_sent_iwant_per_heartbeat]. *)
    let check_too_many_sent_iwant_message count =
      let open Monad.Syntax in
      let*! max_sent_iwant_per_heartbeat in
      fail_if (count >= max_sent_iwant_per_heartbeat)
      @@ Too_many_sent_iwant_messages
           {count; max = max_sent_iwant_per_heartbeat}

    let check_topic_tracked topic =
      let open Monad.Syntax in
      let*! is_topic_tracked = topic_is_tracked topic in
      fail_if_not is_topic_tracked Message_topic_not_tracked

    let check_not_empty iwant_message_ids =
      fail_if (List.is_empty iwant_message_ids)
      @@ Message_requested_message_ids []

    let filter sender topic peer message_ids :
        (Message_id.t list, [`IHave] output) Monad.check =
      let open Monad.Syntax in
      let*! peer_filter in
      let*! message_cache in
      let should_handle_message_id message_id : bool =
        (not (Message_cache.seen_message message_id message_cache))
        && peer_filter peer (`IHave message_id)
      in
      let filtered_message_ids =
        List.filter_e
          (fun message_id ->
            match Message.valid ~message_id () with
            | `Valid -> Ok (should_handle_message_id message_id)
            | `Unknown | `Outdated -> Ok false
            | `Invalid -> Error ())
          message_ids
      in
      match filtered_message_ids with
      | Ok filtered_message_ids -> pass filtered_message_ids
      | Error () ->
          let* () =
            update_score sender (fun stats ->
                Score.invalid_message_delivered stats topic)
          in
          fail Invalid_message_id

    let shuffle_and_trunc message_ids ~limit : (int * Message_id.t list) Monad.t
        =
      let open Monad.Syntax in
      let*! rng in
      let iwant_message_ids_len = List.length message_ids in
      (* Do not send more than [max_sent_iwant_per_heartbeat] message ids. *)
      let iwant_ids_to_send_n = min iwant_message_ids_len limit in
      let shuffle_iwant_ids = List.shuffle ~rng message_ids in
      let requested_message_ids =
        List.take_n iwant_ids_to_send_n shuffle_iwant_ids
      in
      return (iwant_ids_to_send_n, requested_message_ids)

    let handle peer topic message_ids : [`IHave] output Monad.t =
      let open Monad.Syntax in
      let*! gossip_threshold in
      let*? () =
        check_score
          peer
          (Score.of_float gossip_threshold)
          ~on_failure:(fun score ->
            Ihave_from_peer_with_low_score {score; threshold = gossip_threshold})
      in
      let* count_ihave_received = update_and_get_ihave_per_heartbeat peer in
      let*? () = check_too_many_recv_ihave_message count_ihave_received in
      let*! count_iwant_sent = find_iwant_per_heartbeat peer in
      let*? () = check_too_many_sent_iwant_message count_iwant_sent in
      let*? () = check_topic_tracked topic in
      let*? iwant_message_ids = filter peer topic peer message_ids in
      let*? () = check_not_empty iwant_message_ids in
      let*! max_sent_iwant_per_heartbeat in
      let limit = max_sent_iwant_per_heartbeat - count_iwant_sent in
      (* Invariant: limit > 0 *)
      let* length, requested_message_ids =
        shuffle_and_trunc iwant_message_ids ~limit
      in
      let* () = update_iwant_per_heartbeat ~delta:length peer in
      (* FIXME https://gitlab.com/tezos/tezos/-/issues/5532

         The go implementation traces some of the messages
         requested. *)
      Message_requested_message_ids requested_message_ids |> return
  end

  let handle_ihave : ihave -> [`IHave] output Monad.t =
   fun {peer; topic; message_ids} -> IHave.handle peer topic message_ids

  module IWant = struct
    let handle peer message_ids : [`IWant] output Monad.t =
      let open Monad.Syntax in
      let*! gossip_threshold in
      let*? () =
        check_score
          peer
          (Score.of_float gossip_threshold)
          ~on_failure:(fun score ->
            Iwant_from_peer_with_low_score {score; threshold = gossip_threshold})
      in
      let routed_message_ids = Message_id.Map.empty in
      let*! message_cache in
      let*! peer_filter in
      let*! max_gossip_retransmission in
      let message_cache, routed_message_ids =
        List.fold_left
          (fun (message_cache, messages) message_id ->
            let message_cache, info =
              match
                Message_cache.get_message_for_peer peer message_id message_cache
              with
              | None -> (message_cache, `Not_found)
              | Some (message_cache, message, access_counter) ->
                  ( message_cache,
                    if access_counter > max_gossip_retransmission then
                      `Too_many_requests
                    else if peer_filter peer (`IWant message_id) then
                      `Message message
                    else `Ignored )
            in
            (message_cache, Message_id.Map.add message_id info messages))
          (message_cache, routed_message_ids)
          message_ids
      in
      let* () = set_message_cache message_cache in
      On_iwant_messages_to_route {routed_message_ids} |> return
  end

  let handle_iwant : iwant -> [`IWant] output Monad.t =
   fun {peer; message_ids} -> IWant.handle peer message_ids

  module Graft = struct
    let check_filter peer topic =
      let open Monad.Syntax in
      let*! peer_filter in
      if peer_filter peer `Graft then pass ()
      else
        let*! prune_backoff in
        let* () = set_backoff_for_peer prune_backoff topic peer in
        Peer_filtered |> fail

    let check_subscribed peer topic =
      let open Monad.Syntax in
      let*! mesh_opt = find_mesh topic in
      match mesh_opt with
      | None ->
          let*! prune_backoff in
          let* () = set_backoff_for_peer prune_backoff topic peer in
          Unsubscribed_topic |> fail
      | Some mesh -> pass mesh

    let check_not_in_mesh mesh peer =
      fail_if (Peer.Set.mem peer mesh) Peer_already_in_mesh

    let check_active peer =
      let open Monad.Syntax in
      let*! connections in
      match Connections.find peer connections with
      | None -> Unexpected_grafting_peer |> fail
      | Some connection -> pass connection

    let check_not_direct connection peer topic =
      let open Monad.Syntax in
      if connection.direct then
        let*! prune_backoff in
        let* () = set_backoff_for_peer prune_backoff topic peer in
        Grafting_direct_peer |> fail
      else pass ()

    let check_score peer topic score =
      let open Monad.Syntax in
      if Score.(value score >= zero) then unit
      else
        let*! prune_backoff in
        let* () = set_backoff_for_peer prune_backoff topic peer in
        Grafting_peer_with_negative_score |> fail

    let check_mesh_size mesh connection peer topic =
      let open Monad.Syntax in
      let*! degree_high in
      (* Check the number of mesh peers; if it is at (or over) [degree_high], we
         only accept grafts from peers with outbound connections; this is a
         defensive check to restrict potential mesh takeover attacks combined
         with love bombing *)
      if (not connection.outbound) && Peer.Set.cardinal mesh >= degree_high then
        let*! prune_backoff in
        let* () = set_backoff_for_peer prune_backoff topic peer in
        Mesh_full |> fail
      else pass ()

    let check_backoff peer topic score =
      let open Monad.Syntax in
      let*! backoff in
      match get_backoff topic peer backoff with
      | None -> unit
      | Some backoff_expire ->
          let current = Time.now () in
          if Time.(current >= backoff_expire) then unit
          else
            let score = Score.penalty score 1 in
            let*! graft_flood_threshold in
            let*! prune_backoff in
            (* Calculate the last prune time
               based on the recored backoff expire time and prune backoff. *)
            let last_prune_time = Time.(sub backoff_expire prune_backoff) in
            let score =
              if Time.(current < add last_prune_time graft_flood_threshold) then
                Score.penalty score 1
              else score
            in
            let* () = set_backoff_for_peer prune_backoff topic peer in
            let* () = add_score peer score in
            fail Peer_backed_off

    (* NOTE: It is the worker who builds the Prune message in (some of the)
       cases of failed/rejected graft requests. The cases should be the same as
       those for which we set a backoff, which is done here; be sure the two
       modules match! *)
    let handle peer topic =
      let open Monad.Syntax in
      let*? () = check_filter peer topic in
      let*? mesh = check_subscribed peer topic in
      let*? () = check_not_in_mesh mesh peer in
      let*? connection = check_active peer in
      let*? () = check_not_direct connection peer topic in
      let*! score_opt = get_score peer in
      (* Global invariant: peer in mesh implies that score exists *)
      let*? () = with_score score_opt @@ check_backoff peer topic in
      let*? () = with_score score_opt @@ check_score peer topic in
      let*? () = check_mesh_size mesh connection peer topic in
      let* () = update_score peer (fun s -> Score.graft s topic) in
      let* () = set_mesh_topic topic (Peer.Set.add peer mesh) in
      (* Call [handle_subscribe] to ensure the invariant where all grafted peers subscribed. *)
      let* output = handle_subscribe {topic; peer} in
      (match output with
      | Subscribe_to_unknown_peer ->
          (* Not possible due to invariant. *)
          ()
      | Subscribed ->
          (* Expected case. *)
          ()) ;
      Grafting_successfully |> return
  end

  let handle_graft : graft -> [`Graft] output Monad.t =
   fun {peer; topic} -> Graft.handle peer topic

  module Prune = struct
    let check_px_score peer =
      let open Monad.Syntax in
      let*! accept_px_threshold in
      let*! score_opt = get_score peer in
      (* Global invariant: peer in mesh => connection exists => score exists *)
      with_score score_opt @@ fun score ->
      fail_if Score.(value score < of_float accept_px_threshold)
      @@ Ignore_PX_score_too_low score

    let check_topic_tracked topic =
      let open Monad.Syntax in
      let*! mesh_opt = find_mesh topic in
      match mesh_opt with
      | None -> Prune_topic_not_tracked |> fail
      | Some mesh -> pass mesh

    let take_threshold_px (px : Peer.t Seq.t) (state : state) =
      (* [peers_to_px] is not just the max number of peers we send via PX, but
         also the max number of advertised PXs we accept. *)
      let peers_to_px = peers_to_px state in
      List.of_seq px
      |> List.shuffle ~rng:(rng state)
      |> List.take_n peers_to_px |> Peer.Set.of_list

    let handle peer topic ~px ~backoff =
      let open Monad.Syntax in
      let*? mesh = check_topic_tracked topic in
      let*? () = fail_if_not (Peer.Set.mem peer mesh) Peer_not_in_mesh in
      let* () = Peer.Set.remove peer mesh |> set_mesh_topic topic in
      let* () = set_backoff_for_peer backoff topic peer in
      let* () = update_score peer (fun s -> Score.prune s topic) in
      let*? () = check_px_score peer in
      let*! px = take_threshold_px px in
      return @@ if Peer.Set.is_empty px then No_PX else PX px
  end

  let handle_prune : prune -> [`Prune] output Monad.t =
   fun {peer; topic; px; backoff} -> Prune.handle peer topic ~px ~backoff

  module Receive_message = struct
    let check_valid sender topic message message_id =
      let open Monad.Syntax in
      match Message.valid ~message ~message_id () with
      | `Valid -> unit
      | `Unknown | `Outdated -> fail Unknown_validity
      | `Invalid ->
          let* () =
            update_score sender (fun stats ->
                Score.invalid_message_delivered stats topic)
          in
          fail Invalid_message

    let handle sender topic message_id message =
      let open Monad.Syntax in
      let*! mesh_opt = find_mesh topic in
      let*? peers_in_mesh =
        match mesh_opt with
        | Some peers -> pass peers
        | None -> fail Not_subscribed
      in
      let*? () =
        let*! message_cache in
        match Message_cache.get_first_seen_time message_id message_cache with
        | None -> unit
        | Some validated ->
            let* () =
              update_score sender (fun stats ->
                  Score.duplicate_message_delivered stats topic validated)
            in
            fail Already_received
      in
      let*? () = check_valid sender topic message message_id in
      let peers = Peer.Set.remove sender peers_in_mesh in
      let* () = put_message_in_cache message_id message topic in
      let* () =
        update_score sender (fun stats ->
            Score.first_message_delivered stats topic)
      in
      let* direct_peers = get_direct_peers topic in
      let to_route = Peer.Set.union peers direct_peers in
      (* TODO: https://gitlab.com/tezos/tezos/-/issues/5272

         Filter out peers from which we already received the message, or an
         IHave message? *)
      Route_message {to_route} |> return
  end

  let handle_receive_message :
      receive_message -> [`Receive_message] output Monad.t =
   fun {sender; topic; message_id; message} ->
    Receive_message.handle sender topic message_id message

  module Publish_message = struct
    let check_not_seen message_id =
      let open Monad.Syntax in
      let*! message_cache in
      match Message_cache.get_first_seen_time message_id message_cache with
      | None -> unit
      | Some _validated -> fail Already_published

    let get_peers_for_unsubscribed_topic topic =
      let open Monad.Syntax in
      let*! publish_threshold in
      let*! degree_optimal in
      let now = Time.now () in
      let*! fanout_opt = find_fanout topic in
      match fanout_opt with
      | None ->
          let filter_by_score score =
            Score.(score >= of_float publish_threshold)
          in
          let filter _peer {direct; _} score =
            (not direct) && filter_by_score score
          in
          let* not_direct_peers =
            select_peers topic ~filter ~max:degree_optimal
          in
          let* () = set_fanout_topic topic now not_direct_peers in
          return not_direct_peers
      | Some fanout ->
          let* () = set_fanout_topic topic now fanout.peers in
          return fanout.peers

    let handle topic message_id message : [`Publish_message] output Monad.t =
      let open Monad.Syntax in
      let*? () = check_not_seen message_id in
      let* () = put_message_in_cache message_id message topic in
      let*! mesh_opt = find_mesh topic in
      let* peers =
        match mesh_opt with
        | Some peers -> return peers
        | None -> get_peers_for_unsubscribed_topic topic
      in
      let* direct_peers = get_direct_peers topic in
      let to_publish = Peer.Set.union peers direct_peers in
      Publish_message {to_publish} |> return
  end

  let publish_message : publish_message -> [`Publish_message] output Monad.t =
   fun {topic; message_id; message} ->
    Publish_message.handle topic message_id message

  module Join = struct
    let check_is_not_subscribed topic : (unit, [`Join] output) Monad.check =
      let open Monad.Syntax in
      let*! mesh in
      fail_if (Topic.Map.mem topic mesh) Already_joined

    let init_mesh topic : [`Join] output Monad.t =
      let open Monad.Syntax in
      let*! degree_optimal in
      let*! connections in
      let*! backoff in
      let*! scores in
      let is_valid peer =
        match Connections.find peer connections with
        | None ->
            (* FIXME https://gitlab.com/tezos/tezos/-/issues/5005

               Not supposed to happen. But maybe it is better to
               return a value for defensive programming. *)
            false
        | Some _peer_info ->
            let score = get_scores_score_or_zero scores peer in
            let backed_off = exists_backoff topic peer backoff in
            not (backed_off || Score.(score < zero))
      in
      let*! fanout in
      let valid_fanout_peers =
        match Topic.Map.find topic fanout with
        | None -> Peer.Set.empty
        | Some fanout_peers -> Peer.Set.filter is_valid fanout_peers.peers
      in
      let* peers =
        (* We prioritize fanout peers to be in the mesh for this
           topic. If we need more peers, we look at all our peers
           subscribed to this topic. *)
        let valid_fanout_peers_len = Peer.Set.cardinal valid_fanout_peers in
        if valid_fanout_peers_len >= degree_optimal then
          return valid_fanout_peers
        else
          let max = max 0 (degree_optimal - valid_fanout_peers_len) in
          let* more_peers =
            let filter peer {direct; _} score =
              let backed_off = exists_backoff topic peer backoff in
              not
                (direct || backed_off
                || Score.(score < zero)
                || Peer.Set.mem peer valid_fanout_peers)
            in
            select_peers topic ~filter ~max
          in
          return (Peer.Set.union more_peers valid_fanout_peers)
      in
      (* Notify scoring about the graft. *)
      let scores =
        Peer.Set.fold
          (fun peer scores ->
            update_scores_score peer (fun s -> Score.graft s topic) scores)
          peers
          scores
      in
      let* () = set_scores scores in
      let* () = set_mesh_topic topic peers in
      let* () = delete_fanout topic in
      Joining_topic {to_graft = peers} |> return

    let handle topic : [`Join] output Monad.t =
      let open Monad.Syntax in
      let*? () = check_is_not_subscribed topic in
      init_mesh topic
  end

  let join : join -> [`Join] output Monad.t = fun {topic} -> Join.handle topic

  module Leave = struct
    type mesh = Peer.Set.t

    let check_already_subscribed topic : (mesh, [`Leave] output) Monad.check =
      let open Monad.Syntax in
      let*! mesh in
      match Topic.Map.find topic mesh with
      | None -> Not_joined |> fail
      | Some mesh -> pass mesh

    let handle_mesh topic mesh : [`Leave] output Monad.t =
      let open Monad.Syntax in
      let*! unsubscribe_backoff in
      let*! backoff in
      let*! scores in
      let* () =
        Peer.Set.fold
          (fun peer backoff ->
            add_backoff_for_peer unsubscribe_backoff topic peer backoff)
          mesh
          backoff
        |> set_backoff
      in
      let noPX_peers =
        (* no PX for peers with negative score. *)
        let get_score = get_scores_score_or_zero scores in
        Peer.Set.filter (fun peer -> Score.(get_score peer < zero)) mesh
      in
      (* Notify scoring about the prunes. *)
      let* () =
        Peer.Set.fold
          (fun peer scores ->
            update_scores_score peer (fun s -> Score.prune s topic) scores)
          mesh
          scores
        |> set_scores
      in
      Leaving_topic {to_prune = mesh; noPX_peers} |> return

    let handle topic : [`Leave] output Monad.t =
      let open Monad.Syntax in
      let*? mesh = check_already_subscribed topic in
      let* () = delete_mesh topic in
      handle_mesh topic mesh
  end

  let leave : leave -> [`Leave] output Monad.t =
   fun {topic} -> Leave.handle topic

  let set_application_score :
      set_application_score -> [`Set_application_score] output Monad.t =
   fun {peer; score} ->
    let open Monad.Syntax in
    let* () =
      update_score peer (fun s -> Score.set_application_score s score)
    in
    return Set_application_score

  module Heartbeat = struct
    let clear_backoff =
      let open Monad.Syntax in
      let*! heartbeat_ticks in
      let*! heartbeat_interval in
      (* FIXME: https://gitlab.com/tezos/tezos/-/issues/5455

         Move the heartbeat interval/span outside the automaton to the worker. *)
      let*! backoff_cleanup_ticks in
      (* NOTE: Probably the cleanup can also be done lazily: at use, if a
         backoff time is expired, then remove it *)
      (* We only clear once every [backoff_cleanup_ticks] ticks to avoid
         iterating over the map(s) too much *)
      if Int64.(rem heartbeat_ticks (of_int backoff_cleanup_ticks)) <> 0L then
        return ()
      else
        let current = Time.now () in
        let current_with_slack =
          (* Subtract some slack time to the current time to account for
             the message latency; for details, see
             https://github.com/libp2p/go-libp2p-pubsub/issues/368 *)
          Time.sub current (Span.mul heartbeat_interval 2)
        in
        let*! backoff in
        Topic.Map.filter_map
          (fun _topic peer_backoff ->
            let peer_backoff =
              Peer.Map.filter
                (fun _peer expire -> Time.(expire > current_with_slack))
                peer_backoff
            in
            if Peer.Map.is_empty peer_backoff then None else Some peer_backoff)
          backoff
        |> set_backoff

    let clear_or_refresh_scores =
      let open Monad.Syntax in
      let*! heartbeat_ticks in
      let*! score_cleanup_ticks in
      let*! scores in
      (* We only refresh once every [score_cleanup_ticks] ticks to avoid
         iterating over the map(s) too much *)
      if Int64.(rem heartbeat_ticks (of_int score_cleanup_ticks)) <> 0L then
        return ()
      else
        Peer.Map.filter_map (fun _peer score -> Score.refresh score) scores
        |> set_scores

    let cleanup =
      let open Monad.Syntax in
      (* Clean up expired backoffs *)
      let* () = clear_backoff in

      (* Clean up or refresh scores *)
      let* () = clear_or_refresh_scores in

      (* Clean up IHave and IWant counters *)
      let* () = reset_ihave_per_heartbeat in
      let* () = reset_iwant_per_heartbeat in

      (* TODO: https://gitlab.com/tezos/tezos/-/issues/5532
         Apply IWANT request penalties *)
      return ()

    (* Update mesh for grafted and pruned peers. Note that in the Go
       implementation this update is done on-the-fly. Contrary to the Go
       implementation, we explicitly ensure that a peer cannot be grafted and
       then pruned or vice-versa. The reasoning for why that is the case is as
       follows. There are four blocks of updates in the code above:

         1) graft peers if [d_mesh < degree_low]
         2) prune peers if [d_mesh > degree_high]
         3) graft peers if [d_mesh > degree_low] and not enough outbound peers
            ([d_mesh] denotes [Peer.Set.cardinal peers])
         4) graft peers with opportunistic grafting, and remove them from the
            previously pruned peers (if they were indeed meant to be pruned).

       The condition for 1) is mutually exclusive with ones for 2) and 3).
       Now, a peer p cannot be pruned in 2) and then grafted in 3) because in 2):
         A) Either we have enough outbound peers, then we don't execute 3).
         B) Or we don't have enough outbound peers, so p is not outbound and in 3)
            we only graft outbound peers.

       Finally, block 4) ensures the property explicitly.
    *)
    let update_mesh mesh ~to_graft ~to_prune =
      let update f =
        Peer.Map.fold (fun peer topicset mesh ->
            Topic.Set.fold
              (fun topic mesh -> Topic.Map.update topic (f peer) mesh)
              topicset
              mesh)
      in
      let add_peer peer = function
        | None -> Peer.Set.singleton peer |> Option.some
        | Some peers ->
            (* Note: [peer] should not be in [peers] already *)
            Peer.Set.add peer peers |> Option.some
      in
      let remove_peer peer = function
        | None ->
            (* Note: this should not occur *)
            None
        | Some peers -> Peer.Set.remove peer peers |> Option.some
      in
      mesh |> update add_peer to_graft
      |> update remove_peer to_prune
      |> set_mesh

    (* Mesh maintenance. For each topic, do in order:

       - Prune all peers with negative score, do not enable peer exchange for
       these peers.

       - If the number of remaining peers in the topic mesh is less than
       [degree_low], then select as many random peers (not already in the mesh
       topic) to graft as possible so that to have [degree_optimal] in the topic
       mesh. The selected peers should have a non-negative score, should not be
       backed off, and should not be direct peers (and should be subscribed to
       the topic).

       - If the number of remaining peers in the topic mesh is higher than
       [degree_high], then select as many peers (not already in the mesh topic)
       to prune as possible so that to have [degree_optimal] in the topic mesh.
       See [select_peers_to_prune] to see how the selection is performed.

       - If the number of remaining peers in the topic mesh is higher than
       [degree_low] and the number of outbound peers therein is smaller than
       [degree_out], then select an additional number of peers to graft (with
       the same conditions for grafting) to ensure that there are at least
       [degree_out] outbound peers in the mesh.

       - Attempt opportunistic grafting: if the median score of the remaining
       peers is below a given threshold, then graft a few peers at random with
       score above the median and remove them from the peers to be pruned (if
       they were indeed meant to be pruned).

       Finally, for pruned peers, back them off for [prune_backoff] time.
    *)
    let maintain_mesh =
      let open Monad.Syntax in
      let*! connections in
      let*! scores in
      let*! backoff in
      let*! rng in
      let*! prune_backoff in
      let*! degree_optimal in
      let*! degree_low in
      let*! degree_high in
      let*! degree_score in
      let*! degree_out in
      let*! heartbeat_ticks in
      let*! opportunistic_graft_ticks in
      let*! opportunistic_graft_peers in
      let*! opportunistic_graft_threshold in

      let has_outbound_connection =
        peer_has_outbound_connection connections ~default:false
      in

      let get_score = get_scores_score_or_zero scores in

      (* [add_to_peers_topic_set peers_topicset topic peers] adds [topic] to
         each [peer] from the [peers] list into the [peers_topicset], which is a
         peer to topicset map. It returns the new map.

         Note that the following invariant is maintained by the caller: the
         topicset for [peer] does not already contain [topic]. *)
      let add_to_peers_topic_set map topic peers =
        List.fold_left
          (fun acc peer ->
            Peer.Map.update
              peer
              (function
                | None -> Some (Topic.Set.singleton topic)
                | Some topics -> Some (Topic.Set.add topic topics))
              acc)
          map
          peers
      in

      (* Update [to_prune] and [old_peers] as follows:
         - In [to_prune], add [topic] for each of the peers in [new_peers].
         - Remove [new_peers] from [old_peers]. *)
      let prune topic to_prune ~old_peers new_peers =
        let to_prune = add_to_peers_topic_set to_prune topic new_peers in
        let peers =
          List.fold_left
            (fun acc peer -> Peer.Set.remove peer acc)
            old_peers
            new_peers
        in
        (to_prune, peers)
      in

      let opportunistic_grafting topic peers to_prune =
        if Int64.rem heartbeat_ticks opportunistic_graft_ticks = 0L then
          let num_peers = List.length peers in
          if num_peers > 1 then
            (* Opportunistic grafting works as follows: we check the median score
               of peers in the mesh; if this score is below the
               [opportunistic_graft_threshold], we select a few peers at random
               with score over the median.

               The intention is to (slowly) improve an underperforming mesh by
               introducing good scoring peers that may have been gossiping at
               us. This allows us to get out of sticky situations where we are
               stuck with poor peers and also recover from churn of good peers. *)

            (* Compute the median peer score in the mesh. *)
            let median_score =
              let sorted_scores =
                peers |> List.rev_map get_score |> Array.of_list
              in
              Array.sort Score.compare sorted_scores ;
              sorted_scores.(num_peers / 2)
            in
            if Score.(median_score < of_float opportunistic_graft_threshold)
            then
              let peers_set = Peer.Set.of_list peers in
              let filter peer connection score =
                let in_mesh = Peer.Set.mem peer peers_set in
                let backed_off = exists_backoff topic peer backoff in
                let above_median = Score.(score > median_score) in
                let pruned =
                  match Peer.Map.find peer to_prune with
                  | None -> false
                  | Some topics -> Topic.Set.mem topic topics
                in
                (not in_mesh) && (not backed_off) && (not connection.direct)
                && above_median && not pruned
              in
              select_connections_peers
                connections
                scores
                rng
                topic
                ~filter
                ~max:opportunistic_graft_peers
            else []
          else []
        else []
      in

      (* Keep the first [degree_score] peers by score and the remaining up to
         [degree_optimal] randomly, under the constraint that we keep
         [degree_out] peers in the mesh (if we have that many). *)
      let select_peers_to_prune peers =
        (* TODO: https://gitlab.com/tezos/tezos/-/issues/5052
           Consider first selecting [degree_out] peers and then ordering on the score. *)
        (* Sort descendingly by score, but shuffle first for the case we
           don't use the score. Head of list has highest score. *)
        let peers =
          peers |> Peer.Set.elements |> List.shuffle ~rng
          |> List.sort (fun peer1 peer2 ->
                 let s1 = get_score peer1 in
                 let s2 = get_score peer2 in
                 (* Global invariant:
                    peers in mesh => peer in connections => peer in scores *)
                 Score.compare s2 s1)
        in

        let peers_high_score, peers_low_score =
          List.split_n degree_score peers
        in
        let peers_low_score = List.shuffle ~rng peers_low_score in
        let peers_to_keep, peers_to_prune =
          let to_keep, peers_to_prune =
            (* Recall that [degree_score <= degree_optimal] *)
            List.split_n (degree_optimal - degree_score) peers_low_score
          in
          (peers_high_score @ to_keep, peers_to_prune)
        in

        (* Count the outbound peers we are keeping. *)
        let outbound_peers_to_keep, inbound_peers_to_keep =
          List.partition has_outbound_connection peers_to_keep
        in
        let num_outbound_to_keep = List.length outbound_peers_to_keep in

        (* If [num_outbound] is less than [degree_out], swap some
           outbound peers from the peers to prune with inbound peers
           from the peers to keep. *)
        if num_outbound_to_keep < degree_out then
          let outbound_peers_to_prune, inbound_peers_to_prune =
            List.partition has_outbound_connection peers_to_prune
          in
          let num_outbound_to_prune = List.length outbound_peers_to_prune in
          let num_inbound_to_keep = List.length inbound_peers_to_keep in
          let num_to_swap =
            min
              (max 0 (degree_out - num_outbound_to_keep))
              (min num_outbound_to_prune num_inbound_to_keep)
          in
          if num_to_swap > 0 then
            (* We additionally prune the [num_to_swap] inbound peers among the ones with
               a low score (or were shuffled); that's why we revert [inbound_peers_to_keep]. *)
            let inbound_peers_to_prune =
              List.take_n num_to_swap (List.rev inbound_peers_to_keep)
              @ inbound_peers_to_prune
            in
            (* Actually keep [num_to_swap] outbound peers. *)
            let outbound_peers_to_prune =
              List.drop_n num_to_swap outbound_peers_to_prune
            in
            inbound_peers_to_prune @ outbound_peers_to_prune
          else peers_to_prune
        else peers_to_prune
      in

      (* [maintain_topic_mesh topic peers (to_prune, to_graft, noPX_peers)]
         maintains the mesh for [topic] where [peers] are the original [peers]
         in this topic. [to_prune], [to_graft], [noPX_peers] are the peers to be
         pruned, grafted, and those for which no peer exchange should be done
         performed, accumulated from the maintenance for other mesh topics. *)
      let maintain_topic_mesh topic peers
          (`To_prune to_prune, `To_graft to_graft, noPX_peers) =
        let to_prune, peers, noPX_peers =
          (* Drop all peers with negative score, without PX *)
          Peer.Set.fold
            (fun peer (to_prune, peers, noPX_peers) ->
              if Score.(get_score peer < zero) then
                let to_prune, peers =
                  prune topic to_prune ~old_peers:peers [peer]
                in
                let noPX_peers = Peer.Set.add peer noPX_peers in
                (to_prune, peers, noPX_peers)
              else (to_prune, peers, noPX_peers))
            peers
            (to_prune, peers, noPX_peers)
        in

        (* Do we have too few peers? *)
        let num_peers = Peer.Set.cardinal peers in
        let to_graft =
          if num_peers < degree_low then
            let max = degree_optimal - num_peers in
            (* Filter out our current and direct peers, peers we are backing
               off, and peers with negative score. *)
            let filter peer connection score =
              let in_mesh = Peer.Set.mem peer peers in
              let backed_off = exists_backoff topic peer backoff in
              (not in_mesh) && (not backed_off) && (not connection.direct)
              && Score.(score >= zero)
            in
            select_connections_peers connections scores rng topic ~filter ~max
            |> add_to_peers_topic_set to_graft topic
          else to_graft
        in

        (* Do we have too many peers? *)
        let to_prune, peers =
          if num_peers > degree_high then
            (* We'll prune [num_peers - degree_optimal] peers. *)
            select_peers_to_prune peers |> prune topic to_prune ~old_peers:peers
          else (to_prune, peers)
        in

        (* Do we have enough outbound peers? *)
        let num_peers = Peer.Set.cardinal peers in
        let to_graft, peers =
          if num_peers >= degree_low then
            let num_outbound =
              Peer.Set.fold
                (fun peer count ->
                  if has_outbound_connection peer then count + 1 else count)
                peers
                0
            in
            if num_outbound < degree_out then
              let max = degree_out - num_outbound in
              (* Filter out our current and direct peers, peers we are backing
                 off, and peers with negative score *)
              let filter peer connection score =
                let in_mesh = Peer.Set.mem peer peers in
                let backed_off = exists_backoff topic peer backoff in
                (not in_mesh) && (not backed_off) && (not connection.direct)
                && Score.(score >= zero)
                && has_outbound_connection peer
              in
              let new_peers =
                select_connections_peers
                  connections
                  scores
                  rng
                  topic
                  ~filter
                  ~max
              in
              let to_graft = add_to_peers_topic_set to_graft topic new_peers in
              (to_graft, new_peers)
            else (to_graft, Peer.Set.elements peers)
          else (to_graft, Peer.Set.elements peers)
        in

        (* Attempt opportunistic grafting. *)
        let to_graft =
          let peers_to_graft = opportunistic_grafting topic peers to_prune in
          add_to_peers_topic_set to_graft topic peers_to_graft
        in

        (`To_prune to_prune, `To_graft to_graft, noPX_peers)
      in

      let*! mesh in
      let `To_prune to_prune, `To_graft to_graft, noPX_peers =
        Topic.Map.fold
          maintain_topic_mesh
          mesh
          (`To_prune Peer.Map.empty, `To_graft Peer.Map.empty, Peer.Set.empty)
      in

      (* Update backoff for pruned peers. *)
      let* () =
        Peer.Map.fold
          (fun peer topicset backoff ->
            Topic.Set.fold
              (fun topic backoff ->
                add_backoff_for_peer prune_backoff topic peer backoff)
              topicset
              backoff)
          to_prune
          backoff
        |> set_backoff
      in
      (* Notify scoring about the grafts. *)
      let scores =
        Peer.Map.fold
          (fun peer ->
            Topic.Set.fold (fun topic ->
                update_scores_score peer (fun s -> Score.graft s topic)))
          to_graft
          scores
      in
      let* () = set_scores scores in
      (* Update mesh for grafted and pruned peers *)
      let* () = update_mesh mesh ~to_graft ~to_prune in
      return (to_graft, to_prune, noPX_peers)

    let update_fanout fanout ~to_add ~to_remove =
      let update f topic_peers_list fanout =
        List.fold_left
          (fun fanout (topic, peers) -> Topic.Map.update topic (f peers) fanout)
          fanout
          topic_peers_list
      in
      let add_peers peers_to_add = function
        | None ->
            (* impossible: in [maintain_fanout] we only consider topics that are in
               the domain of the fanout map *)
            assert false
        | Some v ->
            let peers =
              List.fold_left
                (fun peers peer -> Peer.Set.add peer peers)
                v.peers
                peers_to_add
            in
            Some {v with peers}
      in
      let remove_peers peers_to_remove = function
        | None -> (* impossible, as in the previous case *) assert false
        | Some v ->
            let peers =
              List.fold_left
                (fun peers peer -> Peer.Set.remove peer peers)
                v.peers
                peers_to_remove
            in
            Some {v with peers}
      in
      fanout |> update add_peers to_add
      |> update remove_peers to_remove
      |> set_fanout

    (* Maintain the fanout map as follows:

       - Remove topics to which the local peer has not published in the
       [fanout_ttl] time.

       - Remove peers that are not subscribed anymore or that have a score below
       [publish_threshold].

       - Remove peers that are expiring.

       - If for a topic the set of fanout peers is below [degree_optimal], then
       try to fill the map so that to have [degree_optimal] in the topic
       fanout. The selected peers should have a score above [publish_threshold]
       and should not be direct peers (and should be subscribed to the topic).
    *)
    let maintain_fanout =
      let open Monad.Syntax in
      let*! connections in
      let*! scores in
      let*! rng in
      let*! degree_optimal in
      let*! publish_threshold in
      let*! fanout_ttl in

      let expire_fanout =
        let current = Time.now () in
        (* TODO: https://gitlab.com/tezos/tezos/-/issues/5184
           Optimize by having a min and a max last published time to avoid
           traversing the map when not needed? *)
        Topic.Map.filter (fun _topic {last_published_time; peers = _} ->
            Time.(add last_published_time fanout_ttl >= current))
      in

      let maintain_topic_fanout topic {peers; _} (to_add, to_remove) =
        (* Check whether our peers are still in the topic and have a score
           above the publish threshold *)
        let peers_to_keep, peers_to_remove =
          Peer.Set.fold
            (fun peer acc ->
              match Connections.find peer connections with
              | None ->
                  (* impossible, given the global invariants on the state *)
                  assert false
              | Some connection ->
                  let score = get_scores_score_or_zero scores peer in
                  if
                    Topic.Set.mem topic connection.topics
                    && Score.(score >= of_float publish_threshold)
                  then acc
                  else
                    let peers_to_keep, peers_to_remove = acc in
                    (Peer.Set.remove peer peers_to_keep, peer :: peers_to_remove))
            peers
            (peers, [])
        in
        let to_remove = (topic, peers_to_remove) :: to_remove in

        (* Do we need more peers? *)
        let num_peers = Peer.Set.cardinal peers_to_keep in
        if num_peers < degree_optimal then
          let ineed = degree_optimal - num_peers in
          (* Filter our current and direct peers and peers with score above
             the publish threshold *)
          let filter peer connection score =
            let in_fanout = Peer.Set.mem peer peers_to_keep in
            (not in_fanout) && (not connection.direct)
            && Score.(score >= of_float publish_threshold)
          in
          let new_peers =
            select_connections_peers
              connections
              scores
              rng
              topic
              ~filter
              ~max:ineed
          in
          let to_add = (topic, new_peers) :: to_add in
          (to_add, to_remove)
        else (to_add, to_remove)
      in

      let*! fanout in
      (* Expire fanout for topics we haven't published to in a while. *)
      let fanout = expire_fanout fanout in
      let to_add, to_remove =
        Topic.Map.fold maintain_topic_fanout fanout ([], [])
      in
      (* Update the fanout map. *)
      update_fanout fanout ~to_add ~to_remove

    let prune_scores to_prune =
      let open Monad.Syntax in
      let*! scores in
      Peer.Map.fold
        (fun peer topics ->
          Topic.Set.fold
            (fun topic ->
              update_scores_score peer (fun score -> Score.prune score topic))
            topics)
        to_prune
        scores
      |> set_scores

    let graft_scores to_graft =
      let open Monad.Syntax in
      let*! scores in
      Peer.Map.fold
        (fun peer topics ->
          Topic.Set.fold
            (fun topic ->
              update_scores_score peer (fun score -> Score.graft score topic))
            topics)
        to_graft
        scores
      |> set_scores

    let handle =
      let open Monad.Syntax in
      let*! heartbeat_ticks in
      let* () = set_heartbeat_ticks (Int64.succ heartbeat_ticks) in
      (* cleaning up *)
      let* () = cleanup in

      (* Maintain the mesh for topics we have joined. Concretely, in case the
         number of peers per topic (in the mesh) is lower than [degree_low] or
         higher than [degree_high], then select peers to graft or prune
         respectively, so that the new number of peers per topic becomes
         [degree_optimal]. *)
      let* to_graft, to_prune, noPX_peers = maintain_mesh in

      (* Maintain our fanout for topics we are publishing to, but we have not
         joined. *)
      let* () = maintain_fanout in

      (* Advance the message history sliding window. *)
      let*! message_cache in
      let* () = Message_cache.shift message_cache |> set_message_cache in

      (* Notify scoring about the prunes/grafts. *)
      let* () = prune_scores to_prune in
      let* () = graft_scores to_graft in
      Heartbeat {to_graft; to_prune; noPX_peers} |> return
  end

  let heartbeat : [`Heartbeat] output Monad.t = Heartbeat.handle

  module Add_peer = struct
    let handle ~direct ~outbound peer : [`Add_peer] output Monad.t =
      let open Monad.Syntax in
      let*! connections in
      let*! scores in
      let*! score_limits in
      match Connections.add_peer peer ~direct ~outbound connections with
      | `added connections ->
          let scores =
            Peer.Map.update
              peer
              (function
                | None -> Some (Score.newly_connected score_limits)
                | Some score -> Some (Score.set_connected score))
              scores
          in
          let* () = set_connections connections in
          let* () = set_scores scores in
          return Peer_added
      | `already_known -> return Peer_already_known
  end

  let add_peer : add_peer -> [`Add_peer] output Monad.t =
   fun {direct; outbound; peer} -> Add_peer.handle ~direct ~outbound peer

  module Remove_peer = struct
    let handle peer : [`Remove_peer] output Monad.t =
      let open Monad.Syntax in
      let*! mesh in
      let mesh = Topic.Map.map (fun peers -> Peer.Set.remove peer peers) mesh in
      let* () = set_mesh mesh in
      let*! fanout in
      let fanout =
        Topic.Map.map
          (fun fanout_peers ->
            {fanout_peers with peers = Peer.Set.remove peer fanout_peers.peers})
          fanout
      in
      let* () = set_fanout fanout in
      let*! retain_duration in
      let*! scores in
      let*! connections in
      let* () = Connections.remove peer connections |> set_connections in
      let* () =
        Peer.Map.update
          peer
          (function
            | None ->
                (* This should not happen (global invariant). *)
                None
            | Some score -> Score.remove_peer score ~retain_duration)
          scores
        |> set_scores
      in
      Removing_peer |> return
  end

  let remove_peer : remove_peer -> [`Remove_peer] output Monad.t =
   fun {peer} -> Remove_peer.handle peer

  (* On spam protection
     ==================

     We make the assumption that the bandwidth per peer can be limited. This
     means that if the automaton allows a peer to send us an unbounded number of
     messages, this is not a case of concern by itself. There is a case of
     concern if this results in wasting space or CPU time.

     Another assumption we make is that messages are of bounded size, because
     the message encoding should ensure this.

     We detail next for each p2p message type what checks are (or are not) in
     place concerning the receipt of "spam" and what are the concerns.

     IHave messages:
     - There is a maximum number, given by [max_recv_ihave_per_heartbeat], of
       IHave messages that a peer accepts from another peer between two
       heartbeat ticks; receiving more than this limit is not punished, the
       automaton simply returns [Too_many_recv_ihave_messages].
     - The number of message ids included is not checked, even though the remote
       peer should include at most [max_sent_iwant_per_heartbeat] ids.

     IWant messages:
     - There is a maximum number, given by [max_gossip_retransmission], of times
       that a peer can request the *same* message (during the time the message is
       in the message cache).
     - There is no imposed bound on the number of IWant messages received from a peer.
     - The number of message ids included is not checked, though the remote peer
       should include at most [max_sent_iwant_per_heartbeat] ids between two
       heartbeat ticks.

     Note on IHave/IWant messages: heartbeats of different peers need not be in
     sync. Therefore we cannot use the limits supposedly used by the sender on
     the receiver side to punish the sender; we could multiply these limits by 2
     though.

     Graft messages:
     - Receiving a Graft message within the backoff period (given by
       [prune_backoff] or [unsubscribed_backoff]) is punished. Receiving such a
       Graft very soon (before [last_prune_time + graft_flood_threshold]) is punished
       further.
     - Receiving a duplicate of an accepted Graft request is not punished, the
       automaton simply returns [Peer_already_in_mesh]. In all other failure cases
       (except for unconnected peers) a backoff is set.

     Prune messages:
     - In case of a duplicate Prune the automaton simply returns
       [Peer_not_in_mesh] because the peer has already been removed from the mesh
       following the first Prune message.
     - The number of PX peers is not bounded. However, the remote peer should
       advertise at most [peers_to_px].

     Subscribe/Unsubscribe messages:
     - Duplicates are not checked for, a duplicate Subscribe/Unsubscribed (for a
       connected peer) is always successful.

     Full messages:
     - We check for duplicates with the message cache. A duplicate is not re-routed.
     - A peer sending duplicates within a small window (given by
       [mesh_message_deliveries_window] of a few ms) is rewarded because they
       count as near-first deliveries. Duplicates received outside of this window
       are not counted.
     - Potential problems:
       - receiving a large number of duplicates, as all duplicate message ids
         are stored in the cache *)

  (* "Getters" *)

  let select_px_peers state ~peer_to_prune topic ~noPX_peers =
    let do_px = do_px state in
    let peers_to_px = peers_to_px state in
    let filter peer _conn score =
      (not (Peer.equal peer_to_prune peer)) && Score.(score >= zero)
    in
    if do_px && not (Peer.Set.mem peer_to_prune noPX_peers) then
      select_connections_peers
        state.connections
        state.scores
        state.rng
        topic
        ~filter
        ~max:peers_to_px
    else []

  let select_gossip_messages state =
    let rng = state.rng in
    let select_gossip_for_peer message_ids =
      (* We shuffle the message ids so that we emit a different set for each
         peer. *)
      (* TODO: https://gitlab.com/tezos/tezos/-/issues/5396
         Can this be optimized? *)
      message_ids |> List.shuffle ~rng
      |> List.take_n state.limits.max_sent_iwant_per_heartbeat
    in
    let select_gossip_for_topic topic excluded_peers =
      let message_ids =
        Message_cache.get_message_ids_to_gossip topic state.message_cache
      in
      if message_ids = [] then []
      else
        (* We collect the peers with a score above [gossip_threshold] that are not
           in the excluded set and are not direct peers. *)
        let filter peer {direct; _} score =
          (not direct)
          && Score.(score >= of_float state.limits.gossip_threshold)
          && not (Peer.Set.mem peer excluded_peers)
        in

        (* We first select all peers satisfying the criterion and then we see if
           we have too many. *)
        let peers =
          select_connections_peers
            state.connections
            state.scores
            rng
            topic
            ~filter
            ~max:Int.max_int
        in
        let num_peers = List.length peers in
        let target_num =
          max
            state.limits.degree_lazy
            (int_of_float
               (state.limits.gossip_factor *. float_of_int num_peers))
        in
        let selected_peers = List.take_n target_num peers in

        (* Prepare the IHave gossip to the selected peers. *)
        List.fold_left
          (fun messages peer ->
            let message_ids = select_gossip_for_peer message_ids in
            {peer; topic; message_ids} :: messages)
          []
          selected_peers
    in

    (* Prepare the IHave gossip for each topic in the mesh or fanout maps. Peers
       in the mesh/fanout are excluded from gossip (because we send full
       messages to them). *)
    let add_gossip_for_topic topic peers gossip_msgs =
      let new_msgs = select_gossip_for_topic topic peers in
      List.rev_append new_msgs gossip_msgs
    in

    Topic.Map.fold add_gossip_for_topic state.mesh []
    |> Topic.Map.fold
         (fun topic {peers; _} -> add_gossip_for_topic topic peers)
         state.fanout

  (* Helpers. *)

  let pp_add_peer fmtr ({direct; outbound; peer} : add_peer) =
    let open Format in
    fprintf
      fmtr
      "{ direct=%b; outbound=%b; peer=%a }"
      direct
      outbound
      Peer.pp
      peer

  let pp_remove_peer fmtr ({peer} : remove_peer) =
    let open Format in
    fprintf fmtr "{ peer=%a }" Peer.pp peer

  let pp_ihave fmtr ({peer; topic; message_ids} : ihave) =
    let open Format in
    fprintf
      fmtr
      "{ peer=%a; topic=%a; message_ids=[%a] }"
      Peer.pp
      peer
      Topic.pp
      topic
      (pp_print_list ~pp_sep:(fun fmtr () -> fprintf fmtr ";") Message_id.pp)
      message_ids

  let pp_iwant fmtr ({peer : Peer.t; message_ids : Message_id.t list} : iwant) =
    let open Format in
    fprintf
      fmtr
      "{ peer=%a; message_ids=[%a] }"
      Peer.pp
      peer
      (pp_print_list ~pp_sep:(fun fmtr () -> fprintf fmtr ";") Message_id.pp)
      message_ids

  let pp_graft fmtr ({peer; topic} : graft) =
    let open Format in
    fprintf fmtr "{ peer=%a; topic=%a }" Peer.pp peer Topic.pp topic

  let pp_prune fmtr ({peer; topic; px; backoff} : prune) =
    let open Format in
    fprintf
      fmtr
      "{ peer=%a; topic=%a; px=[%a]; backoff=%a }"
      Peer.pp
      peer
      Topic.pp
      topic
      (pp_print_list ~pp_sep:(fun fmtr () -> fprintf fmtr ";") Peer.pp)
      (List.of_seq px)
      Span.pp
      backoff

  let pp_receive_message fmtr
      ({sender; topic; message_id; message} : receive_message) =
    let open Format in
    fprintf
      fmtr
      "{ sender=%a; topic=%a; message_id=%a; message=%a }"
      Peer.pp
      sender
      Topic.pp
      topic
      Message_id.pp
      message_id
      Message.pp
      message

  let pp_publish_message fmtr ({topic; message_id; message} : publish_message) =
    let open Format in
    fprintf
      fmtr
      "{ topic=%a; message_id=%a; message=%a }"
      Topic.pp
      topic
      Message_id.pp
      message_id
      Message.pp
      message

  let pp_join fmtr ({topic} : join) =
    let open Format in
    fprintf fmtr "{ topic=%a }" Topic.pp topic

  let pp_leave fmtr ({topic} : leave) =
    let open Format in
    fprintf fmtr "{ topic=%a }" Topic.pp topic

  let pp_subscribe fmtr ({topic; peer} : subscribe) =
    let open Format in
    fprintf fmtr "{ topic=%a; peer=%a }" Topic.pp topic Peer.pp peer

  let pp_unsubscribe fmtr ({topic; peer} : unsubscribe) =
    let open Format in
    fprintf fmtr "{ topic=%a; peer=%a }" Topic.pp topic Peer.pp peer

  let pp_set_application_score fmtr ({peer; score} : set_application_score) =
    let open Format in
    fprintf fmtr "{ peer=%a; score=%g }" Peer.pp peer score

  let pp_peer_map pp_elt =
    Fmt.Dump.iter_bindings Peer.Map.iter Fmt.nop Peer.pp pp_elt

  let pp_message_id_map pp_elt =
    Fmt.Dump.iter_bindings Message_id.Map.iter Fmt.nop Message_id.pp pp_elt

  let pp_peer_set = Fmt.Dump.iter Peer.Set.iter Fmt.nop Peer.pp

  let pp_topic_set = Fmt.Dump.iter Topic.Set.iter Fmt.nop Topic.pp

  let pp_topic_map pp_elt =
    Fmt.Dump.iter_bindings Topic.Map.iter Fmt.nop Topic.pp pp_elt

  let pp_output (type a) fmtr (o : a output) =
    let open Format in
    match o with
    | Ihave_from_peer_with_low_score {score; threshold} ->
        let r = (score, threshold) in
        fprintf
          fmtr
          "Negative_peer_score %a"
          Fmt.Dump.(
            record [field "score" fst Score.pp; field "threshold" snd Fmt.float])
          r
    | Too_many_recv_ihave_messages {count; max} ->
        fprintf
          fmtr
          "Too_many_recv_ihave_messages { count=%d; max=%d }"
          count
          max
    | Too_many_sent_iwant_messages {count; max} ->
        fprintf
          fmtr
          "Too_many_sent_iwant_messages { count=%d; max=%d }"
          count
          max
    | Message_topic_not_tracked -> fprintf fmtr "Message_topic_not_tracked"
    | Invalid_message_id -> fprintf fmtr "Invalid_message_id"
    | Message_requested_message_ids ids ->
        fprintf
          fmtr
          "Message_requested_message_ids %a"
          (Fmt.Dump.list Message_id.pp)
          ids
    | Iwant_from_peer_with_low_score {score; threshold} ->
        let r = (score, threshold) in
        fprintf
          fmtr
          "Iwant_from_peer_with_low_score %a"
          Fmt.Dump.(
            record [field "score" fst Score.pp; field "threshold" snd Fmt.float])
          r
    | On_iwant_messages_to_route {routed_message_ids} ->
        let pp_elt fmtr tag =
          match tag with
          | `Ignored -> fprintf fmtr "ignored"
          | `Message m -> fprintf fmtr "message(%a)" Message.pp m
          | `Not_found -> fprintf fmtr "not_found"
          | `Too_many_requests -> fprintf fmtr "too_many_requests"
        in
        fprintf
          fmtr
          "On_iwant_messages_to_route %a"
          (pp_message_id_map pp_elt)
          routed_message_ids
    | Peer_filtered -> fprintf fmtr "Peer_filtered"
    | Unsubscribed_topic -> fprintf fmtr "Unsubscribed_topic"
    | Peer_already_in_mesh -> fprintf fmtr "Peer_already_in_mesh"
    | Grafting_direct_peer -> fprintf fmtr "Grafting_direct_peer"
    | Unexpected_grafting_peer -> fprintf fmtr "Unexpected_grafting_peer"
    | Grafting_peer_with_negative_score ->
        fprintf fmtr "Grafting_peer_with_negative_score"
    | Grafting_successfully -> fprintf fmtr "Grafting_successfully"
    | Peer_backed_off -> fprintf fmtr "Peer_backed_off"
    | Mesh_full -> fprintf fmtr "Mesh_full"
    | Prune_topic_not_tracked -> fprintf fmtr "Prune_topic_not_tracked"
    | Peer_not_in_mesh -> fprintf fmtr "Peer_not_in_mesh"
    | Ignore_PX_score_too_low score ->
        fprintf fmtr "Ignore_PX_score_too_low %a" Score.pp score
    | No_PX -> fprintf fmtr "No_PX"
    | PX peer_set -> fprintf fmtr "PX %a" pp_peer_set peer_set
    | Publish_message {to_publish} ->
        fprintf
          fmtr
          "Publish_message %a"
          Fmt.Dump.(record [field "to_publish" Fun.id pp_peer_set])
          to_publish
    | Already_published -> fprintf fmtr "Already_published"
    | Invalid_message -> fprintf fmtr "Invalid_message"
    | Unknown_validity -> fprintf fmtr "Unknown_validity"
    | Route_message {to_route} ->
        fprintf
          fmtr
          "Route_message %a"
          Fmt.Dump.(record [field "to_route" Fun.id pp_peer_set])
          to_route
    | Already_received -> fprintf fmtr "Already_received"
    | Not_subscribed -> fprintf fmtr "Not_subscribed"
    | Already_joined -> fprintf fmtr "Already_joined"
    | Joining_topic {to_graft} ->
        fprintf fmtr "Joining_topic %a" pp_peer_set to_graft
    | Not_joined -> fprintf fmtr "Not_joined"
    | Leaving_topic {to_prune; noPX_peers} ->
        let p = (to_prune, noPX_peers) in
        fprintf
          fmtr
          "Leaving_topic %a"
          Fmt.Dump.(
            record
              [
                field "to_prune" fst pp_peer_set;
                field "noPX_peers" snd pp_peer_set;
              ])
          p
    | Heartbeat {to_graft; to_prune; noPX_peers} ->
        let r = (to_graft, to_prune, noPX_peers) in
        Fmt.pf
          fmtr
          "Heartbeat %a"
          Fmt.Dump.(
            record
              [
                field
                  "to_graft"
                  (fun (to_graft, _, _) -> to_graft)
                  (pp_peer_map pp_topic_set);
                field
                  "to_prune"
                  (fun (_, to_prune, _) -> to_prune)
                  (pp_peer_map pp_topic_set);
                field
                  "noPX_peers"
                  (fun (_, _, noPX_peers) -> noPX_peers)
                  pp_peer_set;
              ])
          r
    | Peer_added -> fprintf fmtr "Peer_added"
    | Peer_already_known -> fprintf fmtr "Peer_already_known"
    | Removing_peer -> fprintf fmtr "Removing_peer"
    | Subscribed -> fprintf fmtr "Subscribed"
    | Subscribe_to_unknown_peer -> fprintf fmtr "Subscribe_to_unknown_peer"
    | Unsubscribed -> fprintf fmtr "Unsubscribed"
    | Unsubscribe_from_unknown_peer ->
        fprintf fmtr "Unsubscribe_from_unknown_peer"
    | Set_application_score -> fprintf fmtr "Set_application_score"

  module Introspection = struct
    (* This module reexport datatypes so that it can be used for
       introspection. While at the moment, this module reexport purely
       the datatype, we can decide that for abstraction purpose, we do
       not export all those fields in the future. This may have a
       small overhead cost, but for introspection it should be
       irrelevant. *)

    type nonrec connection = connection = {
      topics : Topic.Set.t;
      direct : bool;
      outbound : bool;
    }

    type nonrec fanout_peers = fanout_peers = {
      peers : Peer.Set.t;
      last_published_time : time;
    }

    module Message_cache = Message_cache
    module Connections = Connections

    type view = state = {
      limits : limits;
      parameters : parameters;
      connections : Connections.t;
      scores : Score.t Peer.Map.t;
      ihave_per_heartbeat : int Peer.Map.t;
      iwant_per_heartbeat : int Peer.Map.t;
      mesh : Peer.Set.t Topic.Map.t;
      fanout : fanout_peers Topic.Map.t;
      backoff : time Peer.Map.t Topic.Map.t;
      message_cache : Message_cache.t;
      rng : Random.State.t;
      heartbeat_ticks : int64;
    }

    let view state = state

    type connected_peers_filter =
      | Direct
      | Subscribed_to of Topic.t
      | Score_above of {threshold : Score.value}
    (* Add other cases here if needed. *)

    let connected_peers_filter _peer connection score = function
      | Direct -> connection.direct
      | Subscribed_to topic -> Topic.Set.mem topic connection.topics
      | Score_above {threshold} -> Score.(score >= threshold)

    let get_connected_peers =
      let rec filter_rec peer connection score = function
        | [] -> true
        | filter :: filters ->
            connected_peers_filter peer connection score filter
            && filter_rec peer connection score filters
      in
      fun ?(filters = []) view ->
        Connections.fold
          (fun peer connection acc ->
            let score = get_score_or_zero peer view in
            if filter_rec peer connection score filters then peer :: acc
            else acc)
          view.connections
          []

    let get_peers_in_topic_mesh topic state =
      match Topic.Map.find topic state.mesh with
      | None -> []
      | Some peers -> Peer.Set.elements peers

    let get_subscribed_topics peer state =
      match Connections.find peer state.connections with
      | None -> []
      | Some connection -> Topic.Set.elements connection.topics

    let get_our_topics state =
      Topic.Map.fold (fun topic _peers acc -> topic :: acc) state.mesh []

    let get_fanout_peers topic state =
      match Topic.Map.find topic state.fanout with
      | None -> []
      | Some fanout_peers -> Peer.Set.elements fanout_peers.peers

    let get_peer_score peer {scores; _} =
      match Peer.Map.find peer scores with
      | None -> Score.zero
      | Some score -> Score.value score

    let get_peer_ihave_per_heartbeat peer {ihave_per_heartbeat; _} =
      match Peer.Map.find peer ihave_per_heartbeat with
      | None ->
          (* Return 0 as this happens when no IHaves have been received from [peer]. *)
          0
      | Some count -> count

    let get_peer_iwant_per_heartbeat peer {iwant_per_heartbeat; _} =
      match Peer.Map.find peer iwant_per_heartbeat with
      | None ->
          (* Return 0 as this happens when no IWants have been sent to [peer]. *)
          0
      | Some count -> count

    let get_peer_backoff topic peer {backoff; _} =
      match Topic.Map.find topic backoff with
      | None -> None
      | Some backoffs -> Peer.Map.find peer backoffs

    let limits state = state.limits

    let has_joined topic {mesh; _} = Topic.Map.mem topic mesh

    let in_mesh topic peer {mesh; _} =
      match Topic.Map.find topic mesh with
      | None -> false
      | Some topic_mesh -> Peer.Set.mem peer topic_mesh

    let is_direct peer {connections; _} =
      match Connections.find peer connections with
      | None -> false
      | Some {direct; _} -> direct

    let is_outbound peer {connections; _} =
      match Connections.find peer connections with
      | None -> false
      | Some {outbound; _} -> outbound

    let pp_connection fmtr c =
      let fields =
        List.concat
          [
            (if Topic.Set.is_empty c.topics then []
            else [Fmt.field "topics" (fun c -> c.topics) pp_topic_set]);
            [Fmt.field "direct" (fun c -> c.direct) Fmt.bool];
            [Fmt.field "outbound" (fun c -> c.outbound) Fmt.bool];
          ]
      in
      Fmt.record fields fmtr c

    let pp_connections =
      Fmt.Dump.iter_bindings Connections.iter Fmt.nop Peer.pp pp_connection

    let pp_scores =
      Fmt.Dump.iter_bindings Peer.Map.iter Fmt.nop Peer.pp Score.pp_value

    (* re-export printers *)
    let pp_peer_map = pp_peer_map

    let pp_message_id_map = pp_message_id_map

    let pp_topic_map = pp_topic_map

    let pp_peer_set = pp_peer_set

    let pp_topic_set = pp_topic_set
  end
end
OCaml

Innovation. Community. Security.