Skip to content

Commit e867561

Browse files
committed
MOVEONLY: non-consensus: from pow to chain:
- GetBlockProof - GetBlockProofEquivalentTime
1 parent 3cd836c commit e867561

File tree

4 files changed

+36
-37
lines changed

4 files changed

+36
-37
lines changed

src/chain.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,35 @@ void CBlockIndex::BuildSkip()
110110
if (pprev)
111111
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
112112
}
113+
114+
arith_uint256 GetBlockProof(const CBlockIndex& block)
115+
{
116+
arith_uint256 bnTarget;
117+
bool fNegative;
118+
bool fOverflow;
119+
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
120+
if (fNegative || fOverflow || bnTarget == 0)
121+
return 0;
122+
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
123+
// as it's too large for a arith_uint256. However, as 2**256 is at least as large
124+
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
125+
// or ~bnTarget / (nTarget+1) + 1.
126+
return (~bnTarget / (bnTarget + 1)) + 1;
127+
}
128+
129+
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
130+
{
131+
arith_uint256 r;
132+
int sign = 1;
133+
if (to.nChainWork > from.nChainWork) {
134+
r = to.nChainWork - from.nChainWork;
135+
} else {
136+
r = from.nChainWork - to.nChainWork;
137+
sign = -1;
138+
}
139+
r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
140+
if (r.bits() > 63) {
141+
return sign * std::numeric_limits<int64_t>::max();
142+
}
143+
return sign * r.GetLow64();
144+
}

src/chain.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ class CBlockIndex
282282
const CBlockIndex* GetAncestor(int height) const;
283283
};
284284

285+
arith_uint256 GetBlockProof(const CBlockIndex& block);
286+
/** Return the time it would take to redo the work difference between from and to, assuming the current hashrate corresponds to the difficulty at tip, in seconds. */
287+
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params&);
288+
285289
/** Used to marshal pointers into hashes for db storage. */
286290
class CDiskBlockIndex : public CBlockIndex
287291
{

src/pow.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,35 +102,3 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&
102102

103103
return true;
104104
}
105-
106-
arith_uint256 GetBlockProof(const CBlockIndex& block)
107-
{
108-
arith_uint256 bnTarget;
109-
bool fNegative;
110-
bool fOverflow;
111-
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
112-
if (fNegative || fOverflow || bnTarget == 0)
113-
return 0;
114-
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
115-
// as it's too large for a arith_uint256. However, as 2**256 is at least as large
116-
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
117-
// or ~bnTarget / (nTarget+1) + 1.
118-
return (~bnTarget / (bnTarget + 1)) + 1;
119-
}
120-
121-
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
122-
{
123-
arith_uint256 r;
124-
int sign = 1;
125-
if (to.nChainWork > from.nChainWork) {
126-
r = to.nChainWork - from.nChainWork;
127-
} else {
128-
r = from.nChainWork - to.nChainWork;
129-
sign = -1;
130-
}
131-
r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
132-
if (r.bits() > 63) {
133-
return sign * std::numeric_limits<int64_t>::max();
134-
}
135-
return sign * r.GetLow64();
136-
}

src/pow.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@
1313
class CBlockHeader;
1414
class CBlockIndex;
1515
class uint256;
16-
class arith_uint256;
1716

1817
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&);
1918
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&);
2019

2120
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
2221
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&);
23-
arith_uint256 GetBlockProof(const CBlockIndex& block);
24-
25-
/** Return the time it would take to redo the work difference between from and to, assuming the current hashrate corresponds to the difficulty at tip, in seconds. */
26-
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params&);
2722

2823
#endif // BITCOIN_POW_H

0 commit comments

Comments
 (0)