diff --git a/src/cryptonote_basic/difficulty.cpp b/src/cryptonote_basic/difficulty.cpp index da73987b2..0e738bcfc 100644 --- a/src/cryptonote_basic/difficulty.cpp +++ b/src/cryptonote_basic/difficulty.cpp @@ -201,7 +201,7 @@ namespace cryptonote { return check_hash_128(hash, difficulty); } - difficulty_type next_difficulty(std::vector timestamps, network_type m_nettype, std::vector cumulative_difficulties, size_t target_seconds, uint64_t HEIGHT) { + difficulty_type next_difficulty(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds, uint64_t HEIGHT) { //cutoff DIFFICULTY_LAG if(timestamps.size() > DIFFICULTY_WINDOW) { @@ -215,9 +215,7 @@ namespace cryptonote { if (length <= 1) { return 1; } - if (HEIGHT < 10 && HEIGHT > 2 && m_nettype == TESTNET) { return 500; } - if (HEIGHT <= 55 + DIFFICULTY_WINDOW && HEIGHT >= 55 && m_nettype == TESTNET) { return 1337; } - if (HEIGHT <= DIFFICULTY_RESET_HEIGHT + DIFFICULTY_WINDOW && HEIGHT >= DIFFICULTY_RESET_HEIGHT && m_nettype == MAINNET) { return DIFFICULTY_RESET_LEVEL; } + if (HEIGHT <= DIFFICULTY_RESET_HEIGHT + DIFFICULTY_WINDOW && HEIGHT >= DIFFICULTY_RESET_HEIGHT) { return DIFFICULTY_RESET_LEVEL; } static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small"); assert(length <= DIFFICULTY_WINDOW); sort(timestamps.begin(), timestamps.end()); @@ -261,21 +259,18 @@ 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, network_type m_nettype, std::vector cumulative_difficulties, size_t target_seconds, uint64_t HEIGHT) { + difficulty_type next_difficulty_v2(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds, uint64_t HEIGHT) { const int64_t T = static_cast(target_seconds); size_t N = DIFFICULTY_WINDOW_V2; - if (m_nettype == MAINNET) { - if (timestamps.size() < 4) { - return 1; - } else if ( timestamps.size() < N+1 ) { + if (timestamps.size() < 4) { + return 1; + } else if ( timestamps.size() < N+1 ) { N = timestamps.size() - 1; - } else { + } else { timestamps.resize(N+1); cumulative_difficulties.resize(N+1); - } } - if (HEIGHT < 200 && m_nettype == TESTNET) { return 500; } 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); @@ -298,13 +293,12 @@ namespace cryptonote { } // LWMA-2 - difficulty_type next_difficulty_v3(std::vector timestamps, network_type m_nettype, std::vector cumulative_difficulties, uint64_t HEIGHT) { + difficulty_type next_difficulty_v3(std::vector timestamps, std::vector cumulative_difficulties, uint64_t HEIGHT) { 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) ); - if (HEIGHT < 200 && m_nettype == TESTNET) { return 500; } 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)); @@ -323,14 +317,13 @@ namespace cryptonote { } // LWMA-4 - difficulty_type next_difficulty_v4(std::vector timestamps, network_type m_nettype, std::vector cumulative_difficulties, uint64_t HEIGHT) { + difficulty_type next_difficulty_v4(std::vector timestamps, std::vector cumulative_difficulties, uint64_t HEIGHT) { uint64_t T = DIFFICULTY_TARGET_V2; uint64_t N = DIFFICULTY_WINDOW_V2; uint64_t L(0), ST(0), next_D, prev_D, avg_D, i; assert(timestamps.size() == cumulative_difficulties.size() && timestamps.size() <= N+1 ); - if (HEIGHT <= 63469 + 1 && m_nettype == MAINNET) { return 100000069; } - if (HEIGHT < 200 && m_nettype == TESTNET) { return 500; } + if (HEIGHT <= 63469 + 1) { return 100000069; } std::vectorTS(N+1); TS[0] = timestamps[0]; for ( i = 1; i <= N; i++) { @@ -372,11 +365,10 @@ namespace cryptonote { // LWMA-1 difficulty algorithm // Copyright (c) 2017-2019 Zawy, MIT License // https://github.com/zawy12/difficulty-algorithms/issues/3 - difficulty_type next_difficulty_v5(std::vector timestamps, network_type m_nettype, std::vector cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT) { + difficulty_type next_difficulty_v5(std::vector timestamps, std::vector cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT) { assert(timestamps.size() == cumulative_difficulties.size() && timestamps.size() <= N+1 ); - if (HEIGHT >= 81769 && HEIGHT < 81769 + N && m_nettype == MAINNET) { return 10000000; } - if (HEIGHT < 200 && m_nettype == TESTNET) { return 500; } + if (HEIGHT >= 81769 && HEIGHT < 81769 + N) { return 10000000; } assert(timestamps.size() == N+1); // hardcoding previously erroneously calculated difficulty entries diff --git a/src/cryptonote_basic/difficulty.h b/src/cryptonote_basic/difficulty.h index d8d2612d5..226c30a63 100644 --- a/src/cryptonote_basic/difficulty.h +++ b/src/cryptonote_basic/difficulty.h @@ -57,11 +57,11 @@ 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, network_type m_nettype, std::vector cumulative_difficulties, size_t target_seconds, uint64_t HEIGHT); - difficulty_type next_difficulty_v2(std::vector timestamps, network_type m_nettype, std::vector cumulative_difficulties, size_t target_seconds, uint64_t HEIGHT); - difficulty_type next_difficulty_v3(std::vector timestamps, network_type m_nettype, std::vector cumulative_difficulties, uint64_t HEIGHT); - difficulty_type next_difficulty_v4(std::vector timestamps, network_type m_nettype, std::vector cumulative_difficulties, uint64_t HEIGHT); - difficulty_type next_difficulty_v5(std::vector timestamps, network_type m_nettype, std::vector cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT); + difficulty_type next_difficulty(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds, uint64_t HEIGHT); + difficulty_type next_difficulty_v2(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds, uint64_t HEIGHT); + difficulty_type next_difficulty_v3(std::vector timestamps, std::vector cumulative_difficulties, uint64_t HEIGHT); + difficulty_type next_difficulty_v4(std::vector timestamps, std::vector cumulative_difficulties, uint64_t HEIGHT); + difficulty_type next_difficulty_v5(std::vector timestamps, std::vector cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT); std::string hex(difficulty_type v); } diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 538d955c1..0ec7740f4 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -946,18 +946,18 @@ start: uint64_t N = DIFFICULTY_WINDOW_V3; uint64_t HEIGHT = m_db->height(); - difficulty_type diff = next_difficulty(timestamps, m_nettype, difficulties, target, HEIGHT); + difficulty_type diff = next_difficulty(timestamps, difficulties, target, HEIGHT); if (version <= 17 && version >= 11) { - diff = next_difficulty_v5(timestamps, m_nettype, difficulties, T, N, HEIGHT); + diff = next_difficulty_v5(timestamps, difficulties, T, N, HEIGHT); } else if (version == 10) { - diff = next_difficulty_v4(timestamps, m_nettype, difficulties, HEIGHT); + diff = next_difficulty_v4(timestamps, difficulties, HEIGHT); } else if (version == 9) { - diff = next_difficulty_v3(timestamps, m_nettype, difficulties, HEIGHT); + diff = next_difficulty_v3(timestamps, difficulties, HEIGHT); } else if (version == 8) { - diff = next_difficulty_v2(timestamps, m_nettype, difficulties, target, HEIGHT); + diff = next_difficulty_v2(timestamps, difficulties, target, HEIGHT); } else { - diff = next_difficulty(timestamps, m_nettype, difficulties, target, HEIGHT); + diff = next_difficulty(timestamps, difficulties, target, HEIGHT); } CRITICAL_REGION_LOCAL1(m_difficulty_lock); @@ -1041,18 +1041,18 @@ size_t Blockchain::recalculate_difficulties(boost::optional start_heig uint64_t N = DIFFICULTY_WINDOW_V3; uint64_t HEIGHT = m_db->height(); size_t target = get_ideal_hard_fork_version(height) < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2; - difficulty_type recalculated_diff = next_difficulty(timestamps, m_nettype, difficulties, target, HEIGHT); + difficulty_type recalculated_diff = next_difficulty(timestamps, difficulties, target, HEIGHT); if (version <= 17 && version >= 11) { - recalculated_diff = next_difficulty_v5(timestamps, m_nettype, difficulties, T, N, HEIGHT); + recalculated_diff = next_difficulty_v5(timestamps, difficulties, T, N, HEIGHT); } else if (version == 10) { - recalculated_diff = next_difficulty_v4(timestamps, m_nettype, difficulties, HEIGHT); + recalculated_diff = next_difficulty_v4(timestamps, difficulties, HEIGHT); } else if (version == 9) { - recalculated_diff = next_difficulty_v3(timestamps, m_nettype, difficulties, HEIGHT); + recalculated_diff = next_difficulty_v3(timestamps, difficulties, HEIGHT); } else if (version == 8) { - recalculated_diff = next_difficulty_v2(timestamps, m_nettype, difficulties, target, HEIGHT); + recalculated_diff = next_difficulty_v2(timestamps, difficulties, target, HEIGHT); } else { - recalculated_diff = next_difficulty(timestamps, m_nettype, difficulties, target, HEIGHT); + recalculated_diff = next_difficulty(timestamps, difficulties, target, HEIGHT); } boost::multiprecision::uint256_t recalculated_cum_diff_256 = boost::multiprecision::uint256_t(recalculated_diff) + last_cum_diff; @@ -1363,17 +1363,17 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std: // calculate the difficulty target for the block and return it if (version <= 17 && version >= 11) { - return next_difficulty_v5(timestamps, m_nettype, cumulative_difficulties, T, N, HEIGHT); + return next_difficulty_v5(timestamps, cumulative_difficulties, T, N, HEIGHT); } else if (version == 10) { - return next_difficulty_v4(timestamps, m_nettype, cumulative_difficulties, HEIGHT); + return next_difficulty_v4(timestamps, cumulative_difficulties, HEIGHT); } else if (version == 9) { - return next_difficulty_v3(timestamps, m_nettype, cumulative_difficulties, HEIGHT); + return next_difficulty_v3(timestamps, cumulative_difficulties, HEIGHT); } else if (version == 8) { - return next_difficulty_v2(timestamps, m_nettype, cumulative_difficulties, target, HEIGHT); + return next_difficulty_v2(timestamps, cumulative_difficulties, target, HEIGHT); } else { - return next_difficulty(timestamps, m_nettype, cumulative_difficulties, target, HEIGHT); + return next_difficulty(timestamps, cumulative_difficulties, target, HEIGHT); } - return next_difficulty(timestamps, m_nettype, cumulative_difficulties, target, HEIGHT); + return next_difficulty(timestamps, cumulative_difficulties, target, HEIGHT); } //------------------------------------------------------------------ // This function does a sanity check on basic things that all miner