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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use clap::*;
use move_vm_config::verifier::{MeterConfig, VerifierConfig};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use std::cell::RefCell;
use std::collections::BTreeSet;
use std::sync::atomic::{AtomicBool, Ordering};
use sui_protocol_config_macros::{ProtocolConfigAccessors, ProtocolConfigFeatureFlagsGetters};
use tracing::{info, warn};

/// The minimum and maximum protocol versions supported by this build.
const MIN_PROTOCOL_VERSION: u64 = 1;
const MAX_PROTOCOL_VERSION: u64 = 45;

// Record history of protocol version allocations here:
//
// Version 1: Original version.
// Version 2: Framework changes, including advancing epoch_start_time in safemode.
// Version 3: gas model v2, including all sui conservation fixes. Fix for loaded child object
//            changes, enable package upgrades, add limits on `max_size_written_objects`,
//            `max_size_written_objects_system_tx`
// Version 4: New reward slashing rate. Framework changes to skip stake susbidy when the epoch
//            length is short.
// Version 5: Package upgrade compatibility error fix. New gas cost table. New scoring decision
//            mechanism that includes up to f scoring authorities.
// Version 6: Change to how bytes are charged in the gas meter, increase buffer stake to 0.5f
// Version 7: Disallow adding new abilities to types during package upgrades,
//            disable_invariant_violation_check_in_swap_loc,
//            disable init functions becoming entry,
//            hash module bytes individually before computing package digest.
// Version 8: Disallow changing abilities and type constraints for type parameters in structs
//            during upgrades.
// Version 9: Limit the length of Move idenfitiers to 128.
//            Disallow extraneous module bytes,
//            advance_to_highest_supported_protocol_version,
// Version 10:increase bytecode verifier `max_verifier_meter_ticks_per_function` and
//            `max_meter_ticks_per_module` limits each from 6_000_000 to 16_000_000. sui-system
//            framework changes.
// Version 11: Introduce `std::type_name::get_with_original_ids` to the system frameworks. Bound max depth of values within the VM.
// Version 12: Changes to deepbook in framework to add API for querying marketplace.
//             Change NW Batch to use versioned metadata field.
//             Changes to sui-system package to add PTB-friendly unstake function, and minor cleanup.
// Version 13: System package change deprecating `0xdee9::clob` and `0xdee9::custodian`, replaced by
//             `0xdee9::clob_v2` and `0xdee9::custodian_v2`.
// Version 14: Introduce a config variable to allow charging of computation to be either
//             bucket base or rounding up. The presence of `gas_rounding_step` (or `None`)
//             decides whether rounding is applied or not.
// Version 15: Add reordering of user transactions by gas price after consensus.
//             Add `sui::table_vec::drop` to the framework via a system package upgrade.
// Version 16: Enabled simplified_unwrap_then_delete feature flag, which allows the execution engine
//             to no longer consult the object store when generating unwrapped_then_deleted in the
//             effects; this also allows us to stop including wrapped tombstones in accumulator.
//             Add self-matching prevention for deepbook.
// Version 17: Enable upgraded multisig support.
// Version 18: Introduce execution layer versioning, preserve all existing behaviour in v0.
//             Gas minimum charges moved to be a multiplier over the reference gas price. In this
//             protocol version the multiplier is the same as the lowest bucket of computation
//             such that the minimum transaction cost is the same as the minimum computation
//             bucket.
//             Add a feature flag to indicate the changes semantics of `base_tx_cost_fixed`.
// Version 19: Changes to sui-system package to enable liquid staking.
//             Add limit for total size of events.
//             Increase limit for number of events emitted to 1024.
// Version 20: Enables the flag `narwhal_new_leader_election_schedule` for the new narwhal leader
//             schedule algorithm for enhanced fault tolerance and sets the bad node stake threshold
//             value. Both values are set for all the environments except mainnet.
// Version 21: ZKLogin known providers.
// Version 22: Child object format change.
// Version 23: Enabling the flag `narwhal_new_leader_election_schedule` for the new narwhal leader
//             schedule algorithm for enhanced fault tolerance and sets the bad node stake threshold
//             value for mainnet.
// Version 24: Re-enable simple gas conservation checks.
//             Package publish/upgrade number in a single transaction limited.
//             JWK / authenticator state flags.
// Version 25: Add sui::table_vec::swap and sui::table_vec::swap_remove to system packages.
// Version 26: New gas model version.
//             Add support for receiving objects off of other objects in devnet only.
// Version 28: Add sui::zklogin::verify_zklogin_id and related functions to sui framework.
//             Enable transaction effects v2 in devnet.
// Version 29: Add verify_legacy_zklogin_address flag to sui framework, this add ability to verify
//             transactions from a legacy zklogin address.
// Version 30: Enable Narwhal CertificateV2
//             Add support for random beacon.
//             Enable transaction effects v2 in testnet.
//             Deprecate supported oauth providers from protocol config and rely on node config
//             instead.
//             In execution, has_public_transfer is recomputed when loading the object.
//             Add support for shared obj deletion and receiving objects off of other objects in devnet only.
// Version 31: Add support for shared object deletion in devnet only.
//             Add support for getting object ID referenced by receiving object in sui framework.
//             Create new execution layer version, and preserve previous behavior in v1.
//             Update semantics of `sui::transfer::receive` and add `sui::transfer::public_receive`.
// Version 32: Add delete functions for VerifiedID and VerifiedIssuer.
//             Add sui::token module to sui framework.
//             Enable transfer to object in testnet.
//             Enable Narwhal CertificateV2 on mainnet
//             Make critbit tree and order getters public in deepbook.
// Version 33: Add support for `receiving_object_id` function in framework
//             Hardened OTW check.
//             Enable transfer-to-object in mainnet.
//             Enable shared object deletion in testnet.
//             Enable effects v2 in mainnet.
// Version 34: Framework changes for random beacon.
// Version 35: Add poseidon hash function.
//             Enable coin deny list.
// Version 36: Enable group operations native functions in devnet.
//             Enable shared object deletion in mainnet.
//             Set the consensus accepted transaction size and the included transactions size in the proposed block.
// Version 37: Reject entry functions with mutable Random.
// Version 38: Introduce limits for binary tables size.
// Version 39: Allow skipped epochs for randomness updates.
//             Extra version to fix `test_upgrade_compatibility` simtest.
// Version 40:
// Version 41: Enable group operations native functions in testnet and mainnet (without msm).
// Version 42: Migrate sui framework and related code to Move 2024
// Version 43: Introduce the upper bound delta config for a zklogin signature's max epoch.
//             Introduce an explicit parameter for the tick limit per package (previously this was
//             represented by the parameter for the tick limit per module).
// Version 44: Enable consensus fork detection on mainnet.
//             Switch between Narwhal and Mysticeti consensus in tests, devnet and testnet.
// Version 45: Use tonic networking for Mysticeti consensus.

#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ProtocolVersion(u64);

impl ProtocolVersion {
    // The minimum and maximum protocol version supported by this binary. Counterintuitively, this constant may
    // change over time as support for old protocol versions is removed from the source. This
    // ensures that when a new network (such as a testnet) is created, its genesis committee will
    // use a protocol version that is actually supported by the binary.
    pub const MIN: Self = Self(MIN_PROTOCOL_VERSION);

    pub const MAX: Self = Self(MAX_PROTOCOL_VERSION);

    #[cfg(not(msim))]
    const MAX_ALLOWED: Self = Self::MAX;

    // We create one additional "fake" version in simulator builds so that we can test upgrades.
    #[cfg(msim)]
    pub const MAX_ALLOWED: Self = Self(MAX_PROTOCOL_VERSION + 1);

    pub fn new(v: u64) -> Self {
        Self(v)
    }

    pub const fn as_u64(&self) -> u64 {
        self.0
    }

    // For serde deserialization - we don't define a Default impl because there isn't a single
    // universally appropriate default value.
    pub fn max() -> Self {
        Self::MAX
    }
}

impl From<u64> for ProtocolVersion {
    fn from(v: u64) -> Self {
        Self::new(v)
    }
}

impl std::ops::Sub<u64> for ProtocolVersion {
    type Output = Self;
    fn sub(self, rhs: u64) -> Self::Output {
        Self::new(self.0 - rhs)
    }
}

impl std::ops::Add<u64> for ProtocolVersion {
    type Output = Self;
    fn add(self, rhs: u64) -> Self::Output {
        Self::new(self.0 + rhs)
    }
}

/// Models the set of protocol versions supported by a validator.
/// The `sui-node` binary will always use the SYSTEM_DEFAULT constant, but for testing we need
/// to be able to inject arbitrary versions into SuiNode.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct SupportedProtocolVersions {
    pub min: ProtocolVersion,
    pub max: ProtocolVersion,
}

impl SupportedProtocolVersions {
    pub const SYSTEM_DEFAULT: Self = Self {
        min: ProtocolVersion::MIN,
        max: ProtocolVersion::MAX,
    };

    /// Use by VersionedProtocolMessage implementors to describe in which range of versions a
    /// message variant is supported.
    pub fn new_for_message(min: u64, max: u64) -> Self {
        let min = ProtocolVersion::new(min);
        let max = ProtocolVersion::new(max);
        Self { min, max }
    }

    pub fn new_for_testing(min: u64, max: u64) -> Self {
        let min = min.into();
        let max = max.into();
        Self { min, max }
    }

    pub fn is_version_supported(&self, v: ProtocolVersion) -> bool {
        v.0 >= self.min.0 && v.0 <= self.max.0
    }
}

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Copy, PartialOrd, Ord, Eq, ValueEnum)]
pub enum Chain {
    Mainnet,
    Testnet,
    Unknown,
}

impl Default for Chain {
    fn default() -> Self {
        Self::Unknown
    }
}

pub struct Error(pub String);

/// Records on/off feature flags that may vary at each protocol version.
#[derive(Default, Clone, Serialize, Debug, ProtocolConfigFeatureFlagsGetters)]
struct FeatureFlags {
    // Add feature flags here, e.g.:
    // new_protocol_feature: bool,
    #[serde(skip_serializing_if = "is_false")]
    package_upgrades: bool,
    // If true, validators will commit to the root state digest
    // in end of epoch checkpoint proposals
    #[serde(skip_serializing_if = "is_false")]
    commit_root_state_digest: bool,
    // Pass epoch start time to advance_epoch safe mode function.
    #[serde(skip_serializing_if = "is_false")]
    advance_epoch_start_time_in_safe_mode: bool,
    // If true, apply the fix to correctly capturing loaded child object versions in execution's
    // object runtime.
    #[serde(skip_serializing_if = "is_false")]
    loaded_child_objects_fixed: bool,
    // If true, treat missing types in the upgraded modules when creating an upgraded package as a
    // compatibility error.
    #[serde(skip_serializing_if = "is_false")]
    missing_type_is_compatibility_error: bool,
    // If true, then the scoring decision mechanism will not get disabled when we do have more than
    // f low scoring authorities, but it will simply flag as low scoring only up to f authorities.
    #[serde(skip_serializing_if = "is_false")]
    scoring_decision_with_validity_cutoff: bool,

    // DEPRECATED: this was an ephemeral feature flag only used by consensus handler, which has now
    // been deployed everywhere.
    #[serde(skip_serializing_if = "is_false")]
    consensus_order_end_of_epoch_last: bool,

    // Disallow adding abilities to types during package upgrades.
    #[serde(skip_serializing_if = "is_false")]
    disallow_adding_abilities_on_upgrade: bool,
    // Disables unnecessary invariant check in the Move VM when swapping the value out of a local
    #[serde(skip_serializing_if = "is_false")]
    disable_invariant_violation_check_in_swap_loc: bool,
    // advance to highest supported protocol version at epoch change, instead of the next consecutive
    // protocol version.
    #[serde(skip_serializing_if = "is_false")]
    advance_to_highest_supported_protocol_version: bool,
    // If true, disallow entry modifiers on entry functions
    #[serde(skip_serializing_if = "is_false")]
    ban_entry_init: bool,
    // If true, hash module bytes individually when calculating package digests for upgrades
    #[serde(skip_serializing_if = "is_false")]
    package_digest_hash_module: bool,
    // If true, disallow changing struct type parameters during package upgrades
    #[serde(skip_serializing_if = "is_false")]
    disallow_change_struct_type_params_on_upgrade: bool,
    // If true, checks no extra bytes in a compiled module
    #[serde(skip_serializing_if = "is_false")]
    no_extraneous_module_bytes: bool,
    // If true, then use the versioned metadata format in narwhal entities.
    #[serde(skip_serializing_if = "is_false")]
    narwhal_versioned_metadata: bool,

    // Enable zklogin auth
    #[serde(skip_serializing_if = "is_false")]
    zklogin_auth: bool,
    // How we order transactions coming out of consensus before sending to execution.
    #[serde(skip_serializing_if = "ConsensusTransactionOrdering::is_none")]
    consensus_transaction_ordering: ConsensusTransactionOrdering,

    // Previously, the unwrapped_then_deleted field in TransactionEffects makes a distinction between
    // whether an object has existed in the store previously (i.e. whether there is a tombstone).
    // Such dependency makes effects generation inefficient, and requires us to include wrapped
    // tombstone in state root hash.
    // To prepare for effects V2, with this flag set to true, we simplify the definition of
    // unwrapped_then_deleted to always include unwrapped then deleted objects,
    // regardless of their previous state in the store.
    #[serde(skip_serializing_if = "is_false")]
    simplified_unwrap_then_delete: bool,
    // Enable upgraded multisig support
    #[serde(skip_serializing_if = "is_false")]
    upgraded_multisig_supported: bool,
    // If true minimum txn charge is a multiplier of the gas price
    #[serde(skip_serializing_if = "is_false")]
    txn_base_cost_as_multiplier: bool,

    // If true, the ability to delete shared objects is in effect
    #[serde(skip_serializing_if = "is_false")]
    shared_object_deletion: bool,

    // If true, then the new algorithm for the leader election schedule will be used
    #[serde(skip_serializing_if = "is_false")]
    narwhal_new_leader_election_schedule: bool,

    // A list of supported OIDC providers that can be used for zklogin.
    #[serde(skip_serializing_if = "is_empty")]
    zklogin_supported_providers: BTreeSet<String>,

    // If true, use the new child object format
    #[serde(skip_serializing_if = "is_false")]
    loaded_child_object_format: bool,

    #[serde(skip_serializing_if = "is_false")]
    enable_jwk_consensus_updates: bool,

    #[serde(skip_serializing_if = "is_false")]
    end_of_epoch_transaction_supported: bool,

    // Perform simple conservation checks keeping into account out of gas scenarios
    // while charging for storage.
    #[serde(skip_serializing_if = "is_false")]
    simple_conservation_checks: bool,

    // If true, use the new child object format type logging
    #[serde(skip_serializing_if = "is_false")]
    loaded_child_object_format_type: bool,

    // Enable receiving sent objects
    #[serde(skip_serializing_if = "is_false")]
    receive_objects: bool,

    // Enable random beacon protocol
    #[serde(skip_serializing_if = "is_false")]
    random_beacon: bool,

    #[serde(skip_serializing_if = "is_false")]
    enable_effects_v2: bool,

    // If true, then use CertificateV2 in narwhal.
    #[serde(skip_serializing_if = "is_false")]
    narwhal_certificate_v2: bool,

    // If true, allow verify with legacy zklogin address
    #[serde(skip_serializing_if = "is_false")]
    verify_legacy_zklogin_address: bool,

    // Enable throughput aware consensus submission
    #[serde(skip_serializing_if = "is_false")]
    throughput_aware_consensus_submission: bool,

    // If true, recompute has_public_transfer from the type instead of what is stored in the object
    #[serde(skip_serializing_if = "is_false")]
    recompute_has_public_transfer_in_execution: bool,

    // If true, multisig containing zkLogin sig is accepted.
    #[serde(skip_serializing_if = "is_false")]
    accept_zklogin_in_multisig: bool,

    // If true, consensus prologue transaction also includes the consensus output digest.
    // It can be used to detect consensus output folk.
    #[serde(skip_serializing_if = "is_false")]
    include_consensus_digest_in_prologue: bool,

    // If true, use the hardened OTW check
    #[serde(skip_serializing_if = "is_false")]
    hardened_otw_check: bool,

    // If true allow calling receiving_object_id function
    #[serde(skip_serializing_if = "is_false")]
    allow_receiving_object_id: bool,

    // Enable the poseidon hash function
    #[serde(skip_serializing_if = "is_false")]
    enable_poseidon: bool,

    // If true, enable the coin deny list.
    #[serde(skip_serializing_if = "is_false")]
    enable_coin_deny_list: bool,

    // Enable native functions for group operations.
    #[serde(skip_serializing_if = "is_false")]
    enable_group_ops_native_functions: bool,

    // Enable native function for msm.
    #[serde(skip_serializing_if = "is_false")]
    enable_group_ops_native_function_msm: bool,

    // Reject functions with mutable Random.
    #[serde(skip_serializing_if = "is_false")]
    reject_mutable_random_on_entry_functions: bool,

    // Controls the behavior of per object congestion control in consensus handler.
    #[serde(skip_serializing_if = "PerObjectCongestionControlMode::is_none")]
    per_object_congestion_control_mode: PerObjectCongestionControlMode,

    // The consensus protocol to be used for the epoch.
    #[serde(skip_serializing_if = "ConsensusChoice::is_narwhal")]
    consensus_choice: ConsensusChoice,

    // Consensus network to use.
    #[serde(skip_serializing_if = "ConsensusNetwork::is_anemo")]
    consensus_network: ConsensusNetwork,

    // Set the upper bound allowed for max_epoch in zklogin signature.
    #[serde(skip_serializing_if = "Option::is_none")]
    zklogin_max_epoch_upper_bound_delta: Option<u64>,
}

fn is_false(b: &bool) -> bool {
    !b
}

fn is_empty(b: &BTreeSet<String>) -> bool {
    b.is_empty()
}

/// Ordering mechanism for transactions in one Narwhal consensus output.
#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Debug)]
pub enum ConsensusTransactionOrdering {
    /// No ordering. Transactions are processed in the order they appear in the consensus output.
    #[default]
    None,
    /// Order transactions by gas price, highest first.
    ByGasPrice,
}

impl ConsensusTransactionOrdering {
    pub fn is_none(&self) -> bool {
        matches!(self, ConsensusTransactionOrdering::None)
    }
}

// The config for per object congestion control in consensus handler.
#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Debug)]
pub enum PerObjectCongestionControlMode {
    #[default]
    None, // No congestion control.
    TotalGasBudget, // Use txn gas budget as execution cost.
}

impl PerObjectCongestionControlMode {
    pub fn is_none(&self) -> bool {
        matches!(self, PerObjectCongestionControlMode::None)
    }
}

// Configuration options for consensus algorithm.
#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Debug)]
pub enum ConsensusChoice {
    #[default]
    Narwhal,
    SwapEachEpoch,
    Mysticeti,
}

impl ConsensusChoice {
    pub fn is_narwhal(&self) -> bool {
        matches!(self, ConsensusChoice::Narwhal)
    }
}

// Configuration options for consensus network.
#[derive(Default, Copy, Clone, PartialEq, Eq, Serialize, Debug)]
pub enum ConsensusNetwork {
    #[default]
    Anemo,
    Tonic,
}

impl ConsensusNetwork {
    pub fn is_anemo(&self) -> bool {
        matches!(self, ConsensusNetwork::Anemo)
    }
}

/// Constants that change the behavior of the protocol.
///
/// The value of each constant here must be fixed for a given protocol version. To change the value
/// of a constant, advance the protocol version, and add support for it in `get_for_version` under
/// the new version number.
/// (below).
///
/// To add a new field to this struct, use the following procedure:
/// - Advance the protocol version.
/// - Add the field as a private `Option<T>` to the struct.
/// - Initialize the field to `None` in prior protocol versions.
/// - Initialize the field to `Some(val)` for your new protocol version.
/// - Add a public getter that simply unwraps the field.
/// - Two public getters of the form `field(&self) -> field_type`
///     and `field_as_option(&self) -> Option<field_type>` will be automatically generated for you.
/// Example for a field: `new_constant: Option<u64>`
/// ```rust,ignore
///      pub fn new_constant(&self) -> u64 {
///         self.new_constant.expect(Self::CONSTANT_ERR_MSG)
///     }
///      pub fn new_constant_as_option(&self) -> Option<u64> {
///         self.new_constant.expect(Self::CONSTANT_ERR_MSG)
///     }
/// ```
/// With `pub fn new_constant(&self) -> u64`, if the constant is accessed in a protocol version
/// in which it is not defined, the validator will crash. (Crashing is necessary because
/// this type of error would almost always result in forking if not prevented here).
/// If you don't want the validator to crash, you can use the
/// `pub fn new_constant_as_option(&self) -> Option<u64>` getter, which will
/// return `None` if the field is not defined at that version.
/// - If you want a customized getter, you can add a method in the impl.
#[skip_serializing_none]
#[derive(Clone, Serialize, Debug, ProtocolConfigAccessors)]
pub struct ProtocolConfig {
    pub version: ProtocolVersion,

    feature_flags: FeatureFlags,

    // ==== Transaction input limits ====
    /// Maximum serialized size of a transaction (in bytes).
    max_tx_size_bytes: Option<u64>,

    /// Maximum number of input objects to a transaction. Enforced by the transaction input checker
    max_input_objects: Option<u64>,

    /// Max size of objects a transaction can write to disk after completion. Enforce by the Sui adapter.
    /// This is the sum of the serialized size of all objects written to disk.
    /// The max size of individual objects on the other hand is `max_move_object_size`.
    max_size_written_objects: Option<u64>,
    /// Max size of objects a system transaction can write to disk after completion. Enforce by the Sui adapter.
    /// Similar to `max_size_written_objects` but for system transactions.
    max_size_written_objects_system_tx: Option<u64>,

    /// Maximum size of serialized transaction effects.
    max_serialized_tx_effects_size_bytes: Option<u64>,

    /// Maximum size of serialized transaction effects for system transactions.
    max_serialized_tx_effects_size_bytes_system_tx: Option<u64>,

    /// Maximum number of gas payment objects for a transaction.
    max_gas_payment_objects: Option<u32>,

    /// Maximum number of modules in a Publish transaction.
    max_modules_in_publish: Option<u32>,

    /// Maximum number of transitive dependencies in a package when publishing.
    max_package_dependencies: Option<u32>,

    /// Maximum number of arguments in a move call or a ProgrammableTransaction's
    /// TransferObjects command.
    max_arguments: Option<u32>,

    /// Maximum number of total type arguments, computed recursively.
    max_type_arguments: Option<u32>,

    /// Maximum depth of an individual type argument.
    max_type_argument_depth: Option<u32>,

    /// Maximum size of a Pure CallArg.
    max_pure_argument_size: Option<u32>,

    /// Maximum number of Commands in a ProgrammableTransaction.
    max_programmable_tx_commands: Option<u32>,

    // ==== Move VM, Move bytecode verifier, and execution limits ===
    /// Maximum Move bytecode version the VM understands. All older versions are accepted.
    move_binary_format_version: Option<u32>,
    /// Configuration controlling binary tables size.
    binary_module_handles: Option<u16>,
    binary_struct_handles: Option<u16>,
    binary_function_handles: Option<u16>,
    binary_function_instantiations: Option<u16>,
    binary_signatures: Option<u16>,
    binary_constant_pool: Option<u16>,
    binary_identifiers: Option<u16>,
    binary_address_identifiers: Option<u16>,
    binary_struct_defs: Option<u16>,
    binary_struct_def_instantiations: Option<u16>,
    binary_function_defs: Option<u16>,
    binary_field_handles: Option<u16>,
    binary_field_instantiations: Option<u16>,
    binary_friend_decls: Option<u16>,

    /// Maximum size of the `contents` part of an object, in bytes. Enforced by the Sui adapter when effects are produced.
    max_move_object_size: Option<u64>,

    // TODO: Option<increase to 500 KB. currently, publishing a package > 500 KB exceeds the max computation gas cost
    /// Maximum size of a Move package object, in bytes. Enforced by the Sui adapter at the end of a publish transaction.
    max_move_package_size: Option<u64>,

    /// Max number of publish or upgrade commands allowed in a programmable transaction block.
    max_publish_or_upgrade_per_ptb: Option<u64>,

    /// Maximum number of gas units that a single MoveCall transaction can use. Enforced by the Sui adapter.
    max_tx_gas: Option<u64>,

    /// Maximum amount of the proposed gas price in MIST (defined in the transaction).
    max_gas_price: Option<u64>,

    /// The max computation bucket for gas. This is the max that can be charged for computation.
    max_gas_computation_bucket: Option<u64>,

    // Define the value used to round up computation gas charges
    gas_rounding_step: Option<u64>,

    /// Maximum number of nested loops. Enforced by the Move bytecode verifier.
    max_loop_depth: Option<u64>,

    /// Maximum number of type arguments that can be bound to generic type parameters. Enforced by the Move bytecode verifier.
    max_generic_instantiation_length: Option<u64>,

    /// Maximum number of parameters that a Move function can have. Enforced by the Move bytecode verifier.
    max_function_parameters: Option<u64>,

    /// Maximum number of basic blocks that a Move function can have. Enforced by the Move bytecode verifier.
    max_basic_blocks: Option<u64>,

    /// Maximum stack size value. Enforced by the Move bytecode verifier.
    max_value_stack_size: Option<u64>,

    /// Maximum number of "type nodes", a metric for how big a SignatureToken will be when expanded into a fully qualified type. Enforced by the Move bytecode verifier.
    max_type_nodes: Option<u64>,

    /// Maximum number of push instructions in one function. Enforced by the Move bytecode verifier.
    max_push_size: Option<u64>,

    /// Maximum number of struct definitions in a module. Enforced by the Move bytecode verifier.
    max_struct_definitions: Option<u64>,

    /// Maximum number of function definitions in a module. Enforced by the Move bytecode verifier.
    max_function_definitions: Option<u64>,

    /// Maximum number of fields allowed in a struct definition. Enforced by the Move bytecode verifier.
    max_fields_in_struct: Option<u64>,

    /// Maximum dependency depth. Enforced by the Move linker when loading dependent modules.
    max_dependency_depth: Option<u64>,

    /// Maximum number of Move events that a single transaction can emit. Enforced by the VM during execution.
    max_num_event_emit: Option<u64>,

    /// Maximum number of new IDs that a single transaction can create. Enforced by the VM during execution.
    max_num_new_move_object_ids: Option<u64>,

    /// Maximum number of new IDs that a single system transaction can create. Enforced by the VM during execution.
    max_num_new_move_object_ids_system_tx: Option<u64>,

    /// Maximum number of IDs that a single transaction can delete. Enforced by the VM during execution.
    max_num_deleted_move_object_ids: Option<u64>,

    /// Maximum number of IDs that a single system transaction can delete. Enforced by the VM during execution.
    max_num_deleted_move_object_ids_system_tx: Option<u64>,

    /// Maximum number of IDs that a single transaction can transfer. Enforced by the VM during execution.
    max_num_transferred_move_object_ids: Option<u64>,

    /// Maximum number of IDs that a single system transaction can transfer. Enforced by the VM during execution.
    max_num_transferred_move_object_ids_system_tx: Option<u64>,

    /// Maximum size of a Move user event. Enforced by the VM during execution.
    max_event_emit_size: Option<u64>,

    /// Maximum size of a Move user event. Enforced by the VM during execution.
    max_event_emit_size_total: Option<u64>,

    /// Maximum length of a vector in Move. Enforced by the VM during execution, and for constants, by the verifier.
    max_move_vector_len: Option<u64>,

    /// Maximum length of an `Identifier` in Move. Enforced by the bytecode verifier at signing.
    max_move_identifier_len: Option<u64>,

    /// Maximum depth of a Move value within the VM.
    max_move_value_depth: Option<u64>,

    /// Maximum number of back edges in Move function. Enforced by the bytecode verifier at signing.
    max_back_edges_per_function: Option<u64>,

    /// Maximum number of back edges in Move module. Enforced by the bytecode verifier at signing.
    max_back_edges_per_module: Option<u64>,

    /// Maximum number of meter `ticks` spent verifying a Move function. Enforced by the bytecode verifier at signing.
    max_verifier_meter_ticks_per_function: Option<u64>,

    /// Maximum number of meter `ticks` spent verifying a Move module. Enforced by the bytecode verifier at signing.
    max_meter_ticks_per_module: Option<u64>,

    /// Maximum number of meter `ticks` spent verifying a Move package. Enforced by the bytecode verifier at signing.
    max_meter_ticks_per_package: Option<u64>,

    // === Object runtime internal operation limits ====
    // These affect dynamic fields
    /// Maximum number of cached objects in the object runtime ObjectStore. Enforced by object runtime during execution
    object_runtime_max_num_cached_objects: Option<u64>,

    /// Maximum number of cached objects in the object runtime ObjectStore in system transaction. Enforced by object runtime during execution
    object_runtime_max_num_cached_objects_system_tx: Option<u64>,

    /// Maximum number of stored objects accessed by object runtime ObjectStore. Enforced by object runtime during execution
    object_runtime_max_num_store_entries: Option<u64>,

    /// Maximum number of stored objects accessed by object runtime ObjectStore in system transaction. Enforced by object runtime during execution
    object_runtime_max_num_store_entries_system_tx: Option<u64>,

    // === Execution gas costs ====
    /// Base cost for any Sui transaction
    base_tx_cost_fixed: Option<u64>,

    /// Additional cost for a transaction that publishes a package
    /// i.e., the base cost of such a transaction is base_tx_cost_fixed + package_publish_cost_fixed
    package_publish_cost_fixed: Option<u64>,

    /// Cost per byte of a Move call transaction
    /// i.e., the cost of such a transaction is base_cost + (base_tx_cost_per_byte * size)
    base_tx_cost_per_byte: Option<u64>,

    /// Cost per byte for a transaction that publishes a package
    package_publish_cost_per_byte: Option<u64>,

    // Per-byte cost of reading an object during transaction execution
    obj_access_cost_read_per_byte: Option<u64>,

    // Per-byte cost of writing an object during transaction execution
    obj_access_cost_mutate_per_byte: Option<u64>,

    // Per-byte cost of deleting an object during transaction execution
    obj_access_cost_delete_per_byte: Option<u64>,

    /// Per-byte cost charged for each input object to a transaction.
    /// Meant to approximate the cost of checking locks for each object
    // TODO: Option<I'm not sure that this cost makes sense. Checking locks is "free"
    // in the sense that an invalid tx that can never be committed/pay gas can
    // force validators to check an arbitrary number of locks. If those checks are
    // "free" for invalid transactions, why charge for them in valid transactions
    // TODO: Option<if we keep this, I think we probably want it to be a fixed cost rather
    // than a per-byte cost. checking an object lock should not require loading an
    // entire object, just consulting an ID -> tx digest map
    obj_access_cost_verify_per_byte: Option<u64>,

    /// === Gas version. gas model ===

    /// Gas model version, what code we are using to charge gas
    gas_model_version: Option<u64>,

    /// === Storage gas costs ===

    /// Per-byte cost of storing an object in the Sui global object store. Some of this cost may be refundable if the object is later freed
    obj_data_cost_refundable: Option<u64>,

    // Per-byte cost of storing an object in the Sui transaction log (e.g., in CertifiedTransactionEffects)
    // This depends on the size of various fields including the effects
    // TODO: Option<I don't fully understand this^ and more details would be useful
    obj_metadata_cost_non_refundable: Option<u64>,

    /// === Tokenomics ===

    // TODO: Option<this should be changed to u64.
    /// Sender of a txn that touches an object will get this percent of the storage rebate back.
    /// In basis point.
    storage_rebate_rate: Option<u64>,

    /// 5% of the storage fund's share of rewards are reinvested into the storage fund.
    /// In basis point.
    storage_fund_reinvest_rate: Option<u64>,

    /// The share of rewards that will be slashed and redistributed is 50%.
    /// In basis point.
    reward_slashing_rate: Option<u64>,

    /// Unit gas price, Mist per internal gas unit.
    storage_gas_price: Option<u64>,

    /// === Core Protocol ===

    /// Max number of transactions per checkpoint.
    /// Note that this is a protocol constant and not a config as validators must have this set to
    /// the same value, otherwise they *will* fork.
    max_transactions_per_checkpoint: Option<u64>,

    /// Max size of a checkpoint in bytes.
    /// Note that this is a protocol constant and not a config as validators must have this set to
    /// the same value, otherwise they *will* fork.
    max_checkpoint_size_bytes: Option<u64>,

    /// A protocol upgrade always requires 2f+1 stake to agree. We support a buffer of additional
    /// stake (as a fraction of f, expressed in basis points) that is required before an upgrade
    /// can happen automatically. 10000bps would indicate that complete unanimity is required (all
    /// 3f+1 must vote), while 0bps would indicate that 2f+1 is sufficient.
    buffer_stake_for_protocol_upgrade_bps: Option<u64>,

    // === Native Function Costs ===

    // `address` module
    // Cost params for the Move native function `address::from_bytes(bytes: vector<u8>)`
    address_from_bytes_cost_base: Option<u64>,
    // Cost params for the Move native function `address::to_u256(address): u256`
    address_to_u256_cost_base: Option<u64>,
    // Cost params for the Move native function `address::from_u256(u256): address`
    address_from_u256_cost_base: Option<u64>,

    // `dynamic_field` module
    // Cost params for the Move native function `hash_type_and_key<K: copy + drop + store>(parent: address, k: K): address`
    dynamic_field_hash_type_and_key_cost_base: Option<u64>,
    dynamic_field_hash_type_and_key_type_cost_per_byte: Option<u64>,
    dynamic_field_hash_type_and_key_value_cost_per_byte: Option<u64>,
    dynamic_field_hash_type_and_key_type_tag_cost_per_byte: Option<u64>,
    // Cost params for the Move native function `add_child_object<Child: key>(parent: address, child: Child)`
    dynamic_field_add_child_object_cost_base: Option<u64>,
    dynamic_field_add_child_object_type_cost_per_byte: Option<u64>,
    dynamic_field_add_child_object_value_cost_per_byte: Option<u64>,
    dynamic_field_add_child_object_struct_tag_cost_per_byte: Option<u64>,
    // Cost params for the Move native function `borrow_child_object_mut<Child: key>(parent: &mut UID, id: address): &mut Child`
    dynamic_field_borrow_child_object_cost_base: Option<u64>,
    dynamic_field_borrow_child_object_child_ref_cost_per_byte: Option<u64>,
    dynamic_field_borrow_child_object_type_cost_per_byte: Option<u64>,
    // Cost params for the Move native function `remove_child_object<Child: key>(parent: address, id: address): Child`
    dynamic_field_remove_child_object_cost_base: Option<u64>,
    dynamic_field_remove_child_object_child_cost_per_byte: Option<u64>,
    dynamic_field_remove_child_object_type_cost_per_byte: Option<u64>,
    // Cost params for the Move native function `has_child_object(parent: address, id: address): bool`
    dynamic_field_has_child_object_cost_base: Option<u64>,
    // Cost params for the Move native function `has_child_object_with_ty<Child: key>(parent: address, id: address): bool`
    dynamic_field_has_child_object_with_ty_cost_base: Option<u64>,
    dynamic_field_has_child_object_with_ty_type_cost_per_byte: Option<u64>,
    dynamic_field_has_child_object_with_ty_type_tag_cost_per_byte: Option<u64>,

    // `event` module
    // Cost params for the Move native function `event::emit<T: copy + drop>(event: T)`
    event_emit_cost_base: Option<u64>,
    event_emit_value_size_derivation_cost_per_byte: Option<u64>,
    event_emit_tag_size_derivation_cost_per_byte: Option<u64>,
    event_emit_output_cost_per_byte: Option<u64>,

    //  `object` module
    // Cost params for the Move native function `borrow_uid<T: key>(obj: &T): &UID`
    object_borrow_uid_cost_base: Option<u64>,
    // Cost params for the Move native function `delete_impl(id: address)`
    object_delete_impl_cost_base: Option<u64>,
    // Cost params for the Move native function `record_new_uid(id: address)`
    object_record_new_uid_cost_base: Option<u64>,

    // Transfer
    // Cost params for the Move native function `transfer_impl<T: key>(obj: T, recipient: address)`
    transfer_transfer_internal_cost_base: Option<u64>,
    // Cost params for the Move native function `freeze_object<T: key>(obj: T)`
    transfer_freeze_object_cost_base: Option<u64>,
    // Cost params for the Move native function `share_object<T: key>(obj: T)`
    transfer_share_object_cost_base: Option<u64>,
    // Cost params for the Move native function
    // `receive_object<T: key>(p: &mut UID, recv: Receiving<T>T)`
    transfer_receive_object_cost_base: Option<u64>,

    // TxContext
    // Cost params for the Move native function `transfer_impl<T: key>(obj: T, recipient: address)`
    tx_context_derive_id_cost_base: Option<u64>,

    // Types
    // Cost params for the Move native function `is_one_time_witness<T: drop>(_: &T): bool`
    types_is_one_time_witness_cost_base: Option<u64>,
    types_is_one_time_witness_type_tag_cost_per_byte: Option<u64>,
    types_is_one_time_witness_type_cost_per_byte: Option<u64>,

    // Validator
    // Cost params for the Move native function `validate_metadata_bcs(metadata: vector<u8>)`
    validator_validate_metadata_cost_base: Option<u64>,
    validator_validate_metadata_data_cost_per_byte: Option<u64>,

    // Crypto natives
    crypto_invalid_arguments_cost: Option<u64>,
    // bls12381::bls12381_min_sig_verify
    bls12381_bls12381_min_sig_verify_cost_base: Option<u64>,
    bls12381_bls12381_min_sig_verify_msg_cost_per_byte: Option<u64>,
    bls12381_bls12381_min_sig_verify_msg_cost_per_block: Option<u64>,

    // bls12381::bls12381_min_pk_verify
    bls12381_bls12381_min_pk_verify_cost_base: Option<u64>,
    bls12381_bls12381_min_pk_verify_msg_cost_per_byte: Option<u64>,
    bls12381_bls12381_min_pk_verify_msg_cost_per_block: Option<u64>,

    // ecdsa_k1::ecrecover
    ecdsa_k1_ecrecover_keccak256_cost_base: Option<u64>,
    ecdsa_k1_ecrecover_keccak256_msg_cost_per_byte: Option<u64>,
    ecdsa_k1_ecrecover_keccak256_msg_cost_per_block: Option<u64>,
    ecdsa_k1_ecrecover_sha256_cost_base: Option<u64>,
    ecdsa_k1_ecrecover_sha256_msg_cost_per_byte: Option<u64>,
    ecdsa_k1_ecrecover_sha256_msg_cost_per_block: Option<u64>,

    // ecdsa_k1::decompress_pubkey
    ecdsa_k1_decompress_pubkey_cost_base: Option<u64>,

    // ecdsa_k1::secp256k1_verify
    ecdsa_k1_secp256k1_verify_keccak256_cost_base: Option<u64>,
    ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_byte: Option<u64>,
    ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_block: Option<u64>,
    ecdsa_k1_secp256k1_verify_sha256_cost_base: Option<u64>,
    ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_byte: Option<u64>,
    ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_block: Option<u64>,

    // ecdsa_r1::ecrecover
    ecdsa_r1_ecrecover_keccak256_cost_base: Option<u64>,
    ecdsa_r1_ecrecover_keccak256_msg_cost_per_byte: Option<u64>,
    ecdsa_r1_ecrecover_keccak256_msg_cost_per_block: Option<u64>,
    ecdsa_r1_ecrecover_sha256_cost_base: Option<u64>,
    ecdsa_r1_ecrecover_sha256_msg_cost_per_byte: Option<u64>,
    ecdsa_r1_ecrecover_sha256_msg_cost_per_block: Option<u64>,

    // ecdsa_r1::secp256k1_verify
    ecdsa_r1_secp256r1_verify_keccak256_cost_base: Option<u64>,
    ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_byte: Option<u64>,
    ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_block: Option<u64>,
    ecdsa_r1_secp256r1_verify_sha256_cost_base: Option<u64>,
    ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_byte: Option<u64>,
    ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_block: Option<u64>,

    // ecvrf::verify
    ecvrf_ecvrf_verify_cost_base: Option<u64>,
    ecvrf_ecvrf_verify_alpha_string_cost_per_byte: Option<u64>,
    ecvrf_ecvrf_verify_alpha_string_cost_per_block: Option<u64>,

    // ed25519
    ed25519_ed25519_verify_cost_base: Option<u64>,
    ed25519_ed25519_verify_msg_cost_per_byte: Option<u64>,
    ed25519_ed25519_verify_msg_cost_per_block: Option<u64>,

    // groth16::prepare_verifying_key
    groth16_prepare_verifying_key_bls12381_cost_base: Option<u64>,
    groth16_prepare_verifying_key_bn254_cost_base: Option<u64>,

    // groth16::verify_groth16_proof_internal
    groth16_verify_groth16_proof_internal_bls12381_cost_base: Option<u64>,
    groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input: Option<u64>,
    groth16_verify_groth16_proof_internal_bn254_cost_base: Option<u64>,
    groth16_verify_groth16_proof_internal_bn254_cost_per_public_input: Option<u64>,
    groth16_verify_groth16_proof_internal_public_input_cost_per_byte: Option<u64>,

    // hash::blake2b256
    hash_blake2b256_cost_base: Option<u64>,
    hash_blake2b256_data_cost_per_byte: Option<u64>,
    hash_blake2b256_data_cost_per_block: Option<u64>,

    // hash::keccak256
    hash_keccak256_cost_base: Option<u64>,
    hash_keccak256_data_cost_per_byte: Option<u64>,
    hash_keccak256_data_cost_per_block: Option<u64>,

    // poseidon::poseidon_bn254
    poseidon_bn254_cost_base: Option<u64>,
    poseidon_bn254_cost_per_block: Option<u64>,

    // group_ops
    group_ops_bls12381_decode_scalar_cost: Option<u64>,
    group_ops_bls12381_decode_g1_cost: Option<u64>,
    group_ops_bls12381_decode_g2_cost: Option<u64>,
    group_ops_bls12381_decode_gt_cost: Option<u64>,
    group_ops_bls12381_scalar_add_cost: Option<u64>,
    group_ops_bls12381_g1_add_cost: Option<u64>,
    group_ops_bls12381_g2_add_cost: Option<u64>,
    group_ops_bls12381_gt_add_cost: Option<u64>,
    group_ops_bls12381_scalar_sub_cost: Option<u64>,
    group_ops_bls12381_g1_sub_cost: Option<u64>,
    group_ops_bls12381_g2_sub_cost: Option<u64>,
    group_ops_bls12381_gt_sub_cost: Option<u64>,
    group_ops_bls12381_scalar_mul_cost: Option<u64>,
    group_ops_bls12381_g1_mul_cost: Option<u64>,
    group_ops_bls12381_g2_mul_cost: Option<u64>,
    group_ops_bls12381_gt_mul_cost: Option<u64>,
    group_ops_bls12381_scalar_div_cost: Option<u64>,
    group_ops_bls12381_g1_div_cost: Option<u64>,
    group_ops_bls12381_g2_div_cost: Option<u64>,
    group_ops_bls12381_gt_div_cost: Option<u64>,
    group_ops_bls12381_g1_hash_to_base_cost: Option<u64>,
    group_ops_bls12381_g2_hash_to_base_cost: Option<u64>,
    group_ops_bls12381_g1_hash_to_cost_per_byte: Option<u64>,
    group_ops_bls12381_g2_hash_to_cost_per_byte: Option<u64>,
    group_ops_bls12381_g1_msm_base_cost: Option<u64>,
    group_ops_bls12381_g2_msm_base_cost: Option<u64>,
    group_ops_bls12381_g1_msm_base_cost_per_input: Option<u64>,
    group_ops_bls12381_g2_msm_base_cost_per_input: Option<u64>,
    group_ops_bls12381_msm_max_len: Option<u32>,
    group_ops_bls12381_pairing_cost: Option<u64>,

    // hmac::hmac_sha3_256
    hmac_hmac_sha3_256_cost_base: Option<u64>,
    hmac_hmac_sha3_256_input_cost_per_byte: Option<u64>,
    hmac_hmac_sha3_256_input_cost_per_block: Option<u64>,

    // zklogin::check_zklogin_id
    check_zklogin_id_cost_base: Option<u64>,
    // zklogin::check_zklogin_issuer
    check_zklogin_issuer_cost_base: Option<u64>,

    // Const params for consensus scoring decision
    // The scaling factor property for the MED outlier detection
    scoring_decision_mad_divisor: Option<f64>,
    // The cutoff value for the MED outlier detection
    scoring_decision_cutoff_value: Option<f64>,

    /// === Execution Version ===
    execution_version: Option<u64>,

    // Dictates the threshold (percentage of stake) that is used to calculate the "bad" nodes to be
    // swapped when creating the consensus schedule. The values should be of the range [0 - 33]. Anything
    // above 33 (f) will not be allowed.
    consensus_bad_nodes_stake_threshold: Option<u64>,

    max_jwk_votes_per_validator_per_epoch: Option<u64>,
    // The maximum age of a JWK in epochs before it is removed from the AuthenticatorState object.
    // Applied at the end of an epoch as a delta from the new epoch value, so setting this to 1
    // will cause the new epoch to start with JWKs from the previous epoch still valid.
    max_age_of_jwk_in_epochs: Option<u64>,

    /// === random beacon ===

    /// Maximum allowed precision loss when reducing voting weights for the random beacon
    /// protocol.
    random_beacon_reduction_allowed_delta: Option<u16>,

    /// Minimum number of shares below which voting weights will not be reduced for the
    /// random beacon protocol.
    random_beacon_reduction_lower_bound: Option<u32>,

    /// Consensus Round after which DKG should be aborted and randomness disabled for
    /// the epoch, if it hasn't already completed.
    random_beacon_dkg_timeout_round: Option<u32>,

    /// Minimum interval between consecutive rounds of generated randomness.
    random_beacon_min_round_interval_ms: Option<u64>,

    /// The maximum serialised transaction size (in bytes) accepted by consensus. That should be bigger than the
    /// `max_tx_size_bytes` with some additional headroom.
    consensus_max_transaction_size_bytes: Option<u64>,
    /// The maximum size of transactions included in a consensus proposed block
    consensus_max_transactions_in_block_bytes: Option<u64>,

    // The max accumulated txn execution cost per object in a checkpoint. Transactions
    // in a checkpoint will be deferred once their touch shared objects hit this limit.
    max_accumulated_txn_cost_per_object_in_checkpoint: Option<u64>,
}

// feature flags
impl ProtocolConfig {
    // Add checks for feature flag support here, e.g.:
    // pub fn check_new_protocol_feature_supported(&self) -> Result<(), Error> {
    //     if self.feature_flags.new_protocol_feature_supported {
    //         Ok(())
    //     } else {
    //         Err(Error(format!(
    //             "new_protocol_feature is not supported at {:?}",
    //             self.version
    //         )))
    //     }
    // }

    pub fn check_package_upgrades_supported(&self) -> Result<(), Error> {
        if self.feature_flags.package_upgrades {
            Ok(())
        } else {
            Err(Error(format!(
                "package upgrades are not supported at {:?}",
                self.version
            )))
        }
    }

    pub fn allow_receiving_object_id(&self) -> bool {
        self.feature_flags.allow_receiving_object_id
    }

    pub fn receiving_objects_supported(&self) -> bool {
        self.feature_flags.receive_objects
    }

    pub fn package_upgrades_supported(&self) -> bool {
        self.feature_flags.package_upgrades
    }

    pub fn check_commit_root_state_digest_supported(&self) -> bool {
        self.feature_flags.commit_root_state_digest
    }

    pub fn get_advance_epoch_start_time_in_safe_mode(&self) -> bool {
        self.feature_flags.advance_epoch_start_time_in_safe_mode
    }

    pub fn loaded_child_objects_fixed(&self) -> bool {
        self.feature_flags.loaded_child_objects_fixed
    }

    pub fn missing_type_is_compatibility_error(&self) -> bool {
        self.feature_flags.missing_type_is_compatibility_error
    }

    pub fn scoring_decision_with_validity_cutoff(&self) -> bool {
        self.feature_flags.scoring_decision_with_validity_cutoff
    }

    pub fn narwhal_versioned_metadata(&self) -> bool {
        self.feature_flags.narwhal_versioned_metadata
    }

    pub fn consensus_order_end_of_epoch_last(&self) -> bool {
        self.feature_flags.consensus_order_end_of_epoch_last
    }

    pub fn disallow_adding_abilities_on_upgrade(&self) -> bool {
        self.feature_flags.disallow_adding_abilities_on_upgrade
    }

    pub fn disable_invariant_violation_check_in_swap_loc(&self) -> bool {
        self.feature_flags
            .disable_invariant_violation_check_in_swap_loc
    }

    pub fn advance_to_highest_supported_protocol_version(&self) -> bool {
        self.feature_flags
            .advance_to_highest_supported_protocol_version
    }

    pub fn ban_entry_init(&self) -> bool {
        self.feature_flags.ban_entry_init
    }

    pub fn package_digest_hash_module(&self) -> bool {
        self.feature_flags.package_digest_hash_module
    }

    pub fn disallow_change_struct_type_params_on_upgrade(&self) -> bool {
        self.feature_flags
            .disallow_change_struct_type_params_on_upgrade
    }

    pub fn no_extraneous_module_bytes(&self) -> bool {
        self.feature_flags.no_extraneous_module_bytes
    }

    pub fn zklogin_auth(&self) -> bool {
        self.feature_flags.zklogin_auth
    }

    pub fn zklogin_supported_providers(&self) -> &BTreeSet<String> {
        &self.feature_flags.zklogin_supported_providers
    }

    pub fn consensus_transaction_ordering(&self) -> ConsensusTransactionOrdering {
        self.feature_flags.consensus_transaction_ordering
    }

    pub fn simplified_unwrap_then_delete(&self) -> bool {
        self.feature_flags.simplified_unwrap_then_delete
    }

    pub fn supports_upgraded_multisig(&self) -> bool {
        self.feature_flags.upgraded_multisig_supported
    }

    pub fn txn_base_cost_as_multiplier(&self) -> bool {
        self.feature_flags.txn_base_cost_as_multiplier
    }

    pub fn shared_object_deletion(&self) -> bool {
        self.feature_flags.shared_object_deletion
    }

    pub fn narwhal_new_leader_election_schedule(&self) -> bool {
        self.feature_flags.narwhal_new_leader_election_schedule
    }

    pub fn loaded_child_object_format(&self) -> bool {
        self.feature_flags.loaded_child_object_format
    }

    pub fn enable_jwk_consensus_updates(&self) -> bool {
        let ret = self.feature_flags.enable_jwk_consensus_updates;
        if ret {
            // jwk updates required end-of-epoch transactions
            assert!(self.feature_flags.end_of_epoch_transaction_supported);
        }
        ret
    }

    pub fn simple_conservation_checks(&self) -> bool {
        self.feature_flags.simple_conservation_checks
    }

    pub fn loaded_child_object_format_type(&self) -> bool {
        self.feature_flags.loaded_child_object_format_type
    }

    pub fn end_of_epoch_transaction_supported(&self) -> bool {
        let ret = self.feature_flags.end_of_epoch_transaction_supported;
        if !ret {
            // jwk updates required end-of-epoch transactions
            assert!(!self.feature_flags.enable_jwk_consensus_updates);
        }
        ret
    }

    pub fn recompute_has_public_transfer_in_execution(&self) -> bool {
        self.feature_flags
            .recompute_has_public_transfer_in_execution
    }

    // this function only exists for readability in the genesis code.
    pub fn create_authenticator_state_in_genesis(&self) -> bool {
        self.enable_jwk_consensus_updates()
    }

    pub fn random_beacon(&self) -> bool {
        self.feature_flags.random_beacon
    }

    pub fn enable_effects_v2(&self) -> bool {
        self.feature_flags.enable_effects_v2
    }

    pub fn narwhal_certificate_v2(&self) -> bool {
        self.feature_flags.narwhal_certificate_v2
    }

    pub fn verify_legacy_zklogin_address(&self) -> bool {
        self.feature_flags.verify_legacy_zklogin_address
    }

    pub fn accept_zklogin_in_multisig(&self) -> bool {
        self.feature_flags.accept_zklogin_in_multisig
    }

    pub fn zklogin_max_epoch_upper_bound_delta(&self) -> Option<u64> {
        self.feature_flags.zklogin_max_epoch_upper_bound_delta
    }

    pub fn throughput_aware_consensus_submission(&self) -> bool {
        self.feature_flags.throughput_aware_consensus_submission
    }

    pub fn include_consensus_digest_in_prologue(&self) -> bool {
        self.feature_flags.include_consensus_digest_in_prologue
    }

    pub fn hardened_otw_check(&self) -> bool {
        self.feature_flags.hardened_otw_check
    }

    pub fn enable_poseidon(&self) -> bool {
        self.feature_flags.enable_poseidon
    }

    pub fn enable_coin_deny_list(&self) -> bool {
        self.feature_flags.enable_coin_deny_list
    }

    pub fn enable_group_ops_native_functions(&self) -> bool {
        self.feature_flags.enable_group_ops_native_functions
    }

    pub fn enable_group_ops_native_function_msm(&self) -> bool {
        self.feature_flags.enable_group_ops_native_function_msm
    }

    pub fn reject_mutable_random_on_entry_functions(&self) -> bool {
        self.feature_flags.reject_mutable_random_on_entry_functions
    }

    pub fn per_object_congestion_control_mode(&self) -> PerObjectCongestionControlMode {
        self.feature_flags.per_object_congestion_control_mode
    }

    pub fn consensus_choice(&self) -> ConsensusChoice {
        self.feature_flags.consensus_choice
    }

    pub fn consensus_network(&self) -> ConsensusNetwork {
        self.feature_flags.consensus_network
    }
}

#[cfg(not(msim))]
static POISON_VERSION_METHODS: AtomicBool = AtomicBool::new(false);

// Use a thread local in sim tests for test isolation.
#[cfg(msim)]
thread_local! {
    static POISON_VERSION_METHODS: AtomicBool = AtomicBool::new(false);
}

// Instantiations for each protocol version.
impl ProtocolConfig {
    /// Get the value ProtocolConfig that are in effect during the given protocol version.
    pub fn get_for_version(version: ProtocolVersion, chain: Chain) -> Self {
        // ProtocolVersion can be deserialized so we need to check it here as well.
        assert!(
            version >= ProtocolVersion::MIN,
            "Network protocol version is {:?}, but the minimum supported version by the binary is {:?}. Please upgrade the binary.",
            version,
            ProtocolVersion::MIN.0,
        );
        assert!(
            version <= ProtocolVersion::MAX_ALLOWED,
            "Network protocol version is {:?}, but the maximum supported version by the binary is {:?}. Please upgrade the binary.",
            version,
            ProtocolVersion::MAX_ALLOWED.0,
        );

        let mut ret = Self::get_for_version_impl(version, chain);
        ret.version = version;

        CONFIG_OVERRIDE.with(|ovr| {
            if let Some(override_fn) = &*ovr.borrow() {
                warn!(
                    "overriding ProtocolConfig settings with custom settings (you should not see this log outside of tests)"
                );
                override_fn(version, ret)
            } else {
                ret
            }
        })
    }

    /// Get the value ProtocolConfig that are in effect during the given protocol version.
    /// Or none if the version is not supported.
    pub fn get_for_version_if_supported(version: ProtocolVersion, chain: Chain) -> Option<Self> {
        if version.0 >= ProtocolVersion::MIN.0 && version.0 <= ProtocolVersion::MAX_ALLOWED.0 {
            let mut ret = Self::get_for_version_impl(version, chain);
            ret.version = version;
            Some(ret)
        } else {
            None
        }
    }

    #[cfg(not(msim))]
    pub fn poison_get_for_min_version() {
        POISON_VERSION_METHODS.store(true, Ordering::Relaxed);
    }

    #[cfg(not(msim))]
    fn load_poison_get_for_min_version() -> bool {
        POISON_VERSION_METHODS.load(Ordering::Relaxed)
    }

    #[cfg(msim)]
    pub fn poison_get_for_min_version() {
        POISON_VERSION_METHODS.with(|p| p.store(true, Ordering::Relaxed));
    }

    #[cfg(msim)]
    fn load_poison_get_for_min_version() -> bool {
        POISON_VERSION_METHODS.with(|p| p.load(Ordering::Relaxed))
    }

    /// Convenience to get the constants at the current minimum supported version.
    /// Mainly used by client code that may not yet be protocol-version aware.
    pub fn get_for_min_version() -> Self {
        if Self::load_poison_get_for_min_version() {
            panic!("get_for_min_version called on validator");
        }
        ProtocolConfig::get_for_version(ProtocolVersion::MIN, Chain::Unknown)
    }

    /// CAREFUL! - You probably want to use `get_for_version` instead.
    ///
    /// Convenience to get the constants at the current maximum supported version.
    /// Mainly used by genesis. Note well that this function uses the max version
    /// supported locally by the node, which is not necessarily the current version
    /// of the network. ALSO, this function disregards chain specific config (by
    /// using Chain::Unknown), thereby potentially returning a protocol config that
    /// is incorrect for some feature flags. Definitely safe for testing and for
    /// protocol version 11 and prior.
    #[allow(non_snake_case)]
    pub fn get_for_max_version_UNSAFE() -> Self {
        if Self::load_poison_get_for_min_version() {
            panic!("get_for_max_version_UNSAFE called on validator");
        }
        ProtocolConfig::get_for_version(ProtocolVersion::MAX, Chain::Unknown)
    }

    fn get_for_version_impl(version: ProtocolVersion, chain: Chain) -> Self {
        #[cfg(msim)]
        {
            // populate the fake simulator version # with a different base tx cost.
            if version == ProtocolVersion::MAX_ALLOWED {
                let mut config = Self::get_for_version_impl(version - 1, Chain::Unknown);
                config.base_tx_cost_fixed = Some(config.base_tx_cost_fixed() + 1000);
                return config;
            }
        }

        // IMPORTANT: Never modify the value of any constant for a pre-existing protocol version.
        // To change the values here you must create a new protocol version with the new values!
        let mut cfg = Self {
            // will be overwritten before being returned
            version,

            // All flags are disabled in V1
            feature_flags: Default::default(),

            max_tx_size_bytes: Some(128 * 1024),
            // We need this number to be at least 100x less than `max_serialized_tx_effects_size_bytes`otherwise effects can be huge
            max_input_objects: Some(2048),
            max_serialized_tx_effects_size_bytes: Some(512 * 1024),
            max_serialized_tx_effects_size_bytes_system_tx: Some(512 * 1024 * 16),
            max_gas_payment_objects: Some(256),
            max_modules_in_publish: Some(128),
            max_package_dependencies: None,
            max_arguments: Some(512),
            max_type_arguments: Some(16),
            max_type_argument_depth: Some(16),
            max_pure_argument_size: Some(16 * 1024),
            max_programmable_tx_commands: Some(1024),
            move_binary_format_version: Some(6),
            binary_module_handles: None,
            binary_struct_handles: None,
            binary_function_handles: None,
            binary_function_instantiations: None,
            binary_signatures: None,
            binary_constant_pool: None,
            binary_identifiers: None,
            binary_address_identifiers: None,
            binary_struct_defs: None,
            binary_struct_def_instantiations: None,
            binary_function_defs: None,
            binary_field_handles: None,
            binary_field_instantiations: None,
            binary_friend_decls: None,
            max_move_object_size: Some(250 * 1024),
            max_move_package_size: Some(100 * 1024),
            max_publish_or_upgrade_per_ptb: None,
            max_tx_gas: Some(10_000_000_000),
            max_gas_price: Some(100_000),
            max_gas_computation_bucket: Some(5_000_000),
            max_loop_depth: Some(5),
            max_generic_instantiation_length: Some(32),
            max_function_parameters: Some(128),
            max_basic_blocks: Some(1024),
            max_value_stack_size: Some(1024),
            max_type_nodes: Some(256),
            max_push_size: Some(10000),
            max_struct_definitions: Some(200),
            max_function_definitions: Some(1000),
            max_fields_in_struct: Some(32),
            max_dependency_depth: Some(100),
            max_num_event_emit: Some(256),
            max_num_new_move_object_ids: Some(2048),
            max_num_new_move_object_ids_system_tx: Some(2048 * 16),
            max_num_deleted_move_object_ids: Some(2048),
            max_num_deleted_move_object_ids_system_tx: Some(2048 * 16),
            max_num_transferred_move_object_ids: Some(2048),
            max_num_transferred_move_object_ids_system_tx: Some(2048 * 16),
            max_event_emit_size: Some(250 * 1024),
            max_move_vector_len: Some(256 * 1024),

            max_back_edges_per_function: Some(10_000),
            max_back_edges_per_module: Some(10_000),
            max_verifier_meter_ticks_per_function: Some(6_000_000),
            max_meter_ticks_per_module: Some(6_000_000),
            max_meter_ticks_per_package: None,

            object_runtime_max_num_cached_objects: Some(1000),
            object_runtime_max_num_cached_objects_system_tx: Some(1000 * 16),
            object_runtime_max_num_store_entries: Some(1000),
            object_runtime_max_num_store_entries_system_tx: Some(1000 * 16),
            base_tx_cost_fixed: Some(110_000),
            package_publish_cost_fixed: Some(1_000),
            base_tx_cost_per_byte: Some(0),
            package_publish_cost_per_byte: Some(80),
            obj_access_cost_read_per_byte: Some(15),
            obj_access_cost_mutate_per_byte: Some(40),
            obj_access_cost_delete_per_byte: Some(40),
            obj_access_cost_verify_per_byte: Some(200),
            obj_data_cost_refundable: Some(100),
            obj_metadata_cost_non_refundable: Some(50),
            gas_model_version: Some(1),
            storage_rebate_rate: Some(9900),
            storage_fund_reinvest_rate: Some(500),
            reward_slashing_rate: Some(5000),
            storage_gas_price: Some(1),
            max_transactions_per_checkpoint: Some(10_000),
            max_checkpoint_size_bytes: Some(30 * 1024 * 1024),

            // For now, perform upgrades with a bare quorum of validators.
            // MUSTFIX: This number should be increased to at least 2000 (20%) for mainnet.
            buffer_stake_for_protocol_upgrade_bps: Some(0),

            // === Native Function Costs ===
            // `address` module
            // Cost params for the Move native function `address::from_bytes(bytes: vector<u8>)`
            address_from_bytes_cost_base: Some(52),
            // Cost params for the Move native function `address::to_u256(address): u256`
            address_to_u256_cost_base: Some(52),
            // Cost params for the Move native function `address::from_u256(u256): address`
            address_from_u256_cost_base: Some(52),

            // `dynamic_field` module
            // Cost params for the Move native function `hash_type_and_key<K: copy + drop + store>(parent: address, k: K): address`
            dynamic_field_hash_type_and_key_cost_base: Some(100),
            dynamic_field_hash_type_and_key_type_cost_per_byte: Some(2),
            dynamic_field_hash_type_and_key_value_cost_per_byte: Some(2),
            dynamic_field_hash_type_and_key_type_tag_cost_per_byte: Some(2),
            // Cost params for the Move native function `add_child_object<Child: key>(parent: address, child: Child)`
            dynamic_field_add_child_object_cost_base: Some(100),
            dynamic_field_add_child_object_type_cost_per_byte: Some(10),
            dynamic_field_add_child_object_value_cost_per_byte: Some(10),
            dynamic_field_add_child_object_struct_tag_cost_per_byte: Some(10),
            // Cost params for the Move native function `borrow_child_object_mut<Child: key>(parent: &mut UID, id: address): &mut Child`
            dynamic_field_borrow_child_object_cost_base: Some(100),
            dynamic_field_borrow_child_object_child_ref_cost_per_byte: Some(10),
            dynamic_field_borrow_child_object_type_cost_per_byte: Some(10),
            // Cost params for the Move native function `remove_child_object<Child: key>(parent: address, id: address): Child`
            dynamic_field_remove_child_object_cost_base: Some(100),
            dynamic_field_remove_child_object_child_cost_per_byte: Some(2),
            dynamic_field_remove_child_object_type_cost_per_byte: Some(2),
            // Cost params for the Move native function `has_child_object(parent: address, id: address): bool`
            dynamic_field_has_child_object_cost_base: Some(100),
            // Cost params for the Move native function `has_child_object_with_ty<Child: key>(parent: address, id: address): bool`
            dynamic_field_has_child_object_with_ty_cost_base: Some(100),
            dynamic_field_has_child_object_with_ty_type_cost_per_byte: Some(2),
            dynamic_field_has_child_object_with_ty_type_tag_cost_per_byte: Some(2),

            // `event` module
            // Cost params for the Move native function `event::emit<T: copy + drop>(event: T)`
            event_emit_cost_base: Some(52),
            event_emit_value_size_derivation_cost_per_byte: Some(2),
            event_emit_tag_size_derivation_cost_per_byte: Some(5),
            event_emit_output_cost_per_byte: Some(10),

            //  `object` module
            // Cost params for the Move native function `borrow_uid<T: key>(obj: &T): &UID`
            object_borrow_uid_cost_base: Some(52),
            // Cost params for the Move native function `delete_impl(id: address)`
            object_delete_impl_cost_base: Some(52),
            // Cost params for the Move native function `record_new_uid(id: address)`
            object_record_new_uid_cost_base: Some(52),

            // `transfer` module
            // Cost params for the Move native function `transfer_impl<T: key>(obj: T, recipient: address)`
            transfer_transfer_internal_cost_base: Some(52),
            // Cost params for the Move native function `freeze_object<T: key>(obj: T)`
            transfer_freeze_object_cost_base: Some(52),
            // Cost params for the Move native function `share_object<T: key>(obj: T)`
            transfer_share_object_cost_base: Some(52),
            transfer_receive_object_cost_base: None,

            // `tx_context` module
            // Cost params for the Move native function `transfer_impl<T: key>(obj: T, recipient: address)`
            tx_context_derive_id_cost_base: Some(52),

            // `types` module
            // Cost params for the Move native function `is_one_time_witness<T: drop>(_: &T): bool`
            types_is_one_time_witness_cost_base: Some(52),
            types_is_one_time_witness_type_tag_cost_per_byte: Some(2),
            types_is_one_time_witness_type_cost_per_byte: Some(2),

            // `validator` module
            // Cost params for the Move native function `validate_metadata_bcs(metadata: vector<u8>)`
            validator_validate_metadata_cost_base: Some(52),
            validator_validate_metadata_data_cost_per_byte: Some(2),

            // Crypto
            crypto_invalid_arguments_cost: Some(100),
            // bls12381::bls12381_min_pk_verify
            bls12381_bls12381_min_sig_verify_cost_base: Some(52),
            bls12381_bls12381_min_sig_verify_msg_cost_per_byte: Some(2),
            bls12381_bls12381_min_sig_verify_msg_cost_per_block: Some(2),

            // bls12381::bls12381_min_pk_verify
            bls12381_bls12381_min_pk_verify_cost_base: Some(52),
            bls12381_bls12381_min_pk_verify_msg_cost_per_byte: Some(2),
            bls12381_bls12381_min_pk_verify_msg_cost_per_block: Some(2),

            // ecdsa_k1::ecrecover
            ecdsa_k1_ecrecover_keccak256_cost_base: Some(52),
            ecdsa_k1_ecrecover_keccak256_msg_cost_per_byte: Some(2),
            ecdsa_k1_ecrecover_keccak256_msg_cost_per_block: Some(2),
            ecdsa_k1_ecrecover_sha256_cost_base: Some(52),
            ecdsa_k1_ecrecover_sha256_msg_cost_per_byte: Some(2),
            ecdsa_k1_ecrecover_sha256_msg_cost_per_block: Some(2),

            // ecdsa_k1::decompress_pubkey
            ecdsa_k1_decompress_pubkey_cost_base: Some(52),

            // ecdsa_k1::secp256k1_verify
            ecdsa_k1_secp256k1_verify_keccak256_cost_base: Some(52),
            ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_byte: Some(2),
            ecdsa_k1_secp256k1_verify_keccak256_msg_cost_per_block: Some(2),
            ecdsa_k1_secp256k1_verify_sha256_cost_base: Some(52),
            ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_byte: Some(2),
            ecdsa_k1_secp256k1_verify_sha256_msg_cost_per_block: Some(2),

            // ecdsa_r1::ecrecover
            ecdsa_r1_ecrecover_keccak256_cost_base: Some(52),
            ecdsa_r1_ecrecover_keccak256_msg_cost_per_byte: Some(2),
            ecdsa_r1_ecrecover_keccak256_msg_cost_per_block: Some(2),
            ecdsa_r1_ecrecover_sha256_cost_base: Some(52),
            ecdsa_r1_ecrecover_sha256_msg_cost_per_byte: Some(2),
            ecdsa_r1_ecrecover_sha256_msg_cost_per_block: Some(2),

            // ecdsa_r1::secp256k1_verify
            ecdsa_r1_secp256r1_verify_keccak256_cost_base: Some(52),
            ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_byte: Some(2),
            ecdsa_r1_secp256r1_verify_keccak256_msg_cost_per_block: Some(2),
            ecdsa_r1_secp256r1_verify_sha256_cost_base: Some(52),
            ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_byte: Some(2),
            ecdsa_r1_secp256r1_verify_sha256_msg_cost_per_block: Some(2),

            // ecvrf::verify
            ecvrf_ecvrf_verify_cost_base: Some(52),
            ecvrf_ecvrf_verify_alpha_string_cost_per_byte: Some(2),
            ecvrf_ecvrf_verify_alpha_string_cost_per_block: Some(2),

            // ed25519
            ed25519_ed25519_verify_cost_base: Some(52),
            ed25519_ed25519_verify_msg_cost_per_byte: Some(2),
            ed25519_ed25519_verify_msg_cost_per_block: Some(2),

            // groth16::prepare_verifying_key
            groth16_prepare_verifying_key_bls12381_cost_base: Some(52),
            groth16_prepare_verifying_key_bn254_cost_base: Some(52),

            // groth16::verify_groth16_proof_internal
            groth16_verify_groth16_proof_internal_bls12381_cost_base: Some(52),
            groth16_verify_groth16_proof_internal_bls12381_cost_per_public_input: Some(2),
            groth16_verify_groth16_proof_internal_bn254_cost_base: Some(52),
            groth16_verify_groth16_proof_internal_bn254_cost_per_public_input: Some(2),
            groth16_verify_groth16_proof_internal_public_input_cost_per_byte: Some(2),

            // hash::blake2b256
            hash_blake2b256_cost_base: Some(52),
            hash_blake2b256_data_cost_per_byte: Some(2),
            hash_blake2b256_data_cost_per_block: Some(2),
            // hash::keccak256
            hash_keccak256_cost_base: Some(52),
            hash_keccak256_data_cost_per_byte: Some(2),
            hash_keccak256_data_cost_per_block: Some(2),

            poseidon_bn254_cost_base: None,
            poseidon_bn254_cost_per_block: None,

            // hmac::hmac_sha3_256
            hmac_hmac_sha3_256_cost_base: Some(52),
            hmac_hmac_sha3_256_input_cost_per_byte: Some(2),
            hmac_hmac_sha3_256_input_cost_per_block: Some(2),

            // group ops
            group_ops_bls12381_decode_scalar_cost: None,
            group_ops_bls12381_decode_g1_cost: None,
            group_ops_bls12381_decode_g2_cost: None,
            group_ops_bls12381_decode_gt_cost: None,
            group_ops_bls12381_scalar_add_cost: None,
            group_ops_bls12381_g1_add_cost: None,
            group_ops_bls12381_g2_add_cost: None,
            group_ops_bls12381_gt_add_cost: None,
            group_ops_bls12381_scalar_sub_cost: None,
            group_ops_bls12381_g1_sub_cost: None,
            group_ops_bls12381_g2_sub_cost: None,
            group_ops_bls12381_gt_sub_cost: None,
            group_ops_bls12381_scalar_mul_cost: None,
            group_ops_bls12381_g1_mul_cost: None,
            group_ops_bls12381_g2_mul_cost: None,
            group_ops_bls12381_gt_mul_cost: None,
            group_ops_bls12381_scalar_div_cost: None,
            group_ops_bls12381_g1_div_cost: None,
            group_ops_bls12381_g2_div_cost: None,
            group_ops_bls12381_gt_div_cost: None,
            group_ops_bls12381_g1_hash_to_base_cost: None,
            group_ops_bls12381_g2_hash_to_base_cost: None,
            group_ops_bls12381_g1_hash_to_cost_per_byte: None,
            group_ops_bls12381_g2_hash_to_cost_per_byte: None,
            group_ops_bls12381_g1_msm_base_cost: None,
            group_ops_bls12381_g2_msm_base_cost: None,
            group_ops_bls12381_g1_msm_base_cost_per_input: None,
            group_ops_bls12381_g2_msm_base_cost_per_input: None,
            group_ops_bls12381_msm_max_len: None,
            group_ops_bls12381_pairing_cost: None,

            // zklogin::check_zklogin_id
            check_zklogin_id_cost_base: None,
            // zklogin::check_zklogin_issuer
            check_zklogin_issuer_cost_base: None,

            max_size_written_objects: None,
            max_size_written_objects_system_tx: None,

            // Const params for consensus scoring decision
            scoring_decision_mad_divisor: None,
            scoring_decision_cutoff_value: None,

            // Limits the length of a Move identifier
            max_move_identifier_len: None,
            max_move_value_depth: None,

            gas_rounding_step: None,

            execution_version: None,

            max_event_emit_size_total: None,

            consensus_bad_nodes_stake_threshold: None,

            max_jwk_votes_per_validator_per_epoch: None,

            max_age_of_jwk_in_epochs: None,

            random_beacon_reduction_allowed_delta: None,

            random_beacon_reduction_lower_bound: None,

            random_beacon_dkg_timeout_round: None,

            random_beacon_min_round_interval_ms: None,

            consensus_max_transaction_size_bytes: None,

            consensus_max_transactions_in_block_bytes: None,

            max_accumulated_txn_cost_per_object_in_checkpoint: None,
            // When adding a new constant, set it to None in the earliest version, like this:
            // new_constant: None,
        };
        for cur in 2..=version.0 {
            match cur {
                1 => unreachable!(),
                2 => {
                    cfg.feature_flags.advance_epoch_start_time_in_safe_mode = true;
                }
                3 => {
                    // changes for gas model
                    cfg.gas_model_version = Some(2);
                    // max gas budget is in MIST and an absolute value 50SUI
                    cfg.max_tx_gas = Some(50_000_000_000);
                    // min gas budget is in MIST and an absolute value 2000MIST or 0.000002SUI
                    cfg.base_tx_cost_fixed = Some(2_000);
                    // storage gas price multiplier
                    cfg.storage_gas_price = Some(76);
                    cfg.feature_flags.loaded_child_objects_fixed = true;
                    // max size of written objects during a TXn
                    // this is a sum of all objects written during a TXn
                    cfg.max_size_written_objects = Some(5 * 1000 * 1000);
                    // max size of written objects during a system TXn to allow for larger writes
                    // akin to `max_size_written_objects` but for system TXns
                    cfg.max_size_written_objects_system_tx = Some(50 * 1000 * 1000);
                    cfg.feature_flags.package_upgrades = true;
                }
                // This is the first protocol version currently possible.
                // Mainnet starts with version 4. Previous versions are pre mainnet and have
                // all been wiped out.
                // Every other chain is after version 4.
                4 => {
                    // Change reward slashing rate to 100%.
                    cfg.reward_slashing_rate = Some(10000);
                    // protect old and new lookup for object version
                    cfg.gas_model_version = Some(3);
                }
                5 => {
                    cfg.feature_flags.missing_type_is_compatibility_error = true;
                    cfg.gas_model_version = Some(4);
                    cfg.feature_flags.scoring_decision_with_validity_cutoff = true;
                    cfg.scoring_decision_mad_divisor = Some(2.3);
                    cfg.scoring_decision_cutoff_value = Some(2.5);
                }
                6 => {
                    cfg.gas_model_version = Some(5);
                    cfg.buffer_stake_for_protocol_upgrade_bps = Some(5000);
                    cfg.feature_flags.consensus_order_end_of_epoch_last = true;
                }
                7 => {
                    cfg.feature_flags.disallow_adding_abilities_on_upgrade = true;
                    cfg.feature_flags
                        .disable_invariant_violation_check_in_swap_loc = true;
                    cfg.feature_flags.ban_entry_init = true;
                    cfg.feature_flags.package_digest_hash_module = true;
                }
                8 => {
                    cfg.feature_flags
                        .disallow_change_struct_type_params_on_upgrade = true;
                }
                9 => {
                    // Limits the length of a Move identifier
                    cfg.max_move_identifier_len = Some(128);
                    cfg.feature_flags.no_extraneous_module_bytes = true;
                    cfg.feature_flags
                        .advance_to_highest_supported_protocol_version = true;
                }
                10 => {
                    cfg.max_verifier_meter_ticks_per_function = Some(16_000_000);
                    cfg.max_meter_ticks_per_module = Some(16_000_000);
                }
                11 => {
                    cfg.max_move_value_depth = Some(128);
                }
                12 => {
                    cfg.feature_flags.narwhal_versioned_metadata = true;
                    if chain != Chain::Mainnet {
                        cfg.feature_flags.commit_root_state_digest = true;
                    }

                    if chain != Chain::Mainnet && chain != Chain::Testnet {
                        cfg.feature_flags.zklogin_auth = true;
                    }
                }
                13 => {}
                14 => {
                    cfg.gas_rounding_step = Some(1_000);
                    cfg.gas_model_version = Some(6);
                }
                15 => {
                    cfg.feature_flags.consensus_transaction_ordering =
                        ConsensusTransactionOrdering::ByGasPrice;
                }
                16 => {
                    cfg.feature_flags.simplified_unwrap_then_delete = true;
                }
                17 => {
                    cfg.feature_flags.upgraded_multisig_supported = true;
                }
                18 => {
                    cfg.execution_version = Some(1);
                    // Following flags are implied by this execution version.  Once support for earlier
                    // protocol versions is dropped, these flags can be removed:
                    // cfg.feature_flags.package_upgrades = true;
                    // cfg.feature_flags.disallow_adding_abilities_on_upgrade = true;
                    // cfg.feature_flags.disallow_change_struct_type_params_on_upgrade = true;
                    // cfg.feature_flags.loaded_child_objects_fixed = true;
                    // cfg.feature_flags.ban_entry_init = true;
                    // cfg.feature_flags.pack_digest_hash_modules = true;
                    cfg.feature_flags.txn_base_cost_as_multiplier = true;
                    // this is a multiplier of the gas price
                    cfg.base_tx_cost_fixed = Some(1_000);
                }
                19 => {
                    cfg.max_num_event_emit = Some(1024);
                    // We maintain the same total size limit for events, but increase the number of
                    // events that can be emitted.
                    cfg.max_event_emit_size_total = Some(
                        256 /* former event count limit */ * 250 * 1024, /* size limit per event */
                    );
                }
                20 => {
                    cfg.feature_flags.commit_root_state_digest = true;

                    if chain != Chain::Mainnet {
                        cfg.feature_flags.narwhal_new_leader_election_schedule = true;
                        cfg.consensus_bad_nodes_stake_threshold = Some(20);
                    }
                }

                21 => {
                    if chain != Chain::Mainnet {
                        cfg.feature_flags.zklogin_supported_providers = BTreeSet::from([
                            "Google".to_string(),
                            "Facebook".to_string(),
                            "Twitch".to_string(),
                        ]);
                    }
                }
                22 => {
                    cfg.feature_flags.loaded_child_object_format = true;
                }
                23 => {
                    cfg.feature_flags.loaded_child_object_format_type = true;
                    cfg.feature_flags.narwhal_new_leader_election_schedule = true;
                    // Taking a baby step approach, we consider only 20% by stake as bad nodes so we
                    // have a 80% by stake of nodes participating in the leader committee. That allow
                    // us for more redundancy in case we have validators under performing - since the
                    // responsibility is shared amongst more nodes. We can increase that once we do have
                    // higher confidence.
                    cfg.consensus_bad_nodes_stake_threshold = Some(20);
                }
                24 => {
                    cfg.feature_flags.simple_conservation_checks = true;
                    cfg.max_publish_or_upgrade_per_ptb = Some(5);

                    cfg.feature_flags.end_of_epoch_transaction_supported = true;

                    if chain != Chain::Mainnet {
                        cfg.feature_flags.enable_jwk_consensus_updates = true;
                        // Max of 10 votes per hour
                        cfg.max_jwk_votes_per_validator_per_epoch = Some(240);
                        cfg.max_age_of_jwk_in_epochs = Some(1);
                    }
                }
                25 => {
                    // Enable zkLogin for all providers in all networks.
                    cfg.feature_flags.zklogin_supported_providers = BTreeSet::from([
                        "Google".to_string(),
                        "Facebook".to_string(),
                        "Twitch".to_string(),
                    ]);
                    cfg.feature_flags.zklogin_auth = true;

                    // Enable jwk consensus updates
                    cfg.feature_flags.enable_jwk_consensus_updates = true;
                    cfg.max_jwk_votes_per_validator_per_epoch = Some(240);
                    cfg.max_age_of_jwk_in_epochs = Some(1);
                }
                26 => {
                    cfg.gas_model_version = Some(7);
                    // Only enable receiving objects in devnet
                    if chain != Chain::Mainnet && chain != Chain::Testnet {
                        cfg.transfer_receive_object_cost_base = Some(52);
                        cfg.feature_flags.receive_objects = true;
                    }
                }
                27 => {
                    cfg.gas_model_version = Some(8);
                }
                28 => {
                    // zklogin::check_zklogin_id
                    cfg.check_zklogin_id_cost_base = Some(200);
                    // zklogin::check_zklogin_issuer
                    cfg.check_zklogin_issuer_cost_base = Some(200);

                    // Only enable effects v2 on devnet.
                    if chain != Chain::Mainnet && chain != Chain::Testnet {
                        cfg.feature_flags.enable_effects_v2 = true;
                    }
                }
                29 => {
                    cfg.feature_flags.verify_legacy_zklogin_address = true;
                }
                30 => {
                    // Only enable nw certificate v2 on testnet.
                    if chain != Chain::Mainnet {
                        cfg.feature_flags.narwhal_certificate_v2 = true;
                    }

                    cfg.random_beacon_reduction_allowed_delta = Some(800);
                    // Only enable effects v2 on devnet and testnet.
                    if chain != Chain::Mainnet {
                        cfg.feature_flags.enable_effects_v2 = true;
                    }

                    // zklogin_supported_providers config is deprecated, zklogin
                    // signature verifier will use the fetched jwk map to determine
                    // whether the provider is supported based on node config.
                    cfg.feature_flags.zklogin_supported_providers = BTreeSet::default();

                    cfg.feature_flags.recompute_has_public_transfer_in_execution = true;
                }
                31 => {
                    cfg.execution_version = Some(2);
                    // Only enable shared object deletion on devnet
                    if chain != Chain::Mainnet && chain != Chain::Testnet {
                        cfg.feature_flags.shared_object_deletion = true;
                    }
                }
                32 => {
                    // enable zklogin in multisig in devnet and testnet
                    if chain != Chain::Mainnet {
                        cfg.feature_flags.accept_zklogin_in_multisig = true;
                    }
                    // enable receiving objects in devnet and testnet
                    if chain != Chain::Mainnet {
                        cfg.transfer_receive_object_cost_base = Some(52);
                        cfg.feature_flags.receive_objects = true;
                    }
                    // Only enable random beacon on devnet
                    if chain != Chain::Mainnet && chain != Chain::Testnet {
                        cfg.feature_flags.random_beacon = true;
                        cfg.random_beacon_reduction_lower_bound = Some(1600);
                        cfg.random_beacon_dkg_timeout_round = Some(200);
                        cfg.random_beacon_min_round_interval_ms = Some(150);
                    }
                    // Only enable consensus digest in consensus commit prologue in devnet.
                    if chain != Chain::Testnet && chain != Chain::Mainnet {
                        cfg.feature_flags.include_consensus_digest_in_prologue = true;
                    }

                    // enable nw cert v2 on mainnet
                    cfg.feature_flags.narwhal_certificate_v2 = true;
                }
                33 => {
                    cfg.feature_flags.hardened_otw_check = true;
                    cfg.feature_flags.allow_receiving_object_id = true;

                    // Enable transfer-to-object in mainnet
                    cfg.transfer_receive_object_cost_base = Some(52);
                    cfg.feature_flags.receive_objects = true;

                    // Enable shared object deletion in testnet and devnet
                    if chain != Chain::Mainnet {
                        cfg.feature_flags.shared_object_deletion = true;
                    }

                    cfg.feature_flags.enable_effects_v2 = true;
                }
                34 => {}
                35 => {
                    // Add costs for poseidon::poseidon_bn254
                    if chain != Chain::Mainnet && chain != Chain::Testnet {
                        cfg.feature_flags.enable_poseidon = true;
                        cfg.poseidon_bn254_cost_base = Some(260);
                        cfg.poseidon_bn254_cost_per_block = Some(10);
                    }

                    cfg.feature_flags.enable_coin_deny_list = true;
                }
                36 => {
                    // Only enable group ops on devnet
                    if chain != Chain::Mainnet && chain != Chain::Testnet {
                        cfg.feature_flags.enable_group_ops_native_functions = true;
                        cfg.feature_flags.enable_group_ops_native_function_msm = true;
                        // Next values are arbitrary in a similar way as the other crypto native functions.
                        cfg.group_ops_bls12381_decode_scalar_cost = Some(52);
                        cfg.group_ops_bls12381_decode_g1_cost = Some(52);
                        cfg.group_ops_bls12381_decode_g2_cost = Some(52);
                        cfg.group_ops_bls12381_decode_gt_cost = Some(52);
                        cfg.group_ops_bls12381_scalar_add_cost = Some(52);
                        cfg.group_ops_bls12381_g1_add_cost = Some(52);
                        cfg.group_ops_bls12381_g2_add_cost = Some(52);
                        cfg.group_ops_bls12381_gt_add_cost = Some(52);
                        cfg.group_ops_bls12381_scalar_sub_cost = Some(52);
                        cfg.group_ops_bls12381_g1_sub_cost = Some(52);
                        cfg.group_ops_bls12381_g2_sub_cost = Some(52);
                        cfg.group_ops_bls12381_gt_sub_cost = Some(52);
                        cfg.group_ops_bls12381_scalar_mul_cost = Some(52);
                        cfg.group_ops_bls12381_g1_mul_cost = Some(52);
                        cfg.group_ops_bls12381_g2_mul_cost = Some(52);
                        cfg.group_ops_bls12381_gt_mul_cost = Some(52);
                        cfg.group_ops_bls12381_scalar_div_cost = Some(52);
                        cfg.group_ops_bls12381_g1_div_cost = Some(52);
                        cfg.group_ops_bls12381_g2_div_cost = Some(52);
                        cfg.group_ops_bls12381_gt_div_cost = Some(52);
                        cfg.group_ops_bls12381_g1_hash_to_base_cost = Some(52);
                        cfg.group_ops_bls12381_g2_hash_to_base_cost = Some(52);
                        cfg.group_ops_bls12381_g1_hash_to_cost_per_byte = Some(2);
                        cfg.group_ops_bls12381_g2_hash_to_cost_per_byte = Some(2);
                        cfg.group_ops_bls12381_g1_msm_base_cost = Some(52);
                        cfg.group_ops_bls12381_g2_msm_base_cost = Some(52);
                        cfg.group_ops_bls12381_g1_msm_base_cost_per_input = Some(52);
                        cfg.group_ops_bls12381_g2_msm_base_cost_per_input = Some(52);
                        cfg.group_ops_bls12381_msm_max_len = Some(32);
                        cfg.group_ops_bls12381_pairing_cost = Some(52);
                    }
                    // Enable shared object deletion on all networks.
                    cfg.feature_flags.shared_object_deletion = true;

                    cfg.consensus_max_transaction_size_bytes = Some(256 * 1024); // 256KB
                    cfg.consensus_max_transactions_in_block_bytes = Some(6 * 1_024 * 1024);
                    // 6 MB
                }
                37 => {
                    cfg.feature_flags.reject_mutable_random_on_entry_functions = true;

                    // Enable consensus digest in consensus commit prologue in testnet and devnet.
                    if chain != Chain::Mainnet {
                        cfg.feature_flags.include_consensus_digest_in_prologue = true;
                    }
                }
                38 => {
                    cfg.binary_module_handles = Some(100);
                    cfg.binary_struct_handles = Some(300);
                    cfg.binary_function_handles = Some(1500);
                    cfg.binary_function_instantiations = Some(750);
                    cfg.binary_signatures = Some(1000);
                    // constants and identifiers are proportional to the binary size,
                    // and they vastly depend on the code, so we are leaving them
                    // reasonably high
                    cfg.binary_constant_pool = Some(4000);
                    cfg.binary_identifiers = Some(10000);
                    cfg.binary_address_identifiers = Some(100);
                    cfg.binary_struct_defs = Some(200);
                    cfg.binary_struct_def_instantiations = Some(100);
                    cfg.binary_function_defs = Some(1000);
                    cfg.binary_field_handles = Some(500);
                    cfg.binary_field_instantiations = Some(250);
                    cfg.binary_friend_decls = Some(100);
                    // reduce dependencies maximum
                    cfg.max_package_dependencies = Some(32);
                    cfg.max_modules_in_publish = Some(64);
                    // bump execution version
                    cfg.execution_version = Some(3);
                }
                39 => {
                    // It is important that we keep this protocol version blank due to an issue with random.move.
                }
                40 => {}
                41 => {
                    // Enable group ops and all networks (but not msm)
                    cfg.feature_flags.enable_group_ops_native_functions = true;
                    // Next values are arbitrary in a similar way as the other crypto native functions.
                    cfg.group_ops_bls12381_decode_scalar_cost = Some(52);
                    cfg.group_ops_bls12381_decode_g1_cost = Some(52);
                    cfg.group_ops_bls12381_decode_g2_cost = Some(52);
                    cfg.group_ops_bls12381_decode_gt_cost = Some(52);
                    cfg.group_ops_bls12381_scalar_add_cost = Some(52);
                    cfg.group_ops_bls12381_g1_add_cost = Some(52);
                    cfg.group_ops_bls12381_g2_add_cost = Some(52);
                    cfg.group_ops_bls12381_gt_add_cost = Some(52);
                    cfg.group_ops_bls12381_scalar_sub_cost = Some(52);
                    cfg.group_ops_bls12381_g1_sub_cost = Some(52);
                    cfg.group_ops_bls12381_g2_sub_cost = Some(52);
                    cfg.group_ops_bls12381_gt_sub_cost = Some(52);
                    cfg.group_ops_bls12381_scalar_mul_cost = Some(52);
                    cfg.group_ops_bls12381_g1_mul_cost = Some(52);
                    cfg.group_ops_bls12381_g2_mul_cost = Some(52);
                    cfg.group_ops_bls12381_gt_mul_cost = Some(52);
                    cfg.group_ops_bls12381_scalar_div_cost = Some(52);
                    cfg.group_ops_bls12381_g1_div_cost = Some(52);
                    cfg.group_ops_bls12381_g2_div_cost = Some(52);
                    cfg.group_ops_bls12381_gt_div_cost = Some(52);
                    cfg.group_ops_bls12381_g1_hash_to_base_cost = Some(52);
                    cfg.group_ops_bls12381_g2_hash_to_base_cost = Some(52);
                    cfg.group_ops_bls12381_g1_hash_to_cost_per_byte = Some(2);
                    cfg.group_ops_bls12381_g2_hash_to_cost_per_byte = Some(2);
                    cfg.group_ops_bls12381_g1_msm_base_cost = Some(52);
                    cfg.group_ops_bls12381_g2_msm_base_cost = Some(52);
                    cfg.group_ops_bls12381_g1_msm_base_cost_per_input = Some(52);
                    cfg.group_ops_bls12381_g2_msm_base_cost_per_input = Some(52);
                    cfg.group_ops_bls12381_msm_max_len = Some(32);
                    cfg.group_ops_bls12381_pairing_cost = Some(52);
                }
                42 => {}
                43 => {
                    cfg.feature_flags.zklogin_max_epoch_upper_bound_delta = Some(30);
                    cfg.max_meter_ticks_per_package = Some(16_000_000);
                }
                44 => {
                    // Enable consensus digest in consensus commit prologue on all networks..
                    cfg.feature_flags.include_consensus_digest_in_prologue = true;
                    // Switch between Narwhal and Mysticeti per epoch in tests, devnet and testnet.
                    if chain != Chain::Mainnet {
                        cfg.feature_flags.consensus_choice = ConsensusChoice::SwapEachEpoch;
                    }
                }
                45 => {
                    // Use tonic networking for consensus, in tests and devnet.
                    if chain != Chain::Testnet && chain != Chain::Mainnet {
                        cfg.feature_flags.consensus_network = ConsensusNetwork::Tonic;
                    }
                    // Also bumps framework snapshot to fix binop issue.
                }
                // Use this template when making changes:
                //
                //     // modify an existing constant.
                //     move_binary_format_version: Some(7),
                //
                //     // Add a new constant (which is set to None in prior versions).
                //     new_constant: Some(new_value),
                //
                //     // Remove a constant (ensure that it is never accessed during this version).
                //     max_move_object_size: None,
                _ => panic!("unsupported version {:?}", version),
            }
        }
        cfg
    }

    // Extract the bytecode verifier config from this protocol config. `for_signing` indicates
    // whether this config is used for verification during signing or execution.
    pub fn verifier_config(&self, for_signing: bool) -> VerifierConfig {
        let (max_back_edges_per_function, max_back_edges_per_module) = if for_signing {
            (
                Some(self.max_back_edges_per_function() as usize),
                Some(self.max_back_edges_per_module() as usize),
            )
        } else {
            (None, None)
        };

        VerifierConfig {
            max_loop_depth: Some(self.max_loop_depth() as usize),
            max_generic_instantiation_length: Some(self.max_generic_instantiation_length() as usize),
            max_function_parameters: Some(self.max_function_parameters() as usize),
            max_basic_blocks: Some(self.max_basic_blocks() as usize),
            max_value_stack_size: self.max_value_stack_size() as usize,
            max_type_nodes: Some(self.max_type_nodes() as usize),
            max_push_size: Some(self.max_push_size() as usize),
            max_dependency_depth: Some(self.max_dependency_depth() as usize),
            max_fields_in_struct: Some(self.max_fields_in_struct() as usize),
            max_function_definitions: Some(self.max_function_definitions() as usize),
            max_struct_definitions: Some(self.max_struct_definitions() as usize),
            max_constant_vector_len: Some(self.max_move_vector_len()),
            max_back_edges_per_function,
            max_back_edges_per_module,
            max_basic_blocks_in_script: None,
            max_idenfitier_len: self.max_move_identifier_len_as_option(), // Before protocol version 9, there was no limit
            allow_receiving_object_id: self.allow_receiving_object_id(),
            reject_mutable_random_on_entry_functions: self
                .reject_mutable_random_on_entry_functions(),
        }
    }

    pub fn meter_config(&self) -> MeterConfig {
        MeterConfig {
            max_per_fun_meter_units: Some(self.max_verifier_meter_ticks_per_function() as u128),
            max_per_mod_meter_units: Some(self.max_meter_ticks_per_module() as u128),
            max_per_pkg_meter_units: Some(
                // Until the per-package limit was introduced, the per-module limit played double
                // duty.
                self.max_meter_ticks_per_package_as_option()
                    .unwrap_or_else(|| self.max_meter_ticks_per_module()) as u128,
            ),
        }
    }

    /// Override one or more settings in the config, for testing.
    /// This must be called at the beginning of the test, before get_for_(min|max)_version is
    /// called, since those functions cache their return value.
    pub fn apply_overrides_for_testing(
        override_fn: impl Fn(ProtocolVersion, Self) -> Self + Send + 'static,
    ) -> OverrideGuard {
        CONFIG_OVERRIDE.with(|ovr| {
            let mut cur = ovr.borrow_mut();
            assert!(cur.is_none(), "config override already present");
            *cur = Some(Box::new(override_fn));
            OverrideGuard
        })
    }
}

// Setters for tests
impl ProtocolConfig {
    pub fn set_package_upgrades_for_testing(&mut self, val: bool) {
        self.feature_flags.package_upgrades = val
    }
    pub fn set_advance_to_highest_supported_protocol_version_for_testing(&mut self, val: bool) {
        self.feature_flags
            .advance_to_highest_supported_protocol_version = val
    }
    pub fn set_commit_root_state_digest_supported(&mut self, val: bool) {
        self.feature_flags.commit_root_state_digest = val
    }
    pub fn set_zklogin_auth_for_testing(&mut self, val: bool) {
        self.feature_flags.zklogin_auth = val
    }
    pub fn set_enable_jwk_consensus_updates_for_testing(&mut self, val: bool) {
        self.feature_flags.enable_jwk_consensus_updates = val
    }
    pub fn set_random_beacon_for_testing(&mut self, val: bool) {
        self.feature_flags.random_beacon = val
    }
    pub fn set_upgraded_multisig_for_testing(&mut self, val: bool) {
        self.feature_flags.upgraded_multisig_supported = val
    }
    pub fn set_accept_zklogin_in_multisig_for_testing(&mut self, val: bool) {
        self.feature_flags.accept_zklogin_in_multisig = val
    }

    pub fn set_shared_object_deletion(&mut self, val: bool) {
        self.feature_flags.shared_object_deletion = val;
    }

    pub fn set_narwhal_new_leader_election_schedule(&mut self, val: bool) {
        self.feature_flags.narwhal_new_leader_election_schedule = val;
    }

    pub fn set_consensus_bad_nodes_stake_threshold(&mut self, val: u64) {
        self.consensus_bad_nodes_stake_threshold = Some(val);
    }
    pub fn set_receive_object_for_testing(&mut self, val: bool) {
        self.feature_flags.receive_objects = val
    }
    pub fn set_narwhal_certificate_v2(&mut self, val: bool) {
        self.feature_flags.narwhal_certificate_v2 = val
    }
    pub fn set_verify_legacy_zklogin_address(&mut self, val: bool) {
        self.feature_flags.verify_legacy_zklogin_address = val
    }
    pub fn set_enable_effects_v2(&mut self, val: bool) {
        self.feature_flags.enable_effects_v2 = val;
    }
    pub fn set_consensus_max_transaction_size_bytes(&mut self, val: u64) {
        self.consensus_max_transaction_size_bytes = Some(val);
    }
    pub fn set_consensus_max_transactions_in_block_bytes(&mut self, val: u64) {
        self.consensus_max_transactions_in_block_bytes = Some(val);
    }

    pub fn set_per_object_congestion_control_mode(&mut self, val: PerObjectCongestionControlMode) {
        self.feature_flags.per_object_congestion_control_mode = val;
    }

    pub fn set_consensus_choice(&mut self, val: ConsensusChoice) {
        self.feature_flags.consensus_choice = val;
    }

    pub fn set_consensus_network(&mut self, val: ConsensusNetwork) {
        self.feature_flags.consensus_network = val;
    }

    pub fn set_max_accumulated_txn_cost_per_object_in_checkpoint(&mut self, val: u64) {
        self.max_accumulated_txn_cost_per_object_in_checkpoint = Some(val);
    }

    pub fn set_zklogin_max_epoch_upper_bound_delta(&mut self, val: Option<u64>) {
        self.feature_flags.zklogin_max_epoch_upper_bound_delta = val
    }
}

type OverrideFn = dyn Fn(ProtocolVersion, ProtocolConfig) -> ProtocolConfig + Send;

thread_local! {
    static CONFIG_OVERRIDE: RefCell<Option<Box<OverrideFn>>> = RefCell::new(None);
}

#[must_use]
pub struct OverrideGuard;

impl Drop for OverrideGuard {
    fn drop(&mut self) {
        info!("restoring override fn");
        CONFIG_OVERRIDE.with(|ovr| {
            *ovr.borrow_mut() = None;
        });
    }
}

/// Defines which limit got crossed.
/// The value which crossed the limit and value of the limit crossed are embedded
#[derive(PartialEq, Eq)]
pub enum LimitThresholdCrossed {
    None,
    Soft(u128, u128),
    Hard(u128, u128),
}

/// Convenience function for comparing limit ranges
/// V::MAX must be at >= U::MAX and T::MAX
pub fn check_limit_in_range<T: Into<V>, U: Into<V>, V: PartialOrd + Into<u128>>(
    x: T,
    soft_limit: U,
    hard_limit: V,
) -> LimitThresholdCrossed {
    let x: V = x.into();
    let soft_limit: V = soft_limit.into();

    debug_assert!(soft_limit <= hard_limit);

    // It is important to preserve this comparison order because if soft_limit == hard_limit
    // we want LimitThresholdCrossed::Hard
    if x >= hard_limit {
        LimitThresholdCrossed::Hard(x.into(), hard_limit.into())
    } else if x < soft_limit {
        LimitThresholdCrossed::None
    } else {
        LimitThresholdCrossed::Soft(x.into(), soft_limit.into())
    }
}

#[macro_export]
macro_rules! check_limit {
    ($x:expr, $hard:expr) => {
        check_limit!($x, $hard, $hard)
    };
    ($x:expr, $soft:expr, $hard:expr) => {
        check_limit_in_range($x as u64, $soft, $hard)
    };
}

/// Used to check which limits were crossed if the TX is metered (not system tx)
/// Args are: is_metered, value_to_check, metered_limit, unmetered_limit
/// metered_limit is always less than or equal to unmetered_hard_limit
#[macro_export]
macro_rules! check_limit_by_meter {
    ($is_metered:expr, $x:expr, $metered_limit:expr, $unmetered_hard_limit:expr, $metric:expr) => {{
        // If this is metered, we use the metered_limit limit as the upper bound
        let (h, metered_str) = if $is_metered {
            ($metered_limit, "metered")
        } else {
            // Unmetered gets more headroom
            ($unmetered_hard_limit, "unmetered")
        };
        use sui_protocol_config::check_limit_in_range;
        let result = check_limit_in_range($x as u64, $metered_limit, h);
        match result {
            LimitThresholdCrossed::None => {}
            LimitThresholdCrossed::Soft(_, _) => {
                $metric.with_label_values(&[metered_str, "soft"]).inc();
            }
            LimitThresholdCrossed::Hard(_, _) => {
                $metric.with_label_values(&[metered_str, "hard"]).inc();
            }
        };
        result
    }};
}

#[cfg(all(test, not(msim)))]
mod test {
    use super::*;
    use insta::assert_yaml_snapshot;

    #[test]
    fn snapshot_tests() {
        println!("\n============================================================================");
        println!("!                                                                          !");
        println!("! IMPORTANT: never update snapshots from this test. only add new versions! !");
        println!("!                                                                          !");
        println!("============================================================================\n");
        for chain_id in &[Chain::Unknown, Chain::Mainnet, Chain::Testnet] {
            // make Chain::Unknown snapshots compatible with pre-chain-id snapshots so that we
            // don't break the release-time compatibility tests. Once Chain Id configs have been
            // released everywhere, we can remove this and only test Mainnet and Testnet
            let chain_str = match chain_id {
                Chain::Unknown => "".to_string(),
                _ => format!("{:?}_", chain_id),
            };
            for i in MIN_PROTOCOL_VERSION..=MAX_PROTOCOL_VERSION {
                let cur = ProtocolVersion::new(i);
                assert_yaml_snapshot!(
                    format!("{}version_{}", chain_str, cur.as_u64()),
                    ProtocolConfig::get_for_version(cur, *chain_id)
                );
            }
        }
    }

    #[test]
    fn test_getters() {
        let prot: ProtocolConfig =
            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
        assert_eq!(
            prot.max_arguments(),
            prot.max_arguments_as_option().unwrap()
        );
    }

    #[test]
    fn test_setters() {
        let mut prot: ProtocolConfig =
            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
        prot.set_max_arguments_for_testing(123);
        assert_eq!(prot.max_arguments(), 123);

        prot.set_max_arguments_from_str_for_testing("321".to_string());
        assert_eq!(prot.max_arguments(), 321);

        prot.disable_max_arguments_for_testing();
        assert_eq!(prot.max_arguments_as_option(), None);

        prot.set_attr_for_testing("max_arguments".to_string(), "456".to_string());
        assert_eq!(prot.max_arguments(), 456);
    }

    #[test]
    fn lookup_by_string_test() {
        let prot: ProtocolConfig =
            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
        // Does not exist
        assert!(prot.lookup_attr("some random string".to_string()).is_none());

        assert!(
            prot.lookup_attr("max_arguments".to_string())
                == Some(ProtocolConfigValue::u32(prot.max_arguments())),
        );

        // We didnt have this in version 1
        assert!(prot
            .lookup_attr("max_move_identifier_len".to_string())
            .is_none());

        // But we did in version 9
        let prot: ProtocolConfig =
            ProtocolConfig::get_for_version(ProtocolVersion::new(9), Chain::Unknown);
        assert!(
            prot.lookup_attr("max_move_identifier_len".to_string())
                == Some(ProtocolConfigValue::u64(prot.max_move_identifier_len()))
        );

        let prot: ProtocolConfig =
            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
        // We didnt have this in version 1
        assert!(prot
            .attr_map()
            .get("max_move_identifier_len")
            .unwrap()
            .is_none());
        // We had this in version 1
        assert!(
            prot.attr_map().get("max_arguments").unwrap()
                == &Some(ProtocolConfigValue::u32(prot.max_arguments()))
        );

        // Check feature flags
        let prot: ProtocolConfig =
            ProtocolConfig::get_for_version(ProtocolVersion::new(1), Chain::Unknown);
        // Does not exist
        assert!(prot
            .feature_flags
            .lookup_attr("some random string".to_owned())
            .is_none());
        assert!(prot
            .feature_flags
            .attr_map()
            .get("some random string")
            .is_none());

        // Was false in v1
        assert!(
            prot.feature_flags
                .lookup_attr("package_upgrades".to_owned())
                == Some(false)
        );
        assert!(
            prot.feature_flags
                .attr_map()
                .get("package_upgrades")
                .unwrap()
                == &false
        );
        let prot: ProtocolConfig =
            ProtocolConfig::get_for_version(ProtocolVersion::new(4), Chain::Unknown);
        // Was true from v3 and up
        assert!(
            prot.feature_flags
                .lookup_attr("package_upgrades".to_owned())
                == Some(true)
        );
        assert!(
            prot.feature_flags
                .attr_map()
                .get("package_upgrades")
                .unwrap()
                == &true
        );
    }

    #[test]
    fn limit_range_fn_test() {
        let low = 100u32;
        let high = 10000u64;

        assert!(check_limit!(1u8, low, high) == LimitThresholdCrossed::None);
        assert!(matches!(
            check_limit!(255u16, low, high),
            LimitThresholdCrossed::Soft(255u128, 100)
        ));
        // This wont compile because lossy
        //assert!(check_limit!(100000000u128, low, high) == LimitThresholdCrossed::None);
        // This wont compile because lossy
        //assert!(check_limit!(100000000usize, low, high) == LimitThresholdCrossed::None);

        assert!(matches!(
            check_limit!(2550000u64, low, high),
            LimitThresholdCrossed::Hard(2550000, 10000)
        ));

        assert!(matches!(
            check_limit!(2550000u64, high, high),
            LimitThresholdCrossed::Hard(2550000, 10000)
        ));

        assert!(matches!(
            check_limit!(1u8, high),
            LimitThresholdCrossed::None
        ));

        assert!(check_limit!(255u16, high) == LimitThresholdCrossed::None);

        assert!(matches!(
            check_limit!(2550000u64, high),
            LimitThresholdCrossed::Hard(2550000, 10000)
        ));
    }
}