From 612bf0b58a3652716a3833f07c0d53fdbf542bf7 Mon Sep 17 00:00:00 2001 From: wowario Date: Fri, 31 May 2019 02:35:02 +0300 Subject: [PATCH] difficulty: revert to int64_t --- src/cryptonote_basic/difficulty.cpp | 32 ++++++++++++++--------------- src/cryptonote_basic/difficulty.h | 2 +- src/cryptonote_core/blockchain.cpp | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/cryptonote_basic/difficulty.cpp b/src/cryptonote_basic/difficulty.cpp index ee09f0b5d..50541e85f 100644 --- a/src/cryptonote_basic/difficulty.cpp +++ b/src/cryptonote_basic/difficulty.cpp @@ -258,10 +258,10 @@ namespace cryptonote { // LWMA difficulty algorithm // Background: https://github.com/zawy12/difficulty-algorithms/issues/3 // Copyright (c) 2017-2018 Zawy - difficulty_type next_difficulty_v2(std::vector timestamps, std::vector cumulative_difficulties) { + difficulty_type next_difficulty_v2(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds) { - uint64_t T = DIFFICULTY_TARGET_V2; - uint64_t N = DIFFICULTY_WINDOW_V2; + const int64_t T = static_cast(target_seconds); + size_t N = DIFFICULTY_WINDOW_V2; if (timestamps.size() < 4) { return 1; } else if ( timestamps.size() < N+1 ) { @@ -273,17 +273,17 @@ namespace cryptonote { const double adjust = 0.998; const double k = N * (N + 1) / 2; double LWMA(0), sum_inverse_D(0), harmonic_mean_D(0), nextDifficulty(0); - uint64_t solveTime(0); + int64_t solveTime(0); uint64_t difficulty(0), next_difficulty(0); - for (uint64_t i = 1; i <= N; i++) { - solveTime = static_cast(timestamps[i]) - static_cast(timestamps[i - 1]); - solveTime = std::min((T * 7), std::max(solveTime, (-7 * T))); + for (size_t i = 1; i <= N; i++) { + solveTime = static_cast(timestamps[i]) - static_cast(timestamps[i - 1]); + solveTime = std::min((T * 7), std::max(solveTime, (-7 * T))); difficulty = static_cast(cumulative_difficulties[i] - cumulative_difficulties[i - 1]); - LWMA += (uint64_t)(solveTime * i) / k; + LWMA += (int64_t)(solveTime * i) / k; sum_inverse_D += 1 / static_cast(difficulty); } harmonic_mean_D = N / sum_inverse_D; - if (static_cast(boost::math::round(LWMA)) < T / 20) + if (static_cast(boost::math::round(LWMA)) < T / 20) LWMA = static_cast(T / 20); nextDifficulty = harmonic_mean_D * T / LWMA * adjust; @@ -294,20 +294,20 @@ namespace cryptonote { // LWMA-2 difficulty_type next_difficulty_v3(std::vector timestamps, std::vector cumulative_difficulties) { - uint64_t T = DIFFICULTY_TARGET_V2; - uint64_t N = DIFFICULTY_WINDOW_V2; - uint64_t L(0), ST, sum_3_ST(0), next_D, prev_D; + int64_t T = DIFFICULTY_TARGET_V2; + int64_t N = DIFFICULTY_WINDOW_V2; + int64_t L(0), ST, sum_3_ST(0), next_D, prev_D; assert(timestamps.size() == cumulative_difficulties.size() && timestamps.size() <= static_cast(N+1) ); - for ( uint64_t i = 1; i <= N; i++ ) { - ST = static_cast(timestamps[i]) - static_cast(timestamps[i-1]); + for ( int64_t i = 1; i <= N; i++ ) { + ST = static_cast(timestamps[i]) - static_cast(timestamps[i-1]); ST = std::max(-4*T, std::min(ST, 6*T)); L += ST * i ; if ( i > N-3 ) { sum_3_ST += ST; } } - next_D = (static_cast(cumulative_difficulties[N] - cumulative_difficulties[0])*T*(N+1)*99)/(100*2*L); - prev_D = static_cast(cumulative_difficulties[N] - cumulative_difficulties[N-1]); + next_D = (static_cast(cumulative_difficulties[N] - cumulative_difficulties[0])*T*(N+1)*99)/(100*2*L); + prev_D = static_cast(cumulative_difficulties[N] - cumulative_difficulties[N-1]); next_D = std::max((prev_D*67)/100, std::min(next_D, (prev_D*150)/100)); if ( sum_3_ST < (8*T)/10) { next_D = std::max(next_D,(prev_D*108)/100); diff --git a/src/cryptonote_basic/difficulty.h b/src/cryptonote_basic/difficulty.h index 7aedc19c8..9e969c9e9 100644 --- a/src/cryptonote_basic/difficulty.h +++ b/src/cryptonote_basic/difficulty.h @@ -59,7 +59,7 @@ namespace cryptonote bool check_hash_128(const crypto::hash &hash, difficulty_type difficulty); bool check_hash(const crypto::hash &hash, difficulty_type difficulty); difficulty_type next_difficulty(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds); - difficulty_type next_difficulty_v2(std::vector timestamps, std::vector cumulative_difficulties); + difficulty_type next_difficulty_v2(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds); difficulty_type next_difficulty_v3(std::vector timestamps, std::vector cumulative_difficulties); difficulty_type next_difficulty_v4(std::vector timestamps, std::vector cumulative_difficulties, size_t height); difficulty_type next_difficulty_v5(std::vector timestamps, std::vector cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT, uint64_t FORK_HEIGHT, uint64_t difficulty_guess); diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index dd29e9e0f..b4dc6692f 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -928,7 +928,7 @@ difficulty_type Blockchain::get_difficulty_for_next_block() } else if (version == 9) { diff = next_difficulty_v3(timestamps, difficulties); } else if (version == 8) { - diff = next_difficulty_v2(timestamps, difficulties); + diff = next_difficulty_v2(timestamps, difficulties, target); } else { diff = next_difficulty(timestamps, difficulties, target); } @@ -1177,7 +1177,7 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std: } else if (version == 9) { return next_difficulty_v3(timestamps, cumulative_difficulties); } else if (version == 8) { - return next_difficulty_v2(timestamps, cumulative_difficulties); + return next_difficulty_v2(timestamps, cumulative_difficulties, target); } else { return next_difficulty(timestamps, cumulative_difficulties, target); }