From e4ddf841ee632087f86d6152162010ca26026afd Mon Sep 17 00:00:00 2001 From: wowario Date: Thu, 7 Feb 2019 17:02:29 +0300 Subject: [PATCH] add lwma-1 --- src/cryptonote_basic/difficulty.cpp | 65 ++++++++++++++--------------- src/cryptonote_basic/difficulty.h | 2 +- src/cryptonote_config.h | 5 +-- src/cryptonote_core/blockchain.cpp | 21 +++++++--- 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/cryptonote_basic/difficulty.cpp b/src/cryptonote_basic/difficulty.cpp index 88952a87b..1515e3ad4 100644 --- a/src/cryptonote_basic/difficulty.cpp +++ b/src/cryptonote_basic/difficulty.cpp @@ -269,43 +269,40 @@ namespace cryptonote { return static_cast(next_D); } - difficulty_type next_difficulty_v5(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds) { - - if(timestamps.size() > DIFFICULTY_WINDOW_V3) - { - timestamps.resize(DIFFICULTY_WINDOW_V3); - cumulative_difficulties.resize(DIFFICULTY_WINDOW_V3); - } + // 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, std::vector cumulative_difficulties, uint64_t T, uint64_t N, uint64_t HEIGHT, uint64_t FORK_HEIGHT, uint64_t difficulty_guess) { + assert(timestamps.size() == cumulative_difficulties.size() && timestamps.size() <= N+1 ); - size_t length = timestamps.size(); - assert(length == cumulative_difficulties.size()); - if (length < DIFFICULTY_FORK_HEIGHT + 72) { - return DIFFICULTY_RESET; - } - static_assert(DIFFICULTY_WINDOW_V3 >= 2, "Window is too small"); - assert(length <= DIFFICULTY_WINDOW_V3); - sort(timestamps.begin(), timestamps.end()); - size_t cut_begin, cut_end; - static_assert(2 * DIFFICULTY_CUT_V2 <= DIFFICULTY_WINDOW_V3 - 2, "Cut length is too large"); - if (length <= DIFFICULTY_WINDOW_V3 - 2 * DIFFICULTY_CUT_V2) { - cut_begin = 0; - cut_end = length; - } else { - cut_begin = (length - (DIFFICULTY_WINDOW_V3 - 2 * DIFFICULTY_CUT_V2) + 1) / 2; - cut_end = cut_begin + (DIFFICULTY_WINDOW_V3 - 2 * DIFFICULTY_CUT_V2); + if (HEIGHT >= FORK_HEIGHT && HEIGHT < FORK_HEIGHT + N) { return difficulty_guess; } + assert(timestamps.size() == N+1); + + uint64_t L(0), next_D, i, this_timestamp(0), previous_timestamp(0), avg_D; + + previous_timestamp = timestamps[0]-T; + for ( i = 1; i <= N; i++) { + // Safely prevent out-of-sequence timestamps + if ( timestamps[i] > previous_timestamp ) { this_timestamp = timestamps[i]; } + else { this_timestamp = previous_timestamp+1; } + L += i*std::min(6*T ,this_timestamp - previous_timestamp); + previous_timestamp = this_timestamp; } - assert(/*cut_begin >= 0 &&*/ cut_begin + 2 <= cut_end && cut_end <= length); - uint64_t time_span = timestamps[cut_end - 1] - timestamps[cut_begin]; - if (time_span == 0) { - time_span = 1; + if (L < N*N*T/20 ) { L = N*N*T/20; } + avg_D = ( cumulative_difficulties[N] - cumulative_difficulties[0] )/ N; + + // Prevent round off error for small D and overflow for large D. + if (avg_D > 2000000*N*N*T) { + next_D = (avg_D/(200*L))*(N*(N+1)*T*99); } - difficulty_type total_work = cumulative_difficulties[cut_end - 1] - cumulative_difficulties[cut_begin]; - assert(total_work > 0); - uint64_t low, high; - mul(total_work, target_seconds, low, high); - if (high != 0 || low + time_span - 1 < low) { - return 0; + else { next_D = (avg_D*N*(N+1)*T*99)/(200*L); } + + // Make all insignificant digits zero for easy reading. + i = 1000000000; + while (i > 1) { + if ( next_D > i*100 ) { next_D = ((next_D+i/2)/i)*i; break; } + else { i /= 10; } } - return (low + time_span - 1) / time_span; + return next_D; } } diff --git a/src/cryptonote_basic/difficulty.h b/src/cryptonote_basic/difficulty.h index ffe3708b8..76a31586d 100644 --- a/src/cryptonote_basic/difficulty.h +++ b/src/cryptonote_basic/difficulty.h @@ -56,5 +56,5 @@ namespace cryptonote 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, size_t target_seconds); + 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_config.h b/src/cryptonote_config.h index b80e71d0d..1d1a0e177 100644 --- a/src/cryptonote_config.h +++ b/src/cryptonote_config.h @@ -49,7 +49,6 @@ #define CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT 60*60*2 #define CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE 4 -#define BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V3 12 #define BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 11 #define BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW 60 @@ -83,11 +82,9 @@ #define DIFFICULTY_WINDOW_V3 144 #define DIFFICULTY_WINDOW_V2 60 #define DIFFICULTY_WINDOW 720 // blocks -#define DIFFICULTY_LAG_V2 3 #define DIFFICULTY_LAG 15 // !!! -#define DIFFICULTY_CUT_V2 12 #define DIFFICULTY_CUT 60 // timestamps to cut after sorting -#define DIFFICULTY_BLOCKS_COUNT_V3 DIFFICULTY_WINDOW_V3 + DIFFICULTY_LAG_V2 +#define DIFFICULTY_BLOCKS_COUNT_V3 DIFFICULTY_WINDOW_V3 + 1 // added +1 to make N=N #define DIFFICULTY_BLOCKS_COUNT_V2 DIFFICULTY_WINDOW_V2 + 1 // added +1 to make N=N #define DIFFICULTY_BLOCKS_COUNT DIFFICULTY_WINDOW + DIFFICULTY_LAG #define DIFFICULTY_FORK_HEIGHT 81769 // ~14 February 2019 diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index bb9968049..dc78e0e66 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -880,10 +880,16 @@ difficulty_type Blockchain::get_difficulty_for_next_block() m_difficulties = difficulties; } size_t target = get_difficulty_target(); + uint64_t T = DIFFICULTY_TARGET_V2; + uint64_t N = DIFFICULTY_WINDOW_V3; + uint64_t HEIGHT = m_db->height(); + uint64_t FORK_HEIGHT = DIFFICULTY_FORK_HEIGHT; + uint64_t difficulty_guess = DIFFICULTY_RESET; + difficulty_type diff = next_difficulty(timestamps, difficulties, target); if (version >= 11) { - diff = next_difficulty_v5(timestamps, difficulties, target); + diff = next_difficulty_v5(timestamps, difficulties, T, N, HEIGHT, FORK_HEIGHT, difficulty_guess); } else if (version == 10) { diff = next_difficulty_v4(timestamps, difficulties, height); } else if (version == 9) { @@ -1130,10 +1136,15 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std: // FIXME: This will fail if fork activation heights are subject to voting size_t target = get_ideal_hard_fork_version(bei.height) < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2; + uint64_t T = DIFFICULTY_TARGET_V2; + uint64_t N = DIFFICULTY_WINDOW_V3; + uint64_t HEIGHT = m_db->height(); + uint64_t FORK_HEIGHT = DIFFICULTY_FORK_HEIGHT; + uint64_t difficulty_guess = DIFFICULTY_RESET; // calculate the difficulty target for the block and return it if (version >= 11) { - return next_difficulty_v5(timestamps, cumulative_difficulties, target); + return next_difficulty_v5(timestamps, cumulative_difficulties, T, N, HEIGHT, FORK_HEIGHT, difficulty_guess); } else if (version == 10) { return next_difficulty_v4(timestamps, cumulative_difficulties, height); } else if (version == 9) { @@ -1452,7 +1463,7 @@ bool Blockchain::complete_timestamps_vector(uint64_t start_top_height, std::vect { LOG_PRINT_L3("Blockchain::" << __func__); uint8_t version = get_current_hard_fork_version(); - size_t blockchain_timestamp_check_window = version >= 11 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V3 : version == 10 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW; + size_t blockchain_timestamp_check_window = version >= 10 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW; if(timestamps.size() >= blockchain_timestamp_check_window) return true; @@ -3217,7 +3228,7 @@ bool Blockchain::check_block_timestamp(std::vector& timestamps, const LOG_PRINT_L3("Blockchain::" << __func__); median_ts = epee::misc_utils::median(timestamps); uint8_t version = get_current_hard_fork_version(); - size_t blockchain_timestamp_check_window = version >= 11 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V3 : version == 10 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW; + size_t blockchain_timestamp_check_window = version >= 10 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW; if(b.timestamp < median_ts) { MERROR_VER("Timestamp of block with id: " << get_block_hash(b) << ", " << b.timestamp << ", less than median of last " << blockchain_timestamp_check_window << " blocks, " << median_ts); @@ -3239,7 +3250,7 @@ bool Blockchain::check_block_timestamp(const block& b, uint64_t& median_ts) cons LOG_PRINT_L3("Blockchain::" << __func__); uint8_t version = get_current_hard_fork_version(); uint64_t cryptonote_block_future_time_limit = version >= 8 ? CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT_V2 : CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT; - size_t blockchain_timestamp_check_window = version >= 11 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V3 : version == 10 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW; + size_t blockchain_timestamp_check_window = version >= 10 ? BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW_V2 : BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW; if(b.timestamp > get_adjusted_time() + cryptonote_block_future_time_limit) {