From c5d51ca56cd6ded8c5490c2d53154a1e3b383132 Mon Sep 17 00:00:00 2001 From: wowario Date: Sun, 27 May 2018 20:57:24 +0300 Subject: [PATCH] add lwma-2 --- src/cryptonote_basic/difficulty.cpp | 33 +++++++++++++++++++++++++++++ src/cryptonote_basic/difficulty.h | 1 + src/cryptonote_core/blockchain.cpp | 12 +++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/cryptonote_basic/difficulty.cpp b/src/cryptonote_basic/difficulty.cpp index b1862fc89..9b188f4d3 100644 --- a/src/cryptonote_basic/difficulty.cpp +++ b/src/cryptonote_basic/difficulty.cpp @@ -240,4 +240,37 @@ namespace cryptonote { next_difficulty = static_cast(nextDifficulty); return next_difficulty; } + +// LWMA-2 difficulty algorithm +// Copyright (c) 2017-2018 Zawy, MIT License +// See commented version in https://github.com/zawy12/difficulty-algorithms/issues/3 +// for required config file changes. + +difficulty_type next_difficulty_v3(std::vector timestamps, std::vector cumulative_difficulties) { + + int64_t T = DIFFICULTY_TARGET_V2; + int64_t N = DIFFICULTY_WINDOW_V2; + int64_t FTL = CRYPTONOTE_BLOCK_FUTURE_TIME_LIMIT_V3; + int64_t L(0), ST, sum_3_ST(0), next_D, prev_D; + + // TODO: change initial_difficulty_guess before v9 mainnet hard fork + // if ( height >= fork_height && height <= fork_height+N ) { return difficulty_guess; } + uint64_t initial_difficulty_guess = 100; + if (timestamps.size() <= static_cast(N)) { + return initial_difficulty_guess; + } + + for ( int64_t i = 1; i <= N; i++) { + ST = std::max(-FTL, std::min( (int64_t)(timestamps[i]) - (int64_t)(timestamps[i-1]), 6*T)); + L += ST * i ; + if ( i > N-3 ) { sum_3_ST += ST; } + } + + next_D = ((cumulative_difficulties[N] - cumulative_difficulties[0])*T*(N+1)*99)/(100*2*L); + + prev_D = cumulative_difficulties[N] - cumulative_difficulties[N-1]; + if ( sum_3_ST < (8*T)/10) { next_D = (prev_D*110)/100; } + + return static_cast(next_D); + } } diff --git a/src/cryptonote_basic/difficulty.h b/src/cryptonote_basic/difficulty.h index 9da643f39..d4f258136 100644 --- a/src/cryptonote_basic/difficulty.h +++ b/src/cryptonote_basic/difficulty.h @@ -54,4 +54,5 @@ namespace cryptonote 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, size_t target_seconds); + difficulty_type next_difficulty_v3(std::vector timestamps, std::vector cumulative_difficulties); } diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index e84518af0..cb23293f1 100755 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -833,8 +833,12 @@ difficulty_type Blockchain::get_difficulty_for_next_block() size_t target = DIFFICULTY_TARGET_V2; if (version == 7) { return next_difficulty(timestamps, difficulties, target); - } else { + } + else if (version == 8) { return next_difficulty_v2(timestamps, difficulties, target); + } + else { + return next_difficulty_v3(timestamps, difficulties); } } //------------------------------------------------------------------ @@ -1047,8 +1051,12 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std: // calculate the difficulty target for the block and return it if (version == 7) { return next_difficulty(timestamps, cumulative_difficulties, target); - } else { + } + else if (version == 8) { return next_difficulty_v2(timestamps, cumulative_difficulties, target); + } + else { + return next_difficulty_v3(timestamps, cumulative_difficulties); } }