From 231db5270acb2e673a641a1800be910ce345668a Mon Sep 17 00:00:00 2001 From: maksymivv Date: Fri, 18 Nov 2016 11:18:23 +0200 Subject: [PATCH] Difficulty algo Difficulty algo cleanup, based on suggestions by Eugene --- ReleaseNotes.txt | 8 +++++++ src/CryptoNoteCore/Currency.cpp | 37 +++++++++++++++------------------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/ReleaseNotes.txt b/ReleaseNotes.txt index 324f87fb8b..26ec84094f 100644 --- a/ReleaseNotes.txt +++ b/ReleaseNotes.txt @@ -1,3 +1,11 @@ +Release notes Karbowanec 1.3.0 + +- Bytecoin core 1.0.11 transition +- New difficulty algorithm +- Tail emission +- Daemon restricted RPC mode +- Fees for open remote node + Release notes 1.0.11 - New Bytecoin Wallet file format diff --git a/src/CryptoNoteCore/Currency.cpp b/src/CryptoNoteCore/Currency.cpp index 8b4e265370..f02a9caf7a 100755 --- a/src/CryptoNoteCore/Currency.cpp +++ b/src/CryptoNoteCore/Currency.cpp @@ -407,10 +407,13 @@ namespace CryptoNote { difficulty_type Currency::nextDifficulty(uint8_t blockMajorVersion, std::vector timestamps, std::vector cumulativeDifficulties) const { - if (blockMajorVersion >= BLOCK_MAJOR_VERSION_2) { + // new difficulty calculation + // based on Zawy difficulty algorithm v1.0 + // next Diff = Avg past N Diff * TargetInterval / Avg past N solve times + // as described at https://github.com/monero-project/research-lab/issues/3 + // Window time span and total difficulty is taken instead of average as suggested by Eugene - // default CN with smaller window DIFFICULTY_WINDOW_V2 - // without DIFFICULTY_CUT it gives very similar results to the Zawy's formula below + if (blockMajorVersion >= BLOCK_MAJOR_VERSION_2) { size_t m_difficultyWindow_2 = CryptoNote::parameters::DIFFICULTY_WINDOW_V2; assert(m_difficultyWindow_2 >= 2); @@ -429,37 +432,31 @@ namespace CryptoNote { sort(timestamps.begin(), timestamps.end()); - /* uint64_t timeSpan = timestamps[length - 1] - timestamps[0]; + uint64_t timeSpan = timestamps.back() - timestamps.front(); if (timeSpan == 0) { timeSpan = 1; } - difficulty_type totalWork = cumulativeDifficulties[length - 1] - cumulativeDifficulties[0]; + difficulty_type totalWork = cumulativeDifficulties.back() - cumulativeDifficulties.front(); assert(totalWork > 0); - uint64_t low, high; + // uint64_t nextDiffZ = totalWork * m_difficultyTarget / timeSpan; + + uint64_t low, high; low = mul128(totalWork, m_difficultyTarget, &high); + // blockchain error "Difficulty overhead" if this function returns zero if (high != 0 || low + timeSpan - 1 < low) { return 0; - } - - uint64_t nextDiffAlt = (low + timeSpan - 1) / timeSpan; */ - // return nextDiffAlt; - - // Zawy difficulty algorithm v1.0 - // next Diff = Avg past N Diff * TargetInterval / Avg past N solve times - // this gives almost same results as modified CN version without cut above + } - uint64_t avgWindowDiff = (cumulativeDifficulties.back() - cumulativeDifficulties.front()) / cumulativeDifficulties.size(); - uint64_t avgSolveTime = (timestamps.back() - timestamps.front()) / timestamps.size(); - uint64_t nextDiffZ = avgWindowDiff * m_difficultyTarget / avgSolveTime; + uint64_t nextDiffZ = low / timeSpan; + // minimum limit if (nextDiffZ <= 100000) { - nextDiffZ = 100000; + nextDiffZ = 100000; } - + return nextDiffZ; - // end of new difficulty calculation