[T4] Secondary proof of work difficulty adjustments (#1709)

* First pass at secondary proof of work difficulty adjustments
* Core and chain test fixes
* Next difficulty calc now needs a height. Scaling calculation fixes. Setting scaling on mined block.
* Change factor to u32 instead of u64.
* Cleanup structs used by next_difficulty
* Fix header size calc with u32 scaling
This commit is contained in:
Ignotus Peverell
2018-10-13 13:57:01 -07:00
committed by GitHub
parent e9f62b74d5
commit 43f4f92730
21 changed files with 376 additions and 301 deletions
+4 -5
View File
@@ -166,11 +166,10 @@ impl ServerConfig {
// check [server.p2p_config.capabilities] with 'archive_mode' in [server]
if let Some(archive) = self.archive_mode {
// note: slog not available before config loaded, only print here.
if archive
!= self
.p2p_config
.capabilities
.contains(p2p::Capabilities::FULL_HIST)
if archive != self
.p2p_config
.capabilities
.contains(p2p::Capabilities::FULL_HIST)
{
// if conflict, 'archive_mode' win
self.p2p_config
+6 -8
View File
@@ -30,7 +30,6 @@ use common::stats::{DiffBlock, DiffStats, PeerStats, ServerStateInfo, ServerStat
use common::types::{Error, ServerConfig, StratumServerConfig, SyncState};
use core::core::hash::Hashed;
use core::core::verifier_cache::{LruVerifierCache, VerifierCache};
use core::pow::Difficulty;
use core::{consensus, genesis, global, pow};
use grin::{dandelion_monitor, seed, sync};
use mining::stratumserver;
@@ -397,14 +396,14 @@ impl Server {
// code clean. This may be handy for testing but not really needed
// for release
let diff_stats = {
let last_blocks: Vec<Result<(u64, Difficulty), consensus::TargetError>> =
let last_blocks: Vec<consensus::HeaderInfo> =
global::difficulty_data_to_vector(self.chain.difficulty_iter())
.into_iter()
.skip(consensus::MEDIAN_TIME_WINDOW as usize)
.take(consensus::DIFFICULTY_ADJUST_WINDOW as usize)
.collect();
let mut last_time = last_blocks[0].clone().unwrap().0;
let mut last_time = last_blocks[0].timestamp;
let tip_height = self.chain.head().unwrap().height as i64;
let earliest_block_height = tip_height as i64 - last_blocks.len() as i64;
@@ -414,15 +413,14 @@ impl Server {
.iter()
.skip(1)
.map(|n| {
let (time, diff) = n.clone().unwrap();
let dur = time - last_time;
let dur = n.timestamp - last_time;
let height = earliest_block_height + i + 1;
i += 1;
last_time = time;
last_time = n.timestamp;
DiffBlock {
block_number: height,
difficulty: diff.to_num(),
time: time,
difficulty: n.difficulty.to_num(),
time: n.timestamp,
duration: dur,
}
}).collect();
+1 -1
View File
@@ -185,7 +185,7 @@ fn needs_syncing(
// sum the last 5 difficulties to give us the threshold
let threshold = chain
.difficulty_iter()
.filter_map(|x| x.map(|(_, x)| x).ok())
.map(|x| x.difficulty)
.take(5)
.fold(Difficulty::zero(), |sum, val| sum + val);
+4 -3
View File
@@ -106,7 +106,7 @@ fn build_block(
// Determine the difficulty our block should be at.
// Note: do not keep the difficulty_iter in scope (it has an active batch).
let difficulty = consensus::next_difficulty(chain.difficulty_iter()).unwrap();
let difficulty = consensus::next_difficulty(1, chain.difficulty_iter());
// extract current transaction from the pool
// TODO - we have a lot of unwrap() going on in this fn...
@@ -126,13 +126,14 @@ fn build_block(
};
let (output, kernel, block_fees) = get_coinbase(wallet_listener_url, block_fees)?;
let mut b = core::Block::with_reward(&head, txs, output, kernel, difficulty.clone())?;
let mut b = core::Block::with_reward(&head, txs, output, kernel, difficulty.difficulty)?;
// making sure we're not spending time mining a useless block
b.validate(&head.total_kernel_offset, verifier_cache)?;
b.header.pow.nonce = thread_rng().gen();
b.header.timestamp = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(now_sec, 0), Utc);;
b.header.pow.scaling_difficulty = difficulty.secondary_scaling;
b.header.timestamp = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(now_sec, 0), Utc);
let b_difficulty = (b.header.total_difficulty() - head.total_difficulty()).to_num();
debug!(