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

use std::time::{Instant, Duration};

use util::*;
use util::using_queue::{UsingQueue, GetAction};
use account_provider::{AccountProvider, SignError as AccountError};
use state::{State, CleanupMode};
use client::{MiningBlockChainClient, Executive, Executed, EnvInfo, TransactOptions, BlockId, CallAnalytics, TransactionId};
use client::TransactionImportResult;
use executive::contract_address;
use block::{ClosedBlock, IsBlock, Block};
use error::*;
use transaction::{Action, UnverifiedTransaction, PendingTransaction, SignedTransaction};
use receipt::{Receipt, RichReceipt};
use spec::Spec;
use engines::{Engine, Seal};
use miner::{MinerService, MinerStatus, TransactionQueue, TransactionQueueDetailsProvider, PrioritizationStrategy,
	AccountDetails, TransactionOrigin};
use miner::banning_queue::{BanningTransactionQueue, Threshold};
use miner::work_notify::{WorkPoster, NotifyWork};
use miner::price_info::PriceInfo;
use miner::local_transactions::{Status as LocalTransactionStatus};
use miner::service_transaction_checker::ServiceTransactionChecker;
use header::BlockNumber;

/// Different possible definitions for pending transaction set.
#[derive(Debug, PartialEq)]
pub enum PendingSet {
	/// Always just the transactions in the queue. These have had only cheap checks.
	AlwaysQueue,
	/// Always just the transactions in the sealing block. These have had full checks but
	/// may be empty if the node is not actively mining or has force_sealing enabled.
	AlwaysSealing,
	/// Try the sealing block, but if it is not currently sealing, fallback to the queue.
	SealingOrElseQueue,
}

/// Type of the gas limit to apply to the transaction queue.
#[derive(Debug, PartialEq)]
pub enum GasLimit {
	/// Depends on the block gas limit and is updated with every block.
	Auto,
	/// No limit.
	None,
	/// Set to a fixed gas value.
	Fixed(U256),
}

/// Transaction queue banning settings.
#[derive(Debug, PartialEq, Clone)]
pub enum Banning {
	/// Banning in transaction queue is disabled
	Disabled,
	/// Banning in transaction queue is enabled
	Enabled {
		/// Upper limit of transaction processing time before banning.
		offend_threshold: Duration,
		/// Number of similar offending transactions before banning.
		min_offends: u16,
		/// Number of seconds the offender is banned for.
		ban_duration: Duration,
	},
}

/// Configures the behaviour of the miner.
#[derive(Debug, PartialEq)]
pub struct MinerOptions {
	/// URLs to notify when there is new work.
	pub new_work_notify: Vec<String>,
	/// Force the miner to reseal, even when nobody has asked for work.
	pub force_sealing: bool,
	/// Reseal on receipt of new external transactions.
	pub reseal_on_external_tx: bool,
	/// Reseal on receipt of new local transactions.
	pub reseal_on_own_tx: bool,
	/// Minimum period between transaction-inspired reseals.
	pub reseal_min_period: Duration,
	/// Maximum amount of gas to bother considering for block insertion.
	pub tx_gas_limit: U256,
	/// Maximum size of the transaction queue.
	pub tx_queue_size: usize,
	/// Strategy to use for prioritizing transactions in the queue.
	pub tx_queue_strategy: PrioritizationStrategy,
	/// Whether we should fallback to providing all the queue's transactions or just pending.
	pub pending_set: PendingSet,
	/// How many historical work packages can we store before running out?
	pub work_queue_size: usize,
	/// Can we submit two different solutions for the same block and expect both to result in an import?
	pub enable_resubmission: bool,
	/// Global gas limit for all transaction in the queue except for local and retracted.
	pub tx_queue_gas_limit: GasLimit,
	/// Banning settings.
	pub tx_queue_banning: Banning,
	/// Do we refuse to accept service transactions even if sender is certified.
	pub refuse_service_transactions: bool,
}

impl Default for MinerOptions {
	fn default() -> Self {
		MinerOptions {
			new_work_notify: vec![],
			force_sealing: false,
			reseal_on_external_tx: false,
			reseal_on_own_tx: true,
			tx_gas_limit: !U256::zero(),
			tx_queue_size: 1024,
			tx_queue_gas_limit: GasLimit::Auto,
			tx_queue_strategy: PrioritizationStrategy::GasPriceOnly,
			pending_set: PendingSet::AlwaysQueue,
			reseal_min_period: Duration::from_secs(2),
			work_queue_size: 20,
			enable_resubmission: true,
			tx_queue_banning: Banning::Disabled,
			refuse_service_transactions: false,
		}
	}
}

/// Options for the dynamic gas price recalibrator.
#[derive(Debug, PartialEq)]
pub struct GasPriceCalibratorOptions {
	/// Base transaction price to match against.
	pub usd_per_tx: f32,
	/// How frequently we should recalibrate.
	pub recalibration_period: Duration,
}

/// The gas price validator variant for a `GasPricer`.
#[derive(Debug, PartialEq)]
pub struct GasPriceCalibrator {
	options: GasPriceCalibratorOptions,
	next_calibration: Instant,
}

impl GasPriceCalibrator {
	fn recalibrate<F: Fn(U256) + Sync + Send + 'static>(&mut self, set_price: F) {
		trace!(target: "miner", "Recalibrating {:?} versus {:?}", Instant::now(), self.next_calibration);
		if Instant::now() >= self.next_calibration {
			let usd_per_tx = self.options.usd_per_tx;
			trace!(target: "miner", "Getting price info");

			PriceInfo::get(move |price: PriceInfo| {
				trace!(target: "miner", "Price info arrived: {:?}", price);
				let usd_per_eth = price.ethusd;
				let wei_per_usd: f32 = 1.0e18 / usd_per_eth;
				let gas_per_tx: f32 = 21000.0;
				let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx;
				info!(target: "miner", "Updated conversion rate to Ξ1 = {} ({} wei/gas)", Colour::White.bold().paint(format!("US${}", usd_per_eth)), Colour::Yellow.bold().paint(format!("{}", wei_per_gas)));
				set_price(U256::from(wei_per_gas as u64));
			});

			self.next_calibration = Instant::now() + self.options.recalibration_period;
		}
	}
}

/// Struct to look after updating the acceptable gas price of a miner.
#[derive(Debug, PartialEq)]
pub enum GasPricer {
	/// A fixed gas price in terms of Wei - always the argument given.
	Fixed(U256),
	/// Gas price is calibrated according to a fixed amount of USD.
	Calibrated(GasPriceCalibrator),
}

impl GasPricer {
	/// Create a new Calibrated `GasPricer`.
	pub fn new_calibrated(options: GasPriceCalibratorOptions) -> GasPricer {
		GasPricer::Calibrated(GasPriceCalibrator {
			options: options,
			next_calibration: Instant::now(),
		})
	}

	/// Create a new Fixed `GasPricer`.
	pub fn new_fixed(gas_price: U256) -> GasPricer {
		GasPricer::Fixed(gas_price)
	}

	fn recalibrate<F: Fn(U256) + Sync + Send + 'static>(&mut self, set_price: F) {
		match *self {
			GasPricer::Fixed(ref max) => set_price(max.clone()),
			GasPricer::Calibrated(ref mut cal) => cal.recalibrate(set_price),
		}
	}
}

struct SealingWork {
	queue: UsingQueue<ClosedBlock>,
	enabled: bool,
}

/// Keeps track of transactions using priority queue and holds currently mined block.
/// Handles preparing work for "work sealing" or seals "internally" if Engine does not require work.
pub struct Miner {
	// NOTE [ToDr]  When locking always lock in this order!
	transaction_queue: Arc<Mutex<BanningTransactionQueue>>,
	sealing_work: Mutex<SealingWork>,
	next_allowed_reseal: Mutex<Instant>,
	sealing_block_last_request: Mutex<u64>,
	// for sealing...
	options: MinerOptions,
	/// Does the node perform internal (without work) sealing.
	pub seals_internally: bool,

	gas_range_target: RwLock<(U256, U256)>,
	author: RwLock<Address>,
	extra_data: RwLock<Bytes>,
	engine: Arc<Engine>,

	accounts: Option<Arc<AccountProvider>>,
	notifiers: RwLock<Vec<Box<NotifyWork>>>,
	gas_pricer: Mutex<GasPricer>,
	service_transaction_action: ServiceTransactionAction,
}

impl Miner {
	/// Push notifier that will handle new jobs
	pub fn push_notifier(&self, notifier: Box<NotifyWork>) {
		self.notifiers.write().push(notifier)
	}

	/// Creates new instance of miner Arc.
	pub fn new(options: MinerOptions, gas_pricer: GasPricer, spec: &Spec, accounts: Option<Arc<AccountProvider>>) -> Arc<Miner> {
		Arc::new(Miner::new_raw(options, gas_pricer, spec, accounts))
	}

	/// Creates new instance of miner.
	fn new_raw(options: MinerOptions, gas_pricer: GasPricer, spec: &Spec, accounts: Option<Arc<AccountProvider>>) -> Miner {
		let gas_limit = match options.tx_queue_gas_limit {
			GasLimit::Fixed(ref limit) => *limit,
			_ => !U256::zero(),
		};

		let txq = TransactionQueue::with_limits(options.tx_queue_strategy, options.tx_queue_size, gas_limit, options.tx_gas_limit);
		let txq = match options.tx_queue_banning {
			Banning::Disabled => BanningTransactionQueue::new(txq, Threshold::NeverBan, Duration::from_secs(180)),
			Banning::Enabled { ban_duration, min_offends, .. } => BanningTransactionQueue::new(
				txq,
				Threshold::BanAfter(min_offends),
				ban_duration,
			),
		};

		let notifiers: Vec<Box<NotifyWork>> = match options.new_work_notify.is_empty() {
			true => Vec::new(),
			false => vec![Box::new(WorkPoster::new(&options.new_work_notify))],
		};

		let service_transaction_action = match options.refuse_service_transactions {
			true => ServiceTransactionAction::Refuse,
			false => ServiceTransactionAction::Check(ServiceTransactionChecker::default()),
		};

		Miner {
			transaction_queue: Arc::new(Mutex::new(txq)),
			next_allowed_reseal: Mutex::new(Instant::now()),
			sealing_block_last_request: Mutex::new(0),
			sealing_work: Mutex::new(SealingWork{
				queue: UsingQueue::new(options.work_queue_size),
				enabled: options.force_sealing
					|| !options.new_work_notify.is_empty()
					|| spec.engine.is_default_sealer().unwrap_or(false)
			}),
			seals_internally: spec.engine.is_default_sealer().is_some(),
			gas_range_target: RwLock::new((U256::zero(), U256::zero())),
			author: RwLock::new(Address::default()),
			extra_data: RwLock::new(Vec::new()),
			options: options,
			accounts: accounts,
			engine: spec.engine.clone(),
			notifiers: RwLock::new(notifiers),
			gas_pricer: Mutex::new(gas_pricer),
			service_transaction_action: service_transaction_action,
		}
	}

	/// Creates new instance of miner with accounts and with given spec.
	pub fn with_spec_and_accounts(spec: &Spec, accounts: Option<Arc<AccountProvider>>) -> Miner {
		Miner::new_raw(Default::default(), GasPricer::new_fixed(20_000_000_000u64.into()), spec, accounts)
	}

	/// Creates new instance of miner without accounts, but with given spec.
	pub fn with_spec(spec: &Spec) -> Miner {
		Miner::new_raw(Default::default(), GasPricer::new_fixed(20_000_000_000u64.into()), spec, None)
	}

	fn forced_sealing(&self) -> bool {
		self.options.force_sealing || !self.options.new_work_notify.is_empty()
	}

	/// Clear all pending block states
	pub fn clear(&self) {
		self.sealing_work.lock().queue.reset();
	}

	/// Get `Some` `clone()` of the current pending block's state or `None` if we're not sealing.
	pub fn pending_state(&self) -> Option<State> {
		self.sealing_work.lock().queue.peek_last_ref().map(|b| b.block().fields().state.clone())
	}

	/// Get `Some` `clone()` of the current pending block or `None` if we're not sealing.
	pub fn pending_block(&self) -> Option<Block> {
		self.sealing_work.lock().queue.peek_last_ref().map(|b| b.to_base())
	}

	#[cfg_attr(feature="dev", allow(match_same_arms))]
	/// Prepares new block for sealing including top transactions from queue.
	fn prepare_block(&self, chain: &MiningBlockChainClient) -> (ClosedBlock, Option<H256>) {
		let _timer = PerfTimer::new("prepare_block");
		let chain_info = chain.chain_info();
		let (transactions, mut open_block, original_work_hash) = {
			let transactions = {self.transaction_queue.lock().top_transactions_at(chain_info.best_block_number)};
			let mut sealing_work = self.sealing_work.lock();
			let last_work_hash = sealing_work.queue.peek_last_ref().map(|pb| pb.block().fields().header.hash());
			let best_hash = chain_info.best_block_hash;
/*
			// check to see if last ClosedBlock in would_seals is actually same parent block.
			// if so
			//   duplicate, re-open and push any new transactions.
			//   if at least one was pushed successfully, close and enqueue new ClosedBlock;
			//   otherwise, leave everything alone.
			// otherwise, author a fresh block.
*/
			let open_block = match sealing_work.queue.pop_if(|b| b.block().fields().header.parent_hash() == &best_hash) {
				Some(old_block) => {
					trace!(target: "miner", "prepare_block: Already have previous work; updating and returning");
					// add transactions to old_block
					old_block.reopen(&*self.engine)
				}
				None => {
					// block not found - create it.
					trace!(target: "miner", "prepare_block: No existing work - making new block");
					chain.prepare_open_block(
						self.author(),
						(self.gas_floor_target(), self.gas_ceil_target()),
						self.extra_data()
					)
				}
			};
			(transactions, open_block, last_work_hash)
		};

		let mut invalid_transactions = HashSet::new();
		let mut transactions_to_penalize = HashSet::new();
		let block_number = open_block.block().fields().header.number();

		// TODO Push new uncles too.
		let mut tx_count: usize = 0;
		let tx_total = transactions.len();
		for tx in transactions {
			let hash = tx.hash();
			let start = Instant::now();
			let result = open_block.push_transaction(tx, None);
			let took = start.elapsed();

			// Check for heavy transactions
			match self.options.tx_queue_banning {
				Banning::Enabled { ref offend_threshold, .. } if &took > offend_threshold => {
					match self.transaction_queue.lock().ban_transaction(&hash) {
						true => {
							warn!(target: "miner", "Detected heavy transaction. Banning the sender and recipient/code.");
						},
						false => {
							transactions_to_penalize.insert(hash);
							debug!(target: "miner", "Detected heavy transaction. Penalizing sender.")
						}
					}
				},
				_ => {},
			}
			trace!(target: "miner", "Adding tx {:?} took {:?}", hash, took);
			match result {
				Err(Error::Execution(ExecutionError::BlockGasLimitReached { gas_limit, gas_used, gas })) => {
					debug!(target: "miner", "Skipping adding transaction to block because of gas limit: {:?} (limit: {:?}, used: {:?}, gas: {:?})", hash, gas_limit, gas_used, gas);

					// Penalize transaction if it's above current gas limit
					if gas > gas_limit {
						transactions_to_penalize.insert(hash);
					}

					// Exit early if gas left is smaller then min_tx_gas
					let min_tx_gas: U256 = 21000.into();	// TODO: figure this out properly.
					if gas_limit - gas_used < min_tx_gas {
						break;
					}
				},
				// Invalid nonce error can happen only if previous transaction is skipped because of gas limit.
				// If there is errornous state of transaction queue it will be fixed when next block is imported.
				Err(Error::Execution(ExecutionError::InvalidNonce { expected, got })) => {
					debug!(target: "miner", "Skipping adding transaction to block because of invalid nonce: {:?} (expected: {:?}, got: {:?})", hash, expected, got);
				},
				// already have transaction - ignore
				Err(Error::Transaction(TransactionError::AlreadyImported)) => {},
				Err(e) => {
					invalid_transactions.insert(hash);
					debug!(target: "miner",
						   "Error adding transaction to block: number={}. transaction_hash={:?}, Error: {:?}",
						   block_number, hash, e);
				},
				_ => {
					tx_count += 1;
				}	// imported ok
			}
		}
		trace!(target: "miner", "Pushed {}/{} transactions", tx_count, tx_total);

		let block = open_block.close();

		let fetch_nonce = |a: &Address| chain.latest_nonce(a);

		{
			let mut queue = self.transaction_queue.lock();
			for hash in invalid_transactions {
				queue.remove_invalid(&hash, &fetch_nonce);
			}
			for hash in transactions_to_penalize {
				queue.penalize(&hash);
			}
		}
		(block, original_work_hash)
	}

	/// Asynchronously updates minimal gas price for transaction queue
	pub fn recalibrate_minimal_gas_price(&self) {
		debug!(target: "miner", "minimal_gas_price: recalibrating...");
		let txq = self.transaction_queue.clone();
		self.gas_pricer.lock().recalibrate(move |price| {
			debug!(target: "miner", "minimal_gas_price: Got gas price! {}", price);
			txq.lock().set_minimal_gas_price(price);
		});
	}

	/// Check is reseal is allowed and necessary.
	fn requires_reseal(&self, best_block: BlockNumber) -> bool {
		let has_local_transactions = self.transaction_queue.lock().has_local_pending_transactions();
		let mut sealing_work = self.sealing_work.lock();
		if sealing_work.enabled {
			trace!(target: "miner", "requires_reseal: sealing enabled");
			let last_request = *self.sealing_block_last_request.lock();
			let should_disable_sealing = !self.forced_sealing()
				&& !has_local_transactions
				&& !self.seals_internally
				&& best_block > last_request
				&& best_block - last_request > SEALING_TIMEOUT_IN_BLOCKS;

			trace!(target: "miner", "requires_reseal: should_disable_sealing={}; best_block={}, last_request={}", should_disable_sealing, best_block, last_request);

			if should_disable_sealing {
				trace!(target: "miner", "Miner sleeping (current {}, last {})", best_block, last_request);
				sealing_work.enabled = false;
				sealing_work.queue.reset();
				false
			} else {
				// sealing enabled and we don't want to sleep.
				*self.next_allowed_reseal.lock() = Instant::now() + self.options.reseal_min_period;
				true
			}
		} else {
			trace!(target: "miner", "requires_reseal: sealing is disabled");
			false
		}
	}

	/// Attempts to perform internal sealing (one that does not require work) and handles the result depending on the type of Seal.
	fn seal_and_import_block_internally(&self, chain: &MiningBlockChainClient, block: ClosedBlock) -> bool {
		if !block.transactions().is_empty() || self.forced_sealing() {
			trace!(target: "miner", "seal_block_internally: attempting internal seal.");
			match self.engine.generate_seal(block.block()) {
				// Save proposal for later seal submission and broadcast it.
				Seal::Proposal(seal) => {
					trace!(target: "miner", "Received a Proposal seal.");
					{
						let mut sealing_work = self.sealing_work.lock();
						sealing_work.queue.push(block.clone());
						sealing_work.queue.use_last_ref();
					}
					block
						.lock()
						.seal(&*self.engine, seal)
						.map(|sealed| { chain.broadcast_proposal_block(sealed); true })
						.unwrap_or_else(|e| {
							warn!("ERROR: seal failed when given internally generated seal: {}", e);
							false
						})
				},
				// Directly import a regular sealed block.
				Seal::Regular(seal) =>
					block
						.lock()
						.seal(&*self.engine, seal)
						.map(|sealed| chain.import_sealed_block(sealed).is_ok())
						.unwrap_or_else(|e| {
							warn!("ERROR: seal failed when given internally generated seal: {}", e);
							false
						}),
				Seal::None => false,
			}
		} else {
			false
		}
	}

	/// Prepares work which has to be done to seal.
	fn prepare_work(&self, block: ClosedBlock, original_work_hash: Option<H256>) {
		let (work, is_new) = {
			let mut sealing_work = self.sealing_work.lock();
			let last_work_hash = sealing_work.queue.peek_last_ref().map(|pb| pb.block().fields().header.hash());
			trace!(target: "miner", "prepare_work: Checking whether we need to reseal: orig={:?} last={:?}, this={:?}", original_work_hash, last_work_hash, block.block().fields().header.hash());
			let (work, is_new) = if last_work_hash.map_or(true, |h| h != block.block().fields().header.hash()) {
				trace!(target: "miner", "prepare_work: Pushing a new, refreshed or borrowed pending {}...", block.block().fields().header.hash());
				let pow_hash = block.block().fields().header.hash();
				let number = block.block().fields().header.number();
				let difficulty = *block.block().fields().header.difficulty();
				let is_new = original_work_hash.map_or(true, |h| block.block().fields().header.hash() != h);
				sealing_work.queue.push(block);
				// If push notifications are enabled we assume all work items are used.
				if !self.notifiers.read().is_empty() && is_new {
					sealing_work.queue.use_last_ref();
				}
				(Some((pow_hash, difficulty, number)), is_new)
			} else {
				(None, false)
			};
			trace!(target: "miner", "prepare_work: leaving (last={:?})", sealing_work.queue.peek_last_ref().map(|b| b.block().fields().header.hash()));
			(work, is_new)
		};
		if is_new {
			work.map(|(pow_hash, difficulty, number)| {
				for notifier in self.notifiers.read().iter() {
					notifier.notify(pow_hash, difficulty, number)
				}
			});
		}
	}

	fn update_gas_limit(&self, client: &MiningBlockChainClient) {
		let gas_limit = client.best_block_header().gas_limit();
		let mut queue = self.transaction_queue.lock();
		queue.set_gas_limit(gas_limit);
		if let GasLimit::Auto = self.options.tx_queue_gas_limit {
			// Set total tx queue gas limit to be 20x the block gas limit.
			queue.set_total_gas_limit(gas_limit * 20.into());
		}
	}

	/// Returns true if we had to prepare new pending block.
	fn prepare_work_sealing(&self, client: &MiningBlockChainClient) -> bool {
		trace!(target: "miner", "prepare_work_sealing: entering");
		let prepare_new = {
			let mut sealing_work = self.sealing_work.lock();
			let have_work = sealing_work.queue.peek_last_ref().is_some();
			trace!(target: "miner", "prepare_work_sealing: have_work={}", have_work);
			if !have_work {
				sealing_work.enabled = true;
				true
			} else {
				false
			}
		};
		if prepare_new {
			// --------------------------------------------------------------------------
			// | NOTE Code below requires transaction_queue and sealing_work locks.     |
			// | Make sure to release the locks before calling that method.             |
			// --------------------------------------------------------------------------
			let (block, original_work_hash) = self.prepare_block(client);
			self.prepare_work(block, original_work_hash);
		}
		let mut sealing_block_last_request = self.sealing_block_last_request.lock();
		let best_number = client.chain_info().best_block_number;
		if *sealing_block_last_request != best_number {
			trace!(target: "miner", "prepare_work_sealing: Miner received request (was {}, now {}) - waking up.", *sealing_block_last_request, best_number);
			*sealing_block_last_request = best_number;
		}

		// Return if we restarted
		prepare_new
	}

	fn add_transactions_to_queue(
		&self,
		client: &MiningBlockChainClient,
		transactions: Vec<UnverifiedTransaction>,
		default_origin: TransactionOrigin,
		min_block: Option<BlockNumber>,
		transaction_queue: &mut BanningTransactionQueue,
	) -> Vec<Result<TransactionImportResult, Error>> {
		let accounts = self.accounts.as_ref()
			.and_then(|provider| provider.accounts().ok())
			.map(|accounts| accounts.into_iter().collect::<HashSet<_>>());

		let best_block_header = client.best_block_header().decode();
		let insertion_time = client.chain_info().best_block_number;

		transactions.into_iter()
			.map(|tx| {
				let hash = tx.hash();
				if client.transaction_block(TransactionId::Hash(hash)).is_some() {
					debug!(target: "miner", "Rejected tx {:?}: already in the blockchain", hash);
					return Err(Error::Transaction(TransactionError::AlreadyImported));
				}
				match self.engine.verify_transaction_basic(&tx, &best_block_header)
					.and_then(|_| self.engine.verify_transaction(tx, &best_block_header))
				{
					Err(e) => {
						debug!(target: "miner", "Rejected tx {:?} with invalid signature: {:?}", hash, e);
						Err(e)
					},
					Ok(transaction) => {
						let origin = accounts.as_ref().and_then(|accounts| {
							match accounts.contains(&transaction.sender()) {
								true => Some(TransactionOrigin::Local),
								false => None,
							}
						}).unwrap_or(default_origin);

						// try to install service transaction checker before appending transactions
						self.service_transaction_action.update_from_chain_client(client);

						let details_provider = TransactionDetailsProvider::new(client, &self.service_transaction_action);
						match origin {
							TransactionOrigin::Local | TransactionOrigin::RetractedBlock => {
								transaction_queue.add(transaction, origin, insertion_time, min_block, &details_provider)
							},
							TransactionOrigin::External => {
								transaction_queue.add_with_banlist(transaction, insertion_time, &details_provider)
							},
						}
					},
				}
			})
			.collect()
	}

	/// Are we allowed to do a non-mandatory reseal?
	fn tx_reseal_allowed(&self) -> bool { Instant::now() > *self.next_allowed_reseal.lock() }

	#[cfg_attr(feature="dev", allow(wrong_self_convention))]
	#[cfg_attr(feature="dev", allow(redundant_closure))]
	fn from_pending_block<H, F, G>(&self, latest_block_number: BlockNumber, from_chain: F, map_block: G) -> H
		where F: Fn() -> H, G: Fn(&ClosedBlock) -> H {
		let sealing_work = self.sealing_work.lock();
		sealing_work.queue.peek_last_ref().map_or_else(
			|| from_chain(),
			|b| {
				if b.block().header().number() > latest_block_number {
					map_block(b)
				} else {
					from_chain()
				}
			}
		)
	}
}

const SEALING_TIMEOUT_IN_BLOCKS : u64 = 5;

impl MinerService for Miner {

	fn clear_and_reset(&self, chain: &MiningBlockChainClient) {
		self.transaction_queue.lock().clear();
		// --------------------------------------------------------------------------
		// | NOTE Code below requires transaction_queue and sealing_work locks.     |
		// | Make sure to release the locks before calling that method.             |
		// --------------------------------------------------------------------------
		self.update_sealing(chain);
	}

	fn status(&self) -> MinerStatus {
		let status = self.transaction_queue.lock().status();
		let sealing_work = self.sealing_work.lock();
		MinerStatus {
			transactions_in_pending_queue: status.pending,
			transactions_in_future_queue: status.future,
			transactions_in_pending_block: sealing_work.queue.peek_last_ref().map_or(0, |b| b.transactions().len()),
		}
	}

	fn call(&self, client: &MiningBlockChainClient, t: &SignedTransaction, analytics: CallAnalytics) -> Result<Executed, CallError> {
		let sealing_work = self.sealing_work.lock();
		match sealing_work.queue.peek_last_ref() {
			Some(work) => {
				let block = work.block();

				// TODO: merge this code with client.rs's fn call somwhow.
				let header = block.header();
				let last_hashes = Arc::new(client.last_hashes());
				let env_info = EnvInfo {
					number: header.number(),
					author: *header.author(),
					timestamp: header.timestamp(),
					difficulty: *header.difficulty(),
					last_hashes: last_hashes,
					gas_used: U256::zero(),
					gas_limit: U256::max_value(),
				};
				// that's just a copy of the state.
				let mut state = block.state().clone();
				let original_state = if analytics.state_diffing { Some(state.clone()) } else { None };

				let sender = t.sender();
				let balance = state.balance(&sender);
				let needed_balance = t.value + t.gas * t.gas_price;
				if balance < needed_balance {
					// give the sender a sufficient balance
					state.add_balance(&sender, &(needed_balance - balance), CleanupMode::NoEmpty);
				}
				let options = TransactOptions { tracing: analytics.transaction_tracing, vm_tracing: analytics.vm_tracing, check_nonce: false };
				let mut ret = Executive::new(&mut state, &env_info, &*self.engine, client.vm_factory()).transact(t, options)?;

				// TODO gav move this into Executive.
				ret.state_diff = original_state.map(|original| state.diff_from(original));

				Ok(ret)
			},
			None => client.call(t, BlockId::Latest, analytics)
		}
	}

	fn balance(&self, chain: &MiningBlockChainClient, address: &Address) -> U256 {
		self.from_pending_block(
			chain.chain_info().best_block_number,
			|| chain.latest_balance(address),
			|b| b.block().fields().state.balance(address)
		)
	}

	fn storage_at(&self, chain: &MiningBlockChainClient, address: &Address, position: &H256) -> H256 {
		self.from_pending_block(
			chain.chain_info().best_block_number,
			|| chain.latest_storage_at(address, position),
			|b| b.block().fields().state.storage_at(address, position)
		)
	}

	fn nonce(&self, chain: &MiningBlockChainClient, address: &Address) -> U256 {
		self.from_pending_block(
			chain.chain_info().best_block_number,
			|| chain.latest_nonce(address),
			|b| b.block().fields().state.nonce(address)
		)
	}

	fn code(&self, chain: &MiningBlockChainClient, address: &Address) -> Option<Bytes> {
		self.from_pending_block(
			chain.chain_info().best_block_number,
			|| chain.latest_code(address),
			|b| b.block().fields().state.code(address).map(|c| (*c).clone())
		)
	}

	fn set_author(&self, author: Address) {
		if self.seals_internally {
			let mut sealing_work = self.sealing_work.lock();
			sealing_work.enabled = self.engine.is_sealer(&author).unwrap_or(false);
		}
		*self.author.write() = author;
	}

	fn set_engine_signer(&self, address: Address, password: String) -> Result<(), AccountError> {
		if self.seals_internally {
			if let Some(ref ap) = self.accounts {
				ap.sign(address.clone(), Some(password.clone()), Default::default())?;
				// Limit the scope of the locks.
				{
					let mut sealing_work = self.sealing_work.lock();
					sealing_work.enabled = self.engine.is_sealer(&address).unwrap_or(false);
					*self.author.write() = address;
				}
				// --------------------------------------------------------------------------
				// | NOTE Code below may require author and sealing_work locks              |
				// | (some `Engine`s call `EngineClient.update_sealing()`)                  |.
				// | Make sure to release the locks before calling that method.             |
				// --------------------------------------------------------------------------
				self.engine.set_signer(ap.clone(), address, password);
			}
		}
		Ok(())
	}

	fn set_extra_data(&self, extra_data: Bytes) {
		*self.extra_data.write() = extra_data;
	}

	/// Set the gas limit we wish to target when sealing a new block.
	fn set_gas_floor_target(&self, target: U256) {
		self.gas_range_target.write().0 = target;
	}

	fn set_gas_ceil_target(&self, target: U256) {
		self.gas_range_target.write().1 = target;
	}

	fn set_minimal_gas_price(&self, min_gas_price: U256) {
		self.transaction_queue.lock().set_minimal_gas_price(min_gas_price);
	}

	fn minimal_gas_price(&self) -> U256 {
		*self.transaction_queue.lock().minimal_gas_price()
	}

	fn sensible_gas_price(&self) -> U256 {
		// 10% above our minimum.
		*self.transaction_queue.lock().minimal_gas_price() * 110.into() / 100.into()
	}

	fn sensible_gas_limit(&self) -> U256 {
		self.gas_range_target.read().0 / 5.into()
	}

	fn transactions_limit(&self) -> usize {
		self.transaction_queue.lock().limit()
	}

	fn set_transactions_limit(&self, limit: usize) {
		self.transaction_queue.lock().set_limit(limit)
	}

	fn set_tx_gas_limit(&self, limit: U256) {
		self.transaction_queue.lock().set_tx_gas_limit(limit)
	}

	/// Get the author that we will seal blocks as.
	fn author(&self) -> Address {
		*self.author.read()
	}

	/// Get the extra_data that we will seal blocks with.
	fn extra_data(&self) -> Bytes {
		self.extra_data.read().clone()
	}

	/// Get the gas limit we wish to target when sealing a new block.
	fn gas_floor_target(&self) -> U256 {
		self.gas_range_target.read().0
	}

	/// Get the gas limit we wish to target when sealing a new block.
	fn gas_ceil_target(&self) -> U256 {
		self.gas_range_target.read().1
	}

	fn import_external_transactions(
		&self,
		chain: &MiningBlockChainClient,
		transactions: Vec<UnverifiedTransaction>
	) -> Vec<Result<TransactionImportResult, Error>> {
		trace!(target: "external_tx", "Importing external transactions");
		let results = {
			let mut transaction_queue = self.transaction_queue.lock();
			self.add_transactions_to_queue(
				chain, transactions, TransactionOrigin::External, None, &mut transaction_queue
			)
		};

		if !results.is_empty() && self.options.reseal_on_external_tx &&	self.tx_reseal_allowed() {
			// --------------------------------------------------------------------------
			// | NOTE Code below requires transaction_queue and sealing_work locks.     |
			// | Make sure to release the locks before calling that method.             |
			// --------------------------------------------------------------------------
			self.update_sealing(chain);
		}
		results
	}

	#[cfg_attr(feature="dev", allow(collapsible_if))]
	fn import_own_transaction(
		&self,
		chain: &MiningBlockChainClient,
		pending: PendingTransaction,
	) -> Result<TransactionImportResult, Error> {

		trace!(target: "own_tx", "Importing transaction: {:?}", pending);

		let imported = {
			// Be sure to release the lock before we call prepare_work_sealing
			let mut transaction_queue = self.transaction_queue.lock();
			// We need to re-validate transactions
			let import = self.add_transactions_to_queue(
				chain, vec![pending.transaction.into()], TransactionOrigin::Local, pending.min_block, &mut transaction_queue
			).pop().expect("one result returned per added transaction; one added => one result; qed");

			match import {
				Ok(_) => {
					trace!(target: "own_tx", "Status: {:?}", transaction_queue.status());
				},
				Err(ref e) => {
					trace!(target: "own_tx", "Status: {:?}", transaction_queue.status());
					warn!(target: "own_tx", "Error importing transaction: {:?}", e);
				},
			}
			import
		};

		// --------------------------------------------------------------------------
		// | NOTE Code below requires transaction_queue and sealing_work locks.     |
		// | Make sure to release the locks before calling that method.             |
		// --------------------------------------------------------------------------
		if imported.is_ok() && self.options.reseal_on_own_tx && self.tx_reseal_allowed() {
			// Make sure to do it after transaction is imported and lock is droped.
			// We need to create pending block and enable sealing.
			if self.seals_internally || !self.prepare_work_sealing(chain) {
				// If new block has not been prepared (means we already had one)
				// or Engine might be able to seal internally,
				// we need to update sealing.
				self.update_sealing(chain);
			}
		}

		imported
	}

	fn pending_transactions(&self) -> Vec<PendingTransaction> {
		let queue = self.transaction_queue.lock();
		queue.pending_transactions(BlockNumber::max_value())
	}

	fn local_transactions(&self) -> BTreeMap<H256, LocalTransactionStatus> {
		let queue = self.transaction_queue.lock();
		queue.local_transactions()
			.iter()
			.map(|(hash, status)| (*hash, status.clone()))
			.collect()
	}

	fn future_transactions(&self) -> Vec<PendingTransaction> {
		self.transaction_queue.lock().future_transactions()
	}

	fn ready_transactions(&self, best_block: BlockNumber) -> Vec<PendingTransaction> {
		let queue = self.transaction_queue.lock();
		match self.options.pending_set {
			PendingSet::AlwaysQueue => queue.pending_transactions(best_block),
			PendingSet::SealingOrElseQueue => {
				self.from_pending_block(
					best_block,
					|| queue.pending_transactions(best_block),
					|sealing| sealing.transactions().iter().map(|t| t.clone().into()).collect()
				)
			},
			PendingSet::AlwaysSealing => {
				self.from_pending_block(
					best_block,
					|| vec![],
					|sealing| sealing.transactions().iter().map(|t| t.clone().into()).collect()
				)
			},
		}
	}

	fn pending_transactions_hashes(&self, best_block: BlockNumber) -> Vec<H256> {
		let queue = self.transaction_queue.lock();
		match self.options.pending_set {
			PendingSet::AlwaysQueue => queue.pending_hashes(),
			PendingSet::SealingOrElseQueue => {
				self.from_pending_block(
					best_block,
					|| queue.pending_hashes(),
					|sealing| sealing.transactions().iter().map(|t| t.hash()).collect()
				)
			},
			PendingSet::AlwaysSealing => {
				self.from_pending_block(
					best_block,
					|| vec![],
					|sealing| sealing.transactions().iter().map(|t| t.hash()).collect()
				)
			},
		}
	}

	fn transaction(&self, best_block: BlockNumber, hash: &H256) -> Option<SignedTransaction> {
		let queue = self.transaction_queue.lock();
		match self.options.pending_set {
			PendingSet::AlwaysQueue => queue.find(hash),
			PendingSet::SealingOrElseQueue => {
				self.from_pending_block(
					best_block,
					|| queue.find(hash),
					|sealing| sealing.transactions().iter().find(|t| &t.hash() == hash).cloned()
				)
			},
			PendingSet::AlwaysSealing => {
				self.from_pending_block(
					best_block,
					|| None,
					|sealing| sealing.transactions().iter().find(|t| &t.hash() == hash).cloned()
				)
			},
		}
	}

	fn pending_receipt(&self, best_block: BlockNumber, hash: &H256) -> Option<RichReceipt> {
		self.from_pending_block(
			best_block,
			|| None,
			|pending| {
				let txs = pending.transactions();
				txs.iter()
					.map(|t| t.hash())
					.position(|t| t == *hash)
					.map(|index| {
						let prev_gas = if index == 0 { Default::default() } else { pending.receipts()[index - 1].gas_used };
						let tx = &txs[index];
						let receipt = &pending.receipts()[index];
						RichReceipt {
							transaction_hash: hash.clone(),
							transaction_index: index,
							cumulative_gas_used: receipt.gas_used,
							gas_used: receipt.gas_used - prev_gas,
							contract_address: match tx.action {
								Action::Call(_) => None,
								Action::Create => {
									let sender = tx.sender();
									Some(contract_address(&sender, &tx.nonce))
								}
							},
							logs: receipt.logs.clone(),
							log_bloom: receipt.log_bloom,
							state_root: receipt.state_root,
						}
					})
			}
		)
	}

	fn pending_receipts(&self, best_block: BlockNumber) -> BTreeMap<H256, Receipt> {
		self.from_pending_block(
			best_block,
			BTreeMap::new,
			|pending| {
				let hashes = pending.transactions()
					.iter()
					.map(|t| t.hash());

				let receipts = pending.receipts().iter().cloned();

				hashes.zip(receipts).collect()
			}
		)
	}

	fn last_nonce(&self, address: &Address) -> Option<U256> {
		self.transaction_queue.lock().last_nonce(address)
	}

	/// Update sealing if required.
	/// Prepare the block and work if the Engine does not seal internally.
	fn update_sealing(&self, chain: &MiningBlockChainClient) {
		trace!(target: "miner", "update_sealing");

		if self.requires_reseal(chain.chain_info().best_block_number) {
			// --------------------------------------------------------------------------
			// | NOTE Code below requires transaction_queue and sealing_work locks.     |
			// | Make sure to release the locks before calling that method.             |
			// --------------------------------------------------------------------------
			trace!(target: "miner", "update_sealing: preparing a block");
			let (block, original_work_hash) = self.prepare_block(chain);
			if self.seals_internally {
				trace!(target: "miner", "update_sealing: engine indicates internal sealing");
				if self.seal_and_import_block_internally(chain, block) {
					trace!(target: "miner", "update_sealing: imported internally sealed block");
				}
			} else {
				trace!(target: "miner", "update_sealing: engine does not seal internally, preparing work");
				self.prepare_work(block, original_work_hash);
			}
		}
	}

	fn is_sealing(&self) -> bool {
		self.sealing_work.lock().queue.is_in_use()
	}

	fn map_sealing_work<F, T>(&self, chain: &MiningBlockChainClient, f: F) -> Option<T> where F: FnOnce(&ClosedBlock) -> T {
		trace!(target: "miner", "map_sealing_work: entering");
		self.prepare_work_sealing(chain);
		trace!(target: "miner", "map_sealing_work: sealing prepared");
		let mut sealing_work = self.sealing_work.lock();
		let ret = sealing_work.queue.use_last_ref();
		trace!(target: "miner", "map_sealing_work: leaving use_last_ref={:?}", ret.as_ref().map(|b| b.block().fields().header.hash()));
		ret.map(f)
	}

	fn submit_seal(&self, chain: &MiningBlockChainClient, block_hash: H256, seal: Vec<Bytes>) -> Result<(), Error> {
		let result =
			if let Some(b) = self.sealing_work.lock().queue.get_used_if(
				if self.options.enable_resubmission {
					GetAction::Clone
				} else {
					GetAction::Take
				},
				|b| &b.hash() == &block_hash
			) {
				trace!(target: "miner", "Submitted block {}={}={} with seal {:?}", block_hash, b.hash(), b.header().bare_hash(), seal);
				b.lock().try_seal(&*self.engine, seal).or_else(|(e, _)| {
					warn!(target: "miner", "Mined solution rejected: {}", e);
					Err(Error::PowInvalid)
				})
			} else {
				warn!(target: "miner", "Submitted solution rejected: Block unknown or out of date.");
				Err(Error::PowHashInvalid)
			};
		result.and_then(|sealed| {
			let n = sealed.header().number();
			let h = sealed.header().hash();
			chain.import_sealed_block(sealed)?;
			info!(target: "miner", "Submitted block imported OK. #{}: {}", Colour::White.bold().paint(format!("{}", n)), Colour::White.bold().paint(h.hex()));
			Ok(())
		})
	}

	fn chain_new_blocks(&self, chain: &MiningBlockChainClient, _imported: &[H256], _invalid: &[H256], enacted: &[H256], retracted: &[H256]) {
		trace!(target: "miner", "chain_new_blocks");

		// 1. We ignore blocks that were `imported` (because it means that they are not in canon-chain, and transactions
		//	  should be still available in the queue.
		// 2. We ignore blocks that are `invalid` because it doesn't have any meaning in terms of the transactions that
		//    are in those blocks

		// First update gas limit in transaction queue
		self.update_gas_limit(chain);

		// Update minimal gas price
		self.recalibrate_minimal_gas_price();

		// Then import all transactions...
		{

			let mut transaction_queue = self.transaction_queue.lock();
			for hash in retracted {
				let block = chain.block(BlockId::Hash(*hash))
					.expect("Client is sending message after commit to db and inserting to chain; the block is available; qed");
				let txs = block.transactions();
				let _ = self.add_transactions_to_queue(
					chain, txs, TransactionOrigin::RetractedBlock, None, &mut transaction_queue
				);
			}
		}

		// ...and at the end remove the old ones
		{
			let fetch_account = |a: &Address| AccountDetails {
				nonce: chain.latest_nonce(a),
				balance: chain.latest_balance(a),
			};
			let time = chain.chain_info().best_block_number;
			let mut transaction_queue = self.transaction_queue.lock();
			transaction_queue.remove_old(&fetch_account, time);
		}

		if enacted.len() > 0 {
			// --------------------------------------------------------------------------
			// | NOTE Code below requires transaction_queue and sealing_work locks.     |
			// | Make sure to release the locks before calling that method.             |
			// --------------------------------------------------------------------------
			self.update_sealing(chain);
		}
	}
}

/// Action when service transaction is received
enum ServiceTransactionAction {
	/// Refuse service transaction immediately
	Refuse,
	/// Accept if sender is certified to send service transactions
	Check(ServiceTransactionChecker),
}

impl ServiceTransactionAction {
	pub fn update_from_chain_client(&self, client: &MiningBlockChainClient) {
		if let ServiceTransactionAction::Check(ref checker) = *self {
			checker.update_from_chain_client(client);
		}
	}

	pub fn check(&self, client: &MiningBlockChainClient, tx: &SignedTransaction) -> Result<bool, String> {
		match *self {
			ServiceTransactionAction::Refuse => Err("configured to refuse service transactions".to_owned()),
			ServiceTransactionAction::Check(ref checker) => checker.check(client, tx),
		}
	}
}

struct TransactionDetailsProvider<'a> {
	client: &'a MiningBlockChainClient,
	service_transaction_action: &'a ServiceTransactionAction,
}

impl<'a> TransactionDetailsProvider<'a> {
	pub fn new(client: &'a MiningBlockChainClient, service_transaction_action: &'a ServiceTransactionAction) -> Self {
		TransactionDetailsProvider {
			client: client,
			service_transaction_action: service_transaction_action,
		}
	}
}

impl<'a> TransactionQueueDetailsProvider for TransactionDetailsProvider<'a> {
	fn fetch_account(&self, address: &Address) -> AccountDetails {
		AccountDetails {
			nonce: self.client.latest_nonce(address),
			balance: self.client.latest_balance(address),
		}
	}

	fn estimate_gas_required(&self, tx: &SignedTransaction) -> U256 {
		tx.gas_required(&self.client.latest_schedule()).into()
	}

	fn is_service_transaction_acceptable(&self, tx: &SignedTransaction) -> Result<bool, String> {
		self.service_transaction_action.check(self.client, tx)
	}
}

#[cfg(test)]
mod tests {

	use std::sync::Arc;
	use std::time::Duration;
	use super::super::{MinerService, PrioritizationStrategy};
	use super::*;
	use block::IsBlock;
	use util::{U256, Uint, FromHex};
	use ethkey::{Generator, Random};
	use client::{BlockChainClient, TestBlockChainClient, EachBlockWith, TransactionImportResult};
	use header::BlockNumber;
	use types::transaction::{SignedTransaction, Transaction, PendingTransaction, Action};
	use spec::Spec;
	use tests::helpers::{generate_dummy_client};

	#[test]
	fn should_prepare_block_to_seal() {
		// given
		let client = TestBlockChainClient::default();
		let miner = Miner::with_spec(&Spec::new_test());

		// when
		let sealing_work = miner.map_sealing_work(&client, |_| ());
		assert!(sealing_work.is_some(), "Expected closed block");
	}

	#[test]
	fn should_still_work_after_a_couple_of_blocks() {
		// given
		let client = TestBlockChainClient::default();
		let miner = Miner::with_spec(&Spec::new_test());

		let res = miner.map_sealing_work(&client, |b| b.block().fields().header.hash());
		assert!(res.is_some());
		assert!(miner.submit_seal(&client, res.unwrap(), vec![]).is_ok());

		// two more blocks mined, work requested.
		client.add_blocks(1, EachBlockWith::Uncle);
		miner.map_sealing_work(&client, |b| b.block().fields().header.hash());

		client.add_blocks(1, EachBlockWith::Uncle);
		miner.map_sealing_work(&client, |b| b.block().fields().header.hash());

		// solution to original work submitted.
		assert!(miner.submit_seal(&client, res.unwrap(), vec![]).is_ok());
	}

	fn miner() -> Miner {
		Arc::try_unwrap(Miner::new(
			MinerOptions {
				new_work_notify: Vec::new(),
				force_sealing: false,
				reseal_on_external_tx: false,
				reseal_on_own_tx: true,
				reseal_min_period: Duration::from_secs(5),
				tx_gas_limit: !U256::zero(),
				tx_queue_size: 1024,
				tx_queue_gas_limit: GasLimit::None,
				tx_queue_strategy: PrioritizationStrategy::GasFactorAndGasPrice,
				pending_set: PendingSet::AlwaysSealing,
				work_queue_size: 5,
				enable_resubmission: true,
				tx_queue_banning: Banning::Disabled,
				refuse_service_transactions: false,
			},
			GasPricer::new_fixed(0u64.into()),
			&Spec::new_test(),
			None, // accounts provider
		)).ok().expect("Miner was just created.")
	}

	fn transaction() -> SignedTransaction {
		let keypair = Random.generate().unwrap();
		Transaction {
			action: Action::Create,
			value: U256::zero(),
			data: "3331600055".from_hex().unwrap(),
			gas: U256::from(100_000),
			gas_price: U256::zero(),
			nonce: U256::zero(),
		}.sign(keypair.secret(), None)
	}

	#[test]
	fn should_make_pending_block_when_importing_own_transaction() {
		// given
		let client = TestBlockChainClient::default();
		let miner = miner();
		let transaction = transaction();
		let best_block = 0;
		// when
		let res = miner.import_own_transaction(&client, PendingTransaction::new(transaction, None));

		// then
		assert_eq!(res.unwrap(), TransactionImportResult::Current);
		assert_eq!(miner.pending_transactions().len(), 1);
		assert_eq!(miner.ready_transactions(best_block).len(), 1);
		assert_eq!(miner.pending_transactions_hashes(best_block).len(), 1);
		assert_eq!(miner.pending_receipts(best_block).len(), 1);
		// This method will let us know if pending block was created (before calling that method)
		assert!(!miner.prepare_work_sealing(&client));
	}

	#[test]
	fn should_not_use_pending_block_if_best_block_is_higher() {
		// given
		let client = TestBlockChainClient::default();
		let miner = miner();
		let transaction = transaction();
		let best_block = 10;
		// when
		let res = miner.import_own_transaction(&client, PendingTransaction::new(transaction, None));

		// then
		assert_eq!(res.unwrap(), TransactionImportResult::Current);
		assert_eq!(miner.pending_transactions().len(), 1);
		assert_eq!(miner.ready_transactions(best_block).len(), 0);
		assert_eq!(miner.pending_transactions_hashes(best_block).len(), 0);
		assert_eq!(miner.pending_receipts(best_block).len(), 0);
	}

	#[test]
	fn should_import_external_transaction() {
		// given
		let client = TestBlockChainClient::default();
		let miner = miner();
		let transaction = transaction().into();
		let best_block = 0;
		// when
		let res = miner.import_external_transactions(&client, vec![transaction]).pop().unwrap();

		// then
		assert_eq!(res.unwrap(), TransactionImportResult::Current);
		assert_eq!(miner.pending_transactions().len(), 1);
		assert_eq!(miner.pending_transactions_hashes(best_block).len(), 0);
		assert_eq!(miner.ready_transactions(best_block).len(), 0);
		assert_eq!(miner.pending_receipts(best_block).len(), 0);
		// This method will let us know if pending block was created (before calling that method)
		assert!(miner.prepare_work_sealing(&client));
	}

	#[test]
	fn should_not_seal_unless_enabled() {
		let miner = miner();
		let client = TestBlockChainClient::default();
		// By default resealing is not required.
		assert!(!miner.requires_reseal(1u8.into()));

		miner.import_external_transactions(&client, vec![transaction().into()]).pop().unwrap().unwrap();
		assert!(miner.prepare_work_sealing(&client));
		// Unless asked to prepare work.
		assert!(miner.requires_reseal(1u8.into()));
	}

	#[test]
	fn internal_seals_without_work() {
		let miner = Miner::with_spec(&Spec::new_instant());

		let c = generate_dummy_client(2);
		let client = c.reference().as_ref();

		assert_eq!(miner.import_external_transactions(client, vec![transaction().into()]).pop().unwrap().unwrap(), TransactionImportResult::Current);

		miner.update_sealing(client);
		client.flush_queue();
		assert!(miner.pending_block().is_none());
		assert_eq!(client.chain_info().best_block_number, 3 as BlockNumber);

		assert_eq!(miner.import_own_transaction(client, PendingTransaction::new(transaction().into(), None)).unwrap(), TransactionImportResult::Current);

		miner.update_sealing(client);
		client.flush_queue();
		assert!(miner.pending_block().is_none());
		assert_eq!(client.chain_info().best_block_number, 4 as BlockNumber);
	}
}