From c2c02f3fa99385f5a5be722fda6f71522c93bdaa Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 28 Jun 2014 14:03:06 +0200 Subject: Move UpdateTime to pow --- src/pow.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/pow.cpp') diff --git a/src/pow.cpp b/src/pow.cpp index c0d0a7ca2..a99c582d7 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -8,6 +8,7 @@ #include "chainparams.h" #include "core.h" #include "main.h" +#include "timedata.h" #include "uint256.h" unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock) @@ -117,3 +118,12 @@ unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime) bnResult = bnLimit; return bnResult.GetCompact(); } + +void UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev) +{ + pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); + + // Updating time can change work required on testnet: + if (Params().AllowMinDifficultyBlocks()) + pblock->nBits = GetNextWorkRequired(pindexPrev, pblock); +} -- cgit v1.2.3 From b343c1a1e34f851e70649ad1f49855a7d878f9ef Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 5 Jul 2014 12:05:33 +0200 Subject: Move CBlockIndex::GetBlockWork() to pow::GetProofIncrement(nBits) --- src/pow.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/pow.cpp') diff --git a/src/pow.cpp b/src/pow.cpp index a99c582d7..d76928bda 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -127,3 +127,18 @@ void UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev) if (Params().AllowMinDifficultyBlocks()) pblock->nBits = GetNextWorkRequired(pindexPrev, pblock); } + +uint256 GetProofIncrement(unsigned int nBits) +{ + uint256 bnTarget; + bool fNegative; + bool fOverflow; + bnTarget.SetCompact(nBits, &fNegative, &fOverflow); + if (fNegative || fOverflow || bnTarget == 0) + return 0; + // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 + // as it's too large for a uint256. However, as 2**256 is at least as large + // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1, + // or ~bnTarget / (nTarget+1) + 1. + return (~bnTarget / (bnTarget + 1)) + 1; +} -- cgit v1.2.3 From 654871d43677947d124673c9e0dd2984f0d3ca61 Mon Sep 17 00:00:00 2001 From: jtimon Date: Fri, 20 Jun 2014 20:55:42 +0200 Subject: replace ComputeMinWork with CheckMinWork --- src/pow.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src/pow.cpp') diff --git a/src/pow.cpp b/src/pow.cpp index d76928bda..1d2b743b4 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -94,29 +94,36 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits) } // -// minimum amount of work that could possibly be required nTime after -// minimum work required was nBase +// true if nBits is greater than the minimum amount of work that could +// possibly be required deltaTime after minimum work required was nBase // -unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime) +bool CheckMinWork(unsigned int nBits, unsigned int nBase, int64_t deltaTime) { + bool fOverflow = false; + uint256 bnNewBlock; + bnNewBlock.SetCompact(nBits, NULL, &fOverflow); + if (fOverflow) + return false; + const uint256 &bnLimit = Params().ProofOfWorkLimit(); // Testnet has min-difficulty blocks // after Params().TargetSpacing()*2 time between blocks: - if (Params().AllowMinDifficultyBlocks() && nTime > Params().TargetSpacing()*2) - return bnLimit.GetCompact(); + if (Params().AllowMinDifficultyBlocks() && deltaTime > Params().TargetSpacing()*2) + return bnNewBlock <= bnLimit; uint256 bnResult; bnResult.SetCompact(nBase); - while (nTime > 0 && bnResult < bnLimit) + while (deltaTime > 0 && bnResult < bnLimit) { // Maximum 400% adjustment... bnResult *= 4; // ... in best-case exactly 4-times-normal target time - nTime -= Params().TargetTimespan()*4; + deltaTime -= Params().TargetTimespan()*4; } if (bnResult > bnLimit) bnResult = bnLimit; - return bnResult.GetCompact(); + + return bnNewBlock <= bnResult; } void UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev) -- cgit v1.2.3