From 29842505cc0dfe1b346cd189463896fd701a0d68 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 5 Sep 2016 22:25:11 +1200 Subject: [PATCH] Adjust from average difficulty instead of previous difficulty --- src/pow.cpp | 26 ++++++++++++++++++-------- src/pow.h | 4 +++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/pow.cpp b/src/pow.cpp index 9371863c4..c45a95a9d 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -43,7 +43,15 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead // Find the first block in the averaging interval const CBlockIndex* pindexFirst = pindexLast; + arith_uint256 bnAvg; + arith_uint256 bnTmp; for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) { + if (i == 0) { + bnAvg.SetCompact(pindexFirst->nBits); + } else { + bnTmp.SetCompact(pindexFirst->nBits); + bnAvg = ((bnAvg * i) + bnTmp) / (i + 1); + } pindexFirst = pindexFirst->pprev; } @@ -51,15 +59,20 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead if (pindexFirst == NULL) return nProofOfWorkLimit; - return CalculateNextWorkRequired(pindexBits->nBits, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); + return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params); } +// Left for testing purposes unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) { - return CalculateNextWorkRequired(pindexLast->nBits, pindexLast->GetMedianTimePast(), nFirstBlockTime, params); + arith_uint256 bnAvg; + bnAvg.SetCompact(pindexLast->nBits); + return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), nFirstBlockTime, params); } -unsigned int CalculateNextWorkRequired(uint32_t nBits, int64_t nLastBlockTime, int64_t nFirstBlockTime, const Consensus::Params& params) +unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, + int64_t nLastBlockTime, int64_t nFirstBlockTime, + const Consensus::Params& params) { // Limit adjustment step // Use medians to prevent time-warp attacks @@ -75,10 +88,7 @@ unsigned int CalculateNextWorkRequired(uint32_t nBits, int64_t nLastBlockTime, i // Retarget const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); - arith_uint256 bnNew; - arith_uint256 bnOld; - bnNew.SetCompact(nBits); - bnOld = bnNew; + arith_uint256 bnNew {bnAvg}; bnNew /= params.AveragingWindowTimespan(); bnNew *= nActualTimespan; @@ -88,7 +98,7 @@ unsigned int CalculateNextWorkRequired(uint32_t nBits, int64_t nLastBlockTime, i /// debug print LogPrint("pow", "GetNextWorkRequired RETARGET\n"); LogPrint("pow", "params.AveragingWindowTimespan() = %d nActualTimespan = %d\n", params.AveragingWindowTimespan(), nActualTimespan); - LogPrint("pow", "Before: %08x %s\n", nBits, bnOld.ToString()); + LogPrint("pow", "Current average: %08x %s\n", bnAvg.GetCompact(), bnAvg.ToString()); LogPrint("pow", "After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); return bnNew.GetCompact(); diff --git a/src/pow.h b/src/pow.h index 89f131a8d..99f53c395 100644 --- a/src/pow.h +++ b/src/pow.h @@ -18,7 +18,9 @@ class arith_uint256; unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&); unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&); -unsigned int CalculateNextWorkRequired(uint32_t nBits, int64_t nLastBlockTime, int64_t nFirstBlockTime, const Consensus::Params&); +unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg, + int64_t nLastBlockTime, int64_t nFirstBlockTime, + const Consensus::Params&); /** Check whether the Equihash solution in a block header is valid */ bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams&);