[T4] Set genesis block data + initial secondary scaling correctly (#1776)

* ensure genesis block+pre genesis is populated correctly with secondary scaling

* rustfmt
This commit is contained in:
Yeastplume
2018-10-17 16:53:31 +01:00
committed by GitHub
parent e9dcc143bf
commit b43d6e4326
4 changed files with 55 additions and 17 deletions
+33 -12
View File
@@ -49,8 +49,8 @@ pub fn reward(fee: u64) -> u64 {
/// Nominal height for standard time intervals
pub const HOUR_HEIGHT: u64 = 3600 / BLOCK_TIME_SEC;
pub const DAY_HEIGHT: u64 = 24 * HOUR_HEIGHT;
pub const WEEK_HEIGHT: u64 = 7 * DAY_HEIGHT;
pub const DAY_HEIGHT: u64 = 24 * HOUR_HEIGHT;
pub const WEEK_HEIGHT: u64 = 7 * DAY_HEIGHT;
pub const YEAR_HEIGHT: u64 = 52 * WEEK_HEIGHT;
/// Number of blocks before a coinbase matures and can be spent
@@ -156,7 +156,8 @@ pub fn graph_weight(edge_bits: u8) -> u64 {
}
/// minimum possible difficulty equal to graph_weight(SECOND_POW_EDGE_BITS)
pub const MIN_DIFFICULTY: u64 = ((2 as u64) << (SECOND_POW_EDGE_BITS - BASE_EDGE_BITS)) * (SECOND_POW_EDGE_BITS as u64);
pub const MIN_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
@@ -216,7 +217,7 @@ impl HeaderInfo {
HeaderInfo {
timestamp,
difficulty,
secondary_scaling: 1,
secondary_scaling: global::initial_graph_weight(),
is_secondary: false,
}
}
@@ -233,9 +234,12 @@ impl HeaderInfo {
}
}
/// TODO: Doc
pub fn damp(actual: u64, goal: u64, damp_factor: u64) -> u64 {
(1 * actual + (damp_factor-1) * goal) / damp_factor
(1 * actual + (damp_factor - 1) * goal) / damp_factor
}
/// TODO: Doc
pub fn clamp(actual: u64, goal: u64, clamp_factor: u64) -> u64 {
max(goal / clamp_factor, min(actual, goal * clamp_factor))
}
@@ -267,13 +271,22 @@ where
let sec_pow_scaling = secondary_pow_scaling(height, &diff_data);
// Get the timestamp delta across the window
let ts_delta: u64 = diff_data[DIFFICULTY_ADJUST_WINDOW as usize].timestamp - diff_data[0].timestamp;
let ts_delta: u64 =
diff_data[DIFFICULTY_ADJUST_WINDOW as usize].timestamp - diff_data[0].timestamp;
// Get the difficulty sum of the last DIFFICULTY_ADJUST_WINDOW elements
let diff_sum: u64 = diff_data.iter().skip(1).map(|dd| dd.difficulty.to_num()).sum();
let diff_sum: u64 = diff_data
.iter()
.skip(1)
.map(|dd| dd.difficulty.to_num())
.sum();
// adjust time delta toward goal subject to dampening and clamping
let adj_ts = clamp(damp(ts_delta, BLOCK_TIME_WINDOW, DAMP_FACTOR), BLOCK_TIME_WINDOW, CLAMP_FACTOR);
// adjust time delta toward goal subject to dampening and clamping
let adj_ts = clamp(
damp(ts_delta, BLOCK_TIME_WINDOW, DAMP_FACTOR),
BLOCK_TIME_WINDOW,
CLAMP_FACTOR,
);
let difficulty = max(1, diff_sum * BLOCK_TIME_SEC / adj_ts);
HeaderInfo::from_diff_scaling(Difficulty::from_num(difficulty), sec_pow_scaling)
@@ -285,14 +298,22 @@ pub fn secondary_pow_scaling(height: u64, diff_data: &Vec<HeaderInfo>) -> u32 {
let snd_count = 100 * diff_data.iter().filter(|n| n.is_secondary).count() as u64;
// Get the scaling factor sum of the last DIFFICULTY_ADJUST_WINDOW elements
let scale_sum: u64 = diff_data.iter().skip(1).map(|dd| dd.secondary_scaling as u64).sum();
let scale_sum: u64 = diff_data
.iter()
.skip(1)
.map(|dd| dd.secondary_scaling as u64)
.sum();
// compute ideal 2nd_pow_fraction in pct and across window
let target_pct = secondary_pow_ratio(height);
let target_count = DIFFICULTY_ADJUST_WINDOW * target_pct;
// adjust count toward goal subject to dampening and clamping
let adj_count = clamp(damp(snd_count, target_count, DAMP_FACTOR), target_count, CLAMP_FACTOR);
// adjust count toward goal subject to dampening and clamping
let adj_count = clamp(
damp(snd_count, target_count, DAMP_FACTOR),
target_count,
CLAMP_FACTOR,
);
let scale = scale_sum * target_pct / adj_count;
max(1, min(scale, MAX_SECONDARY_SCALING)) as u32
+2 -2
View File
@@ -111,10 +111,10 @@ pub fn genesis_testnet4() -> core::Block {
core::Block::with_header(core::BlockHeader {
height: 0,
previous: core::hash::Hash([0xff; 32]),
timestamp: Utc.ymd(2018, 10, 17).and_hms(13, 0, 0),
timestamp: Utc.ymd(2018, 10, 17).and_hms(16, 0, 0),
pow: ProofOfWork {
total_difficulty: Difficulty::from_num(global::initial_block_difficulty()),
scaling_difficulty: 1,
scaling_difficulty: global::initial_graph_weight(),
nonce: 4956988373127692,
proof: Proof::new(vec![
0xa420dc, 0xc8ffee, 0x10e433e, 0x1de9428, 0x2ed4cea, 0x52d907b, 0x5af0e3f,
+19 -2
View File
@@ -18,8 +18,9 @@
use consensus::HeaderInfo;
use consensus::{
BASE_EDGE_BITS, BLOCK_TIME_SEC, COINBASE_MATURITY, CUT_THROUGH_HORIZON, DAY_HEIGHT,
DIFFICULTY_ADJUST_WINDOW, INITIAL_DIFFICULTY, PROOFSIZE, SECOND_POW_EDGE_BITS,
graph_weight, BASE_EDGE_BITS, BLOCK_TIME_SEC, COINBASE_MATURITY, CUT_THROUGH_HORIZON,
DAY_HEIGHT, DIFFICULTY_ADJUST_WINDOW, INITIAL_DIFFICULTY, MIN_DIFFICULTY, PROOFSIZE,
SECOND_POW_EDGE_BITS,
};
use pow::{self, CuckatooContext, EdgeType, PoWContext};
/// An enum collecting sets of parameters used throughout the
@@ -52,6 +53,9 @@ pub const USER_TESTING_COINBASE_MATURITY: u64 = 3;
/// Testing cut through horizon in blocks
pub const TESTING_CUT_THROUGH_HORIZON: u32 = 20;
/// Testing initial graph weight
pub const TESTING_INITIAL_GRAPH_WEIGHT: u32 = 1;
/// Testing initial block difficulty
pub const TESTING_INITIAL_DIFFICULTY: u64 = 1;
@@ -199,6 +203,19 @@ pub fn initial_block_difficulty() -> u64 {
ChainTypes::Mainnet => INITIAL_DIFFICULTY,
}
}
/// Initial mining secondary scale
pub fn initial_graph_weight() -> u32 {
let param_ref = CHAIN_TYPE.read().unwrap();
match *param_ref {
ChainTypes::AutomatedTesting => TESTING_INITIAL_GRAPH_WEIGHT,
ChainTypes::UserTesting => TESTING_INITIAL_GRAPH_WEIGHT,
ChainTypes::Testnet1 => TESTING_INITIAL_GRAPH_WEIGHT,
ChainTypes::Testnet2 => TESTING_INITIAL_GRAPH_WEIGHT,
ChainTypes::Testnet3 => TESTING_INITIAL_GRAPH_WEIGHT,
ChainTypes::Testnet4 => graph_weight(SECOND_POW_EDGE_BITS) as u32,
ChainTypes::Mainnet => graph_weight(SECOND_POW_EDGE_BITS) as u32,
}
}
/// Horizon at which we can cut-through and do full local pruning
pub fn cut_through_horizon() -> u32 {
+1 -1
View File
@@ -35,7 +35,7 @@ pub const PROTOCOL_VERSION: u32 = 1;
pub const USER_AGENT: &'static str = concat!("MW/Grin ", env!("CARGO_PKG_VERSION"));
/// Magic number expected in the header of every message
const MAGIC: [u8; 2] = [0x47, 0x33];
const MAGIC: [u8; 2] = [0x47, 0x34];
/// Size in bytes of a message header
pub const HEADER_LEN: u64 = 11;