package tezos-protocol-020-PsParisC

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

Source file validate.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
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com>                *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Validate_errors
open Alpha_context

type consensus_info = {
  predecessor_level : Raw_level.t;
  predecessor_round : Round.t;
  preattestation_slot_map : (Consensus_key.pk * int * int) Slot.Map.t option;
  attestation_slot_map : (Consensus_key.pk * int * int) Slot.Map.t option;
}

let init_consensus_info ctxt (predecessor_level, predecessor_round) =
  {
    predecessor_level;
    predecessor_round;
    preattestation_slot_map = Consensus.allowed_preattestations ctxt;
    attestation_slot_map = Consensus.allowed_attestations ctxt;
  }

(** Map used to detect consensus operation conflicts. Each delegate may
    (pre)attest at most once for each level and round, so two attestations
    (resp. two DAL attestations or two preattestations) conflict when they have
    the same slot, level, and round.

    Note that when validating the mempool, several (DAL/pre)attestations by the
    same delegate at the same level and round would not conflict if they have
    different Tenderbake slots.

    Note that when validating a block, all attestations (resp. all
    preattestations) must have the same level and round anyway, so only
    the slot is relevant. Taking the level and round into account is
    useful in mempool mode, because we want to be able to accept and
    propagate consensus operations for multiple close
    past/future/cousin blocks. *)
module Consensus_conflict_map = Map.Make (struct
  type t = Slot.t * Raw_level.t * Round.t

  let compare (slot1, level1, round1) (slot2, level2, round2) =
    Compare.or_else (Raw_level.compare level1 level2) @@ fun () ->
    Compare.or_else (Slot.compare slot1 slot2) @@ fun () ->
    Round.compare round1 round2
end)

type consensus_state = {
  preattestations_seen : Operation_hash.t Consensus_conflict_map.t;
  attestations_seen : Operation_hash.t Consensus_conflict_map.t;
}

let consensus_conflict_map_encoding =
  let open Data_encoding in
  conv
    (fun map -> Consensus_conflict_map.bindings map)
    (fun l ->
      Consensus_conflict_map.(
        List.fold_left (fun m (k, v) -> add k v m) empty l))
    (list
       (tup2
          (tup3 Slot.encoding Raw_level.encoding Round.encoding)
          Operation_hash.encoding))

let consensus_state_encoding =
  let open Data_encoding in
  def "consensus_state"
  @@ conv
       (fun {preattestations_seen; attestations_seen} ->
         (preattestations_seen, attestations_seen))
       (fun (preattestations_seen, attestations_seen) ->
         {preattestations_seen; attestations_seen})
       (obj2
          (req "preattestations_seen" consensus_conflict_map_encoding)
          (req "attestations_seen" consensus_conflict_map_encoding))

let empty_consensus_state =
  {
    preattestations_seen = Consensus_conflict_map.empty;
    attestations_seen = Consensus_conflict_map.empty;
  }

type voting_state = {
  proposals_seen : Operation_hash.t Signature.Public_key_hash.Map.t;
      (** To each delegate that has submitted a Proposals operation in a
          previously validated operation, associates the hash of this
          operation. This includes Proposals from a potential Testnet
          Dictator. *)
  ballots_seen : Operation_hash.t Signature.Public_key_hash.Map.t;
      (** To each delegate that has submitted a ballot in a previously
          validated operation, associates the hash of this operation.  *)
}

let voting_state_encoding =
  let open Data_encoding in
  def "voting_state"
  @@ conv
       (fun {proposals_seen; ballots_seen} -> (proposals_seen, ballots_seen))
       (fun (proposals_seen, ballots_seen) -> {proposals_seen; ballots_seen})
       (obj2
          (req
             "proposals_seen"
             (Signature.Public_key_hash.Map.encoding Operation_hash.encoding))
          (req
             "ballots_seen"
             (Signature.Public_key_hash.Map.encoding Operation_hash.encoding)))

module Double_baking_evidence_map = struct
  include Map.Make (struct
    type t = Raw_level.t * Round.t

    let compare (l, r) (l', r') =
      Compare.or_else (Raw_level.compare l l') @@ fun () ->
      Compare.or_else (Round.compare r r') @@ fun () -> 0
  end)

  let encoding elt_encoding =
    Data_encoding.conv
      (fun map -> bindings map)
      (fun l -> List.fold_left (fun m (k, v) -> add k v m) empty l)
      Data_encoding.(
        list (tup2 (tup2 Raw_level.encoding Round.encoding) elt_encoding))
end

module Double_operation_evidence_map = struct
  include Map.Make (struct
    type t = Raw_level.t * Round.t * Slot.t * Misbehaviour.kind

    let compare (l, r, s, k) (l', r', s', k') =
      Compare.or_else (Raw_level.compare l l') @@ fun () ->
      Compare.or_else (Round.compare r r') @@ fun () ->
      Compare.or_else (Slot.compare s s') @@ fun () ->
      Misbehaviour.compare_kind k k'
  end)

  let encoding elt_encoding =
    Data_encoding.conv
      (fun map -> bindings map)
      (fun l -> List.fold_left (fun m (k, v) -> add k v m) empty l)
      Data_encoding.(
        list
          (tup2
             (tup4
                Raw_level.encoding
                Round.encoding
                Slot.encoding
                Misbehaviour.kind_encoding)
             elt_encoding))
end

(** State used and modified when validating anonymous operations.
    These fields are used to enforce that we do not validate the same
    operation multiple times.

    Note that as part of {!state}, these maps live
    in memory. They are not explicitly bounded here, however:

    - In block validation mode, they are bounded by the number of
    anonymous operations allowed in the block.

    - In mempool mode, bounding the number of operations in this map
    is the responsability of the prevalidator on the shell side. *)
type anonymous_state = {
  activation_pkhs_seen : Operation_hash.t Ed25519.Public_key_hash.Map.t;
  double_baking_evidences_seen : Operation_hash.t Double_baking_evidence_map.t;
  double_attesting_evidences_seen :
    Operation_hash.t Double_operation_evidence_map.t;
  seed_nonce_levels_seen : Operation_hash.t Raw_level.Map.t;
  vdf_solution_seen : Operation_hash.t option;
}

let raw_level_map_encoding elt_encoding =
  let open Data_encoding in
  conv
    (fun map -> Raw_level.Map.bindings map)
    (fun l ->
      Raw_level.Map.(List.fold_left (fun m (k, v) -> add k v m) empty l))
    (list (tup2 Raw_level.encoding elt_encoding))

let anonymous_state_encoding =
  let open Data_encoding in
  def "anonymous_state"
  @@ conv
       (fun {
              activation_pkhs_seen;
              double_baking_evidences_seen;
              double_attesting_evidences_seen;
              seed_nonce_levels_seen;
              vdf_solution_seen;
            } ->
         ( activation_pkhs_seen,
           double_baking_evidences_seen,
           double_attesting_evidences_seen,
           seed_nonce_levels_seen,
           vdf_solution_seen ))
       (fun ( activation_pkhs_seen,
              double_baking_evidences_seen,
              double_attesting_evidences_seen,
              seed_nonce_levels_seen,
              vdf_solution_seen ) ->
         {
           activation_pkhs_seen;
           double_baking_evidences_seen;
           double_attesting_evidences_seen;
           seed_nonce_levels_seen;
           vdf_solution_seen;
         })
       (obj5
          (req
             "activation_pkhs_seen"
             (Ed25519.Public_key_hash.Map.encoding Operation_hash.encoding))
          (req
             "double_baking_evidences_seen"
             (Double_baking_evidence_map.encoding Operation_hash.encoding))
          (req
             "double_attesting_evidences_seen"
             (Double_operation_evidence_map.encoding Operation_hash.encoding))
          (req
             "seed_nonce_levels_seen"
             (raw_level_map_encoding Operation_hash.encoding))
          (opt "vdf_solution_seen" Operation_hash.encoding))

let empty_anonymous_state =
  {
    activation_pkhs_seen = Ed25519.Public_key_hash.Map.empty;
    double_baking_evidences_seen = Double_baking_evidence_map.empty;
    double_attesting_evidences_seen = Double_operation_evidence_map.empty;
    seed_nonce_levels_seen = Raw_level.Map.empty;
    vdf_solution_seen = None;
  }

(** Static information used to validate manager operations. *)
type manager_info = {
  hard_storage_limit_per_operation : Z.t;
  hard_gas_limit_per_operation : Gas.Arith.integral;
}

let init_manager_info ctxt =
  {
    hard_storage_limit_per_operation =
      Constants.hard_storage_limit_per_operation ctxt;
    hard_gas_limit_per_operation = Constants.hard_gas_limit_per_operation ctxt;
  }

(** State used and modified when validating manager operations. *)
type manager_state = {
  managers_seen : Operation_hash.t Signature.Public_key_hash.Map.t;
      (** To enforce the one-operation-per manager-per-block restriction
          (1M). The operation hash lets us indicate the conflicting
          operation in the {!Manager_restriction} error.

          Note that as part of {!state}, this map
          lives in memory. It is not explicitly bounded here, however:

          - In block validation mode, it is bounded by the number of
            manager operations allowed in the block.

          - In mempool mode, bounding the number of operations in this
            map is the responsability of the mempool. (E.g. the plugin used
            by Octez has a [max_prechecked_manager_operations] parameter to
            ensure this.) *)
}

let manager_state_encoding =
  let open Data_encoding in
  def "manager_state"
  @@ conv
       (fun {managers_seen} -> managers_seen)
       (fun managers_seen -> {managers_seen})
       (obj1
          (req
             "managers_seen"
             (Signature.Public_key_hash.Map.encoding Operation_hash.encoding)))

let empty_manager_state = {managers_seen = Signature.Public_key_hash.Map.empty}

(** Information needed to validate consensus operations and/or to
    finalize the block in both modes that handle a preexisting block:
    [Application] and [Partial_validation]. *)
type block_info = {
  round : Round.t;
  locked_round : Round.t option;
  predecessor_hash : Block_hash.t;
  header_contents : Block_header.contents;
}

(** Information needed to validate consensus operations and/or to
    finalize the block in [Construction] mode. *)
type construction_info = {
  round : Round.t;
  predecessor_hash : Block_hash.t;
  header_contents : Block_header.contents;
}

(** Circumstances in which operations are validated, and corresponding
    specific information.

    If you add a new mode, please make sure that it has a way to bound
    the size of the maps in the {!operation_conflict_state}. *)
type mode =
  | Application of block_info
      (** [Application] is used for the validation of a preexisting block,
          often in preparation for its future application. *)
  | Partial_validation of block_info
      (** [Partial_validation] is used to quickly but partially validate a
          preexisting block, e.g. to quickly decide whether an alternate
          branch seems viable. In this mode, the initial {!type:context} may
          come from an ancestor block instead of the predecessor block. Only
          consensus operations are validated in this mode. *)
  | Construction of construction_info
      (** Used for the construction of a new block. *)
  | Mempool
      (** Used by the mempool ({!module:Mempool_validation}) and by the
          [Partial_construction] mode in {!module:Main}, which may itself be
          used by RPCs or by another mempool implementation. *)

(** {2 Definition and initialization of [info] and [state]} *)

type info = {
  ctxt : t;  (** The context at the beginning of the block or mempool. *)
  mode : mode;
  chain_id : Chain_id.t;  (** Needed for signature checks. *)
  current_level : Level.t;
  consensus_info : consensus_info option;
      (** Needed to validate consensus operations. This can be [None] during
          some RPC calls when some predecessor information is unavailable,
          in which case the validation of all consensus operations will
          systematically fail. *)
  manager_info : manager_info;
}

type operation_conflict_state = {
  consensus_state : consensus_state;
  voting_state : voting_state;
  anonymous_state : anonymous_state;
  manager_state : manager_state;
}

let operation_conflict_state_encoding =
  let open Data_encoding in
  def "operation_conflict_state"
  @@ conv
       (fun {consensus_state; voting_state; anonymous_state; manager_state} ->
         (consensus_state, voting_state, anonymous_state, manager_state))
       (fun (consensus_state, voting_state, anonymous_state, manager_state) ->
         {consensus_state; voting_state; anonymous_state; manager_state})
       (obj4
          (req "consensus_state" consensus_state_encoding)
          (req "voting_state" voting_state_encoding)
          (req "anonymous_state" anonymous_state_encoding)
          (req "manager_state" manager_state_encoding))

type block_state = {
  op_count : int;
  remaining_block_gas : Gas.Arith.fp;
  recorded_operations_rev : Operation_hash.t list;
  last_op_validation_pass : int option;
  locked_round_evidence : (Round.t * int) option;
  attestation_power : int;
}

type validation_state = {
  info : info;
  operation_state : operation_conflict_state;
  block_state : block_state;
}

let ok_unit = Result_syntax.return_unit

let result_error = Result_syntax.tzfail

let init_info ctxt mode chain_id ~predecessor_level_and_round =
  let consensus_info =
    Option.map (init_consensus_info ctxt) predecessor_level_and_round
  in
  {
    ctxt;
    mode;
    chain_id;
    current_level = Level.current ctxt;
    consensus_info;
    manager_info = init_manager_info ctxt;
  }

let empty_voting_state =
  {
    proposals_seen = Signature.Public_key_hash.Map.empty;
    ballots_seen = Signature.Public_key_hash.Map.empty;
  }

let empty_operation_conflict_state =
  {
    consensus_state = empty_consensus_state;
    voting_state = empty_voting_state;
    anonymous_state = empty_anonymous_state;
    manager_state = empty_manager_state;
  }

let init_block_state vi =
  {
    op_count = 0;
    remaining_block_gas =
      Gas.Arith.fp (Constants.hard_gas_limit_per_block vi.ctxt);
    recorded_operations_rev = [];
    last_op_validation_pass = None;
    locked_round_evidence = None;
    attestation_power = 0;
  }

let get_initial_ctxt {info; _} = info.ctxt

(** Validation of consensus operations (validation pass [0]):
    preattestations and attestations. *)
module Consensus = struct
  open Validate_errors.Consensus

  let check_delegate_is_not_forbidden ctxt delegate_pkh =
    fail_when
      (Delegate.is_forbidden_delegate ctxt delegate_pkh)
      (Forbidden_delegate delegate_pkh)

  let get_delegate_details slot_map kind slot =
    let open Result_syntax in
    match slot_map with
    | None -> tzfail (Consensus.Slot_map_not_found {loc = __LOC__})
    | Some slot_map -> (
        match Slot.Map.find slot slot_map with
        | None -> tzfail (Wrong_slot_used_for_consensus_operation {kind})
        | Some x -> return x)

  (** When validating a block (ie. in [Application],
      [Partial_validation], and [Construction] modes), any
      preattestations must point to a round that is strictly before the
      block's round. *)
  let check_round_before_block ~block_round provided =
    error_unless
      Round.(provided < block_round)
      (Preattestation_round_too_high {block_round; provided})

  let check_level kind expected provided =
    (* We use [if] instead of [error_unless] to avoid computing the
       error when it is not needed. *)
    if Raw_level.equal expected provided then Result.return_unit
    else if Raw_level.(expected > provided) then
      result_error
        (Consensus_operation_for_old_level {kind; expected; provided})
    else
      result_error
        (Consensus_operation_for_future_level {kind; expected; provided})

  let check_round kind expected provided =
    (* We use [if] instead of [error_unless] to avoid computing the
       error when it is not needed. *)
    if Round.equal expected provided then ok_unit
    else if Round.(expected > provided) then
      result_error
        (Consensus_operation_for_old_round {kind; expected; provided})
    else
      result_error
        (Consensus_operation_for_future_round {kind; expected; provided})

  let check_payload_hash kind expected provided =
    error_unless
      (Block_payload_hash.equal expected provided)
      (Wrong_payload_hash_for_consensus_operation {kind; expected; provided})

  (** Preattestation checks for both [Application] and
      [Partial_validation] modes.

      Return the slot owner's consensus key and voting power. *)
  let check_preexisting_block_preattestation vi consensus_info block_info
      {level; round; block_payload_hash = bph; slot} =
    let open Lwt_result_syntax in
    let*? locked_round =
      match block_info.locked_round with
      | Some locked_round -> Ok locked_round
      | None ->
          (* A preexisting block whose fitness has no locked round
             should contain no preattestations. *)
          error Unexpected_preattestation_in_block
    in
    let kind = Preattestation in
    let*? () = check_round_before_block ~block_round:block_info.round round in
    let*? () = check_level kind vi.current_level.level level in
    let*? () = check_round kind locked_round round in
    let expected_payload_hash = block_info.header_contents.payload_hash in
    let*? () = check_payload_hash kind expected_payload_hash bph in
    let*? consensus_key, voting_power, _dal_power =
      get_delegate_details consensus_info.preattestation_slot_map kind slot
    in
    return (consensus_key, voting_power)

  (** Preattestation checks for Construction mode.

      Return the slot owner's consensus key and voting power. *)
  let check_constructed_block_preattestation vi consensus_info cons_info
      {level; round; block_payload_hash = bph; slot} =
    let open Lwt_result_syntax in
    let expected_payload_hash = cons_info.header_contents.payload_hash in
    let*? () =
      (* When the proposal is fresh, a fake [payload_hash] of [zero]
         has been provided. In this case, the block should not contain
         any preattestations. *)
      error_when
        Block_payload_hash.(expected_payload_hash = zero)
        Unexpected_preattestation_in_block
    in
    let kind = Preattestation in
    let*? () = check_round_before_block ~block_round:cons_info.round round in
    let*? () = check_level kind vi.current_level.level level in
    (* We cannot check the exact round here in construction mode, because
       there is no preexisting fitness to provide the locked_round. We do
       however check that all preattestations have the same round in
       [check_construction_preattestation_round_consistency] further below. *)
    let*? () = check_payload_hash kind expected_payload_hash bph in
    let*? consensus_key, voting_power, _dal_power =
      get_delegate_details consensus_info.preattestation_slot_map kind slot
    in
    return (consensus_key, voting_power)

  (** Preattestation/attestation checks for Mempool mode.

      We want this mode to be very permissive, to allow the mempool to
      accept and propagate consensus operations even if they point to a
      block which is not known to the mempool (e.g. because the block
      has just been validated and the mempool has not had time to
      switch its head to it yet, or because the block belongs to a
      cousin branch). Therefore, we do not check the round nor the
      payload, which may correspond to blocks that we do not know of
      yet. As to the level, we only require it to be the
      [predecessor_level] (aka the level of the mempool's head) plus or
      minus one, that is:
      [predecessor_level - 1 <= op_level <= predecessor_level + 1]
      (note that [predecessor_level + 1] is also known as [current_level]).

      Note that we also don't check whether the slot is normalized
      (that is, whether it is the delegate's smallest slot). Indeed,
      we don't want to compute the right tables by first slot for all
      three allowed levels. Checking the slot normalization is
      therefore the responsability of the baker when it selects
      the consensus operations to include in a new block. Moreover,
      multiple attestations pointing to the same block with different
      slots can be punished by a double-(pre)attestation operation.

      Return the slot owner's consensus key and a fake voting power (the
      latter won't be used anyway in Mempool mode). *)
  let check_mempool_consensus vi consensus_info kind {level; slot; _} =
    let open Lwt_result_syntax in
    let*? () =
      if Raw_level.(succ level < consensus_info.predecessor_level) then
        let expected = consensus_info.predecessor_level and provided = level in
        result_error
          (Consensus_operation_for_old_level {kind; expected; provided})
      else if Raw_level.(level > vi.current_level.level) then
        let expected = consensus_info.predecessor_level and provided = level in
        result_error
          (Consensus_operation_for_future_level {kind; expected; provided})
      else ok_unit
    in
    let* (_ctxt : t), consensus_key =
      Stake_distribution.slot_owner vi.ctxt (Level.from_raw vi.ctxt level) slot
    in
    return (consensus_key, 0 (* Fake voting power *))
  (* We do not check that the frozen deposits are positive because this
     only needs to be true in the context of a block that actually
     contains the operation, which may not be the same as the current
     mempool's context. *)

  let check_preattestation vi ~check_signature
      (operation : Kind.preattestation operation) =
    let open Lwt_result_syntax in
    let*? consensus_info =
      Option.value_e
        ~error:(trace_of_error Consensus_operation_not_allowed)
        vi.consensus_info
    in
    let (Single (Preattestation consensus_content)) =
      operation.protocol_data.contents
    in
    let* consensus_key, voting_power =
      match vi.mode with
      | Application block_info | Partial_validation block_info ->
          check_preexisting_block_preattestation
            vi
            consensus_info
            block_info
            consensus_content
      | Construction construction_info ->
          check_constructed_block_preattestation
            vi
            consensus_info
            construction_info
            consensus_content
      | Mempool ->
          check_mempool_consensus
            vi
            consensus_info
            Preattestation
            consensus_content
    in
    let* () = check_delegate_is_not_forbidden vi.ctxt consensus_key.delegate in
    let*? () =
      if check_signature then
        Operation.check_signature
          consensus_key.consensus_pk
          vi.chain_id
          operation
      else ok_unit
    in
    return voting_power

  let check_preattestation_conflict vs oph (op : Kind.preattestation operation)
      =
    let (Single (Preattestation {slot; level; round; _})) =
      op.protocol_data.contents
    in
    match
      Consensus_conflict_map.find_opt
        (slot, level, round)
        vs.consensus_state.preattestations_seen
    with
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})
    | None -> ok_unit

  let wrap_preattestation_conflict = function
    | Ok () -> ok_unit
    | Error conflict ->
        result_error
          Validate_errors.Consensus.(
            Conflicting_consensus_operation {kind = Preattestation; conflict})

  let add_preattestation vs oph (op : Kind.preattestation operation) =
    let (Single (Preattestation {slot; level; round; _})) =
      op.protocol_data.contents
    in
    let preattestations_seen =
      Consensus_conflict_map.add
        (slot, level, round)
        oph
        vs.consensus_state.preattestations_seen
    in
    {vs with consensus_state = {vs.consensus_state with preattestations_seen}}

  let may_update_locked_round_evidence block_state mode
      (consensus_content : consensus_content) voting_power =
    let locked_round_evidence =
      match mode with
      | Mempool -> (* The block_state is not relevant in this mode. *) None
      | Application _ | Partial_validation _ | Construction _ -> (
          match block_state.locked_round_evidence with
          | None -> Some (consensus_content.round, voting_power)
          | Some (_stored_round, evidences) ->
              (* [_stored_round] is always equal to [consensus_content.round].
                 Indeed, this is ensured by
                 {!check_preattestation_content_preexisting_block} in
                 application and partial validation modes, and by
                 {!check_construction_preattestation_round_consistency} in
                 construction mode. *)
              Some (consensus_content.round, evidences + voting_power))
    in
    {block_state with locked_round_evidence}

  (* Hypothesis: this function will only be called in mempool mode *)
  let remove_preattestation vs (operation : Kind.preattestation operation) =
    (* As we are in mempool mode, we do not update
       [locked_round_evidence]. *)
    let (Single (Preattestation {slot; level; round; _})) =
      operation.protocol_data.contents
    in
    let preattestations_seen =
      Consensus_conflict_map.remove
        (slot, level, round)
        vs.consensus_state.preattestations_seen
    in
    {vs with consensus_state = {vs.consensus_state with preattestations_seen}}

  (** Attestation checks for all modes that involve a block:
      Application, Partial_validation, and Construction.
      Checks regarding the DAL content are done separately.

      Return the slot owner's consensus key and voting power. *)
  let check_block_attestation vi consensus_info
      {level; round; block_payload_hash = bph; slot} =
    let open Lwt_result_syntax in
    let*? expected_payload_hash =
      match Consensus.attestation_branch vi.ctxt with
      | Some ((_branch : Block_hash.t), payload_hash) -> Ok payload_hash
      | None ->
          (* [Consensus.attestation_branch] only returns [None] when the
             predecessor is the block that activates the first protocol
             of the Tenderbake family; this block should not be
             attested. This can only happen in tests and test
             networks. *)
          result_error Unexpected_attestation_in_block
    in
    let kind = Attestation in
    let*? () = check_level kind consensus_info.predecessor_level level in
    let*? () = check_round kind consensus_info.predecessor_round round in
    let*? () = check_payload_hash kind expected_payload_hash bph in
    let*? consensus_key, voting_power, _dal_power =
      get_delegate_details consensus_info.attestation_slot_map kind slot
    in
    return (consensus_key, voting_power)

  let check_attestation vi ~check_signature
      (operation : Kind.attestation operation) =
    let open Lwt_result_syntax in
    let*? consensus_info =
      Option.value_e
        ~error:(trace_of_error Consensus_operation_not_allowed)
        vi.consensus_info
    in
    let (Single (Attestation {consensus_content; dal_content})) =
      operation.protocol_data.contents
    in
    let* consensus_key, voting_power =
      match vi.mode with
      | Application _ | Partial_validation _ | Construction _ ->
          check_block_attestation vi consensus_info consensus_content
      | Mempool ->
          check_mempool_consensus
            vi
            consensus_info
            Attestation
            consensus_content
    in
    let* () = check_delegate_is_not_forbidden vi.ctxt consensus_key.delegate in
    let* () =
      Option.fold
        ~none:return_unit
        ~some:(fun dal ->
          Dal_apply.validate_attestation
            vi.ctxt
            consensus_content.level
            consensus_content.slot
            consensus_key
            dal.attestation)
        dal_content
    in
    let*? () =
      if check_signature then
        Operation.check_signature
          consensus_key.consensus_pk
          vi.chain_id
          operation
      else ok_unit
    in
    return voting_power

  let check_attestation_conflict vs oph (operation : Kind.attestation operation)
      =
    let (Single
          (Attestation
            {consensus_content = {slot; level; round; _}; dal_content = _})) =
      operation.protocol_data.contents
    in
    match
      Consensus_conflict_map.find_opt
        (slot, level, round)
        vs.consensus_state.attestations_seen
    with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let wrap_attestation_conflict = function
    | Ok () -> ok_unit
    | Error conflict ->
        result_error
          Validate_errors.Consensus.(
            Conflicting_consensus_operation {kind = Attestation; conflict})

  let add_attestation vs oph (op : Kind.attestation operation) =
    let (Single
          (Attestation
            {consensus_content = {slot; level; round; _}; dal_content = _})) =
      op.protocol_data.contents
    in
    let attestations_seen =
      Consensus_conflict_map.add
        (slot, level, round)
        oph
        vs.consensus_state.attestations_seen
    in
    {vs with consensus_state = {vs.consensus_state with attestations_seen}}

  let may_update_attestation_power vi block_state voting_power =
    match vi.mode with
    | Mempool -> (* The block_state is not relevant. *) block_state
    | Application _ | Partial_validation _ | Construction _ ->
        {
          block_state with
          attestation_power = block_state.attestation_power + voting_power;
        }

  (* Hypothesis: this function will only be called in mempool mode *)
  let remove_attestation vs (operation : Kind.attestation operation) =
    (* We do not remove the attestation power because it is not
       relevant for the mempool mode. *)
    let (Single
          (Attestation
            {consensus_content = {slot; level; round; _}; dal_content = _})) =
      operation.protocol_data.contents
    in
    let attestations_seen =
      Consensus_conflict_map.remove
        (slot, level, round)
        vs.consensus_state.attestations_seen
    in
    {vs with consensus_state = {vs.consensus_state with attestations_seen}}

  (** In Construction mode, check that the preattestation has the same
      round as any previously validated preattestations.

      This check is not needed in other modes because
      {!check_preattestation} already checks that all preattestations
      have the same expected round (the locked_round in Application and
      Partial_validation modes when there is one (otherwise all
      preattestations are rejected so the point is moot), or the
      predecessor_round in Mempool mode). *)
  let check_construction_preattestation_round_consistency vi block_state
      (consensus_content : consensus_content) =
    let open Result_syntax in
    match vi.mode with
    | Construction _ -> (
        match block_state.locked_round_evidence with
        | None ->
            (* This is the first validated preattestation:
               there is nothing to check. *)
            return_unit
        | Some (expected, _power) ->
            (* Other preattestations have already been validated: we check
               that the current operation has the same round as them. *)
            check_round Preattestation expected consensus_content.round)
    | Application _ | Partial_validation _ | Mempool -> return_unit

  let validate_preattestation ~check_signature info operation_state block_state
      oph (operation : Kind.preattestation operation) =
    let open Lwt_result_syntax in
    let (Single (Preattestation consensus_content)) =
      operation.protocol_data.contents
    in
    let* voting_power = check_preattestation info ~check_signature operation in
    let*? () =
      check_construction_preattestation_round_consistency
        info
        block_state
        consensus_content
    in
    let*? () =
      check_preattestation_conflict operation_state oph operation
      |> wrap_preattestation_conflict
    in
    (* We need to update the block state *)
    let block_state =
      may_update_locked_round_evidence
        block_state
        info.mode
        consensus_content
        voting_power
    in
    let operation_state = add_preattestation operation_state oph operation in
    return {info; operation_state; block_state}

  let validate_attestation ~check_signature info operation_state block_state oph
      operation =
    let open Lwt_result_syntax in
    let* power = check_attestation info ~check_signature operation in
    let*? () =
      check_attestation_conflict operation_state oph operation
      |> wrap_attestation_conflict
    in
    let block_state = may_update_attestation_power info block_state power in
    let operation_state = add_attestation operation_state oph operation in
    return {info; operation_state; block_state}
end

(** {2 Validation of voting operations}

    There are two kinds of voting operations:

    - Proposals: A delegate submits a list of protocol amendment
      proposals. This operation is only accepted during a Proposal period
      (see above).

    - Ballot: A delegate casts a vote for/against the current proposal
      (or pass). This operation is only accepted during an Exploration
      or Promotion period (see above). *)

module Voting = struct
  open Validate_errors.Voting

  let check_period_index ~expected period_index =
    error_unless
      Compare.Int32.(expected = period_index)
      (Wrong_voting_period_index {expected; provided = period_index})

  let check_proposals_source_is_registered ctxt source =
    let open Lwt_result_syntax in
    let*! is_registered = Delegate.registered ctxt source in
    fail_unless is_registered (Proposals_from_unregistered_delegate source)

  (** Check that the list of proposals is not empty and does not contain
      duplicates. *)
  let check_proposal_list_sanity proposals =
    let open Result_syntax in
    let* () =
      match proposals with [] -> tzfail Empty_proposals | _ :: _ -> ok_unit
    in
    let* (_ : Protocol_hash.Set.t) =
      List.fold_left_e
        (fun previous_elements proposal ->
          let* () =
            error_when
              (Protocol_hash.Set.mem proposal previous_elements)
              (Proposals_contain_duplicate {proposal})
          in
          return (Protocol_hash.Set.add proposal previous_elements))
        Protocol_hash.Set.empty
        proposals
    in
    return_unit

  let check_period_kind_for_proposals current_period =
    match current_period.Voting_period.kind with
    | Proposal -> ok_unit
    | (Exploration | Cooldown | Promotion | Adoption) as current ->
        result_error (Wrong_voting_period_kind {current; expected = [Proposal]})

  let check_in_listings ctxt source =
    let open Lwt_result_syntax in
    let*! in_listings = Vote.in_listings ctxt source in
    fail_unless in_listings Source_not_in_vote_listings

  let check_count ~count_in_ctxt ~proposals_length =
    (* The proposal count of the proposer in the context should never
       have been increased above [max_proposals_per_delegate]. *)
    assert (Compare.Int.(count_in_ctxt <= Constants.max_proposals_per_delegate)) ;
    error_unless
      Compare.Int.(
        count_in_ctxt + proposals_length <= Constants.max_proposals_per_delegate)
      (Too_many_proposals
         {previous_count = count_in_ctxt; operation_count = proposals_length})

  let check_already_proposed ctxt proposer proposals =
    let open Lwt_result_syntax in
    List.iter_es
      (fun proposal ->
        let*! already_proposed = Vote.has_proposed ctxt proposer proposal in
        fail_when already_proposed (Already_proposed {proposal}))
      proposals

  (** Check that the [apply_testnet_dictator_proposals] function in
      {!module:Amendment} will not fail.

      The current function is designed to be exclusively called by
      [check_proposals] right below.

      @return [Error Testnet_dictator_multiple_proposals] if
      [proposals] has more than one element. *)
  let check_testnet_dictator_proposals chain_id proposals =
    (* This assertion should be ensured by the fact that
       {!Amendment.is_testnet_dictator} cannot be [true] on mainnet
       (so the current function cannot be called there). However, we
       still double check it because of its criticality. *)
    assert (Chain_id.(chain_id <> Constants.mainnet_id)) ;
    match proposals with
    | [] | [_] ->
        (* In [Amendment.apply_testnet_dictator_proposals], the call to
           {!Vote.init_current_proposal} (in the singleton list case)
           cannot fail because {!Vote.clear_current_proposal} is called
           right before.

           The calls to
           {!Voting_period.Testnet_dictator.overwrite_current_kind} may
           usually fail when the voting period is not
           initialized. However, this cannot happen here because the
           current function is only called in [check_proposals] after a
           successful call to {!Voting_period.get_current}. *)
        ok_unit
    | _ :: _ :: _ -> result_error Testnet_dictator_multiple_proposals

  (** Check that a Proposals operation can be safely applied.

      @return [Error Wrong_voting_period_index] if the operation's
      period and the current period in the {!type:context} do not have
      the same index.

      @return [Error Proposals_from_unregistered_delegate] if the
      source is not a registered delegate.

      @return [Error Empty_proposals] if the list of proposals is empty.

      @return [Error Proposals_contain_duplicate] if the list of
      proposals contains a duplicate element.

      @return [Error Wrong_voting_period_kind] if the voting period is
      not of the Proposal kind.

      @return [Error Source_not_in_vote_listings] if the source is not
      in the vote listings.

      @return [Error Too_many_proposals] if the operation causes the
      source's total number of proposals during the current voting
      period to exceed {!Constants.max_proposals_per_delegate}.

      @return [Error Already_proposed] if one of the proposals has
      already been proposed by the source in the current voting period.

      @return [Error Testnet_dictator_multiple_proposals] if the
      source is a testnet dictator and the operation contains more than
      one proposal.

      @return [Error Operation.Missing_signature] or [Error
      Operation.Invalid_signature] if the operation is unsigned or
      incorrectly signed. *)
  let check_proposals vi ~check_signature (operation : Kind.proposals operation)
      =
    let open Lwt_result_syntax in
    let (Single (Proposals {source; period; proposals})) =
      operation.protocol_data.contents
    in
    let* current_period = Voting_period.get_current vi.ctxt in
    let*? () = check_period_index ~expected:current_period.index period in
    let* () =
      if Amendment.is_testnet_dictator vi.ctxt vi.chain_id source then
        let*? () = check_testnet_dictator_proposals vi.chain_id proposals in
        return_unit
      else
        let* () = check_proposals_source_is_registered vi.ctxt source in
        let*? () = check_proposal_list_sanity proposals in
        let*? () = check_period_kind_for_proposals current_period in
        let* () = check_in_listings vi.ctxt source in
        let* count_in_ctxt = Vote.get_delegate_proposal_count vi.ctxt source in
        let proposals_length = List.length proposals in
        let*? () = check_count ~count_in_ctxt ~proposals_length in
        check_already_proposed vi.ctxt source proposals
    in
    if check_signature then
      (* Retrieving the public key should not fail as it *should* be
         called after checking that the delegate is in the vote
         listings (or is a testnet dictator), which implies that it
         is a manager with a revealed key. *)
      let* public_key = Contract.get_manager_key vi.ctxt source in
      Lwt.return (Operation.check_signature public_key vi.chain_id operation)
    else return_unit

  (** Check that a Proposals operation is compatible with previously
      validated operations in the current block/mempool.

      @return [Error Operation_conflict] if the current block/mempool
      already contains a Proposals operation from the same source
      (regardless of whether this source is a testnet dictator or an
      ordinary manager). *)
  let check_proposals_conflict vs oph (operation : Kind.proposals operation) =
    let open Result_syntax in
    let (Single (Proposals {source; _})) = operation.protocol_data.contents in
    match
      Signature.Public_key_hash.Map.find_opt
        source
        vs.voting_state.proposals_seen
    with
    | None -> return_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let wrap_proposals_conflict = function
    | Ok () -> ok_unit
    | Error conflict ->
        result_error Validate_errors.Voting.(Conflicting_proposals conflict)

  let add_proposals vs oph (operation : Kind.proposals operation) =
    let (Single (Proposals {source; _})) = operation.protocol_data.contents in
    let proposals_seen =
      Signature.Public_key_hash.Map.add
        source
        oph
        vs.voting_state.proposals_seen
    in
    let voting_state = {vs.voting_state with proposals_seen} in
    {vs with voting_state}

  let remove_proposals vs (operation : Kind.proposals operation) =
    let (Single (Proposals {source; _})) = operation.protocol_data.contents in
    let proposals_seen =
      Signature.Public_key_hash.Map.remove source vs.voting_state.proposals_seen
    in
    {vs with voting_state = {vs.voting_state with proposals_seen}}

  let check_ballot_source_is_registered ctxt source =
    let open Lwt_result_syntax in
    let*! is_registered = Delegate.registered ctxt source in
    fail_unless is_registered (Ballot_from_unregistered_delegate source)

  let check_period_kind_for_ballot current_period =
    match current_period.Voting_period.kind with
    | Exploration | Promotion -> ok_unit
    | (Cooldown | Proposal | Adoption) as current ->
        result_error
          (Wrong_voting_period_kind
             {current; expected = [Exploration; Promotion]})

  let check_current_proposal ctxt op_proposal =
    let open Lwt_result_syntax in
    let* current_proposal = Vote.get_current_proposal ctxt in
    fail_unless
      (Protocol_hash.equal op_proposal current_proposal)
      (Ballot_for_wrong_proposal
         {current = current_proposal; submitted = op_proposal})

  let check_source_has_not_already_voted ctxt source =
    let open Lwt_result_syntax in
    let*! has_ballot = Vote.has_recorded_ballot ctxt source in
    fail_when has_ballot Already_submitted_a_ballot

  (** Check that a Ballot operation can be safely applied.

      @return [Error Ballot_from_unregistered_delegate] if the source
      is not a registered delegate.

      @return [Error Wrong_voting_period_index] if the operation's
      period and the current period in the {!type:context} do not have
      the same index.

      @return [Error Wrong_voting_period_kind] if the voting period is
      not of the Exploration or Promotion kind.

      @return [Error Ballot_for_wrong_proposal] if the operation's
      proposal is different from the current proposal in the context.

      @return [Error Already_submitted_a_ballot] if the source has
      already voted during the current voting period.

      @return [Error Source_not_in_vote_listings] if the source is not
      in the vote listings.

      @return [Error Operation.Missing_signature] or [Error
      Operation.Invalid_signature] if the operation is unsigned or
      incorrectly signed. *)
  let check_ballot vi ~check_signature (operation : Kind.ballot operation) =
    let open Lwt_result_syntax in
    let (Single (Ballot {source; period; proposal; ballot = _})) =
      operation.protocol_data.contents
    in
    let* () = check_ballot_source_is_registered vi.ctxt source in
    let* current_period = Voting_period.get_current vi.ctxt in
    let*? () = check_period_index ~expected:current_period.index period in
    let*? () = check_period_kind_for_ballot current_period in
    let* () = check_current_proposal vi.ctxt proposal in
    let* () = check_source_has_not_already_voted vi.ctxt source in
    let* () = check_in_listings vi.ctxt source in
    when_ check_signature (fun () ->
        (* Retrieving the public key cannot fail. Indeed, we have
           already checked that the delegate is in the vote listings,
           which implies that it is a manager with a revealed key. *)
        let* public_key = Contract.get_manager_key vi.ctxt source in
        Lwt.return (Operation.check_signature public_key vi.chain_id operation))

  (** Check that a Ballot operation is compatible with previously
      validated operations in the current block/mempool.

      @return [Error Operation_conflict] if the current block/mempool
      already contains a Ballot operation from the same source. *)
  let check_ballot_conflict vs oph (operation : Kind.ballot operation) =
    let (Single (Ballot {source; _})) = operation.protocol_data.contents in
    match
      Signature.Public_key_hash.Map.find_opt source vs.voting_state.ballots_seen
    with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let wrap_ballot_conflict = function
    | Ok () -> ok_unit
    | Error conflict -> result_error (Conflicting_ballot conflict)

  let add_ballot vs oph (operation : Kind.ballot operation) =
    let (Single (Ballot {source; _})) = operation.protocol_data.contents in
    let ballots_seen =
      Signature.Public_key_hash.Map.add source oph vs.voting_state.ballots_seen
    in
    let voting_state = {vs.voting_state with ballots_seen} in
    {vs with voting_state}

  let remove_ballot vs (operation : Kind.ballot operation) =
    let (Single (Ballot {source; _})) = operation.protocol_data.contents in
    let ballots_seen =
      Signature.Public_key_hash.Map.remove source vs.voting_state.ballots_seen
    in
    {vs with voting_state = {vs.voting_state with ballots_seen}}
end

module Anonymous = struct
  open Validate_errors.Anonymous

  let check_activate_account vi (operation : Kind.activate_account operation) =
    let open Lwt_result_syntax in
    let (Single (Activate_account {id = edpkh; activation_code})) =
      operation.protocol_data.contents
    in
    let blinded_pkh =
      Blinded_public_key_hash.of_ed25519_pkh activation_code edpkh
    in
    let*! exists = Commitment.exists vi.ctxt blinded_pkh in
    let*? () = error_unless exists (Invalid_activation {pkh = edpkh}) in
    return_unit

  let check_activate_account_conflict vs oph
      (operation : Kind.activate_account operation) =
    let (Single (Activate_account {id = edpkh; _})) =
      operation.protocol_data.contents
    in
    match
      Ed25519.Public_key_hash.Map.find_opt
        edpkh
        vs.anonymous_state.activation_pkhs_seen
    with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let wrap_activate_account_conflict
      (operation : Kind.activate_account operation) = function
    | Ok () -> ok_unit
    | Error conflict ->
        let (Single (Activate_account {id = edpkh; _})) =
          operation.protocol_data.contents
        in
        result_error (Conflicting_activation {edpkh; conflict})

  let add_activate_account vs oph (operation : Kind.activate_account operation)
      =
    let (Single (Activate_account {id = edpkh; _})) =
      operation.protocol_data.contents
    in
    let activation_pkhs_seen =
      Ed25519.Public_key_hash.Map.add
        edpkh
        oph
        vs.anonymous_state.activation_pkhs_seen
    in
    {vs with anonymous_state = {vs.anonymous_state with activation_pkhs_seen}}

  let remove_activate_account vs (operation : Kind.activate_account operation) =
    let (Single (Activate_account {id = edpkh; _})) =
      operation.protocol_data.contents
    in
    let activation_pkhs_seen =
      Ed25519.Public_key_hash.Map.remove
        edpkh
        vs.anonymous_state.activation_pkhs_seen
    in
    {vs with anonymous_state = {vs.anonymous_state with activation_pkhs_seen}}

  let check_denunciation_age vi kind given_level =
    let open Result_syntax in
    let current_cycle = vi.current_level.cycle in
    let given_cycle = (Level.from_raw vi.ctxt given_level).cycle in
    let max_slashing_period = Constants.max_slashing_period in
    let last_slashable_cycle = Cycle.add given_cycle max_slashing_period in
    let* () =
      error_unless
        Cycle.(given_cycle <= current_cycle)
        (Too_early_denunciation
           {kind; level = given_level; current = vi.current_level.level})
    in
    error_unless
      Cycle.(last_slashable_cycle > current_cycle)
      (Outdated_denunciation
         {kind; level = given_level; last_cycle = last_slashable_cycle})

  let check_double_attesting_evidence (type kind) vi
      (op1 : kind Kind.consensus Operation.t)
      (op2 : kind Kind.consensus Operation.t) =
    let open Lwt_result_syntax in
    let e1, e2, kind =
      match (op1.protocol_data.contents, op2.protocol_data.contents) with
      | Single (Preattestation e1), Single (Preattestation e2) ->
          (e1, e2, Misbehaviour.Double_preattesting)
      | ( Single (Attestation {consensus_content = e1; dal_content = _}),
          Single (Attestation {consensus_content = e2; dal_content = _}) ) ->
          (e1, e2, Double_attesting)
    in
    let op1_hash = Operation.hash op1 in
    let op2_hash = Operation.hash op2 in
    let same_levels = Raw_level.(e1.level = e2.level) in
    let same_rounds = Round.(e1.round = e2.round) in
    let same_payload =
      Block_payload_hash.(e1.block_payload_hash = e2.block_payload_hash)
    in
    let same_branches = Block_hash.(op1.shell.branch = op2.shell.branch) in
    let same_slots = Slot.(e1.slot = e2.slot) in
    let ordered_hashes = Operation_hash.(op1_hash < op2_hash) in
    let is_denunciation_consistent =
      same_levels && same_rounds
      (* For the double (pre)attestations to be punishable, they
         must point to the same block (same level and round), but
         also have at least a difference that is the delegate's
         fault: different payloads, different branches, or
         different slots. Note that different payloads would
         endanger the consensus process, while different branches
         or slots could be used to spam mempools with a lot of
         valid operations (since the minimality of the slot in not
         checked in mempool mode, only in block-related modes). On
         the other hand, if the operations have identical levels,
         rounds, payloads, branches, and slots, then only their
         signatures are different, which is not considered the
         delegate's fault and therefore is not punished. *)
      && ((not same_payload) || (not same_branches) || not same_slots)
      && (* we require an order on hashes to avoid the existence of
               equivalent evidences *)
      ordered_hashes
    in
    let*? () =
      error_unless is_denunciation_consistent (Invalid_denunciation kind)
    in
    (* Disambiguate: levels are equal *)
    let level = Level.from_raw vi.ctxt e1.level in
    let*? () = check_denunciation_age vi kind level.level in
    let* ctxt, consensus_key1 =
      Stake_distribution.slot_owner vi.ctxt level e1.slot
    in
    let* ctxt, consensus_key2 =
      Stake_distribution.slot_owner ctxt level e2.slot
    in
    let delegate1, delegate2 =
      (consensus_key1.delegate, consensus_key2.delegate)
    in
    let*? () =
      error_unless
        (Signature.Public_key_hash.equal delegate1 delegate2)
        (Inconsistent_denunciation {kind; delegate1; delegate2})
    in
    let delegate_pk, delegate = (consensus_key1.consensus_pk, delegate1) in
    let* already_slashed =
      Delegate.already_denounced ctxt delegate level e1.round kind
    in
    let*? () =
      error_unless
        (not already_slashed)
        (Already_denounced {kind; delegate; level})
    in
    let*? () = Operation.check_signature delegate_pk vi.chain_id op1 in
    let*? () = Operation.check_signature delegate_pk vi.chain_id op2 in
    return_unit

  let check_double_preattestation_evidence vi
      (operation : Kind.double_preattestation_evidence operation) =
    let (Single (Double_preattestation_evidence {op1; op2})) =
      operation.protocol_data.contents
    in
    check_double_attesting_evidence vi op1 op2

  let check_double_attestation_evidence vi
      (operation : Kind.double_attestation_evidence operation) =
    let (Single (Double_attestation_evidence {op1; op2})) =
      operation.protocol_data.contents
    in
    check_double_attesting_evidence vi op1 op2

  let double_operation_conflict_key (type kind)
      (op1 : kind Kind.consensus Operation.t) =
    let {slot; level; round; block_payload_hash = _}, kind =
      match op1.protocol_data.contents with
      | Single (Preattestation cc) -> (cc, Misbehaviour.Double_preattesting)
      | Single (Attestation {consensus_content; dal_content = _}) ->
          (consensus_content, Double_attesting)
    in
    (level, round, slot, kind)

  let check_double_attesting_evidence_conflict (type kind) vs oph
      (op1 : kind Kind.consensus Operation.t) =
    match
      Double_operation_evidence_map.find
        (double_operation_conflict_key op1)
        vs.anonymous_state.double_attesting_evidences_seen
    with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let check_double_preattestation_evidence_conflict vs oph
      (operation : Kind.double_preattestation_evidence operation) =
    let (Single (Double_preattestation_evidence {op1; _})) =
      operation.protocol_data.contents
    in
    check_double_attesting_evidence_conflict vs oph op1

  let check_double_attestation_evidence_conflict vs oph
      (operation : Kind.double_attestation_evidence operation) =
    let (Single (Double_attestation_evidence {op1; _})) =
      operation.protocol_data.contents
    in
    check_double_attesting_evidence_conflict vs oph op1

  let wrap_denunciation_conflict kind = function
    | Ok () -> ok_unit
    | Error conflict -> result_error (Conflicting_denunciation {kind; conflict})

  let add_double_attesting_evidence (type kind) vs oph
      (op1 : kind Kind.consensus Operation.t) =
    let double_attesting_evidences_seen =
      Double_operation_evidence_map.add
        (double_operation_conflict_key op1)
        oph
        vs.anonymous_state.double_attesting_evidences_seen
    in
    {
      vs with
      anonymous_state =
        {vs.anonymous_state with double_attesting_evidences_seen};
    }

  let add_double_attestation_evidence vs oph
      (operation : Kind.double_attestation_evidence operation) =
    let (Single (Double_attestation_evidence {op1; _})) =
      operation.protocol_data.contents
    in
    add_double_attesting_evidence vs oph op1

  let add_double_preattestation_evidence vs oph
      (operation : Kind.double_preattestation_evidence operation) =
    let (Single (Double_preattestation_evidence {op1; _})) =
      operation.protocol_data.contents
    in
    add_double_attesting_evidence vs oph op1

  let remove_double_attesting_evidence (type kind) vs
      (op : kind Kind.consensus Operation.t) =
    let double_attesting_evidences_seen =
      Double_operation_evidence_map.remove
        (double_operation_conflict_key op)
        vs.anonymous_state.double_attesting_evidences_seen
    in
    let anonymous_state =
      {vs.anonymous_state with double_attesting_evidences_seen}
    in
    {vs with anonymous_state}

  let remove_double_preattestation_evidence vs
      (operation : Kind.double_preattestation_evidence operation) =
    let (Single (Double_preattestation_evidence {op1; _})) =
      operation.protocol_data.contents
    in
    remove_double_attesting_evidence vs op1

  let remove_double_attestation_evidence vs
      (operation : Kind.double_attestation_evidence operation) =
    let (Single (Double_attestation_evidence {op1; _})) =
      operation.protocol_data.contents
    in
    remove_double_attesting_evidence vs op1

  let check_double_baking_evidence vi
      (operation : Kind.double_baking_evidence operation) =
    let open Lwt_result_syntax in
    let (Single (Double_baking_evidence {bh1; bh2})) =
      operation.protocol_data.contents
    in
    let hash1 = Block_header.hash bh1 in
    let hash2 = Block_header.hash bh2 in
    let*? bh1_fitness = Fitness.from_raw bh1.shell.fitness in
    let round1 = Fitness.round bh1_fitness in
    let*? bh2_fitness = Fitness.from_raw bh2.shell.fitness in
    let round2 = Fitness.round bh2_fitness in
    let*? level1 = Raw_level.of_int32 bh1.shell.level in
    let*? level2 = Raw_level.of_int32 bh2.shell.level in
    let*? () =
      error_unless
        (Raw_level.(level1 = level2)
        && Round.(round1 = round2)
        && (* we require an order on hashes to avoid the existence of
              equivalent evidences *)
        Block_hash.(hash1 < hash2))
        (Invalid_double_baking_evidence
           {hash1; level1; round1; hash2; level2; round2})
    in
    let*? () = check_denunciation_age vi Double_baking level1 in
    let level = Level.from_raw vi.ctxt level1 in
    let committee_size = Constants.consensus_committee_size vi.ctxt in
    let*? slot1 = Round.to_slot round1 ~committee_size in
    let* ctxt, consensus_key1 =
      Stake_distribution.slot_owner vi.ctxt level slot1
    in
    let*? slot2 = Round.to_slot round2 ~committee_size in
    let* ctxt, consensus_key2 =
      Stake_distribution.slot_owner ctxt level slot2
    in
    let delegate1, delegate2 =
      (consensus_key1.delegate, consensus_key2.delegate)
    in
    let*? () =
      error_unless
        Signature.Public_key_hash.(delegate1 = delegate2)
        (Inconsistent_denunciation {kind = Double_baking; delegate1; delegate2})
    in
    let delegate_pk, delegate = (consensus_key1.consensus_pk, delegate1) in
    let* already_slashed =
      Delegate.already_denounced ctxt delegate level round1 Double_baking
    in
    let*? () =
      error_unless
        (not already_slashed)
        (Already_denounced {kind = Double_baking; delegate; level})
    in
    let*? () = Block_header.check_signature bh1 vi.chain_id delegate_pk in
    let*? () = Block_header.check_signature bh2 vi.chain_id delegate_pk in
    return_unit

  let check_double_baking_evidence_conflict vs oph
      (operation : Kind.double_baking_evidence operation) =
    let (Single (Double_baking_evidence {bh1; _})) =
      operation.protocol_data.contents
    in
    let bh1_fitness =
      Fitness.from_raw bh1.shell.fitness |> function
      | Ok f -> f
      | Error _ ->
          (* We assume the operation valid, it cannot fail anymore *)
          assert false
    in
    let round = Fitness.round bh1_fitness in
    let level = Fitness.level bh1_fitness in
    match
      Double_baking_evidence_map.find
        (level, round)
        vs.anonymous_state.double_baking_evidences_seen
    with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let add_double_baking_evidence vs oph
      (operation : Kind.double_baking_evidence operation) =
    let (Single (Double_baking_evidence {bh1; _})) =
      operation.protocol_data.contents
    in
    let bh1_fitness =
      Fitness.from_raw bh1.shell.fitness |> function
      | Ok f -> f
      | Error _ -> assert false
    in
    let round = Fitness.round bh1_fitness in
    let level = Fitness.level bh1_fitness in
    let double_baking_evidences_seen =
      Double_baking_evidence_map.add
        (level, round)
        oph
        vs.anonymous_state.double_baking_evidences_seen
    in
    {
      vs with
      anonymous_state = {vs.anonymous_state with double_baking_evidences_seen};
    }

  let remove_double_baking_evidence vs
      (operation : Kind.double_baking_evidence operation) =
    let (Single (Double_baking_evidence {bh1; _})) =
      operation.protocol_data.contents
    in
    let bh1_fitness, level =
      match
        (Fitness.from_raw bh1.shell.fitness, Raw_level.of_int32 bh1.shell.level)
      with
      | Ok v, Ok v' -> (v, v')
      | _ ->
          (* The operation is valid therefore decoding cannot fail *)
          assert false
    in
    let round = Fitness.round bh1_fitness in
    let double_baking_evidences_seen =
      Double_baking_evidence_map.remove
        (level, round)
        vs.anonymous_state.double_baking_evidences_seen
    in
    let anonymous_state =
      {vs.anonymous_state with double_baking_evidences_seen}
    in
    {vs with anonymous_state}

  let check_drain_delegate info ~check_signature
      (operation : Kind.drain_delegate Operation.t) =
    let open Lwt_result_syntax in
    let (Single (Drain_delegate {delegate; destination; consensus_key})) =
      operation.protocol_data.contents
    in
    let*! is_registered = Delegate.registered info.ctxt delegate in
    let* () =
      fail_unless
        is_registered
        (Drain_delegate_on_unregistered_delegate delegate)
    in
    let* active_pk = Delegate.Consensus_key.active_pubkey info.ctxt delegate in
    let* () =
      fail_unless
        (Signature.Public_key_hash.equal active_pk.consensus_pkh consensus_key)
        (Invalid_drain_delegate_inactive_key
           {
             delegate;
             consensus_key;
             active_consensus_key = active_pk.consensus_pkh;
           })
    in
    let* () =
      fail_when
        (Signature.Public_key_hash.equal active_pk.consensus_pkh delegate)
        (Invalid_drain_delegate_no_consensus_key delegate)
    in
    let* () =
      fail_when
        (Signature.Public_key_hash.equal destination delegate)
        (Invalid_drain_delegate_noop delegate)
    in
    let*! is_destination_allocated =
      Contract.allocated info.ctxt (Contract.Implicit destination)
    in
    let* balance =
      Contract.get_balance info.ctxt (Contract.Implicit delegate)
    in
    let*? origination_burn =
      if is_destination_allocated then Ok Tez.zero
      else
        let cost_per_byte = Constants.cost_per_byte info.ctxt in
        let origination_size = Constants.origination_size info.ctxt in
        Tez.(cost_per_byte *? Int64.of_int origination_size)
    in
    let* drain_fees =
      let*? one_percent = Tez.(balance /? 100L) in
      return Tez.(max one one_percent)
    in
    let*? min_amount = Tez.(origination_burn +? drain_fees) in
    let* () =
      fail_when
        Tez.(balance < min_amount)
        (Invalid_drain_delegate_insufficient_funds_for_burn_or_fees
           {delegate; destination; min_amount})
    in
    let*? () =
      if check_signature then
        Operation.check_signature active_pk.consensus_pk info.chain_id operation
      else ok_unit
    in
    return_unit

  let check_drain_delegate_conflict state oph
      (operation : Kind.drain_delegate Operation.t) =
    let (Single (Drain_delegate {delegate; _})) =
      operation.protocol_data.contents
    in
    match
      Signature.Public_key_hash.Map.find_opt
        delegate
        state.manager_state.managers_seen
    with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let wrap_drain_delegate_conflict (operation : Kind.drain_delegate Operation.t)
      =
    let (Single (Drain_delegate {delegate; _})) =
      operation.protocol_data.contents
    in
    function
    | Ok () -> ok_unit
    | Error conflict ->
        result_error (Conflicting_drain_delegate {delegate; conflict})

  let add_drain_delegate state oph (operation : Kind.drain_delegate Operation.t)
      =
    let (Single (Drain_delegate {delegate; _})) =
      operation.protocol_data.contents
    in
    let managers_seen =
      Signature.Public_key_hash.Map.add
        delegate
        oph
        state.manager_state.managers_seen
    in
    {state with manager_state = {managers_seen}}

  let remove_drain_delegate state (operation : Kind.drain_delegate Operation.t)
      =
    let (Single (Drain_delegate {delegate; _})) =
      operation.protocol_data.contents
    in
    let managers_seen =
      Signature.Public_key_hash.Map.remove
        delegate
        state.manager_state.managers_seen
    in
    {state with manager_state = {managers_seen}}

  let check_seed_nonce_revelation vi
      (operation : Kind.seed_nonce_revelation operation) =
    let open Lwt_result_syntax in
    let (Single (Seed_nonce_revelation {level = commitment_raw_level; nonce})) =
      operation.protocol_data.contents
    in
    let commitment_level = Level.from_raw vi.ctxt commitment_raw_level in
    let* () = Nonce.check_unrevealed vi.ctxt commitment_level nonce in
    return_unit

  let check_seed_nonce_revelation_conflict vs oph
      (operation : Kind.seed_nonce_revelation operation) =
    let (Single (Seed_nonce_revelation {level = commitment_raw_level; _})) =
      operation.protocol_data.contents
    in
    match
      Raw_level.Map.find_opt
        commitment_raw_level
        vs.anonymous_state.seed_nonce_levels_seen
    with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let wrap_seed_nonce_revelation_conflict = function
    | Ok () -> ok_unit
    | Error conflict -> result_error (Conflicting_nonce_revelation conflict)

  let add_seed_nonce_revelation vs oph
      (operation : Kind.seed_nonce_revelation operation) =
    let (Single (Seed_nonce_revelation {level = commitment_raw_level; _})) =
      operation.protocol_data.contents
    in
    let seed_nonce_levels_seen =
      Raw_level.Map.add
        commitment_raw_level
        oph
        vs.anonymous_state.seed_nonce_levels_seen
    in
    let anonymous_state = {vs.anonymous_state with seed_nonce_levels_seen} in
    {vs with anonymous_state}

  let remove_seed_nonce_revelation vs
      (operation : Kind.seed_nonce_revelation operation) =
    let (Single (Seed_nonce_revelation {level = commitment_raw_level; _})) =
      operation.protocol_data.contents
    in
    let seed_nonce_levels_seen =
      Raw_level.Map.remove
        commitment_raw_level
        vs.anonymous_state.seed_nonce_levels_seen
    in
    let anonymous_state = {vs.anonymous_state with seed_nonce_levels_seen} in
    {vs with anonymous_state}

  let check_vdf_revelation vi (operation : Kind.vdf_revelation operation) =
    let open Lwt_result_syntax in
    let (Single (Vdf_revelation {solution})) =
      operation.protocol_data.contents
    in
    let* () = Seed.check_vdf vi.ctxt solution in
    return_unit

  let check_vdf_revelation_conflict vs oph =
    match vs.anonymous_state.vdf_solution_seen with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let wrap_vdf_revelation_conflict = function
    | Ok () -> ok_unit
    | Error conflict -> result_error (Conflicting_vdf_revelation conflict)

  let add_vdf_revelation vs oph =
    {
      vs with
      anonymous_state = {vs.anonymous_state with vdf_solution_seen = Some oph};
    }

  let remove_vdf_revelation vs =
    let anonymous_state = {vs.anonymous_state with vdf_solution_seen = None} in
    {vs with anonymous_state}
end

module Manager = struct
  open Validate_errors.Manager

  (** State that simulates changes from individual operations that have
      an effect on future operations inside the same batch. *)
  type batch_state = {
    balance : Tez.t;
        (** Remaining balance in the contract, used to simulate the
            payment of fees by each operation in the batch. *)
    is_allocated : bool;
        (** Track whether the contract is still allocated. Indeed,
            previous operations' fee payment may empty the contract and
            this may deallocate the contract.

            TODO: https://gitlab.com/tezos/tezos/-/issues/3209 Change
            empty account cleanup mechanism to avoid the need for this
            field. *)
    total_gas_used : Gas.Arith.fp;
  }

  let check_source_and_counter ~expected_source ~source ~previous_counter
      ~counter =
    let open Result_syntax in
    let* () =
      error_unless
        (Signature.Public_key_hash.equal expected_source source)
        Inconsistent_sources
    in
    error_unless
      Manager_counter.(succ previous_counter = counter)
      Inconsistent_counters

  let rec check_batch_tail_sanity :
      type kind.
      public_key_hash ->
      Manager_counter.t ->
      kind Kind.manager contents_list ->
      unit tzresult =
    let open Result_syntax in
    fun expected_source previous_counter -> function
      | Single (Manager_operation {operation = Reveal _key; _}) ->
          tzfail Incorrect_reveal_position
      | Cons (Manager_operation {operation = Reveal _key; _}, _res) ->
          tzfail Incorrect_reveal_position
      | Single (Manager_operation {source; counter; _}) ->
          check_source_and_counter
            ~expected_source
            ~source
            ~previous_counter
            ~counter
      | Cons (Manager_operation {source; counter; _}, rest) ->
          let* () =
            check_source_and_counter
              ~expected_source
              ~source
              ~previous_counter
              ~counter
          in
          check_batch_tail_sanity source counter rest

  let check_batch :
      type kind.
      kind Kind.manager contents_list ->
      (public_key_hash * public_key option * Manager_counter.t) tzresult =
    let open Result_syntax in
    fun contents_list ->
      match contents_list with
      | Single (Manager_operation {source; operation = Reveal key; counter; _})
        ->
          return (source, Some key, counter)
      | Single (Manager_operation {source; counter; _}) ->
          return (source, None, counter)
      | Cons
          (Manager_operation {source; operation = Reveal key; counter; _}, rest)
        ->
          let* () = check_batch_tail_sanity source counter rest in
          return (source, Some key, counter)
      | Cons (Manager_operation {source; counter; _}, rest) ->
          let* () = check_batch_tail_sanity source counter rest in
          return (source, None, counter)

  (** Check a few simple properties of the batch, and return the
      initial {!batch_state} and the contract public key.

      Invariants checked:

      - All operations in a batch have the same source.

      - The source's contract is allocated.

      - The counters in a batch are successive, and the first of them
        is the source's next expected counter.

      - A batch contains at most one Reveal operation that must occur
        in first position.

      - The source's public key has been revealed (either before the
        considered batch, or during its first operation).

      Note that currently, the [op] batch contains only one signature,
      so all operations in the batch are required to originate from the
      same manager. This may change in the future, in order to allow
      several managers to group-sign a sequence of operations. *)
  let check_sanity_and_find_public_key vi
      (contents_list : _ Kind.manager contents_list) =
    let open Lwt_result_syntax in
    let*? source, revealed_key, first_counter = check_batch contents_list in
    let* balance = Contract.check_allocated_and_get_balance vi.ctxt source in
    let* () = Contract.check_counter_increment vi.ctxt source first_counter in
    let* pk =
      (* Note that it is important to always retrieve the public
         key. This includes the case where the key ends up not being
         used because the signature check is skipped in
         {!validate_manager_operation} called with
         [~check_signature:false]. Indeed, the mempool may use
         this argument when it has already checked the signature of
         the operation in the past; but if there has been a branch
         reorganization since then, the key might not be revealed in
         the new branch anymore, in which case
         {!Contract.get_manager_key} will return an error. *)
      match revealed_key with
      | Some pk -> return pk
      | None -> Contract.get_manager_key vi.ctxt source
    in
    let initial_batch_state =
      {
        balance;
        (* Initial contract allocation is ensured by the success of
           the call to {!Contract.check_allocated_and_get_balance}
           above. *)
        is_allocated = true;
        total_gas_used = Gas.Arith.zero;
      }
    in
    return (initial_batch_state, pk)

  let check_gas_limit info ~gas_limit =
    Gas.check_gas_limit
      ~hard_gas_limit_per_operation:
        info.manager_info.hard_gas_limit_per_operation
      ~gas_limit

  let check_storage_limit vi storage_limit =
    error_unless
      Compare.Z.(
        storage_limit <= vi.manager_info.hard_storage_limit_per_operation
        && storage_limit >= Z.zero)
      Fees.Storage_limit_too_high

  let assert_pvm_kind_enabled vi kind =
    let open Result_syntax in
    let* () =
      error_when
        ((not (Constants.sc_rollup_arith_pvm_enable vi.ctxt))
        && Sc_rollup.Kind.(equal kind Example_arith))
        Sc_rollup_arith_pvm_disabled
    in
    error_when
      ((not (Constants.sc_rollup_riscv_pvm_enable vi.ctxt))
      && Sc_rollup.Kind.(equal kind Riscv))
      Sc_rollup_riscv_pvm_disabled

  let assert_not_zero_messages messages =
    match messages with
    | [] -> result_error Sc_rollup_errors.Sc_rollup_add_zero_messages
    | _ -> ok_unit

  let assert_zk_rollup_feature_enabled vi =
    error_unless (Constants.zk_rollup_enable vi.ctxt) Zk_rollup_feature_disabled

  let consume_decoding_gas remaining_gas lexpr =
    record_trace Gas_quota_exceeded_init_deserialize
    @@ (* Fail early if the operation does not have enough gas to
          cover the deserialization cost. We always consider the full
          deserialization cost, independently from the internal state
          of the lazy_expr. Otherwise we might risk getting different
          results if the operation has already been deserialized
          before (e.g. when retrieved in JSON format). Note that the
          lazy_expr is not actually decoded here; its deserialization
          cost is estimated from the size of its bytes. *)
    Script.consume_decoding_gas remaining_gas lexpr

  let may_trace_gas_limit_too_high info =
    match info.mode with
    | Application _ | Partial_validation _ | Construction _ -> fun x -> x
    | Mempool ->
        (* [Gas.check_limit] will only
           raise a "temporary" error, however when
           {!validate_operation} is called on a batch in isolation
           (like e.g. in the mempool) it must "refuse" operations
           whose total gas limit (the sum of the [gas_limit]s of each
           operation) is already above the block limit. We add the
           "permanent" error [Gas.Gas_limit_too_high] on top of the
           trace to this effect. *)
        record_trace Gas.Gas_limit_too_high

  let check_kind_specific_content (type kind)
      (contents : kind Kind.manager contents) remaining_gas vi =
    let open Result_syntax in
    let (Manager_operation
          {
            source;
            fee = _;
            counter = _;
            operation;
            gas_limit = _;
            storage_limit = _;
          }) =
      contents
    in
    match operation with
    | Reveal pk -> Contract.check_public_key pk source
    | Transaction {parameters; _} ->
        let* (_ : Gas.Arith.fp) =
          consume_decoding_gas remaining_gas parameters
        in
        return_unit
    | Origination {script; _} ->
        let* remaining_gas = consume_decoding_gas remaining_gas script.code in
        let* (_ : Gas.Arith.fp) =
          consume_decoding_gas remaining_gas script.storage
        in
        return_unit
    | Register_global_constant {value} ->
        let* (_ : Gas.Arith.fp) = consume_decoding_gas remaining_gas value in
        return_unit
    | Delegation (Some pkh) -> Delegate.check_not_tz4 pkh
    | Update_consensus_key pk -> Delegate.Consensus_key.check_not_tz4 pk
    | Delegation None | Set_deposits_limit _ | Increase_paid_storage _ ->
        return_unit
    | Transfer_ticket {contents; ty; _} ->
        let* remaining_gas = consume_decoding_gas remaining_gas contents in
        let* (_ : Gas.Arith.fp) = consume_decoding_gas remaining_gas ty in
        return_unit
    | Sc_rollup_originate {kind; _} -> assert_pvm_kind_enabled vi kind
    | Sc_rollup_add_messages {messages; _} -> assert_not_zero_messages messages
    | Sc_rollup_cement _ | Sc_rollup_publish _ | Sc_rollup_refute _
    | Sc_rollup_timeout _ | Sc_rollup_execute_outbox_message _
    | Sc_rollup_recover_bond _ ->
        (* TODO: https://gitlab.com/tezos/tezos/-/issues/3063
           Should we successfully precheck Sc_rollup_recover_bond and any
           (simple) Sc rollup operation, or should we add some some checks to make
           the operations Branch_delayed if they cannot be successfully
           prechecked? *)
        return_unit
    | Dal_publish_commitment slot_header ->
        Dal_apply.validate_publish_commitment vi.ctxt slot_header
    | Zk_rollup_origination _ | Zk_rollup_publish _ | Zk_rollup_update _ ->
        assert_zk_rollup_feature_enabled vi

  let check_contents (type kind) vi batch_state
      (contents : kind Kind.manager contents) ~consume_gas_for_sig_check
      remaining_block_gas =
    let open Lwt_result_syntax in
    let (Manager_operation
          {source; fee; counter = _; operation = _; gas_limit; storage_limit}) =
      contents
    in
    let*? () = check_gas_limit vi ~gas_limit in
    let total_gas_used =
      Gas.Arith.(add batch_state.total_gas_used (fp gas_limit))
    in
    let*? () =
      may_trace_gas_limit_too_high vi
      @@ error_unless
           Gas.Arith.(fp total_gas_used <= remaining_block_gas)
           Gas.Block_quota_exceeded
    in
    (* Part of the gas cost of the operation which is independent of
       the contents of the operation. It is
       Michelson_v1_gas.Cost_of.manager_operation constant plus the
       cost of checking the signature if the operation is the first of
       the batch. *)
    let fixed_gas_cost =
      let manager_op_cost = Michelson_v1_gas.Cost_of.manager_operation in
      match consume_gas_for_sig_check with
      | None -> manager_op_cost
      | Some gas_for_sig_check -> Gas.(manager_op_cost +@ gas_for_sig_check)
    in
    let*? remaining_gas =
      record_trace
        Insufficient_gas_for_manager
        (Gas.consume_from (Gas.Arith.fp gas_limit) fixed_gas_cost)
    in
    let*? () = check_storage_limit vi storage_limit in
    let*? () =
      (* {!Contract.must_be_allocated} has already been called while
         initializing [batch_state]. This checks that the contract has
         not been emptied by spending fees for previous operations in
         the batch. *)
      error_unless
        batch_state.is_allocated
        (Contract_storage.Empty_implicit_contract source)
    in
    let*? () = check_kind_specific_content contents remaining_gas vi in
    (* Gas should no longer be consumed below this point, because it
       would not take into account any gas consumed by
       {!check_kind_specific_content}. If you really need to consume gas here, then you
       must make {!check_kind_specific_content} return the [remaining_gas].*)
    let* balance, is_allocated =
      Contract.simulate_spending
        vi.ctxt
        ~balance:batch_state.balance
        ~amount:fee
        source
    in
    return {total_gas_used; balance; is_allocated}

  (** This would be [fold_left_es (check_contents vi) batch_state
     contents_list] if [contents_list] were an ordinary [list].  The
     [consume_gas_for_sig_check] arg indicates whether or not gas for
     checking the signature of the batch should be consumed; it is
     [None] if the cost has already been consumed and [Some cost] if
     the cost to be consumed is [cost] and remains to be
     consumed. This cost is consumed in the first operation of the
     batch. *)
  let rec check_contents_list :
      type kind.
      info ->
      batch_state ->
      kind Kind.manager contents_list ->
      consume_gas_for_sig_check:Gas.cost option ->
      Gas.Arith.fp ->
      Gas.Arith.fp tzresult Lwt.t =
   fun vi batch_state contents_list ~consume_gas_for_sig_check remaining_gas ->
    let open Lwt_result_syntax in
    match contents_list with
    | Single contents ->
        let* batch_state =
          check_contents
            vi
            batch_state
            contents
            ~consume_gas_for_sig_check
            remaining_gas
        in
        return batch_state.total_gas_used
    | Cons (contents, tail) ->
        let* batch_state =
          check_contents
            vi
            batch_state
            contents
            ~consume_gas_for_sig_check
            remaining_gas
        in
        check_contents_list
          vi
          batch_state
          tail
          ~consume_gas_for_sig_check:None
          remaining_gas

  let check_manager_operation vi ~check_signature
      (operation : _ Kind.manager operation) remaining_block_gas =
    let open Lwt_result_syntax in
    let contents_list = operation.protocol_data.contents in
    let* batch_state, source_pk =
      check_sanity_and_find_public_key vi contents_list
    in
    let signature_checking_gas_cost =
      Operation_costs.check_signature_cost
        (Michelson_v1_gas.Cost_of.Interpreter.algo_of_public_key source_pk)
        operation
    in
    let* gas_used =
      check_contents_list
        vi
        batch_state
        contents_list
        ~consume_gas_for_sig_check:(Some signature_checking_gas_cost)
        remaining_block_gas
    in
    let*? () =
      if check_signature then
        Operation.check_signature source_pk vi.chain_id operation
      else ok_unit
    in
    return gas_used

  let check_manager_operation_conflict (type kind) vs oph
      (operation : kind Kind.manager operation) =
    let source =
      match operation.protocol_data.contents with
      | Single (Manager_operation {source; _})
      | Cons (Manager_operation {source; _}, _) ->
          source
    in
    (* One-operation-per-manager-per-block restriction (1M) *)
    match
      Signature.Public_key_hash.Map.find_opt
        source
        vs.manager_state.managers_seen
    with
    | None -> ok_unit
    | Some existing ->
        Error (Operation_conflict {existing; new_operation = oph})

  let wrap_check_manager_operation_conflict (type kind)
      (operation : kind Kind.manager operation) =
    let source =
      match operation.protocol_data.contents with
      | Single (Manager_operation {source; _})
      | Cons (Manager_operation {source; _}, _) ->
          source
    in
    function
    | Ok () -> ok_unit
    | Error conflict -> result_error (Manager_restriction {source; conflict})

  let add_manager_operation (type kind) vs oph
      (operation : kind Kind.manager operation) =
    let source =
      match operation.protocol_data.contents with
      | Single (Manager_operation {source; _})
      | Cons (Manager_operation {source; _}, _) ->
          source
    in
    let managers_seen =
      Signature.Public_key_hash.Map.add
        source
        oph
        vs.manager_state.managers_seen
    in
    {vs with manager_state = {managers_seen}}

  (* Return the new [block_state] with the updated remaining gas used:
     - In non-mempool modes, this value is
       [block_state.remaining_block_gas], in which the gas from the
       validated operation has been subtracted.

     - In [Mempool] mode, the [block_state] should remain
       unchanged. Indeed, we only want each batch to not exceed the
       block limit individually, without taking other operations
       into account. *)
  let may_update_remaining_gas_used mode (block_state : block_state)
      operation_gas_used =
    match mode with
    | Application _ | Partial_validation _ | Construction _ ->
        let remaining_block_gas =
          Gas.Arith.(sub block_state.remaining_block_gas operation_gas_used)
        in
        {block_state with remaining_block_gas}
    | Mempool -> block_state

  let remove_manager_operation (type kind) vs
      (operation : kind Kind.manager operation) =
    let source =
      match operation.protocol_data.contents with
      | Single (Manager_operation {source; _})
      | Cons (Manager_operation {source; _}, _) ->
          source
    in
    let managers_seen =
      Signature.Public_key_hash.Map.remove source vs.manager_state.managers_seen
    in
    {vs with manager_state = {managers_seen}}

  let validate_manager_operation ~check_signature info operation_state
      block_state oph operation =
    let open Lwt_result_syntax in
    let* gas_used =
      check_manager_operation
        info
        ~check_signature
        operation
        block_state.remaining_block_gas
    in
    let*? () =
      check_manager_operation_conflict operation_state oph operation
      |> wrap_check_manager_operation_conflict operation
    in
    let operation_state = add_manager_operation operation_state oph operation in
    let block_state =
      may_update_remaining_gas_used info.mode block_state gas_used
    in
    return {info; operation_state; block_state}
end

let init_validation_state ctxt mode chain_id ~predecessor_level_and_round =
  let info = init_info ctxt mode chain_id ~predecessor_level_and_round in
  let operation_state = empty_operation_conflict_state in
  let block_state = init_block_state info in
  {info; operation_state; block_state}

(* Pre-condition: Shell block headers' checks have already been done.
   These checks must ensure that:
   - the block header level is the succ of the predecessor block level
   - the timestamp of the predecessor is lower than the current block's
   - the fitness of the block is greater than its predecessor's
   - the number of operations by validation passes does not exceed the quota
     established by the protocol
   - the size of an operation does not exceed [max_operation_data_length]
*)
let begin_any_application ctxt chain_id ~predecessor_level
    ~predecessor_timestamp (block_header : Block_header.t) fitness ~is_partial =
  let open Lwt_result_syntax in
  let predecessor_round = Fitness.predecessor_round fitness in
  let round = Fitness.round fitness in
  let current_level = Level.current ctxt in
  let* ctxt, _slot, block_producer =
    Stake_distribution.baking_rights_owner ctxt current_level ~round
  in
  let*? () =
    Block_header.begin_validate_block_header
      ~block_header
      ~chain_id
      ~predecessor_timestamp
      ~predecessor_round
      ~fitness
      ~timestamp:block_header.shell.timestamp
      ~delegate_pk:block_producer.consensus_pk
      ~round_durations:(Constants.round_durations ctxt)
      ~proof_of_work_threshold:(Constants.proof_of_work_threshold ctxt)
      ~expected_commitment:current_level.expected_commitment
  in
  let* () =
    Consensus.check_delegate_is_not_forbidden ctxt block_producer.delegate
  in
  let* ctxt, _slot, _payload_producer =
    (* We just make sure that this call will not fail in apply.ml *)
    Stake_distribution.baking_rights_owner
      ctxt
      current_level
      ~round:block_header.protocol_data.contents.payload_round
  in
  let predecessor_hash = block_header.shell.predecessor in
  let block_info =
    {
      round;
      locked_round = Fitness.locked_round fitness;
      predecessor_hash;
      header_contents = block_header.protocol_data.contents;
    }
  in
  let mode =
    if is_partial then Partial_validation block_info else Application block_info
  in
  let validation_state =
    init_validation_state
      ctxt
      mode
      chain_id
      ~predecessor_level_and_round:
        (Some (predecessor_level.Level.level, predecessor_round))
  in
  return validation_state

let begin_partial_validation ctxt chain_id ~predecessor_level
    ~predecessor_timestamp block_header fitness =
  begin_any_application
    ctxt
    chain_id
    ~predecessor_level
    ~predecessor_timestamp
    block_header
    fitness
    ~is_partial:true

let begin_application ctxt chain_id ~predecessor_level ~predecessor_timestamp
    block_header fitness =
  begin_any_application
    ctxt
    chain_id
    ~predecessor_level
    ~predecessor_timestamp
    block_header
    fitness
    ~is_partial:false

let begin_full_construction ctxt chain_id ~predecessor_level ~predecessor_round
    ~predecessor_timestamp ~predecessor_hash round
    (header_contents : Block_header.contents) =
  let open Lwt_result_syntax in
  let round_durations = Constants.round_durations ctxt in
  let timestamp = Timestamp.current ctxt in
  let*? () =
    Block_header.check_timestamp
      round_durations
      ~timestamp
      ~round
      ~predecessor_timestamp
      ~predecessor_round
  in
  let current_level = Level.current ctxt in
  let* ctxt, _slot, block_producer =
    Stake_distribution.baking_rights_owner ctxt current_level ~round
  in
  let* () =
    Consensus.check_delegate_is_not_forbidden ctxt block_producer.delegate
  in
  let* ctxt, _slot, _payload_producer =
    (* We just make sure that this call will not fail in apply.ml *)
    Stake_distribution.baking_rights_owner
      ctxt
      current_level
      ~round:header_contents.payload_round
  in
  let validation_state =
    init_validation_state
      ctxt
      (Construction {round; predecessor_hash; header_contents})
      chain_id
      ~predecessor_level_and_round:
        (Some (predecessor_level.Level.level, predecessor_round))
  in
  return validation_state

let begin_partial_construction ctxt chain_id ~predecessor_level
    ~predecessor_round =
  let validation_state =
    init_validation_state
      ctxt
      Mempool
      chain_id
      ~predecessor_level_and_round:
        (Some (predecessor_level.Level.level, predecessor_round))
  in
  validation_state

let begin_no_predecessor_info ctxt chain_id =
  init_validation_state ctxt Mempool chain_id ~predecessor_level_and_round:None

let check_operation ?(check_signature = true) info (type kind)
    (operation : kind operation) : unit tzresult Lwt.t =
  let open Lwt_result_syntax in
  match operation.protocol_data.contents with
  | Single (Preattestation _) ->
      let* (_voting_power : int) =
        Consensus.check_preattestation info ~check_signature operation
      in
      return_unit
  | Single (Attestation _) ->
      let* (_voting_power : int) =
        Consensus.check_attestation info ~check_signature operation
      in
      return_unit
  | Single (Proposals _) ->
      Voting.check_proposals info ~check_signature operation
  | Single (Ballot _) -> Voting.check_ballot info ~check_signature operation
  | Single (Activate_account _) ->
      Anonymous.check_activate_account info operation
  | Single (Double_preattestation_evidence _) ->
      Anonymous.check_double_preattestation_evidence info operation
  | Single (Double_attestation_evidence _) ->
      Anonymous.check_double_attestation_evidence info operation
  | Single (Double_baking_evidence _) ->
      Anonymous.check_double_baking_evidence info operation
  | Single (Drain_delegate _) ->
      Anonymous.check_drain_delegate info ~check_signature operation
  | Single (Seed_nonce_revelation _) ->
      Anonymous.check_seed_nonce_revelation info operation
  | Single (Vdf_revelation _) -> Anonymous.check_vdf_revelation info operation
  | Single (Manager_operation _) ->
      let remaining_gas =
        Gas.Arith.fp (Constants.hard_gas_limit_per_block info.ctxt)
      in
      let* (_remaining_gas : Gas.Arith.fp) =
        Manager.check_manager_operation
          info
          ~check_signature
          operation
          remaining_gas
      in
      return_unit
  | Cons (Manager_operation _, _) ->
      let remaining_gas =
        Gas.Arith.fp (Constants.hard_gas_limit_per_block info.ctxt)
      in
      let* (_remaining_gas : Gas.Arith.fp) =
        Manager.check_manager_operation
          info
          ~check_signature
          operation
          remaining_gas
      in
      return_unit
  | Single (Failing_noop _) -> tzfail Validate_errors.Failing_noop_error

let check_operation_conflict (type kind) operation_conflict_state oph
    (operation : kind operation) =
  match operation.protocol_data.contents with
  | Single (Preattestation _) ->
      Consensus.check_preattestation_conflict
        operation_conflict_state
        oph
        operation
  | Single (Attestation _) ->
      Consensus.check_attestation_conflict
        operation_conflict_state
        oph
        operation
  | Single (Proposals _) ->
      Voting.check_proposals_conflict operation_conflict_state oph operation
  | Single (Ballot _) ->
      Voting.check_ballot_conflict operation_conflict_state oph operation
  | Single (Activate_account _) ->
      Anonymous.check_activate_account_conflict
        operation_conflict_state
        oph
        operation
  | Single (Double_preattestation_evidence _) ->
      Anonymous.check_double_preattestation_evidence_conflict
        operation_conflict_state
        oph
        operation
  | Single (Double_attestation_evidence _) ->
      Anonymous.check_double_attestation_evidence_conflict
        operation_conflict_state
        oph
        operation
  | Single (Double_baking_evidence _) ->
      Anonymous.check_double_baking_evidence_conflict
        operation_conflict_state
        oph
        operation
  | Single (Drain_delegate _) ->
      Anonymous.check_drain_delegate_conflict
        operation_conflict_state
        oph
        operation
  | Single (Seed_nonce_revelation _) ->
      Anonymous.check_seed_nonce_revelation_conflict
        operation_conflict_state
        oph
        operation
  | Single (Vdf_revelation _) ->
      Anonymous.check_vdf_revelation_conflict operation_conflict_state oph
  | Single (Manager_operation _) ->
      Manager.check_manager_operation_conflict
        operation_conflict_state
        oph
        operation
  | Cons (Manager_operation _, _) ->
      Manager.check_manager_operation_conflict
        operation_conflict_state
        oph
        operation
  | Single (Failing_noop _) -> (* Nothing to do *) ok_unit

let add_valid_operation operation_conflict_state oph (type kind)
    (operation : kind operation) =
  match operation.protocol_data.contents with
  | Single (Preattestation _) ->
      Consensus.add_preattestation operation_conflict_state oph operation
  | Single (Attestation _) ->
      Consensus.add_attestation operation_conflict_state oph operation
  | Single (Proposals _) ->
      Voting.add_proposals operation_conflict_state oph operation
  | Single (Ballot _) ->
      Voting.add_ballot operation_conflict_state oph operation
  | Single (Activate_account _) ->
      Anonymous.add_activate_account operation_conflict_state oph operation
  | Single (Double_preattestation_evidence _) ->
      Anonymous.add_double_preattestation_evidence
        operation_conflict_state
        oph
        operation
  | Single (Double_attestation_evidence _) ->
      Anonymous.add_double_attestation_evidence
        operation_conflict_state
        oph
        operation
  | Single (Double_baking_evidence _) ->
      Anonymous.add_double_baking_evidence
        operation_conflict_state
        oph
        operation
  | Single (Drain_delegate _) ->
      Anonymous.add_drain_delegate operation_conflict_state oph operation
  | Single (Seed_nonce_revelation _) ->
      Anonymous.add_seed_nonce_revelation operation_conflict_state oph operation
  | Single (Vdf_revelation _) ->
      Anonymous.add_vdf_revelation operation_conflict_state oph
  | Single (Manager_operation _) ->
      Manager.add_manager_operation operation_conflict_state oph operation
  | Cons (Manager_operation _, _) ->
      Manager.add_manager_operation operation_conflict_state oph operation
  | Single (Failing_noop _) -> (* Nothing to do *) operation_conflict_state

(* Hypothesis:
   - the [operation] has been validated and is present in [vs];
   - this function is only valid for the mempool mode. *)
let remove_operation operation_conflict_state (type kind)
    (operation : kind operation) =
  match operation.protocol_data.contents with
  | Single (Preattestation _) ->
      Consensus.remove_preattestation operation_conflict_state operation
  | Single (Attestation _) ->
      Consensus.remove_attestation operation_conflict_state operation
  | Single (Proposals _) ->
      Voting.remove_proposals operation_conflict_state operation
  | Single (Ballot _) -> Voting.remove_ballot operation_conflict_state operation
  | Single (Activate_account _) ->
      Anonymous.remove_activate_account operation_conflict_state operation
  | Single (Double_preattestation_evidence _) ->
      Anonymous.remove_double_preattestation_evidence
        operation_conflict_state
        operation
  | Single (Double_attestation_evidence _) ->
      Anonymous.remove_double_attestation_evidence
        operation_conflict_state
        operation
  | Single (Double_baking_evidence _) ->
      Anonymous.remove_double_baking_evidence operation_conflict_state operation
  | Single (Drain_delegate _) ->
      Anonymous.remove_drain_delegate operation_conflict_state operation
  | Single (Seed_nonce_revelation _) ->
      Anonymous.remove_seed_nonce_revelation operation_conflict_state operation
  | Single (Vdf_revelation _) ->
      Anonymous.remove_vdf_revelation operation_conflict_state
  | Single (Manager_operation _) ->
      Manager.remove_manager_operation operation_conflict_state operation
  | Cons (Manager_operation _, _) ->
      Manager.remove_manager_operation operation_conflict_state operation
  | Single (Failing_noop _) -> (* Nothing to do *) operation_conflict_state

let check_validation_pass_consistency vi vs validation_pass =
  let open Lwt_result_syntax in
  match vi.mode with
  | Mempool | Construction _ -> return vs
  | Application _ | Partial_validation _ -> (
      match (vs.last_op_validation_pass, validation_pass) with
      | None, validation_pass ->
          return {vs with last_op_validation_pass = validation_pass}
      | Some previous_vp, Some validation_pass ->
          let* () =
            fail_unless
              Compare.Int.(previous_vp <= validation_pass)
              (Validate_errors.Block.Inconsistent_validation_passes_in_block
                 {expected = previous_vp; provided = validation_pass})
          in
          return {vs with last_op_validation_pass = Some validation_pass}
      | Some _, None -> tzfail Validate_errors.Failing_noop_error)

(** Increment [vs.op_count] for all operations, and record
    non-consensus operation hashes in [vs.recorded_operations_rev]. *)
let record_operation vs ophash validation_pass_opt =
  let op_count = vs.op_count + 1 in
  match validation_pass_opt with
  | Some n when Compare.Int.(n = Operation_repr.consensus_pass) ->
      {vs with op_count}
  | _ ->
      {
        vs with
        op_count;
        recorded_operations_rev = ophash :: vs.recorded_operations_rev;
      }

let validate_operation ?(check_signature = true)
    {info; operation_state; block_state} oph
    (packed_operation : packed_operation) =
  let open Lwt_result_syntax in
  let {shell; protocol_data = Operation_data protocol_data} =
    packed_operation
  in
  let validation_pass_opt = Operation.acceptable_pass packed_operation in
  let* block_state =
    check_validation_pass_consistency info block_state validation_pass_opt
  in
  let block_state = record_operation block_state oph validation_pass_opt in
  let operation : _ operation = {shell; protocol_data} in
  match (info.mode, validation_pass_opt) with
  | Partial_validation _, Some n
    when Compare.Int.(n <> Operation_repr.consensus_pass) ->
      (* Do not validate non-consensus operations in
         [Partial_validation] mode. *)
      return {info; operation_state; block_state}
  | (Application _ | Partial_validation _ | Construction _ | Mempool), _ -> (
      match operation.protocol_data.contents with
      | Single (Preattestation _) ->
          Consensus.validate_preattestation
            ~check_signature
            info
            operation_state
            block_state
            oph
            operation
      | Single (Attestation _) ->
          Consensus.validate_attestation
            ~check_signature
            info
            operation_state
            block_state
            oph
            operation
      | Single (Proposals _) ->
          let open Voting in
          let* () = check_proposals info ~check_signature operation in
          let*? () =
            check_proposals_conflict operation_state oph operation
            |> wrap_proposals_conflict
          in
          let operation_state = add_proposals operation_state oph operation in
          return {info; operation_state; block_state}
      | Single (Ballot _) ->
          let open Voting in
          let* () = check_ballot info ~check_signature operation in
          let*? () =
            check_ballot_conflict operation_state oph operation
            |> wrap_ballot_conflict
          in
          let operation_state = add_ballot operation_state oph operation in
          return {info; operation_state; block_state}
      | Single (Activate_account _) ->
          let open Anonymous in
          let* () = check_activate_account info operation in
          let*? () =
            check_activate_account_conflict operation_state oph operation
            |> wrap_activate_account_conflict operation
          in
          let operation_state =
            add_activate_account operation_state oph operation
          in
          return {info; operation_state; block_state}
      | Single (Double_preattestation_evidence _) ->
          let open Anonymous in
          let* () = check_double_preattestation_evidence info operation in
          let*? () =
            check_double_preattestation_evidence_conflict
              operation_state
              oph
              operation
            |> wrap_denunciation_conflict Double_preattesting
          in
          let operation_state =
            add_double_preattestation_evidence operation_state oph operation
          in
          return {info; operation_state; block_state}
      | Single (Double_attestation_evidence _) ->
          let open Anonymous in
          let* () = check_double_attestation_evidence info operation in
          let*? () =
            check_double_attestation_evidence_conflict
              operation_state
              oph
              operation
            |> wrap_denunciation_conflict Double_attesting
          in
          let operation_state =
            add_double_attestation_evidence operation_state oph operation
          in
          return {info; operation_state; block_state}
      | Single (Double_baking_evidence _) ->
          let open Anonymous in
          let* () = check_double_baking_evidence info operation in
          let*? () =
            check_double_baking_evidence_conflict operation_state oph operation
            |> wrap_denunciation_conflict Double_baking
          in
          let operation_state =
            add_double_baking_evidence operation_state oph operation
          in
          return {info; operation_state; block_state}
      | Single (Drain_delegate _) ->
          let open Anonymous in
          let* () = check_drain_delegate info ~check_signature operation in
          let*? () =
            check_drain_delegate_conflict operation_state oph operation
            |> wrap_drain_delegate_conflict operation
          in
          let operation_state =
            add_drain_delegate operation_state oph operation
          in
          return {info; operation_state; block_state}
      | Single (Seed_nonce_revelation _) ->
          let open Anonymous in
          let* () = check_seed_nonce_revelation info operation in
          let*? () =
            check_seed_nonce_revelation_conflict operation_state oph operation
            |> wrap_seed_nonce_revelation_conflict
          in
          let operation_state =
            add_seed_nonce_revelation operation_state oph operation
          in
          return {info; operation_state; block_state}
      | Single (Vdf_revelation _) ->
          let open Anonymous in
          let* () = check_vdf_revelation info operation in
          let*? () =
            check_vdf_revelation_conflict operation_state oph
            |> wrap_vdf_revelation_conflict
          in
          let operation_state = add_vdf_revelation operation_state oph in
          return {info; operation_state; block_state}
      | Single (Manager_operation _) ->
          Manager.validate_manager_operation
            ~check_signature
            info
            operation_state
            block_state
            oph
            operation
      | Cons (Manager_operation _, _) ->
          Manager.validate_manager_operation
            ~check_signature
            info
            operation_state
            block_state
            oph
            operation
      | Single (Failing_noop _) -> tzfail Validate_errors.Failing_noop_error)

(** Block finalization *)

open Validate_errors.Block

let check_attestation_power vi bs =
  let open Lwt_result_syntax in
  let* are_attestations_required =
    (* The migration block (whose level is [first_level_of_protocol])
       is always considered final, and is not attested. Therefore, the
       block at the next level does not need to contain attestations.
       (Note that the migration block itself is validated by the
       previous protocol, so the returned value for it does not matter.) *)
    let* first_level_of_protocol = First_level_of_protocol.get vi.ctxt in
    let level_position_in_protocol =
      Raw_level.diff vi.current_level.level first_level_of_protocol
    in
    return Compare.Int32.(level_position_in_protocol > 1l)
  in
  if are_attestations_required then
    let required = Constants.consensus_threshold vi.ctxt in
    let provided = bs.attestation_power in
    fail_unless
      Compare.Int.(provided >= required)
      (Not_enough_attestations {required; provided})
  else return_unit

(** Check that the locked round in the fitness and the locked round
    observed in the preattestations are the same.

    This check is not called in construction mode because there is
    no provided fitness (meaning that we do not know whether the block
    should contain any preattestations).

    When the observed locked round is [Some _], we actually already
    know that it is identical to the fitness locked round, otherwise
    {!Consensus.check_preexisting_block_preattestation} would have
    rejected the preattestations. But this check is needed to reject
    blocks where the fitness locked round has a value yet there are no
    preattestations (ie. the observed locked round is [None]). *)
let check_fitness_locked_round bs fitness_locked_round =
  let observed_locked_round = Option.map fst bs.locked_round_evidence in
  error_unless
    (Option.equal Round.equal observed_locked_round fitness_locked_round)
    Fitness.Wrong_fitness

(** When there are preattestations, check that they point to a round
    before the block's round, and that their total power is high enough.

    Note that this function does not check whether the block actually
    contains preattestations when they are mandatory. This is checked by
    {!check_fitness_locked_round} instead. *)
let check_preattestation_round_and_power vi vs round =
  let open Result_syntax in
  match vs.locked_round_evidence with
  | None -> ok_unit
  | Some (preattestation_round, preattestation_count) ->
      let* () =
        (* Actually, this check should never fail, because we have
           already called {!Consensus.check_round_before_block} for
           all preattestations in a block. Nevertheless, it does not
           cost much to check again here. *)
        error_when
          Round.(preattestation_round >= round)
          (Locked_round_after_block_round
             {locked_round = preattestation_round; round})
      in
      let consensus_threshold = Constants.consensus_threshold vi.ctxt in
      error_when
        Compare.Int.(preattestation_count < consensus_threshold)
        (Insufficient_locked_round_evidence
           {voting_power = preattestation_count; consensus_threshold})

let check_payload_hash block_state ~predecessor_hash
    (block_header_contents : Block_header.contents) =
  let expected =
    Block_payload.hash
      ~predecessor_hash
      ~payload_round:block_header_contents.payload_round
      (List.rev block_state.recorded_operations_rev)
  in
  let provided = block_header_contents.payload_hash in
  error_unless
    (Block_payload_hash.equal expected provided)
    (Invalid_payload_hash {expected; provided})

let finalize_block {info; block_state; _} =
  let open Lwt_result_syntax in
  match info.mode with
  | Application {round; locked_round; predecessor_hash; header_contents} ->
      let* () = check_attestation_power info block_state in
      let*? () = check_fitness_locked_round block_state locked_round in
      let*? () = check_preattestation_round_and_power info block_state round in
      let*? () =
        check_payload_hash block_state ~predecessor_hash header_contents
      in
      return_unit
  | Partial_validation {round; locked_round; _} ->
      let* () = check_attestation_power info block_state in
      let*? () = check_fitness_locked_round block_state locked_round in
      let*? () = check_preattestation_round_and_power info block_state round in
      return_unit
  | Construction {round; predecessor_hash; header_contents} ->
      let* () = check_attestation_power info block_state in
      let*? () = check_preattestation_round_and_power info block_state round in
      let*? () =
        match block_state.locked_round_evidence with
        | Some _ ->
            check_payload_hash block_state ~predecessor_hash header_contents
        | None ->
            (* In construction mode, when there is no locked round
               evidence (ie. no preattestations), the baker cannot know
               the payload hash before selecting the operations.
               Therefore, we do not check the initially given payload
               hash. The baker will have to patch the resulting block
               header with the actual payload hash afterwards. *)
            ok_unit
      in
      return_unit
  | Mempool ->
      (* There is no block to finalize in mempool mode. *)
      return_unit
OCaml

Innovation. Community. Security.