rename MIN_DIFFICULTY to UNIT_DIFFICULTY; define Difficulty::unit, fix INITIAL_DIFFICULTY

This commit is contained in:
John Tromp
2018-10-18 14:20:00 +02:00
parent 545d9d6116
commit e24e65399b
4 changed files with 17 additions and 13 deletions
+3 -6
View File
@@ -158,18 +158,15 @@ pub fn graph_weight(edge_bits: u8) -> u64 {
(2 << (edge_bits - global::base_edge_bits()) as u64) * (edge_bits as u64)
}
/// minimum possible difficulty equal to graph_weight(SECOND_POW_EDGE_BITS)
pub const MIN_DIFFICULTY: u64 =
/// unit difficulty, equal to graph_weight(SECOND_POW_EDGE_BITS)
pub const UNIT_DIFFICULTY: u64 =
((2 as u64) << (SECOND_POW_EDGE_BITS - BASE_EDGE_BITS)) * (SECOND_POW_EDGE_BITS as u64);
/// The initial difficulty at launch. This should be over-estimated
/// and difficulty should come down at launch rather than up
/// Currently grossly over-estimated at 10% of current
/// ethereum GPUs (assuming 1GPU can solve a block at diff 1 in one block interval)
/// FOR MAINNET, use
/// pub const INITIAL_DIFFICULTY: u64 = 1_000_000 * MIN_DIFFICULTY;
/// Pick MUCH more modest value for TESTNET4:
pub const INITIAL_DIFFICULTY: u64 = 1_000 * MIN_DIFFICULTY;
pub const INITIAL_DIFFICULTY: u64 = 1_000_000 * UNIT_DIFFICULTY;
/// Consensus errors
#[derive(Clone, Debug, Eq, PartialEq, Fail)]
+2 -1
View File
@@ -20,6 +20,7 @@ use consensus::HeaderInfo;
use consensus::{
graph_weight, BASE_EDGE_BITS, BLOCK_TIME_SEC, COINBASE_MATURITY, CUT_THROUGH_HORIZON,
DAY_HEIGHT, DIFFICULTY_ADJUST_WINDOW, INITIAL_DIFFICULTY, PROOFSIZE, SECOND_POW_EDGE_BITS,
UNIT_DIFFICULTY
};
use pow::{self, CuckatooContext, EdgeType, PoWContext};
/// An enum collecting sets of parameters used throughout the
@@ -71,7 +72,7 @@ pub const STUCK_PEER_KICK_TIME: i64 = 2 * 3600 * 1000;
/// Testnet 4 initial block difficulty
/// 1_000 times natural scale factor for cuckatoo29
pub const TESTNET4_INITIAL_DIFFICULTY: u64 = 1_000 * (2 << (29 - 24)) * 29;
pub const TESTNET4_INITIAL_DIFFICULTY: u64 = 1_000 * UNIT_DIFFICULTY;
/// Trigger compaction check on average every day for FAST_SYNC_NODE,
/// roll the dice on every block to decide,
+5
View File
@@ -70,6 +70,11 @@ impl Difficulty {
Difficulty { num: 1 }
}
/// Difficulty unit, which is the graph weight of minimal graph
pub fn unit() -> Difficulty {
Difficulty { num: global::initial_graph_weight() }
}
/// Convert a `u32` into a `Difficulty`
pub fn from_num(num: u64) -> Difficulty {
// can't have difficulty lower than 1
+7 -6
View File
@@ -383,36 +383,37 @@ fn next_target_adjustment() {
let cur_time = Utc::now().timestamp() as u64;
let diff_one = Difficulty::one();
let diff_unit = Difficulty::unit();
assert_eq!(
next_difficulty(1, vec![HeaderInfo::from_ts_diff(cur_time, diff_one)]),
HeaderInfo::from_diff_scaling(Difficulty::one(), 1),
HeaderInfo::from_diff_scaling(diff_one, diff_unit),
);
assert_eq!(
next_difficulty(1, vec![HeaderInfo::new(cur_time, diff_one, 10, true)]),
HeaderInfo::from_diff_scaling(Difficulty::one(), 1),
HeaderInfo::from_diff_scaling(diff_one, 1),
);
let mut hi = HeaderInfo::from_diff_scaling(diff_one, 1);
assert_eq!(
next_difficulty(1, repeat(60, hi.clone(), DIFFICULTY_ADJUST_WINDOW, None)),
HeaderInfo::from_diff_scaling(Difficulty::one(), 1),
HeaderInfo::from_diff_scaling(diff_one, 1),
);
hi.is_secondary = true;
assert_eq!(
next_difficulty(1, repeat(60, hi.clone(), DIFFICULTY_ADJUST_WINDOW, None)),
HeaderInfo::from_diff_scaling(Difficulty::one(), 1),
HeaderInfo::from_diff_scaling(diff_one, 1),
);
hi.secondary_scaling = 100;
assert_eq!(
next_difficulty(1, repeat(60, hi.clone(), DIFFICULTY_ADJUST_WINDOW, None)),
HeaderInfo::from_diff_scaling(Difficulty::one(), 96),
HeaderInfo::from_diff_scaling(diff_one, 96),
);
// Check we don't get stuck on difficulty 1
let mut hi = HeaderInfo::from_diff_scaling(Difficulty::from_num(10), 1);
assert_ne!(
next_difficulty(1, repeat(1, hi.clone(), DIFFICULTY_ADJUST_WINDOW, None)).difficulty,
Difficulty::one()
diff_one
);
// just enough data, right interval, should stay constant