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
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Tetsy Vapory.

// Tetsy Vapory is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Tetsy Vapory is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Tetsy Vapory.  If not, see <http://www.gnu.org/licenses/>.

//! A blockchain engine that supports a non-instant BFT proof-of-authority.
//!
//! It is recommended to use the `two_thirds_majority_transition` option, to defend against the
//! ["Attack of the Clones"](https://arxiv.org/pdf/1902.10244.pdf). Newly started networks can
//! set this option to `0`, to use a 2/3 quorum from the beginning.
//!
//! To support on-chain governance, the [ValidatorSet] is pluggable: Aura supports simple
//! constant lists of validators as well as smart contract-based dynamic validator sets.
//! Misbehavior is reported to the [ValidatorSet] as well, so that e.g. governance contracts
//! can penalize or ban attacker's nodes.
//!
//! * "Benign" misbehavior are faults that can happen in normal operation, like failing
//!   to propose a block in your slot, which could be due to a temporary network outage, or
//!   wrong timestamps (due to out-of-sync clocks).
//! * "Malicious" reports are made only if the sender misbehaved deliberately (or due to a
//!   software bug), e.g. if they proposed multiple blocks with the same step number.

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::{cmp, fmt};
use std::iter::{self, FromIterator};
use std::ops::Deref;
use std::sync::atomic::{AtomicU64, AtomicBool, Ordering as AtomicOrdering};
use std::sync::{Weak, Arc};
use std::time::{UNIX_EPOCH, Duration};
use std::u64;

use client_traits::{EngineClient, ForceUpdateSealing, TransactionRequest};
use enjen::{Engine, ConstructedVerifier};
use block_gas_limit::block_gas_limit;
use block_reward::{self, BlockRewardContract, RewardKind};
use vapjson;
use mashina::{
	ExecutedBlock,
	Machine,
};
use tetsy_macros::map;
use tetsy_keccak_hash::keccak;
use log::{info, debug, error, trace, warn};
use lru_cache::LruCache;
use enjen::signer::EngineSigner;
use tetsy_crypto::publickey::Signature;
use io::{IoContext, IoHandler, TimerToken, IoService};
use itertools::{self, Itertools};
use rand::rngs::OsRng;
use tetsy_rlp::{encode, Decodable, DecoderError, Encodable, RlpStream, Rlp};
use vapory_types::{H256, H520, Address, U128, U256};
use parking_lot::{Mutex, RwLock};
use time_utils::CheckedSystemTime;
use common_types::{
	ancestry_action::AncestryAction,
	BlockNumber,
	header::{Header, ExtendedHeader},
	engines::{
		Headers,
		params::CommonParams,
		PendingTransitionStore,
		Seal,
		SealingState,
		machine::{Call, AuxiliaryData},
	},
	errors::{BlockError, VapcoreError as Error, EngineError},
	ids::BlockId,
	snapshot::Snapshotting,
	transaction::SignedTransaction,
};
use tetsy_unexpected::{Mismatch, OutOfBounds};
use validator_set::{ValidatorSet, SimpleList, new_validator_set};

mod finality;
mod randomness;
pub(crate) mod util;

use self::finality::RollingFinality;

/// `AuthorityRound` params.
pub struct AuthorityRoundParams {
	/// A map defining intervals of blocks with the given times (in seconds) to wait before next
	/// block or authority switching. The keys in the map are steps of starting blocks of those
	/// periods. The entry at `0` should be defined.
	///
	/// Wait times (durations) are additionally required to be less than 65535 since larger values
	/// lead to slow block issuance.
	pub step_durations: BTreeMap<u64, u64>,
	/// Starting step,
	pub start_step: Option<u64>,
	/// Valid validators.
	pub validators: Box<dyn ValidatorSet>,
	/// Chain score validation transition block.
	pub validate_score_transition: u64,
	/// Monotonic step validation transition block.
	pub validate_step_transition: u64,
	/// Immediate transitions.
	pub immediate_transitions: bool,
	/// Block reward in base units.
	pub block_reward: U256,
	/// Block reward contract addresses with their associated starting block numbers.
	pub block_reward_contract_transitions: BTreeMap<u64, BlockRewardContract>,
	/// Number of accepted uncles transition block.
	pub maximum_uncle_count_transition: u64,
	/// Number of accepted uncles.
	pub maximum_uncle_count: usize,
	/// Empty step messages transition block.
	pub empty_steps_transition: u64,
	/// First block for which a 2/3 quorum (instead of 1/2) is required.
	pub two_thirds_majority_transition: BlockNumber,
	/// Number of accepted empty steps.
	pub maximum_empty_steps: usize,
	/// Transition block to strict empty steps validation.
	pub strict_empty_steps_transition: u64,
	/// If set, enables random number contract integration. It maps the transition block to the contract address.
	pub randomness_contract_address: BTreeMap<u64, Address>,
	/// The addresses of contracts that determine the block gas limit with their associated block
	/// numbers.
	pub block_gas_limit_contract_transitions: BTreeMap<u64, Address>,
}

const U16_MAX: usize = ::std::u16::MAX as usize;

/// The number of recent block hashes for which the gas limit override is memoized.
const GAS_LIMIT_OVERRIDE_CACHE_CAPACITY: usize = 10;

impl From<vapjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
	fn from(p: vapjson::spec::AuthorityRoundParams) -> Self {
		let map_step_duration = |u: vapjson::uint::Uint| {
			let mut step_duration_usize: usize = u.into();
			if step_duration_usize == 0 {
				panic!("AuthorityRoundParams: step duration cannot be 0");
			}
			if step_duration_usize > U16_MAX {
				warn!(target: "engine", "step duration is too high ({}), setting it to {}", step_duration_usize, U16_MAX);
				step_duration_usize = U16_MAX;
			}
			step_duration_usize as u64
		};
		let step_durations: BTreeMap<_, _> = match p.step_duration {
			vapjson::spec::StepDuration::Single(u) =>
				iter::once((0,  map_step_duration(u))).collect(),
			vapjson::spec::StepDuration::Transitions(tr) => {
				if tr.is_empty() {
					panic!("AuthorityRoundParams: step duration transitions cannot be empty");
				}
				tr.into_iter().map(|(timestamp, u)| (timestamp.into(), map_step_duration(u))).collect()
			}
		};
		let transition_block_num = p.block_reward_contract_transition.map_or(0, Into::into);
		let mut br_transitions: BTreeMap<_, _> = p.block_reward_contract_transitions
			.unwrap_or_default()
			.into_iter()
			.map(|(block_num, address)|
				 (block_num.into(), BlockRewardContract::new_from_address(address.into())))
			.collect();
		if (p.block_reward_contract_code.is_some() || p.block_reward_contract_address.is_some()) &&
			br_transitions.keys().next().map_or(false, |&block_num| block_num <= transition_block_num)
		{
			let s = "blockRewardContractTransition";
			panic!("{} should be less than any of the keys in {}s", s, s);
		}
		if let Some(code) = p.block_reward_contract_code {
			br_transitions.insert(
				transition_block_num,
				BlockRewardContract::new_from_code(Arc::new(code.into()))
			);
		} else if let Some(address) = p.block_reward_contract_address {
			br_transitions.insert(
				transition_block_num,
				BlockRewardContract::new_from_address(address.into())
			);
		}
		let randomness_contract_address = p.randomness_contract_address.map_or_else(BTreeMap::new, |transitions| {
			transitions.into_iter().map(|(vapjson::uint::Uint(block), addr)| {
				(block.as_u64(), addr.into())
			}).collect()
		});
		let block_gas_limit_contract_transitions: BTreeMap<_, _> =
			p.block_gas_limit_contract_transitions
			.unwrap_or_default()
			.into_iter()
			.map(|(block_num, address)| (block_num.into(), address.into()))
			.collect();
		AuthorityRoundParams {
			step_durations,
			validators: new_validator_set(p.validators),
			start_step: p.start_step.map(Into::into),
			validate_score_transition: p.validate_score_transition.map_or(0, Into::into),
			validate_step_transition: p.validate_step_transition.map_or(0, Into::into),
			immediate_transitions: p.immediate_transitions.unwrap_or(false),
			block_reward: p.block_reward.map_or_else(Default::default, Into::into),
			block_reward_contract_transitions: br_transitions,
			maximum_uncle_count_transition: p.maximum_uncle_count_transition.map_or(0, Into::into),
			maximum_uncle_count: p.maximum_uncle_count.map_or(0, Into::into),
			empty_steps_transition: p.empty_steps_transition.map_or(u64::max_value(), |n| ::std::cmp::max(n.into(), 1)),
			maximum_empty_steps: p.maximum_empty_steps.map_or(0, Into::into),
			two_thirds_majority_transition: p.two_thirds_majority_transition.map_or_else(BlockNumber::max_value, Into::into),
			strict_empty_steps_transition: p.strict_empty_steps_transition.map_or(0, Into::into),
			randomness_contract_address,
			block_gas_limit_contract_transitions,
		}
	}
}

/// A triple containing the first step number and the starting timestamp of the given step duration.
#[derive(Clone, Copy, Debug)]
struct StepDurationInfo {
	transition_step: u64,
	transition_timestamp: u64,
	step_duration: u64,
}

/// Helper for managing the step.
#[derive(Debug)]
struct Step {
	calibrate: bool, // whether calibration is enabled.
	inner: AtomicU64,
	/// Planned durations of steps.
	durations: Vec<StepDurationInfo>,
}

impl Step {
	fn load(&self) -> u64 { self.inner.load(AtomicOrdering::SeqCst) }

	/// Finds the remaining duration of the current step. Panics if there was a counter under- or
	/// overflow.
	fn duration_remaining(&self) -> Duration {
		self.opt_duration_remaining().unwrap_or_else(|| {
			let ctr = self.load();
			error!(target: "engine", "Step counter under- or overflow: {}, aborting", ctr);
			panic!("step counter under- or overflow: {}", ctr)
		})
	}

	/// Finds the remaining duration of the current step. Returns `None` if there was a counter
	/// under- or overflow.
	fn opt_duration_remaining(&self) -> Option<Duration> {
		let next_step = self.load().checked_add(1)?;
		let StepDurationInfo { transition_step, transition_timestamp, step_duration } =
			self.durations.iter()
			.take_while(|info| info.transition_step < next_step)
			.last()
			.expect("durations cannot be empty")
			.clone();
		let next_time = transition_timestamp
			.checked_add(next_step.checked_sub(transition_step)?.checked_mul(step_duration)?)?;
		Some(Duration::from_secs(next_time.saturating_sub(unix_now().as_secs())))
	}

	/// Increments the step number.
	///
	/// Panics if the new step number is `u64::MAX`.
	fn increment(&self) {
		// fetch_add won't panic on overflow but will rather wrap
		// around, leading to zero as the step counter, which might
		// lead to unexpected situations, so it's better to shut down.
		if self.inner.fetch_add(1, AtomicOrdering::SeqCst) == u64::MAX {
			error!(target: "engine", "Step counter is too high: {}, aborting", u64::MAX);
			panic!("step counter is too high: {}", u64::MAX);
		}
	}

	fn calibrate(&self) {
		if self.calibrate {
			if self.opt_calibrate().is_none() {
				let ctr = self.load();
				error!(target: "engine", "Step counter under- or overflow: {}, aborting", ctr);
				panic!("step counter under- or overflow: {}", ctr)
			}
		}
	}

	/// Calibrates the AuRa step number according to the current time.
	fn opt_calibrate(&self) -> Option<()> {
		let now = unix_now().as_secs();
		let StepDurationInfo { transition_step, transition_timestamp, step_duration } =
			self.durations.iter()
			.take_while(|info| info.transition_timestamp < now)
			.last()
			.expect("durations cannot be empty")
			.clone();
		let new_step = (now.checked_sub(transition_timestamp)? / step_duration)
			.checked_add(transition_step)?;
		self.inner.store(new_step, AtomicOrdering::SeqCst);
		Some(())
	}

	fn check_future(&self, given: u64) -> Result<(), Option<OutOfBounds<u64>>> {
		const REJECTED_STEP_DRIFT: u64 = 4;

		// Verify if the step is correct.
		if given <= self.load() {
			return Ok(());
		}

		// Make absolutely sure that the given step is incorrect.
		self.calibrate();
		let current = self.load();

		// reject blocks too far in the future
		if given > current + REJECTED_STEP_DRIFT {
			Err(None)
		// wait a bit for blocks in near future
		} else if given > current {
			let d = self.durations.iter().take_while(|info| info.transition_step <= current).last()
				.expect("Duration map has at least a 0 entry.")
				.step_duration;
			Err(Some(OutOfBounds {
				min: None,
				max: Some(d * current),
				found: d * given,
			}))
		} else {
			Ok(())
		}
	}
}

// Chain scoring: total weight is sqrt(U256::max_value())*height - step
fn calculate_score(parent_step: u64, current_step: u64, current_empty_steps: usize) -> U256 {
	U256::from(U128::max_value()) + U256::from(parent_step) - U256::from(current_step) + U256::from(current_empty_steps)
}

struct EpochManager {
	epoch_transition_hash: H256,
	epoch_transition_number: BlockNumber,
	finality_checker: RollingFinality,
	force: bool,
}

impl EpochManager {
	fn blank(two_thirds_majority_transition: BlockNumber) -> Self {
		EpochManager {
			epoch_transition_hash: H256::zero(),
			epoch_transition_number: 0,
			finality_checker: RollingFinality::blank(Vec::new(), two_thirds_majority_transition),
			force: true,
		}
	}

	// Zooms to the epoch after the header with the given hash. Returns true if succeeded, false otherwise.
	fn zoom_to_after(
		&mut self,
		client: &dyn EngineClient,
		mashina: &Machine,
		validators: &dyn ValidatorSet,
		hash: H256
	) -> bool {
		let last_was_parent = self.finality_checker.subchain_head() == Some(hash);

		// early exit for current target == chain head, but only if the epochs are
		// the same.
		if last_was_parent && !self.force {
			return true;
		}

		self.force = false;
		debug!(target: "engine", "Zooming to epoch after block {}", hash);
		trace!(target: "engine", "Current validator set: {:?}", self.validators());


		// epoch_transition_for can be an expensive call, but in the absence of
		// forks it will only need to be called for the block directly after
		// epoch transition, in which case it will be O(1) and require a single
		// DB lookup.
		let last_transition = match client.epoch_transition_for(hash) {
			Some(t) => t,
			None => {
				// this really should never happen unless the block passed
				// hasn't got a parent in the database.
				warn!(target: "engine", "No genesis transition found. Block hash {} does not have a parent in the DB", hash);
				return false;
			}
		};

		// extract other epoch set if it's not the same as the last.
		if last_transition.block_hash != self.epoch_transition_hash {
			let (signal_number, set_proof, _) = destructure_proofs(&last_transition.proof)
				.expect("proof produced by this engine; therefore it is valid; qed");

			trace!(target: "engine", "extracting epoch validator set for epoch ({}, {}) signalled at #{}",
				last_transition.block_number, last_transition.block_hash, signal_number);

			let first = signal_number == 0;
			let epoch_set = validators.epoch_set(
				first,
				mashina,
				signal_number, // use signal number so multi-set first calculation is correct.
				set_proof,
			)
				.ok()
				.map(|(list, _)| {
					trace!(target: "engine", "Updating finality checker with new validator set extracted from epoch ({}, {}): {:?}",
						last_transition.block_number, last_transition.block_hash, &list);

					list.into_inner()
				})
				.expect("proof produced by this engine; therefore it is valid; qed");

			let two_thirds_majority_transition = self.finality_checker.two_thirds_majority_transition();
			self.finality_checker = RollingFinality::blank(epoch_set, two_thirds_majority_transition);
		}

		self.epoch_transition_hash = last_transition.block_hash;
		self.epoch_transition_number = last_transition.block_number;

		true
	}

	// Note new epoch hash. This will force the next block to re-load
	// the epoch set.
	// TODO: optimize and don't require re-loading after epoch change.
	fn note_new_epoch(&mut self) {
		self.force = true;
	}

	/// Get validator set. Zoom to the correct epoch first.
	fn validators(&self) -> &SimpleList {
		self.finality_checker.validators()
	}
}

/// A message broadcast by authorities when it's their turn to seal a block but there are no
/// transactions. Other authorities accumulate these messages and later include them in the seal as
/// proof.
#[derive(Clone, Debug, PartialEq, Eq)]
struct EmptyStep {
	signature: H520,
	step: u64,
	parent_hash: H256,
}

impl PartialOrd for EmptyStep {
	fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
		Some(self.cmp(other))
	}
}
impl Ord for EmptyStep {
	fn cmp(&self, other: &Self) -> cmp::Ordering {
		self.step.cmp(&other.step)
			.then_with(|| self.parent_hash.cmp(&other.parent_hash))
			.then_with(|| self.signature.cmp(&other.signature))
	}
}

impl EmptyStep {
	fn from_sealed(sealed_empty_step: SealedEmptyStep, parent_hash: &H256) -> EmptyStep {
		let signature = sealed_empty_step.signature;
		let step = sealed_empty_step.step;
		let parent_hash = parent_hash.clone();
		EmptyStep { signature, step, parent_hash }
	}

	fn verify(&self, validators: &dyn ValidatorSet) -> Result<bool, Error> {
		let message = keccak(empty_step_rlp(self.step, &self.parent_hash));
		let correct_proposer = step_proposer(validators, &self.parent_hash, self.step);

		tetsy_crypto::publickey::verify_address(&correct_proposer, &self.signature.into(), &message)
			.map_err(|e| e.into())
	}

	fn author(&self) -> Result<Address, Error> {
		let message = keccak(empty_step_rlp(self.step, &self.parent_hash));
		let public = tetsy_crypto::publickey::recover(&self.signature.into(), &message)?;
		Ok(tetsy_crypto::publickey::public_to_address(&public))
	}

	fn sealed(&self) -> SealedEmptyStep {
		let signature = self.signature;
		let step = self.step;
		SealedEmptyStep { signature, step }
	}
}

impl fmt::Display for EmptyStep {
	fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
		write!(f, "({:x}, {}, {:x})", self.signature, self.step, self.parent_hash)
	}
}

impl Encodable for EmptyStep {
	fn rlp_append(&self, s: &mut RlpStream) {
		let empty_step_rlp = empty_step_rlp(self.step, &self.parent_hash);
		s.begin_list(2)
			.append(&self.signature)
			.append_raw(&empty_step_rlp, 1);
	}
}

impl Decodable for EmptyStep {
	fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
		let signature = rlp.val_at(0)?;
		let empty_step_rlp = rlp.at(1)?;

		let step = empty_step_rlp.val_at(0)?;
		let parent_hash = empty_step_rlp.val_at(1)?;

		Ok(EmptyStep { signature, step, parent_hash })
	}
}

pub fn empty_step_full_rlp(signature: &H520, empty_step_rlp: &[u8]) -> Vec<u8> {
	let mut s = RlpStream::new_list(2);
	s.append(signature).append_raw(empty_step_rlp, 1);
	s.out()
}

pub fn empty_step_rlp(step: u64, parent_hash: &H256) -> Vec<u8> {
	let mut s = RlpStream::new_list(2);
	s.append(&step).append(parent_hash);
	s.out()
}

/// An empty step message that is included in a seal, the only difference is that it doesn't include
/// the `parent_hash` in order to save space. The included signature is of the original empty step
/// message, which can be reconstructed by using the parent hash of the block in which this sealed
/// empty message is included.
struct SealedEmptyStep {
	signature: H520,
	step: u64,
}

impl Encodable for SealedEmptyStep {
	fn rlp_append(&self, s: &mut RlpStream) {
		s.begin_list(2)
			.append(&self.signature)
			.append(&self.step);
	}
}

impl Decodable for SealedEmptyStep {
	fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
		let signature = rlp.val_at(0)?;
		let step = rlp.val_at(1)?;

		Ok(SealedEmptyStep { signature, step })
	}
}

struct PermissionedStep {
	inner: Step,
	can_propose: AtomicBool,
}

/// Engine using `AuthorityRound` proof-of-authority BFT consensus.
pub struct AuthorityRound {
	transition_service: IoService<()>,
	step: Arc<PermissionedStep>,
	client: Arc<RwLock<Option<Weak<dyn EngineClient>>>>,
	signer: RwLock<Option<Box<dyn EngineSigner>>>,
	validators: Box<dyn ValidatorSet>,
	validate_score_transition: u64,
	validate_step_transition: u64,
	empty_steps: Mutex<BTreeSet<EmptyStep>>,
	epoch_manager: Mutex<EpochManager>,
	immediate_transitions: bool,
	block_reward: U256,
	block_reward_contract_transitions: BTreeMap<u64, BlockRewardContract>,
	maximum_uncle_count_transition: u64,
	maximum_uncle_count: usize,
	empty_steps_transition: u64,
	strict_empty_steps_transition: u64,
	two_thirds_majority_transition: BlockNumber,
	maximum_empty_steps: usize,
	mashina: Machine,
	/// History of step hashes recently received from peers.
	received_step_hashes: RwLock<BTreeMap<(u64, Address), H256>>,
	/// If set, enables random number contract integration. It maps the transition block to the contract address.
	randomness_contract_address: BTreeMap<u64, Address>,
	/// The addresses of contracts that determine the block gas limit.
	block_gas_limit_contract_transitions: BTreeMap<u64, Address>,
	/// Memoized gas limit overrides, by block hash.
	gas_limit_override_cache: Mutex<LruCache<H256, Option<U256>>>,
}

// header-chain validator.
struct EpochVerifier {
	step: Arc<PermissionedStep>,
	subchain_validators: SimpleList,
	empty_steps_transition: u64,
	/// First block for which a 2/3 quorum (instead of 1/2) is required.
	two_thirds_majority_transition: BlockNumber,
}

impl enjen::EpochVerifier for EpochVerifier {
	fn verify_light(&self, header: &Header) -> Result<(), Error> {
		// Validate the timestamp
		verify_timestamp(&self.step.inner, header_step(header, self.empty_steps_transition)?)?;
		// always check the seal since it's fast.
		// nothing heavier to do.
		verify_external(header, &self.subchain_validators, self.empty_steps_transition)
	}

	fn check_finality_proof(&self, proof: &[u8]) -> Option<Vec<H256>> {
		let signers = self.subchain_validators.clone().into_inner();
		let mut finality_checker = RollingFinality::blank(signers, self.two_thirds_majority_transition);
		let mut finalized = Vec::new();

		let headers: Vec<Header> = Rlp::new(proof).as_list().ok()?;

		{
			let mut push_header = |parent_header: &Header, header: Option<&Header>| {
				// ensure all headers have correct number of seal fields so we can `verify_external`
				// and get `empty_steps` without panic.
				if parent_header.seal().len() != header_expected_seal_fields(parent_header, self.empty_steps_transition) {
					return None
				}
				if header.iter().any(|h| h.seal().len() != header_expected_seal_fields(h, self.empty_steps_transition)) {
					return None
				}

				// `verify_external` checks that signature is correct and author == signer.
				verify_external(parent_header, &self.subchain_validators, self.empty_steps_transition).ok()?;

				let mut signers = match header {
					Some(header) => header_empty_steps_signers(header, self.empty_steps_transition).ok()?,
					_ => Vec::new(),
				};
				signers.push(*parent_header.author());

				let newly_finalized =
					finality_checker.push_hash(parent_header.hash(), parent_header.number(), signers).ok()?;
				finalized.extend(newly_finalized);

				Some(())
			};

			for window in headers.windows(2) {
				push_header(&window[0], Some(&window[1]))?;
			}

			if let Some(last) = headers.last() {
				push_header(last, None)?;
			}
		}

		if finalized.is_empty() { None } else { Some(finalized) }
	}
}

fn header_seal_hash(header: &Header, empty_steps_rlp: Option<&[u8]>) -> H256 {
	match empty_steps_rlp {
		Some(empty_steps_rlp) => {
			let mut message = header.bare_hash().as_bytes().to_vec();
			message.extend_from_slice(empty_steps_rlp);
			keccak(message)
		},
		None => {
			header.bare_hash()
		},
	}
}

fn header_expected_seal_fields(header: &Header, empty_steps_transition: u64) -> usize {
	if header.number() >= empty_steps_transition {
		3
	} else {
		2
	}
}

fn header_step(header: &Header, empty_steps_transition: u64) -> Result<u64, ::tetsy_rlp::DecoderError> {
	Rlp::new(&header.seal().get(0).unwrap_or_else(||
		panic!("was either checked with verify_block_basic or is genesis; has {} fields; qed (Make sure the spec \
				file has a correct genesis seal)", header_expected_seal_fields(header, empty_steps_transition))
	))
	.as_val()
}

fn header_signature(header: &Header, empty_steps_transition: u64) -> Result<Signature, ::tetsy_rlp::DecoderError> {
	Rlp::new(&header.seal().get(1).unwrap_or_else(||
		panic!("was checked with verify_block_basic; has {} fields; qed",
			header_expected_seal_fields(header, empty_steps_transition))
	))
	.as_val::<H520>().map(Into::into)
}

// extracts the raw empty steps vec from the header seal. should only be called when there are 3 fields in the seal
// (i.e. header.number() >= self.empty_steps_transition)
fn header_empty_steps_raw(header: &Header) -> &[u8] {
	header.seal().get(2).expect("was checked with verify_block_basic; has 3 fields; qed")
}

// extracts the empty steps from the header seal. should only be called when there are 3 fields in the seal
// (i.e. header.number() >= self.empty_steps_transition).
fn header_empty_steps(header: &Header) -> Result<Vec<EmptyStep>, ::tetsy_rlp::DecoderError> {
	let empty_steps = Rlp::new(header_empty_steps_raw(header)).as_list::<SealedEmptyStep>()?;
	Ok(empty_steps.into_iter().map(|s| EmptyStep::from_sealed(s, header.parent_hash())).collect())
}

// gets the signers of empty step messages for the given header, does not include repeated signers
fn header_empty_steps_signers(header: &Header, empty_steps_transition: u64) -> Result<Vec<Address>, Error> {
	if header.number() >= empty_steps_transition {
		let mut signers = HashSet::new();
		for empty_step in header_empty_steps(header)? {
			signers.insert(empty_step.author()?);
		}

		Ok(Vec::from_iter(signers.into_iter()))
	} else {
		Ok(Vec::new())
	}
}

fn step_proposer(validators: &dyn ValidatorSet, bh: &H256, step: u64) -> Address {
	let proposer = validators.get(bh, step as usize);
	trace!(target: "engine", "step_proposer: Fetched proposer for step {}: {}", step, proposer);
	proposer
}

fn is_step_proposer(validators: &dyn ValidatorSet, bh: &H256, step: u64, address: &Address) -> bool {
	step_proposer(validators, bh, step) == *address
}

fn verify_timestamp(step: &Step, header_step: u64) -> Result<(), BlockError> {
	match step.check_future(header_step) {
		Err(None) => {
			trace!(target: "engine", "verify_timestamp: block from the future");
			Err(BlockError::InvalidSeal.into())
		},
		Err(Some(oob)) => {
			// NOTE This error might be returned only in early stage of verification (Stage 1).
			// Returning it further won't recover the sync process.
			trace!(target: "engine", "verify_timestamp: block too early");

			let found = CheckedSystemTime::checked_add(UNIX_EPOCH, Duration::from_secs(oob.found))
				.ok_or(BlockError::TimestampOverflow)?;
			let max = oob.max.and_then(|m| CheckedSystemTime::checked_add(UNIX_EPOCH, Duration::from_secs(m)));
			let min = oob.min.and_then(|m| CheckedSystemTime::checked_add(UNIX_EPOCH, Duration::from_secs(m)));

			let new_oob = OutOfBounds { min, max, found };

			Err(BlockError::TemporarilyInvalid(new_oob.into()))
		},
		Ok(_) => Ok(()),
	}
}

fn verify_external(header: &Header, validators: &dyn ValidatorSet, empty_steps_transition: u64) -> Result<(), Error> {
	let header_step = header_step(header, empty_steps_transition)?;

	let proposer_signature = header_signature(header, empty_steps_transition)?;
	let correct_proposer = validators.get(header.parent_hash(), header_step as usize);
	let is_invalid_proposer = *header.author() != correct_proposer || {
		let empty_steps_rlp = if header.number() >= empty_steps_transition {
			Some(header_empty_steps_raw(header))
		} else {
			None
		};

		let header_seal_hash = header_seal_hash(header, empty_steps_rlp);
		!tetsy_crypto::publickey::verify_address(&correct_proposer, &proposer_signature, &header_seal_hash)?
	};

	if is_invalid_proposer {
		warn!(target: "engine", "verify_block_external: bad proposer for step: {}", header_step);
		Err(EngineError::NotProposer(Mismatch { expected: correct_proposer, found: *header.author() }))?
	} else {
		Ok(())
	}
}

fn combine_proofs(signal_number: BlockNumber, set_proof: &[u8], finality_proof: &[u8]) -> Vec<u8> {
	let mut stream = ::tetsy_rlp::RlpStream::new_list(3);
	stream.append(&signal_number).append(&set_proof).append(&finality_proof);
	stream.out()
}


fn destructure_proofs(combined: &[u8]) -> Result<(BlockNumber, &[u8], &[u8]), Error> {
	let rlp = Rlp::new(combined);
	Ok((
		rlp.at(0)?.as_val()?,
		rlp.at(1)?.data()?,
		rlp.at(2)?.data()?,
	))
}

trait AsMillis {
	fn as_millis(&self) -> u64;
}

impl AsMillis for Duration {
	fn as_millis(&self) -> u64 {
		self.as_secs() * 1_000 + (self.subsec_nanos() / 1_000_000) as u64
	}
}

// A type for storing owned or borrowed data that has a common type.
// Useful for returning either a borrow or owned data from a function.
enum CowLike<'a, A: 'a + ?Sized, B> {
	Borrowed(&'a A),
	Owned(B),
}

impl<'a, A: ?Sized, B> Deref for CowLike<'a, A, B> where B: AsRef<A> {
	type Target = A;
	fn deref(&self) -> &A {
		match self {
			CowLike::Borrowed(b) => b,
			CowLike::Owned(o) => o.as_ref(),
		}
	}
}

impl AuthorityRound {
	/// Create a new instance of AuthorityRound engine.
	pub fn new(our_params: AuthorityRoundParams, mashina: Machine) -> Result<Arc<Self>, Error> {
		if !our_params.step_durations.contains_key(&0) {
			error!(target: "engine", "Authority Round step 0 duration is undefined, aborting");
			return Err(Error::Engine(EngineError::Custom(String::from("step 0 duration is undefined"))));
		}
		if our_params.step_durations.values().any(|v| *v == 0) {
			error!(target: "engine", "Authority Round step duration cannot be 0");
			return Err(Error::Engine(EngineError::Custom(String::from("step duration cannot be 0"))));
		}
		let should_timeout = our_params.start_step.is_none();
		let initial_step = our_params.start_step.unwrap_or(0);

		let mut durations = Vec::new();
		let mut prev_step = 0u64;
		let mut prev_time = 0u64;
		let mut prev_dur = our_params.step_durations[&0];
		durations.push(StepDurationInfo {
			transition_step: prev_step,
			transition_timestamp: prev_time,
			step_duration: prev_dur
		});
		for (time, dur) in our_params.step_durations.iter().skip(1) {
			let (step, time) = next_step_time_duration(
				StepDurationInfo{
					transition_step: prev_step,
					transition_timestamp: prev_time,
					step_duration: prev_dur,
				}, *time)
				.ok_or(BlockError::TimestampOverflow)?;
			durations.push(StepDurationInfo {
				transition_step: step,
				transition_timestamp: time,
				step_duration: *dur
			});
			prev_step = step;
			prev_time = time;
			prev_dur = *dur;
		}

		let step = Step {
			inner: AtomicU64::new(initial_step),
			calibrate: our_params.start_step.is_none(),
			durations,
		};
		step.calibrate();
		let engine = Arc::new(
			AuthorityRound {
				transition_service: IoService::<()>::start()?,
				step: Arc::new(PermissionedStep { inner: step, can_propose: AtomicBool::new(true) }),
				client: Arc::new(RwLock::new(None)),
				signer: RwLock::new(None),
				validators: our_params.validators,
				validate_score_transition: our_params.validate_score_transition,
				validate_step_transition: our_params.validate_step_transition,
				empty_steps: Default::default(),
				epoch_manager: Mutex::new(EpochManager::blank(our_params.two_thirds_majority_transition)),
				immediate_transitions: our_params.immediate_transitions,
				block_reward: our_params.block_reward,
				block_reward_contract_transitions: our_params.block_reward_contract_transitions,
				maximum_uncle_count_transition: our_params.maximum_uncle_count_transition,
				maximum_uncle_count: our_params.maximum_uncle_count,
				empty_steps_transition: our_params.empty_steps_transition,
				maximum_empty_steps: our_params.maximum_empty_steps,
				two_thirds_majority_transition: our_params.two_thirds_majority_transition,
				strict_empty_steps_transition: our_params.strict_empty_steps_transition,
				mashina,
				received_step_hashes: RwLock::new(Default::default()),
				randomness_contract_address: our_params.randomness_contract_address,
				block_gas_limit_contract_transitions: our_params.block_gas_limit_contract_transitions,
				gas_limit_override_cache: Mutex::new(LruCache::new(GAS_LIMIT_OVERRIDE_CACHE_CAPACITY)),
			});

		// Do not initialize timeouts for tests.
		if should_timeout {
			let handler = TransitionHandler {
				step: engine.step.clone(),
				client: engine.client.clone(),
			};
			engine.transition_service.register_handler(Arc::new(handler))?;
		}
		Ok(engine)
	}

	// fetch correct validator set for epoch at header, taking into account
	// finality of previous transitions.
	fn epoch_set<'a>(&'a self, header: &Header) -> Result<(CowLike<dyn ValidatorSet, SimpleList>, BlockNumber), Error> {
		Ok(if self.immediate_transitions {
			(CowLike::Borrowed(&*self.validators), header.number())
		} else {
			let mut epoch_manager = self.epoch_manager.lock();
			let client = match self.client.read().as_ref().and_then(|weak| weak.upgrade()) {
				Some(client) => client,
				None => {
					debug!(target: "engine", "Unable to verify sig: missing client ref.");
					return Err(EngineError::RequiresClient.into())
				}
			};

			if !epoch_manager.zoom_to_after(&*client, &self.mashina, &*self.validators, *header.parent_hash()) {
				debug!(target: "engine", "Unable to zoom to epoch.");
				return Err(EngineError::MissingParent(*header.parent_hash()).into())
			}

			(CowLike::Owned(epoch_manager.validators().clone()), epoch_manager.epoch_transition_number)
		})
	}

	fn empty_steps(&self, from_step: u64, to_step: u64, parent_hash: H256) -> Vec<EmptyStep> {
		let from = EmptyStep {
			step: from_step + 1,
			parent_hash,
			signature: Default::default(),
		};
		let to = EmptyStep {
			step: to_step,
			parent_hash: Default::default(),
			signature: Default::default(),
		};

		if from >= to {
			return vec![];
		}

		self.empty_steps.lock()
			.range(from..to)
			.filter(|e| e.parent_hash == parent_hash)
			.cloned()
			.collect()
	}

	fn clear_empty_steps(&self, step: u64) {
		// clear old `empty_steps` messages
		let mut empty_steps = self.empty_steps.lock();
		*empty_steps = empty_steps.split_off(&EmptyStep {
			step: step + 1,
			parent_hash: Default::default(),
			signature: Default::default(),
		});
	}

	fn handle_empty_step_message(&self, empty_step: EmptyStep) {
		self.empty_steps.lock().insert(empty_step);
	}

	fn generate_empty_step(&self, parent_hash: &H256) {
		let step = self.step.inner.load();
		let empty_step_rlp = empty_step_rlp(step, parent_hash);

		if let Ok(signature) = self.sign(keccak(&empty_step_rlp)).map(Into::into) {
			let message_rlp = empty_step_full_rlp(&signature, &empty_step_rlp);

			let parent_hash = *parent_hash;
			let empty_step = EmptyStep { signature, step, parent_hash };

			trace!(target: "engine", "broadcasting empty step message: {:?}", empty_step);
			self.broadcast_message(message_rlp);
			self.handle_empty_step_message(empty_step);

		} else {
			warn!(target: "engine", "generate_empty_step: FAIL: accounts secret key unavailable");
		}
	}

	fn broadcast_message(&self, message: Vec<u8>) {
		if let Some(ref weak) = *self.client.read() {
			if let Some(c) = weak.upgrade() {
				c.broadcast_consensus_message(message);
			}
		}
	}

	fn report_skipped(&self, header: &Header, current_step: u64, parent_step: u64, validators: &dyn ValidatorSet, set_number: u64) {
		// we're building on top of the genesis block so don't report any skipped steps
		if header.number() == 1 {
			return;
		}

		if let (true, Some(me)) = (current_step > parent_step + 1, self.address()) {
			debug!(target: "engine", "Author {} built block with step gap. current step: {}, parent step: {}",
				header.author(), current_step, parent_step);
			let mut reported = HashSet::new();
			for step in parent_step + 1..current_step {
				let skipped_primary = step_proposer(validators, header.parent_hash(), step);
				// Do not report this signer.
				if skipped_primary != me {
					// Stop reporting once validators start repeating.
					if !reported.insert(skipped_primary) { break; }
					trace!(target: "engine", "Reporting benign misbehaviour (cause: skipped step) at block #{}, epoch set number {}, step proposer={:#x}. Own address: {}",
						header.number(), set_number, skipped_primary, me);
					self.validators.report_benign(&skipped_primary, set_number, header.number());
				} else {
					trace!(target: "engine", "Primary that skipped is self, not self-reporting. Own address: {}", me);
				}
			}
		}
	}

	// Returns the hashes of all ancestor blocks that are finalized by the given `chain_head`.
	fn build_finality(&self, chain_head: &Header, ancestry: &mut dyn Iterator<Item=Header>) -> Vec<H256> {
		if self.immediate_transitions { return Vec::new() }

		let client = match self.client.read().as_ref().and_then(|weak| weak.upgrade()) {
			Some(client) => client,
			None => {
				warn!(target: "engine", "Unable to apply ancestry actions: missing client ref.");
				return Vec::new();
			}
		};

		let mut epoch_manager = self.epoch_manager.lock();
		if !epoch_manager.zoom_to_after(&*client, &self.mashina, &*self.validators, *chain_head.parent_hash()) {
			return Vec::new();
		}

		if epoch_manager.finality_checker.subchain_head() != Some(*chain_head.parent_hash()) {
			// build new finality checker from unfinalized ancestry of chain head, not including chain head itself yet.
			trace!(target: "finality", "Building finality up to parent of {} ({})",
				chain_head.hash(), chain_head.parent_hash());

			// the empty steps messages in a header signal approval of the
			// parent header.
			let mut parent_empty_steps_signers = match header_empty_steps_signers(&chain_head, self.empty_steps_transition) {
				Ok(empty_step_signers) => empty_step_signers,
				Err(_) => {
					warn!(target: "finality", "Failed to get empty step signatures from block {}", chain_head.hash());
					return Vec::new();
				}
			};

			let epoch_transition_hash = epoch_manager.epoch_transition_hash;
			let ancestry_iter = ancestry.map(|header| {
				let mut signers = vec![*header.author()];
				signers.extend(parent_empty_steps_signers.drain(..));

				if let Ok(empty_step_signers) = header_empty_steps_signers(&header, self.empty_steps_transition) {
					let res = (header.hash(), header.number(), signers);
					trace!(target: "finality", "Ancestry iteration: yielding {:?}", res);

					parent_empty_steps_signers = empty_step_signers;

					Some(res)

				} else {
					warn!(target: "finality", "Failed to get empty step signatures from block {}", header.hash());
					None
				}
			})
				.while_some()
				.take_while(|&(h, _, _)| h != epoch_transition_hash);

			if let Err(e) = epoch_manager.finality_checker.build_ancestry_subchain(ancestry_iter) {
				debug!(target: "engine", "inconsistent validator set within epoch: {:?}", e);
				return Vec::new();
			}
		}

		let finalized = epoch_manager.finality_checker.push_hash(
			chain_head.hash(), chain_head.number(), vec![*chain_head.author()]);
		finalized.unwrap_or_default()
	}

	fn address(&self) -> Option<Address> {
		self.signer.read().as_ref().map(|s| s.address() )
	}

	/// Make calls to the randomness contract.
	fn run_randomness_phase(&self, block: &ExecutedBlock) -> Result<Vec<SignedTransaction>, Error> {
		let contract_addr = match self.randomness_contract_address.range(..=block.header.number()).last() {
			Some((_, &contract_addr)) => contract_addr,
			None => return Ok(Vec::new()), // No randomness contract.
		};

		let opt_signer = self.signer.read();
		let signer = match opt_signer.as_ref() {
			Some(signer) => signer,
			None => return Ok(Vec::new()), // We are not a validator, so we shouldn't call the contracts.
		};
		let our_addr = signer.address();
		let client = self.client.read().as_ref().and_then(|weak| weak.upgrade()).ok_or_else(|| {
			debug!(target: "engine", "Unable to prepare block: missing client ref.");
			EngineError::RequiresClient
		})?;
		let full_client = client.as_full_client()
			.ok_or_else(|| EngineError::FailedSystemCall("Failed to upgrade to BlockchainClient.".to_string()))?;

		// Random number generation
		let contract = util::BoundContract::new(&*client, BlockId::Latest, contract_addr);
		let phase = randomness::RandomnessPhase::load(&contract, our_addr)
			.map_err(|err| EngineError::Custom(format!("Randomness error in load(): {:?}", err)))?;
		let data = match phase.advance(&contract, &mut OsRng, signer.as_ref())
				.map_err(|err| EngineError::Custom(format!("Randomness error in advance(): {:?}", err)))? {
			Some(data) => data,
			None => return Ok(Vec::new()), // Nothing to commit or reveal at the moment.
		};

		let nonce = block.state.nonce(&our_addr)?;
		let tx_request = TransactionRequest::call(contract_addr, data).gas_price(U256::zero()).nonce(nonce);
		Ok(vec![full_client.create_transaction(tx_request)?])
	}
}

fn unix_now() -> Duration {
	UNIX_EPOCH.elapsed().expect("Valid time has to be set in your system.")
}

struct TransitionHandler {
	step: Arc<PermissionedStep>,
	client: Arc<RwLock<Option<Weak<dyn EngineClient>>>>,
}

const ENGINE_TIMEOUT_TOKEN: TimerToken = 23;

impl IoHandler<()> for TransitionHandler {
	fn initialize(&self, io: &IoContext<()>) {
		let remaining = AsMillis::as_millis(&self.step.inner.duration_remaining());
		io.register_timer_once(ENGINE_TIMEOUT_TOKEN, Duration::from_millis(remaining))
			.unwrap_or_else(|e| warn!(target: "engine", "Failed to start consensus step timer: {}.", e))
	}

	fn timeout(&self, io: &IoContext<()>, timer: TimerToken) {
		if timer == ENGINE_TIMEOUT_TOKEN {
			// NOTE we might be lagging by couple of steps in case the timeout
			// has not been called fast enough.
			// Make sure to advance up to the actual step.
			while AsMillis::as_millis(&self.step.inner.duration_remaining()) == 0 {
				self.step.inner.increment();
				self.step.can_propose.store(true, AtomicOrdering::SeqCst);
				if let Some(ref weak) = *self.client.read() {
					if let Some(c) = weak.upgrade() {
						c.update_sealing(ForceUpdateSealing::No);
					}
				}
			}

			let next_run_at = Duration::from_millis(
				AsMillis::as_millis(&self.step.inner.duration_remaining()) >> 2
			);
			io.register_timer_once(ENGINE_TIMEOUT_TOKEN, next_run_at)
				.unwrap_or_else(|e| warn!(target: "engine", "Failed to restart consensus step timer: {}.", e))
		}
	}
}

impl Engine for AuthorityRound {
	fn name(&self) -> &str { "AuthorityRound" }

	fn mashina(&self) -> &Machine { &self.mashina }

	/// Three fields - consensus step and the corresponding proposer signature, and a list of empty
	/// step messages (which should be empty if no steps are skipped)
	fn seal_fields(&self, header: &Header) -> usize {
		header_expected_seal_fields(header, self.empty_steps_transition)
	}

	fn step(&self) {
		self.step.inner.increment();
		self.step.can_propose.store(true, AtomicOrdering::SeqCst);
		if let Some(ref weak) = *self.client.read() {
			if let Some(c) = weak.upgrade() {
				c.update_sealing(ForceUpdateSealing::No);
			}
		}
	}

	/// Additional engine-specific information for the user/developer concerning `header`.
	fn extra_info(&self, header: &Header) -> BTreeMap<String, String> {
		if header.seal().len() < header_expected_seal_fields(header, self.empty_steps_transition) {
			return BTreeMap::default();
		}

		let step = header_step(header, self.empty_steps_transition).as_ref()
			.map(ToString::to_string)
			.unwrap_or_default();
		let signature = header_signature(header, self.empty_steps_transition).as_ref()
			.map(ToString::to_string)
			.unwrap_or_default();

		let mut info = map![
			"step".into() => step,
			"signature".into() => signature
		];

		if header.number() >= self.empty_steps_transition {
			let empty_steps =
				if let Ok(empty_steps) = header_empty_steps(header).as_ref() {
					format!("[{}]",
						empty_steps.iter().fold(
							"".to_string(),
							|acc, e| if acc.len() > 0 { acc + ","} else { acc } + &e.to_string()))

				} else {
					"".into()
				};

			info.insert("emptySteps".into(), empty_steps);
		}

		info
	}

	fn maximum_uncle_count(&self, block: BlockNumber) -> usize {
		if block >= self.maximum_uncle_count_transition {
			self.maximum_uncle_count
		} else {
			// fallback to default value
			2
		}
	}

	fn populate_from_parent(&self, header: &mut Header, parent: &Header) {
		let parent_step = header_step(parent, self.empty_steps_transition).expect("Header has been verified; qed");
		let current_step = self.step.inner.load();

		let current_empty_steps_len = if header.number() >= self.empty_steps_transition {
			self.empty_steps(parent_step, current_step, parent.hash()).len()
		} else {
			0
		};

		let score = calculate_score(parent_step, current_step, current_empty_steps_len);
		header.set_difficulty(score);
		if let Some(gas_limit) = self.gas_limit_override(header) {
			trace!(target: "engine", "Setting gas limit to {} for block {}.", gas_limit, header.number());
			let parent_gas_limit = *parent.gas_limit();
			header.set_gas_limit(gas_limit);
			if parent_gas_limit != gas_limit {
				info!(target: "engine", "Block gas limit was changed from {} to {}.", parent_gas_limit, gas_limit);
			}
		}
	}

	fn sealing_state(&self) -> SealingState {
		let our_addr = match *self.signer.read() {
			Some(ref signer) => signer.address(),
			None => {
				warn!(target: "engine", "Not preparing block; cannot sign.");
				return SealingState::NotReady;
			}
		};

		let client = match self.client.read().as_ref().and_then(|weak| weak.upgrade()) {
			Some(client) => client,
			None => {
				warn!(target: "engine", "Not preparing block: missing client ref.");
				return SealingState::NotReady;
			}
		};

		let parent = match client.as_full_client() {
			Some(full_client) => full_client.best_block_header(),
			None => {
				debug!(target: "engine", "Not preparing block: not a full client.");
				return SealingState::NotReady;
			},
		};

		let validators = if self.immediate_transitions {
			CowLike::Borrowed(&*self.validators)
		} else {
			let mut epoch_manager = self.epoch_manager.lock();
			if !epoch_manager.zoom_to_after(&*client, &self.mashina, &*self.validators, parent.hash()) {
				debug!(target: "engine", "Not preparing block: Unable to zoom to epoch.");
				return SealingState::NotReady;
			}
			CowLike::Owned(epoch_manager.validators().clone())
		};

		let step = self.step.inner.load();

		if !is_step_proposer(&*validators, &parent.hash(), step, &our_addr) {
			trace!(target: "engine", "Not preparing block: not a proposer for step {}. (Our address: {})",
				step, our_addr);
			return SealingState::NotReady;
		}

		SealingState::Ready
	}

	fn handle_message(&self, rlp: &[u8]) -> Result<(), EngineError> {
		fn fmt_err<T: ::std::fmt::Debug>(x: T) -> EngineError {
			EngineError::MalformedMessage(format!("{:?}", x))
		}

		let rlp = Rlp::new(rlp);
		let empty_step: EmptyStep = rlp.as_val().map_err(fmt_err)?;

		if empty_step.verify(&*self.validators).unwrap_or(false) {
			if self.step.inner.check_future(empty_step.step).is_ok() {
				trace!(target: "engine", "handle_message: received empty step message {:?}", empty_step);
				self.handle_empty_step_message(empty_step);
			} else {
				trace!(target: "engine", "handle_message: empty step message from the future {:?}", empty_step);
			}
		} else {
			trace!(target: "engine", "handle_message: received invalid step message {:?}", empty_step);
		};

		Ok(())
	}

	/// Attempt to seal the block internally.
	///
	/// This operation is synchronous and may (quite reasonably) not be available, in which case
	/// `Seal::None` will be returned.
	fn generate_seal(&self, block: &ExecutedBlock, parent: &Header) -> Seal {
		// first check to avoid generating signature most of the time
		// (but there's still a race to the `compare_and_swap`)
		if !self.step.can_propose.load(AtomicOrdering::SeqCst) {
			trace!(target: "engine", "Aborting seal generation. Can't propose.");
			return Seal::None;
		}

		let header = &block.header;
		let parent_step = header_step(parent, self.empty_steps_transition)
			.expect("Header has been verified; qed");

		let step = self.step.inner.load();

		// filter messages from old and future steps and different parents
		let empty_steps = if header.number() >= self.empty_steps_transition {
			self.empty_steps(parent_step, step, *header.parent_hash())
		} else {
			Vec::new()
		};

		let expected_diff = calculate_score(parent_step, step, empty_steps.len());

		if header.difficulty() != &expected_diff {
			debug!(target: "engine", "Aborting seal generation. The step or empty_steps have changed in the meantime. {:?} != {:?}",
				header.difficulty(), expected_diff);
			return Seal::None;
		}

		if parent_step > step {
			warn!(target: "engine", "Aborting seal generation for invalid step: {} > {}", parent_step, step);
			return Seal::None;
		} else if parent_step == step {
			// this is guarded against by `can_propose` unless the block was signed
			// on the same step (implies same key) and on a different node.
			warn!("Attempted to seal block on the same step as parent. Is this authority sealing with more than one node?");
			return Seal::None;
		}

		let (validators, epoch_transition_number) = match self.epoch_set(header) {
			Err(err) => {
				warn!(target: "engine", "Unable to generate seal: {}", err);
				return Seal::None;
			},
			Ok(ok) => ok,
		};

		if is_step_proposer(&*validators, header.parent_hash(), step, header.author()) {
			trace!(target: "engine", "generate_seal: we are step proposer for step={}, block=#{}", step, header.number());
			// if there are no transactions to include in the block, we don't seal and instead broadcast a signed
			// `EmptyStep(step, parent_hash)` message. If we exceed the maximum amount of `empty_step` rounds we proceed
			// with the seal.
			if header.number() >= self.empty_steps_transition &&
				block.transactions.is_empty() &&
				empty_steps.len() < self.maximum_empty_steps {

				if self.step.can_propose.compare_and_swap(true, false, AtomicOrdering::SeqCst) {
					trace!(target: "engine", "generate_seal: generating empty step at step={}, block=#{}", step, header.number());
					self.generate_empty_step(header.parent_hash());
				}

				return Seal::None;
			}

			let empty_steps_rlp = if header.number() >= self.empty_steps_transition {
				let empty_steps: Vec<_> = empty_steps.iter().map(|e| e.sealed()).collect();
				Some(::tetsy_rlp::encode_list(&empty_steps))
			} else {
				None
			};

			if let Ok(signature) = self.sign(header_seal_hash(header, empty_steps_rlp.as_ref().map(|e| &**e))) {
				trace!(target: "engine", "generate_seal: Issuing a block for step {}.", step);

				// only issue the seal if we were the first to reach the compare_and_swap.
				if self.step.can_propose.compare_and_swap(true, false, AtomicOrdering::SeqCst) {
					// we can drop all accumulated empty step messages that are
					// older than the parent step since we're including them in
					// the seal
					self.clear_empty_steps(parent_step);

					// report any skipped primaries between the parent block and
					// the block we're sealing, unless we have empty steps enabled
					if header.number() < self.empty_steps_transition {
						self.report_skipped(header, step, parent_step, &*validators, epoch_transition_number);
					}

					let mut fields = vec![
						encode(&step),
						encode(&(H520::from(signature).as_bytes())),
					];

					if let Some(empty_steps_rlp) = empty_steps_rlp {
						fields.push(empty_steps_rlp);
					}
					trace!(target: "engine", "generate_seal: returning Seal::Regular for step={}, block=#{}", step, header.number());
					return Seal::Regular(fields);
				}
			} else {
				warn!(target: "engine", "generate_seal: FAIL: Accounts secret key unavailable.");
			}
		} else {
			trace!(target: "engine", "generate_seal: {} not a proposer for step {}.",
				header.author(), step);
		}
		trace!(target: "engine", "generate_seal: returning Seal::None for step={}, block=#{}", step, header.number());
		Seal::None
	}

	fn verify_local_seal(&self, _header: &Header) -> Result<(), Error> {
		Ok(())
	}

	fn on_new_block(
		&self,
		block: &mut ExecutedBlock,
		epoch_begin: bool,
	) -> Result<(), Error> {
		// with immediate transitions, we don't use the epoch mechanism anyway.
		// the genesis is always considered an epoch, but we ignore it intentionally.
		if self.immediate_transitions || !epoch_begin { return Ok(()) }

		// genesis is never a new block, but might as well check.
		let header = block.header.clone();
		let first = header.number() == 0;

		let mut call = |to, data| {
			let result = self.mashina.execute_as_system(
				block,
				to,
				U256::max_value(), // unbounded gas? maybe make configurable.
				Some(data),
			);

			result.map_err(|e| format!("{}", e))
		};

		self.validators.on_epoch_begin(first, &header, &mut call)
	}

	/// Apply the block reward on finalisation of the block.
	fn on_close_block(
		&self,
		block: &mut ExecutedBlock,
		parent: &Header,
	) -> Result<(), Error> {
		let mut beneficiaries = Vec::new();

		if block.header.number() == self.two_thirds_majority_transition {
			info!(target: "engine", "Block {}: Transitioning to 2/3 quorum.", self.two_thirds_majority_transition);
		}

		if block.header.number() >= self.empty_steps_transition {
			let empty_steps = if block.header.seal().is_empty() {
				// this is a new block, calculate rewards based on the empty steps messages we have accumulated
				let parent_step = header_step(parent, self.empty_steps_transition)?;
				let current_step = self.step.inner.load();
				self.empty_steps(parent_step, current_step, parent.hash())
			} else {
				// we're verifying a block, extract empty steps from the seal
				header_empty_steps(&block.header)?
			};

			for empty_step in empty_steps {
				let author = empty_step.author()?;
				beneficiaries.push((author, RewardKind::EmptyStep));
			}
		}

		let author = *block.header.author();
		beneficiaries.push((author, RewardKind::Author));

		let block_reward_contract_transition = self
			.block_reward_contract_transitions
			.range(..=block.header.number())
			.last();
		let rewards: Vec<_> = if let Some((_, contract)) = block_reward_contract_transition {
			let mut call = enjen::default_system_or_code_call(&self.mashina, block);
			let rewards = contract.reward(beneficiaries, &mut call)?;
			rewards.into_iter().map(|(author, amount)| (author, RewardKind::External, amount)).collect()
		} else {
			beneficiaries.into_iter().map(|(author, reward_kind)| (author, reward_kind, self.block_reward)).collect()
		};

		block_reward::apply_block_rewards(&rewards, block, &self.mashina)
	}

	fn generate_engine_transactions(&self, block: &ExecutedBlock) -> Result<Vec<SignedTransaction>, Error> {
		self.run_randomness_phase(block)
	}

	/// Check the number of seal fields.
	fn verify_block_basic(&self, header: &Header) -> Result<(), Error> {
		if header.number() >= self.validate_score_transition && *header.difficulty() >= U256::from(U128::max_value()) {
			return Err(From::from(BlockError::DifficultyOutOfBounds(
				OutOfBounds { min: None, max: Some(U256::from(U128::max_value())), found: *header.difficulty() }
			)));
		}

		match verify_timestamp(&self.step.inner, header_step(header, self.empty_steps_transition)?) {
			Err(BlockError::InvalidSeal) => {
				// This check runs in Phase 1 where there is no guarantee that the parent block is
				// already imported, therefore the call to `epoch_set` may fail. In that case we
				// won't report the misbehavior but this is not a concern because:
				// - Authorities will have a signing key available to report and it's expected that
				//   they'll be up-to-date and importing, therefore the parent header will most likely
				//   be available
				// - Even if you are an authority that is syncing the chain, the contract will most
				//   likely ignore old reports
				// - This specific check is only relevant if you're importing (since it checks
				//   against wall clock)
				if let Ok((_, set_number)) = self.epoch_set(header) {
					trace!(target: "engine", "Reporting benign misbehaviour (cause: InvalidSeal) at block #{}, epoch set number {}. Own address: {}",
						header.number(), set_number, self.address().unwrap_or_default());
					self.validators.report_benign(header.author(), set_number, header.number());
				}

				Err(BlockError::InvalidSeal.into())
			}
			Err(e) => Err(e.into()),
			Ok(()) => Ok(()),
		}
	}

	/// Do the step and gas limit validation.
	fn verify_block_family(&self, header: &Header, parent: &Header) -> Result<(), Error> {
		let step = header_step(header, self.empty_steps_transition)?;
		let parent_step = header_step(parent, self.empty_steps_transition)?;

		let (validators, set_number) = self.epoch_set(header)?;

		// Ensure header is from the step after parent.
		if step == parent_step
			|| (header.number() >= self.validate_step_transition && step <= parent_step) {
			warn!(target: "engine", "Multiple blocks proposed for step {}.", parent_step);

			self.validators.report_malicious(header.author(), set_number, header.number(), Default::default());
			Err(EngineError::DoubleVote(*header.author()))?;
		}

		// Report malice if the validator produced other sibling blocks in the same step.
		let received_step_key = (step, *header.author());
		let new_hash = header.hash();
		if self.received_step_hashes.read().get(&received_step_key).map_or(false, |h| *h != new_hash) {
			trace!(target: "engine", "Validator {} produced sibling blocks in the same step", header.author());
			self.validators.report_malicious(header.author(), set_number, header.number(), Default::default());
		} else {
			self.received_step_hashes.write().insert(received_step_key, new_hash);
		}

		// Remove hash records older than two full rounds of steps (picked as a reasonable trade-off between
		// memory consumption and fault-tolerance).
		let sibling_malice_detection_period = 2 * validators.count(&parent.hash()) as u64;
		let oldest_step = parent_step.saturating_sub(sibling_malice_detection_period);
		if oldest_step > 0 {
			let mut rsh = self.received_step_hashes.write();
			let new_rsh = rsh.split_off(&(oldest_step, Address::zero()));
			*rsh = new_rsh;
		}

		// If empty step messages are enabled we will validate the messages in the seal, missing messages are not
		// reported as there's no way to tell whether the empty step message was never sent or simply not included.
		let empty_steps_len = if header.number() >= self.empty_steps_transition {
			let validate_empty_steps = || -> Result<usize, Error> {
				let strict_empty_steps = header.number() >= self.strict_empty_steps_transition;
				let empty_steps = header_empty_steps(header)?;
				let empty_steps_len = empty_steps.len();
				let mut prev_empty_step = 0;

				for empty_step in empty_steps {
					if empty_step.step <= parent_step || empty_step.step >= step {
						Err(EngineError::InsufficientProof(
							format!("empty step proof for invalid step: {:?}", empty_step.step)))?;
					}

					if empty_step.parent_hash != *header.parent_hash() {
						Err(EngineError::InsufficientProof(
							format!("empty step proof for invalid parent hash: {:?}", empty_step.parent_hash)))?;
					}

					if !empty_step.verify(&*validators).unwrap_or(false) {
						Err(EngineError::InsufficientProof(
							format!("invalid empty step proof: {:?}", empty_step)))?;
					}

					if strict_empty_steps {
						if empty_step.step <= prev_empty_step {
							Err(EngineError::InsufficientProof(format!(
								"{} empty step: {:?}",
								if empty_step.step == prev_empty_step { "duplicate" } else { "unordered" },
								empty_step
							)))?;
						}

						prev_empty_step = empty_step.step;
					}
				}

				Ok(empty_steps_len)
			};

			match validate_empty_steps() {
				Ok(len) => len,
				Err(err) => {
					trace!(target: "engine", "Reporting benign misbehaviour (cause: invalid empty steps) at block #{}, epoch set number {}. Own address: {}",
						header.number(), set_number, self.address().unwrap_or_default());
					self.validators.report_benign(header.author(), set_number, header.number());
					return Err(err);
				},
			}
		} else {
			self.report_skipped(header, step, parent_step, &*validators, set_number);

			0
		};

		if header.number() >= self.validate_score_transition {
			let expected_difficulty = calculate_score(parent_step.into(), step.into(), empty_steps_len.into());
			if header.difficulty() != &expected_difficulty {
				return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: expected_difficulty, found: header.difficulty().clone() })));
			}
		}

		Ok(())
	}

	// Check the validators.
	fn verify_block_external(&self, header: &Header) -> Result<(), Error> {
		let (validators, set_number) = self.epoch_set(header)?;

		// verify signature against fixed list, but reports should go to the
		// contract itself.
		let res = verify_external(header, &*validators, self.empty_steps_transition);
		match res {
			Err(Error::Engine(EngineError::NotProposer(_))) => {
				trace!(target: "engine", "Reporting benign misbehaviour (cause: block from incorrect proposer) at block #{}, epoch set number {}. Own address: {}",
					header.number(), set_number, self.address().unwrap_or_default());
				self.validators.report_benign(header.author(), set_number, header.number());
			},
			Ok(_) => {
				// we can drop all accumulated empty step messages that are older than this header's step
				let header_step = header_step(header, self.empty_steps_transition)?;
				self.clear_empty_steps(header_step.into());
			},
			_ => {},
		}
		res
	}

	fn genesis_epoch_data(&self, header: &Header, call: &Call) -> Result<Vec<u8>, String> {
		self.validators.genesis_epoch_data(header, call)
			.map(|set_proof| combine_proofs(0, &set_proof, &[]))
	}

	fn signals_epoch_end(&self, header: &Header, aux: AuxiliaryData) -> enjen::EpochChange {
		if self.immediate_transitions { return enjen::EpochChange::No }

		let first = header.number() == 0;
		self.validators.signals_epoch_end(first, header, aux)
	}

	fn is_epoch_end_light(
		&self,
		chain_head: &Header,
		chain: &Headers<Header>,
		transition_store: &PendingTransitionStore,
	) -> Option<Vec<u8>> {
		// epochs only matter if we want to support light clients.
		if self.immediate_transitions { return None }

		let epoch_transition_hash = {
			let client = match self.client.read().as_ref().and_then(|weak| weak.upgrade()) {
				Some(client) => client,
				None => {
					warn!(target: "engine", "Unable to check for epoch end: missing client ref.");
					return None;
				}
			};

			let mut epoch_manager = self.epoch_manager.lock();
			if !epoch_manager.zoom_to_after(&*client, &self.mashina, &*self.validators, *chain_head.parent_hash()) {
				return None;
			}

			epoch_manager.epoch_transition_hash
		};

		let mut hash = *chain_head.parent_hash();

		let mut ancestry = std::iter::repeat_with(move || {
			chain(hash).and_then(|header| {
				if header.number() == 0 { return None }
				hash = *header.parent_hash();
				Some(header)
			})
		})
			.while_some()
			.take_while(|header| header.hash() != epoch_transition_hash);

		let finalized = self.build_finality(chain_head, &mut ancestry);

		self.is_epoch_end(chain_head, &finalized, chain, transition_store)
	}

	fn is_epoch_end(
		&self,
		chain_head: &Header,
		finalized: &[H256],
		chain: &Headers<Header>,
		transition_store: &PendingTransitionStore,
	) -> Option<Vec<u8>> {
		// epochs only matter if we want to support light clients.
		if self.immediate_transitions { return None }

		let first = chain_head.number() == 0;

		// Apply transitions that don't require finality and should be enacted immediately (e.g from chain spec)
		if let Some(change) = self.validators.is_epoch_end(first, chain_head) {
			info!(target: "engine", "Immediately applying validator set change signalled at block {}", chain_head.number());
			self.epoch_manager.lock().note_new_epoch();
			let change = combine_proofs(chain_head.number(), &change, &[]);
			return Some(change)
		}

		// check transition store for pending transitions against recently finalized blocks
		for finalized_hash in finalized {
			if let Some(pending) = transition_store(*finalized_hash) {
				// walk the chain backwards from current head until finalized_hash
				// to construct transition proof. author == ec_recover(sig) known
				// since the blocks are in the DB.
				let mut hash = chain_head.hash();
				let mut finality_proof: Vec<_> = std::iter::repeat_with(move || {
					chain(hash).and_then(|header| {
						hash = *header.parent_hash();
						if header.number() == 0 { None }
						else { Some(header) }
					})
				})
					.while_some()
					.take_while(|h| h.hash() != *finalized_hash)
					.collect();

				let finalized_header = if *finalized_hash == chain_head.hash() {
					// chain closure only stores ancestry, but the chain head is also unfinalized.
					chain_head.clone()
				} else {
					chain(*finalized_hash)
						.expect("header is finalized; finalized headers must exist in the chain; qed")
				};

				let signal_number = finalized_header.number();
				info!(target: "engine", "Applying validator set change signalled at block {}", signal_number);

				finality_proof.push(finalized_header);
				finality_proof.reverse();

				let finality_proof = ::tetsy_rlp::encode_list(&finality_proof);

				self.epoch_manager.lock().note_new_epoch();

				// We turn off can_propose here because upon validator set change there can
				// be two valid proposers for a single step: one from the old set and
				// one from the new.
				//
				// This way, upon encountering an epoch change, the proposer from the
				// new set will be forced to wait until the next step to avoid sealing a
				// block that breaks the invariant that the parent's step < the block's step.
				self.step.can_propose.store(false, AtomicOrdering::SeqCst);
				return Some(combine_proofs(signal_number, &pending.proof, &*finality_proof));
			}
		}

		None
	}

	fn epoch_verifier<'a>(&self, _header: &Header, proof: &'a [u8]) -> ConstructedVerifier<'a> {
		let (signal_number, set_proof, finality_proof) = match destructure_proofs(proof) {
			Ok(x) => x,
			Err(e) => return ConstructedVerifier::Err(e),
		};

		let first = signal_number == 0;
		match self.validators.epoch_set(first, &self.mashina, signal_number, set_proof) {
			Ok((list, finalize)) => {
				let verifier = Box::new(EpochVerifier {
					step: self.step.clone(),
					subchain_validators: list,
					empty_steps_transition: self.empty_steps_transition,
					two_thirds_majority_transition: self.two_thirds_majority_transition,
				});

				match finalize {
					Some(finalize) => ConstructedVerifier::Unconfirmed(verifier, finality_proof, finalize),
					None => ConstructedVerifier::Trusted(verifier),
				}
			}
			Err(e) => ConstructedVerifier::Err(e),
		}
	}

	fn register_client(&self, client: Weak<dyn EngineClient>) {
		*self.client.write() = Some(client.clone());
		self.validators.register_client(client);
	}

	fn set_signer(&self, signer: Option<Box<dyn EngineSigner>>) {
		*self.signer.write() = signer;
	}

	fn sign(&self, hash: H256) -> Result<Signature, Error> {
		Ok(self.signer.read()
			.as_ref()
			.ok_or(tetsy_crypto::publickey::Error::InvalidAddress)?
			.sign(hash)?
		)
	}

	fn snapshot_mode(&self) -> Snapshotting {
		if self.immediate_transitions {
			Snapshotting::Unsupported
		} else {
			Snapshotting::PoA
		}
	}

	fn ancestry_actions(&self, header: &Header, ancestry: &mut dyn Iterator<Item=ExtendedHeader>) -> Vec<AncestryAction> {
		let finalized = self.build_finality(
			header,
			&mut ancestry.take_while(|e| !e.is_finalized).map(|e| e.header),
		);

		if !finalized.is_empty() {
			debug!(target: "finality", "Finalizing blocks: {:?}", finalized);
		}

		finalized.into_iter().map(AncestryAction::MarkFinalized).collect()
	}

	fn params(&self) -> &CommonParams {
		self.mashina.params()
	}

	fn gas_limit_override(&self, header: &Header) -> Option<U256> {
		let (_, &address) = self.block_gas_limit_contract_transitions.range(..=header.number()).last()?;
		let client = match self.client.read().as_ref().and_then(|weak| weak.upgrade()) {
			Some(client) => client,
			None => {
				error!(target: "engine", "Unable to prepare block: missing client ref.");
				return None;
			}
		};
		let full_client = match client.as_full_client() {
			Some(full_client) => full_client,
			None => {
				error!(target: "engine", "Failed to upgrade to BlockchainClient.");
				return None;
			}
		};
		if let Some(limit) = self.gas_limit_override_cache.lock().get_mut(&header.hash()) {
			return *limit;
		}
		let limit = block_gas_limit(full_client, header, address);
		self.gas_limit_override_cache.lock().insert(header.hash(), limit);
		limit
	}
}

/// A helper accumulator function mapping a step duration and a step duration transition timestamp
/// to the corresponding step number and the correct starting second of the step.
fn next_step_time_duration(info: StepDurationInfo, time: u64) -> Option<(u64, u64)>
{
	let step_diff = time.checked_add(info.step_duration)?
		.checked_sub(1)?
		.checked_sub(info.transition_timestamp)?
		.checked_div(info.step_duration)?;
	let time_diff = step_diff.checked_mul(info.step_duration)?;
	Some((
		info.transition_step.checked_add(step_diff)?,
		info.transition_timestamp.checked_add(time_diff)?,
	))
}

#[cfg(test)]
mod tests {
	use std::collections::BTreeMap;
	use std::str::FromStr;
	use std::sync::Arc;
	use std::sync::atomic::{AtomicUsize, AtomicU64, Ordering as AtomicOrdering};
	use std::time::Duration;
	use tetsy_keccak_hash::keccak;
	use accounts::AccountProvider;
	use vapabi_contract::use_contract;
	use vapory_types::{Address, H520, H256, U256};
	use tetsy_crypto::publickey::Signature;
	use common_types::{
		header::Header,
		engines::{Seal, params::CommonParams},
		ids::BlockId,
		errors::{VapcoreError as Error, EngineError},
		transaction::{Action, Transaction},
	};
	use tetsy_rlp::encode;
	use vapcore::{
		block::*,
		miner::{Author, MinerService},
		test_helpers::{
			generate_dummy_client_with_spec, generate_dummy_client_with_spec_and_data, get_temp_state_db,
			TestNotify
		},
	};
	use enjen::Engine;
	use block_reward::BlockRewardContract;
	use mashina::Machine;
	use vapcore_spec::{self, Spec};
	use validator_set::{TestSet, SimpleList};
	use vapjson;
	use serde_json;

	use super::{
		AuthorityRoundParams, AuthorityRound, EmptyStep, SealedEmptyStep, StepDurationInfo,
		calculate_score, util::BoundContract, next_step_time_duration,
	};

	fn build_aura<F>(f: F) -> Arc<AuthorityRound> where
		F: FnOnce(&mut AuthorityRoundParams),
	{
		let mut params = AuthorityRoundParams {
			step_durations: [(0, 1)].to_vec().into_iter().collect(),
			start_step: Some(1),
			validators: Box::new(TestSet::default()),
			validate_score_transition: 0,
			validate_step_transition: 0,
			immediate_transitions: true,
			maximum_uncle_count_transition: 0,
			maximum_uncle_count: 0,
			empty_steps_transition: u64::max_value(),
			maximum_empty_steps: 0,
			block_reward: Default::default(),
			block_reward_contract_transitions: Default::default(),
			strict_empty_steps_transition: 0,
			two_thirds_majority_transition: 0,
			randomness_contract_address: BTreeMap::new(),
			block_gas_limit_contract_transitions: BTreeMap::new(),
		};

		// mutate aura params
		f(&mut params);
		// create engine
		let mut c_params = CommonParams::default();
		c_params.gas_limit_bound_divisor = 5.into();
		let mashina = Machine::regular(c_params, Default::default());
		AuthorityRound::new(params, mashina).unwrap()
	}

	#[test]
	fn has_valid_metadata() {
		let engine = vapcore_spec::new_test_round().engine;
		assert_eq!(engine.name(), "AuthorityRound");
	}

	#[test]
	fn can_return_schedule() {
		let engine = vapcore_spec::new_test_round().engine;
		let schedule = engine.schedule(10000000);

		assert!(schedule.stack_limit > 0);
	}

	#[test]
	fn can_do_signature_verification_fail() {
		let engine = vapcore_spec::new_test_round().engine;
		let mut header: Header = Header::default();
		header.set_seal(vec![encode(&H520::default())]);

		let verify_result = engine.verify_block_external(&header);
		assert!(verify_result.is_err());
	}

	#[test]
	fn generates_seal_and_does_not_double_propose() {
		let tap = Arc::new(AccountProvider::transient_provider());
		let addr1 = tap.insert_account(keccak("1").into(), &"1".into()).unwrap();
		let spec = vapcore_spec::new_test_round();
		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let last_hashes = Arc::new(vec![genesis_header.hash()]);
		let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b1 = b1.close_and_lock().unwrap();

		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		if let Seal::Regular(seal) = engine.generate_seal(&b1, &genesis_header) {
			assert!(b1.clone().try_seal(engine, seal).is_ok());
			// Second proposal is forbidden.
			assert!(engine.generate_seal(&b1, &genesis_header) == Seal::None);
		} else {
			panic!("block 1 not sealed");
		}
	}

	#[test]
	fn generates_seal_iff_sealer_is_set() {
		let tap = Arc::new(AccountProvider::transient_provider());
		let addr1 = tap.insert_account(keccak("1").into(), &"1".into()).unwrap();
		let spec = vapcore_spec::new_test_round();
		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let last_hashes = Arc::new(vec![genesis_header.hash()]);
		let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header,
								last_hashes.clone(), addr1, (3141562.into(), 31415620.into()),
								vec![], false)
			.unwrap().close_and_lock().unwrap();
		// Not a signer. A seal cannot be generated.
		assert!(engine.generate_seal(&b1, &genesis_header) == Seal::None);
		// Become a signer.
		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		if let Seal::Regular(seal) = engine.generate_seal(&b1, &genesis_header) {
			assert!(b1.clone().try_seal(engine, seal).is_ok());
			// Second proposal is forbidden.
			assert!(engine.generate_seal(&b1, &genesis_header) == Seal::None);
		} else {
			panic!("block 1 not sealed");
		}
		// Stop being a signer.
		engine.set_signer(None);
		// Make a step first and then create a new block in that new step.
		engine.step();
		let addr2 = tap.insert_account(keccak("0").into(), &"0".into()).unwrap();
		let mut header2 = genesis_header.clone();
		header2.set_number(2);
		header2.set_author(addr2);
		header2.set_parent_hash(header2.hash());
		let db2 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let b2 = OpenBlock::new(engine, Default::default(), false, db2, &header2,
								last_hashes, addr2, (3141562.into(), 31415620.into()),
								vec![], false)
			.unwrap().close_and_lock().unwrap();
		// Not a signer. A seal cannot be generated.
		assert!(engine.generate_seal(&b2, &header2) == Seal::None);
		// Become a signer once more.
		engine.set_signer(Some(Box::new((tap, addr2, "0".into()))));
		if let Seal::Regular(seal) = engine.generate_seal(&b2, &header2) {
			assert!(b2.clone().try_seal(engine, seal).is_ok());
			// Second proposal is forbidden.
			assert!(engine.generate_seal(&b2, &header2) == Seal::None);
		} else {
			panic!("block 2 not sealed");
		}
	}

	#[test]
	fn checks_difficulty_in_generate_seal() {
		let tap = Arc::new(AccountProvider::transient_provider());
		let addr1 = tap.insert_account(keccak("1").into(), &"1".into()).unwrap();
		let addr2 = tap.insert_account(keccak("0").into(), &"0".into()).unwrap();

		let spec = vapcore_spec::new_test_round();
		let engine = &*spec.engine;

		let genesis_header = spec.genesis_header();
		let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let db2 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let last_hashes = Arc::new(vec![genesis_header.hash()]);

		let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b1 = b1.close_and_lock().unwrap();
		let b2 = OpenBlock::new(engine, Default::default(), false, db2, &genesis_header, last_hashes, addr2, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b2 = b2.close_and_lock().unwrap();

		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		match engine.generate_seal(&b1, &genesis_header) {
			Seal::None => panic!("wrong seal"),
			Seal::Regular(_) => {
				engine.step();

				engine.set_signer(Some(Box::new((tap.clone(), addr2, "0".into()))));
				match engine.generate_seal(&b2, &genesis_header) {
					Seal::Regular(_) => panic!("sealed despite wrong difficulty"),
					Seal::None => {}
				}
			}
		}
	}

	#[test]
	fn proposer_switching() {
		let tap = AccountProvider::transient_provider();
		let addr = tap.insert_account(keccak("0").into(), &"0".into()).unwrap();
		let mut parent_header: Header = Header::default();
		parent_header.set_seal(vec![encode(&0usize)]);
		parent_header.set_gas_limit("222222".parse::<U256>().unwrap());
		let mut header: Header = Header::default();
		header.set_number(1);
		header.set_gas_limit("222222".parse::<U256>().unwrap());
		header.set_author(addr);

		let engine = vapcore_spec::new_test_round().engine;

		// Two validators.
		// Spec starts with step 2.
		header.set_difficulty(calculate_score(0, 2, 0));
		let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
		header.set_seal(vec![encode(&2usize), encode(&(&*signature as &[u8]))]);
		assert!(engine.verify_block_family(&header, &parent_header).is_ok());
		assert!(engine.verify_block_external(&header).is_err());
		header.set_difficulty(calculate_score(0, 1, 0));
		let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
		header.set_seal(vec![encode(&1usize), encode(&(&*signature as &[u8]))]);
		assert!(engine.verify_block_family(&header, &parent_header).is_ok());
		assert!(engine.verify_block_external(&header).is_ok());
	}

	#[test]
	fn rejects_future_block() {
		let tap = AccountProvider::transient_provider();
		let addr = tap.insert_account(keccak("0").into(), &"0".into()).unwrap();

		let mut parent_header: Header = Header::default();
		parent_header.set_seal(vec![encode(&0usize)]);
		parent_header.set_gas_limit("222222".parse::<U256>().unwrap());
		let mut header: Header = Header::default();
		header.set_number(1);
		header.set_gas_limit("222222".parse::<U256>().unwrap());
		header.set_author(addr);

		let engine = vapcore_spec::new_test_round().engine;

		// Two validators.
		// Spec starts with step 2.
		header.set_difficulty(calculate_score(0, 1, 0));
		let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
		header.set_seal(vec![encode(&1usize), encode(&(&*signature as &[u8]))]);
		assert!(engine.verify_block_family(&header, &parent_header).is_ok());
		assert!(engine.verify_block_external(&header).is_ok());
		header.set_seal(vec![encode(&5usize), encode(&(&*signature as &[u8]))]);
		assert!(engine.verify_block_basic(&header).is_err());
	}

	#[test]
	fn rejects_step_backwards() {
		let tap = AccountProvider::transient_provider();
		let addr = tap.insert_account(keccak("0").into(), &"0".into()).unwrap();

		let mut parent_header: Header = Header::default();
		parent_header.set_seal(vec![encode(&4usize)]);
		parent_header.set_gas_limit("222222".parse::<U256>().unwrap());
		let mut header: Header = Header::default();
		header.set_number(1);
		header.set_gas_limit("222222".parse::<U256>().unwrap());
		header.set_author(addr);

		let engine = vapcore_spec::new_test_round().engine;

		let signature = tap.sign(addr, Some("0".into()), header.bare_hash()).unwrap();
		// Two validators.
		// Spec starts with step 2.
		header.set_seal(vec![encode(&5usize), encode(&(&*signature as &[u8]))]);
		header.set_difficulty(calculate_score(4, 5, 0));
		assert!(engine.verify_block_family(&header, &parent_header).is_ok());
		header.set_seal(vec![encode(&3usize), encode(&(&*signature as &[u8]))]);
		header.set_difficulty(calculate_score(4, 3, 0));
		assert!(engine.verify_block_family(&header, &parent_header).is_err());
	}

	#[test]
	fn reports_skipped() {
		let validator1 = Address::from_low_u64_be(1);
		let validator2 = Address::from_low_u64_be(2);
		let last_benign = Arc::new(AtomicUsize::new(0));

		let aura = build_aura(|p| {
			let validator_set = TestSet::new(
				Default::default(),
				last_benign.clone(),
				vec![validator1, validator2],
			);
			p.validators = Box::new(validator_set);
		});

		let mut parent_header: Header = Header::default();
		parent_header.set_seal(vec![encode(&1usize)]);
		parent_header.set_gas_limit("222222".parse::<U256>().unwrap());
		let mut header: Header = Header::default();
		header.set_difficulty(calculate_score(1, 3, 0));
		header.set_gas_limit("222222".parse::<U256>().unwrap());
		header.set_seal(vec![encode(&3usize)]);

		// Do not report when signer not present.
		assert!(aura.verify_block_family(&header, &parent_header).is_ok());
		assert_eq!(last_benign.load(AtomicOrdering::SeqCst), 0);

		aura.set_signer(Some(Box::new((
			Arc::new(AccountProvider::transient_provider()),
			validator2,
			"".into(),
		))));

		// Do not report on steps skipped between genesis and first block.
		header.set_number(1);
		assert!(aura.verify_block_family(&header, &parent_header).is_ok());
		assert_eq!(last_benign.load(AtomicOrdering::SeqCst), 0);

		// Report on skipped steps otherwise.
		header.set_number(2);
		assert!(aura.verify_block_family(&header, &parent_header).is_ok());
		assert_eq!(last_benign.load(AtomicOrdering::SeqCst), 2);
	}

	#[test]
	fn reports_multiple_blocks_per_step() {
		let tap = AccountProvider::transient_provider();
		let addr0 = tap.insert_account(keccak("0").into(), &"0".into()).unwrap();
		let addr1 = tap.insert_account(keccak("1").into(), &"1".into()).unwrap();

		let validator_set = TestSet::from_validators(vec![addr0, addr1]);
		let aura = build_aura(|p| p.validators = Box::new(validator_set.clone()));

		aura.set_signer(Some(Box::new((Arc::new(tap), addr0, "0".into()))));

		let mut parent_header: Header = Header::default();
		parent_header.set_number(2);
		parent_header.set_seal(vec![encode(&1usize)]);
		parent_header.set_gas_limit("222222".parse::<U256>().unwrap());
		let mut header: Header = Header::default();
		header.set_number(3);
		header.set_difficulty(calculate_score(1, 2, 0));
		header.set_gas_limit("222222".parse::<U256>().unwrap());
		header.set_seal(vec![encode(&2usize)]);
		header.set_author(addr1);

		// First sibling block.
		assert!(aura.verify_block_family(&header, &parent_header).is_ok());
		assert_eq!(validator_set.last_malicious(), 0);

		// Second sibling block: should be reported.
		header.set_gas_limit("222223".parse::<U256>().unwrap());
		assert!(aura.verify_block_family(&header, &parent_header).is_ok());
		assert_eq!(validator_set.last_malicious(), 3);
	}

	#[test]
	fn test_uncles_transition() {
		let aura = build_aura(|params| {
			params.maximum_uncle_count_transition = 1;
		});

		assert_eq!(aura.maximum_uncle_count(0), 2);
		assert_eq!(aura.maximum_uncle_count(1), 0);
		assert_eq!(aura.maximum_uncle_count(100), 0);
	}

	#[test]
	#[should_panic(expected="counter is too high")]
	fn test_counter_increment_too_high() {
		use super::Step;
		let step = Step {
			calibrate: false,
			inner: AtomicU64::new(::std::u64::MAX),
			durations: [StepDurationInfo {
				transition_step: 0,
				transition_timestamp: 0,
				step_duration: 1,
			}].to_vec().into_iter().collect(),
		};
		step.increment();
	}

	#[test]
	#[should_panic(expected="step counter under- or overflow")]
	fn test_counter_duration_remaining_too_high() {
		use super::Step;
		let step = Step {
			calibrate: false,
			inner: AtomicU64::new(::std::u64::MAX),
			durations: [StepDurationInfo {
				transition_step: 0,
				transition_timestamp: 0,
				step_duration: 1,
			}].to_vec().into_iter().collect(),
		};
		step.duration_remaining();
	}

	#[test]
	fn test_next_step_time_duration() {
		// At step 7 (time 1000), we transitioned to step duration 10.
		let info = StepDurationInfo {
			step_duration: 10,
			transition_step: 7,
			transition_timestamp: 1000,
		};
		// So the next transition can happen e.g. at step 12 (time 1050) or 13 (time 1060).
		assert_eq!(Some((12, 1050)), next_step_time_duration(info, 1050));
		assert_eq!(Some((13, 1060)), next_step_time_duration(info, 1051));
		assert_eq!(Some((13, 1060)), next_step_time_duration(info, 1055));
		// The next transition could also happen immediately.
		assert_eq!(Some((7, 1000)), next_step_time_duration(info, 1000));
	}

	#[test]
	fn test_change_step_duration() {
		use super::Step;
		use std::thread;

		let now = super::unix_now().as_secs();
		let step = Step {
			calibrate: true,
			inner: AtomicU64::new(::std::u64::MAX),
			durations: [
				StepDurationInfo { transition_step: 0, transition_timestamp: 0, step_duration: 1 },
				StepDurationInfo { transition_step: now, transition_timestamp: now, step_duration: 2 },
				StepDurationInfo { transition_step: now + 1, transition_timestamp: now + 2, step_duration: 4 },
			].to_vec().into_iter().collect(),
		};
		// calibrated step `now`
		step.calibrate();
		let duration_remaining = step.duration_remaining();
		assert_eq!(step.inner.load(AtomicOrdering::SeqCst), now);
		assert!(duration_remaining <= Duration::from_secs(2));
		thread::sleep(duration_remaining);
		step.increment();
		// calibrated step `now + 1`
		step.calibrate();
		let duration_remaining = step.duration_remaining();
		assert_eq!(step.inner.load(AtomicOrdering::SeqCst), now + 1);
		assert!(duration_remaining > Duration::from_secs(2));
		assert!(duration_remaining <= Duration::from_secs(4));
	}

	#[test]
	#[should_panic(expected="called `Result::unwrap()` on an `Err` value: Engine(Custom(\"step duration cannot be 0\"))")]
	fn test_step_duration_zero() {
		build_aura(|params| {
			params.step_durations = [(0, 0)].to_vec().into_iter().collect();
		});
	}

	fn setup_empty_steps() -> (Spec, Arc<AccountProvider>, Vec<Address>) {
		let spec = vapcore_spec::new_test_round_empty_steps();
		let tap = Arc::new(AccountProvider::transient_provider());

		let addr1 = tap.insert_account(keccak("1").into(), &"1".into()).unwrap();
		let addr2 = tap.insert_account(keccak("0").into(), &"0".into()).unwrap();

		let accounts = vec![addr1, addr2];

		(spec, tap, accounts)
	}

	fn empty_step(engine: &dyn Engine, step: u64, parent_hash: &H256) -> EmptyStep {
		let empty_step_rlp = super::empty_step_rlp(step, parent_hash);
		let signature = engine.sign(keccak(&empty_step_rlp)).unwrap().into();
		let parent_hash = parent_hash.clone();
		EmptyStep { step, signature, parent_hash }
	}

	fn sealed_empty_step(engine: &dyn Engine, step: u64, parent_hash: &H256) -> SealedEmptyStep {
		let empty_step_rlp = super::empty_step_rlp(step, parent_hash);
		let signature = engine.sign(keccak(&empty_step_rlp)).unwrap().into();
		SealedEmptyStep { signature, step }
	}

	fn set_empty_steps_seal(header: &mut Header, step: u64, block_signature: &Signature, empty_steps: &[SealedEmptyStep]) {
		header.set_seal(vec![
			encode(&(step as usize)),
			encode(&(&**block_signature as &[u8])),
			::tetsy_rlp::encode_list(&empty_steps),
		]);
	}

	fn assert_insufficient_proof<T: ::std::fmt::Debug>(result: Result<T, Error>, contains: &str) {
		match result {
			Err(Error::Engine(EngineError::InsufficientProof(ref s))) =>{
				assert!(s.contains(contains), "Expected {:?} to contain {:?}", s, contains);
			},
			e => assert!(false, "Unexpected result: {:?}", e),
		}
	}

	#[test]
	fn broadcast_empty_step_message() {
		let (spec, tap, accounts) = setup_empty_steps();

		let addr1 = accounts[0];

		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();

		let last_hashes = Arc::new(vec![genesis_header.hash()]);

		let client = generate_dummy_client_with_spec(vapcore_spec::new_test_round_empty_steps);
		let notify = Arc::new(TestNotify::default());
		client.add_notify(notify.clone());
		engine.register_client(Arc::downgrade(&client) as _);

		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));

		let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b1 = b1.close_and_lock().unwrap();

		// the block is empty so we don't seal and instead broadcast an empty step message
		assert_eq!(engine.generate_seal(&b1, &genesis_header), Seal::None);

		// spec starts with step 2
		let empty_step_rlp = encode(&empty_step(engine, 2, &genesis_header.hash()));

		// we've received the message
		assert!(notify.messages.read().contains(&empty_step_rlp));
		let len = notify.messages.read().len();

		// make sure that we don't generate empty step for the second time
		assert_eq!(engine.generate_seal(&b1, &genesis_header), Seal::None);
		assert_eq!(len, notify.messages.read().len());
	}

	#[test]
	fn seal_with_empty_steps() {
		let (spec, tap, accounts) = setup_empty_steps();

		let addr1 = accounts[0];
		let addr2 = accounts[1];

		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let db2 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();

		let last_hashes = Arc::new(vec![genesis_header.hash()]);

		let client = generate_dummy_client_with_spec(vapcore_spec::new_test_round_empty_steps);
		let notify = Arc::new(TestNotify::default());
		client.add_notify(notify.clone());
		engine.register_client(Arc::downgrade(&client) as _);

		// step 2
		let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b1 = b1.close_and_lock().unwrap();

		// since the block is empty it isn't sealed and we generate empty steps
		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		assert_eq!(engine.generate_seal(&b1, &genesis_header), Seal::None);
		engine.step();

		// step 3
		let mut b2 = OpenBlock::new(engine, Default::default(), false, db2, &genesis_header, last_hashes.clone(), addr2, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		b2.push_transaction(Transaction {
			action: Action::Create,
			nonce: U256::from(0),
			gas_price: U256::from(3000),
			gas: U256::from(53_000),
			value: U256::from(1),
			data: vec![],
		}.fake_sign(addr2), None).unwrap();
		let b2 = b2.close_and_lock().unwrap();

		// we will now seal a block with 1tx and include the accumulated empty step message
		engine.set_signer(Some(Box::new((tap.clone(), addr2, "0".into()))));
		if let Seal::Regular(seal) = engine.generate_seal(&b2, &genesis_header) {
			engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
			let empty_step2 = sealed_empty_step(engine, 2, &genesis_header.hash());
			let empty_steps = ::tetsy_rlp::encode_list(&vec![empty_step2]);

			assert_eq!(seal[0], encode(&3usize));
			assert_eq!(seal[2], empty_steps);
		}
	}

	#[test]
	fn seal_empty_block_with_empty_steps() {
		let (spec, tap, accounts) = setup_empty_steps();

		let addr1 = accounts[0];
		let addr2 = accounts[1];

		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let db2 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let db3 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();

		let last_hashes = Arc::new(vec![genesis_header.hash()]);

		let client = generate_dummy_client_with_spec(vapcore_spec::new_test_round_empty_steps);
		let notify = Arc::new(TestNotify::default());
		client.add_notify(notify.clone());
		engine.register_client(Arc::downgrade(&client) as _);

		// step 2
		let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b1 = b1.close_and_lock().unwrap();

		// since the block is empty it isn't sealed and we generate empty steps
		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		assert_eq!(engine.generate_seal(&b1, &genesis_header), Seal::None);
		engine.step();

		// step 3
		let b2 = OpenBlock::new(engine, Default::default(), false, db2, &genesis_header, last_hashes.clone(), addr2, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b2 = b2.close_and_lock().unwrap();
		engine.set_signer(Some(Box::new((tap.clone(), addr2, "0".into()))));
		assert_eq!(engine.generate_seal(&b2, &genesis_header), Seal::None);
		engine.step();

		// step 4
		// the spec sets the maximum_empty_steps to 2 so we will now seal an empty block and include the empty step messages
		let b3 = OpenBlock::new(engine, Default::default(), false, db3, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b3 = b3.close_and_lock().unwrap();

		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		if let Seal::Regular(seal) = engine.generate_seal(&b3, &genesis_header) {
			let empty_step2 = sealed_empty_step(engine, 2, &genesis_header.hash());
			engine.set_signer(Some(Box::new((tap.clone(), addr2, "0".into()))));
			let empty_step3 = sealed_empty_step(engine, 3, &genesis_header.hash());

			let empty_steps = ::tetsy_rlp::encode_list(&vec![empty_step2, empty_step3]);

			assert_eq!(seal[0], encode(&4usize));
			assert_eq!(seal[2], empty_steps);
		}
	}

	#[test]
	fn reward_empty_steps() {
		let (spec, tap, accounts) = setup_empty_steps();

		let addr1 = accounts[0];

		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let db2 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();

		let last_hashes = Arc::new(vec![genesis_header.hash()]);

		let client = generate_dummy_client_with_spec(vapcore_spec::new_test_round_empty_steps);
		engine.register_client(Arc::downgrade(&client) as _);

		// step 2
		let b1 = OpenBlock::new(engine, Default::default(), false, db1, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b1 = b1.close_and_lock().unwrap();

		// since the block is empty it isn't sealed and we generate empty steps
		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		assert_eq!(engine.generate_seal(&b1, &genesis_header), Seal::None);
		engine.step();

		// step 3
		// the signer of the accumulated empty step message should be rewarded
		let b2 = OpenBlock::new(engine, Default::default(), false, db2, &genesis_header, last_hashes.clone(), addr1, (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let addr1_balance = b2.state.balance(&addr1).unwrap();

		// after closing the block `addr1` should be reward twice, one for the included empty step message and another for block creation
		let b2 = b2.close_and_lock().unwrap();

		// the spec sets the block reward to 10
		assert_eq!(b2.state.balance(&addr1).unwrap(), addr1_balance + (10 * 2))
	}

	#[test]
	fn verify_seal_empty_steps() {
		let (spec, tap, accounts) = setup_empty_steps();
		let addr1 = accounts[0];
		let addr2 = accounts[1];
		let engine = &*spec.engine;

		let mut parent_header: Header = Header::default();
		parent_header.set_seal(vec![encode(&0usize)]);
		parent_header.set_gas_limit("222222".parse::<U256>().unwrap());

		let mut header: Header = Header::default();
		header.set_parent_hash(parent_header.hash());
		header.set_number(1);
		header.set_gas_limit("222222".parse::<U256>().unwrap());
		header.set_author(addr1);

		let signature = tap.sign(addr1, Some("1".into()), header.bare_hash()).unwrap();

		// empty step with invalid step
		let empty_steps = vec![SealedEmptyStep { signature: H520::zero(), step: 2 }];
		set_empty_steps_seal(&mut header, 2, &signature, &empty_steps);

		assert_insufficient_proof(
			engine.verify_block_family(&header, &parent_header),
			"invalid step"
		);

		// empty step with invalid signature
		let empty_steps = vec![SealedEmptyStep { signature: H520::zero(), step: 1 }];
		set_empty_steps_seal(&mut header, 2, &signature, &empty_steps);

		assert_insufficient_proof(
			engine.verify_block_family(&header, &parent_header),
			"invalid empty step proof"
		);

		// empty step with valid signature from incorrect proposer for step
		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		let empty_steps = vec![sealed_empty_step(engine, 1, &parent_header.hash())];
		set_empty_steps_seal(&mut header, 2, &signature, &empty_steps);

		assert_insufficient_proof(
			engine.verify_block_family(&header, &parent_header),
			"invalid empty step proof"
		);

		// valid empty steps
		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		let empty_step2 = sealed_empty_step(engine, 2, &parent_header.hash());
		engine.set_signer(Some(Box::new((tap.clone(), addr2, "0".into()))));
		let empty_step3 = sealed_empty_step(engine, 3, &parent_header.hash());

		let empty_steps = vec![empty_step2, empty_step3];
		header.set_difficulty(calculate_score(0, 4, 2));
		let signature = tap.sign(addr1, Some("1".into()), header.bare_hash()).unwrap();
		set_empty_steps_seal(&mut header, 4, &signature, &empty_steps);

		assert!(engine.verify_block_family(&header, &parent_header).is_ok());
	}

	#[test]
	fn block_reward_contract() {
		let spec = vapcore_spec::new_test_round_block_reward_contract();
		let tap = Arc::new(AccountProvider::transient_provider());

		let addr1 = tap.insert_account(keccak("1").into(), &"1".into()).unwrap();

		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db1 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let db2 = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();

		let last_hashes = Arc::new(vec![genesis_header.hash()]);

		let client = generate_dummy_client_with_spec(vapcore_spec::new_test_round_block_reward_contract);
		engine.register_client(Arc::downgrade(&client) as _);

		// step 2
		let b1 = OpenBlock::new(
			engine,
			Default::default(),
			false,
			db1,
			&genesis_header,
			last_hashes.clone(),
			addr1,
			(3141562.into(), 31415620.into()),
			vec![],
			false,
		).unwrap();
		let b1 = b1.close_and_lock().unwrap();

		// since the block is empty it isn't sealed and we generate empty steps
		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));
		assert_eq!(engine.generate_seal(&b1, &genesis_header), Seal::None);
		engine.step();

		// step 3
		// the signer of the accumulated empty step message should be rewarded
		let b2 = OpenBlock::new(
			engine,
			Default::default(),
			false,
			db2,
			&genesis_header,
			last_hashes.clone(),
			addr1,
			(3141562.into(), 31415620.into()),
			vec![],
			false,
		).unwrap();
		let addr1_balance = b2.state.balance(&addr1).unwrap();

		// after closing the block `addr1` should be reward twice, one for the included empty step
		// message and another for block creation
		let b2 = b2.close_and_lock().unwrap();

		// the contract rewards (1000 + kind) for each benefactor/reward kind
		assert_eq!(
			b2.state.balance(&addr1).unwrap(),
			addr1_balance + (1000 + 0) + (1000 + 2),
		)
	}

	#[test]
	fn randomness_contract() -> Result<(), super::util::CallError> {
		use_contract!(rand_contract, "res/contracts/test_authority_round_random.json");

		env_logger::init();

		let contract_addr = Address::from_str("0000000000000000000000000000000000000042").unwrap();
		let client = generate_dummy_client_with_spec_and_data(
			vapcore_spec::new_test_round_randomness_contract, 0, 0, &[], true
		);

		let tap = Arc::new(AccountProvider::transient_provider());

		let addr1 = tap.insert_account(keccak("1").into(), &"1".into()).unwrap();
		// Unlock account so that the engine can decrypt the secret.
		tap.unlock_account_permanently(addr1, "1".into()).expect("unlock");

		let signer = Box::new((tap.clone(), addr1, "1".into()));
		client.miner().set_author(Author::Sealer(signer.clone()));
		client.miner().set_gas_range_target((U256::from(1000000), U256::from(1000000)));

		let engine = client.engine();
		engine.set_signer(Some(signer));
		engine.register_client(Arc::downgrade(&client) as _);
		let bc = BoundContract::new(&*client, BlockId::Latest, contract_addr);

		// First the contract is in the commit phase, and we haven't committed yet.
		assert!(bc.call_const(rand_contract::functions::is_commit_phase::call())?);
		assert!(!bc.call_const(rand_contract::functions::is_committed::call(0, addr1))?);

		// We produce a block and commit.
		engine.step();
		assert!(bc.call_const(rand_contract::functions::is_committed::call(0, addr1))?);

		// After two more blocks we are in the reveal phase...
		engine.step();
		engine.step();
		assert!(bc.call_const(rand_contract::functions::is_reveal_phase::call())?);
		assert!(!bc.call_const(rand_contract::functions::sent_reveal::call(0, addr1))?);
		assert!(bc.call_const(rand_contract::functions::get_value::call())?.is_zero());

		// ...so in the next step, we reveal our random value, and the contract's random value is not zero anymore.
		engine.step();
		assert!(bc.call_const(rand_contract::functions::sent_reveal::call(0, addr1))?);
		assert!(!bc.call_const(rand_contract::functions::get_value::call())?.is_zero());
		Ok(())
	}

	#[test]
	fn extra_info_from_seal() {
		let (spec, tap, accounts) = setup_empty_steps();
		let engine = &*spec.engine;

		let addr1 = accounts[0];
		engine.set_signer(Some(Box::new((tap.clone(), addr1, "1".into()))));

		let mut header: Header = Header::default();
		let empty_step = empty_step(engine, 1, &header.parent_hash());
		let sealed_empty_step = empty_step.sealed();

		header.set_number(2);
		header.set_seal(vec![
			encode(&2usize),
			encode(&H520::default()),
			::tetsy_rlp::encode_list(&vec![sealed_empty_step]),
		]);

		let info = engine.extra_info(&header);

		let mut expected = BTreeMap::default();
		expected.insert("step".into(), "2".into());
		expected.insert("signature".into(), Signature::from(H520::default()).to_string());
		expected.insert("emptySteps".into(), format!("[{}]", empty_step));

		assert_eq!(info, expected);

		header.set_seal(vec![]);

		assert_eq!(
			engine.extra_info(&header),
			BTreeMap::default(),
		);
	}

	#[test]
	fn test_empty_steps() {
		let engine = build_aura(|p| {
			p.step_durations = [(0, 4)].to_vec().into_iter().collect();
			p.empty_steps_transition = 0;
			p.maximum_empty_steps = 0;
		});

		let parent_hash = H256::from_low_u64_be(1);
		let signature = H520::default();
		let step = |step: u64| EmptyStep {
			step,
			parent_hash,
			signature,
		};

		engine.handle_empty_step_message(step(1));
		engine.handle_empty_step_message(step(3));
		engine.handle_empty_step_message(step(2));
		engine.handle_empty_step_message(step(1));

		assert_eq!(engine.empty_steps(0, 4, parent_hash), vec![step(1), step(2), step(3)]);
		assert_eq!(engine.empty_steps(2, 3, parent_hash), vec![]);
		assert_eq!(engine.empty_steps(2, 4, parent_hash), vec![step(3)]);

		engine.clear_empty_steps(2);

		assert_eq!(engine.empty_steps(0, 3, parent_hash), vec![]);
		assert_eq!(engine.empty_steps(0, 4, parent_hash), vec![step(3)]);
	}

	#[test]
	fn should_reject_duplicate_empty_steps() {
		// given
		let (_spec, tap, accounts) = setup_empty_steps();
		let engine = build_aura(|p| {
			p.validators = Box::new(SimpleList::new(accounts.clone()));
			p.step_durations = [(0, 4)].to_vec().into_iter().collect();
			p.empty_steps_transition = 0;
			p.maximum_empty_steps = 0;
		});

		let mut parent = Header::default();
		parent.set_seal(vec![encode(&0usize)]);

		let mut header = Header::default();
		header.set_number(parent.number() + 1);
		header.set_parent_hash(parent.hash());
		header.set_author(accounts[0]);

		// when
		engine.set_signer(Some(Box::new((tap.clone(), accounts[1], "0".into()))));
		let empty_steps = vec![
			sealed_empty_step(&*engine, 1, &parent.hash()),
			sealed_empty_step(&*engine, 1, &parent.hash()),
		];
		let step = 2;
		let signature = tap.sign(accounts[0], Some("1".into()), header.bare_hash()).unwrap();
		set_empty_steps_seal(&mut header, step, &signature, &empty_steps);
		header.set_difficulty(calculate_score(0, step, empty_steps.len()));

		// then
		assert_insufficient_proof(
			engine.verify_block_family(&header, &parent),
			"duplicate empty step"
		);
	}

	#[test]
	fn should_reject_empty_steps_out_of_order() {
		// given
		let (_spec, tap, accounts) = setup_empty_steps();
		let engine = build_aura(|p| {
			p.validators = Box::new(SimpleList::new(accounts.clone()));
			p.step_durations = [(0, 4)].to_vec().into_iter().collect();
			p.empty_steps_transition = 0;
			p.maximum_empty_steps = 0;
		});

		let mut parent = Header::default();
		parent.set_seal(vec![encode(&0usize)]);

		let mut header = Header::default();
		header.set_number(parent.number() + 1);
		header.set_parent_hash(parent.hash());
		header.set_author(accounts[0]);

		// when
		engine.set_signer(Some(Box::new((tap.clone(), accounts[1], "0".into()))));
		let es1 = sealed_empty_step(&*engine, 1, &parent.hash());
		engine.set_signer(Some(Box::new((tap.clone(), accounts[0], "1".into()))));
		let es2 = sealed_empty_step(&*engine, 2, &parent.hash());

		let mut empty_steps = vec![es2, es1];

		let step = 3;
		let signature = tap.sign(accounts[1], Some("0".into()), header.bare_hash()).unwrap();
		set_empty_steps_seal(&mut header, step, &signature, &empty_steps);
		header.set_difficulty(calculate_score(0, step, empty_steps.len()));

		// then make sure it's rejected because of the order
		assert_insufficient_proof(
			engine.verify_block_family(&header, &parent),
			"unordered empty step"
		);

		// now try to fix the order
		empty_steps.reverse();
		set_empty_steps_seal(&mut header, step, &signature, &empty_steps);
		assert_eq!(engine.verify_block_family(&header, &parent).unwrap(), ());
	}

	#[test]
	fn should_collect_block_reward_transitions() {
		let config = r#"{
			"params": {
				"stepDuration": "5",
				"validators": {
					"list" : ["0x1000000000000000000000000000000000000001"]
				},
				"blockRewardContractTransition": "0",
				"blockRewardContractAddress": "0x2000000000000000000000000000000000000002",
				"blockRewardContractTransitions": {
					"7": "0x3000000000000000000000000000000000000003",
					"42": "0x4000000000000000000000000000000000000004"
				}
			}
		}"#;
		let deserialized: vapjson::spec::AuthorityRound = serde_json::from_str(config).unwrap();
		let params = AuthorityRoundParams::from(deserialized.params);
		for ((block_num1, address1), (block_num2, address2)) in
			params.block_reward_contract_transitions.iter().zip(
				[(0u64, BlockRewardContract::new_from_address(Address::from_str("2000000000000000000000000000000000000002").unwrap())),
				 (7u64, BlockRewardContract::new_from_address(Address::from_str("3000000000000000000000000000000000000003").unwrap())),
				 (42u64, BlockRewardContract::new_from_address(Address::from_str("4000000000000000000000000000000000000004").unwrap())),
				].iter())
		{
			assert_eq!(block_num1, block_num2);
			assert_eq!(address1, address2);
		}
	}

	#[test]
	#[should_panic(expected="blockRewardContractTransition should be less than any of the keys in blockRewardContractTransitions")]
	fn should_reject_out_of_order_block_reward_transition() {
		let config = r#"{
			"params": {
				"stepDuration": "5",
				"validators": {
					"list" : ["0x1000000000000000000000000000000000000001"]
				},
				"blockRewardContractTransition": "7",
				"blockRewardContractAddress": "0x2000000000000000000000000000000000000002",
				"blockRewardContractTransitions": {
					"0": "0x3000000000000000000000000000000000000003",
					"42": "0x4000000000000000000000000000000000000004"
				}
			}
		}"#;
		let deserialized: vapjson::spec::AuthorityRound = serde_json::from_str(config).unwrap();
		AuthorityRoundParams::from(deserialized.params);
	}
}