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

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

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

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

use std::cmp;
use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Arc;

use block_reward::{self, BlockRewardContract, RewardKind};
use common_types::{
	BlockNumber,
	header::Header,
	engines::{
		VapashSeal,
		OptimizeFor,
		params::CommonParams,
	},
	errors::{BlockError, VapcoreError as Error},
	snapshot::Snapshotting,
};
use enjen::Engine;
use vapory_types::{H256, U256};
use vapjson;
use vapash::{self, quick_get_difficulty, slow_hash_block_number, VapashManager};
use tetsy_keccak_hash::{KECCAK_EMPTY_LIST_RLP};
use log::trace;
use tetsy_macros::map;
use mashina::{
	ExecutedBlock,
	Machine,
};
use tetsy_unexpected::{OutOfBounds, Mismatch};

/// Number of blocks in an vapash snapshot.
// make dependent on difficulty increment divisor?
const SNAPSHOT_BLOCKS: u64 = 5000;
/// Maximum number of blocks allowed in an vapash snapshot.
const MAX_SNAPSHOT_BLOCKS: u64 = 30000;

/// Vapash params.
#[derive(Debug, PartialEq)]
pub struct VapashParams {
	/// Minimum difficulty.
	pub minimum_difficulty: U256,
	/// Difficulty bound divisor.
	pub difficulty_bound_divisor: U256,
	/// Difficulty increment divisor.
	pub difficulty_increment_divisor: u64,
	/// Metropolis difficulty increment divisor.
	pub metropolis_difficulty_increment_divisor: u64,
	/// Block duration.
	pub duration_limit: u64,
	/// Homestead transition block number.
	pub homestead_transition: u64,
	/// Transition block for a change of difficulty params (currently just bound_divisor).
	pub difficulty_hardfork_transition: u64,
	/// Difficulty param after the difficulty transition.
	pub difficulty_hardfork_bound_divisor: U256,
	/// Block on which there is no additional difficulty from the exponential bomb.
	pub bomb_defuse_transition: u64,
	/// Number of first block where EIP-100 rules begin.
	pub eip100b_transition: u64,
	/// Number of first block where ECIP-1010 begins.
	pub ecip1010_pause_transition: u64,
	/// Number of first block where ECIP-1010 ends.
	pub ecip1010_continue_transition: u64,
	/// Total block number for one ECIP-1017 era.
	pub ecip1017_era_rounds: u64,
	/// Block reward in base units.
	pub block_reward: BTreeMap<BlockNumber, U256>,
	/// EXPIP-2 block height
	pub expip2_transition: u64,
	/// EXPIP-2 duration limit
	pub expip2_duration_limit: u64,
	/// Block reward contract transition block.
	pub block_reward_contract_transition: u64,
	/// Block reward contract.
	pub block_reward_contract: Option<BlockRewardContract>,
	/// Difficulty bomb delays.
	pub difficulty_bomb_delays: BTreeMap<BlockNumber, BlockNumber>,
	/// Block to transition to progpow
	pub progpow_transition: u64,
}

impl From<vapjson::spec::VapashParams> for VapashParams {
	fn from(p: vapjson::spec::VapashParams) -> Self {
		VapashParams {
			minimum_difficulty: p.minimum_difficulty.into(),
			difficulty_bound_divisor: p.difficulty_bound_divisor.into(),
			difficulty_increment_divisor: p.difficulty_increment_divisor.map_or(10, Into::into),
			metropolis_difficulty_increment_divisor: p.metropolis_difficulty_increment_divisor.map_or(9, Into::into),
			duration_limit: p.duration_limit.map_or(0, Into::into),
			homestead_transition: p.homestead_transition.map_or(0, Into::into),
			difficulty_hardfork_transition: p.difficulty_hardfork_transition.map_or(u64::max_value(), Into::into),
			difficulty_hardfork_bound_divisor: p.difficulty_hardfork_bound_divisor.map_or(p.difficulty_bound_divisor.into(), Into::into),
			bomb_defuse_transition: p.bomb_defuse_transition.map_or(u64::max_value(), Into::into),
			eip100b_transition: p.eip100b_transition.map_or(u64::max_value(), Into::into),
			ecip1010_pause_transition: p.ecip1010_pause_transition.map_or(u64::max_value(), Into::into),
			ecip1010_continue_transition: p.ecip1010_continue_transition.map_or(u64::max_value(), Into::into),
			ecip1017_era_rounds: p.ecip1017_era_rounds.map_or(u64::max_value(), Into::into),
			block_reward: p.block_reward.map_or_else(
				|| {
					let mut ret = BTreeMap::new();
					ret.insert(0, U256::zero());
					ret
				},
				|reward| {
					match reward {
						vapjson::spec::BlockReward::Single(reward) => {
							let mut ret = BTreeMap::new();
							ret.insert(0, reward.into());
							ret
						},
						vapjson::spec::BlockReward::Multi(multi) => {
							multi.into_iter()
								.map(|(block, reward)| (block.into(), reward.into()))
								.collect()
						},
					}
				}),
			expip2_transition: p.expip2_transition.map_or(u64::max_value(), Into::into),
			expip2_duration_limit: p.expip2_duration_limit.map_or(30, Into::into),
			progpow_transition: p.progpow_transition.map_or(u64::max_value(), Into::into),
			block_reward_contract_transition: p.block_reward_contract_transition.map_or(0, Into::into),
			block_reward_contract: match (p.block_reward_contract_code, p.block_reward_contract_address) {
				(Some(code), _) => Some(BlockRewardContract::new_from_code(Arc::new(code.into()))),
				(_, Some(address)) => Some(BlockRewardContract::new_from_address(address.into())),
				(None, None) => None,
			},
			difficulty_bomb_delays: p.difficulty_bomb_delays.unwrap_or_default().into_iter()
				.map(|(block, delay)| (block.into(), delay.into()))
				.collect()
		}
	}
}

/// Engine using Vapash proof-of-work consensus algorithm, suitable for Vapory
/// mainnet chains in the Olympic, Frontier and Homestead eras.
pub struct Vapash {
	vapash_params: VapashParams,
	pow: Arc<VapashManager>,
	mashina: Machine,
}

impl Vapash {
	/// Create a new instance of Vapash engine
	pub fn new<T: Into<Option<OptimizeFor>>>(
		cache_dir: &Path,
		vapash_params: VapashParams,
		mashina: Machine,
		optimize_for: T,
	) -> Self {
		let progpow_transition = vapash_params.progpow_transition;

		Vapash {
			vapash_params,
			mashina,
			pow: Arc::new(VapashManager::new(
				cache_dir.as_ref(),
				optimize_for.into(),
				progpow_transition
			)),
		}
	}
}

fn verify_block_unordered(pow: &Arc<VapashManager>, header: &Header) -> Result<(), Error> {
	let seal = VapashSeal::parse_seal(header.seal())?;

	let result = pow.compute_light(
		header.number() as u64,
		&header.bare_hash().0,
		seal.nonce.to_low_u64_be()
	);
	let mix = H256(result.mix_hash);
	let difficulty = vapash::boundary_to_difficulty(&H256(result.value));
	trace!(target: "miner", "num: {num}, seed: {seed}, h: {h}, non: {non}, mix: {mix}, res: {res}",
	       num = header.number() as u64,
	       seed = H256(slow_hash_block_number(header.number() as u64)),
	       h = header.bare_hash(),
	       non = seal.nonce.to_low_u64_be(),
	       mix = H256(result.mix_hash),
	       res = H256(result.value));
	if mix != seal.mix_hash {
		return Err(From::from(BlockError::MismatchedH256SealElement(Mismatch { expected: mix, found: seal.mix_hash })));
	}
	if &difficulty < header.difficulty() {
		return Err(From::from(BlockError::InvalidProofOfWork(OutOfBounds { min: Some(header.difficulty().clone()), max: None, found: difficulty })));
	}
	Ok(())
}


// TODO [rphmeier]
//
// for now, this is different than Vapash's own epochs, and signal
// "consensus epochs".
// in this sense, `Vapash` is epochless: the same `EpochVerifier` can be used
// for any block in the chain.
// in the future, we might move the Vapash epoch
// caching onto this mechanism as well.
struct EpochVerifier {
	pow: Arc<VapashManager>
}

impl enjen::EpochVerifier for EpochVerifier {
	fn verify_heavy(&self, header: &Header) -> Result<(), Error> {
		verify_block_unordered(&self.pow, header)
	}
}

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

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

	// Two fields - nonce and mix.
	fn seal_fields(&self, _header: &Header) -> usize { 2 }

	/// Additional engine-specific information for the user/developer concerning `header`.
	fn extra_info(&self, header: &Header) -> BTreeMap<String, String> {
		match VapashSeal::parse_seal(header.seal()) {
			Ok(seal) => map![
				"nonce".to_owned() => format!("0x{:x}", seal.nonce),
				"mixHash".to_owned() => format!("0x{:x}", seal.mix_hash)
			],
			_ => BTreeMap::default()
		}
	}

	fn maximum_uncle_count(&self, _block: BlockNumber) -> usize { 2 }

	fn maximum_gas_limit(&self) -> Option<U256> { Some(0x7fff_ffff_ffff_ffffu64.into()) }

	/// Apply the block reward on finalisation of the block.
	/// This assumes that all uncles are valid uncles (i.e. of at least one generation before the current).
	fn on_close_block(&self, block: &mut ExecutedBlock, _parent_header: &Header) -> Result<(), Error> {
		use std::ops::Shr;

		let author = *block.header.author();
		let number = block.header.number();

		let rewards = match self.vapash_params.block_reward_contract {
			Some(ref c) if number >= self.vapash_params.block_reward_contract_transition => {
				let mut beneficiaries = Vec::new();

				beneficiaries.push((author, RewardKind::Author));
				for u in &block.uncles {
					let uncle_author = u.author();
					beneficiaries.push((*uncle_author, RewardKind::uncle(number, u.number())));
				}

				let mut call = enjen::default_system_or_code_call(&self.mashina, block);

				let rewards = c.reward(beneficiaries, &mut call)?;
				rewards.into_iter().map(|(author, amount)| (author, RewardKind::External, amount)).collect()
			},
			_ => {
				let mut rewards = Vec::new();

				let (_, reward) = self.vapash_params.block_reward.iter()
					.rev()
					.find(|&(block, _)| *block <= number)
					.expect("Current block's reward is not found; this indicates a chain config error; qed");
				let reward = *reward;

				// Applies ECIP-1017 eras.
				let eras_rounds = self.vapash_params.ecip1017_era_rounds;
				let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, reward, number);

				//let n_uncles = LiveBlock::uncles(&*block).len();
				let n_uncles = block.uncles.len();

				// Bestow block rewards.
				let result_block_reward = reward + reward.shr(5) * U256::from(n_uncles);

				rewards.push((author, RewardKind::Author, result_block_reward));

				// Bestow uncle rewards.
				for u in &block.uncles {
					let uncle_author = u.author();
					let result_uncle_reward = if eras == 0 {
						(reward * U256::from(8 + u.number() - number)).shr(3)
					} else {
						reward.shr(5)
					};

					rewards.push((*uncle_author, RewardKind::uncle(number, u.number()), result_uncle_reward));
				}

				rewards
			},
		};

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

	#[cfg(not(feature = "miner-debug"))]
	fn verify_local_seal(&self, header: &Header) -> Result<(), Error> {
		self.verify_block_basic(header)
			.and_then(|_| self.verify_block_unordered(header))
	}

	#[cfg(feature = "miner-debug")]
	fn verify_local_seal(&self, _header: &Header) -> Result<(), Error> {
		warn!("Skipping seal verification, running in miner testing mode.");
		Ok(())
	}

	fn verify_block_basic(&self, header: &Header) -> Result<(), Error> {
		// check the seal fields.
		let seal = VapashSeal::parse_seal(header.seal())?;

		// TODO: consider removing these lines.
		let min_difficulty = self.vapash_params.minimum_difficulty;
		if header.difficulty() < &min_difficulty {
			return Err(From::from(BlockError::DifficultyOutOfBounds(OutOfBounds { min: Some(min_difficulty), max: None, found: header.difficulty().clone() })))
		}

		let difficulty = vapash::boundary_to_difficulty(&H256(quick_get_difficulty(
			&header.bare_hash().0,
			seal.nonce.to_low_u64_be(),
			&seal.mix_hash.0,
			header.number() >= self.vapash_params.progpow_transition
		)));

		if &difficulty < header.difficulty() {
			return Err(From::from(BlockError::InvalidProofOfWork(OutOfBounds { min: Some(header.difficulty().clone()), max: None, found: difficulty })));
		}

		Ok(())
	}

	fn verify_block_unordered(&self, header: &Header) -> Result<(), Error> {
		verify_block_unordered(&self.pow, header)
	}

	fn verify_block_family(&self, header: &Header, parent: &Header) -> Result<(), Error> {
		// we should not calculate difficulty for genesis blocks
		if header.number() == 0 {
			return Err(From::from(BlockError::RidiculousNumber(OutOfBounds { min: Some(1), max: None, found: header.number() })));
		}

		// Check difficulty is correct given the two timestamps.
		let expected_difficulty = self.calculate_difficulty(header, parent);
		if header.difficulty() != &expected_difficulty {
			return Err(From::from(BlockError::InvalidDifficulty(Mismatch { expected: expected_difficulty, found: header.difficulty().clone() })))
		}

		Ok(())
	}

	fn epoch_verifier<'a>(&self, _header: &Header, _proof: &'a [u8]) -> enjen::ConstructedVerifier<'a> {
		let v = EpochVerifier{pow: self.pow.clone()};
		enjen::ConstructedVerifier::Trusted(Box::new(v))
	}

	fn populate_from_parent(&self, header: &mut Header, parent: &Header) {
		let difficulty = self.calculate_difficulty(header, parent);
		header.set_difficulty(difficulty);
	}

	fn snapshot_mode(&self) -> Snapshotting {
		Snapshotting::PoW {
			blocks: SNAPSHOT_BLOCKS,
			max_restore_blocks: MAX_SNAPSHOT_BLOCKS
		}
	}

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

impl Vapash {
	fn calculate_difficulty(&self, header: &Header, parent: &Header) -> U256 {
		const EXP_DIFF_PERIOD: u64 = 100_000;
		if header.number() == 0 {
			panic!("Can't calculate genesis block difficulty");
		}

		let parent_has_uncles = parent.uncles_hash() != &KECCAK_EMPTY_LIST_RLP;

		let min_difficulty = self.vapash_params.minimum_difficulty;

		let difficulty_hardfork = header.number() >= self.vapash_params.difficulty_hardfork_transition;
		let difficulty_bound_divisor = if difficulty_hardfork {
			self.vapash_params.difficulty_hardfork_bound_divisor
		} else {
			self.vapash_params.difficulty_bound_divisor
		};

		let expip2_hardfork = header.number() >= self.vapash_params.expip2_transition;
		let duration_limit = if expip2_hardfork {
			self.vapash_params.expip2_duration_limit
		} else {
			self.vapash_params.duration_limit
		};

		let frontier_limit = self.vapash_params.homestead_transition;

		let mut target = if header.number() < frontier_limit {
			if header.timestamp() >= parent.timestamp() + duration_limit {
				*parent.difficulty() - (*parent.difficulty() / difficulty_bound_divisor)
			} else {
				*parent.difficulty() + (*parent.difficulty() / difficulty_bound_divisor)
			}
		} else {
			trace!(target: "vapash", "Calculating difficulty parent.difficulty={}, header.timestamp={}, parent.timestamp={}", parent.difficulty(), header.timestamp(), parent.timestamp());
			//block_diff = parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99)
			let (increment_divisor, threshold) = if header.number() < self.vapash_params.eip100b_transition {
				(self.vapash_params.difficulty_increment_divisor, 1)
			} else if parent_has_uncles {
				(self.vapash_params.metropolis_difficulty_increment_divisor, 2)
			} else {
				(self.vapash_params.metropolis_difficulty_increment_divisor, 1)
			};

			let diff_inc = (header.timestamp() - parent.timestamp()) / increment_divisor;
			if diff_inc <= threshold {
				*parent.difficulty() + *parent.difficulty() / difficulty_bound_divisor * U256::from(threshold - diff_inc)
			} else {
				let multiplier: U256 = cmp::min(diff_inc - threshold, 99).into();
				parent.difficulty().saturating_sub(
					*parent.difficulty() / difficulty_bound_divisor * multiplier
				)
			}
		};
		target = cmp::max(min_difficulty, target);
		if header.number() < self.vapash_params.bomb_defuse_transition {
			if header.number() < self.vapash_params.ecip1010_pause_transition {
				let mut number = header.number();
				let original_number = number;
				for (block, delay) in &self.vapash_params.difficulty_bomb_delays {
					if original_number >= *block {
						number = number.saturating_sub(*delay);
					}
				}
				let period = (number / EXP_DIFF_PERIOD) as usize;
				if period > 1 {
					target = cmp::max(min_difficulty, target + (U256::from(1) << (period - 2)));
				}
			} else if header.number() < self.vapash_params.ecip1010_continue_transition {
				let fixed_difficulty = ((self.vapash_params.ecip1010_pause_transition / EXP_DIFF_PERIOD) - 2) as usize;
				target = cmp::max(min_difficulty, target + (U256::from(1) << fixed_difficulty));
			} else {
				let period = ((parent.number() + 1) / EXP_DIFF_PERIOD) as usize;
				let delay = ((self.vapash_params.ecip1010_continue_transition - self.vapash_params.ecip1010_pause_transition) / EXP_DIFF_PERIOD) as usize;
				target = cmp::max(min_difficulty, target + (U256::from(1) << (period - delay - 2)));
			}
		}
		target
	}
}


/// Calculates the number of eras and reward
///
/// # Panics
///
/// This function will panic if `era_rounds` is less than `2`
fn ecip1017_eras_block_reward(era_rounds: u64, mut reward: U256, block_number: u64) -> (u64, U256) {
	// NOTE(niklasad1): all numbers is divisible by 1, it will cause the if below
	// to succeed except for the first block. Thus, `era_rounds - 1 == 0` and cause `divide by zero`
	assert!(era_rounds > 1, "ecip1017EraRounds must be bigger than 1");
	let eras = if block_number != 0 && block_number % era_rounds == 0 {
		block_number / era_rounds - 1
	} else {
		block_number / era_rounds
	};
	let mut divi = U256::from(1);
	for _ in 0..eras {
		reward = reward * U256::from(4);
		divi = divi * U256::from(5);
	}
	reward = reward / divi;
	(eras, reward)
}

#[cfg(test)]
mod tests {
	use std::str::FromStr;
	use std::sync::Arc;
	use std::collections::BTreeMap;

	use common_types::{
		header::Header,
		errors::{BlockError, VapcoreError as Error}
	};
	use engine::Engine;
	use vapory_types::{H64, H256, U256, Address};
	use vapcore::{
		block::*,
		test_helpers::get_temp_state_db,
	};
	use rlp;
	use spec::{new_morden, new_mcip3_test, new_homestead_test_mashina, Spec};
	use tempdir::TempDir;

	use super::{Vapash, VapashParams, ecip1017_eras_block_reward};

	fn test_spec() -> Spec {
		let tempdir = TempDir::new("").unwrap();
		new_morden(&tempdir.path())
	}

	fn get_default_vapash_params() -> VapashParams {
		VapashParams {
			minimum_difficulty: U256::from(131072),
			difficulty_bound_divisor: U256::from(2048),
			difficulty_increment_divisor: 10,
			metropolis_difficulty_increment_divisor: 9,
			homestead_transition: 1150000,
			duration_limit: 13,
			block_reward: {
				let mut ret = BTreeMap::new();
				ret.insert(0, 0.into());
				ret
			},
			difficulty_hardfork_transition: u64::max_value(),
			difficulty_hardfork_bound_divisor: U256::from(0),
			bomb_defuse_transition: u64::max_value(),
			eip100b_transition: u64::max_value(),
			ecip1010_pause_transition: u64::max_value(),
			ecip1010_continue_transition: u64::max_value(),
			ecip1017_era_rounds: u64::max_value(),
			expip2_transition: u64::max_value(),
			expip2_duration_limit: 30,
			block_reward_contract: None,
			block_reward_contract_transition: 0,
			difficulty_bomb_delays: BTreeMap::new(),
			progpow_transition: u64::max_value(),
		}
	}

	#[test]
	fn on_close_block() {
		let spec = test_spec();
		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let last_hashes = Arc::new(vec![genesis_header.hash()]);
		let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b = b.close().unwrap();
		assert_eq!(b.state.balance(&Address::zero()).unwrap(), U256::from_str("4563918244f40000").unwrap());
	}

	#[test]
	fn has_valid_ecip1017_eras_block_reward() {
		let eras_rounds = 5000000;

		let start_reward: U256 = "4563918244F40000".parse().unwrap();

		let block_number = 0;
		let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
		assert_eq!(0, eras);
		assert_eq!(U256::from_str("4563918244F40000").unwrap(), reward);

		let block_number = 5000000;
		let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
		assert_eq!(0, eras);
		assert_eq!(U256::from_str("4563918244F40000").unwrap(), reward);

		let block_number = 10000000;
		let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
		assert_eq!(1, eras);
		assert_eq!(U256::from_str("3782DACE9D900000").unwrap(), reward);

		let block_number = 20000000;
		let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
		assert_eq!(3, eras);
		assert_eq!(U256::from_str("2386F26FC1000000").unwrap(), reward);

		let block_number = 80000000;
		let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
		assert_eq!(15, eras);
		assert_eq!(U256::from_str("271000000000000").unwrap(), reward);

		let block_number = 250000000;
		let (eras, reward) = ecip1017_eras_block_reward(eras_rounds, start_reward, block_number);
		assert_eq!(49, eras);
		assert_eq!(U256::from_str("51212FFBAF0A").unwrap(), reward);
	}

	#[test]
	fn on_close_block_with_uncle() {
		let spec = test_spec();
		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let last_hashes = Arc::new(vec![genesis_header.hash()]);
		let mut b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let mut uncle = Header::new();
		let uncle_author = Address::from_str("ef2d6d194084c2de36e0dabfce45d046b37d1106").unwrap();
		uncle.set_author(uncle_author);
		b.push_uncle(uncle).unwrap();

		let b = b.close().unwrap();
		assert_eq!(b.state.balance(&Address::zero()).unwrap(), "478eae0e571ba000".into());
		assert_eq!(b.state.balance(&uncle_author).unwrap(), "3cb71f51fc558000".into());
	}

	#[test]
	fn has_valid_mcip3_era_block_rewards() {
		let spec = new_mcip3_test();
		let engine = &*spec.engine;
		let genesis_header = spec.genesis_header();
		let db = spec.ensure_db_good(get_temp_state_db(), &Default::default()).unwrap();
		let last_hashes = Arc::new(vec![genesis_header.hash()]);
		let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::zero(), (3141562.into(), 31415620.into()), vec![], false).unwrap();
		let b = b.close().unwrap();

		let ubi_contract = Address::from_str("00efdd5883ec628983e9063c7d969fe268bbf310").unwrap();
		let dev_contract = Address::from_str("00756cf8159095948496617f5fb17ed95059f536").unwrap();
		assert_eq!(b.state.balance(&Address::zero()).unwrap(), U256::from_str("d8d726b7177a80000").unwrap());
		assert_eq!(b.state.balance(&ubi_contract).unwrap(), U256::from_str("2b5e3af16b1880000").unwrap());
		assert_eq!(b.state.balance(&dev_contract).unwrap(), U256::from_str("c249fdd327780000").unwrap());
	}

	#[test]
	fn has_valid_metadata() {
		let engine = test_spec().engine;
		assert_eq!(engine.name(), "Vapash");
	}

	#[test]
	fn can_return_schedule() {
		let engine = test_spec().engine;
		let schedule = engine.schedule(10000000);
		assert!(schedule.stack_limit > 0);

		let schedule = engine.schedule(100);
		assert!(!schedule.have_delegate_call);
	}

	#[test]
	fn can_do_seal_verification_fail() {
		let engine = test_spec().engine;
		let header: Header = Header::default();

		let verify_result = engine.verify_block_basic(&header);

		match verify_result {
			Err(Error::Block(BlockError::InvalidSealArity(_))) => {},
			Err(_) => { panic!("should be block seal-arity mismatch error (got {:?})", verify_result); },
			_ => { panic!("Should be error, got Ok"); },
		}
	}

	#[test]
	fn can_do_difficulty_verification_fail() {
		let engine = test_spec().engine;
		let mut header: Header = Header::default();
		header.set_seal(vec![tetsy_rlp::encode(&H256::zero()), tetsy_rlp::encode(&H64::zero())]);

		let verify_result = engine.verify_block_basic(&header);

		match verify_result {
			Err(Error::Block(BlockError::DifficultyOutOfBounds(_))) => {},
			Err(_) => { panic!("should be block difficulty error (got {:?})", verify_result); },
			_ => { panic!("Should be error, got Ok"); },
		}
	}

	#[test]
	fn can_do_proof_of_work_verification_fail() {
		let engine = test_spec().engine;
		let mut header: Header = Header::default();
		header.set_seal(vec![tetsy_rlp::encode(&H256::zero()), tetsy_rlp::encode(&H64::zero())]);
		header.set_difficulty(U256::from_str("ffffffffffffffffffffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaa").unwrap());

		let verify_result = engine.verify_block_basic(&header);

		match verify_result {
			Err(Error::Block(BlockError::InvalidProofOfWork(_))) => {},
			Err(_) => { panic!("should be invalid proof of work error (got {:?})", verify_result); },
			_ => { panic!("Should be error, got Ok"); },
		}
	}

	#[test]
	fn can_do_seal_unordered_verification_fail() {
		let engine = test_spec().engine;
		let header = Header::default();

		let verify_result = engine.verify_block_unordered(&header);

		match verify_result {
			Err(Error::Block(BlockError::InvalidSealArity(_))) => {},
			Err(_) => { panic!("should be block seal-arity mismatch error (got {:?})", verify_result); },
			_ => { panic!("Should be error, got Ok"); },
		}
	}

	#[test]
	fn can_do_seal_unordered_verification_fail2() {
		let engine = test_spec().engine;
		let mut header = Header::default();
		header.set_seal(vec![vec![], vec![]]);

		let verify_result = engine.verify_block_unordered(&header);
		// rlp error, shouldn't panic
		assert!(verify_result.is_err());
	}

	#[test]
	fn can_do_seal256_verification_fail() {
		let engine = test_spec().engine;
		let mut header: Header = Header::default();
		header.set_seal(vec![tetsy_rlp::encode(&H256::zero()), tetsy_rlp::encode(&H64::zero())]);
		let verify_result = engine.verify_block_unordered(&header);

		match verify_result {
			Err(Error::Block(BlockError::MismatchedH256SealElement(_))) => {},
			Err(_) => { panic!("should be invalid 256-bit seal fail (got {:?})", verify_result); },
			_ => { panic!("Should be error, got Ok"); },
		}
	}

	#[test]
	fn can_do_proof_of_work_unordered_verification_fail() {
		let engine = test_spec().engine;
		let mut header: Header = Header::default();
		header.set_seal(vec![tetsy_rlp::encode(&H256::from_str("b251bd2e0283d0658f2cadfdc8ca619b5de94eca5742725e2e757dd13ed7503d").unwrap()), tetsy_rlp::encode(&H64::zero())]);
		header.set_difficulty(U256::from_str("ffffffffffffffffffffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaa").unwrap());

		let verify_result = engine.verify_block_unordered(&header);

		match verify_result {
			Err(Error::Block(BlockError::InvalidProofOfWork(_))) => {},
			Err(_) => { panic!("should be invalid proof-of-work fail (got {:?})", verify_result); },
			_ => { panic!("Should be error, got Ok"); },
		}
	}

	#[test]
	fn can_verify_block_family_genesis_fail() {
		let engine = test_spec().engine;
		let header: Header = Header::default();
		let parent_header: Header = Header::default();

		let verify_result = engine.verify_block_family(&header, &parent_header);

		match verify_result {
			Err(Error::Block(BlockError::RidiculousNumber(_))) => {},
			Err(_) => { panic!("should be invalid block number fail (got {:?})", verify_result); },
			_ => { panic!("Should be error, got Ok"); },
		}
	}

	#[test]
	fn can_verify_block_family_difficulty_fail() {
		let engine = test_spec().engine;
		let mut header: Header = Header::default();
		header.set_number(2);
		let mut parent_header: Header = Header::default();
		parent_header.set_number(1);

		let verify_result = engine.verify_block_family(&header, &parent_header);

		match verify_result {
			Err(Error::Block(BlockError::InvalidDifficulty(_))) => {},
			Err(_) => { panic!("should be invalid difficulty fail (got {:?})", verify_result); },
			_ => { panic!("Should be error, got Ok"); },
		}
	}

	#[test]
	fn difficulty_frontier() {
		let mashina = new_homestead_test_mashina();
		let vapparams = get_default_vapash_params();
		let tempdir = TempDir::new("").unwrap();
		let vapash = Vapash::new(tempdir.path(), vapparams, mashina, None);

		let mut parent_header = Header::default();
		parent_header.set_number(1000000);
		parent_header.set_difficulty(U256::from_str("b69de81a22b").unwrap());
		parent_header.set_timestamp(1455404053);
		let mut header = Header::default();
		header.set_number(parent_header.number() + 1);
		header.set_timestamp(1455404058);

		let difficulty = vapash.calculate_difficulty(&header, &parent_header);
		assert_eq!(U256::from_str("b6b4bbd735f").unwrap(), difficulty);
	}

	#[test]
	fn difficulty_homestead() {
		let mashina = new_homestead_test_mashina();
		let vapparams = get_default_vapash_params();
		let tempdir = TempDir::new("").unwrap();
		let vapash = Vapash::new(tempdir.path(), vapparams, mashina, None);

		let mut parent_header = Header::default();
		parent_header.set_number(1500000);
		parent_header.set_difficulty(U256::from_str("1fd0fd70792b").unwrap());
		parent_header.set_timestamp(1463003133);
		let mut header = Header::default();
		header.set_number(parent_header.number() + 1);
		header.set_timestamp(1463003177);

		let difficulty = vapash.calculate_difficulty(&header, &parent_header);
		assert_eq!(U256::from_str("1fc50f118efe").unwrap(), difficulty);
	}

	#[test]
	fn difficulty_classic_bomb_delay() {
		let mashina = new_homestead_test_mashina();
		let vapparams = VapashParams {
			ecip1010_pause_transition: 3000000,
			..get_default_vapash_params()
		};
		let tempdir = TempDir::new("").unwrap();
		let vapash = Vapash::new(tempdir.path(), vapparams, mashina, None);

		let mut parent_header = Header::default();
		parent_header.set_number(3500000);
		parent_header.set_difficulty(U256::from_str("6F62EAF8D3C").unwrap());
		parent_header.set_timestamp(1452838500);
		let mut header = Header::default();
		header.set_number(parent_header.number() + 1);

		header.set_timestamp(parent_header.timestamp() + 20);
		assert_eq!(
			U256::from_str("6F55FE9B74B").unwrap(),
			vapash.calculate_difficulty(&header, &parent_header)
		);
		header.set_timestamp(parent_header.timestamp() + 5);
		assert_eq!(
			U256::from_str("6F71D75632D").unwrap(),
			vapash.calculate_difficulty(&header, &parent_header)
		);
		header.set_timestamp(parent_header.timestamp() + 80);
		assert_eq!(
			U256::from_str("6F02746B3A5").unwrap(),
			vapash.calculate_difficulty(&header, &parent_header)
		);
	}

	#[test]
	fn test_difficulty_bomb_continue() {
		let mashina = new_homestead_test_mashina();
		let vapparams = VapashParams {
			ecip1010_pause_transition: 3000000,
			ecip1010_continue_transition: 5000000,
			..get_default_vapash_params()
		};
		let tempdir = TempDir::new("").unwrap();
		let vapash = Vapash::new(tempdir.path(), vapparams, mashina, None);

		let mut parent_header = Header::default();
		parent_header.set_number(5000102);
		parent_header.set_difficulty(U256::from_str("14944397EE8B").unwrap());
		parent_header.set_timestamp(1513175023);
		let mut header = Header::default();
		header.set_number(parent_header.number() + 1);
		header.set_timestamp(parent_header.timestamp() + 6);
		assert_eq!(
			U256::from_str("1496E6206188").unwrap(),
			vapash.calculate_difficulty(&header, &parent_header)
		);
		parent_header.set_number(5100123);
		parent_header.set_difficulty(U256::from_str("14D24B39C7CF").unwrap());
		parent_header.set_timestamp(1514609324);
		header.set_number(parent_header.number() + 1);
		header.set_timestamp(parent_header.timestamp() + 41);
		assert_eq!(
			U256::from_str("14CA9C5D9227").unwrap(),
			vapash.calculate_difficulty(&header, &parent_header)
		);
		parent_header.set_number(6150001);
		parent_header.set_difficulty(U256::from_str("305367B57227").unwrap());
		parent_header.set_timestamp(1529664575);
		header.set_number(parent_header.number() + 1);
		header.set_timestamp(parent_header.timestamp() + 105);
		assert_eq!(
			U256::from_str("309D09E0C609").unwrap(),
			vapash.calculate_difficulty(&header, &parent_header)
		);
		parent_header.set_number(8000000);
		parent_header.set_difficulty(U256::from_str("1180B36D4CE5B6A").unwrap());
		parent_header.set_timestamp(1535431724);
		header.set_number(parent_header.number() + 1);
		header.set_timestamp(parent_header.timestamp() + 420);
		assert_eq!(
			U256::from_str("5126FFD5BCBB9E7").unwrap(),
			vapash.calculate_difficulty(&header, &parent_header)
		);
	}

	#[test]
	fn difficulty_max_timestamp() {
		let mashina = new_homestead_test_mashina();
		let vapparams = get_default_vapash_params();
		let tempdir = TempDir::new("").unwrap();
		let vapash = Vapash::new(tempdir.path(), vapparams, mashina, None);

		let mut parent_header = Header::default();
		parent_header.set_number(1000000);
		parent_header.set_difficulty(U256::from_str("b69de81a22b").unwrap());
		parent_header.set_timestamp(1455404053);
		let mut header = Header::default();
		header.set_number(parent_header.number() + 1);
		header.set_timestamp(u64::max_value());

		let difficulty = vapash.calculate_difficulty(&header, &parent_header);
		assert_eq!(U256::from(12543204905719u64), difficulty);
	}

	#[test]
	fn test_extra_info() {
		let mashina = new_homestead_test_mashina();
		let vapparams = get_default_vapash_params();
		let tempdir = TempDir::new("").unwrap();
		let vapash = Vapash::new(tempdir.path(), vapparams, mashina, None);
		let mut header = Header::default();
		header.set_seal(vec![tetsy_rlp::encode(&H256::from_str("b251bd2e0283d0658f2cadfdc8ca619b5de94eca5742725e2e757dd13ed7503d").unwrap()), tetsy_rlp::encode(&H64::zero())]);
		let info = vapash.extra_info(&header);
		assert_eq!(info["nonce"], "0x0000000000000000");
		assert_eq!(info["mixHash"], "0xb251bd2e0283d0658f2cadfdc8ca619b5de94eca5742725e2e757dd13ed7503d");
	}
}