Skip to content

Commit 957f529

Browse files
committed
[Refactor] pow/pos: use arith_uint256 for difficulty targets
1 parent 9663d99 commit 957f529

File tree

8 files changed

+36
-35
lines changed

8 files changed

+36
-35
lines changed

src/chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class CBlockIndex
204204
unsigned int nUndoPos{0};
205205

206206
//! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
207-
uint256 nChainWork{};
207+
arith_uint256 nChainWork{};
208208

209209
//! Number of transactions in this block.
210210
//! Note: in a potential headers-first mode, this number cannot be relied upon

src/chainparams.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ class CMainParams : public CChainParams
134134
assert(genesis.hashMerkleRoot == uint256S("0x1b2ef6e2f28be914103a277377ae7729dcd125dfeb8bf97bd5964ba72b6dc39b"));
135135

136136
consensus.fPowAllowMinDifficultyBlocks = false;
137-
consensus.powLimit = ~UINT256_ZERO >> 20; // PIVX starting difficulty is 1 / 2^12
138-
consensus.posLimitV1 = ~UINT256_ZERO >> 24;
139-
consensus.posLimitV2 = ~UINT256_ZERO >> 20;
137+
consensus.powLimit = uint256S("0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
138+
consensus.posLimitV1 = uint256S("0x000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
139+
consensus.posLimitV2 = uint256S("0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
140140
consensus.nBudgetCycleBlocks = 43200; // approx. 1 every 30 days
141141
consensus.nBudgetFeeConfirmations = 6; // Number of confirmations for the finalization fee
142142
consensus.nCoinbaseMaturity = 100;
@@ -274,9 +274,9 @@ class CTestNetParams : public CChainParams
274274
assert(genesis.hashMerkleRoot == uint256S("0x1b2ef6e2f28be914103a277377ae7729dcd125dfeb8bf97bd5964ba72b6dc39b"));
275275

276276
consensus.fPowAllowMinDifficultyBlocks = true;
277-
consensus.powLimit = ~UINT256_ZERO >> 20; // PIVX starting difficulty is 1 / 2^12
278-
consensus.posLimitV1 = ~UINT256_ZERO >> 24;
279-
consensus.posLimitV2 = ~UINT256_ZERO >> 20;
277+
consensus.powLimit = uint256S("0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
278+
consensus.posLimitV1 = uint256S("0x000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
279+
consensus.posLimitV2 = uint256S("0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
280280
consensus.nBudgetCycleBlocks = 144; // approx 10 cycles per day
281281
consensus.nBudgetFeeConfirmations = 3; // (only 8-blocks window for finalization on testnet)
282282
consensus.nCoinbaseMaturity = 15;
@@ -397,9 +397,9 @@ class CRegTestParams : public CChainParams
397397
assert(genesis.hashMerkleRoot == uint256S("0x1b2ef6e2f28be914103a277377ae7729dcd125dfeb8bf97bd5964ba72b6dc39b"));
398398

399399
consensus.fPowAllowMinDifficultyBlocks = true;
400-
consensus.powLimit = ~UINT256_ZERO >> 20; // PIVX starting difficulty is 1 / 2^12
401-
consensus.posLimitV1 = ~UINT256_ZERO >> 24;
402-
consensus.posLimitV2 = ~UINT256_ZERO >> 20;
400+
consensus.powLimit = uint256S("0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
401+
consensus.posLimitV1 = uint256S("0x000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
402+
consensus.posLimitV2 = uint256S("0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
403403
consensus.nBudgetCycleBlocks = 144; // approx 10 cycles per day
404404
consensus.nBudgetFeeConfirmations = 3; // (only 8-blocks window for finalization on regtest)
405405
consensus.nCoinbaseMaturity = 100;

src/kernel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ uint256 CStakeKernel::GetHash() const
5959
bool CStakeKernel::CheckKernelHash(bool fSkipLog) const
6060
{
6161
// Get weighted target
62-
uint256 bnTarget;
62+
arith_uint256 bnTarget;
6363
bnTarget.SetCompact(nBits);
64-
bnTarget *= (uint256(stakeValue) / 100);
64+
bnTarget *= (arith_uint256(stakeValue) / 100);
6565

6666
// Check PoS kernel hash
67-
const uint256& hashProofOfStake = GetHash();
67+
const arith_uint256& hashProofOfStake = UintToArith256(GetHash());
6868
const bool res = hashProofOfStake < bnTarget;
6969

7070
if (!fSkipLog || res) {

src/legacy/stakemodifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static bool SelectBlockFromCandidates(
3737
bool fModifierV2 = false;
3838
bool fFirstRun = true;
3939
bool fSelected = false;
40-
uint256 hashBest;
40+
arith_uint256 hashBest = ARITH_UINT256_ZERO;
4141
*pindexSelected = (const CBlockIndex*)0;
4242
for (const auto& item : vSortedByTimestamp) {
4343
if (!mapBlockIndex.count(item.second))
@@ -65,7 +65,7 @@ static bool SelectBlockFromCandidates(
6565

6666
CDataStream ss(SER_GETHASH, 0);
6767
ss << hashProof << nStakeModifierPrev;
68-
uint256 hashSelection = Hash(ss.begin(), ss.end());
68+
arith_uint256 hashSelection = UintToArith256(Hash(ss.begin(), ss.end()));
6969

7070
// the selection hash is divided by 2**32 so that proof-of-stake block
7171
// is always favored over proof-of-work block. this is to preserve

src/miner.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,13 @@ void BitcoinMiner(CWallet* pwallet, bool fProofOfStake)
195195
// Search
196196
//
197197
int64_t nStart = GetTime();
198-
uint256 hashTarget = uint256().SetCompact(pblock->nBits);
198+
arith_uint256& hashTarget = arith_uint256().SetCompact(pblock->nBits);
199199
while (true) {
200200
unsigned int nHashesDone = 0;
201201

202-
uint256 hash;
202+
arith_uint256 hash;
203203
while (true) {
204-
hash = pblock->GetHash();
204+
hash = UintToArith256(pblock->GetHash());
205205
if (hash <= hashTarget) {
206206
// Found a solution
207207
SetThreadPriority(THREAD_PRIORITY_NORMAL);

src/pow.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
2929
int64_t PastBlocksMin = 24;
3030
int64_t PastBlocksMax = 24;
3131
int64_t CountBlocks = 0;
32-
uint256 PastDifficultyAverage;
33-
uint256 PastDifficultyAveragePrev;
32+
arith_uint256 PastDifficultyAverage;
33+
arith_uint256 PastDifficultyAveragePrev;
3434
const Consensus::Params& consensus = Params().GetConsensus();
35+
const arith_uint256& powLimit = UintToArith256(consensus.powLimit);
3536

3637
if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || BlockLastSolved->nHeight < PastBlocksMin) {
37-
return consensus.powLimit.GetCompact();
38+
return powLimit.GetCompact();
3839
}
3940

4041
if (consensus.NetworkUpgradeActive(pindexLast->nHeight + 1, Consensus::UPGRADE_POS)) {
4142
const bool fTimeV2 = !Params().IsRegTestNet() && consensus.IsTimeProtocolV2(pindexLast->nHeight+1);
42-
const uint256& bnTargetLimit = consensus.ProofOfStakeLimit(fTimeV2);
43+
const arith_uint256& bnTargetLimit = UintToArith256(consensus.ProofOfStakeLimit(fTimeV2));
4344
const int64_t& nTargetTimespan = consensus.TargetTimespan(fTimeV2);
4445

4546
int64_t nActualSpacing = 0;
@@ -52,7 +53,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
5253

5354
// ppcoin: target change every block
5455
// ppcoin: retarget with exponential moving toward target spacing
55-
uint256 bnNew;
56+
arith_uint256 bnNew;
5657
bnNew.SetCompact(pindexLast->nBits);
5758

5859
// on first block with V2 time protocol, reduce the difficulty by a factor 16
@@ -79,7 +80,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
7980
if (CountBlocks == 1) {
8081
PastDifficultyAverage.SetCompact(BlockReading->nBits);
8182
} else {
82-
PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks) + (uint256().SetCompact(BlockReading->nBits))) / (CountBlocks + 1);
83+
PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks) + (arith_uint256().SetCompact(BlockReading->nBits))) / (CountBlocks + 1);
8384
}
8485
PastDifficultyAveragePrev = PastDifficultyAverage;
8586
}
@@ -97,7 +98,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
9798
BlockReading = BlockReading->pprev;
9899
}
99100

100-
uint256 bnNew(PastDifficultyAverage);
101+
arith_uint256 bnNew(PastDifficultyAverage);
101102

102103
int64_t _nTargetTimespan = CountBlocks * consensus.nTargetSpacing;
103104

@@ -110,8 +111,8 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
110111
bnNew *= nActualTimespan;
111112
bnNew /= _nTargetTimespan;
112113

113-
if (bnNew > consensus.powLimit) {
114-
bnNew = consensus.powLimit;
114+
if (bnNew > powLimit) {
115+
bnNew = powLimit;
115116
}
116117

117118
return bnNew.GetCompact();
@@ -121,31 +122,31 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
121122
{
122123
bool fNegative;
123124
bool fOverflow;
124-
uint256 bnTarget;
125+
arith_uint256 bnTarget;
125126

126127
if (Params().IsRegTestNet()) return true;
127128

128129
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
129130

130131
// Check range
131-
if (fNegative || bnTarget.IsNull() || fOverflow || bnTarget > Params().GetConsensus().powLimit)
132+
if (fNegative || bnTarget.IsNull() || fOverflow || bnTarget > UintToArith256(Params().GetConsensus().powLimit))
132133
return error("CheckProofOfWork() : nBits below minimum work");
133134

134135
// Check proof of work matches claimed amount
135-
if (hash > bnTarget)
136+
if (UintToArith256(hash) > bnTarget)
136137
return error("CheckProofOfWork() : hash doesn't match nBits");
137138

138139
return true;
139140
}
140141

141-
uint256 GetBlockProof(const CBlockIndex& block)
142+
arith_uint256 GetBlockProof(const CBlockIndex& block)
142143
{
143-
uint256 bnTarget;
144+
arith_uint256 bnTarget;
144145
bool fNegative;
145146
bool fOverflow;
146147
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
147148
if (fNegative || fOverflow || bnTarget.IsNull())
148-
return UINT256_ZERO;
149+
return ARITH_UINT256_ZERO;
149150
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
150151
// as it's too large for a uint256. However, as 2**256 is at least as large
151152
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,

src/pow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
2727

2828
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
2929
bool CheckProofOfWork(uint256 hash, unsigned int nBits);
30-
uint256 GetBlockProof(const CBlockIndex& block);
30+
arith_uint256 GetBlockProof(const CBlockIndex& block);
3131

3232
#endif // BITCOIN_POW_H

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
614614
UniValue aux(UniValue::VOBJ);
615615
aux.pushKV("flags", HexStr(COINBASE_FLAGS.begin(), COINBASE_FLAGS.end()));
616616

617-
uint256 hashTarget = uint256().SetCompact(pblock->nBits);
617+
arith_uint256& hashTarget = arith_uint256().SetCompact(pblock->nBits);
618618

619619
static UniValue aMutable(UniValue::VARR);
620620
if (aMutable.empty()) {

0 commit comments

Comments
 (0)