Skip to content

Commit 284271c

Browse files
committed
[Refactor] Consensus: bnProofOfWorkLimit / bnProofOfStakeLimit (v1-v2)
1 parent 735d8dd commit 284271c

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

src/chainparams.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ class CMainParams : public CChainParams
193193
strNetworkID = "main";
194194

195195
consensus.BIP65Height = 1808634; // 82629b7a9978f5c7ea3f70a12db92633a7d2e436711500db28b97efd48b1e527
196-
consensus.powLimit = uint256S("00000ffff0000000000000000000000000000000000000000000000000000000");
197-
consensus.posLimitv1 = uint256S("000000ffff000000000000000000000000000000000000000000000000000000");
198-
consensus.posLimitv2 = uint256S("00000ffff0000000000000000000000000000000000000000000000000000000");
196+
consensus.powLimit = ~uint256(0) >> 20; // PIVX starting difficulty is 1 / 2^12
197+
consensus.posLimitV1 = ~uint256(0) >> 24;
198+
consensus.posLimitV2 = ~uint256(0) >> 20;
199199
consensus.nCoinbaseMaturity = 100;
200200
consensus.nTargetTimespan = 40 * 60;
201201
consensus.nTargetSpacing = 1 * 60;
@@ -219,9 +219,6 @@ class CMainParams : public CChainParams
219219
pchMessageStart[3] = 0xe9;
220220
vAlertPubKey = ParseHex("0000098d3ba6ba6e7423fa5cbd6a89e0a9a5348f88d332b44a5cb1a8b7ed2c1eaa335fc8dc4f012cb8241cc0bdafd6ca70c5f5448916e4e6f511bcd746ed57dc50");
221221
nDefaultPort = 51472;
222-
bnProofOfWorkLimit = ~uint256(0) >> 20; // PIVX starting difficulty is 1 / 2^12
223-
bnProofOfStakeLimit = ~uint256(0) >> 24;
224-
bnProofOfStakeLimit_V2 = ~uint256(0) >> 20; // 60/4 = 15 ==> use 2**4 higher limit
225222
nMaxReorganizationDepth = 100;
226223

227224
genesis = CreateGenesisBlock(1454124731, 2402015, 0x1e0ffff0, 1, 250 * COIN);
@@ -350,9 +347,9 @@ class CTestNetParams : public CMainParams
350347
strNetworkID = "test";
351348

352349
consensus.BIP65Height = 851019;
353-
consensus.powLimit = uint256S("00000ffff0000000000000000000000000000000000000000000000000000000");
354-
consensus.posLimitv1 = uint256S("000000ffff000000000000000000000000000000000000000000000000000000");
355-
consensus.posLimitv2 = uint256S("00000ffff0000000000000000000000000000000000000000000000000000000");
350+
consensus.powLimit = ~uint256(0) >> 20; // PIVX starting difficulty is 1 / 2^12
351+
consensus.posLimitV1 = ~uint256(0) >> 24;
352+
consensus.posLimitV2 = ~uint256(0) >> 20;
356353
consensus.nCoinbaseMaturity = 15;
357354
consensus.nTargetTimespan = 40 * 60;
358355
consensus.nTargetSpacing = 1 * 60;
@@ -476,10 +473,10 @@ class CRegTestParams : public CTestNetParams
476473
networkID = CBaseChainParams::REGTEST;
477474
strNetworkID = "regtest";
478475

479-
consensus.BIP65Height = 1808634; // 82629b7a9978f5c7ea3f70a12db92633a7d2e436711500db28b97efd48b1e527
480-
consensus.powLimit = uint256S("ffff000000000000000000000000000000000000000000000000000000000000");
481-
consensus.posLimitv1 = uint256S("000000ffff000000000000000000000000000000000000000000000000000000");
482-
consensus.posLimitv2 = uint256S("00000ffff0000000000000000000000000000000000000000000000000000000");
476+
consensus.BIP65Height = 851019; // Not defined for regtest. Inherit TestNet value.
477+
consensus.powLimit = ~uint256(0) >> 20; // PIVX starting difficulty is 1 / 2^12
478+
consensus.posLimitV1 = ~uint256(0) >> 24;
479+
consensus.posLimitV2 = ~uint256(0) >> 20;
483480
consensus.nCoinbaseMaturity = 100;
484481
consensus.nTargetTimespan = 40 * 60;
485482
consensus.nTargetSpacing = 1 * 60;

src/chainparams.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class CChainParams
5555
const Consensus::Params& GetConsensus() const { return consensus; }
5656
const MessageStartChars& MessageStart() const { return pchMessageStart; }
5757
int GetDefaultPort() const { return nDefaultPort; }
58-
const uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; }
59-
const uint256& ProofOfStakeLimit(const bool fV2) const { return fV2 ? bnProofOfStakeLimit_V2 : bnProofOfStakeLimit; }
6058

6159
const CBlock& GenesisBlock() const { return genesis; }
6260
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
@@ -171,9 +169,6 @@ class CChainParams
171169
//! Raw pub key bytes for the broadcast alert signing key.
172170
std::vector<unsigned char> vAlertPubKey;
173171
int nDefaultPort;
174-
uint256 bnProofOfWorkLimit;
175-
uint256 bnProofOfStakeLimit;
176-
uint256 bnProofOfStakeLimit_V2;
177172
int nMaxReorganizationDepth;
178173
int nEnforceBlockUpgradeMajority;
179174
int nRejectBlockOutdatedMajority;

src/consensus/params.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ struct Params {
2323

2424
// TODO: Implement the following parameters
2525
uint256 powLimit;
26-
uint256 posLimitv1;
27-
uint256 posLimitv2;
26+
uint256 posLimitV1;
27+
uint256 posLimitV2;
2828
bool fPowAllowMinDifficultyBlocks;
2929
bool fPowNoRetargeting;
3030
int64_t nTargetSpacing;
@@ -34,8 +34,7 @@ struct Params {
3434

3535
// TODO: Implement the following methods
3636
int64_t DifficultyAdjustmentInterval() const { return nTargetTimespan / nTargetSpacing; }
37-
uint256 ProofOfWorkLimit() const { return powLimit; }
38-
uint256 ProofOfStakeLimit(const bool fV2) const { return fV2 ? posLimitv2 : posLimitv1; }
37+
uint256 ProofOfStakeLimit(const bool fV2) const { return fV2 ? posLimitV2 : posLimitV1; }
3938
uint256 nMinimumChainWork;
4039
uint256 defaultAssumeValid;
4140
};

src/pow.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
3232
int64_t CountBlocks = 0;
3333
uint256 PastDifficultyAverage;
3434
uint256 PastDifficultyAveragePrev;
35+
const uint256& PowLimit = Params().GetConsensus().powLimit;
3536

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

4041
if (pindexLast->nHeight >= Params().LAST_POW_BLOCK()) {
4142
const bool fTimeV2 = Params().IsTimeProtocolV2(pindexLast->nHeight+1);
42-
const uint256 bnTargetLimit = Params().ProofOfStakeLimit(fTimeV2);
43+
const uint256 bnTargetLimit = Params().GetConsensus().ProofOfStakeLimit(fTimeV2);
4344
const int64_t nTargetSpacing = Params().TargetSpacing();
4445
const int64_t nTargetTimespan = Params().TargetTimespan(fTimeV2);
4546

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

114-
if (bnNew > Params().ProofOfWorkLimit()) {
115-
bnNew = Params().ProofOfWorkLimit();
115+
if (bnNew > PowLimit) {
116+
bnNew = PowLimit;
116117
}
117118

118119
return bnNew.GetCompact();
@@ -130,7 +131,7 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
130131
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
131132

132133
// Check range
133-
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit())
134+
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().GetConsensus().powLimit)
134135
return error("CheckProofOfWork() : nBits below minimum work");
135136

136137
// Check proof of work matches claimed amount

0 commit comments

Comments
 (0)