       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

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity 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.

    // Parity 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 Parity.  If not, see <http://www.gnu.org/licenses/>.

    //! Transaction Queue
    //!
    //! `TransactionQueue` keeps track of all transactions seen by the node (received from other peers) and own transactions
    //! and orders them by priority. Top priority transactions are those with low nonce height (difference between
    //! transaction's nonce and next nonce expected from this sender). If nonces are equal transaction's gas price is used
    //! for comparison (higher gas price = higher priority).
    //!
    //! # Usage Example
    //!
    //! ```rust
    //! extern crate ethcore_util as util;
    //! extern crate ethcore;
    //! extern crate ethkey;
    //! extern crate rustc_serialize;
    //!
    //! use util::{Uint, U256, Address};
    //! use ethkey::{Random, Generator};
    //!	use ethcore::miner::{TransactionQueue, TransactionQueueDetailsProvider, AccountDetails, TransactionOrigin};
    //!	use ethcore::transaction::*;
    //!	use rustc_serialize::hex::FromHex;
    //!
    //!	#[derive(Default)]
    //! struct DummyTransactionDetailsProvider;
    //!
    //!	impl TransactionQueueDetailsProvider for DummyTransactionDetailsProvider {
    //!		fn fetch_account(&self, _address: &Address) -> AccountDetails {
    //!			AccountDetails {
    //!				nonce: U256::from(10),
    //!				balance: U256::from(1_000_000)
    //!			}
    //!		}
    //!
    //!		fn estimate_gas_required(&self, _tx: &SignedTransaction) -> U256 {
    //!			2.into()
    //!		}
    //!
    //!		fn is_service_transaction_acceptable(&self, _tx: &SignedTransaction) -> Result<bool, String> {
    //!			Ok(true)
    //!		}
    //! }
    //!
    //! fn main() {
    //!		let key = Random.generate().unwrap();
    //!		let t1 = Transaction { action: Action::Create, value: U256::from(100), data: "3331600055".from_hex().unwrap(),
    //!			gas: U256::from(100_000), gas_price: U256::one(), nonce: U256::from(10) };
    //!		let t2 = Transaction { action: Action::Create, value: U256::from(100), data: "3331600055".from_hex().unwrap(),
    //!			gas: U256::from(100_000), gas_price: U256::one(), nonce: U256::from(11) };
    //!
    //!		let st1 = t1.sign(&key.secret(), None);
    //!		let st2 = t2.sign(&key.secret(), None);
    //!		let details_provider = DummyTransactionDetailsProvider::default();
    //!
    //!		let mut txq = TransactionQueue::default();
    //!		txq.add(st2.clone(), TransactionOrigin::External, 0, None, &details_provider).unwrap();
    //!		txq.add(st1.clone(), TransactionOrigin::External, 0, None, &details_provider).unwrap();
    //!
    //!		// Check status
    //!		assert_eq!(txq.status().pending, 2);
    //!		// Check top transactions
    //!		let top = txq.top_transactions();
    //!		assert_eq!(top.len(), 2);
    //!		assert_eq!(top[0], st1);
    //!		assert_eq!(top[1], st2);
    //!
    //!		// And when transaction is removed (but nonce haven't changed)
    //!		// it will move subsequent transactions to future
    //!		txq.remove_invalid(&st1.hash(), &|_| 10.into());
    //!		assert_eq!(txq.status().pending, 0);
    //!		assert_eq!(txq.status().future, 1);
    //!		assert_eq!(txq.top_transactions().len(), 0);
    //!	}
    //! ```
    //!
    //!	# Maintaing valid state
    //!
    //!	1. Whenever transaction is imported to queue (to queue) all other transactions from this sender are revalidated in current. It means that they are moved to future and back again (height recalculation & gap filling).
    //!	2. Whenever invalid transaction is removed:
    //!		- When it's removed from `future` - all `future` transactions heights are recalculated and then
    //!		  we check if the transactions should go to `current` (comparing state nonce)
    //!		- When it's removed from `current` - all transactions from this sender (`current` & `future`) are recalculated.
    //!	3. `cull` is used to inform the queue about client (state) nonce changes.
    //!     - It removes all transactions (either from `current` or `future`) with nonce < client nonce
    //!     - It moves matching `future` transactions to `current`
    //! 4. `remove_old` is used as convenient method to update the state nonce for all senders in the queue.
    //!		- Invokes `cull` with latest state nonce for all senders.

    use std::ops::Deref;
    use std::cmp::Ordering;
    use std::cmp;
    use std::collections::{HashSet, HashMap, BTreeSet, BTreeMap};
    use linked_hash_map::LinkedHashMap;
    use util::{Address, H256, Uint, U256};
    use util::table::Table;
    use transaction::*;
    use error::{Error, TransactionError};
    use client::TransactionImportResult;
    use header::BlockNumber;
    use miner::local_transactions::{LocalTransactionsList, Status as LocalTransactionStatus};

    /// Transaction origin
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub enum TransactionOrigin {
    	/// Transaction coming from local RPC
    	Local,
    	/// External transaction received from network
    	External,
    	/// Transactions from retracted blocks
    	RetractedBlock,
    }

    impl PartialOrd for TransactionOrigin {
    	fn partial_cmp(&self, other: &TransactionOrigin) -> Option<Ordering> {
    		Some(self.cmp(other))
    	}
    }

    impl Ord for TransactionOrigin {
    	#[cfg_attr(feature="dev", allow(match_same_arms))]
    	fn cmp(&self, other: &TransactionOrigin) -> Ordering {
    		if *other == *self {
    			return Ordering::Equal;
    		}

    		match (*self, *other) {
    			(TransactionOrigin::RetractedBlock, _) => Ordering::Less,
    			(_, TransactionOrigin::RetractedBlock) => Ordering::Greater,
    			(TransactionOrigin::Local, _) => Ordering::Less,
    			_ => Ordering::Greater,
    		}
    	}
    }

    impl TransactionOrigin {
    	fn is_local(&self) -> bool {
    		*self == TransactionOrigin::Local
    	}
    }

    #[derive(Clone, Debug)]
    /// Light structure used to identify transaction and its order
    struct TransactionOrder {
    	/// Primary ordering factory. Difference between transaction nonce and expected nonce in state
    	/// (e.g. Tx(nonce:5), State(nonce:0) -> height: 5)
    	/// High nonce_height = Low priority (processed later)
    	nonce_height: U256,
    	/// Gas Price of the transaction.
    	/// Low gas price = Low priority (processed later)
    	gas_price: U256,
    	/// Gas usage priority factor. Usage depends on strategy.
    	/// Represents the linear increment in required gas price for heavy transactions.
    	///
    	/// High gas limit + Low gas price = Low priority
    	/// High gas limit + High gas price = High priority
    	gas_factor: U256,
    	/// Gas (limit) of the transaction. Usage depends on strategy.
    	/// Low gas limit = High priority (processed earlier)
    	gas: U256,
    	/// Transaction ordering strategy
    	strategy: PrioritizationStrategy,
    	/// Hash to identify associated transaction
    	hash: H256,
    	/// Origin of the transaction
    	origin: TransactionOrigin,
    	/// Penalties
    	penalties: usize,
    }

    impl TransactionOrder {

    	fn for_transaction(tx: &VerifiedTransaction, base_nonce: U256, min_gas_price: U256, strategy: PrioritizationStrategy) -> Self {
    		let factor = (tx.transaction.gas >> 15) * min_gas_price;
    		TransactionOrder {
    			nonce_height: tx.nonce() - base_nonce,
    			gas_price: tx.transaction.gas_price,
    			gas: tx.transaction.gas,
    			gas_factor: factor,
    			strategy: strategy,
    			hash: tx.hash(),
    			origin: tx.origin,
    			penalties: 0,
    		}
    	}

    	fn update_height(mut self, nonce: U256, base_nonce: U256) -> Self {
    		self.nonce_height = nonce - base_nonce;
    		self
    	}

    	fn penalize(mut self) -> Self {
    		self.penalties = self.penalties.saturating_add(1);
    		self
    	}
    }

    impl Eq for TransactionOrder {}
    impl PartialEq for TransactionOrder {
    	fn eq(&self, other: &TransactionOrder) -> bool {
    		self.cmp(other) == Ordering::Equal
    	}
    }
    impl PartialOrd for TransactionOrder {
    	fn partial_cmp(&self, other: &TransactionOrder) -> Option<Ordering> {
    		Some(self.cmp(other))
    	}
    }

    impl Ord for TransactionOrder {
    	fn cmp(&self, b: &TransactionOrder) -> Ordering {
    		// First check number of penalties
    		if self.penalties != b.penalties {
    			return self.penalties.cmp(&b.penalties);
    		}

    		// Local transactions should always have priority
    		if self.origin != b.origin {
    			return self.origin.cmp(&b.origin);
    		}

    		// Check nonce_height
    		if self.nonce_height != b.nonce_height {
    			return self.nonce_height.cmp(&b.nonce_height);
    		}

    		match self.strategy {
    			PrioritizationStrategy::GasAndGasPrice => {
    				if self.gas != b.gas {
    					return self.gas.cmp(&b.gas);
    				}
    			},
    			PrioritizationStrategy::GasFactorAndGasPrice => {
    				// avoiding overflows
    				// (gp1 - g1) > (gp2 - g2) <=>
    				// (gp1 + g2) > (gp2 + g1)
    				let f_a = self.gas_price + b.gas_factor;
    				let f_b = b.gas_price + self.gas_factor;
    				if f_a != f_b {
    					return f_b.cmp(&f_a);
    				}
    			},
    			PrioritizationStrategy::GasPriceOnly => {},
    		}

    		// Then compare gas_prices
    		if self.gas_price != b.gas_price {
    			return b.gas_price.cmp(&self.gas_price);
    		}

    		// Compare hashes
    		self.hash.cmp(&b.hash)
    	}
    }

    /// Verified transaction
    #[derive(Debug)]
    struct VerifiedTransaction {
    	/// Transaction.
    	transaction: SignedTransaction,
    	/// Transaction origin.
    	origin: TransactionOrigin,
    	/// Insertion time
    	insertion_time: QueuingInstant,
    	/// Delay until specifid block.
    	min_block: Option<BlockNumber>,
    }

    impl VerifiedTransaction {
    	fn new(transaction: SignedTransaction, origin: TransactionOrigin, time: QueuingInstant, min_block: Option<BlockNumber>) -> Self {
    		VerifiedTransaction {
    			transaction: transaction,
    			origin: origin,
    			insertion_time: time,
    			min_block: min_block,
    		}
    	}

    	fn hash(&self) -> H256 {
    		self.transaction.hash()
    	}

    	fn nonce(&self) -> U256 {
    		self.transaction.nonce
    	}

    	fn sender(&self) -> Address {
    		self.transaction.sender()
    	}

    	fn cost(&self) -> U256 {
    		self.transaction.value + self.transaction.gas_price * self.transaction.gas
    	}
    }

    #[derive(Debug, Default)]
    struct GasPriceQueue {
    	backing: BTreeMap<U256, HashSet<H256>>,
    }

    impl GasPriceQueue {
    	/// Insert an item into a BTreeMap/HashSet "multimap".
    	pub fn insert(&mut self, gas_price: U256, hash: H256) -> bool {
    		self.backing.entry(gas_price).or_insert_with(Default::default).insert(hash)
    	}

    	/// Remove an item from a BTreeMap/HashSet "multimap".
    	/// Returns true if the item was removed successfully.
    	pub fn remove(&mut self, gas_price: &U256, hash: &H256) -> bool {
    		if let Some(mut hashes) = self.backing.get_mut(gas_price) {
    			let only_one_left = hashes.len() == 1;
    			if !only_one_left {
    				// Operation may be ok: only if hash is in gas-price's Set.
    				return hashes.remove(hash);
    			}
    			if hash != hashes.iter().next().expect("We know there is only one element in collection, tested above; qed") {
    				// Operation failed: hash not the single item in gas-price's Set.
    				return false;
    			}
    		} else {
    			// Operation failed: gas-price not found in Map.
    			return false;
    		}
    		// Operation maybe ok: only if hash not found in gas-price Set.
    		self.backing.remove(gas_price).is_some()
    	}
    }

    impl Deref for GasPriceQueue {
    	type Target=BTreeMap<U256, HashSet<H256>>;

    	fn deref(&self) -> &Self::Target {
    		&self.backing
    	}
    }

    /// Holds transactions accessible by (address, nonce) and by priority
    ///
    /// `TransactionSet` keeps number of entries below limit, but it doesn't
    /// automatically happen during `insert/remove` operations.
    /// You have to call `enforce_limit` to remove lowest priority transactions from set.
    struct TransactionSet {
    	by_priority: BTreeSet<TransactionOrder>,
    	by_address: Table<Address, U256, TransactionOrder>,
    	by_gas_price: GasPriceQueue,
    	limit: usize,
    	gas_limit: U256,
    }

    impl TransactionSet {
    	/// Inserts `TransactionOrder` to this set. Transaction does not need to be unique -
    	/// the same transaction may be validly inserted twice. Any previous transaction that
    	/// it replaces (i.e. with the same `sender` and `nonce`) should be returned.
    	fn insert(&mut self, sender: Address, nonce: U256, order: TransactionOrder) -> Option<TransactionOrder> {
    		if !self.by_priority.insert(order.clone()) {
    			return Some(order.clone());
    		}
    		let order_hash = order.hash.clone();
    		let order_gas_price = order.gas_price.clone();
    		let by_address_replaced = self.by_address.insert(sender, nonce, order);
    		// If transaction was replaced remove it from priority queue
    		if let Some(ref old_order) = by_address_replaced {
    			assert!(self.by_priority.remove(old_order), "hash is in `by_address`; all transactions in `by_address` must be in `by_priority`; qed");
    			assert!(self.by_gas_price.remove(&old_order.gas_price, &old_order.hash),
    				"hash is in `by_address`; all transactions' gas_prices in `by_address` must be in `by_gas_limit`; qed");
    		}
    		self.by_gas_price.insert(order_gas_price, order_hash);
    		assert_eq!(self.by_priority.len(), self.by_address.len());
    		assert_eq!(self.by_gas_price.values().map(|v| v.len()).fold(0, |a, b| a + b), self.by_address.len());
    		by_address_replaced
    	}

    	/// Remove low priority transactions if there is more than specified by given `limit`.
    	///
    	/// It drops transactions from this set but also removes associated `VerifiedTransaction`.
    	/// Returns addresses and lowest nonces of transactions removed because of limit.
    	fn enforce_limit(&mut self, by_hash: &mut HashMap<H256, VerifiedTransaction>, local: &mut LocalTransactionsList) -> Option<HashMap<Address, U256>> {
    		let mut count = 0;
    		let mut gas: U256 = 0.into();
    		let to_drop : Vec<(Address, U256)> = {
    			self.by_priority
    				.iter()
    				.filter(|order| {
    					count = count + 1;
    					let r = gas.overflowing_add(order.gas);
    					if r.1 { return false }
    					gas = r.0;
    					// Own and retracted transactions are allowed to go above all limits.
    					order.origin != TransactionOrigin::Local && order.origin != TransactionOrigin::RetractedBlock &&
    					(gas > self.gas_limit || count > self.limit)
    				})
    				.map(|order| by_hash.get(&order.hash)
    					.expect("All transactions in `self.by_priority` and `self.by_address` are kept in sync with `by_hash`."))
    				.map(|tx| (tx.sender(), tx.nonce()))
    				.collect()
    		};

    		Some(to_drop.into_iter()
    			.fold(HashMap::new(), |mut removed, (sender, nonce)| {
    				let order = self.drop(&sender, &nonce)
    					.expect("Transaction has just been found in `by_priority`; so it is in `by_address` also.");
    				trace!(target: "txqueue", "Dropped out of limit transaction: {:?}", order.hash);

    				let order = by_hash.remove(&order.hash)
    					.expect("hash is in `by_priorty`; all hashes in `by_priority` must be in `by_hash`; qed");

    				if order.origin.is_local() {
    					local.mark_dropped(order.transaction);
    				}

    				let min = removed.get(&sender).map_or(nonce, |val| cmp::min(*val, nonce));
    				removed.insert(sender, min);
    				removed
    			}))
    	}

    	/// Drop transaction from this set (remove from `by_priority` and `by_address`)
    	fn drop(&mut self, sender: &Address, nonce: &U256) -> Option<TransactionOrder> {
    		if let Some(tx_order) = self.by_address.remove(sender, nonce) {
    			assert!(self.by_gas_price.remove(&tx_order.gas_price, &tx_order.hash),
    				"hash is in `by_address`; all transactions' gas_prices in `by_address` must be in `by_gas_limit`; qed");
    			assert!(self.by_priority.remove(&tx_order),
    				"hash is in `by_address`; all transactions' gas_prices in `by_address` must be in `by_priority`; qed");
    			assert_eq!(self.by_priority.len(), self.by_address.len());
    			assert_eq!(self.by_gas_price.values().map(|v| v.len()).fold(0, |a, b| a + b), self.by_address.len());
    			return Some(tx_order);
    		}
    		assert_eq!(self.by_priority.len(), self.by_address.len());
    		assert_eq!(self.by_gas_price.values().map(|v| v.len()).fold(0, |a, b| a + b), self.by_address.len());
    		None
    	}

    	/// Drop all transactions.
    	fn clear(&mut self) {
    		self.by_priority.clear();
    		self.by_address.clear();
    		self.by_gas_price.backing.clear();
    	}

    	/// Sets new limit for number of transactions in this `TransactionSet`.
    	/// Note the limit is not applied (no transactions are removed) by calling this method.
    	fn set_limit(&mut self, limit: usize) {
    		self.limit = limit;
    	}

    	/// Get the minimum gas price that we can accept into this queue that wouldn't cause the transaction to
    	/// immediately be dropped. 0 if the queue isn't at capacity; 1 plus the lowest if it is.
    	fn gas_price_entry_limit(&self) -> U256 {
    		match self.by_gas_price.keys().next() {
    			Some(k) if self.by_priority.len() >= self.limit => *k + 1.into(),
    			_ => U256::default(),
    		}
    	}
    }

    #[derive(Debug)]
    /// Current status of the queue
    pub struct TransactionQueueStatus {
    	/// Number of pending transactions (ready to go to block)
    	pub pending: usize,
    	/// Number of future transactions (waiting for transactions with lower nonces first)
    	pub future: usize,
    }

    /// Details of account
    pub struct AccountDetails {
    	/// Most recent account nonce
    	pub nonce: U256,
    	/// Current account balance
    	pub balance: U256,
    }

    /// Transactions with `gas > (gas_limit + gas_limit * Factor(in percents))` are not imported to the queue.
    const GAS_LIMIT_HYSTERESIS: usize = 10; // (100/GAS_LIMIT_HYSTERESIS) %

    /// Describes the strategy used to prioritize transactions in the queue.
    #[cfg_attr(feature="dev", allow(enum_variant_names))]
    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub enum PrioritizationStrategy {
    	/// Use only gas price. Disregards the actual computation cost of the transaction.
    	/// i.e. Higher gas price = Higher priority
    	GasPriceOnly,
    	/// Use gas limit and then gas price.
    	/// i.e. Higher gas limit = Lower priority
    	GasAndGasPrice,
    	/// Calculate and use priority based on gas and gas price.
    	/// PRIORITY = GAS_PRICE - GAS/2^15 * MIN_GAS_PRICE
    	///
    	/// Rationale:
    	/// Heavy transactions are paying linear cost (GAS * GAS_PRICE)
    	/// while the computation might be more expensive.
    	///
    	/// i.e.
    	/// 1M gas tx with `gas_price=30*min` has the same priority
    	/// as 32k gas tx with `gas_price=min`
    	GasFactorAndGasPrice,
    }

    /// Point in time when transaction was inserted.
    pub type QueuingInstant = BlockNumber;
    const DEFAULT_QUEUING_PERIOD: BlockNumber = 128;

    /// `TransactionQueue` transaction details provider.
    pub trait TransactionDetailsProvider {
    	/// Fetch transaction-related account details.
    	fn fetch_account(&self, address: &Address) -> AccountDetails;
    	/// Estimate gas required for transaction.
    	fn estimate_gas_required(&self, tx: &SignedTransaction) -> U256;
    	/// Check if this service transaction can be accepted by `TransactionQueue`.
    	fn is_service_transaction_acceptable(&self, tx: &SignedTransaction) -> Result<bool, String>;
    }

    /// `TransactionQueue` implementation
    pub struct TransactionQueue {
    	/// Prioritization strategy for this queue
    	strategy: PrioritizationStrategy,
    	/// Gas Price threshold for transactions that can be imported to this queue (defaults to 0)
    	minimal_gas_price: U256,
    	/// The maximum amount of gas any individual transaction may use.
    	tx_gas_limit: U256,
    	/// Current gas limit (block gas limit * factor). Transactions above the limit will not be accepted (default to !0)
    	gas_limit: U256,
    	/// Maximal time transaction may occupy the queue.
    	/// When we reach `max_time_in_queue / 2^3` we re-validate
    	/// account balance.
    	max_time_in_queue: QueuingInstant,
    	/// Priority queue for transactions that can go to block
    	current: TransactionSet,
    	/// Priority queue for transactions that has been received but are not yet valid to go to block
    	future: TransactionSet,
    	/// All transactions managed by queue indexed by hash
    	by_hash: HashMap<H256, VerifiedTransaction>,
    	/// Last nonce of transaction in current (to quickly check next expected transaction)
    	last_nonces: HashMap<Address, U256>,
    	/// List of local transactions and their statuses.
    	local_transactions: LocalTransactionsList,
    }

    impl Default for TransactionQueue {
    	fn default() -> Self {
    		TransactionQueue::new(PrioritizationStrategy::GasPriceOnly)
    	}
    }

    impl TransactionQueue {
    	/// Creates new instance of this Queue
    	pub fn new(strategy: PrioritizationStrategy) -> Self {
    		Self::with_limits(strategy, 1024, !U256::zero(), !U256::zero())
    	}

    	/// Create new instance of this Queue with specified limits
    	pub fn with_limits(strategy: PrioritizationStrategy, limit: usize, gas_limit: U256, tx_gas_limit: U256) -> Self {
    		let current = TransactionSet {
    			by_priority: BTreeSet::new(),
    			by_address: Table::new(),
    			by_gas_price: Default::default(),
    			limit: limit,
    			gas_limit: gas_limit,
    		};

    		let future = TransactionSet {
    			by_priority: BTreeSet::new(),
    			by_address: Table::new(),
    			by_gas_price: Default::default(),
    			limit: limit,
    			gas_limit: gas_limit,
    		};

    		TransactionQueue {
    			strategy: strategy,
    			minimal_gas_price: U256::zero(),
    			tx_gas_limit: tx_gas_limit,
    			gas_limit: !U256::zero(),
    			max_time_in_queue: DEFAULT_QUEUING_PERIOD,
    			current: current,
    			future: future,
    			by_hash: HashMap::new(),
    			last_nonces: HashMap::new(),
    			local_transactions: LocalTransactionsList::default(),
    		}
    	}

    	/// Set the new limit for `current` and `future` queue.
    	pub fn set_limit(&mut self, limit: usize) {
    		self.current.set_limit(limit);
    		self.future.set_limit(limit);
    		// And ensure the limits
    		self.current.enforce_limit(&mut self.by_hash, &mut self.local_transactions);
    		self.future.enforce_limit(&mut self.by_hash, &mut self.local_transactions);
    	}

    	/// Returns current limit of transactions in the queue.
    	pub fn limit(&self) -> usize {
    		self.current.limit
    	}

    	/// Get the minimal gas price.
    	pub fn minimal_gas_price(&self) -> &U256 {
    		&self.minimal_gas_price
    	}

    	/// Sets new gas price threshold for incoming transactions.
    	/// Any transaction already imported to the queue is not affected.
    	pub fn set_minimal_gas_price(&mut self, min_gas_price: U256) {
    		self.minimal_gas_price = min_gas_price;
    	}

    	/// Get one more than the lowest gas price in the queue iff the pool is
    	/// full, otherwise 0.
    	pub fn effective_minimum_gas_price(&self) -> U256 {
    		self.current.gas_price_entry_limit()
    	}

    	/// Sets new gas limit. Transactions with gas slightly (`GAS_LIMIT_HYSTERESIS`) above the limit won't be imported.
    	/// Any transaction already imported to the queue is not affected.
    	pub fn set_gas_limit(&mut self, gas_limit: U256) {
    		let extra = gas_limit / U256::from(GAS_LIMIT_HYSTERESIS);

    		self.gas_limit = match gas_limit.overflowing_add(extra) {
    			(_, true) => !U256::zero(),
    			(val, false) => val,
    		};
    	}

    	/// Sets new total gas limit.
    	pub fn set_total_gas_limit(&mut self, gas_limit: U256) {
    		self.future.gas_limit = gas_limit;
    		self.current.gas_limit = gas_limit;
    		self.future.enforce_limit(&mut self.by_hash, &mut self.local_transactions);
    	}

    	/// Set the new limit for the amount of gas any individual transaction may have.
    	/// Any transaction already imported to the queue is not affected.
    	pub fn set_tx_gas_limit(&mut self, limit: U256) {
    		self.tx_gas_limit = limit;
    	}

    	/// Returns current status for this queue
    	pub fn status(&self) -> TransactionQueueStatus {
    		TransactionQueueStatus {
    			pending: self.current.by_priority.len(),
    			future: self.future.by_priority.len(),
    		}
    	}

    	/// Add signed transaction to queue to be verified and imported.
    	///
    	/// NOTE details_provider methods should be cheap to compute
    	/// otherwise it might open up an attack vector.
    	pub fn add(
    		&mut self,
    		tx: SignedTransaction,
    		origin: TransactionOrigin,
    		time: QueuingInstant,
    		min_block: Option<BlockNumber>,
    		details_provider: &TransactionDetailsProvider,
    	) -> Result<TransactionImportResult, Error> {
    		if origin == TransactionOrigin::Local {
    			let hash = tx.hash();
    			let cloned_tx = tx.clone();

    			let result = self.add_internal(tx, origin, time, min_block, details_provider);
    			match result {
    				Ok(TransactionImportResult::Current) => {
    					self.local_transactions.mark_pending(hash);
    				},
    				Ok(TransactionImportResult::Future) => {
    					self.local_transactions.mark_future(hash);
    				},
    				Err(Error::Transaction(ref err)) => {
    					// Sometimes transactions are re-imported, so
    					// don't overwrite transactions if they are already on the list
    					if !self.local_transactions.contains(&hash) {
    						self.local_transactions.mark_rejected(cloned_tx, err.clone());
    					}
    				},
    				Err(_) => {
    					self.local_transactions.mark_invalid(cloned_tx);
    				},
    			}
    			result
    		} else {
    			self.add_internal(tx, origin, time, min_block, details_provider)
    		}
    	}

    	/// Adds signed transaction to the queue.
    	fn add_internal(
    		&mut self,
    		tx: SignedTransaction,
    		origin: TransactionOrigin,
    		time: QueuingInstant,
    		min_block: Option<BlockNumber>,
    		details_provider: &TransactionDetailsProvider,
    	) -> Result<TransactionImportResult, Error> {
    		if origin != TransactionOrigin::Local && tx.gas_price < self.minimal_gas_price {
    			// if it is non-service-transaction => drop
    			let is_service_transaction = tx.gas_price.is_zero();
    			if !is_service_transaction {
    				trace!(target: "txqueue",
    					"Dropping transaction below minimal gas price threshold: {:?} (gp: {} < {})",
    					tx.hash(),
    					tx.gas_price,
    					self.minimal_gas_price
    				);

    				return Err(Error::Transaction(TransactionError::InsufficientGasPrice {
    					minimal: self.minimal_gas_price,
    					got: tx.gas_price,
    				}));
    			}

    			let is_service_transaction_accepted = match details_provider.is_service_transaction_acceptable(&tx) {
    				Ok(true) => true,
    				Ok(false) => {
    					trace!(target: "txqueue",
    						"Dropping service transaction as sender is not certified to send service transactions: {:?} (sender: {:?})",
    						tx.hash(),
    						tx.sender(),
    					);

    					false
    				},
    				Err(contract_err) => {
    					trace!(target: "txqueue",
    						"Dropping service transaction as service contract returned error: {:?} (error: {:?})",
    						tx.hash(),
    						contract_err,
    					);

    					false
    				},
    			};

    			if !is_service_transaction_accepted {
    				return Err(Error::Transaction(TransactionError::InsufficientGasPrice {
    						minimal: self.minimal_gas_price,
    						got: tx.gas_price,
    					}));
    			}
    		}

    		let full_queues_lowest = self.effective_minimum_gas_price();
    		if tx.gas_price < full_queues_lowest && origin != TransactionOrigin::Local {
    			trace!(target: "txqueue",
    				"Dropping transaction below lowest gas price in a full queue: {:?} (gp: {} < {})",
    				tx.hash(),
    				tx.gas_price,
    				full_queues_lowest
    			);

    			return Err(Error::Transaction(TransactionError::InsufficientGasPrice {
    				minimal: full_queues_lowest,
    				got: tx.gas_price,
    			}));
    		}

    		if tx.gas > self.gas_limit || tx.gas > self.tx_gas_limit {
    			trace!(target: "txqueue",
    				"Dropping transaction above gas limit: {:?} ({} > min({}, {}))",
    				tx.hash(),
    				tx.gas,
    				self.gas_limit,
    				self.tx_gas_limit
    			);
    			return Err(Error::Transaction(TransactionError::GasLimitExceeded {
    				limit: self.gas_limit,
    				got: tx.gas,
    			}));
    		}

    		let minimal_gas = details_provider.estimate_gas_required(&tx);
    		if tx.gas < minimal_gas {
    			trace!(target: "txqueue",
    				"Dropping transaction with insufficient gas: {:?} ({} > {})",
    				tx.hash(),
    				tx.gas,
    				minimal_gas,
    			);

    			return Err(Error::Transaction(TransactionError::InsufficientGas {
    				minimal: minimal_gas,
    				got: tx.gas,
    			}));
    		}

    		let client_account = details_provider.fetch_account(&tx.sender());
    		let cost = tx.value + tx.gas_price * tx.gas;
    		if client_account.balance < cost {
    			trace!(target: "txqueue",
    				"Dropping transaction without sufficient balance: {:?} ({} < {})",
    				tx.hash(),
    				client_account.balance,
    				cost
    			);

    			return Err(Error::Transaction(TransactionError::InsufficientBalance {
    				cost: cost,
    				balance: client_account.balance
    			}));
    		}
    		tx.check_low_s()?;
    		// No invalid transactions beyond this point.
    		let vtx = VerifiedTransaction::new(tx, origin, time, min_block);
    		let r = self.import_tx(vtx, client_account.nonce).map_err(Error::Transaction);
    		assert_eq!(self.future.by_priority.len() + self.current.by_priority.len(), self.by_hash.len());
    		r
    	}

    	/// Removes all transactions from particular sender up to (excluding) given client (state) nonce.
    	/// Client (State) Nonce = next valid nonce for this sender.
    	pub fn cull(&mut self, sender: Address, client_nonce: U256) {
    		// Check if there is anything in current...
    		let should_check_in_current = self.current.by_address.row(&sender)
    			// If nonce == client_nonce nothing is changed
    			.and_then(|by_nonce| by_nonce.keys().find(|nonce| *nonce < &client_nonce))
    			.map(|_| ());
    		// ... or future
    		let should_check_in_future = self.future.by_address.row(&sender)
    			// if nonce == client_nonce we need to promote to current
    			.and_then(|by_nonce| by_nonce.keys().find(|nonce| *nonce <= &client_nonce))
    			.map(|_| ());

    		if should_check_in_current.or(should_check_in_future).is_none() {
    			return;
    		}

    		self.cull_internal(sender, client_nonce);
    	}

    	/// Always updates future and moves transactions from current to future.
    	fn cull_internal(&mut self, sender: Address, client_nonce: U256) {
    		// We will either move transaction to future or remove it completely
    		// so there will be no transactions from this sender in current
    		self.last_nonces.remove(&sender);
    		// First update height of transactions in future to avoid collisions
    		self.update_future(&sender, client_nonce);
    		// This should move all current transactions to future and remove old transactions
    		self.move_all_to_future(&sender, client_nonce);
    		// And now lets check if there is some batch of transactions in future
    		// that should be placed in current. It should also update last_nonces.
    		self.move_matching_future_to_current(sender, client_nonce, client_nonce);
    		assert_eq!(self.future.by_priority.len() + self.current.by_priority.len(), self.by_hash.len());
    	}

    	/// Checks the current nonce for all transactions' senders in the queue and removes the old transactions.
    	pub fn remove_old<F>(&mut self, fetch_account: &F, current_time: QueuingInstant) where
    		F: Fn(&Address) -> AccountDetails,
    	{
    		let senders = self.current.by_address.keys()
    			.chain(self.future.by_address.keys())
    			.map(|sender| (*sender, fetch_account(sender)))
    			.collect::<HashMap<_, _>>();

    		for (sender, details) in senders.iter() {
    			self.cull(*sender, details.nonce);
    		}

    		let max_time = self.max_time_in_queue;
    		let balance_check = max_time >> 3;
    		// Clear transactions occupying the queue too long
    		let invalid = self.by_hash.iter()
    			.filter(|&(_, ref tx)| !tx.origin.is_local())
    			.map(|(hash, tx)| (hash, tx, current_time.saturating_sub(tx.insertion_time)))
    			.filter_map(|(hash, tx, time_diff)| {
    				if time_diff > max_time {
    					return Some(*hash);
    				}

    				if time_diff > balance_check {
    					return match senders.get(&tx.sender()) {
    						Some(details) if tx.cost() > details.balance => {
    							Some(*hash)
    						},
    						_ => None,
    					};
    				}

    				None
    			})
    			.collect::<Vec<_>>();
    		let fetch_nonce = |a: &Address| senders.get(a)
    			.expect("We fetch details for all senders from both current and future")
    			.nonce;
    		for hash in invalid {
    			self.remove_invalid(&hash, &fetch_nonce);
    		}
    	}

    	/// Penalize transactions from sender of transaction with given hash.
    	/// I.e. it should change the priority of the transaction in the queue.
    	///
    	/// NOTE: We need to penalize all transactions from particular sender
    	/// to avoid breaking invariants in queue (ordered by nonces).
    	/// Consecutive transactions from this sender would fail otherwise (because of invalid nonce).
    	pub fn penalize(&mut self, transaction_hash: &H256) {
    		let transaction = match self.by_hash.get(transaction_hash) {
    			None => return,
    			Some(t) => t,
    		};

    		// Never penalize local transactions
    		if transaction.origin.is_local() {
    			return;
    		}

    		let sender = transaction.sender();

    		// Penalize all transactions from this sender
    		let nonces_from_sender = match self.current.by_address.row(&sender) {
    			Some(row_map) => row_map.keys().cloned().collect::<Vec<U256>>(),
    			None => vec![],
    		};
    		for k in nonces_from_sender {
    			let order = self.current.drop(&sender, &k).expect("transaction known to be in self.current; qed");
    			self.current.insert(sender, k, order.penalize());
    		}
    		// Same thing for future
    		let nonces_from_sender = match self.future.by_address.row(&sender) {
    			Some(row_map) => row_map.keys().cloned().collect::<Vec<U256>>(),
    			None => vec![],
    		};
    		for k in nonces_from_sender {
    			let order = self.future.drop(&sender, &k).expect("transaction known to be in self.future; qed");
    			self.future.insert(sender, k, order.penalize());
    		}
    	}

    	/// Removes invalid transaction identified by hash from queue.
    	/// Assumption is that this transaction nonce is not related to client nonce,
    	/// so transactions left in queue are processed according to client nonce.
    	///
    	/// If gap is introduced marks subsequent transactions as future
    	pub fn remove_invalid<F>(&mut self, transaction_hash: &H256, fetch_nonce: &F)
    		where F: Fn(&Address) -> U256 {

    		assert_eq!(self.future.by_priority.len() + self.current.by_priority.len(), self.by_hash.len());
    		let transaction = self.by_hash.remove(transaction_hash);
    		if transaction.is_none() {
    			// We don't know this transaction
    			return;
    		}

    		let transaction = transaction.expect("None is tested in early-exit condition above; qed");
    		let sender = transaction.sender();
    		let nonce = transaction.nonce();
    		let current_nonce = fetch_nonce(&sender);

    		trace!(target: "txqueue", "Removing invalid transaction: {:?}", transaction.hash());

    		// Mark in locals
    		if self.local_transactions.contains(transaction_hash) {
    			self.local_transactions.mark_invalid(transaction.transaction.into());
    		}

    		// Remove from future
    		let order = self.future.drop(&sender, &nonce);
    		if order.is_some() {
    			self.update_future(&sender, current_nonce);
    			// And now lets check if there is some chain of transactions in future
    			// that should be placed in current
    			self.move_matching_future_to_current(sender, current_nonce, current_nonce);
    			assert_eq!(self.future.by_priority.len() + self.current.by_priority.len(), self.by_hash.len());
    			return;
    		}

    		// Remove from current
    		let order = self.current.drop(&sender, &nonce);
    		if order.is_some() {
    			// This will keep consistency in queue
    			// Moves all to future and then promotes a batch from current:
    			self.cull_internal(sender, current_nonce);
    			assert_eq!(self.future.by_priority.len() + self.current.by_priority.len(), self.by_hash.len());
    			return;
    		}
    	}

    	/// Marks all transactions from particular sender as local transactions
    	fn mark_transactions_local(&mut self, sender: &Address) {
    		fn mark_local<F: FnMut(H256)>(sender: &Address, set: &mut TransactionSet, mut mark: F) {
    			// Mark all transactions from this sender as local
    			let nonces_from_sender = set.by_address.row(sender)
    				.map(|row_map| {
    					row_map.iter().filter_map(|(nonce, order)| if order.origin.is_local() {
    						None
    					} else {
    						Some(*nonce)
    					}).collect::<Vec<U256>>()
    				})
    				.unwrap_or_else(Vec::new);

    			for k in nonces_from_sender {
    				let mut order = set.drop(sender, &k).expect("transaction known to be in self.current/self.future; qed");
    				order.origin = TransactionOrigin::Local;
    				mark(order.hash);
    				set.insert(*sender, k, order);
    			}
    		}

    		let local = &mut self.local_transactions;
    		mark_local(sender, &mut self.current, |hash| local.mark_pending(hash));
    		mark_local(sender, &mut self.future, |hash| local.mark_future(hash));
    	}

    	/// Update height of all transactions in future transactions set.
    	fn update_future(&mut self, sender: &Address, current_nonce: U256) {
    		// We need to drain all transactions for current sender from future and reinsert them with updated height
    		let all_nonces_from_sender = match self.future.by_address.row(sender) {
    			Some(row_map) => row_map.keys().cloned().collect::<Vec<U256>>(),
    			None => vec![],
    		};
    		for k in all_nonces_from_sender {
    			let order = self.future.drop(sender, &k).expect("iterating over a collection that has been retrieved above; qed");
    			if k >= current_nonce {
    				self.future.insert(*sender, k, order.update_height(k, current_nonce));
    			} else {
    				trace!(target: "txqueue", "Removing old transaction: {:?} (nonce: {} < {})", order.hash, k, current_nonce);
    				// Remove the transaction completely
    				self.by_hash.remove(&order.hash).expect("All transactions in `future` are also in `by_hash`");
    			}
    		}
    	}

    	/// Drop all transactions from given sender from `current`.
    	/// Either moves them to `future` or removes them from queue completely.
    	fn move_all_to_future(&mut self, sender: &Address, current_nonce: U256) {
    		let all_nonces_from_sender = match self.current.by_address.row(sender) {
    			Some(row_map) => row_map.keys().cloned().collect::<Vec<U256>>(),
    			None => vec![],
    		};

    		for k in all_nonces_from_sender {
    			// Goes to future or is removed
    			let order = self.current.drop(sender, &k).expect("iterating over a collection that has been retrieved above;
    															 qed");
    			if k >= current_nonce {
    				let order = order.update_height(k, current_nonce);
    				if order.origin.is_local() {
    					self.local_transactions.mark_future(order.hash);
    				}
    				if let Some(old) = self.future.insert(*sender, k, order.clone()) {
    					Self::replace_orders(*sender, k, old, order, &mut self.future, &mut self.by_hash, &mut self.local_transactions);
    				}
    			} else {
    				trace!(target: "txqueue", "Removing old transaction: {:?} (nonce: {} < {})", order.hash, k, current_nonce);
    				let tx = self.by_hash.remove(&order.hash).expect("All transactions in `future` are also in `by_hash`");
    				if tx.origin.is_local() {
    					self.local_transactions.mark_mined(tx.transaction);
    				}
    			}
    		}
    		self.future.enforce_limit(&mut self.by_hash, &mut self.local_transactions);
    	}

    	/// Returns top transactions from the queue ordered by priority.
    	pub fn top_transactions(&self) -> Vec<SignedTransaction> {
    		self.top_transactions_at(BlockNumber::max_value())

    	}

    	fn filter_pending_transaction<F>(&self, best_block: BlockNumber, mut f: F)
    		where F: FnMut(&VerifiedTransaction) {

    		let mut delayed = HashSet::new();
    		for t in self.current.by_priority.iter() {
    			let tx = self.by_hash.get(&t.hash).expect("All transactions in `current` and `future` are always included in `by_hash`");
    			let sender = tx.sender();
    			if delayed.contains(&sender) {
    				continue;
    			}
    			if tx.min_block.unwrap_or(0) > best_block {
    				delayed.insert(sender);
    				continue;
    			}
    			f(&tx);
    		}
    	}

    	/// Returns top transactions from the queue ordered by priority.
    	pub fn top_transactions_at(&self, best_block: BlockNumber) -> Vec<SignedTransaction> {
    		let mut r = Vec::new();
    		self.filter_pending_transaction(best_block, |tx| r.push(tx.transaction.clone()));
    		r
    	}

    	/// Return all ready transactions.
    	pub fn pending_transactions(&self, best_block: BlockNumber) -> Vec<PendingTransaction> {
    		let mut r = Vec::new();
    		self.filter_pending_transaction(best_block, |tx| r.push(PendingTransaction::new(tx.transaction.clone(), tx.min_block)));
    		r
    	}

    	/// Return all ready transactions.
    	pub fn future_transactions(&self) -> Vec<PendingTransaction> {
    		self.future.by_priority
    			.iter()
    			.map(|t| self.by_hash.get(&t.hash).expect("All transactions in `current` and `future` are always included in `by_hash`"))
    			.map(|t| PendingTransaction { transaction: t.transaction.clone(), min_block: t.min_block })
    			.collect()
    	}

    	/// Returns local transactions (some of them might not be part of the queue anymore).
    	pub fn local_transactions(&self) -> &LinkedHashMap<H256, LocalTransactionStatus> {
    		self.local_transactions.all_transactions()
    	}

    	/// Returns hashes of all transactions from current, ordered by priority.
    	pub fn pending_hashes(&self) -> Vec<H256> {
    		self.current.by_priority
    			.iter()
    			.map(|t| t.hash)
    			.collect()
    	}

    	/// Returns true if there is at least one local transaction pending
    	pub fn has_local_pending_transactions(&self) -> bool {
    		self.current.by_priority.iter().any(|tx| tx.origin == TransactionOrigin::Local)
    	}

    	/// Finds transaction in the queue by hash (if any)
    	pub fn find(&self, hash: &H256) -> Option<SignedTransaction> {
    		self.by_hash.get(hash).map(|tx| tx.transaction.clone())
    	}

    	/// Removes all elements (in any state) from the queue
    	pub fn clear(&mut self) {
    		self.current.clear();
    		self.future.clear();
    		self.by_hash.clear();
    		self.last_nonces.clear();
    	}

    	/// Returns highest transaction nonce for given address.
    	pub fn last_nonce(&self, address: &Address) -> Option<U256> {
    		self.last_nonces.get(address).cloned()
    	}

    	/// Checks if there are any transactions in `future` that should actually be promoted to `current`
    	/// (because nonce matches).
    	fn move_matching_future_to_current(&mut self, address: Address, mut current_nonce: U256, first_nonce: U256) {
    		let mut update_last_nonce_to = None;
    		{
    			let by_nonce = self.future.by_address.row_mut(&address);
    			if by_nonce.is_none() {
    				return;
    			}
    			let mut by_nonce = by_nonce.expect("None is tested in early-exit condition above; qed");
    			while let Some(order) = by_nonce.remove(&current_nonce) {
    				// remove also from priority and gas_price
    				self.future.by_priority.remove(&order);
    				self.future.by_gas_price.remove(&order.gas_price, &order.hash);
    				// Put to current
    				let order = order.update_height(current_nonce, first_nonce);
    				if order.origin.is_local() {
    					self.local_transactions.mark_pending(order.hash);
    				}
    				if let Some(old) = self.current.insert(address, current_nonce, order.clone()) {
    					Self::replace_orders(address, current_nonce, old, order, &mut self.current, &mut self.by_hash, &mut self.local_transactions);
    				}
    				update_last_nonce_to = Some(current_nonce);
    				current_nonce = current_nonce + U256::one();
    			}
    		}
    		self.future.by_address.clear_if_empty(&address);
    		if let Some(x) = update_last_nonce_to {
    			// Update last inserted nonce
    			self.last_nonces.insert(address, x);
    		}
    	}

    	/// Adds VerifiedTransaction to this queue.
    	///
    	/// Determines if it should be placed in current or future. When transaction is
    	/// imported to `current` also checks if there are any `future` transactions that should be promoted because of
    	/// this.
    	///
    	/// It ignores transactions that has already been imported (same `hash`) and replaces the transaction
    	/// iff `(address, nonce)` is the same but `gas_price` is higher.
    	///
    	/// Returns `true` when transaction was imported successfuly
    	fn import_tx(&mut self, tx: VerifiedTransaction, state_nonce: U256) -> Result<TransactionImportResult, TransactionError> {

    		if self.by_hash.get(&tx.hash()).is_some() {
    			// Transaction is already imported.
    			trace!(target: "txqueue", "Dropping already imported transaction: {:?}", tx.hash());
    			return Err(TransactionError::AlreadyImported);
    		}

    		let min_gas_price = (self.minimal_gas_price, self.strategy);
    		let address = tx.sender();
    		let nonce = tx.nonce();
    		let hash = tx.hash();

    		// The transaction might be old, let's check that.
    		// This has to be the first test, otherwise calculating
    		// nonce height would result in overflow.
    		if nonce < state_nonce {
    			// Droping transaction
    			trace!(target: "txqueue", "Dropping old transaction: {:?} (nonce: {} < {})", tx.hash(), nonce, state_nonce);
    			return Err(TransactionError::Old);
    		}

    		// Update nonces of transactions in future (remove old transactions)
    		self.update_future(&address, state_nonce);
    		// State nonce could be updated. Maybe there are some more items waiting in future?
    		self.move_matching_future_to_current(address, state_nonce, state_nonce);
    		// Check the next expected nonce (might be updated by move above)
    		let next_nonce = self.last_nonces
    			.get(&address)
    			.cloned()
    			.map_or(state_nonce, |n| n + U256::one());

    		if tx.origin.is_local() {
    			self.mark_transactions_local(&address);
    		}

    		// Future transaction
    		if nonce > next_nonce {
    			// We have a gap - put to future.
    			// Insert transaction (or replace old one with lower gas price)
    			check_too_cheap(
    				Self::replace_transaction(tx, state_nonce, min_gas_price, &mut self.future, &mut self.by_hash, &mut self.local_transactions)
    			)?;
    			// Enforce limit in Future
    			let removed = self.future.enforce_limit(&mut self.by_hash, &mut self.local_transactions);
    			// Return an error if this transaction was not imported because of limit.
    			check_if_removed(&address, &nonce, removed)?;

    			debug!(target: "txqueue", "Importing transaction to future: {:?}", hash);
    			debug!(target: "txqueue", "status: {:?}", self.status());
    			return Ok(TransactionImportResult::Future);
    		}

    		// We might have filled a gap - move some more transactions from future
    		self.move_matching_future_to_current(address, nonce, state_nonce);
    		self.move_matching_future_to_current(address, nonce + U256::one(), state_nonce);

    		// Replace transaction if any
    		check_too_cheap(
    			Self::replace_transaction(tx, state_nonce, min_gas_price, &mut self.current, &mut self.by_hash, &mut self.local_transactions)
    		)?;
    		// Keep track of highest nonce stored in current
    		let new_max = self.last_nonces.get(&address).map_or(nonce, |n| cmp::max(nonce, *n));
    		self.last_nonces.insert(address, new_max);

    		// Also enforce the limit
    		let removed = self.current.enforce_limit(&mut self.by_hash, &mut self.local_transactions);
    		// If some transaction were removed because of limit we need to update last_nonces also.
    		self.update_last_nonces(&removed);
    		// Trigger error if the transaction we are importing was removed.
    		check_if_removed(&address, &nonce, removed)?;

    		debug!(target: "txqueue", "Imported transaction to current: {:?}", hash);
    		debug!(target: "txqueue", "status: {:?}", self.status());
    		Ok(TransactionImportResult::Current)
    	}

    	/// Updates
    	fn update_last_nonces(&mut self, removed_min_nonces: &Option<HashMap<Address, U256>>) {
    		if let Some(ref min_nonces) = *removed_min_nonces {
    			for (sender, nonce) in min_nonces.iter() {
    				if *nonce == U256::zero() {
    					self.last_nonces.remove(sender);
    				} else {
    					self.last_nonces.insert(*sender, *nonce - U256::one());
    				}
    			}
    		}
    	}

    	/// Replaces transaction in given set (could be `future` or `current`).
    	///
    	/// If there is already transaction with same `(sender, nonce)` it will be replaced iff `gas_price` is higher.
    	/// One of the transactions is dropped from set and also removed from queue entirely (from `by_hash`).
    	///
    	/// Returns `true` if transaction actually got to the queue (`false` if there was already a transaction with higher
    	/// gas_price)
    	fn replace_transaction(
    		tx: VerifiedTransaction,
    		base_nonce: U256,
    		min_gas_price: (U256, PrioritizationStrategy),
    		set: &mut TransactionSet,
    		by_hash: &mut HashMap<H256, VerifiedTransaction>,
    		local: &mut LocalTransactionsList,
    	) -> bool {
    		let order = TransactionOrder::for_transaction(&tx, base_nonce, min_gas_price.0, min_gas_price.1);
    		let hash = tx.hash();
    		let address = tx.sender();
    		let nonce = tx.nonce();

    		let old_hash = by_hash.insert(hash, tx);
    		assert!(old_hash.is_none(), "Each hash has to be inserted exactly once.");

    		trace!(target: "txqueue", "Inserting: {:?}", order);

    		if let Some(old) = set.insert(address, nonce, order.clone()) {
    			Self::replace_orders(address, nonce, old, order, set, by_hash, local)
    		} else {
    			true
    		}
    	}

    	fn replace_orders(
    		address: Address,
    		nonce: U256,
    		old: TransactionOrder,
    		order: TransactionOrder,
    		set: &mut TransactionSet,
    		by_hash: &mut HashMap<H256, VerifiedTransaction>,
    		local: &mut LocalTransactionsList,
    	) -> bool {
    		// There was already transaction in queue. Let's check which one should stay
    		let old_hash = old.hash;
    		let new_hash = order.hash;
    		let old_fee = old.gas_price;
    		let new_fee = order.gas_price;
    		if old_fee.cmp(&new_fee) == Ordering::Greater {
    			trace!(target: "txqueue", "Didn't insert transaction because gas price was too low: {:?} ({:?} stays in the queue)", order.hash, old.hash);
    			// Put back old transaction since it has greater priority (higher gas_price)
    			set.insert(address, nonce, old);
    			// and remove new one
    			let order = by_hash.remove(&order.hash).expect("The hash has been just inserted and no other line is altering `by_hash`.");
    			if order.origin.is_local() {
    				local.mark_replaced(order.transaction, old_fee, old_hash);
    			}
    			false
    		} else {
    			trace!(target: "txqueue", "Replaced transaction: {:?} with transaction with higher gas price: {:?}", old.hash, order.hash);
    			// Make sure we remove old transaction entirely
    			let old = by_hash.remove(&old.hash).expect("The hash is coming from `future` so it has to be in `by_hash`.");
    			if old.origin.is_local() {
    				local.mark_replaced(old.transaction, new_fee, new_hash);
    			}
    			true
    		}
    	}
    }

    fn check_too_cheap(is_in: bool) -> Result<(), TransactionError> {
    	if is_in {
    		Ok(())
    	} else {
    		Err(TransactionError::TooCheapToReplace)
    	}
    }

    fn check_if_removed(sender: &Address, nonce: &U256, dropped: Option<HashMap<Address, U256>>) -> Result<(), TransactionError> {
    	match dropped {
    		Some(ref dropped) => match dropped.get(sender) {
    			Some(min) if nonce >= min => {
    				Err(TransactionError::LimitReached)
    			},
    			_ => Ok(()),
    		},
    		_ => Ok(()),
    	}
    }

    #[cfg(test)]
    pub mod test {
    	extern crate rustc_serialize;
    	use util::table::*;
    	use util::*;
    	use ethkey::{Random, Generator};
    	use error::{Error, TransactionError};
    	use super::*;
    	use super::{TransactionSet, TransactionOrder, VerifiedTransaction};
    	use miner::local_transactions::LocalTransactionsList;
    	use client::TransactionImportResult;
    	use transaction::{SignedTransaction, Transaction, Action};

    	pub struct DummyTransactionDetailsProvider {
    		account_details: AccountDetails,
    		gas_required: U256,
    		service_transactions_check_result: Result<bool, String>,
    	}

    	impl Default for DummyTransactionDetailsProvider {
    		fn default() -> Self {
    			DummyTransactionDetailsProvider {
    				account_details: default_account_details(),
    				gas_required: U256::zero(),
    				service_transactions_check_result: Ok(false),
    			}
    		}
    	}

    	impl DummyTransactionDetailsProvider {
    		pub fn with_account(mut self, account_details: AccountDetails) -> Self {
    			self.account_details = account_details;
    			self
    		}

    		pub fn with_account_nonce(mut self, nonce: U256) -> Self {
    			self.account_details.nonce = nonce;
    			self
    		}

    		pub fn with_tx_gas_required(mut self, gas_required: U256) -> Self {
    			self.gas_required = gas_required;
    			self
    		}

    		pub fn service_transaction_checker_returns_error(mut self, error: &str) -> Self {
    			self.service_transactions_check_result = Err(error.to_owned());
    			self
    		}

    		pub fn service_transaction_checker_accepts(mut self, accepts: bool) -> Self {
    			self.service_transactions_check_result = Ok(accepts);
    			self
    		}
    	}

    	impl TransactionDetailsProvider for DummyTransactionDetailsProvider {
    		fn fetch_account(&self, _address: &Address) -> AccountDetails {
    			AccountDetails {
    				nonce: self.account_details.nonce,
    				balance: self.account_details.balance,
    			}
    		}

    		fn estimate_gas_required(&self, _tx: &SignedTransaction) -> U256 {
    			self.gas_required
    		}

    		fn is_service_transaction_acceptable(&self, _tx: &SignedTransaction) -> Result<bool, String> {
    			self.service_transactions_check_result.clone()
    		}
    	}

    	fn unwrap_tx_err(err: Result<TransactionImportResult, Error>) -> TransactionError {
    		match err.unwrap_err() {
    			Error::Transaction(e) => e,
    			_ => panic!("Expected transaction error!"),
    		}
    	}

    	fn default_nonce() -> U256 { 123.into() }
    	fn default_gas_val() -> U256 { 100_000.into() }
    	fn default_gas_price() -> U256 { 1.into() }

    	fn new_unsigned_tx(nonce: U256, gas: U256, gas_price: U256) -> Transaction {
    		Transaction {
    			action: Action::Create,
    			value: U256::from(100),
    			data: "3331600055".from_hex().unwrap(),
    			gas: gas,
    			gas_price: gas_price,
    			nonce: nonce
    		}
    	}

    	fn new_tx(nonce: U256, gas_price: U256) -> SignedTransaction {
    		let keypair = Random.generate().unwrap();
    		new_unsigned_tx(nonce, default_gas_val(), gas_price).sign(keypair.secret(), None)
    	}

    	fn new_tx_with_gas(gas: U256, gas_price: U256) -> SignedTransaction {
    		let keypair = Random.generate().unwrap();
    		new_unsigned_tx(default_nonce(), gas, gas_price).sign(keypair.secret(), None)
    	}

    	fn new_tx_default() -> SignedTransaction {
    		new_tx(default_nonce(), default_gas_price())
    	}

    	fn default_account_details() -> AccountDetails {
    		AccountDetails {
    			nonce: default_nonce(),
    			balance: !U256::zero()
    		}
    	}

    	fn default_account_details_for_addr(_a: &Address) -> AccountDetails {
    		default_account_details()
    	}

    	fn default_tx_provider() -> DummyTransactionDetailsProvider {
    		DummyTransactionDetailsProvider::default()
    	}

    	fn new_tx_pair(nonce: U256, gas_price: U256, nonce_increment: U256, gas_price_increment: U256) -> (SignedTransaction, SignedTransaction) {
    		let tx1 = new_unsigned_tx(nonce, default_gas_val(), gas_price);
    		let tx2 = new_unsigned_tx(nonce + nonce_increment, default_gas_val(), gas_price + gas_price_increment);

    		let keypair = Random.generate().unwrap();
    		let secret = &keypair.secret();
    		(tx1.sign(secret, None).into(), tx2.sign(secret, None).into())
    	}

    	/// Returns two consecutive transactions, both with increased gas price
    	fn new_tx_pair_with_gas_price_increment(gas_price_increment: U256) -> (SignedTransaction, SignedTransaction) {
    		let gas = default_gas_price() + gas_price_increment;
    		let tx1 = new_unsigned_tx(default_nonce(), default_gas_val(), gas);
    		let tx2 = new_unsigned_tx(default_nonce() + 1.into(), default_gas_val(), gas);

    		let keypair = Random.generate().unwrap();
    		let secret = &keypair.secret();
    		(tx1.sign(secret, None).into(), tx2.sign(secret, None).into())
    	}

    	fn new_tx_pair_default(nonce_increment: U256, gas_price_increment: U256) -> (SignedTransaction, SignedTransaction) {
    		new_tx_pair(default_nonce(), default_gas_price(), nonce_increment, gas_price_increment)
    	}

    	/// Returns two transactions with identical (sender, nonce) but different gas price/hash.
    	fn new_similar_tx_pair() -> (SignedTransaction, SignedTransaction) {
    		new_tx_pair_default(0.into(), 1.into())
    	}

    	#[test]
    	fn test_ordering() {
    		assert_eq!(TransactionOrigin::Local.cmp(&TransactionOrigin::External), Ordering::Less);
    		assert_eq!(TransactionOrigin::RetractedBlock.cmp(&TransactionOrigin::Local), Ordering::Less);
    		assert_eq!(TransactionOrigin::RetractedBlock.cmp(&TransactionOrigin::External), Ordering::Less);

    		assert_eq!(TransactionOrigin::External.cmp(&TransactionOrigin::Local), Ordering::Greater);
    		assert_eq!(TransactionOrigin::Local.cmp(&TransactionOrigin::RetractedBlock), Ordering::Greater);
    		assert_eq!(TransactionOrigin::External.cmp(&TransactionOrigin::RetractedBlock), Ordering::Greater);
    	}

    	fn transaction_order(tx: &VerifiedTransaction, nonce: U256) -> TransactionOrder {
    		TransactionOrder::for_transaction(tx, nonce, 0.into(), PrioritizationStrategy::GasPriceOnly)
    	}

    	#[test]
    	fn should_return_correct_nonces_when_dropped_because_of_limit() {
    		// given
    		let mut txq = TransactionQueue::with_limits(PrioritizationStrategy::GasPriceOnly, 2, !U256::zero(), !U256::zero());
    		let (tx1, tx2) = new_tx_pair(123.into(), 1.into(), 1.into(), 0.into());
    		let sender = tx1.sender();
    		let nonce = tx1.nonce;
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().pending, 2);
    		assert_eq!(txq.last_nonce(&sender), Some(nonce + 1.into()));

    		// when
    		let tx = new_tx(123.into(), 1.into());
    		let res = txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider());

    		// then
    		// No longer the case as we don't even consider a transaction that isn't above a full
    		// queue's minimum gas price.
    		// We may want to reconsider this in the near future so leaving this code in as a
    		// possible alternative.
    		/*
    		assert_eq!(res.unwrap(), TransactionImportResult::Current);
    		assert_eq!(txq.status().pending, 2);
    		assert_eq!(txq.last_nonce(&sender), Some(nonce));
    		*/
    		assert_eq!(unwrap_tx_err(res), TransactionError::InsufficientGasPrice {
    			minimal: 2.into(),
    			got: 1.into(),
    		});
    		assert_eq!(txq.status().pending, 2);
    		assert_eq!(txq.last_nonce(&sender), Some(tx2.nonce));
    	}

    	#[test]
    	fn should_create_transaction_set() {
    		// given
    		let mut local = LocalTransactionsList::default();
    		let mut set = TransactionSet {
    			by_priority: BTreeSet::new(),
    			by_address: Table::new(),
    			by_gas_price: Default::default(),
    			limit: 1,
    			gas_limit: !U256::zero(),
    		};
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let tx1 = VerifiedTransaction::new(tx1, TransactionOrigin::External, 0, None);
    		let tx2 = VerifiedTransaction::new(tx2, TransactionOrigin::External, 0, None);
    		let mut by_hash = {
    			let mut x = HashMap::new();
    			let tx1 = VerifiedTransaction::new(tx1.transaction.clone(), TransactionOrigin::External, 0, None);
    			let tx2 = VerifiedTransaction::new(tx2.transaction.clone(), TransactionOrigin::External, 0, None);
    			x.insert(tx1.hash(), tx1);
    			x.insert(tx2.hash(), tx2);
    			x
    		};
    		// Insert both transactions
    		let order1 = transaction_order(&tx1, U256::zero());
    		set.insert(tx1.sender(), tx1.nonce(), order1.clone());
    		let order2 = transaction_order(&tx2, U256::zero());
    		set.insert(tx2.sender(), tx2.nonce(), order2.clone());
    		assert_eq!(set.by_priority.len(), 2);
    		assert_eq!(set.by_address.len(), 2);

    		// when
    		set.enforce_limit(&mut by_hash, &mut local);

    		// then
    		assert_eq!(by_hash.len(), 1);
    		assert_eq!(set.by_priority.len(), 1);
    		assert_eq!(set.by_address.len(), 1);
    		assert_eq!(set.by_priority.iter().next().unwrap().clone(), order1);
    		set.clear();
    		assert_eq!(set.by_priority.len(), 0);
    		assert_eq!(set.by_address.len(), 0);
    	}

    	#[test]
    	fn should_replace_transaction_in_set() {
    		let mut set = TransactionSet {
    			by_priority: BTreeSet::new(),
    			by_address: Table::new(),
    			by_gas_price: Default::default(),
    			limit: 1,
    			gas_limit: !U256::zero(),
    		};
    		// Create two transactions with same nonce
    		// (same hash)
    		let (tx1, tx2) = new_tx_pair_default(0.into(), 0.into());
    		let tx1 = VerifiedTransaction::new(tx1, TransactionOrigin::External, 0, None);
    		let tx2 = VerifiedTransaction::new(tx2, TransactionOrigin::External, 0, None);
    		let by_hash = {
    			let mut x = HashMap::new();
    			let tx1 = VerifiedTransaction::new(tx1.transaction.clone(), TransactionOrigin::External, 0, None);
    			let tx2 = VerifiedTransaction::new(tx2.transaction.clone(), TransactionOrigin::External, 0, None);
    			x.insert(tx1.hash(), tx1);
    			x.insert(tx2.hash(), tx2);
    			x
    		};
    		// Insert both transactions
    		let order1 = transaction_order(&tx1, U256::zero());
    		set.insert(tx1.sender(), tx1.nonce(), order1.clone());
    		assert_eq!(set.by_priority.len(), 1);
    		assert_eq!(set.by_address.len(), 1);
    		assert_eq!(set.by_gas_price.len(), 1);
    		assert_eq!(*set.by_gas_price.iter().next().unwrap().0, 1.into());
    		assert_eq!(set.by_gas_price.iter().next().unwrap().1.len(), 1);
    		// Two different orders (imagine nonce changed in the meantime)
    		let order2 = transaction_order(&tx2, U256::one());
    		set.insert(tx2.sender(), tx2.nonce(), order2.clone());
    		assert_eq!(set.by_priority.len(), 1);
    		assert_eq!(set.by_address.len(), 1);
    		assert_eq!(set.by_gas_price.len(), 1);
    		assert_eq!(*set.by_gas_price.iter().next().unwrap().0, 1.into());
    		assert_eq!(set.by_gas_price.iter().next().unwrap().1.len(), 1);

    		// then
    		assert_eq!(by_hash.len(), 1);
    		assert_eq!(set.by_priority.len(), 1);
    		assert_eq!(set.by_address.len(), 1);
    		assert_eq!(set.by_gas_price.len(), 1);
    		assert_eq!(*set.by_gas_price.iter().next().unwrap().0, 1.into());
    		assert_eq!(set.by_gas_price.iter().next().unwrap().1.len(), 1);
    		assert_eq!(set.by_priority.iter().next().unwrap().clone(), order2);
    	}

    	#[test]
    	fn should_not_insert_same_transaction_twice_into_set() {
    		let mut set = TransactionSet {
    			by_priority: BTreeSet::new(),
    			by_address: Table::new(),
    			by_gas_price: Default::default(),
    			limit: 2,
    			gas_limit: !U256::zero(),
    		};
    		let tx = new_tx_default();
    		let tx1 = VerifiedTransaction::new(tx.clone(), TransactionOrigin::External, 0, None);
    		let order1 = TransactionOrder::for_transaction(&tx1, 0.into(), 1.into(), PrioritizationStrategy::GasPriceOnly);
    		assert!(set.insert(tx1.sender(), tx1.nonce(), order1).is_none());
    		let tx2 = VerifiedTransaction::new(tx, TransactionOrigin::External, 0, None);
    		let order2 = TransactionOrder::for_transaction(&tx2, 0.into(), 1.into(), PrioritizationStrategy::GasPriceOnly);
    		assert!(set.insert(tx2.sender(), tx2.nonce(), order2).is_some());
    	}

    	#[test]
    	fn should_give_correct_gas_price_entry_limit() {
    		let mut set = TransactionSet {
    			by_priority: BTreeSet::new(),
    			by_address: Table::new(),
    			by_gas_price: Default::default(),
    			limit: 1,
    			gas_limit: !U256::zero(),
    		};

    		assert_eq!(set.gas_price_entry_limit(), 0.into());
    		let tx = new_tx_default();
    		let tx1 = VerifiedTransaction::new(tx.clone(), TransactionOrigin::External, 0, None);
    		let order1 = TransactionOrder::for_transaction(&tx1, 0.into(), 1.into(), PrioritizationStrategy::GasPriceOnly);
    		assert!(set.insert(tx1.sender(), tx1.nonce(), order1.clone()).is_none());
    		assert_eq!(set.gas_price_entry_limit(), 2.into());
    	}

    	#[test]
    	fn should_handle_same_transaction_imported_twice_with_different_state_nonces() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx, tx2) = new_similar_tx_pair();
    		let prev_nonce = default_account_details().nonce - U256::one();

    		// First insert one transaction to future
    		let res = txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce));
    		assert_eq!(res.unwrap(), TransactionImportResult::Future);
    		assert_eq!(txq.status().future, 1);

    		// now import second transaction to current
    		let res = txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider());

    		// and then there should be only one transaction in current (the one with higher gas_price)
    		assert_eq!(res.unwrap(), TransactionImportResult::Current);
    		assert_eq!(txq.status().pending, 1);
    		assert_eq!(txq.status().future, 0);
    		assert_eq!(txq.current.by_priority.len(), 1);
    		assert_eq!(txq.current.by_address.len(), 1);
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx2);
    	}

    	#[test]
    	fn should_move_all_transactions_from_future() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx, tx2) = new_tx_pair_default(1.into(), 1.into());
    		let prev_nonce = default_account_details().nonce - U256::one();

    		// First insert one transaction to future
    		let res = txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce));
    		assert_eq!(res.unwrap(), TransactionImportResult::Future);
    		assert_eq!(txq.status().future, 1);

    		// now import second transaction to current
    		let res = txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider());

    		// then
    		assert_eq!(res.unwrap(), TransactionImportResult::Current);
    		assert_eq!(txq.status().pending, 2);
    		assert_eq!(txq.status().future, 0);
    		assert_eq!(txq.current.by_priority.len(), 2);
    		assert_eq!(txq.current.by_address.len(), 2);
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx);
    		assert_eq!(top[1], tx2);
    	}

    	#[test]
    	fn should_import_tx() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();

    		// when
    		let res = txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider());

    		// then
    		assert_eq!(res.unwrap(), TransactionImportResult::Current);
    		let stats = txq.status();
    		assert_eq!(stats.pending, 1);
    	}

    	#[test]
    	fn should_order_by_gas() {
    		// given
    		let mut txq = TransactionQueue::new(PrioritizationStrategy::GasAndGasPrice);
    		let tx1 = new_tx_with_gas(50000.into(), 40.into());
    		let tx2 = new_tx_with_gas(40000.into(), 30.into());
    		let tx3 = new_tx_with_gas(30000.into(), 10.into());
    		let tx4 = new_tx_with_gas(50000.into(), 20.into());
    		txq.set_minimal_gas_price(15.into());

    		// when
    		let res1 = txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider());
    		let res2 = txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider());
    		let res3 = txq.add(tx3, TransactionOrigin::External, 0, None, &default_tx_provider());
    		let res4 = txq.add(tx4, TransactionOrigin::External, 0, None, &default_tx_provider());

    		// then
    		assert_eq!(res1.unwrap(), TransactionImportResult::Current);
    		assert_eq!(res2.unwrap(), TransactionImportResult::Current);
    		assert_eq!(unwrap_tx_err(res3), TransactionError::InsufficientGasPrice {
    			minimal: U256::from(15),
    			got: U256::from(10),
    		});
    		assert_eq!(res4.unwrap(), TransactionImportResult::Current);
    		let stats = txq.status();
    		assert_eq!(stats.pending, 3);
    		assert_eq!(txq.top_transactions()[0].gas, 40000.into());
    		assert_eq!(txq.top_transactions()[1].gas, 50000.into());
    		assert_eq!(txq.top_transactions()[2].gas, 50000.into());
    		assert_eq!(txq.top_transactions()[1].gas_price, 40.into());
    		assert_eq!(txq.top_transactions()[2].gas_price, 20.into());
    	}

    	#[test]
    	fn should_order_by_gas_factor() {
    		// given
    		let mut txq = TransactionQueue::new(PrioritizationStrategy::GasFactorAndGasPrice);

    		let tx1 = new_tx_with_gas(150_000.into(), 40.into());
    		let tx2 = new_tx_with_gas(40_000.into(), 16.into());
    		let tx3 = new_tx_with_gas(30_000.into(), 15.into());
    		let tx4 = new_tx_with_gas(150_000.into(), 62.into());
    		txq.set_minimal_gas_price(15.into());

    		// when
    		let res1 = txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider());
    		let res2 = txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider());
    		let res3 = txq.add(tx3, TransactionOrigin::External, 0, None, &default_tx_provider());
    		let res4 = txq.add(tx4, TransactionOrigin::External, 0, None, &default_tx_provider());

    		// then
    		assert_eq!(res1.unwrap(), TransactionImportResult::Current);
    		assert_eq!(res2.unwrap(), TransactionImportResult::Current);
    		assert_eq!(res3.unwrap(), TransactionImportResult::Current);
    		assert_eq!(res4.unwrap(), TransactionImportResult::Current);
    		let stats = txq.status();
    		assert_eq!(stats.pending, 4);
    		assert_eq!(txq.top_transactions()[0].gas, 30_000.into());
    		assert_eq!(txq.top_transactions()[1].gas, 150_000.into());
    		assert_eq!(txq.top_transactions()[2].gas, 40_000.into());
    		assert_eq!(txq.top_transactions()[3].gas, 150_000.into());
    		assert_eq!(txq.top_transactions()[0].gas_price, 15.into());
    		assert_eq!(txq.top_transactions()[1].gas_price, 62.into());
    		assert_eq!(txq.top_transactions()[2].gas_price, 16.into());
    		assert_eq!(txq.top_transactions()[3].gas_price, 40.into());
    	}

    	#[test]
    	fn gas_limit_should_never_overflow() {
    		// given
    		let mut txq = TransactionQueue::default();
    		txq.set_gas_limit(U256::zero());
    		assert_eq!(txq.gas_limit, U256::zero());

    		// when
    		txq.set_gas_limit(!U256::zero());

    		// then
    		assert_eq!(txq.gas_limit, !U256::zero());
    	}

    	#[test]
    	fn should_not_import_transaction_above_gas_limit() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();
    		let gas = tx.gas;
    		let limit = gas / U256::from(2);
    		txq.set_gas_limit(limit);

    		// when
    		let res = txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider());

    		// then
    		assert_eq!(unwrap_tx_err(res), TransactionError::GasLimitExceeded {
    			limit: U256::from(55_000), // Should be 110% of set_gas_limit
    			got: gas,
    		});
    		let stats = txq.status();
    		assert_eq!(stats.pending, 0);
    		assert_eq!(stats.future, 0);
    	}

    	#[test]
    	fn should_drop_transactions_from_senders_without_balance() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();
    		let account = AccountDetails {
    			nonce: default_account_details().nonce,
    			balance: U256::one()
    		};

    		// when
    		let res = txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider().with_account(account));

    		// then
    		assert_eq!(unwrap_tx_err(res), TransactionError::InsufficientBalance {
    			balance: U256::from(1),
    			cost: U256::from(100_100),
    		});
    		let stats = txq.status();
    		assert_eq!(stats.pending, 0);
    		assert_eq!(stats.future, 0);
    	}

    	#[test]
    	fn should_not_import_transaction_below_min_gas_price_threshold_if_external() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();
    		txq.set_minimal_gas_price(tx.gas_price + U256::one());

    		// when
    		let res = txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider());

    		// then
    		assert_eq!(unwrap_tx_err(res), TransactionError::InsufficientGasPrice {
    			minimal: U256::from(2),
    			got: U256::from(1),
    		});
    		let stats = txq.status();
    		assert_eq!(stats.pending, 0);
    		assert_eq!(stats.future, 0);
    	}

    	#[test]
    	fn should_import_transaction_below_min_gas_price_threshold_if_local() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();
    		txq.set_minimal_gas_price(tx.gas_price + U256::one());

    		// when
    		let res = txq.add(tx, TransactionOrigin::Local, 0, None, &default_tx_provider());

    		// then
    		assert_eq!(res.unwrap(), TransactionImportResult::Current);
    		let stats = txq.status();
    		assert_eq!(stats.pending, 1);
    		assert_eq!(stats.future, 0);
    	}

    	#[test]
    	fn should_import_txs_from_same_sender() {
    		// given
    		let mut txq = TransactionQueue::default();

    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());

    		// when
    		txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx);
    		assert_eq!(top[1], tx2);
    		assert_eq!(top.len(), 2);
    	}

    	#[test]
    	fn should_prioritize_local_transactions_within_same_nonce_height() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();
    		// the second one has same nonce but higher `gas_price`
    		let (_, tx2) = new_similar_tx_pair();

    		// when
    		// first insert the one with higher gas price
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		// then the one with lower gas price, but local
    		txq.add(tx.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx); // local should be first
    		assert_eq!(top[1], tx2);
    		assert_eq!(top.len(), 2);
    	}

    	#[test]
    	fn when_importing_local_should_mark_others_from_the_same_sender_as_local() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		// the second one has same nonce but higher `gas_price`
    		let (_, tx0) = new_similar_tx_pair();

    		txq.add(tx0.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		// the one with higher gas price is first
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx0);
    		assert_eq!(top[1], tx1);

    		// when
    		// insert second as local
    		txq.add(tx2.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();

    		// then
    		// the order should be updated
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx1);
    		assert_eq!(top[1], tx2);
    		assert_eq!(top[2], tx0);
    	}

    	#[test]
    	fn should_prioritize_reimported_transactions_within_same_nonce_height() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();
    		// the second one has same nonce but higher `gas_price`
    		let (_, tx2) = new_similar_tx_pair();

    		// when
    		// first insert local one with higher gas price
    		txq.add(tx2.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		// then the one with lower gas price, but from retracted block
    		txq.add(tx.clone(), TransactionOrigin::RetractedBlock, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx); // retracted should be first
    		assert_eq!(top[1], tx2);
    		assert_eq!(top.len(), 2);
    	}

    	#[test]
    	fn should_not_prioritize_local_transactions_with_different_nonce_height() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());

    		// when
    		txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx);
    		assert_eq!(top[1], tx2);
    		assert_eq!(top.len(), 2);
    	}

    	#[test]
    	fn should_penalize_transactions_from_sender_in_future() {
    		// given
    		let prev_nonce = default_account_details().nonce - U256::one();
    		let mut txq = TransactionQueue::default();
    		// txa, txb - slightly bigger gas price to have consistent ordering
    		let (txa, txb) = new_tx_pair_default(1.into(), 0.into());
    		let (tx1, tx2) = new_tx_pair_with_gas_price_increment(3.into());

    		// insert everything
    		txq.add(txa.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce)).unwrap();
    		txq.add(txb.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce)).unwrap();
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce)).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce)).unwrap();

    		assert_eq!(txq.status().future, 4);

    		// when
    		txq.penalize(&tx1.hash());

    		// then
    		let top: Vec<_> = txq.future_transactions().into_iter().map(|tx| tx.transaction).collect();
    		assert_eq!(top[0], txa);
    		assert_eq!(top[1], txb);
    		assert_eq!(top[2], tx1);
    		assert_eq!(top[3], tx2);
    		assert_eq!(top.len(), 4);
    	}

    	#[test]
    	fn should_not_penalize_local_transactions() {
    		// given
    		let mut txq = TransactionQueue::default();
    		// txa, txb - slightly bigger gas price to have consistent ordering
    		let (txa, txb) = new_tx_pair_default(1.into(), 0.into());
    		let (tx1, tx2) = new_tx_pair_with_gas_price_increment(3.into());

    		// insert everything
    		txq.add(txa.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		txq.add(txb.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx1.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();

    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx1);
    		assert_eq!(top[1], txa);
    		assert_eq!(top[2], tx2);
    		assert_eq!(top[3], txb);
    		assert_eq!(top.len(), 4);

    		// when
    		txq.penalize(&tx1.hash());

    		// then (order is the same)
    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx1);
    		assert_eq!(top[1], txa);
    		assert_eq!(top[2], tx2);
    		assert_eq!(top[3], txb);
    		assert_eq!(top.len(), 4);
    	}

    	#[test]
    	fn should_penalize_transactions_from_sender() {
    		// given
    		let mut txq = TransactionQueue::default();
    		// txa, txb - slightly bigger gas price to have consistent ordering
    		let (txa, txb) = new_tx_pair_default(1.into(), 0.into());
    		let (tx1, tx2) = new_tx_pair_with_gas_price_increment(3.into());

    		// insert everything
    		txq.add(txa.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(txb.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		let top = txq.top_transactions();
    		assert_eq!(top[0], tx1);
    		assert_eq!(top[1], txa);
    		assert_eq!(top[2], tx2);
    		assert_eq!(top[3], txb);
    		assert_eq!(top.len(), 4);

    		// when
    		txq.penalize(&tx1.hash());

    		// then
    		let top = txq.top_transactions();
    		assert_eq!(top[0], txa);
    		assert_eq!(top[1], txb);
    		assert_eq!(top[2], tx1);
    		assert_eq!(top[3], tx2);
    		assert_eq!(top.len(), 4);
    	}

    	#[test]
    	fn should_return_pending_hashes() {
    		// given
    		let mut txq = TransactionQueue::default();

    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());

    		// when
    		txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let top = txq.pending_hashes();
    		assert_eq!(top[0], tx.hash());
    		assert_eq!(top[1], tx2.hash());
    		assert_eq!(top.len(), 2);
    	}

    	#[test]
    	fn should_put_transaction_to_futures_if_gap_detected() {
    		// given
    		let mut txq = TransactionQueue::default();

    		let (tx, tx2) = new_tx_pair_default(2.into(), 0.into());

    		// when
    		let res1 = txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		let res2 = txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		assert_eq!(res1, TransactionImportResult::Current);
    		assert_eq!(res2, TransactionImportResult::Future);
    		let stats = txq.status();
    		assert_eq!(stats.pending, 1);
    		assert_eq!(stats.future, 1);
    		let top = txq.top_transactions();
    		assert_eq!(top.len(), 1);
    		assert_eq!(top[0], tx);
    	}

    	#[test]
    	fn should_handle_min_block() {
    		// given
    		let mut txq = TransactionQueue::default();

    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());

    		// when
    		let res1 = txq.add(tx.clone(), TransactionOrigin::External, 0, Some(1), &default_tx_provider()).unwrap();
    		let res2 = txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		assert_eq!(res1, TransactionImportResult::Current);
    		assert_eq!(res2, TransactionImportResult::Current);
    		let top = txq.top_transactions_at(0);
    		assert_eq!(top.len(), 0);
    		let top = txq.top_transactions_at(1);
    		assert_eq!(top.len(), 2);
    	}

    	#[test]
    	fn should_correctly_update_futures_when_removing() {
    		// given
    		let prev_nonce = default_account_details().nonce - U256::one();
    		let next2_nonce = default_nonce() + U256::from(3);

    		let mut txq = TransactionQueue::default();

    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());
    		txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce)).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce)).unwrap();
    		assert_eq!(txq.status().future, 2);

    		// when
    		txq.cull(tx.sender(), next2_nonce);
    		// should remove both transactions since they are not valid

    		// then
    		assert_eq!(txq.status().pending, 0);
    		assert_eq!(txq.status().future, 0);
    	}

    	#[test]
    	fn should_move_transactions_if_gap_filled() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let kp = Random.generate().unwrap();
    		let secret = kp.secret();
    		let tx = new_unsigned_tx(123.into(), default_gas_val(), 1.into()).sign(secret, None).into();
    		let tx1 = new_unsigned_tx(124.into(), default_gas_val(), 1.into()).sign(secret, None).into();
    		let tx2 = new_unsigned_tx(125.into(), default_gas_val(), 1.into()).sign(secret, None).into();

    		txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().pending, 1);
    		txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().future, 1);

    		// when
    		txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let stats = txq.status();
    		assert_eq!(stats.pending, 3);
    		assert_eq!(stats.future, 0);
    		assert_eq!(txq.future.by_priority.len(), 0);
    		assert_eq!(txq.future.by_address.len(), 0);
    		assert_eq!(txq.future.by_gas_price.len(), 0);
    	}

    	#[test]
    	fn should_remove_transaction() {
    		// given
    		let mut txq2 = TransactionQueue::default();
    		let (tx, tx2) = new_tx_pair_default(3.into(), 0.into());
    		txq2.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq2.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq2.status().pending, 1);
    		assert_eq!(txq2.status().future, 1);

    		// when
    		txq2.cull(tx.sender(), tx.nonce + U256::one());
    		txq2.cull(tx2.sender(), tx2.nonce + U256::one());

    		// then
    		let stats = txq2.status();
    		assert_eq!(stats.pending, 0);
    		assert_eq!(stats.future, 0);
    	}

    	#[test]
    	fn should_move_transactions_to_future_if_gap_introduced() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let tx3 = new_tx_default();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().future, 1);
    		txq.add(tx3.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().pending, 3);

    		// when
    		txq.remove_invalid(&tx.hash(), &|_| default_nonce());

    		// then
    		let stats = txq.status();
    		assert_eq!(stats.future, 1);
    		assert_eq!(stats.pending, 1);
    	}

    	#[test]
    	fn should_clear_queue() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());

    		// add
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		let stats = txq.status();
    		assert_eq!(stats.pending, 2);

    		// when
    		txq.clear();

    		// then
    		let stats = txq.status();
    		assert_eq!(stats.pending, 0);
    	}

    	#[test]
    	fn should_drop_old_transactions_when_hitting_the_limit() {
    		// given
    		let mut txq = TransactionQueue::with_limits(PrioritizationStrategy::GasPriceOnly, 1, !U256::zero(), !U256::zero());
    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let sender = tx.sender();
    		let nonce = tx.nonce;
    		txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().pending, 1);

    		// when
    		let res = txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider());

    		// then
    		let t = txq.top_transactions();
    		assert_eq!(unwrap_tx_err(res), TransactionError::InsufficientGasPrice { minimal: 2.into(), got: 1.into() });
    		assert_eq!(txq.status().pending, 1);
    		assert_eq!(t.len(), 1);
    		assert_eq!(t[0], tx);
    		assert_eq!(txq.last_nonce(&sender), Some(nonce));
    	}

    	#[test]
    	fn should_limit_future_transactions() {
    		let mut txq = TransactionQueue::with_limits(PrioritizationStrategy::GasPriceOnly, 1, !U256::zero(), !U256::zero());
    		txq.current.set_limit(10);
    		let (tx1, tx2) = new_tx_pair_default(4.into(), 1.into());
    		let (tx3, tx4) = new_tx_pair_default(4.into(), 2.into());
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx3.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().pending, 2);

    		// when
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().future, 1);
    		txq.add(tx4.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		assert_eq!(txq.status().future, 1);
    	}

    	#[test]
    	fn should_limit_by_gas() {
    		let mut txq = TransactionQueue::with_limits(PrioritizationStrategy::GasPriceOnly, 100, default_gas_val() * U256::from(2), !U256::zero());
    		let (tx1, tx2) = new_tx_pair_default(U256::from(1), U256::from(1));
    		let (tx3, tx4) = new_tx_pair_default(U256::from(1), U256::from(2));
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx3.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		// limited by gas
    		txq.add(tx4.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap_err();
    		assert_eq!(txq.status().pending, 2);
    	}

    	#[test]
    	fn should_keep_own_transactions_above_gas_limit() {
    		let mut txq = TransactionQueue::with_limits(PrioritizationStrategy::GasPriceOnly, 100, default_gas_val() * U256::from(2), !U256::zero());
    		let (tx1, tx2) = new_tx_pair_default(U256::from(1), U256::from(1));
    		let (tx3, tx4) = new_tx_pair_default(U256::from(1), U256::from(2));
    		let (tx5, _) = new_tx_pair_default(U256::from(1), U256::from(2));
    		txq.add(tx1.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		// Not accepted because of limit
    		txq.add(tx5.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap_err();
    		txq.add(tx3.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx4.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().pending, 4);
    	}

    	#[test]
    	fn should_drop_transactions_with_old_nonces() {
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();
    		let last_nonce = tx.nonce + U256::one();

    		// when
    		let res = txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(last_nonce));

    		// then
    		assert_eq!(unwrap_tx_err(res), TransactionError::Old);
    		let stats = txq.status();
    		assert_eq!(stats.pending, 0);
    		assert_eq!(stats.future, 0);
    	}

    	#[test]
    	fn should_not_insert_same_transaction_twice() {
    		// given
    		let nonce = default_account_details().nonce + U256::one();
    		let mut txq = TransactionQueue::default();
    		let (_tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().future, 1);
    		assert_eq!(txq.status().pending, 0);

    		// when
    		let res = txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(nonce));

    		// then
    		assert_eq!(unwrap_tx_err(res), TransactionError::AlreadyImported);
    		let stats = txq.status();
    		assert_eq!(stats.future, 1);
    		assert_eq!(stats.pending, 0);
    	}

    	#[test]
    	fn should_accept_same_transaction_twice_if_removed() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().pending, 2);

    		// when
    		txq.remove_invalid(&tx1.hash(), &|_| default_nonce());
    		assert_eq!(txq.status().pending, 0);
    		assert_eq!(txq.status().future, 1);
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let stats = txq.status();
    		assert_eq!(stats.future, 0);
    		assert_eq!(stats.pending, 2);
    	}

    	#[test]
    	fn should_not_move_to_future_if_state_nonce_is_higher() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let tx3 = new_tx_default();
    		txq.add(tx2.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().future, 1);
    		txq.add(tx3.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx.clone(), TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().pending, 3);

    		// when
    		txq.cull(tx.sender(), default_nonce() + U256::one());

    		// then
    		let stats = txq.status();
    		assert_eq!(stats.future, 0);
    		assert_eq!(stats.pending, 2);
    	}

    	#[test]
    	fn should_replace_same_transaction_when_has_higher_fee() {
    		init_log();
    		// given
    		let mut txq = TransactionQueue::default();
    		let keypair = Random.generate().unwrap();
    		let tx = new_unsigned_tx(123.into(), default_gas_val(), 1.into()).sign(keypair.secret(), None);
    		let tx2 = {
    			let mut tx2 = (**tx).clone();
    			tx2.gas_price = U256::from(200);
    			tx2.sign(keypair.secret(), None)
    		};

    		// when
    		txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let stats = txq.status();
    		assert_eq!(stats.pending, 1);
    		assert_eq!(stats.future, 0);
    		assert_eq!(txq.top_transactions()[0].gas_price, U256::from(200));
    	}

    	#[test]
    	fn should_replace_same_transaction_when_importing_to_futures() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let keypair = Random.generate().unwrap();
    		let tx0 = new_unsigned_tx(123.into(), default_gas_val(), 1.into()).sign(keypair.secret(), None);
    		let tx1 = {
    			let mut tx1 = (**tx0).clone();
    			tx1.nonce = U256::from(124);
    			tx1.sign(keypair.secret(), None)
    		};
    		let tx2 = {
    			let mut tx2 = (**tx1).clone();
    			tx2.gas_price = U256::from(200);
    			tx2.sign(keypair.secret(), None)
    		};

    		// when
    		txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.status().future, 1);
    		txq.add(tx0, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();

    		// then
    		let stats = txq.status();
    		assert_eq!(stats.future, 0);
    		assert_eq!(stats.pending, 2);
    		assert_eq!(txq.top_transactions()[1].gas_price, U256::from(200));
    	}

    	#[test]
    	fn should_recalculate_height_when_removing_from_future() {
    		// given
    		let previous_nonce = default_account_details().nonce - U256::one();
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(previous_nonce)).unwrap();
    		txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(previous_nonce)).unwrap();
    		assert_eq!(txq.status().future, 2);

    		// when
    		txq.remove_invalid(&tx1.hash(), &|_| default_nonce() + 1.into());

    		// then
    		let stats = txq.status();
    		assert_eq!(stats.future, 0);
    		assert_eq!(stats.pending, 1);
    	}

    	#[test]
    	fn should_return_none_when_transaction_from_given_address_does_not_exist() {
    		// given
    		let txq = TransactionQueue::default();

    		// then
    		assert_eq!(txq.last_nonce(&Address::default()), None);
    	}

    	#[test]
    	fn should_return_correct_nonce_when_transactions_from_given_address_exist() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let tx = new_tx_default();
    		let from = tx.sender();
    		let nonce = tx.nonce;

    		// when
    		txq.add(tx, TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(nonce)).unwrap();

    		// then
    		assert_eq!(txq.last_nonce(&from), Some(nonce));
    	}

    	#[test]
    	fn should_remove_old_transaction_even_if_newer_transaction_was_not_known() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let (nonce1, nonce2) = (tx1.nonce, tx2.nonce);

    		// Insert first transaction
    		txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(nonce1)).unwrap();

    		// when
    		txq.cull(tx2.sender(), nonce2 + U256::one());

    		// then
    		assert!(txq.top_transactions().is_empty());
    	}

    	#[test]
    	fn should_return_valid_last_nonce_after_cull() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(4.into(), 0.into());
    		let sender = tx1.sender();
    		let (nonce1, nonce2) = (tx1.nonce, tx2.nonce);

    		// when
    		// Insert first transaction
    		assert_eq!(txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(nonce1)).unwrap(), TransactionImportResult::Current);
    		// Second should go to future
    		assert_eq!(txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(nonce1)).unwrap(), TransactionImportResult::Future);
    		// Now block is imported
    		txq.cull(sender, nonce2 - U256::from(1));
    		// tx2 should be not be promoted to current
    		assert_eq!(txq.status().pending, 0);
    		assert_eq!(txq.status().future, 1);

    		// then
    		assert_eq!(txq.last_nonce(&sender), None);
    	}

    	#[test]
    	fn should_return_true_if_there_is_local_transaction_pending() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		assert_eq!(txq.has_local_pending_transactions(), false);

    		// when
    		assert_eq!(txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap(), TransactionImportResult::Current);
    		assert_eq!(txq.has_local_pending_transactions(), false);
    		assert_eq!(txq.add(tx2, TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap(), TransactionImportResult::Current);

    		// then
    		assert_eq!(txq.has_local_pending_transactions(), true);
    	}

    	#[test]
    	fn should_keep_right_order_in_future() {
    		// given
    		let mut txq = TransactionQueue::with_limits(PrioritizationStrategy::GasPriceOnly, 1, !U256::zero(), !U256::zero());
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let prev_nonce = default_account_details().nonce - U256::one();

    		// when
    		assert_eq!(txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce)).unwrap(), TransactionImportResult::Future);
    		assert_eq!(txq.add(tx1.clone(), TransactionOrigin::External, 0, None, &default_tx_provider().with_account_nonce(prev_nonce)).unwrap(), TransactionImportResult::Future);

    		// then
    		assert_eq!(txq.future.by_priority.len(), 1);
    		assert_eq!(txq.future.by_priority.iter().next().unwrap().hash, tx1.hash());
    	}

    	#[test]
    	fn should_return_correct_last_nonce() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2, tx2_2, tx3) = {
    			let keypair = Random.generate().unwrap();
    			let secret = &keypair.secret();
    			let nonce = 123.into();
    			let gas = default_gas_val();
    			let tx = new_unsigned_tx(nonce, gas, 1.into());
    			let tx2 = new_unsigned_tx(nonce + 1.into(), gas, 1.into());
    			let tx2_2 = new_unsigned_tx(nonce + 1.into(), gas, 5.into());
    			let tx3 = new_unsigned_tx(nonce + 2.into(), gas, 1.into());

    			(tx.sign(secret, None), tx2.sign(secret, None), tx2_2.sign(secret, None), tx3.sign(secret, None))
    		};
    		let sender = tx1.sender();
    		txq.add(tx1, TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2, TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx3, TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.future.by_priority.len(), 0);
    		assert_eq!(txq.current.by_priority.len(), 3);

    		// when
    		let res = txq.add(tx2_2, TransactionOrigin::Local, 0, None, &default_tx_provider());

    		// then
    		assert_eq!(txq.last_nonce(&sender).unwrap(), 125.into());
    		assert_eq!(res.unwrap(), TransactionImportResult::Current);
    		assert_eq!(txq.current.by_priority.len(), 3);
    	}

    	#[test]
    	fn should_reject_transactions_below_base_gas() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let high_gas = 100_001.into();

    		// when
    		let res1 = txq.add(tx1, TransactionOrigin::Local, 0, None, &default_tx_provider());
    		let res2 = txq.add(tx2, TransactionOrigin::Local, 0, None, &default_tx_provider().with_tx_gas_required(high_gas));

    		// then
    		assert_eq!(res1.unwrap(), TransactionImportResult::Current);
    		assert_eq!(unwrap_tx_err(res2), TransactionError::InsufficientGas {
    			minimal: 100_001.into(),
    			got: 100_000.into(),
    		});

    	}

    	#[test]
    	fn should_clear_all_old_transactions() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let (tx3, tx4) = new_tx_pair_default(1.into(), 0.into());
    		let next_nonce = |_: &Address|
    			AccountDetails { nonce: default_nonce() + U256::one(), balance: !U256::zero() };

    		// Insert all transactions
    		txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx3, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx4, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.top_transactions().len(), 4);

    		// when
    		txq.remove_old(&next_nonce, 0);

    		// then
    		assert_eq!(txq.top_transactions().len(), 2);
    	}

    	#[test]
    	fn should_remove_out_of_date_transactions_occupying_queue() {
    		// given
    		let mut txq = TransactionQueue::default();
    		let (tx1, tx2) = new_tx_pair_default(1.into(), 0.into());
    		let (tx3, tx4) = new_tx_pair_default(2.into(), 0.into());

    		// Insert all transactions
    		txq.add(tx1.clone(), TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();
    		txq.add(tx2, TransactionOrigin::External, 5, None, &default_tx_provider()).unwrap();
    		txq.add(tx3.clone(), TransactionOrigin::External, 10, None, &default_tx_provider()).unwrap();
    		txq.add(tx4, TransactionOrigin::External, 0, None, &default_tx_provider()).unwrap();
    		assert_eq!(txq.top_transactions().len(), 3);
    		assert_eq!(txq.future_transactions().len(), 1);

    		// when
    		txq.remove_old(&default_account_details_for_addr, 9 + super::DEFAULT_QUEUING_PERIOD);

    		// then
    		assert_eq!(txq.top_transactions().len(), 2);
    		assert_eq!(txq.future_transactions().len(), 0);
    		assert_eq!(txq.top_transactions(), vec![tx1, tx3]);
    	}

    	#[test]
    	fn should_accept_local_service_transaction() {
    		// given
    		let tx = new_tx(123.into(), 0.into());
    		let mut txq = TransactionQueue::default();
    		txq.set_minimal_gas_price(100.into());

    		// when
    		txq.add(tx, TransactionOrigin::Local, 0, None, &default_tx_provider()).unwrap();

    		// then
    		assert_eq!(txq.top_transactions().len(), 1);
    	}

    	#[test]
    	fn should_not_accept_external_service_transaction_if_sender_not_certified() {
    		// given
    		let tx1 = new_tx(123.into(), 0.into());
    		let tx2 = new_tx(456.into(), 0.into());
    		let mut txq = TransactionQueue::default();
    		txq.set_minimal_gas_price(100.into());

    		// when
    		assert_eq!(unwrap_tx_err(txq.add(tx1, TransactionOrigin::External, 0, None, &default_tx_provider())),
    			TransactionError::InsufficientGasPrice {
    				minimal: 100.into(),
    				got: 0.into(),
    			});
    		assert_eq!(unwrap_tx_err(txq.add(tx2, TransactionOrigin::RetractedBlock, 0, None, &default_tx_provider())),
    			TransactionError::InsufficientGasPrice {
    				minimal: 100.into(),
    				got: 0.into(),
    			});

    		// then
    		assert_eq!(txq.top_transactions().len(), 0);
    	}

    	#[test]
    	fn should_not_accept_external_service_transaction_if_contract_returns_error() {
    		// given
    		let tx = new_tx(123.into(), 0.into());
    		let mut txq = TransactionQueue::default();
    		txq.set_minimal_gas_price(100.into());

    		// when
    		let details_provider = default_tx_provider().service_transaction_checker_returns_error("Contract error");
    		assert_eq!(unwrap_tx_err(txq.add(tx, TransactionOrigin::External, 0, None, &details_provider)),
    			TransactionError::InsufficientGasPrice {
    				minimal: 100.into(),
    				got: 0.into(),
    			});

    		// then
    		assert_eq!(txq.top_transactions().len(), 0);
    	}

    	#[test]
    	fn should_accept_external_service_transaction_if_sender_is_certified() {
    		// given
    		let tx = new_tx(123.into(), 0.into());
    		let mut txq = TransactionQueue::default();
    		txq.set_minimal_gas_price(100.into());

    		// when
    		let details_provider = default_tx_provider().service_transaction_checker_accepts(true);
    		txq.add(tx, TransactionOrigin::External, 0, None, &details_provider).unwrap();

    		// then
    		assert_eq!(txq.top_transactions().len(), 1);
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)