diff --git a/core/src/consensus.rs b/core/src/consensus.rs index aecfb137..dbc68789 100644 --- a/core/src/consensus.rs +++ b/core/src/consensus.rs @@ -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)] diff --git a/core/src/global.rs b/core/src/global.rs index bb1e6baa..0002d82b 100644 --- a/core/src/global.rs +++ b/core/src/global.rs @@ -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, diff --git a/core/src/pow/types.rs b/core/src/pow/types.rs index 505957c5..96ba1d52 100644 --- a/core/src/pow/types.rs +++ b/core/src/pow/types.rs @@ -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 diff --git a/core/tests/consensus.rs b/core/tests/consensus.rs index 41f3d583..039753a9 100644 --- a/core/tests/consensus.rs +++ b/core/tests/consensus.rs @@ -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