solana_vote_program/vote_state/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
//! Vote state, vote program
//! Receive and processes votes from validators
pub use solana_program::vote::state::{vote_state_versions::*, *};
use {
log::*,
serde_derive::{Deserialize, Serialize},
solana_metrics::datapoint_debug,
solana_program::vote::{error::VoteError, program::id},
solana_sdk::{
account::{AccountSharedData, ReadableAccount, WritableAccount},
clock::{Epoch, Slot, UnixTimestamp},
epoch_schedule::EpochSchedule,
feature_set::{self, FeatureSet},
hash::Hash,
instruction::InstructionError,
pubkey::Pubkey,
rent::Rent,
slot_hashes::SlotHash,
sysvar::clock::Clock,
transaction_context::{
BorrowedAccount, IndexOfAccount, InstructionContext, TransactionContext,
},
},
std::{
cmp::Ordering,
collections::{HashSet, VecDeque},
fmt::Debug,
},
};
#[frozen_abi(digest = "2AuJFjx7SYrJ2ugCfH1jFh3Lr9UHMEPfKwwk1NcjqND1")]
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, AbiEnumVisitor, AbiExample)]
pub enum VoteTransaction {
Vote(Vote),
VoteStateUpdate(VoteStateUpdate),
#[serde(with = "serde_compact_vote_state_update")]
CompactVoteStateUpdate(VoteStateUpdate),
}
impl VoteTransaction {
pub fn slots(&self) -> Vec<Slot> {
match self {
VoteTransaction::Vote(vote) => vote.slots.clone(),
VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update.slots(),
VoteTransaction::CompactVoteStateUpdate(vote_state_update) => vote_state_update.slots(),
}
}
pub fn slot(&self, i: usize) -> Slot {
match self {
VoteTransaction::Vote(vote) => vote.slots[i],
VoteTransaction::VoteStateUpdate(vote_state_update)
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.lockouts[i].slot()
}
}
}
pub fn len(&self) -> usize {
match self {
VoteTransaction::Vote(vote) => vote.slots.len(),
VoteTransaction::VoteStateUpdate(vote_state_update)
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.lockouts.len()
}
}
}
pub fn is_empty(&self) -> bool {
match self {
VoteTransaction::Vote(vote) => vote.slots.is_empty(),
VoteTransaction::VoteStateUpdate(vote_state_update)
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.lockouts.is_empty()
}
}
}
pub fn hash(&self) -> Hash {
match self {
VoteTransaction::Vote(vote) => vote.hash,
VoteTransaction::VoteStateUpdate(vote_state_update) => vote_state_update.hash,
VoteTransaction::CompactVoteStateUpdate(vote_state_update) => vote_state_update.hash,
}
}
pub fn timestamp(&self) -> Option<UnixTimestamp> {
match self {
VoteTransaction::Vote(vote) => vote.timestamp,
VoteTransaction::VoteStateUpdate(vote_state_update)
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.timestamp
}
}
}
pub fn set_timestamp(&mut self, ts: Option<UnixTimestamp>) {
match self {
VoteTransaction::Vote(vote) => vote.timestamp = ts,
VoteTransaction::VoteStateUpdate(vote_state_update)
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.timestamp = ts
}
}
}
pub fn last_voted_slot(&self) -> Option<Slot> {
match self {
VoteTransaction::Vote(vote) => vote.last_voted_slot(),
VoteTransaction::VoteStateUpdate(vote_state_update)
| VoteTransaction::CompactVoteStateUpdate(vote_state_update) => {
vote_state_update.last_voted_slot()
}
}
}
pub fn last_voted_slot_hash(&self) -> Option<(Slot, Hash)> {
Some((self.last_voted_slot()?, self.hash()))
}
}
impl From<Vote> for VoteTransaction {
fn from(vote: Vote) -> Self {
VoteTransaction::Vote(vote)
}
}
impl From<VoteStateUpdate> for VoteTransaction {
fn from(vote_state_update: VoteStateUpdate) -> Self {
VoteTransaction::VoteStateUpdate(vote_state_update)
}
}
// utility function, used by Stakes, tests
pub fn from<T: ReadableAccount>(account: &T) -> Option<VoteState> {
VoteState::deserialize(account.data()).ok()
}
// utility function, used by Stakes, tests
pub fn to<T: WritableAccount>(versioned: &VoteStateVersions, account: &mut T) -> Option<()> {
VoteState::serialize(versioned, account.data_as_mut_slice()).ok()
}
// Updates the vote account state with a new VoteState instance. This is required temporarily during the
// upgrade of vote account state from V1_14_11 to Current.
fn set_vote_account_state(
vote_account: &mut BorrowedAccount,
vote_state: VoteState,
feature_set: &FeatureSet,
) -> Result<(), InstructionError> {
// Only if vote_state_add_vote_latency feature is enabled should the new version of vote state be stored
if feature_set.is_active(&feature_set::vote_state_add_vote_latency::id()) {
// If the account is not large enough to store the vote state, then attempt a realloc to make it large enough.
// The realloc can only proceed if the vote account has balance sufficient for rent exemption at the new size.
if (vote_account.get_data().len() < VoteStateVersions::vote_state_size_of(true))
&& (!vote_account
.is_rent_exempt_at_data_length(VoteStateVersions::vote_state_size_of(true))
|| vote_account
.set_data_length(VoteStateVersions::vote_state_size_of(true))
.is_err())
{
// Account cannot be resized to the size of a vote state as it will not be rent exempt, or failed to be
// resized for other reasons. So store the V1_14_11 version.
return vote_account.set_state(&VoteStateVersions::V1_14_11(Box::new(
VoteState1_14_11::from(vote_state),
)));
}
// Vote account is large enough to store the newest version of vote state
vote_account.set_state(&VoteStateVersions::new_current(vote_state))
// Else when the vote_state_add_vote_latency feature is not enabled, then the V1_14_11 version is stored
} else {
vote_account.set_state(&VoteStateVersions::V1_14_11(Box::new(
VoteState1_14_11::from(vote_state),
)))
}
}
fn check_update_vote_state_slots_are_valid(
vote_state: &VoteState,
vote_state_update: &mut VoteStateUpdate,
slot_hashes: &[(Slot, Hash)],
) -> Result<(), VoteError> {
if vote_state_update.lockouts.is_empty() {
return Err(VoteError::EmptySlots);
}
let last_vote_state_update_slot = vote_state_update
.lockouts
.back()
.expect("must be nonempty, checked above")
.slot();
// If the vote state update is not new enough, return
if let Some(last_vote_slot) = vote_state.votes.back().map(|lockout| lockout.slot()) {
if last_vote_state_update_slot <= last_vote_slot {
return Err(VoteError::VoteTooOld);
}
}
if slot_hashes.is_empty() {
return Err(VoteError::SlotsMismatch);
}
let earliest_slot_hash_in_history = slot_hashes.last().unwrap().0;
// Check if the proposed vote is too old to be in the SlotHash history
if last_vote_state_update_slot < earliest_slot_hash_in_history {
// If this is the last slot in the vote update, it must be in SlotHashes,
// otherwise we have no way of confirming if the hash matches
return Err(VoteError::VoteTooOld);
}
// Overwrite the proposed root if it is too old to be in the SlotHash history
if let Some(proposed_root) = vote_state_update.root {
// If the new proposed root `R` is less than the earliest slot hash in the history
// such that we cannot verify whether the slot was actually was on this fork, set
// the root to the latest vote in the vote state that's less than R. If no
// votes from the vote state are less than R, use its root instead.
if proposed_root < earliest_slot_hash_in_history {
// First overwrite the proposed root with the vote state's root
vote_state_update.root = vote_state.root_slot;
// Then try to find the latest vote in vote state that's less than R
for vote in vote_state.votes.iter().rev() {
if vote.slot() <= proposed_root {
vote_state_update.root = Some(vote.slot());
break;
}
}
}
}
// Index into the new proposed vote state's slots, starting with the root if it exists then
// we use this mutable root to fold checking the root slot into the below loop
// for performance
let mut root_to_check = vote_state_update.root;
let mut vote_state_update_index = 0;
// index into the slot_hashes, starting at the oldest known
// slot hash
let mut slot_hashes_index = slot_hashes.len();
let mut vote_state_update_indexes_to_filter = vec![];
// Note:
//
// 1) `vote_state_update.lockouts` is sorted from oldest/smallest vote to newest/largest
// vote, due to the way votes are applied to the vote state (newest votes
// pushed to the back).
//
// 2) Conversely, `slot_hashes` is sorted from newest/largest vote to
// the oldest/smallest vote
//
// Unlike for vote updates, vote state updates here can't only check votes older than the last vote
// because have to ensure that every slot is actually part of the history, not just the most
// recent ones
while vote_state_update_index < vote_state_update.lockouts.len() && slot_hashes_index > 0 {
let proposed_vote_slot = if let Some(root) = root_to_check {
root
} else {
vote_state_update.lockouts[vote_state_update_index].slot()
};
if root_to_check.is_none()
&& vote_state_update_index > 0
&& proposed_vote_slot
<= vote_state_update.lockouts[vote_state_update_index.checked_sub(1).expect(
"`vote_state_update_index` is positive when checking `SlotsNotOrdered`",
)]
.slot()
{
return Err(VoteError::SlotsNotOrdered);
}
let ancestor_slot = slot_hashes[slot_hashes_index
.checked_sub(1)
.expect("`slot_hashes_index` is positive when computing `ancestor_slot`")]
.0;
// Find if this slot in the proposed vote state exists in the SlotHashes history
// to confirm if it was a valid ancestor on this fork
match proposed_vote_slot.cmp(&ancestor_slot) {
Ordering::Less => {
if slot_hashes_index == slot_hashes.len() {
// The vote slot does not exist in the SlotHashes history because it's too old,
// i.e. older than the oldest slot in the history.
assert!(proposed_vote_slot < earliest_slot_hash_in_history);
if !vote_state.contains_slot(proposed_vote_slot) && root_to_check.is_none() {
// If the vote slot is both:
// 1) Too old
// 2) Doesn't already exist in vote state
//
// Then filter it out
vote_state_update_indexes_to_filter.push(vote_state_update_index);
}
if let Some(new_proposed_root) = root_to_check {
// 1. Because `root_to_check.is_some()`, then we know that
// we haven't checked the root yet in this loop, so
// `proposed_vote_slot` == `new_proposed_root` == `vote_state_update.root`.
assert_eq!(new_proposed_root, proposed_vote_slot);
// 2. We know from the assert earlier in the function that
// `proposed_vote_slot < earliest_slot_hash_in_history`,
// so from 1. we know that `new_proposed_root < earliest_slot_hash_in_history`.
assert!(new_proposed_root < earliest_slot_hash_in_history);
root_to_check = None;
} else {
vote_state_update_index = vote_state_update_index.checked_add(1).expect(
"`vote_state_update_index` is bounded by `MAX_LOCKOUT_HISTORY` when `proposed_vote_slot` is too old to be in SlotHashes history",
);
}
continue;
} else {
// If the vote slot is new enough to be in the slot history,
// but is not part of the slot history, then it must belong to another fork,
// which means this vote state update is invalid.
if root_to_check.is_some() {
return Err(VoteError::RootOnDifferentFork);
} else {
return Err(VoteError::SlotsMismatch);
}
}
}
Ordering::Greater => {
// Decrement `slot_hashes_index` to find newer slots in the SlotHashes history
slot_hashes_index = slot_hashes_index
.checked_sub(1)
.expect("`slot_hashes_index` is positive when finding newer slots in SlotHashes history");
continue;
}
Ordering::Equal => {
// Once the slot in `vote_state_update.lockouts` is found, bump to the next slot
// in `vote_state_update.lockouts` and continue. If we were checking the root,
// start checking the vote state instead.
if root_to_check.is_some() {
root_to_check = None;
} else {
vote_state_update_index = vote_state_update_index
.checked_add(1)
.expect("`vote_state_update_index` is bounded by `MAX_LOCKOUT_HISTORY` when match is found in SlotHashes history");
slot_hashes_index = slot_hashes_index.checked_sub(1).expect(
"`slot_hashes_index` is positive when match is found in SlotHashes history",
);
}
}
}
}
if vote_state_update_index != vote_state_update.lockouts.len() {
// The last vote slot in the update did not exist in SlotHashes
return Err(VoteError::SlotsMismatch);
}
// This assertion must be true at this point because we can assume by now:
// 1) vote_state_update_index == vote_state_update.lockouts.len()
// 2) last_vote_state_update_slot >= earliest_slot_hash_in_history
// 3) !vote_state_update.lockouts.is_empty()
//
// 1) implies that during the last iteration of the loop above,
// `vote_state_update_index` was equal to `vote_state_update.lockouts.len() - 1`,
// and was then incremented to `vote_state_update.lockouts.len()`.
// This means in that last loop iteration,
// `proposed_vote_slot ==
// vote_state_update.lockouts[vote_state_update.lockouts.len() - 1] ==
// last_vote_state_update_slot`.
//
// Then we know the last comparison `match proposed_vote_slot.cmp(&ancestor_slot)`
// is equivalent to `match last_vote_state_update_slot.cmp(&ancestor_slot)`. The result
// of this match to increment `vote_state_update_index` must have been either:
//
// 1) The Equal case ran, in which case then we know this assertion must be true
// 2) The Less case ran, and more specifically the case
// `proposed_vote_slot < earliest_slot_hash_in_history` ran, which is equivalent to
// `last_vote_state_update_slot < earliest_slot_hash_in_history`, but this is impossible
// due to assumption 3) above.
assert_eq!(
last_vote_state_update_slot,
slot_hashes[slot_hashes_index].0
);
if slot_hashes[slot_hashes_index].1 != vote_state_update.hash {
// This means the newest vote in the slot has a match that
// doesn't match the expected hash for that slot on this
// fork
warn!(
"{} dropped vote {:?} failed to match hash {} {}",
vote_state.node_pubkey,
vote_state_update,
vote_state_update.hash,
slot_hashes[slot_hashes_index].1
);
inc_new_counter_info!("dropped-vote-hash", 1);
return Err(VoteError::SlotHashMismatch);
}
// Filter out the irrelevant votes
let mut vote_state_update_index = 0;
let mut filter_votes_index = 0;
vote_state_update.lockouts.retain(|_lockout| {
let should_retain = if filter_votes_index == vote_state_update_indexes_to_filter.len() {
true
} else if vote_state_update_index == vote_state_update_indexes_to_filter[filter_votes_index]
{
filter_votes_index = filter_votes_index.checked_add(1).unwrap();
false
} else {
true
};
vote_state_update_index = vote_state_update_index
.checked_add(1)
.expect("`vote_state_update_index` is bounded by `MAX_LOCKOUT_HISTORY` when filtering out irrelevant votes");
should_retain
});
Ok(())
}
fn check_slots_are_valid(
vote_state: &VoteState,
vote_slots: &[Slot],
vote_hash: &Hash,
slot_hashes: &[(Slot, Hash)],
) -> Result<(), VoteError> {
// index into the vote's slots, starting at the oldest
// slot
let mut i = 0;
// index into the slot_hashes, starting at the oldest known
// slot hash
let mut j = slot_hashes.len();
// Note:
//
// 1) `vote_slots` is sorted from oldest/smallest vote to newest/largest
// vote, due to the way votes are applied to the vote state (newest votes
// pushed to the back).
//
// 2) Conversely, `slot_hashes` is sorted from newest/largest vote to
// the oldest/smallest vote
while i < vote_slots.len() && j > 0 {
// 1) increment `i` to find the smallest slot `s` in `vote_slots`
// where `s` >= `last_voted_slot`
if vote_state
.last_voted_slot()
.map_or(false, |last_voted_slot| vote_slots[i] <= last_voted_slot)
{
i = i
.checked_add(1)
.expect("`i` is bounded by `MAX_LOCKOUT_HISTORY` when finding larger slots");
continue;
}
// 2) Find the hash for this slot `s`.
if vote_slots[i] != slot_hashes[j.checked_sub(1).expect("`j` is positive")].0 {
// Decrement `j` to find newer slots
j = j
.checked_sub(1)
.expect("`j` is positive when finding newer slots");
continue;
}
// 3) Once the hash for `s` is found, bump `s` to the next slot
// in `vote_slots` and continue.
i = i
.checked_add(1)
.expect("`i` is bounded by `MAX_LOCKOUT_HISTORY` when hash is found");
j = j
.checked_sub(1)
.expect("`j` is positive when hash is found");
}
if j == slot_hashes.len() {
// This means we never made it to steps 2) or 3) above, otherwise
// `j` would have been decremented at least once. This means
// there are not slots in `vote_slots` greater than `last_voted_slot`
debug!(
"{} dropped vote slots {:?}, vote hash: {:?} slot hashes:SlotHash {:?}, too old ",
vote_state.node_pubkey, vote_slots, vote_hash, slot_hashes
);
return Err(VoteError::VoteTooOld);
}
if i != vote_slots.len() {
// This means there existed some slot for which we couldn't find
// a matching slot hash in step 2)
info!(
"{} dropped vote slots {:?} failed to match slot hashes: {:?}",
vote_state.node_pubkey, vote_slots, slot_hashes,
);
inc_new_counter_info!("dropped-vote-slot", 1);
return Err(VoteError::SlotsMismatch);
}
if &slot_hashes[j].1 != vote_hash {
// This means the newest slot in the `vote_slots` has a match that
// doesn't match the expected hash for that slot on this
// fork
warn!(
"{} dropped vote slots {:?} failed to match hash {} {}",
vote_state.node_pubkey, vote_slots, vote_hash, slot_hashes[j].1
);
inc_new_counter_info!("dropped-vote-hash", 1);
return Err(VoteError::SlotHashMismatch);
}
Ok(())
}
//Ensure `check_update_vote_state_slots_are_valid(&)` runs on the slots in `new_state`
// before `process_new_vote_state()` is called
// This function should guarantee the following about `new_state`:
//
// 1) It's well ordered, i.e. the slots are sorted from smallest to largest,
// and the confirmations sorted from largest to smallest.
// 2) Confirmations `c` on any vote slot satisfy `0 < c <= MAX_LOCKOUT_HISTORY`
// 3) Lockouts are not expired by consecutive votes, i.e. for every consecutive
// `v_i`, `v_{i + 1}` satisfy `v_i.last_locked_out_slot() >= v_{i + 1}`.
// We also guarantee that compared to the current vote state, `new_state`
// introduces no rollback. This means:
//
// 1) The last slot in `new_state` is always greater than any slot in the
// current vote state.
//
// 2) From 1), this means that for every vote `s` in the current state:
// a) If there exists an `s'` in `new_state` where `s.slot == s'.slot`, then
// we must guarantee `s.confirmations <= s'.confirmations`
//
// b) If there does not exist any such `s'` in `new_state`, then there exists
// some `t` that is the smallest vote in `new_state` where `t.slot > s.slot`.
// `t` must have expired/popped off s', so it must be guaranteed that
// `s.last_locked_out_slot() < t`.
// Note these two above checks do not guarantee that the vote state being submitted
// is a vote state that could have been created by iteratively building a tower
// by processing one vote at a time. For instance, the tower:
//
// { slot 0, confirmations: 31 }
// { slot 1, confirmations: 30 }
//
// is a legal tower that could be submitted on top of a previously empty tower. However,
// there is no way to create this tower from the iterative process, because slot 1 would
// have to have at least one other slot on top of it, even if the first 30 votes were all
// popped off.
pub fn process_new_vote_state(
vote_state: &mut VoteState,
mut new_state: VecDeque<LandedVote>,
new_root: Option<Slot>,
timestamp: Option<i64>,
epoch: Epoch,
current_slot: Slot,
feature_set: Option<&FeatureSet>,
) -> Result<(), VoteError> {
assert!(!new_state.is_empty());
if new_state.len() > MAX_LOCKOUT_HISTORY {
return Err(VoteError::TooManyVotes);
}
match (new_root, vote_state.root_slot) {
(Some(new_root), Some(current_root)) => {
if new_root < current_root {
return Err(VoteError::RootRollBack);
}
}
(None, Some(_)) => {
return Err(VoteError::RootRollBack);
}
_ => (),
}
let mut previous_vote: Option<&LandedVote> = None;
// Check that all the votes in the new proposed state are:
// 1) Strictly sorted from oldest to newest vote
// 2) The confirmations are strictly decreasing
// 3) Not zero confirmation votes
for vote in &new_state {
if vote.confirmation_count() == 0 {
return Err(VoteError::ZeroConfirmations);
} else if vote.confirmation_count() > MAX_LOCKOUT_HISTORY as u32 {
return Err(VoteError::ConfirmationTooLarge);
} else if let Some(new_root) = new_root {
if vote.slot() <= new_root
&&
// This check is necessary because
// https://github.com/ryoqun/solana/blob/df55bfb46af039cbc597cd60042d49b9d90b5961/core/src/consensus.rs#L120
// always sets a root for even empty towers, which is then hard unwrapped here
// https://github.com/ryoqun/solana/blob/df55bfb46af039cbc597cd60042d49b9d90b5961/core/src/consensus.rs#L776
new_root != Slot::default()
{
return Err(VoteError::SlotSmallerThanRoot);
}
}
if let Some(previous_vote) = previous_vote {
if previous_vote.slot() >= vote.slot() {
return Err(VoteError::SlotsNotOrdered);
} else if previous_vote.confirmation_count() <= vote.confirmation_count() {
return Err(VoteError::ConfirmationsNotOrdered);
} else if vote.slot() > previous_vote.lockout.last_locked_out_slot() {
return Err(VoteError::NewVoteStateLockoutMismatch);
}
}
previous_vote = Some(vote);
}
// Find the first vote in the current vote state for a slot greater
// than the new proposed root
let mut current_vote_state_index: usize = 0;
let mut new_vote_state_index = 0;
// Accumulate credits earned by newly rooted slots. The behavior changes with timely_vote_credits: prior to
// this feature, there was a bug that counted a new root slot as 1 credit even if it had never been voted on.
// timely_vote_credits fixes this bug by only awarding credits for slots actually voted on and finalized.
let timely_vote_credits = feature_set.map_or(false, |f| {
f.is_active(&feature_set::timely_vote_credits::id())
});
let deprecate_unused_legacy_vote_plumbing = feature_set.map_or(false, |f| {
f.is_active(&feature_set::deprecate_unused_legacy_vote_plumbing::id())
});
let mut earned_credits = if timely_vote_credits { 0_u64 } else { 1_u64 };
if let Some(new_root) = new_root {
for current_vote in &vote_state.votes {
// Find the first vote in the current vote state for a slot greater
// than the new proposed root
if current_vote.slot() <= new_root {
if timely_vote_credits || (current_vote.slot() != new_root) {
earned_credits = earned_credits
.checked_add(vote_state.credits_for_vote_at_index(
current_vote_state_index,
timely_vote_credits,
deprecate_unused_legacy_vote_plumbing,
))
.expect("`earned_credits` does not overflow");
}
current_vote_state_index = current_vote_state_index
.checked_add(1)
.expect("`current_vote_state_index` is bounded by `MAX_LOCKOUT_HISTORY` when processing new root");
continue;
}
break;
}
}
// For any slots newly added to the new vote state, the vote latency of that slot is not provided by the
// VoteStateUpdate instruction contents, but instead is computed from the actual latency of the VoteStateUpdate
// instruction. This prevents other validators from manipulating their own vote latencies within their vote states
// and forcing the rest of the cluster to accept these possibly fraudulent latency values. If the
// timly_vote_credits feature is not enabled then vote latency is set to 0 for new votes.
//
// For any slot that is in both the new state and the current state, the vote latency of the new state is taken
// from the current state.
//
// Thus vote latencies are set here for any newly vote-on slots when a VoteStateUpdate instruction is received.
// They are copied into the new vote state after every VoteStateUpdate for already voted-on slots.
// And when voted-on slots are rooted, the vote latencies stored in the vote state of all the rooted slots is used
// to compute credits earned.
// All validators compute the same vote latencies because all process the same VoteStateUpdate instruction at the
// same slot, and the only time vote latencies are ever computed is at the time that their slot is first voted on;
// after that, the latencies are retained unaltered until the slot is rooted.
// All the votes in our current vote state that are missing from the new vote state
// must have been expired by later votes. Check that the lockouts match this assumption.
while current_vote_state_index < vote_state.votes.len()
&& new_vote_state_index < new_state.len()
{
let current_vote = &vote_state.votes[current_vote_state_index];
let new_vote = &mut new_state[new_vote_state_index];
// If the current slot is less than the new proposed slot, then the
// new slot must have popped off the old slot, so check that the
// lockouts are corrects.
match current_vote.slot().cmp(&new_vote.slot()) {
Ordering::Less => {
if current_vote.lockout.last_locked_out_slot() >= new_vote.slot() {
return Err(VoteError::LockoutConflict);
}
current_vote_state_index = current_vote_state_index
.checked_add(1)
.expect("`current_vote_state_index` is bounded by `MAX_LOCKOUT_HISTORY` when slot is less than proposed");
}
Ordering::Equal => {
// The new vote state should never have less lockout than
// the previous vote state for the same slot
if new_vote.confirmation_count() < current_vote.confirmation_count() {
return Err(VoteError::ConfirmationRollBack);
}
// Copy the vote slot latency in from the current state to the new state
new_vote.latency = vote_state.votes[current_vote_state_index].latency;
current_vote_state_index = current_vote_state_index
.checked_add(1)
.expect("`current_vote_state_index` is bounded by `MAX_LOCKOUT_HISTORY` when slot is equal to proposed");
new_vote_state_index = new_vote_state_index
.checked_add(1)
.expect("`new_vote_state_index` is bounded by `MAX_LOCKOUT_HISTORY` when slot is equal to proposed");
}
Ordering::Greater => {
new_vote_state_index = new_vote_state_index
.checked_add(1)
.expect("`new_vote_state_index` is bounded by `MAX_LOCKOUT_HISTORY` when slot is greater than proposed");
}
}
}
// `new_vote_state` passed all the checks, finalize the change by rewriting
// our state.
// Now set the vote latencies on new slots not in the current state. New slots not in the current vote state will
// have had their latency initialized to 0 by the above loop. Those will now be updated to their actual latency.
// If the timely_vote_credits feature is not enabled, then the latency is left as 0 for such slots, which will
// result in 1 credit per slot when credits are calculated at the time that the slot is rooted.
if timely_vote_credits {
for new_vote in new_state.iter_mut() {
if new_vote.latency == 0 {
new_vote.latency = VoteState::compute_vote_latency(new_vote.slot(), current_slot);
}
}
}
if vote_state.root_slot != new_root {
// Award vote credits based on the number of slots that were voted on and have reached finality
// For each finalized slot, there was one voted-on slot in the new vote state that was responsible for
// finalizing it. Each of those votes is awarded 1 credit.
vote_state.increment_credits(epoch, earned_credits);
}
if let Some(timestamp) = timestamp {
let last_slot = new_state.back().unwrap().slot();
vote_state.process_timestamp(last_slot, timestamp)?;
}
vote_state.root_slot = new_root;
vote_state.votes = new_state;
Ok(())
}
pub fn process_vote_unfiltered(
vote_state: &mut VoteState,
vote_slots: &[Slot],
vote: &Vote,
slot_hashes: &[SlotHash],
epoch: Epoch,
current_slot: Slot,
timely_vote_credits: bool,
deprecate_unused_legacy_vote_plumbing: bool,
) -> Result<(), VoteError> {
check_slots_are_valid(vote_state, vote_slots, &vote.hash, slot_hashes)?;
vote_slots.iter().for_each(|s| {
vote_state.process_next_vote_slot(
*s,
epoch,
current_slot,
timely_vote_credits,
deprecate_unused_legacy_vote_plumbing,
)
});
Ok(())
}
pub fn process_vote(
vote_state: &mut VoteState,
vote: &Vote,
slot_hashes: &[SlotHash],
epoch: Epoch,
current_slot: Slot,
timely_vote_credits: bool,
deprecate_unused_legacy_vote_plumbing: bool,
) -> Result<(), VoteError> {
if vote.slots.is_empty() {
return Err(VoteError::EmptySlots);
}
let earliest_slot_in_history = slot_hashes.last().map(|(slot, _hash)| *slot).unwrap_or(0);
let vote_slots = vote
.slots
.iter()
.filter(|slot| **slot >= earliest_slot_in_history)
.cloned()
.collect::<Vec<Slot>>();
if vote_slots.is_empty() {
return Err(VoteError::VotesTooOldAllFiltered);
}
process_vote_unfiltered(
vote_state,
&vote_slots,
vote,
slot_hashes,
epoch,
current_slot,
timely_vote_credits,
deprecate_unused_legacy_vote_plumbing,
)
}
/// "unchecked" functions used by tests and Tower
pub fn process_vote_unchecked(vote_state: &mut VoteState, vote: Vote) -> Result<(), VoteError> {
if vote.slots.is_empty() {
return Err(VoteError::EmptySlots);
}
let slot_hashes: Vec<_> = vote.slots.iter().rev().map(|x| (*x, vote.hash)).collect();
process_vote_unfiltered(
vote_state,
&vote.slots,
&vote,
&slot_hashes,
vote_state.current_epoch(),
0,
true,
true,
)
}
#[cfg(test)]
pub fn process_slot_votes_unchecked(vote_state: &mut VoteState, slots: &[Slot]) {
for slot in slots {
process_slot_vote_unchecked(vote_state, *slot);
}
}
pub fn process_slot_vote_unchecked(vote_state: &mut VoteState, slot: Slot) {
let _ = process_vote_unchecked(vote_state, Vote::new(vec![slot], Hash::default()));
}
/// Authorize the given pubkey to withdraw or sign votes. This may be called multiple times,
/// but will implicitly withdraw authorization from the previously authorized
/// key
pub fn authorize<S: std::hash::BuildHasher>(
vote_account: &mut BorrowedAccount,
authorized: &Pubkey,
vote_authorize: VoteAuthorize,
signers: &HashSet<Pubkey, S>,
clock: &Clock,
feature_set: &FeatureSet,
) -> Result<(), InstructionError> {
let mut vote_state: VoteState = vote_account
.get_state::<VoteStateVersions>()?
.convert_to_current();
match vote_authorize {
VoteAuthorize::Voter => {
let authorized_withdrawer_signer =
verify_authorized_signer(&vote_state.authorized_withdrawer, signers).is_ok();
vote_state.set_new_authorized_voter(
authorized,
clock.epoch,
clock
.leader_schedule_epoch
.checked_add(1)
.expect("epoch should be much less than u64::MAX"),
|epoch_authorized_voter| {
// current authorized withdrawer or authorized voter must say "yay"
if authorized_withdrawer_signer {
Ok(())
} else {
verify_authorized_signer(&epoch_authorized_voter, signers)
}
},
)?;
}
VoteAuthorize::Withdrawer => {
// current authorized withdrawer must say "yay"
verify_authorized_signer(&vote_state.authorized_withdrawer, signers)?;
vote_state.authorized_withdrawer = *authorized;
}
}
set_vote_account_state(vote_account, vote_state, feature_set)
}
/// Update the node_pubkey, requires signature of the authorized voter
pub fn update_validator_identity<S: std::hash::BuildHasher>(
vote_account: &mut BorrowedAccount,
node_pubkey: &Pubkey,
signers: &HashSet<Pubkey, S>,
feature_set: &FeatureSet,
) -> Result<(), InstructionError> {
let mut vote_state: VoteState = vote_account
.get_state::<VoteStateVersions>()?
.convert_to_current();
// current authorized withdrawer must say "yay"
verify_authorized_signer(&vote_state.authorized_withdrawer, signers)?;
// new node must say "yay"
verify_authorized_signer(node_pubkey, signers)?;
vote_state.node_pubkey = *node_pubkey;
set_vote_account_state(vote_account, vote_state, feature_set)
}
/// Update the vote account's commission
pub fn update_commission<S: std::hash::BuildHasher>(
vote_account: &mut BorrowedAccount,
commission: u8,
signers: &HashSet<Pubkey, S>,
epoch_schedule: &EpochSchedule,
clock: &Clock,
feature_set: &FeatureSet,
) -> Result<(), InstructionError> {
// Decode vote state only once, and only if needed
let mut vote_state = None;
let enforce_commission_update_rule =
if feature_set.is_active(&feature_set::allow_commission_decrease_at_any_time::id()) {
if let Ok(decoded_vote_state) = vote_account.get_state::<VoteStateVersions>() {
vote_state = Some(decoded_vote_state.convert_to_current());
is_commission_increase(vote_state.as_ref().unwrap(), commission)
} else {
true
}
} else {
true
};
#[allow(clippy::collapsible_if)]
if enforce_commission_update_rule
&& feature_set
.is_active(&feature_set::commission_updates_only_allowed_in_first_half_of_epoch::id())
{
if !is_commission_update_allowed(clock.slot, epoch_schedule) {
return Err(VoteError::CommissionUpdateTooLate.into());
}
}
let mut vote_state = match vote_state {
Some(vote_state) => vote_state,
None => vote_account
.get_state::<VoteStateVersions>()?
.convert_to_current(),
};
// current authorized withdrawer must say "yay"
verify_authorized_signer(&vote_state.authorized_withdrawer, signers)?;
vote_state.commission = commission;
set_vote_account_state(vote_account, vote_state, feature_set)
}
/// Given a proposed new commission, returns true if this would be a commission increase, false otherwise
pub fn is_commission_increase(vote_state: &VoteState, commission: u8) -> bool {
commission > vote_state.commission
}
/// Given the current slot and epoch schedule, determine if a commission change
/// is allowed
pub fn is_commission_update_allowed(slot: Slot, epoch_schedule: &EpochSchedule) -> bool {
// always allowed during warmup epochs
if let Some(relative_slot) = slot
.saturating_sub(epoch_schedule.first_normal_slot)
.checked_rem(epoch_schedule.slots_per_epoch)
{
// allowed up to the midpoint of the epoch
relative_slot.saturating_mul(2) <= epoch_schedule.slots_per_epoch
} else {
// no slots per epoch, just allow it, even though this should never happen
true
}
}
fn verify_authorized_signer<S: std::hash::BuildHasher>(
authorized: &Pubkey,
signers: &HashSet<Pubkey, S>,
) -> Result<(), InstructionError> {
if signers.contains(authorized) {
Ok(())
} else {
Err(InstructionError::MissingRequiredSignature)
}
}
/// Withdraw funds from the vote account
pub fn withdraw<S: std::hash::BuildHasher>(
transaction_context: &TransactionContext,
instruction_context: &InstructionContext,
vote_account_index: IndexOfAccount,
lamports: u64,
to_account_index: IndexOfAccount,
signers: &HashSet<Pubkey, S>,
rent_sysvar: &Rent,
clock: &Clock,
feature_set: &FeatureSet,
) -> Result<(), InstructionError> {
let mut vote_account = instruction_context
.try_borrow_instruction_account(transaction_context, vote_account_index)?;
let vote_state: VoteState = vote_account
.get_state::<VoteStateVersions>()?
.convert_to_current();
verify_authorized_signer(&vote_state.authorized_withdrawer, signers)?;
let remaining_balance = vote_account
.get_lamports()
.checked_sub(lamports)
.ok_or(InstructionError::InsufficientFunds)?;
if remaining_balance == 0 {
let reject_active_vote_account_close = vote_state
.epoch_credits
.last()
.map(|(last_epoch_with_credits, _, _)| {
let current_epoch = clock.epoch;
// if current_epoch - last_epoch_with_credits < 2 then the validator has received credits
// either in the current epoch or the previous epoch. If it's >= 2 then it has been at least
// one full epoch since the validator has received credits.
current_epoch.saturating_sub(*last_epoch_with_credits) < 2
})
.unwrap_or(false);
if reject_active_vote_account_close {
datapoint_debug!("vote-account-close", ("reject-active", 1, i64));
return Err(VoteError::ActiveVoteAccountClose.into());
} else {
// Deinitialize upon zero-balance
datapoint_debug!("vote-account-close", ("allow", 1, i64));
set_vote_account_state(&mut vote_account, VoteState::default(), feature_set)?;
}
} else {
let min_rent_exempt_balance = rent_sysvar.minimum_balance(vote_account.get_data().len());
if remaining_balance < min_rent_exempt_balance {
return Err(InstructionError::InsufficientFunds);
}
}
vote_account.checked_sub_lamports(lamports)?;
drop(vote_account);
let mut to_account = instruction_context
.try_borrow_instruction_account(transaction_context, to_account_index)?;
to_account.checked_add_lamports(lamports)?;
Ok(())
}
/// Initialize the vote_state for a vote account
/// Assumes that the account is being init as part of a account creation or balance transfer and
/// that the transaction must be signed by the staker's keys
pub fn initialize_account<S: std::hash::BuildHasher>(
vote_account: &mut BorrowedAccount,
vote_init: &VoteInit,
signers: &HashSet<Pubkey, S>,
clock: &Clock,
feature_set: &FeatureSet,
) -> Result<(), InstructionError> {
if vote_account.get_data().len()
!= VoteStateVersions::vote_state_size_of(
feature_set.is_active(&feature_set::vote_state_add_vote_latency::id()),
)
{
return Err(InstructionError::InvalidAccountData);
}
let versioned = vote_account.get_state::<VoteStateVersions>()?;
if !versioned.is_uninitialized() {
return Err(InstructionError::AccountAlreadyInitialized);
}
// node must agree to accept this vote account
verify_authorized_signer(&vote_init.node_pubkey, signers)?;
set_vote_account_state(vote_account, VoteState::new(vote_init, clock), feature_set)
}
fn verify_and_get_vote_state<S: std::hash::BuildHasher>(
vote_account: &BorrowedAccount,
clock: &Clock,
signers: &HashSet<Pubkey, S>,
) -> Result<VoteState, InstructionError> {
let versioned = vote_account.get_state::<VoteStateVersions>()?;
if versioned.is_uninitialized() {
return Err(InstructionError::UninitializedAccount);
}
let mut vote_state = versioned.convert_to_current();
let authorized_voter = vote_state.get_and_update_authorized_voter(clock.epoch)?;
verify_authorized_signer(&authorized_voter, signers)?;
Ok(vote_state)
}
pub fn process_vote_with_account<S: std::hash::BuildHasher>(
vote_account: &mut BorrowedAccount,
slot_hashes: &[SlotHash],
clock: &Clock,
vote: &Vote,
signers: &HashSet<Pubkey, S>,
feature_set: &FeatureSet,
) -> Result<(), InstructionError> {
let mut vote_state = verify_and_get_vote_state(vote_account, clock, signers)?;
let timely_vote_credits = feature_set.is_active(&feature_set::timely_vote_credits::id());
let deprecate_unused_legacy_vote_plumbing =
feature_set.is_active(&feature_set::deprecate_unused_legacy_vote_plumbing::id());
process_vote(
&mut vote_state,
vote,
slot_hashes,
clock.epoch,
clock.slot,
timely_vote_credits,
deprecate_unused_legacy_vote_plumbing,
)?;
if let Some(timestamp) = vote.timestamp {
vote.slots
.iter()
.max()
.ok_or(VoteError::EmptySlots)
.and_then(|slot| vote_state.process_timestamp(*slot, timestamp))?;
}
set_vote_account_state(vote_account, vote_state, feature_set)
}
pub fn process_vote_state_update<S: std::hash::BuildHasher>(
vote_account: &mut BorrowedAccount,
slot_hashes: &[SlotHash],
clock: &Clock,
vote_state_update: VoteStateUpdate,
signers: &HashSet<Pubkey, S>,
feature_set: &FeatureSet,
) -> Result<(), InstructionError> {
let mut vote_state = verify_and_get_vote_state(vote_account, clock, signers)?;
do_process_vote_state_update(
&mut vote_state,
slot_hashes,
clock.epoch,
clock.slot,
vote_state_update,
Some(feature_set),
)?;
set_vote_account_state(vote_account, vote_state, feature_set)
}
pub fn do_process_vote_state_update(
vote_state: &mut VoteState,
slot_hashes: &[SlotHash],
epoch: u64,
slot: u64,
mut vote_state_update: VoteStateUpdate,
feature_set: Option<&FeatureSet>,
) -> Result<(), VoteError> {
check_update_vote_state_slots_are_valid(vote_state, &mut vote_state_update, slot_hashes)?;
process_new_vote_state(
vote_state,
vote_state_update
.lockouts
.iter()
.map(|lockout| LandedVote::from(*lockout))
.collect(),
vote_state_update.root,
vote_state_update.timestamp,
epoch,
slot,
feature_set,
)
}
// This function is used:
// a. In many tests.
// b. In the genesis tool that initializes a cluster to create the bootstrap validator.
// c. In the ledger tool when creating bootstrap vote accounts.
pub fn create_account_with_authorized(
node_pubkey: &Pubkey,
authorized_voter: &Pubkey,
authorized_withdrawer: &Pubkey,
commission: u8,
lamports: u64,
) -> AccountSharedData {
let mut vote_account = AccountSharedData::new(lamports, VoteState::size_of(), &id());
let vote_state = VoteState::new(
&VoteInit {
node_pubkey: *node_pubkey,
authorized_voter: *authorized_voter,
authorized_withdrawer: *authorized_withdrawer,
commission,
},
&Clock::default(),
);
VoteState::serialize(
&VoteStateVersions::Current(Box::new(vote_state)),
vote_account.data_as_mut_slice(),
)
.unwrap();
vote_account
}
// create_account() should be removed, use create_account_with_authorized() instead
pub fn create_account(
vote_pubkey: &Pubkey,
node_pubkey: &Pubkey,
commission: u8,
lamports: u64,
) -> AccountSharedData {
create_account_with_authorized(node_pubkey, vote_pubkey, vote_pubkey, commission, lamports)
}
#[cfg(test)]
mod tests {
use {
super::*,
crate::vote_state,
assert_matches::assert_matches,
solana_sdk::{
account::AccountSharedData, account_utils::StateMut, clock::DEFAULT_SLOTS_PER_EPOCH,
hash::hash, transaction_context::InstructionAccount,
},
std::cell::RefCell,
test_case::test_case,
};
const MAX_RECENT_VOTES: usize = 16;
fn vote_state_new_for_test(auth_pubkey: &Pubkey) -> VoteState {
VoteState::new(
&VoteInit {
node_pubkey: solana_sdk::pubkey::new_rand(),
authorized_voter: *auth_pubkey,
authorized_withdrawer: *auth_pubkey,
commission: 0,
},
&Clock::default(),
)
}
fn create_test_account() -> (Pubkey, RefCell<AccountSharedData>) {
let rent = Rent::default();
let balance = VoteState::get_rent_exempt_reserve(&rent);
let vote_pubkey = solana_sdk::pubkey::new_rand();
(
vote_pubkey,
RefCell::new(vote_state::create_account(
&vote_pubkey,
&solana_sdk::pubkey::new_rand(),
0,
balance,
)),
)
}
#[test]
fn test_vote_state_upgrade_from_1_14_11() {
let mut feature_set = FeatureSet::default();
// Create an initial vote account that is sized for the 1_14_11 version of vote state, and has only the
// required lamports for rent exempt minimum at that size
let node_pubkey = solana_sdk::pubkey::new_rand();
let withdrawer_pubkey = solana_sdk::pubkey::new_rand();
let mut vote_state = VoteState::new(
&VoteInit {
node_pubkey,
authorized_voter: withdrawer_pubkey,
authorized_withdrawer: withdrawer_pubkey,
commission: 10,
},
&Clock::default(),
);
// Simulate prior epochs completed with credits and each setting a new authorized voter
vote_state.increment_credits(0, 100);
assert_eq!(
vote_state
.set_new_authorized_voter(&solana_sdk::pubkey::new_rand(), 0, 1, |_pubkey| Ok(())),
Ok(())
);
vote_state.increment_credits(1, 200);
assert_eq!(
vote_state
.set_new_authorized_voter(&solana_sdk::pubkey::new_rand(), 1, 2, |_pubkey| Ok(())),
Ok(())
);
vote_state.increment_credits(2, 300);
assert_eq!(
vote_state
.set_new_authorized_voter(&solana_sdk::pubkey::new_rand(), 2, 3, |_pubkey| Ok(())),
Ok(())
);
// Simulate votes having occurred
vec![
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,
]
.into_iter()
.for_each(|v| vote_state.process_next_vote_slot(v, 4, 0, false, true));
let version1_14_11_serialized = bincode::serialize(&VoteStateVersions::V1_14_11(Box::new(
VoteState1_14_11::from(vote_state.clone()),
)))
.unwrap();
let version1_14_11_serialized_len = version1_14_11_serialized.len();
let rent = Rent::default();
let lamports = rent.minimum_balance(version1_14_11_serialized_len);
let mut vote_account =
AccountSharedData::new(lamports, version1_14_11_serialized_len, &id());
vote_account.set_data_from_slice(&version1_14_11_serialized);
// Create a fake TransactionContext with a fake InstructionContext with a single account which is the
// vote account that was just created
let processor_account = AccountSharedData::new(0, 0, &solana_sdk::native_loader::id());
let transaction_context = TransactionContext::new(
vec![(id(), processor_account), (node_pubkey, vote_account)],
#[allow(clippy::clone_on_copy)]
rent.clone(),
0,
0,
);
let mut instruction_context = InstructionContext::default();
instruction_context.configure(
&[0],
&[InstructionAccount {
index_in_transaction: 1,
index_in_caller: 1,
index_in_callee: 0,
is_signer: false,
is_writable: true,
}],
&[],
);
// Get the BorrowedAccount from the InstructionContext which is what is used to manipulate and inspect account
// state
let mut borrowed_account = instruction_context
.try_borrow_instruction_account(&transaction_context, 0)
.unwrap();
// Ensure that the vote state started out at 1_14_11
let vote_state_version = borrowed_account.get_state::<VoteStateVersions>().unwrap();
assert_matches!(vote_state_version, VoteStateVersions::V1_14_11(_));
// Convert the vote state to current as would occur during vote instructions
let converted_vote_state = vote_state_version.convert_to_current();
// Check to make sure that the vote_state is unchanged
assert!(vote_state == converted_vote_state);
let vote_state = converted_vote_state;
// Now re-set the vote account state; because the feature is not enabled, the old 1_14_11 format should be
// written out
assert_eq!(
set_vote_account_state(&mut borrowed_account, vote_state.clone(), &feature_set),
Ok(())
);
let vote_state_version = borrowed_account.get_state::<VoteStateVersions>().unwrap();
assert_matches!(vote_state_version, VoteStateVersions::V1_14_11(_));
// Convert the vote state to current as would occur during vote instructions
let converted_vote_state = vote_state_version.convert_to_current();
// Check to make sure that the vote_state is unchanged
assert_eq!(vote_state, converted_vote_state);
let vote_state = converted_vote_state;
// Test that when the feature is enabled, if the vote account does not have sufficient lamports to realloc,
// the old vote state is written out
feature_set.activate(&feature_set::vote_state_add_vote_latency::id(), 1);
assert_eq!(
set_vote_account_state(&mut borrowed_account, vote_state.clone(), &feature_set),
Ok(())
);
let vote_state_version = borrowed_account.get_state::<VoteStateVersions>().unwrap();
assert_matches!(vote_state_version, VoteStateVersions::V1_14_11(_));
// Convert the vote state to current as would occur during vote instructions
let converted_vote_state = vote_state_version.convert_to_current();
// Check to make sure that the vote_state is unchanged
assert_eq!(vote_state, converted_vote_state);
let vote_state = converted_vote_state;
// Test that when the feature is enabled, if the vote account does have sufficient lamports, the
// new vote state is written out
assert_eq!(
borrowed_account.set_lamports(rent.minimum_balance(VoteState::size_of()),),
Ok(())
);
assert_eq!(
set_vote_account_state(&mut borrowed_account, vote_state.clone(), &feature_set),
Ok(())
);
let vote_state_version = borrowed_account.get_state::<VoteStateVersions>().unwrap();
assert_matches!(vote_state_version, VoteStateVersions::Current(_));
// Convert the vote state to current as would occur during vote instructions
let converted_vote_state = vote_state_version.convert_to_current();
// Check to make sure that the vote_state is unchanged
assert_eq!(vote_state, converted_vote_state);
}
#[test]
fn test_vote_lockout() {
let (_vote_pubkey, vote_account) = create_test_account();
let mut vote_state: VoteState =
StateMut::<VoteStateVersions>::state(&*vote_account.borrow())
.unwrap()
.convert_to_current();
for i in 0..(MAX_LOCKOUT_HISTORY + 1) {
process_slot_vote_unchecked(&mut vote_state, (INITIAL_LOCKOUT * i) as u64);
}
// The last vote should have been popped b/c it reached a depth of MAX_LOCKOUT_HISTORY
assert_eq!(vote_state.votes.len(), MAX_LOCKOUT_HISTORY);
assert_eq!(vote_state.root_slot, Some(0));
check_lockouts(&vote_state);
// One more vote that confirms the entire stack,
// the root_slot should change to the
// second vote
let top_vote = vote_state.votes.front().unwrap().slot();
let slot = vote_state.last_lockout().unwrap().last_locked_out_slot();
process_slot_vote_unchecked(&mut vote_state, slot);
assert_eq!(Some(top_vote), vote_state.root_slot);
// Expire everything except the first vote
let slot = vote_state
.votes
.front()
.unwrap()
.lockout
.last_locked_out_slot();
process_slot_vote_unchecked(&mut vote_state, slot);
// First vote and new vote are both stored for a total of 2 votes
assert_eq!(vote_state.votes.len(), 2);
}
#[test]
fn test_update_commission() {
let node_pubkey = Pubkey::new_unique();
let withdrawer_pubkey = Pubkey::new_unique();
let clock = Clock::default();
let vote_state = VoteState::new(
&VoteInit {
node_pubkey,
authorized_voter: withdrawer_pubkey,
authorized_withdrawer: withdrawer_pubkey,
commission: 10,
},
&clock,
);
let serialized =
bincode::serialize(&VoteStateVersions::Current(Box::new(vote_state.clone()))).unwrap();
let serialized_len = serialized.len();
let rent = Rent::default();
let lamports = rent.minimum_balance(serialized_len);
let mut vote_account = AccountSharedData::new(lamports, serialized_len, &id());
vote_account.set_data_from_slice(&serialized);
// Create a fake TransactionContext with a fake InstructionContext with a single account which is the
// vote account that was just created
let processor_account = AccountSharedData::new(0, 0, &solana_sdk::native_loader::id());
let transaction_context = TransactionContext::new(
vec![(id(), processor_account), (node_pubkey, vote_account)],
rent,
0,
0,
);
let mut instruction_context = InstructionContext::default();
instruction_context.configure(
&[0],
&[InstructionAccount {
index_in_transaction: 1,
index_in_caller: 1,
index_in_callee: 0,
is_signer: false,
is_writable: true,
}],
&[],
);
// Get the BorrowedAccount from the InstructionContext which is what is used to manipulate and inspect account
// state
let mut borrowed_account = instruction_context
.try_borrow_instruction_account(&transaction_context, 0)
.unwrap();
let epoch_schedule = std::sync::Arc::new(EpochSchedule::without_warmup());
let first_half_clock = std::sync::Arc::new(Clock {
slot: epoch_schedule.slots_per_epoch / 4,
..Clock::default()
});
let second_half_clock = std::sync::Arc::new(Clock {
slot: (epoch_schedule.slots_per_epoch * 3) / 4,
..Clock::default()
});
let mut feature_set = FeatureSet::default();
feature_set.activate(
&feature_set::commission_updates_only_allowed_in_first_half_of_epoch::id(),
1,
);
let signers: HashSet<Pubkey> = vec![withdrawer_pubkey].into_iter().collect();
// Increase commission in first half of epoch -- allowed
assert_eq!(
borrowed_account
.get_state::<VoteStateVersions>()
.unwrap()
.convert_to_current()
.commission,
10
);
assert_matches!(
update_commission(
&mut borrowed_account,
11,
&signers,
&epoch_schedule,
&first_half_clock,
&feature_set
),
Ok(())
);
assert_eq!(
borrowed_account
.get_state::<VoteStateVersions>()
.unwrap()
.convert_to_current()
.commission,
11
);
// Increase commission in second half of epoch -- disallowed
assert_matches!(
update_commission(
&mut borrowed_account,
12,
&signers,
&epoch_schedule,
&second_half_clock,
&feature_set
),
Err(_)
);
assert_eq!(
borrowed_account
.get_state::<VoteStateVersions>()
.unwrap()
.convert_to_current()
.commission,
11
);
// Decrease commission in first half of epoch -- allowed
assert_matches!(
update_commission(
&mut borrowed_account,
10,
&signers,
&epoch_schedule,
&first_half_clock,
&feature_set
),
Ok(())
);
assert_eq!(
borrowed_account
.get_state::<VoteStateVersions>()
.unwrap()
.convert_to_current()
.commission,
10
);
// Decrease commission in second half of epoch -- disallowed because feature_set does not allow it
assert_matches!(
update_commission(
&mut borrowed_account,
9,
&signers,
&epoch_schedule,
&second_half_clock,
&feature_set
),
Err(_)
);
assert_eq!(
borrowed_account
.get_state::<VoteStateVersions>()
.unwrap()
.convert_to_current()
.commission,
10
);
// Decrease commission in second half of epoch -- allowed because feature_set allows it
feature_set.activate(&feature_set::allow_commission_decrease_at_any_time::id(), 1);
assert_matches!(
update_commission(
&mut borrowed_account,
9,
&signers,
&epoch_schedule,
&second_half_clock,
&feature_set
),
Ok(())
);
assert_eq!(
borrowed_account
.get_state::<VoteStateVersions>()
.unwrap()
.convert_to_current()
.commission,
9
);
}
#[test]
fn test_vote_double_lockout_after_expiration() {
let voter_pubkey = solana_sdk::pubkey::new_rand();
let mut vote_state = vote_state_new_for_test(&voter_pubkey);
for i in 0..3 {
process_slot_vote_unchecked(&mut vote_state, i as u64);
}
check_lockouts(&vote_state);
// Expire the third vote (which was a vote for slot 2). The height of the
// vote stack is unchanged, so none of the previous votes should have
// doubled in lockout
process_slot_vote_unchecked(&mut vote_state, (2 + INITIAL_LOCKOUT + 1) as u64);
check_lockouts(&vote_state);
// Vote again, this time the vote stack depth increases, so the votes should
// double for everybody
process_slot_vote_unchecked(&mut vote_state, (2 + INITIAL_LOCKOUT + 2) as u64);
check_lockouts(&vote_state);
// Vote again, this time the vote stack depth increases, so the votes should
// double for everybody
process_slot_vote_unchecked(&mut vote_state, (2 + INITIAL_LOCKOUT + 3) as u64);
check_lockouts(&vote_state);
}
#[test]
fn test_expire_multiple_votes() {
let voter_pubkey = solana_sdk::pubkey::new_rand();
let mut vote_state = vote_state_new_for_test(&voter_pubkey);
for i in 0..3 {
process_slot_vote_unchecked(&mut vote_state, i as u64);
}
assert_eq!(vote_state.votes[0].confirmation_count(), 3);
// Expire the second and third votes
let expire_slot = vote_state.votes[1].slot() + vote_state.votes[1].lockout.lockout() + 1;
process_slot_vote_unchecked(&mut vote_state, expire_slot);
assert_eq!(vote_state.votes.len(), 2);
// Check that the old votes expired
assert_eq!(vote_state.votes[0].slot(), 0);
assert_eq!(vote_state.votes[1].slot(), expire_slot);
// Process one more vote
process_slot_vote_unchecked(&mut vote_state, expire_slot + 1);
// Confirmation count for the older first vote should remain unchanged
assert_eq!(vote_state.votes[0].confirmation_count(), 3);
// The later votes should still have increasing confirmation counts
assert_eq!(vote_state.votes[1].confirmation_count(), 2);
assert_eq!(vote_state.votes[2].confirmation_count(), 1);
}
#[test]
fn test_vote_credits() {
let voter_pubkey = solana_sdk::pubkey::new_rand();
let mut vote_state = vote_state_new_for_test(&voter_pubkey);
for i in 0..MAX_LOCKOUT_HISTORY {
process_slot_vote_unchecked(&mut vote_state, i as u64);
}
assert_eq!(vote_state.credits(), 0);
process_slot_vote_unchecked(&mut vote_state, MAX_LOCKOUT_HISTORY as u64 + 1);
assert_eq!(vote_state.credits(), 1);
process_slot_vote_unchecked(&mut vote_state, MAX_LOCKOUT_HISTORY as u64 + 2);
assert_eq!(vote_state.credits(), 2);
process_slot_vote_unchecked(&mut vote_state, MAX_LOCKOUT_HISTORY as u64 + 3);
assert_eq!(vote_state.credits(), 3);
}
#[test]
fn test_duplicate_vote() {
let voter_pubkey = solana_sdk::pubkey::new_rand();
let mut vote_state = vote_state_new_for_test(&voter_pubkey);
process_slot_vote_unchecked(&mut vote_state, 0);
process_slot_vote_unchecked(&mut vote_state, 1);
process_slot_vote_unchecked(&mut vote_state, 0);
assert_eq!(vote_state.nth_recent_lockout(0).unwrap().slot(), 1);
assert_eq!(vote_state.nth_recent_lockout(1).unwrap().slot(), 0);
assert!(vote_state.nth_recent_lockout(2).is_none());
}
#[test]
fn test_nth_recent_lockout() {
let voter_pubkey = solana_sdk::pubkey::new_rand();
let mut vote_state = vote_state_new_for_test(&voter_pubkey);
for i in 0..MAX_LOCKOUT_HISTORY {
process_slot_vote_unchecked(&mut vote_state, i as u64);
}
for i in 0..(MAX_LOCKOUT_HISTORY - 1) {
assert_eq!(
vote_state.nth_recent_lockout(i).unwrap().slot() as usize,
MAX_LOCKOUT_HISTORY - i - 1,
);
}
assert!(vote_state.nth_recent_lockout(MAX_LOCKOUT_HISTORY).is_none());
}
fn check_lockouts(vote_state: &VoteState) {
for (i, vote) in vote_state.votes.iter().enumerate() {
let num_votes = vote_state
.votes
.len()
.checked_sub(i)
.expect("`i` is less than `vote_state.votes.len()`");
assert_eq!(
vote.lockout.lockout(),
INITIAL_LOCKOUT.pow(num_votes as u32) as u64
);
}
}
fn recent_votes(vote_state: &VoteState) -> Vec<Vote> {
let start = vote_state.votes.len().saturating_sub(MAX_RECENT_VOTES);
(start..vote_state.votes.len())
.map(|i| {
Vote::new(
vec![vote_state.votes.get(i).unwrap().slot()],
Hash::default(),
)
})
.collect()
}
/// check that two accounts with different data can be brought to the same state with one vote submission
#[test]
fn test_process_missed_votes() {
let account_a = solana_sdk::pubkey::new_rand();
let mut vote_state_a = vote_state_new_for_test(&account_a);
let account_b = solana_sdk::pubkey::new_rand();
let mut vote_state_b = vote_state_new_for_test(&account_b);
// process some votes on account a
(0..5).for_each(|i| process_slot_vote_unchecked(&mut vote_state_a, i as u64));
assert_ne!(recent_votes(&vote_state_a), recent_votes(&vote_state_b));
// as long as b has missed less than "NUM_RECENT" votes both accounts should be in sync
let slots = (0u64..MAX_RECENT_VOTES as u64).collect();
let vote = Vote::new(slots, Hash::default());
let slot_hashes: Vec<_> = vote.slots.iter().rev().map(|x| (*x, vote.hash)).collect();
assert_eq!(
process_vote(&mut vote_state_a, &vote, &slot_hashes, 0, 0, true, true),
Ok(())
);
assert_eq!(
process_vote(&mut vote_state_b, &vote, &slot_hashes, 0, 0, true, true),
Ok(())
);
assert_eq!(recent_votes(&vote_state_a), recent_votes(&vote_state_b));
}
#[test]
fn test_process_vote_skips_old_vote() {
let mut vote_state = VoteState::default();
let vote = Vote::new(vec![0], Hash::default());
let slot_hashes: Vec<_> = vec![(0, vote.hash)];
assert_eq!(
process_vote(&mut vote_state, &vote, &slot_hashes, 0, 0, true, true),
Ok(())
);
let recent = recent_votes(&vote_state);
assert_eq!(
process_vote(&mut vote_state, &vote, &slot_hashes, 0, 0, true, true),
Err(VoteError::VoteTooOld)
);
assert_eq!(recent, recent_votes(&vote_state));
}
#[test]
fn test_check_slots_are_valid_vote_empty_slot_hashes() {
let vote_state = VoteState::default();
let vote = Vote::new(vec![0], Hash::default());
assert_eq!(
check_slots_are_valid(&vote_state, &vote.slots, &vote.hash, &[]),
Err(VoteError::VoteTooOld)
);
}
#[test]
fn test_check_slots_are_valid_new_vote() {
let vote_state = VoteState::default();
let vote = Vote::new(vec![0], Hash::default());
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), vote.hash)];
assert_eq!(
check_slots_are_valid(&vote_state, &vote.slots, &vote.hash, &slot_hashes),
Ok(())
);
}
#[test]
fn test_check_slots_are_valid_bad_hash() {
let vote_state = VoteState::default();
let vote = Vote::new(vec![0], Hash::default());
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), hash(vote.hash.as_ref()))];
assert_eq!(
check_slots_are_valid(&vote_state, &vote.slots, &vote.hash, &slot_hashes),
Err(VoteError::SlotHashMismatch)
);
}
#[test]
fn test_check_slots_are_valid_bad_slot() {
let vote_state = VoteState::default();
let vote = Vote::new(vec![1], Hash::default());
let slot_hashes: Vec<_> = vec![(0, vote.hash)];
assert_eq!(
check_slots_are_valid(&vote_state, &vote.slots, &vote.hash, &slot_hashes),
Err(VoteError::SlotsMismatch)
);
}
#[test]
fn test_check_slots_are_valid_duplicate_vote() {
let mut vote_state = VoteState::default();
let vote = Vote::new(vec![0], Hash::default());
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), vote.hash)];
assert_eq!(
process_vote(&mut vote_state, &vote, &slot_hashes, 0, 0, true, true),
Ok(())
);
assert_eq!(
check_slots_are_valid(&vote_state, &vote.slots, &vote.hash, &slot_hashes),
Err(VoteError::VoteTooOld)
);
}
#[test]
fn test_check_slots_are_valid_next_vote() {
let mut vote_state = VoteState::default();
let vote = Vote::new(vec![0], Hash::default());
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), vote.hash)];
assert_eq!(
process_vote(&mut vote_state, &vote, &slot_hashes, 0, 0, true, true),
Ok(())
);
let vote = Vote::new(vec![0, 1], Hash::default());
let slot_hashes: Vec<_> = vec![(1, vote.hash), (0, vote.hash)];
assert_eq!(
check_slots_are_valid(&vote_state, &vote.slots, &vote.hash, &slot_hashes),
Ok(())
);
}
#[test]
fn test_check_slots_are_valid_next_vote_only() {
let mut vote_state = VoteState::default();
let vote = Vote::new(vec![0], Hash::default());
let slot_hashes: Vec<_> = vec![(*vote.slots.last().unwrap(), vote.hash)];
assert_eq!(
process_vote(&mut vote_state, &vote, &slot_hashes, 0, 0, true, true),
Ok(())
);
let vote = Vote::new(vec![1], Hash::default());
let slot_hashes: Vec<_> = vec![(1, vote.hash), (0, vote.hash)];
assert_eq!(
check_slots_are_valid(&vote_state, &vote.slots, &vote.hash, &slot_hashes),
Ok(())
);
}
#[test]
fn test_process_vote_empty_slots() {
let mut vote_state = VoteState::default();
let vote = Vote::new(vec![], Hash::default());
assert_eq!(
process_vote(&mut vote_state, &vote, &[], 0, 0, true, true),
Err(VoteError::EmptySlots)
);
}
pub fn process_new_vote_state_from_lockouts(
vote_state: &mut VoteState,
new_state: VecDeque<Lockout>,
new_root: Option<Slot>,
timestamp: Option<i64>,
epoch: Epoch,
feature_set: Option<&FeatureSet>,
) -> Result<(), VoteError> {
process_new_vote_state(
vote_state,
new_state.into_iter().map(LandedVote::from).collect(),
new_root,
timestamp,
epoch,
0,
feature_set,
)
}
// Test vote credit updates after "one credit per slot" feature is enabled
#[test]
fn test_vote_state_update_increment_credits() {
// Create a new Votestate
let mut vote_state = VoteState::new(&VoteInit::default(), &Clock::default());
// Test data: a sequence of groups of votes to simulate having been cast, after each group a vote
// state update is compared to "normal" vote processing to ensure that credits are earned equally
let test_vote_groups: Vec<Vec<Slot>> = vec![
// Initial set of votes that don't dequeue any slots, so no credits earned
vec![1, 2, 3, 4, 5, 6, 7, 8],
vec![
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31,
],
// Now a single vote which should result in the first root and first credit earned
vec![32],
// Now another vote, should earn one credit
vec![33],
// Two votes in sequence
vec![34, 35],
// 3 votes in sequence
vec![36, 37, 38],
// 30 votes in sequence
vec![
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,
],
// 31 votes in sequence
vec![
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,
],
// Votes with expiry
vec![100, 101, 106, 107, 112, 116, 120, 121, 122, 124],
// More votes with expiry of a large number of votes
vec![200, 201],
vec![
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,
],
vec![227, 228, 229, 230, 231, 232, 233, 234, 235, 236],
];
let feature_set = FeatureSet::default();
for vote_group in test_vote_groups {
// Duplicate vote_state so that the new vote can be applied
let mut vote_state_after_vote = vote_state.clone();
process_vote_unchecked(
&mut vote_state_after_vote,
Vote {
slots: vote_group.clone(),
hash: Hash::new_unique(),
timestamp: None,
},
)
.unwrap();
// Now use the resulting new vote state to perform a vote state update on vote_state
assert_eq!(
process_new_vote_state(
&mut vote_state,
vote_state_after_vote.votes,
vote_state_after_vote.root_slot,
None,
0,
0,
Some(&feature_set)
),
Ok(())
);
// And ensure that the credits earned were the same
assert_eq!(
vote_state.epoch_credits,
vote_state_after_vote.epoch_credits
);
}
}
// Test vote credit updates after "timely vote credits" feature is enabled
#[test]
fn test_timely_credits() {
// Each of the following (Vec<Slot>, Slot, u32) tuples gives a set of slots to cast votes on, a slot in which
// the vote was cast, and the number of credits that should have been earned by the vote account after this
// and all prior votes were cast.
let test_vote_groups: Vec<(Vec<Slot>, Slot, u32)> = vec![
// Initial set of votes that don't dequeue any slots, so no credits earned
(
vec![1, 2, 3, 4, 5, 6, 7, 8],
9,
// root: none, no credits earned
0,
),
(
vec![
9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29, 30, 31,
],
34,
// lockouts full
// root: none, no credits earned
0,
),
// Now a single vote which should result in the first root and first credit earned
(
vec![32],
35,
// root: 1
// when slot 1 was voted on in slot 9, it earned 10 credits
10,
),
// Now another vote, should earn one credit
(
vec![33],
36,
// root: 2
// when slot 2 was voted on in slot 9, it earned 11 credits
10 + 11, // 21
),
// Two votes in sequence
(
vec![34, 35],
37,
// root: 4
// when slots 3 and 4 were voted on in slot 9, they earned 12 and 13 credits
21 + 12 + 13, // 46
),
// 3 votes in sequence
(
vec![36, 37, 38],
39,
// root: 7
// slots 5, 6, and 7 earned 14, 15, and 16 credits when voted in slot 9
46 + 14 + 15 + 16, // 91
),
(
// 30 votes in sequence
vec![
39, 40, 41, 42, 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,
// root: 37
// slot 8 was voted in slot 9, earning 16 credits
// slots 9 - 25 earned 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, and 9 credits when voted in
// slot 34
// slot 26, 27, 28, 29, 30, 31 earned 10, 11, 12, 13, 14, 15 credits when voted in slot 34
// slot 32 earned 15 credits when voted in slot 35
// slot 33 earned 15 credits when voted in slot 36
// slot 34 and 35 earned 15 and 16 credits when voted in slot 37
// slot 36 and 37 earned 15 and 16 credits when voted in slot 39
91 + 16
+ 9 // * 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ 11
+ 12
+ 13
+ 14
+ 15
+ 15
+ 15
+ 15
+ 16
+ 15
+ 16, // 327
),
// 31 votes in sequence
(
vec![
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,
// root: 68
// slot 38 earned 16 credits when voted in slot 39
// slot 39 - 60 earned 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, and 9 credits
// when voted in slot 69
// slot 61, 62, 63, 64, 65, 66, 67, 68 earned 10, 11, 12, 13, 14, 15, 16, and 16 credits when
// voted in slot 69
327 + 16
+ 14 // * 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ 11
+ 12
+ 13
+ 14
+ 15
+ 16
+ 16, // 508
),
// Votes with expiry
(
vec![115, 116, 117, 118, 119, 120, 121, 122, 123, 124],
130,
// root: 74
// slots 96 - 114 expire
// slots 69 - 74 earned 1 credit when voted in slot 100
508 + ((74 - 69) + 1), // 514
),
// More votes with expiry of a large number of votes
(
vec![200, 201],
202,
// root: 74
// slots 119 - 124 expire
514,
),
(
vec![
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,
// root: 95
// slot 75 - 91 earned 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, and 9 credits when voted in
// slot 100
// slot 92, 93, 94, 95 earned 10, 11, 12, 13, credits when voted in slot 100
514 + 9 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13, // 613
),
(
vec![227, 228, 229, 230, 231, 232, 233, 234, 235, 236],
237,
// root: 205
// slot 115 - 118 earned 3, 4, 5, and 6 credits when voted in slot 130
// slot 200 and 201 earned 16 credits when voted in slot 202
// slots 202 - 205 earned 1 credit when voted in slot 227
613 + 3 + 4 + 5 + 6 + 16 + 16 + 1 + 1 + 1 + 1, // 667
),
];
let mut feature_set = FeatureSet::default();
feature_set.activate(&feature_set::timely_vote_credits::id(), 1);
feature_set.activate(&feature_set::deprecate_unused_legacy_vote_plumbing::id(), 1);
// For each vote group, process all vote groups leading up to it and it itself, and ensure that the number of
// credits earned is correct for both regular votes and vote state updates
for i in 0..test_vote_groups.len() {
// Create a new VoteState for vote transaction
let mut vote_state_1 = VoteState::new(&VoteInit::default(), &Clock::default());
// Create a new VoteState for vote state update transaction
let mut vote_state_2 = VoteState::new(&VoteInit::default(), &Clock::default());
test_vote_groups.iter().take(i + 1).for_each(|vote_group| {
let vote = Vote {
slots: vote_group.0.clone(), //vote_group.0 is the set of slots to cast votes on
hash: Hash::new_unique(),
timestamp: None,
};
let slot_hashes: Vec<_> =
vote.slots.iter().rev().map(|x| (*x, vote.hash)).collect();
assert_eq!(
process_vote(
&mut vote_state_1,
&vote,
&slot_hashes,
0,
vote_group.1, // vote_group.1 is the slot in which the vote was cast
true,
true
),
Ok(())
);
assert_eq!(
process_new_vote_state(
&mut vote_state_2,
vote_state_1.votes.clone(),
vote_state_1.root_slot,
None,
0,
vote_group.1, // vote_group.1 is the slot in which the vote was cast
Some(&feature_set)
),
Ok(())
);
});
// Ensure that the credits earned is correct for both vote states
let vote_group = &test_vote_groups[i];
assert_eq!(vote_state_1.credits(), vote_group.2 as u64); // vote_group.2 is the expected number of credits
assert_eq!(vote_state_2.credits(), vote_group.2 as u64); // vote_group.2 is the expected number of credits
}
}
#[test]
fn test_retroactive_voting_timely_credits() {
// Each of the following (Vec<(Slot, int)>, Slot, Option<Slot>, u32) tuples gives the following data:
// Vec<(Slot, int)> -- the set of slots and confirmation_counts that is the VoteStateUpdate
// Slot -- the slot in which the VoteStateUpdate occurred
// Option<Slot> -- the root after processing the VoteStateUpdate
// u32 -- the credits after processing the VoteStateUpdate
#[allow(clippy::type_complexity)]
let test_vote_state_updates: Vec<(Vec<(Slot, u32)>, Slot, Option<Slot>, u32)> = vec![
// VoteStateUpdate to set initial vote state
(
vec![(7, 4), (8, 3), (9, 2), (10, 1)],
11,
// root: none
None,
// no credits earned
0,
),
// VoteStateUpdate to include the missing slots *prior to previously included slots*
(
vec![
(1, 10),
(2, 9),
(3, 8),
(4, 7),
(5, 6),
(6, 5),
(7, 4),
(8, 3),
(9, 2),
(10, 1),
],
12,
// root: none
None,
// no credits earned
0,
),
// Now a single VoteStateUpdate which roots all of the slots from 1 - 10
(
vec![
(11, 31),
(12, 30),
(13, 29),
(14, 28),
(15, 27),
(16, 26),
(17, 25),
(18, 24),
(19, 23),
(20, 22),
(21, 21),
(22, 20),
(23, 19),
(24, 18),
(25, 17),
(26, 16),
(27, 15),
(28, 14),
(29, 13),
(30, 12),
(31, 11),
(32, 10),
(33, 9),
(34, 8),
(35, 7),
(36, 6),
(37, 5),
(38, 4),
(39, 3),
(40, 2),
(41, 1),
],
42,
// root: 10
Some(10),
// when slots 1 - 6 were voted on in slot 12, they earned 7, 8, 9, 10, 11, and 12 credits
// when slots 7 - 10 were voted on in slot 11, they earned 14, 15, 16, and 16 credits
7 + 8 + 9 + 10 + 11 + 12 + 14 + 15 + 16 + 16,
),
];
let mut feature_set = FeatureSet::default();
feature_set.activate(&feature_set::timely_vote_credits::id(), 1);
feature_set.activate(&feature_set::deprecate_unused_legacy_vote_plumbing::id(), 1);
// Retroactive voting is only possible with VoteStateUpdate transactions, which is why Vote transactions are
// not tested here
// Initial vote state
let mut vote_state = VoteState::new(&VoteInit::default(), &Clock::default());
// Process the vote state updates in sequence and ensure that the credits earned after each is processed is
// correct
test_vote_state_updates
.iter()
.for_each(|vote_state_update| {
let new_state = vote_state_update
.0 // vote_state_update.0 is the set of slots and confirmation_counts that is the VoteStateUpdate
.iter()
.map(|(slot, confirmation_count)| LandedVote {
latency: 0,
lockout: Lockout::new_with_confirmation_count(*slot, *confirmation_count),
})
.collect::<VecDeque<LandedVote>>();
assert_eq!(
process_new_vote_state(
&mut vote_state,
new_state,
vote_state_update.2, // vote_state_update.2 is root after processing the VoteStateUpdate
None,
0,
vote_state_update.1, // vote_state_update.1 is the slot in which the VoteStateUpdate occurred
Some(&feature_set)
),
Ok(())
);
// Ensure that the credits earned is correct
assert_eq!(vote_state.credits(), vote_state_update.3 as u64);
});
}
#[test]
fn test_process_new_vote_too_many_votes() {
let mut vote_state1 = VoteState::default();
let bad_votes: VecDeque<Lockout> = (0..=MAX_LOCKOUT_HISTORY)
.map(|slot| {
Lockout::new_with_confirmation_count(
slot as Slot,
(MAX_LOCKOUT_HISTORY - slot + 1) as u32,
)
})
.collect();
let current_epoch = vote_state1.current_epoch();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::TooManyVotes)
);
}
#[test]
fn test_process_new_vote_state_root_rollback() {
let mut vote_state1 = VoteState::default();
for i in 0..MAX_LOCKOUT_HISTORY + 2 {
process_slot_vote_unchecked(&mut vote_state1, i as Slot);
}
assert_eq!(vote_state1.root_slot.unwrap(), 1);
// Update vote_state2 with a higher slot so that `process_new_vote_state`
// doesn't panic.
let mut vote_state2 = vote_state1.clone();
process_slot_vote_unchecked(&mut vote_state2, MAX_LOCKOUT_HISTORY as Slot + 3);
// Trying to set a lesser root should error
let lesser_root = Some(0);
let current_epoch = vote_state2.current_epoch();
assert_eq!(
process_new_vote_state(
&mut vote_state1,
vote_state2.votes.clone(),
lesser_root,
None,
current_epoch,
0,
None,
),
Err(VoteError::RootRollBack)
);
// Trying to set root to None should error
let none_root = None;
assert_eq!(
process_new_vote_state(
&mut vote_state1,
vote_state2.votes.clone(),
none_root,
None,
current_epoch,
0,
None,
),
Err(VoteError::RootRollBack)
);
}
fn process_new_vote_state_replaced_root_vote_credits(
feature_set: &FeatureSet,
expected_credits: u64,
) {
let mut vote_state1 = VoteState::default();
// Initial vote state: as if 31 votes had occurred on slots 0 - 30 (inclusive)
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
(0..MAX_LOCKOUT_HISTORY)
.enumerate()
.map(|(index, slot)| Lockout::new_with_confirmation_count(
slot as Slot,
(MAX_LOCKOUT_HISTORY.checked_sub(index).unwrap()) as u32
))
.collect(),
None,
None,
0,
Some(feature_set),
),
Ok(())
);
// Now vote as if new votes on slots 31 and 32 had occurred, yielding a new Root of 1
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
(2..(MAX_LOCKOUT_HISTORY.checked_add(2).unwrap()))
.enumerate()
.map(|(index, slot)| Lockout::new_with_confirmation_count(
slot as Slot,
(MAX_LOCKOUT_HISTORY.checked_sub(index).unwrap()) as u32
))
.collect(),
Some(1),
None,
0,
Some(feature_set),
),
Ok(())
);
// Vote credits should be 2, since two voted-on slots were "popped off the back" of the tower
assert_eq!(vote_state1.credits(), 2);
// Create a new vote state that represents the validator having not voted for a long time, then voting on
// slots 10001 through 10032 (inclusive) with an entirely new root of 10000 that was never previously voted
// on. This is valid because a vote state can include a root that it never voted on (if it votes after a very
// long delinquency, the new votes will have a root much newer than its most recently voted slot).
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
(10001..(MAX_LOCKOUT_HISTORY.checked_add(10001).unwrap()))
.enumerate()
.map(|(index, slot)| Lockout::new_with_confirmation_count(
slot as Slot,
(MAX_LOCKOUT_HISTORY.checked_sub(index).unwrap()) as u32
))
.collect(),
Some(10000),
None,
0,
Some(feature_set),
),
Ok(())
);
// The vote is valid, but no vote credits should be awarded because although there is a new root, it does not
// represent a slot previously voted on.
assert_eq!(vote_state1.credits(), expected_credits)
}
#[test]
fn test_process_new_vote_state_replaced_root_vote_credits() {
let mut feature_set = FeatureSet::default();
// Always use allow_votes_to_directly_update_vote_state feature because VoteStateUpdate is being tested
feature_set.activate(
&feature_set::allow_votes_to_directly_update_vote_state::id(),
1,
);
// Test without the timely_vote_credits feature. The expected credits here of 34 is *incorrect* but is what
// is expected using vote_state_update_credit_per_dequeue. With this feature, the credits earned will be
// calculated as:
// 2 (from initial vote state)
// + 31 (for votes which were "popped off of the back of the tower" by the new vote
// + 1 (just because there is a new root, even though it was never voted on -- this is the flaw)
feature_set.activate(&feature_set::vote_state_update_credit_per_dequeue::id(), 1);
process_new_vote_state_replaced_root_vote_credits(&feature_set, 34);
// Now test using the timely_vote_credits feature. The expected credits here of 33 is *correct*. With
// this feature, the credits earned will be calculated as:
// 2 (from initial vote state)
// + 31 (for votes which were "popped off of the back of the tower" by the new vote)
feature_set.activate(&feature_set::timely_vote_credits::id(), 1);
process_new_vote_state_replaced_root_vote_credits(&feature_set, 33);
}
#[test]
fn test_process_new_vote_state_zero_confirmations() {
let mut vote_state1 = VoteState::default();
let current_epoch = vote_state1.current_epoch();
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(0, 0),
Lockout::new_with_confirmation_count(1, 1),
]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::ZeroConfirmations)
);
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(0, 2),
Lockout::new_with_confirmation_count(1, 0),
]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::ZeroConfirmations)
);
}
#[test]
fn test_process_new_vote_state_confirmations_too_large() {
let mut vote_state1 = VoteState::default();
let current_epoch = vote_state1.current_epoch();
let good_votes: VecDeque<Lockout> = vec![Lockout::new_with_confirmation_count(
0,
MAX_LOCKOUT_HISTORY as u32,
)]
.into_iter()
.collect();
process_new_vote_state_from_lockouts(
&mut vote_state1,
good_votes,
None,
None,
current_epoch,
None,
)
.unwrap();
let mut vote_state1 = VoteState::default();
let bad_votes: VecDeque<Lockout> = vec![Lockout::new_with_confirmation_count(
0,
MAX_LOCKOUT_HISTORY as u32 + 1,
)]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::ConfirmationTooLarge)
);
}
#[test]
fn test_process_new_vote_state_slot_smaller_than_root() {
let mut vote_state1 = VoteState::default();
let current_epoch = vote_state1.current_epoch();
let root_slot = 5;
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(root_slot, 2),
Lockout::new_with_confirmation_count(root_slot + 1, 1),
]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
Some(root_slot),
None,
current_epoch,
None,
),
Err(VoteError::SlotSmallerThanRoot)
);
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(root_slot - 1, 2),
Lockout::new_with_confirmation_count(root_slot + 1, 1),
]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
Some(root_slot),
None,
current_epoch,
None,
),
Err(VoteError::SlotSmallerThanRoot)
);
}
#[test]
fn test_process_new_vote_state_slots_not_ordered() {
let mut vote_state1 = VoteState::default();
let current_epoch = vote_state1.current_epoch();
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(1, 2),
Lockout::new_with_confirmation_count(0, 1),
]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::SlotsNotOrdered)
);
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(1, 2),
Lockout::new_with_confirmation_count(1, 1),
]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::SlotsNotOrdered)
);
}
#[test]
fn test_process_new_vote_state_confirmations_not_ordered() {
let mut vote_state1 = VoteState::default();
let current_epoch = vote_state1.current_epoch();
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(0, 1),
Lockout::new_with_confirmation_count(1, 2),
]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::ConfirmationsNotOrdered)
);
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(0, 1),
Lockout::new_with_confirmation_count(1, 1),
]
.into_iter()
.collect();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::ConfirmationsNotOrdered)
);
}
#[test]
fn test_process_new_vote_state_new_vote_state_lockout_mismatch() {
let mut vote_state1 = VoteState::default();
let current_epoch = vote_state1.current_epoch();
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(0, 2),
Lockout::new_with_confirmation_count(7, 1),
]
.into_iter()
.collect();
// Slot 7 should have expired slot 0
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
None,
None,
current_epoch,
None
),
Err(VoteError::NewVoteStateLockoutMismatch)
);
}
#[test]
fn test_process_new_vote_state_confirmation_rollback() {
let mut vote_state1 = VoteState::default();
let current_epoch = vote_state1.current_epoch();
let votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(0, 4),
Lockout::new_with_confirmation_count(1, 3),
]
.into_iter()
.collect();
process_new_vote_state_from_lockouts(
&mut vote_state1,
votes,
None,
None,
current_epoch,
None,
)
.unwrap();
let votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(0, 4),
// Confirmation count lowered illegally
Lockout::new_with_confirmation_count(1, 2),
Lockout::new_with_confirmation_count(2, 1),
]
.into_iter()
.collect();
// Should error because newer vote state should not have lower confirmation the same slot
// 1
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
votes,
None,
None,
current_epoch,
None
),
Err(VoteError::ConfirmationRollBack)
);
}
#[test]
fn test_process_new_vote_state_root_progress() {
let mut vote_state1 = VoteState::default();
for i in 0..MAX_LOCKOUT_HISTORY {
process_slot_vote_unchecked(&mut vote_state1, i as u64);
}
assert!(vote_state1.root_slot.is_none());
let mut vote_state2 = vote_state1.clone();
// 1) Try to update `vote_state1` with no root,
// to `vote_state2`, which has a new root, should succeed.
//
// 2) Then try to update`vote_state1` with an existing root,
// to `vote_state2`, which has a newer root, which
// should succeed.
for new_vote in MAX_LOCKOUT_HISTORY + 1..=MAX_LOCKOUT_HISTORY + 2 {
process_slot_vote_unchecked(&mut vote_state2, new_vote as Slot);
assert_ne!(vote_state1.root_slot, vote_state2.root_slot);
process_new_vote_state(
&mut vote_state1,
vote_state2.votes.clone(),
vote_state2.root_slot,
None,
vote_state2.current_epoch(),
0,
None,
)
.unwrap();
assert_eq!(vote_state1, vote_state2);
}
}
#[test]
fn test_process_new_vote_state_same_slot_but_not_common_ancestor() {
// It might be possible that during the switch from old vote instructions
// to new vote instructions, new_state contains votes for slots LESS
// than the current state, for instance:
//
// Current on-chain state: 1, 5
// New state: 1, 2 (lockout: 4), 3, 5, 7
//
// Imagine the validator made two of these votes:
// 1) The first vote {1, 2, 3} didn't land in the old state, but didn't
// land on chain
// 2) A second vote {1, 2, 5} was then submitted, which landed
//
//
// 2 is not popped off in the local tower because 3 doubled the lockout.
// However, 3 did not land in the on-chain state, so the vote {1, 2, 6}
// will immediately pop off 2.
// Construct on-chain vote state
let mut vote_state1 = VoteState::default();
process_slot_votes_unchecked(&mut vote_state1, &[1, 2, 5]);
assert_eq!(
vote_state1
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![1, 5]
);
// Construct local tower state
let mut vote_state2 = VoteState::default();
process_slot_votes_unchecked(&mut vote_state2, &[1, 2, 3, 5, 7]);
assert_eq!(
vote_state2
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![1, 2, 3, 5, 7]
);
// See that on-chain vote state can update properly
process_new_vote_state(
&mut vote_state1,
vote_state2.votes.clone(),
vote_state2.root_slot,
None,
vote_state2.current_epoch(),
0,
None,
)
.unwrap();
assert_eq!(vote_state1, vote_state2);
}
#[test]
fn test_process_new_vote_state_lockout_violation() {
// Construct on-chain vote state
let mut vote_state1 = VoteState::default();
process_slot_votes_unchecked(&mut vote_state1, &[1, 2, 4, 5]);
assert_eq!(
vote_state1
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![1, 2, 4, 5]
);
// Construct conflicting tower state. Vote 4 is missing,
// but 5 should not have popped off vote 4.
let mut vote_state2 = VoteState::default();
process_slot_votes_unchecked(&mut vote_state2, &[1, 2, 3, 5, 7]);
assert_eq!(
vote_state2
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![1, 2, 3, 5, 7]
);
// See that on-chain vote state can update properly
assert_eq!(
process_new_vote_state(
&mut vote_state1,
vote_state2.votes.clone(),
vote_state2.root_slot,
None,
vote_state2.current_epoch(),
0,
None
),
Err(VoteError::LockoutConflict)
);
}
#[test]
fn test_process_new_vote_state_lockout_violation2() {
// Construct on-chain vote state
let mut vote_state1 = VoteState::default();
process_slot_votes_unchecked(&mut vote_state1, &[1, 2, 5, 6, 7]);
assert_eq!(
vote_state1
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![1, 5, 6, 7]
);
// Construct a new vote state. Violates on-chain state because 8
// should not have popped off 7
let mut vote_state2 = VoteState::default();
process_slot_votes_unchecked(&mut vote_state2, &[1, 2, 3, 5, 6, 8]);
assert_eq!(
vote_state2
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![1, 2, 3, 5, 6, 8]
);
// Both vote states contain `5`, but `5` is not part of the common prefix
// of both vote states. However, the violation should still be detected.
assert_eq!(
process_new_vote_state(
&mut vote_state1,
vote_state2.votes.clone(),
vote_state2.root_slot,
None,
vote_state2.current_epoch(),
0,
None
),
Err(VoteError::LockoutConflict)
);
}
#[test]
fn test_process_new_vote_state_expired_ancestor_not_removed() {
// Construct on-chain vote state
let mut vote_state1 = VoteState::default();
process_slot_votes_unchecked(&mut vote_state1, &[1, 2, 3, 9]);
assert_eq!(
vote_state1
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![1, 9]
);
// Example: {1: lockout 8, 9: lockout 2}, vote on 10 will not pop off 1
// because 9 is not popped off yet
let mut vote_state2 = vote_state1.clone();
process_slot_vote_unchecked(&mut vote_state2, 10);
// Slot 1 has been expired by 10, but is kept alive by its descendant
// 9 which has not been expired yet.
assert_eq!(vote_state2.votes[0].slot(), 1);
assert_eq!(vote_state2.votes[0].lockout.last_locked_out_slot(), 9);
assert_eq!(
vote_state2
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![1, 9, 10]
);
// Should be able to update vote_state1
process_new_vote_state(
&mut vote_state1,
vote_state2.votes.clone(),
vote_state2.root_slot,
None,
vote_state2.current_epoch(),
0,
None,
)
.unwrap();
assert_eq!(vote_state1, vote_state2,);
}
#[test]
fn test_process_new_vote_current_state_contains_bigger_slots() {
let mut vote_state1 = VoteState::default();
process_slot_votes_unchecked(&mut vote_state1, &[6, 7, 8]);
assert_eq!(
vote_state1
.votes
.iter()
.map(|vote| vote.slot())
.collect::<Vec<Slot>>(),
vec![6, 7, 8]
);
// Try to process something with lockout violations
let bad_votes: VecDeque<Lockout> = vec![
Lockout::new_with_confirmation_count(2, 5),
// Slot 14 could not have popped off slot 6 yet
Lockout::new_with_confirmation_count(14, 1),
]
.into_iter()
.collect();
let root = Some(1);
let current_epoch = vote_state1.current_epoch();
assert_eq!(
process_new_vote_state_from_lockouts(
&mut vote_state1,
bad_votes,
root,
None,
current_epoch,
None
),
Err(VoteError::LockoutConflict)
);
let good_votes: VecDeque<LandedVote> = vec![
Lockout::new_with_confirmation_count(2, 5).into(),
Lockout::new_with_confirmation_count(15, 1).into(),
]
.into_iter()
.collect();
let current_epoch = vote_state1.current_epoch();
process_new_vote_state(
&mut vote_state1,
good_votes.clone(),
root,
None,
current_epoch,
0,
None,
)
.unwrap();
assert_eq!(vote_state1.votes, good_votes);
}
#[test]
fn test_filter_old_votes() {
let mut vote_state = VoteState::default();
let old_vote_slot = 1;
let vote = Vote::new(vec![old_vote_slot], Hash::default());
// Vote with all slots that are all older than the SlotHashes history should
// error with `VotesTooOldAllFiltered`
let slot_hashes = vec![(3, Hash::new_unique()), (2, Hash::new_unique())];
assert_eq!(
process_vote(&mut vote_state, &vote, &slot_hashes, 0, 0, true, true),
Err(VoteError::VotesTooOldAllFiltered)
);
// Vote with only some slots older than the SlotHashes history should
// filter out those older slots
let vote_slot = 2;
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let vote = Vote::new(vec![old_vote_slot, vote_slot], vote_slot_hash);
process_vote(&mut vote_state, &vote, &slot_hashes, 0, 0, true, true).unwrap();
assert_eq!(
vote_state
.votes
.into_iter()
.map(|vote| vote.lockout)
.collect::<Vec<Lockout>>(),
vec![Lockout::new_with_confirmation_count(vote_slot, 1)]
);
}
fn build_slot_hashes(slots: Vec<Slot>) -> Vec<(Slot, Hash)> {
slots
.iter()
.rev()
.map(|x| (*x, Hash::new_unique()))
.collect()
}
fn build_vote_state(vote_slots: Vec<Slot>, slot_hashes: &[(Slot, Hash)]) -> VoteState {
let mut vote_state = VoteState::default();
if !vote_slots.is_empty() {
let vote_hash = slot_hashes
.iter()
.find(|(slot, _hash)| slot == vote_slots.last().unwrap())
.unwrap()
.1;
let vote = Vote::new(vote_slots, vote_hash);
process_vote_unfiltered(
&mut vote_state,
&vote.slots,
&vote,
slot_hashes,
0,
0,
true,
true,
)
.unwrap();
}
vote_state
}
#[test]
fn test_check_update_vote_state_empty() {
let empty_slot_hashes = build_slot_hashes(vec![]);
let empty_vote_state = build_vote_state(vec![], &empty_slot_hashes);
// Test with empty vote state update, should return EmptySlots error
let mut vote_state_update = VoteStateUpdate::from(vec![]);
assert_eq!(
check_update_vote_state_slots_are_valid(
&empty_vote_state,
&mut vote_state_update,
&empty_slot_hashes,
),
Err(VoteError::EmptySlots),
);
// Test with non-empty vote state update, should return SlotsMismatch since nothing exists in SlotHashes
let mut vote_state_update = VoteStateUpdate::from(vec![(0, 1)]);
assert_eq!(
check_update_vote_state_slots_are_valid(
&empty_vote_state,
&mut vote_state_update,
&empty_slot_hashes,
),
Err(VoteError::SlotsMismatch),
);
}
#[test]
fn test_check_update_vote_state_too_old() {
let slot_hashes = build_slot_hashes(vec![1, 2, 3, 4]);
let latest_vote = 4;
let vote_state = build_vote_state(vec![1, 2, 3, latest_vote], &slot_hashes);
// Test with a vote for a slot less than the latest vote in the vote_state,
// should return error `VoteTooOld`
let mut vote_state_update = VoteStateUpdate::from(vec![(latest_vote, 1)]);
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::VoteTooOld),
);
// Test with a vote state update where the latest slot `X` in the update is
// 1) Less than the earliest slot in slot_hashes history, AND
// 2) `X` > latest_vote
let earliest_slot_in_history = latest_vote + 2;
let slot_hashes = build_slot_hashes(vec![earliest_slot_in_history]);
let mut vote_state_update = VoteStateUpdate::from(vec![(earliest_slot_in_history - 1, 1)]);
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::VoteTooOld),
);
}
fn run_test_check_update_vote_state_older_than_history_root(
earliest_slot_in_history: Slot,
current_vote_state_slots: Vec<Slot>,
current_vote_state_root: Option<Slot>,
vote_state_update_slots_and_lockouts: Vec<(Slot, u32)>,
vote_state_update_root: Slot,
expected_root: Option<Slot>,
expected_vote_state: Vec<Lockout>,
) {
assert!(vote_state_update_root < earliest_slot_in_history);
assert_eq!(
expected_root,
current_vote_state_slots
.iter()
.rev()
.find(|slot| **slot <= vote_state_update_root)
.cloned()
);
let latest_slot_in_history = vote_state_update_slots_and_lockouts
.last()
.unwrap()
.0
.max(earliest_slot_in_history);
let mut slot_hashes = build_slot_hashes(
(current_vote_state_slots.first().copied().unwrap_or(0)..=latest_slot_in_history)
.collect::<Vec<Slot>>(),
);
let mut vote_state = build_vote_state(current_vote_state_slots, &slot_hashes);
vote_state.root_slot = current_vote_state_root;
slot_hashes.retain(|slot| slot.0 >= earliest_slot_in_history);
assert!(!vote_state_update_slots_and_lockouts.is_empty());
let vote_state_update_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_state_update_slots_and_lockouts.last().unwrap().0)
.unwrap()
.1;
// Test with a `vote_state_update` where the root is less than `earliest_slot_in_history`.
// Root slot in the `vote_state_update` should be updated to match the root slot in the
// current vote state
let mut vote_state_update = VoteStateUpdate::from(vote_state_update_slots_and_lockouts);
vote_state_update.hash = vote_state_update_hash;
vote_state_update.root = Some(vote_state_update_root);
check_update_vote_state_slots_are_valid(&vote_state, &mut vote_state_update, &slot_hashes)
.unwrap();
assert_eq!(vote_state_update.root, expected_root);
// The proposed root slot should become the biggest slot in the current vote state less than
// `earliest_slot_in_history`.
assert!(do_process_vote_state_update(
&mut vote_state,
&slot_hashes,
0,
0,
vote_state_update.clone(),
Some(&FeatureSet::all_enabled()),
)
.is_ok());
assert_eq!(vote_state.root_slot, expected_root);
assert_eq!(
vote_state
.votes
.into_iter()
.map(|vote| vote.lockout)
.collect::<Vec<Lockout>>(),
expected_vote_state,
);
}
#[test]
fn test_check_update_vote_state_older_than_history_root() {
// Test when `vote_state_update_root` is in `current_vote_state_slots` but it's not the latest
// slot
let earliest_slot_in_history = 5;
let current_vote_state_slots: Vec<Slot> = vec![1, 2, 3, 4];
let current_vote_state_root = None;
let vote_state_update_slots_and_lockouts = vec![(5, 1)];
let vote_state_update_root = 4;
let expected_root = Some(4);
let expected_vote_state = vec![Lockout::new_with_confirmation_count(5, 1)];
run_test_check_update_vote_state_older_than_history_root(
earliest_slot_in_history,
current_vote_state_slots,
current_vote_state_root,
vote_state_update_slots_and_lockouts,
vote_state_update_root,
expected_root,
expected_vote_state,
);
// Test when `vote_state_update_root` is in `current_vote_state_slots` but it's not the latest
// slot and the `current_vote_state_root.is_some()`.
let earliest_slot_in_history = 5;
let current_vote_state_slots: Vec<Slot> = vec![1, 2, 3, 4];
let current_vote_state_root = Some(0);
let vote_state_update_slots_and_lockouts = vec![(5, 1)];
let vote_state_update_root = 4;
let expected_root = Some(4);
let expected_vote_state = vec![Lockout::new_with_confirmation_count(5, 1)];
run_test_check_update_vote_state_older_than_history_root(
earliest_slot_in_history,
current_vote_state_slots,
current_vote_state_root,
vote_state_update_slots_and_lockouts,
vote_state_update_root,
expected_root,
expected_vote_state,
);
// Test when `vote_state_update_root` is in `current_vote_state_slots` but it's not the latest
// slot
let earliest_slot_in_history = 5;
let current_vote_state_slots: Vec<Slot> = vec![1, 2, 3, 4];
let current_vote_state_root = Some(0);
let vote_state_update_slots_and_lockouts = vec![(4, 2), (5, 1)];
let vote_state_update_root = 3;
let expected_root = Some(3);
let expected_vote_state = vec![
Lockout::new_with_confirmation_count(4, 2),
Lockout::new_with_confirmation_count(5, 1),
];
run_test_check_update_vote_state_older_than_history_root(
earliest_slot_in_history,
current_vote_state_slots,
current_vote_state_root,
vote_state_update_slots_and_lockouts,
vote_state_update_root,
expected_root,
expected_vote_state,
);
// Test when `vote_state_update_root` is not in `current_vote_state_slots`
let earliest_slot_in_history = 5;
let current_vote_state_slots: Vec<Slot> = vec![1, 2, 4];
let current_vote_state_root = Some(0);
let vote_state_update_slots_and_lockouts = vec![(4, 2), (5, 1)];
let vote_state_update_root = 3;
let expected_root = Some(2);
let expected_vote_state = vec![
Lockout::new_with_confirmation_count(4, 2),
Lockout::new_with_confirmation_count(5, 1),
];
run_test_check_update_vote_state_older_than_history_root(
earliest_slot_in_history,
current_vote_state_slots,
current_vote_state_root,
vote_state_update_slots_and_lockouts,
vote_state_update_root,
expected_root,
expected_vote_state,
);
// Test when the `vote_state_update_root` is smaller than all the slots in
// `current_vote_state_slots`, no roots should be set.
let earliest_slot_in_history = 4;
let current_vote_state_slots: Vec<Slot> = vec![3, 4];
let current_vote_state_root = None;
let vote_state_update_slots_and_lockouts = vec![(3, 3), (4, 2), (5, 1)];
let vote_state_update_root = 2;
let expected_root = None;
let expected_vote_state = vec![
Lockout::new_with_confirmation_count(3, 3),
Lockout::new_with_confirmation_count(4, 2),
Lockout::new_with_confirmation_count(5, 1),
];
run_test_check_update_vote_state_older_than_history_root(
earliest_slot_in_history,
current_vote_state_slots,
current_vote_state_root,
vote_state_update_slots_and_lockouts,
vote_state_update_root,
expected_root,
expected_vote_state,
);
// Test when `current_vote_state_slots` is empty, no roots should be set
let earliest_slot_in_history = 4;
let current_vote_state_slots: Vec<Slot> = vec![];
let current_vote_state_root = None;
let vote_state_update_slots_and_lockouts = vec![(5, 1)];
let vote_state_update_root = 2;
let expected_root = None;
let expected_vote_state = vec![Lockout::new_with_confirmation_count(5, 1)];
run_test_check_update_vote_state_older_than_history_root(
earliest_slot_in_history,
current_vote_state_slots,
current_vote_state_root,
vote_state_update_slots_and_lockouts,
vote_state_update_root,
expected_root,
expected_vote_state,
);
}
#[test]
fn test_check_update_vote_state_slots_not_ordered() {
let slot_hashes = build_slot_hashes(vec![1, 2, 3, 4]);
let vote_state = build_vote_state(vec![1], &slot_hashes);
// Test with a `vote_state_update` where the slots are out of order
let vote_slot = 3;
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let mut vote_state_update = VoteStateUpdate::from(vec![(2, 2), (1, 3), (vote_slot, 1)]);
vote_state_update.hash = vote_slot_hash;
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::SlotsNotOrdered),
);
// Test with a `vote_state_update` where there are multiples of the same slot
let mut vote_state_update = VoteStateUpdate::from(vec![(2, 2), (2, 2), (vote_slot, 1)]);
vote_state_update.hash = vote_slot_hash;
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::SlotsNotOrdered),
);
}
#[test]
fn test_check_update_vote_state_older_than_history_slots_filtered() {
let slot_hashes = build_slot_hashes(vec![1, 2, 3, 4]);
let mut vote_state = build_vote_state(vec![1, 2, 3, 4], &slot_hashes);
// Test with a `vote_state_update` where there:
// 1) Exists a slot less than `earliest_slot_in_history`
// 2) This slot does not exist in the vote state already
// This slot should be filtered out
let earliest_slot_in_history = 11;
let slot_hashes = build_slot_hashes(vec![earliest_slot_in_history, 12, 13, 14]);
let vote_slot = 12;
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let missing_older_than_history_slot = earliest_slot_in_history - 1;
let mut vote_state_update = VoteStateUpdate::from(vec![
(1, 4),
(missing_older_than_history_slot, 2),
(vote_slot, 3),
]);
vote_state_update.hash = vote_slot_hash;
check_update_vote_state_slots_are_valid(&vote_state, &mut vote_state_update, &slot_hashes)
.unwrap();
// Check the earlier slot was filtered out
assert_eq!(
vote_state_update
.clone()
.lockouts
.into_iter()
.collect::<Vec<Lockout>>(),
vec![
Lockout::new_with_confirmation_count(1, 4),
Lockout::new_with_confirmation_count(vote_slot, 3)
]
);
assert!(do_process_vote_state_update(
&mut vote_state,
&slot_hashes,
0,
0,
vote_state_update,
Some(&FeatureSet::all_enabled()),
)
.is_ok());
}
#[test]
fn test_check_update_vote_state_older_than_history_slots_not_filtered() {
let slot_hashes = build_slot_hashes(vec![4]);
let mut vote_state = build_vote_state(vec![4], &slot_hashes);
// Test with a `vote_state_update` where there:
// 1) Exists a slot less than `earliest_slot_in_history`
// 2) This slot exists in the vote state already
// This slot should *NOT* be filtered out
let earliest_slot_in_history = 11;
let slot_hashes = build_slot_hashes(vec![earliest_slot_in_history, 12, 13, 14]);
let vote_slot = 12;
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let existing_older_than_history_slot = 4;
let mut vote_state_update =
VoteStateUpdate::from(vec![(existing_older_than_history_slot, 3), (vote_slot, 2)]);
vote_state_update.hash = vote_slot_hash;
check_update_vote_state_slots_are_valid(&vote_state, &mut vote_state_update, &slot_hashes)
.unwrap();
// Check the earlier slot was *NOT* filtered out
assert_eq!(vote_state_update.lockouts.len(), 2);
assert_eq!(
vote_state_update
.clone()
.lockouts
.into_iter()
.collect::<Vec<Lockout>>(),
vec![
Lockout::new_with_confirmation_count(existing_older_than_history_slot, 3),
Lockout::new_with_confirmation_count(vote_slot, 2)
]
);
assert!(do_process_vote_state_update(
&mut vote_state,
&slot_hashes,
0,
0,
vote_state_update,
Some(&FeatureSet::all_enabled()),
)
.is_ok());
}
#[test]
fn test_check_update_vote_state_older_than_history_slots_filtered_and_not_filtered() {
let slot_hashes = build_slot_hashes(vec![6]);
let mut vote_state = build_vote_state(vec![6], &slot_hashes);
// Test with a `vote_state_update` where there exists both a slot:
// 1) Less than `earliest_slot_in_history`
// 2) This slot exists in the vote state already
// which should not be filtered
//
// AND a slot that
//
// 1) Less than `earliest_slot_in_history`
// 2) This slot does not exist in the vote state already
// which should be filtered
let earliest_slot_in_history = 11;
let slot_hashes = build_slot_hashes(vec![earliest_slot_in_history, 12, 13, 14]);
let vote_slot = 14;
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let missing_older_than_history_slot = 4;
let existing_older_than_history_slot = 6;
let mut vote_state_update = VoteStateUpdate::from(vec![
(missing_older_than_history_slot, 4),
(existing_older_than_history_slot, 3),
(12, 2),
(vote_slot, 1),
]);
vote_state_update.hash = vote_slot_hash;
check_update_vote_state_slots_are_valid(&vote_state, &mut vote_state_update, &slot_hashes)
.unwrap();
assert_eq!(vote_state_update.lockouts.len(), 3);
assert_eq!(
vote_state_update
.clone()
.lockouts
.into_iter()
.collect::<Vec<Lockout>>(),
vec![
Lockout::new_with_confirmation_count(existing_older_than_history_slot, 3),
Lockout::new_with_confirmation_count(12, 2),
Lockout::new_with_confirmation_count(vote_slot, 1)
]
);
assert!(do_process_vote_state_update(
&mut vote_state,
&slot_hashes,
0,
0,
vote_state_update,
Some(&FeatureSet::all_enabled()),
)
.is_ok());
}
#[test]
fn test_check_update_vote_state_slot_not_on_fork() {
let slot_hashes = build_slot_hashes(vec![2, 4, 6, 8]);
let vote_state = build_vote_state(vec![2, 4, 6], &slot_hashes);
// Test with a `vote_state_update` where there:
// 1) Exists a slot not in the slot hashes history
// 2) The slot is greater than the earliest slot in the history
// Thus this slot is not part of the fork and the update should be rejected
// with error `SlotsMismatch`
let missing_vote_slot = 3;
// Have to vote for a slot greater than the last vote in the vote state to avoid VoteTooOld
// errors
let vote_slot = vote_state.votes.back().unwrap().slot() + 2;
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let mut vote_state_update =
VoteStateUpdate::from(vec![(missing_vote_slot, 2), (vote_slot, 3)]);
vote_state_update.hash = vote_slot_hash;
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::SlotsMismatch),
);
// Test where some earlier vote slots exist in the history, but others don't
let missing_vote_slot = 7;
let mut vote_state_update = VoteStateUpdate::from(vec![
(2, 5),
(4, 4),
(6, 3),
(missing_vote_slot, 2),
(vote_slot, 1),
]);
vote_state_update.hash = vote_slot_hash;
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::SlotsMismatch),
);
}
#[test]
fn test_check_update_vote_state_root_on_different_fork() {
let slot_hashes = build_slot_hashes(vec![2, 4, 6, 8]);
let vote_state = build_vote_state(vec![6], &slot_hashes);
// Test with a `vote_state_update` where:
// 1) The root is not present in slot hashes history
// 2) The slot is greater than the earliest slot in the history
// Thus this slot is not part of the fork and the update should be rejected
// with error `RootOnDifferentFork`
let new_root = 3;
// Have to vote for a slot greater than the last vote in the vote state to avoid VoteTooOld
// errors, but also this slot must be present in SlotHashes
let vote_slot = 8;
assert_eq!(vote_slot, slot_hashes.first().unwrap().0);
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let mut vote_state_update = VoteStateUpdate::from(vec![(vote_slot, 1)]);
vote_state_update.hash = vote_slot_hash;
vote_state_update.root = Some(new_root);
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::RootOnDifferentFork),
);
}
#[test]
fn test_check_update_vote_state_slot_newer_than_slot_history() {
let slot_hashes = build_slot_hashes(vec![2, 4, 6, 8, 10]);
let vote_state = build_vote_state(vec![2, 4, 6], &slot_hashes);
// Test with a `vote_state_update` where there:
// 1) The last slot in the update is a slot not in the slot hashes history
// 2) The slot is greater than the newest slot in the slot history
// Thus this slot is not part of the fork and the update should be rejected
// with error `SlotsMismatch`
let missing_vote_slot = slot_hashes.first().unwrap().0 + 1;
let vote_slot_hash = Hash::new_unique();
let mut vote_state_update = VoteStateUpdate::from(vec![(8, 2), (missing_vote_slot, 3)]);
vote_state_update.hash = vote_slot_hash;
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::SlotsMismatch),
);
}
#[test]
fn test_check_update_vote_state_slot_all_slot_hashes_in_update_ok() {
let slot_hashes = build_slot_hashes(vec![2, 4, 6, 8]);
let mut vote_state = build_vote_state(vec![2, 4, 6], &slot_hashes);
// Test with a `vote_state_update` where every slot in the history is
// in the update
// Have to vote for a slot greater than the last vote in the vote state to avoid VoteTooOld
// errors
let vote_slot = vote_state.votes.back().unwrap().slot() + 2;
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let mut vote_state_update =
VoteStateUpdate::from(vec![(2, 4), (4, 3), (6, 2), (vote_slot, 1)]);
vote_state_update.hash = vote_slot_hash;
check_update_vote_state_slots_are_valid(&vote_state, &mut vote_state_update, &slot_hashes)
.unwrap();
// Nothing in the update should have been filtered out
assert_eq!(
vote_state_update
.clone()
.lockouts
.into_iter()
.collect::<Vec<Lockout>>(),
vec![
Lockout::new_with_confirmation_count(2, 4),
Lockout::new_with_confirmation_count(4, 3),
Lockout::new_with_confirmation_count(6, 2),
Lockout::new_with_confirmation_count(vote_slot, 1)
]
);
assert!(do_process_vote_state_update(
&mut vote_state,
&slot_hashes,
0,
0,
vote_state_update,
Some(&FeatureSet::all_enabled()),
)
.is_ok());
}
#[test]
fn test_check_update_vote_state_slot_some_slot_hashes_in_update_ok() {
let slot_hashes = build_slot_hashes(vec![2, 4, 6, 8, 10]);
let mut vote_state = build_vote_state(vec![6], &slot_hashes);
// Test with a `vote_state_update` where only some slots in the history are
// in the update, and others slots in the history are missing.
// Have to vote for a slot greater than the last vote in the vote state to avoid VoteTooOld
// errors
let vote_slot = vote_state.votes.back().unwrap().slot() + 2;
let vote_slot_hash = slot_hashes
.iter()
.find(|(slot, _hash)| *slot == vote_slot)
.unwrap()
.1;
let mut vote_state_update = VoteStateUpdate::from(vec![(4, 2), (vote_slot, 1)]);
vote_state_update.hash = vote_slot_hash;
check_update_vote_state_slots_are_valid(&vote_state, &mut vote_state_update, &slot_hashes)
.unwrap();
// Nothing in the update should have been filtered out
assert_eq!(
vote_state_update
.clone()
.lockouts
.into_iter()
.collect::<Vec<Lockout>>(),
vec![
Lockout::new_with_confirmation_count(4, 2),
Lockout::new_with_confirmation_count(vote_slot, 1)
]
);
// Because 6 from the original VoteState
// should not have been popped off in the proposed state,
// we should get a lockout conflict
assert_eq!(
do_process_vote_state_update(
&mut vote_state,
&slot_hashes,
0,
0,
vote_state_update,
Some(&FeatureSet::all_enabled())
),
Err(VoteError::LockoutConflict)
);
}
#[test]
fn test_check_update_vote_state_slot_hash_mismatch() {
let slot_hashes = build_slot_hashes(vec![2, 4, 6, 8]);
let vote_state = build_vote_state(vec![2, 4, 6], &slot_hashes);
// Test with a `vote_state_update` where the hash is mismatched
// Have to vote for a slot greater than the last vote in the vote state to avoid VoteTooOld
// errors
let vote_slot = vote_state.votes.back().unwrap().slot() + 2;
let vote_slot_hash = Hash::new_unique();
let mut vote_state_update =
VoteStateUpdate::from(vec![(2, 4), (4, 3), (6, 2), (vote_slot, 1)]);
vote_state_update.hash = vote_slot_hash;
assert_eq!(
check_update_vote_state_slots_are_valid(
&vote_state,
&mut vote_state_update,
&slot_hashes,
),
Err(VoteError::SlotHashMismatch),
);
}
#[test_case(0, true; "first slot")]
#[test_case(DEFAULT_SLOTS_PER_EPOCH / 2, true; "halfway through epoch")]
#[test_case((DEFAULT_SLOTS_PER_EPOCH / 2).saturating_add(1), false; "halfway through epoch plus one")]
#[test_case(DEFAULT_SLOTS_PER_EPOCH.saturating_sub(1), false; "last slot in epoch")]
#[test_case(DEFAULT_SLOTS_PER_EPOCH, true; "first slot in second epoch")]
fn test_epoch_half_check(slot: Slot, expected_allowed: bool) {
let epoch_schedule = EpochSchedule::without_warmup();
assert_eq!(
is_commission_update_allowed(slot, &epoch_schedule),
expected_allowed
);
}
#[test]
fn test_warmup_epoch_half_check_with_warmup() {
let epoch_schedule = EpochSchedule::default();
let first_normal_slot = epoch_schedule.first_normal_slot;
// first slot works
assert!(is_commission_update_allowed(0, &epoch_schedule));
// right before first normal slot works, since all warmup slots allow
// commission updates
assert!(is_commission_update_allowed(
first_normal_slot - 1,
&epoch_schedule
));
}
#[test_case(0, true; "first slot")]
#[test_case(DEFAULT_SLOTS_PER_EPOCH / 2, true; "halfway through epoch")]
#[test_case((DEFAULT_SLOTS_PER_EPOCH / 2).saturating_add(1), false; "halfway through epoch plus one")]
#[test_case(DEFAULT_SLOTS_PER_EPOCH.saturating_sub(1), false; "last slot in epoch")]
#[test_case(DEFAULT_SLOTS_PER_EPOCH, true; "first slot in second epoch")]
fn test_epoch_half_check_with_warmup(slot: Slot, expected_allowed: bool) {
let epoch_schedule = EpochSchedule::default();
let first_normal_slot = epoch_schedule.first_normal_slot;
assert_eq!(
is_commission_update_allowed(first_normal_slot.saturating_add(slot), &epoch_schedule),
expected_allowed
);
}
}