diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-02-02 14:15:36 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-02-02 14:16:16 +0100 |
commit | 47ac04e8b10e7fb9db99d7c3184368c9c221ea78 (patch) | |
tree | d9a4f6ad10cc25401c5edb6abca7d343d6ea35d6 /src/chain.cpp | |
parent | 3dc3149e63934f6ba9d030aec6dcfe839e592b9a (diff) | |
parent | e86756193ebdbf71504e2a1a8db43e38d57f9673 (diff) | |
download | bitcoin-47ac04e8b10e7fb9db99d7c3184368c9c221ea78.tar.xz |
Merge #7311: MOVEONLY: Move non-consensus functions out of pow
e867561 MOVEONLY: non-consensus: from pow to chain: (Jorge Timón)
Diffstat (limited to 'src/chain.cpp')
-rw-r--r-- | src/chain.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/chain.cpp b/src/chain.cpp index 3450ed6c3f..32f6480f84 100644 --- a/src/chain.cpp +++ b/src/chain.cpp @@ -110,3 +110,35 @@ void CBlockIndex::BuildSkip() if (pprev) pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); } + +arith_uint256 GetBlockProof(const CBlockIndex& block) +{ + arith_uint256 bnTarget; + bool fNegative; + bool fOverflow; + bnTarget.SetCompact(block.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 arith_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; +} + +int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params) +{ + arith_uint256 r; + int sign = 1; + if (to.nChainWork > from.nChainWork) { + r = to.nChainWork - from.nChainWork; + } else { + r = from.nChainWork - to.nChainWork; + sign = -1; + } + r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip); + if (r.bits() > 63) { + return sign * std::numeric_limits<int64_t>::max(); + } + return sign * r.GetLow64(); +} |