2018-01-27 10:48:53 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! core consensus.rs tests (separated to de-clutter consensus.rs)
|
2018-12-08 02:59:40 +03:00
|
|
|
use grin_core as core;
|
2018-01-27 10:48:53 +03:00
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::core::consensus::*;
|
2019-05-08 23:10:42 +03:00
|
|
|
use self::core::core::block::HeaderVersion;
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::core::global;
|
|
|
|
use self::core::pow::Difficulty;
|
2018-09-03 14:09:28 +03:00
|
|
|
use chrono::prelude::Utc;
|
|
|
|
use std::fmt::{self, Display};
|
2018-01-27 10:48:53 +03:00
|
|
|
|
2018-07-05 15:18:09 +03:00
|
|
|
/// Last n blocks for difficulty calculation purposes
|
|
|
|
/// (copied from stats in server crate)
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct DiffBlock {
|
|
|
|
/// Block number (can be negative for a new chain)
|
|
|
|
pub block_number: i64,
|
|
|
|
/// Block network difficulty
|
|
|
|
pub difficulty: u64,
|
|
|
|
/// Time block was found (epoch seconds)
|
|
|
|
pub time: u64,
|
|
|
|
/// Duration since previous block (epoch seconds)
|
|
|
|
pub duration: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stats on the last WINDOW blocks and the difficulty calculation
|
|
|
|
/// (Copied from stats in server crate)
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DiffStats {
|
|
|
|
/// latest height
|
|
|
|
pub height: u64,
|
|
|
|
/// Last WINDOW block data
|
|
|
|
pub last_blocks: Vec<DiffBlock>,
|
|
|
|
/// Average block time for last WINDOW blocks
|
|
|
|
pub average_block_time: u64,
|
|
|
|
/// Average WINDOW difficulty
|
|
|
|
pub average_difficulty: u64,
|
|
|
|
/// WINDOW size
|
|
|
|
pub window_size: u64,
|
|
|
|
/// Block time sum
|
|
|
|
pub block_time_sum: u64,
|
|
|
|
/// Block diff sum
|
|
|
|
pub block_diff_sum: u64,
|
|
|
|
/// latest ts
|
|
|
|
pub latest_ts: u64,
|
2018-09-03 14:09:28 +03:00
|
|
|
/// earliest ts
|
2018-07-05 15:18:09 +03:00
|
|
|
pub earliest_ts: u64,
|
|
|
|
/// ts delta
|
|
|
|
pub ts_delta: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for DiffBlock {
|
2018-12-08 02:59:40 +03:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-07-05 15:18:09 +03:00
|
|
|
let output = format!(
|
|
|
|
"Block Number: {} Difficulty: {}, Time: {}, Duration: {}",
|
|
|
|
self.block_number, self.difficulty, self.time, self.duration
|
|
|
|
);
|
|
|
|
Display::fmt(&output, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// Builds an iterator for next difficulty calculation with the provided
|
|
|
|
// constant time interval, difficulty and total length.
|
2018-10-13 23:57:01 +03:00
|
|
|
fn repeat(interval: u64, diff: HeaderInfo, len: u64, cur_time: Option<u64>) -> Vec<HeaderInfo> {
|
2018-01-27 10:48:53 +03:00
|
|
|
let cur_time = match cur_time {
|
|
|
|
Some(t) => t,
|
2018-07-30 11:33:28 +03:00
|
|
|
None => Utc::now().timestamp() as u64,
|
2018-01-27 10:48:53 +03:00
|
|
|
};
|
|
|
|
// watch overflow here, length shouldn't be ridiculous anyhow
|
|
|
|
assert!(len < std::usize::MAX as u64);
|
2018-10-13 23:57:01 +03:00
|
|
|
let diffs = vec![diff.difficulty.clone(); len as usize];
|
2018-01-27 10:48:53 +03:00
|
|
|
let times = (0..(len as usize)).map(|n| n * interval as usize).rev();
|
|
|
|
let pairs = times.zip(diffs.iter());
|
|
|
|
pairs
|
2018-10-13 23:57:01 +03:00
|
|
|
.map(|(t, d)| {
|
|
|
|
HeaderInfo::new(
|
|
|
|
cur_time + t as u64,
|
|
|
|
d.clone(),
|
|
|
|
diff.secondary_scaling,
|
|
|
|
diff.is_secondary,
|
|
|
|
)
|
2018-12-08 02:59:40 +03:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new chain with a genesis at a simulated difficulty
|
2018-10-13 23:57:01 +03:00
|
|
|
fn create_chain_sim(diff: u64) -> Vec<(HeaderInfo, DiffStats)> {
|
2018-03-28 00:11:52 +03:00
|
|
|
println!(
|
|
|
|
"adding create: {}, {}",
|
2018-07-30 11:33:28 +03:00
|
|
|
Utc::now().timestamp(),
|
2018-03-28 00:11:52 +03:00
|
|
|
Difficulty::from_num(diff)
|
|
|
|
);
|
2018-10-13 23:57:01 +03:00
|
|
|
let return_vec = vec![HeaderInfo::from_ts_diff(
|
2018-09-03 14:09:28 +03:00
|
|
|
Utc::now().timestamp() as u64,
|
|
|
|
Difficulty::from_num(diff),
|
2018-10-13 23:57:01 +03:00
|
|
|
)];
|
2018-07-05 15:18:09 +03:00
|
|
|
let diff_stats = get_diff_stats(&return_vec);
|
2018-09-03 14:09:28 +03:00
|
|
|
vec![(
|
2018-10-13 23:57:01 +03:00
|
|
|
HeaderInfo::from_ts_diff(Utc::now().timestamp() as u64, Difficulty::from_num(diff)),
|
2018-09-03 14:09:28 +03:00
|
|
|
diff_stats,
|
|
|
|
)]
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
|
2018-10-13 23:57:01 +03:00
|
|
|
fn get_diff_stats(chain_sim: &Vec<HeaderInfo>) -> DiffStats {
|
2018-07-05 15:18:09 +03:00
|
|
|
// Fill out some difficulty stats for convenience
|
|
|
|
let diff_iter = chain_sim.clone();
|
2018-10-13 23:57:01 +03:00
|
|
|
let last_blocks: Vec<HeaderInfo> = global::difficulty_data_to_vector(diff_iter.iter().cloned());
|
2018-07-05 15:18:09 +03:00
|
|
|
|
2018-10-13 23:57:01 +03:00
|
|
|
let mut last_time = last_blocks[0].timestamp;
|
2018-07-05 15:18:09 +03:00
|
|
|
let tip_height = chain_sim.len();
|
|
|
|
let earliest_block_height = tip_height as i64 - last_blocks.len() as i64;
|
|
|
|
|
2018-10-17 02:55:40 +03:00
|
|
|
let earliest_ts = last_blocks[0].timestamp;
|
2018-10-18 13:23:22 +03:00
|
|
|
let latest_ts = last_blocks[last_blocks.len() - 1].timestamp;
|
2018-07-05 15:18:09 +03:00
|
|
|
|
|
|
|
let mut i = 1;
|
2018-09-03 14:09:28 +03:00
|
|
|
|
2018-10-13 23:57:01 +03:00
|
|
|
let sum_blocks: Vec<HeaderInfo> = global::difficulty_data_to_vector(diff_iter.iter().cloned())
|
|
|
|
.into_iter()
|
2018-09-03 14:09:28 +03:00
|
|
|
.take(DIFFICULTY_ADJUST_WINDOW as usize)
|
|
|
|
.collect();
|
2018-07-05 15:18:09 +03:00
|
|
|
|
|
|
|
let sum_entries: Vec<DiffBlock> = sum_blocks
|
|
|
|
.iter()
|
|
|
|
//.skip(1)
|
|
|
|
.map(|n| {
|
2018-10-13 23:57:01 +03:00
|
|
|
let dur = n.timestamp - last_time;
|
2018-07-05 15:18:09 +03:00
|
|
|
let height = earliest_block_height + i + 1;
|
|
|
|
i += 1;
|
2018-10-13 23:57:01 +03:00
|
|
|
last_time = n.timestamp;
|
2018-07-05 15:18:09 +03:00
|
|
|
DiffBlock {
|
|
|
|
block_number: height,
|
2018-10-13 23:57:01 +03:00
|
|
|
difficulty: n.difficulty.to_num(),
|
|
|
|
time: n.timestamp,
|
2018-07-05 15:18:09 +03:00
|
|
|
duration: dur,
|
|
|
|
}
|
2018-12-08 02:59:40 +03:00
|
|
|
})
|
|
|
|
.collect();
|
2018-07-05 15:18:09 +03:00
|
|
|
|
|
|
|
let block_time_sum = sum_entries.iter().fold(0, |sum, t| sum + t.duration);
|
|
|
|
let block_diff_sum = sum_entries.iter().fold(0, |sum, d| sum + d.difficulty);
|
|
|
|
|
|
|
|
i = 1;
|
2018-10-13 23:57:01 +03:00
|
|
|
last_time = last_blocks[0].clone().timestamp;
|
2018-07-05 15:18:09 +03:00
|
|
|
|
|
|
|
let diff_entries: Vec<DiffBlock> = last_blocks
|
|
|
|
.iter()
|
|
|
|
.skip(1)
|
|
|
|
.map(|n| {
|
2018-10-13 23:57:01 +03:00
|
|
|
let dur = n.timestamp - last_time;
|
2018-07-05 15:18:09 +03:00
|
|
|
let height = earliest_block_height + i;
|
|
|
|
i += 1;
|
2018-10-13 23:57:01 +03:00
|
|
|
last_time = n.timestamp;
|
2018-07-05 15:18:09 +03:00
|
|
|
DiffBlock {
|
|
|
|
block_number: height,
|
2018-10-13 23:57:01 +03:00
|
|
|
difficulty: n.difficulty.to_num(),
|
|
|
|
time: n.timestamp,
|
2018-07-05 15:18:09 +03:00
|
|
|
duration: dur,
|
|
|
|
}
|
2018-12-08 02:59:40 +03:00
|
|
|
})
|
|
|
|
.collect();
|
2018-07-05 15:18:09 +03:00
|
|
|
|
|
|
|
DiffStats {
|
|
|
|
height: tip_height as u64,
|
|
|
|
last_blocks: diff_entries,
|
|
|
|
average_block_time: block_time_sum / (DIFFICULTY_ADJUST_WINDOW),
|
|
|
|
average_difficulty: block_diff_sum / (DIFFICULTY_ADJUST_WINDOW),
|
|
|
|
window_size: DIFFICULTY_ADJUST_WINDOW,
|
|
|
|
block_time_sum: block_time_sum,
|
|
|
|
block_diff_sum: block_diff_sum,
|
|
|
|
latest_ts: latest_ts,
|
|
|
|
earliest_ts: earliest_ts,
|
2018-09-03 14:09:28 +03:00
|
|
|
ts_delta: latest_ts - earliest_ts,
|
2018-07-05 15:18:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 10:48:53 +03:00
|
|
|
// Adds another 'block' to the iterator, so to speak, with difficulty calculated
|
|
|
|
// from the difficulty adjustment at interval seconds from the previous block
|
2018-03-04 03:19:54 +03:00
|
|
|
fn add_block(
|
|
|
|
interval: u64,
|
2018-10-13 23:57:01 +03:00
|
|
|
chain_sim: Vec<(HeaderInfo, DiffStats)>,
|
|
|
|
) -> Vec<(HeaderInfo, DiffStats)> {
|
2018-07-05 15:18:09 +03:00
|
|
|
let mut ret_chain_sim = chain_sim.clone();
|
2018-10-13 23:57:01 +03:00
|
|
|
let mut return_chain: Vec<HeaderInfo> = chain_sim.clone().iter().map(|e| e.0.clone()).collect();
|
2018-01-27 10:48:53 +03:00
|
|
|
// get last interval
|
2018-10-13 23:57:01 +03:00
|
|
|
let diff = next_difficulty(1, return_chain.clone());
|
|
|
|
let last_elem = chain_sim.first().unwrap().clone().0;
|
|
|
|
let time = last_elem.timestamp + interval;
|
|
|
|
return_chain.insert(0, HeaderInfo::from_ts_diff(time, diff.difficulty));
|
2018-07-05 15:18:09 +03:00
|
|
|
let diff_stats = get_diff_stats(&return_chain);
|
2018-10-13 23:57:01 +03:00
|
|
|
ret_chain_sim.insert(
|
|
|
|
0,
|
|
|
|
(HeaderInfo::from_ts_diff(time, diff.difficulty), diff_stats),
|
|
|
|
);
|
2018-07-05 15:18:09 +03:00
|
|
|
ret_chain_sim
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds another n 'blocks' to the iterator, with difficulty calculated
|
2018-03-04 03:19:54 +03:00
|
|
|
fn add_block_repeated(
|
|
|
|
interval: u64,
|
2018-10-13 23:57:01 +03:00
|
|
|
chain_sim: Vec<(HeaderInfo, DiffStats)>,
|
2018-03-04 03:19:54 +03:00
|
|
|
iterations: usize,
|
2018-10-13 23:57:01 +03:00
|
|
|
) -> Vec<(HeaderInfo, DiffStats)> {
|
2018-01-27 10:48:53 +03:00
|
|
|
let mut return_chain = chain_sim.clone();
|
|
|
|
for _ in 0..iterations {
|
|
|
|
return_chain = add_block(interval, return_chain.clone());
|
|
|
|
}
|
|
|
|
return_chain
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// Prints the contents of the iterator and its difficulties.. useful for
|
|
|
|
// tweaking
|
2018-10-13 23:57:01 +03:00
|
|
|
fn print_chain_sim(chain_sim: Vec<(HeaderInfo, DiffStats)>) {
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut chain_sim = chain_sim.clone();
|
2018-01-27 10:48:53 +03:00
|
|
|
chain_sim.reverse();
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut last_time = 0;
|
2018-03-27 21:15:42 +03:00
|
|
|
let mut first = true;
|
2018-07-05 15:18:09 +03:00
|
|
|
println!("Constants");
|
|
|
|
println!("DIFFICULTY_ADJUST_WINDOW: {}", DIFFICULTY_ADJUST_WINDOW);
|
|
|
|
println!("BLOCK_TIME_WINDOW: {}", BLOCK_TIME_WINDOW);
|
2018-10-17 18:21:59 +03:00
|
|
|
println!("CLAMP_FACTOR: {}", CLAMP_FACTOR);
|
2018-11-30 14:19:55 +03:00
|
|
|
println!("DAMP_FACTOR: {}", DIFFICULTY_DAMP_FACTOR);
|
2018-03-04 03:19:54 +03:00
|
|
|
chain_sim.iter().enumerate().for_each(|(i, b)| {
|
2018-10-13 23:57:01 +03:00
|
|
|
let block = b.0.clone();
|
2018-07-05 15:18:09 +03:00
|
|
|
let stats = b.1.clone();
|
2018-03-27 21:15:42 +03:00
|
|
|
if first {
|
2018-10-13 23:57:01 +03:00
|
|
|
last_time = block.timestamp;
|
2018-03-27 21:15:42 +03:00
|
|
|
first = false;
|
|
|
|
}
|
2018-03-04 03:19:54 +03:00
|
|
|
println!(
|
2018-07-05 15:18:09 +03:00
|
|
|
"Height: {}, Time: {}, Interval: {}, Network difficulty:{}, Average Block Time: {}, Average Difficulty {}, Block Time Sum: {}, Block Diff Sum: {}, Latest Timestamp: {}, Earliest Timestamp: {}, Timestamp Delta: {}",
|
2018-03-04 03:19:54 +03:00
|
|
|
i,
|
2018-10-13 23:57:01 +03:00
|
|
|
block.timestamp,
|
|
|
|
block.timestamp - last_time,
|
|
|
|
block.difficulty,
|
2018-07-05 15:18:09 +03:00
|
|
|
stats.average_block_time,
|
|
|
|
stats.average_difficulty,
|
|
|
|
stats.block_time_sum,
|
|
|
|
stats.block_diff_sum,
|
|
|
|
stats.latest_ts,
|
|
|
|
stats.earliest_ts,
|
|
|
|
stats.ts_delta,
|
2018-03-04 03:19:54 +03:00
|
|
|
);
|
2018-07-05 15:18:09 +03:00
|
|
|
let mut sb = stats.last_blocks.clone();
|
|
|
|
sb.reverse();
|
|
|
|
for i in sb {
|
|
|
|
println!(" {}", i);
|
|
|
|
}
|
2018-10-13 23:57:01 +03:00
|
|
|
last_time = block.timestamp;
|
2018-03-04 03:19:54 +03:00
|
|
|
});
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
|
2018-10-13 23:57:01 +03:00
|
|
|
fn repeat_offs(from: u64, interval: u64, diff: u64, len: u64) -> Vec<HeaderInfo> {
|
|
|
|
repeat(
|
|
|
|
interval,
|
|
|
|
HeaderInfo::from_ts_diff(1, Difficulty::from_num(diff)),
|
|
|
|
len,
|
|
|
|
Some(from),
|
|
|
|
)
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks different next_target adjustments and difficulty boundaries
|
|
|
|
#[test]
|
|
|
|
fn adjustment_scenarios() {
|
|
|
|
// Use production parameters for genesis diff
|
|
|
|
global::set_mining_mode(global::ChainTypes::Mainnet);
|
|
|
|
|
|
|
|
// Genesis block with initial diff
|
|
|
|
let chain_sim = create_chain_sim(global::initial_block_difficulty());
|
|
|
|
// Scenario 1) Hash power is massively over estimated, first block takes an hour
|
|
|
|
let chain_sim = add_block_repeated(3600, chain_sim, 2);
|
|
|
|
let chain_sim = add_block_repeated(1800, chain_sim, 2);
|
|
|
|
let chain_sim = add_block_repeated(900, chain_sim, 10);
|
2018-07-05 15:18:09 +03:00
|
|
|
let chain_sim = add_block_repeated(450, chain_sim, 30);
|
|
|
|
let chain_sim = add_block_repeated(400, chain_sim, 30);
|
|
|
|
let chain_sim = add_block_repeated(300, chain_sim, 30);
|
2018-01-27 10:48:53 +03:00
|
|
|
|
|
|
|
println!("*********************************************************");
|
|
|
|
println!("Scenario 1) Grossly over-estimated genesis difficulty ");
|
|
|
|
println!("*********************************************************");
|
2018-07-05 15:18:09 +03:00
|
|
|
print_chain_sim(chain_sim);
|
2018-01-27 10:48:53 +03:00
|
|
|
println!("*********************************************************");
|
|
|
|
|
|
|
|
// Under-estimated difficulty
|
|
|
|
let chain_sim = create_chain_sim(global::initial_block_difficulty());
|
|
|
|
let chain_sim = add_block_repeated(1, chain_sim, 5);
|
|
|
|
let chain_sim = add_block_repeated(20, chain_sim, 5);
|
2018-07-05 15:18:09 +03:00
|
|
|
let chain_sim = add_block_repeated(30, chain_sim, 20);
|
2018-01-27 10:48:53 +03:00
|
|
|
|
|
|
|
println!("*********************************************************");
|
|
|
|
println!("Scenario 2) Grossly under-estimated genesis difficulty ");
|
|
|
|
println!("*********************************************************");
|
2018-07-05 15:18:09 +03:00
|
|
|
print_chain_sim(chain_sim);
|
2018-01-27 10:48:53 +03:00
|
|
|
println!("*********************************************************");
|
2018-10-17 02:55:40 +03:00
|
|
|
let just_enough = (DIFFICULTY_ADJUST_WINDOW) as usize;
|
2018-01-27 10:48:53 +03:00
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// Steady difficulty for a good while, then a sudden drop
|
2018-01-27 10:48:53 +03:00
|
|
|
let chain_sim = create_chain_sim(global::initial_block_difficulty());
|
2018-07-05 15:18:09 +03:00
|
|
|
let chain_sim = add_block_repeated(60, chain_sim, just_enough as usize);
|
|
|
|
let chain_sim = add_block_repeated(600, chain_sim, 60);
|
2018-01-27 10:48:53 +03:00
|
|
|
|
|
|
|
println!("");
|
|
|
|
println!("*********************************************************");
|
|
|
|
println!("Scenario 3) Sudden drop in hashpower");
|
|
|
|
println!("*********************************************************");
|
2018-07-05 15:18:09 +03:00
|
|
|
print_chain_sim(chain_sim);
|
2018-01-27 10:48:53 +03:00
|
|
|
println!("*********************************************************");
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// Sudden increase
|
2018-01-27 10:48:53 +03:00
|
|
|
let chain_sim = create_chain_sim(global::initial_block_difficulty());
|
|
|
|
let chain_sim = add_block_repeated(60, chain_sim, just_enough as usize);
|
|
|
|
let chain_sim = add_block_repeated(10, chain_sim, 10);
|
|
|
|
|
|
|
|
println!("");
|
|
|
|
println!("*********************************************************");
|
|
|
|
println!("Scenario 4) Sudden increase in hashpower");
|
|
|
|
println!("*********************************************************");
|
2018-07-05 15:18:09 +03:00
|
|
|
print_chain_sim(chain_sim);
|
2018-01-27 10:48:53 +03:00
|
|
|
println!("*********************************************************");
|
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
// Oscillations
|
2018-01-27 10:48:53 +03:00
|
|
|
let chain_sim = create_chain_sim(global::initial_block_difficulty());
|
|
|
|
let chain_sim = add_block_repeated(60, chain_sim, just_enough as usize);
|
|
|
|
let chain_sim = add_block_repeated(10, chain_sim, 10);
|
|
|
|
let chain_sim = add_block_repeated(60, chain_sim, 20);
|
|
|
|
let chain_sim = add_block_repeated(10, chain_sim, 10);
|
|
|
|
|
|
|
|
println!("");
|
|
|
|
println!("*********************************************************");
|
|
|
|
println!("Scenario 5) Oscillations in hashpower");
|
|
|
|
println!("*********************************************************");
|
2018-07-05 15:18:09 +03:00
|
|
|
print_chain_sim(chain_sim);
|
2018-01-27 10:48:53 +03:00
|
|
|
println!("*********************************************************");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks different next_target adjustments and difficulty boundaries
|
|
|
|
#[test]
|
|
|
|
fn next_target_adjustment() {
|
|
|
|
global::set_mining_mode(global::ChainTypes::AutomatedTesting);
|
2018-07-30 11:33:28 +03:00
|
|
|
let cur_time = Utc::now().timestamp() as u64;
|
2018-10-18 21:37:33 +03:00
|
|
|
let diff_min = Difficulty::min();
|
2018-01-27 10:48:53 +03:00
|
|
|
|
2018-10-18 21:37:33 +03:00
|
|
|
// Check we don't get stuck on difficulty <= MIN_DIFFICULTY (at 4x faster blocks at least)
|
2018-12-12 19:49:35 +03:00
|
|
|
let mut hi = HeaderInfo::from_diff_scaling(diff_min, AR_SCALE_DAMP_FACTOR as u32);
|
2018-10-18 21:37:33 +03:00
|
|
|
hi.is_secondary = false;
|
2018-12-12 19:49:35 +03:00
|
|
|
let hinext = next_difficulty(
|
|
|
|
1,
|
|
|
|
repeat(
|
|
|
|
BLOCK_TIME_SEC / 4,
|
|
|
|
hi.clone(),
|
|
|
|
DIFFICULTY_ADJUST_WINDOW,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2018-10-18 21:37:33 +03:00
|
|
|
assert_ne!(hinext.difficulty, diff_min);
|
2018-01-27 10:48:53 +03:00
|
|
|
|
2018-10-18 21:37:33 +03:00
|
|
|
// Check we don't get stuck on scale MIN_DIFFICULTY, when primary frequency is too high
|
|
|
|
assert_ne!(hinext.secondary_scaling, MIN_DIFFICULTY as u32);
|
2018-01-27 10:48:53 +03:00
|
|
|
|
|
|
|
// just enough data, right interval, should stay constant
|
2018-10-17 02:55:40 +03:00
|
|
|
let just_enough = DIFFICULTY_ADJUST_WINDOW + 1;
|
2018-10-18 21:37:33 +03:00
|
|
|
hi.difficulty = Difficulty::from_num(10000);
|
2018-01-27 10:48:53 +03:00
|
|
|
assert_eq!(
|
2018-10-18 21:37:33 +03:00
|
|
|
next_difficulty(1, repeat(BLOCK_TIME_SEC, hi.clone(), just_enough, None)).difficulty,
|
|
|
|
Difficulty::from_num(10000)
|
2018-01-27 10:48:53 +03:00
|
|
|
);
|
|
|
|
|
2018-10-19 22:39:54 +03:00
|
|
|
// check pre difficulty_data_to_vector effect on retargetting
|
|
|
|
assert_eq!(
|
|
|
|
next_difficulty(1, vec![HeaderInfo::from_ts_diff(42, hi.difficulty)]).difficulty,
|
|
|
|
Difficulty::from_num(14913)
|
|
|
|
);
|
|
|
|
|
2018-01-27 10:48:53 +03:00
|
|
|
// checking averaging works
|
2018-10-13 23:57:01 +03:00
|
|
|
hi.difficulty = Difficulty::from_num(500);
|
2018-10-17 02:55:40 +03:00
|
|
|
let sec = DIFFICULTY_ADJUST_WINDOW / 2;
|
2018-10-18 21:37:33 +03:00
|
|
|
let mut s1 = repeat(BLOCK_TIME_SEC, hi.clone(), sec, Some(cur_time));
|
2018-03-04 03:19:54 +03:00
|
|
|
let mut s2 = repeat_offs(
|
2018-10-18 21:37:33 +03:00
|
|
|
cur_time + (sec * BLOCK_TIME_SEC) as u64,
|
|
|
|
BLOCK_TIME_SEC,
|
2018-03-04 03:19:54 +03:00
|
|
|
1500,
|
|
|
|
DIFFICULTY_ADJUST_WINDOW / 2,
|
|
|
|
);
|
2018-01-27 10:48:53 +03:00
|
|
|
s2.append(&mut s1);
|
2018-10-13 23:57:01 +03:00
|
|
|
assert_eq!(
|
|
|
|
next_difficulty(1, s2).difficulty,
|
|
|
|
Difficulty::from_num(1000)
|
|
|
|
);
|
2018-01-27 10:48:53 +03:00
|
|
|
|
|
|
|
// too slow, diff goes down
|
2018-10-13 23:57:01 +03:00
|
|
|
hi.difficulty = Difficulty::from_num(1000);
|
2018-01-27 10:48:53 +03:00
|
|
|
assert_eq!(
|
2018-10-13 23:57:01 +03:00
|
|
|
next_difficulty(1, repeat(90, hi.clone(), just_enough, None)).difficulty,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::from_num(857)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-10-13 23:57:01 +03:00
|
|
|
next_difficulty(1, repeat(120, hi.clone(), just_enough, None)).difficulty,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::from_num(750)
|
|
|
|
);
|
|
|
|
|
|
|
|
// too fast, diff goes up
|
|
|
|
assert_eq!(
|
2018-10-13 23:57:01 +03:00
|
|
|
next_difficulty(1, repeat(55, hi.clone(), just_enough, None)).difficulty,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::from_num(1028)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-10-13 23:57:01 +03:00
|
|
|
next_difficulty(1, repeat(45, hi.clone(), just_enough, None)).difficulty,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::from_num(1090)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-10-18 21:37:33 +03:00
|
|
|
next_difficulty(1, repeat(30, hi.clone(), just_enough, None)).difficulty,
|
|
|
|
Difficulty::from_num(1200)
|
2018-01-27 10:48:53 +03:00
|
|
|
);
|
2018-10-18 21:37:33 +03:00
|
|
|
|
|
|
|
// hitting lower time bound, should always get the same result below
|
2018-01-27 10:48:53 +03:00
|
|
|
assert_eq!(
|
2018-10-13 23:57:01 +03:00
|
|
|
next_difficulty(1, repeat(0, hi.clone(), just_enough, None)).difficulty,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::from_num(1500)
|
|
|
|
);
|
|
|
|
|
|
|
|
// hitting higher time bound, should always get the same result above
|
|
|
|
assert_eq!(
|
2018-10-13 23:57:01 +03:00
|
|
|
next_difficulty(1, repeat(300, hi.clone(), just_enough, None)).difficulty,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::from_num(500)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2018-10-13 23:57:01 +03:00
|
|
|
next_difficulty(1, repeat(400, hi.clone(), just_enough, None)).difficulty,
|
2018-01-27 10:48:53 +03:00
|
|
|
Difficulty::from_num(500)
|
|
|
|
);
|
|
|
|
|
2018-10-18 21:37:33 +03:00
|
|
|
// We should never drop below minimum
|
2018-10-13 23:57:01 +03:00
|
|
|
hi.difficulty = Difficulty::zero();
|
2018-01-27 10:48:53 +03:00
|
|
|
assert_eq!(
|
2018-10-13 23:57:01 +03:00
|
|
|
next_difficulty(1, repeat(90, hi.clone(), just_enough, None)).difficulty,
|
2018-10-18 21:37:33 +03:00
|
|
|
Difficulty::min()
|
2018-01-27 10:48:53 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:18:00 +03:00
|
|
|
#[test]
|
2018-11-29 01:05:13 +03:00
|
|
|
fn test_secondary_pow_ratio() {
|
|
|
|
// Tests for mainnet chain type.
|
|
|
|
{
|
|
|
|
global::set_mining_mode(global::ChainTypes::Mainnet);
|
2018-12-29 01:46:21 +03:00
|
|
|
assert_eq!(global::is_floonet(), false);
|
2018-11-29 01:05:13 +03:00
|
|
|
|
|
|
|
assert_eq!(secondary_pow_ratio(1), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(89), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(90), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(91), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(179), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(180), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(181), 90);
|
|
|
|
|
|
|
|
let one_week = 60 * 24 * 7;
|
|
|
|
assert_eq!(secondary_pow_ratio(one_week - 1), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(one_week), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(one_week + 1), 90);
|
|
|
|
|
|
|
|
let two_weeks = one_week * 2;
|
|
|
|
assert_eq!(secondary_pow_ratio(two_weeks - 1), 89);
|
|
|
|
assert_eq!(secondary_pow_ratio(two_weeks), 89);
|
|
|
|
assert_eq!(secondary_pow_ratio(two_weeks + 1), 89);
|
|
|
|
|
|
|
|
let t4_fork_height = 64_000;
|
|
|
|
assert_eq!(secondary_pow_ratio(t4_fork_height - 1), 85);
|
|
|
|
assert_eq!(secondary_pow_ratio(t4_fork_height), 85);
|
|
|
|
assert_eq!(secondary_pow_ratio(t4_fork_height + 1), 85);
|
|
|
|
|
|
|
|
let one_year = one_week * 52;
|
|
|
|
assert_eq!(secondary_pow_ratio(one_year), 45);
|
|
|
|
|
|
|
|
let ninety_one_weeks = one_week * 91;
|
|
|
|
assert_eq!(secondary_pow_ratio(ninety_one_weeks - 1), 12);
|
|
|
|
assert_eq!(secondary_pow_ratio(ninety_one_weeks), 12);
|
|
|
|
assert_eq!(secondary_pow_ratio(ninety_one_weeks + 1), 12);
|
|
|
|
|
|
|
|
let two_year = one_year * 2;
|
|
|
|
assert_eq!(secondary_pow_ratio(two_year - 1), 1);
|
|
|
|
assert_eq!(secondary_pow_ratio(two_year), 0);
|
|
|
|
assert_eq!(secondary_pow_ratio(two_year + 1), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests for testnet4 chain type (covers pre and post hardfork).
|
|
|
|
{
|
2018-12-19 01:03:55 +03:00
|
|
|
global::set_mining_mode(global::ChainTypes::Floonet);
|
2018-12-29 01:46:21 +03:00
|
|
|
assert_eq!(global::is_floonet(), true);
|
2018-11-29 01:05:13 +03:00
|
|
|
|
|
|
|
assert_eq!(secondary_pow_ratio(1), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(89), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(90), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(91), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(179), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(180), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(181), 90);
|
|
|
|
|
|
|
|
let one_week = 60 * 24 * 7;
|
|
|
|
assert_eq!(secondary_pow_ratio(one_week - 1), 90);
|
2018-12-19 01:03:55 +03:00
|
|
|
assert_eq!(secondary_pow_ratio(one_week), 90);
|
|
|
|
assert_eq!(secondary_pow_ratio(one_week + 1), 90);
|
2018-11-29 01:05:13 +03:00
|
|
|
|
|
|
|
let two_weeks = one_week * 2;
|
|
|
|
assert_eq!(secondary_pow_ratio(two_weeks - 1), 89);
|
2018-12-19 01:03:55 +03:00
|
|
|
assert_eq!(secondary_pow_ratio(two_weeks), 89);
|
|
|
|
assert_eq!(secondary_pow_ratio(two_weeks + 1), 89);
|
2018-11-29 01:05:13 +03:00
|
|
|
|
|
|
|
let t4_fork_height = 64_000;
|
2018-12-19 01:03:55 +03:00
|
|
|
assert_eq!(secondary_pow_ratio(t4_fork_height - 1), 85);
|
2018-11-29 01:05:13 +03:00
|
|
|
assert_eq!(secondary_pow_ratio(t4_fork_height), 85);
|
|
|
|
assert_eq!(secondary_pow_ratio(t4_fork_height + 1), 85);
|
|
|
|
|
|
|
|
let one_year = one_week * 52;
|
|
|
|
assert_eq!(secondary_pow_ratio(one_year), 45);
|
|
|
|
|
|
|
|
let ninety_one_weeks = one_week * 91;
|
|
|
|
assert_eq!(secondary_pow_ratio(ninety_one_weeks - 1), 12);
|
|
|
|
assert_eq!(secondary_pow_ratio(ninety_one_weeks), 12);
|
|
|
|
assert_eq!(secondary_pow_ratio(ninety_one_weeks + 1), 12);
|
|
|
|
|
|
|
|
let two_year = one_year * 2;
|
|
|
|
assert_eq!(secondary_pow_ratio(two_year - 1), 1);
|
|
|
|
assert_eq!(secondary_pow_ratio(two_year), 0);
|
|
|
|
assert_eq!(secondary_pow_ratio(two_year + 1), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_secondary_pow_scale() {
|
2018-10-17 02:55:40 +03:00
|
|
|
let window = DIFFICULTY_ADJUST_WINDOW;
|
2018-10-16 00:18:00 +03:00
|
|
|
let mut hi = HeaderInfo::from_diff_scaling(Difficulty::from_num(10), 100);
|
|
|
|
|
2018-11-30 14:19:55 +03:00
|
|
|
// mainnet testing
|
|
|
|
{
|
|
|
|
global::set_mining_mode(global::ChainTypes::Mainnet);
|
2018-12-29 01:46:21 +03:00
|
|
|
assert_eq!(global::is_floonet(), false);
|
2018-11-30 14:19:55 +03:00
|
|
|
|
|
|
|
// all primary, factor should increase so it becomes easier to find a high
|
|
|
|
// difficulty block
|
|
|
|
hi.is_secondary = false;
|
|
|
|
assert_eq!(
|
|
|
|
secondary_pow_scaling(1, &(0..window).map(|_| hi.clone()).collect::<Vec<_>>()),
|
2018-12-22 21:22:36 +03:00
|
|
|
108
|
2018-11-30 14:19:55 +03:00
|
|
|
);
|
|
|
|
// all secondary on 90%, factor should go down a bit
|
|
|
|
hi.is_secondary = true;
|
|
|
|
assert_eq!(
|
|
|
|
secondary_pow_scaling(1, &(0..window).map(|_| hi.clone()).collect::<Vec<_>>()),
|
2018-12-22 21:22:36 +03:00
|
|
|
99
|
2018-11-30 14:19:55 +03:00
|
|
|
);
|
|
|
|
// all secondary on 1%, factor should go down to bound (divide by 2)
|
|
|
|
assert_eq!(
|
|
|
|
secondary_pow_scaling(
|
2018-12-22 21:22:36 +03:00
|
|
|
2 * YEAR_HEIGHT * 83 / 90,
|
2018-11-30 14:19:55 +03:00
|
|
|
&(0..window).map(|_| hi.clone()).collect::<Vec<_>>()
|
|
|
|
),
|
2018-12-22 21:22:36 +03:00
|
|
|
50
|
2018-11-30 14:19:55 +03:00
|
|
|
);
|
|
|
|
// same as above, testing lowest bound
|
|
|
|
let mut low_hi =
|
2018-12-22 21:22:36 +03:00
|
|
|
HeaderInfo::from_diff_scaling(Difficulty::from_num(10), MIN_AR_SCALE as u32);
|
2018-11-30 14:19:55 +03:00
|
|
|
low_hi.is_secondary = true;
|
|
|
|
assert_eq!(
|
|
|
|
secondary_pow_scaling(
|
2018-12-22 21:22:36 +03:00
|
|
|
2 * YEAR_HEIGHT,
|
2018-11-30 14:19:55 +03:00
|
|
|
&(0..window).map(|_| low_hi.clone()).collect::<Vec<_>>()
|
|
|
|
),
|
2018-12-22 21:22:36 +03:00
|
|
|
MIN_AR_SCALE as u32
|
2018-11-30 14:19:55 +03:00
|
|
|
);
|
2018-12-22 21:22:36 +03:00
|
|
|
// the right ratio of 95% secondary
|
2018-11-30 14:19:55 +03:00
|
|
|
let mut primary_hi = HeaderInfo::from_diff_scaling(Difficulty::from_num(10), 50);
|
|
|
|
primary_hi.is_secondary = false;
|
|
|
|
assert_eq!(
|
|
|
|
secondary_pow_scaling(
|
|
|
|
1,
|
|
|
|
&(0..(window / 10))
|
|
|
|
.map(|_| primary_hi.clone())
|
|
|
|
.chain((0..(window * 9 / 10)).map(|_| hi.clone()))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
),
|
2018-12-22 21:22:36 +03:00
|
|
|
95, // avg ar_scale of 10% * 50 + 90% * 100
|
2018-11-30 14:19:55 +03:00
|
|
|
);
|
|
|
|
// 95% secondary, should come down based on 97.5 average
|
|
|
|
assert_eq!(
|
|
|
|
secondary_pow_scaling(
|
|
|
|
1,
|
|
|
|
&(0..(window / 20))
|
|
|
|
.map(|_| primary_hi.clone())
|
|
|
|
.chain((0..(window * 95 / 100)).map(|_| hi.clone()))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
),
|
2018-12-22 21:22:36 +03:00
|
|
|
97
|
2018-11-30 14:19:55 +03:00
|
|
|
);
|
|
|
|
// 40% secondary, should come up based on 70 average
|
|
|
|
assert_eq!(
|
|
|
|
secondary_pow_scaling(
|
|
|
|
1,
|
|
|
|
&(0..(window * 6 / 10))
|
|
|
|
.map(|_| primary_hi.clone())
|
|
|
|
.chain((0..(window * 4 / 10)).map(|_| hi.clone()))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
),
|
2018-12-22 21:22:36 +03:00
|
|
|
73
|
2018-11-30 14:19:55 +03:00
|
|
|
);
|
|
|
|
}
|
2018-10-16 00:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-01-27 10:48:53 +03:00
|
|
|
#[test]
|
2018-09-11 01:36:57 +03:00
|
|
|
fn hard_forks() {
|
2019-05-08 23:10:42 +03:00
|
|
|
assert!(valid_header_version(0, HeaderVersion::new(1)));
|
|
|
|
assert!(valid_header_version(10, HeaderVersion::new(1)));
|
|
|
|
assert!(!valid_header_version(10, HeaderVersion::new(2)));
|
|
|
|
assert!(valid_header_version(
|
|
|
|
YEAR_HEIGHT / 2 - 1,
|
|
|
|
HeaderVersion::new(1)
|
|
|
|
));
|
2018-10-13 23:57:01 +03:00
|
|
|
// v2 not active yet
|
2019-05-08 23:10:42 +03:00
|
|
|
assert!(!valid_header_version(
|
|
|
|
YEAR_HEIGHT / 2,
|
|
|
|
HeaderVersion::new(2)
|
|
|
|
));
|
|
|
|
assert!(!valid_header_version(
|
|
|
|
YEAR_HEIGHT / 2,
|
|
|
|
HeaderVersion::new(1)
|
|
|
|
));
|
|
|
|
assert!(!valid_header_version(YEAR_HEIGHT, HeaderVersion::new(1)));
|
|
|
|
assert!(!valid_header_version(
|
|
|
|
YEAR_HEIGHT / 2 + 1,
|
|
|
|
HeaderVersion::new(2)
|
|
|
|
));
|
2018-01-27 10:48:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn hard_fork_2() {
|
|
|
|
// assert!(valid_header_version(0, 1));
|
|
|
|
// assert!(valid_header_version(10, 1));
|
|
|
|
// assert!(valid_header_version(10, 2));
|
|
|
|
// assert!(valid_header_version(250_000, 1));
|
|
|
|
// assert!(!valid_header_version(250_001, 1));
|
|
|
|
// assert!(!valid_header_version(500_000, 1));
|
|
|
|
// assert!(valid_header_version(250_001, 2));
|
|
|
|
// assert!(valid_header_version(500_000, 2));
|
|
|
|
// assert!(!valid_header_version(500_001, 2));
|
|
|
|
// }
|