Skip to content

Commit d698ef6

Browse files
committed
Consensus: Refactor: Decouple pow.o from chainparams.o
1 parent bd00611 commit d698ef6

File tree

8 files changed

+42
-35
lines changed

8 files changed

+42
-35
lines changed

src/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos)
11581158
}
11591159

11601160
// Check the header
1161-
if (!CheckProofOfWork(block.GetHash(), block.nBits))
1161+
if (!CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus()))
11621162
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
11631163

11641164
return true;
@@ -2462,7 +2462,7 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne
24622462
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW)
24632463
{
24642464
// Check proof of work matches claimed amount
2465-
if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits))
2465+
if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus()))
24662466
return state.DoS(50, error("CheckBlockHeader(): proof of work failed"),
24672467
REJECT_INVALID, "high-hash");
24682468

@@ -2545,7 +2545,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
25452545
int nHeight = pindexPrev->nHeight+1;
25462546

25472547
// Check proof of work
2548-
if ((block.nBits != GetNextWorkRequired(pindexPrev, &block)))
2548+
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, Params().GetConsensus()))
25492549
return state.DoS(100, error("%s: incorrect proof of work", __func__),
25502550
REJECT_INVALID, "bad-diffbits");
25512551

src/main.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "primitives/block.h"
1818
#include "primitives/transaction.h"
1919
#include "net.h"
20-
#include "pow.h"
2120
#include "script/script.h"
2221
#include "script/sigcache.h"
2322
#include "script/standard.h"

src/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev)
8484

8585
// Updating time can change work required on testnet:
8686
if (Params().AllowMinDifficultyBlocks())
87-
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
87+
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
8888
}
8989

9090
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
@@ -326,7 +326,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
326326
// Fill in header
327327
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
328328
UpdateTime(pblock, pindexPrev);
329-
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
329+
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
330330
pblock->nNonce = 0;
331331
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
332332

src/pow.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,33 @@
77

88
#include "arith_uint256.h"
99
#include "chain.h"
10-
#include "chainparams.h"
1110
#include "primitives/block.h"
1211
#include "uint256.h"
1312
#include "util.h"
1413

15-
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
14+
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
1615
{
17-
unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact();
16+
unsigned int nProofOfWorkLimit = params.powLimit.GetCompact();
1817

1918
// Genesis block
2019
if (pindexLast == NULL)
2120
return nProofOfWorkLimit;
2221

2322
// Only change once per difficulty adjustment interval
24-
if ((pindexLast->nHeight+1) % Params().DifficultyAdjustmentInterval() != 0)
23+
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
2524
{
26-
if (Params().AllowMinDifficultyBlocks())
25+
if (params.fPowAllowMinDifficultyBlocks)
2726
{
2827
// Special difficulty rule for testnet:
2928
// If the new block's timestamp is more than 2* 10 minutes
3029
// then allow mining of a min-difficulty block.
31-
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + Params().TargetSpacing()*2)
30+
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
3231
return nProofOfWorkLimit;
3332
else
3433
{
3534
// Return the last non-special-min-difficulty-rules-block
3635
const CBlockIndex* pindex = pindexLast;
37-
while (pindex->pprev && pindex->nHeight % Params().DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
36+
while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
3837
pindex = pindex->pprev;
3938
return pindex->nBits;
4039
}
@@ -44,44 +43,44 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
4443

4544
// Go back by what we want to be 14 days worth of blocks
4645
const CBlockIndex* pindexFirst = pindexLast;
47-
for (int i = 0; pindexFirst && i < Params().DifficultyAdjustmentInterval()-1; i++)
46+
for (int i = 0; pindexFirst && i < params.DifficultyAdjustmentInterval()-1; i++)
4847
pindexFirst = pindexFirst->pprev;
4948
assert(pindexFirst);
5049

51-
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime());
50+
return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
5251
}
5352

54-
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime)
53+
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
5554
{
5655
// Limit adjustment step
5756
int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
5857
LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan);
59-
if (nActualTimespan < Params().TargetTimespan()/4)
60-
nActualTimespan = Params().TargetTimespan()/4;
61-
if (nActualTimespan > Params().TargetTimespan()*4)
62-
nActualTimespan = Params().TargetTimespan()*4;
58+
if (nActualTimespan < params.nPowTargetTimespan/4)
59+
nActualTimespan = params.nPowTargetTimespan/4;
60+
if (nActualTimespan > params.nPowTargetTimespan*4)
61+
nActualTimespan = params.nPowTargetTimespan*4;
6362

6463
// Retarget
6564
arith_uint256 bnNew;
6665
arith_uint256 bnOld;
6766
bnNew.SetCompact(pindexLast->nBits);
6867
bnOld = bnNew;
6968
bnNew *= nActualTimespan;
70-
bnNew /= Params().TargetTimespan();
69+
bnNew /= params.nPowTargetTimespan;
7170

72-
if (bnNew > Params().ProofOfWorkLimit())
73-
bnNew = Params().ProofOfWorkLimit();
71+
if (bnNew > params.powLimit)
72+
bnNew = params.powLimit;
7473

7574
/// debug print
7675
LogPrintf("GetNextWorkRequired RETARGET\n");
77-
LogPrintf("Params().TargetTimespan() = %d nActualTimespan = %d\n", Params().TargetTimespan(), nActualTimespan);
76+
LogPrintf("params.nPowTargetTimespan = %d nActualTimespan = %d\n", params.nPowTargetTimespan, nActualTimespan);
7877
LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
7978
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
8079

8180
return bnNew.GetCompact();
8281
}
8382

84-
bool CheckProofOfWork(uint256 hash, unsigned int nBits)
83+
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
8584
{
8685
bool fNegative;
8786
bool fOverflow;
@@ -90,7 +89,7 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
9089
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
9190

9291
// Check range
93-
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit())
92+
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > params.powLimit)
9493
return error("CheckProofOfWork(): nBits below minimum work");
9594

9695
// Check proof of work matches claimed amount

src/pow.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
#ifndef BITCOIN_POW_H
77
#define BITCOIN_POW_H
88

9+
#include "consensus/params.h"
10+
911
#include <stdint.h>
1012

1113
class CBlockHeader;
1214
class CBlockIndex;
1315
class uint256;
1416
class arith_uint256;
1517

16-
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock);
17-
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime);
18+
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&);
19+
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&);
1820

1921
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
20-
bool CheckProofOfWork(uint256 hash, unsigned int nBits);
22+
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&);
2123
arith_uint256 GetBlockProof(const CBlockIndex& block);
2224

2325
#endif // BITCOIN_POW_H

src/rpcmining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Value setgenerate(const Array& params, bool fHelp)
181181
LOCK(cs_main);
182182
IncrementExtraNonce(pblock, chainActive.Tip(), nExtraNonce);
183183
}
184-
while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits)) {
184+
while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
185185
// Yes, there is a chance every nonce could fail to satisfy the -regtest
186186
// target -- 1 in 2^(2^32). That ain't gonna happen.
187187
++pblock->nNonce;

src/test/pow_tests.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,56 @@ BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
1717
BOOST_AUTO_TEST_CASE(get_next_work)
1818
{
1919
SelectParams(CBaseChainParams::MAIN);
20+
const Consensus::Params& params = Params().GetConsensus();
2021

2122
int64_t nLastRetargetTime = 1261130161; // Block #30240
2223
CBlockIndex pindexLast;
2324
pindexLast.nHeight = 32255;
2425
pindexLast.nTime = 1262152739; // Block #32255
2526
pindexLast.nBits = 0x1d00ffff;
26-
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime), 0x1d00d86a);
27+
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00d86a);
2728
}
2829

2930
/* Test the constraint on the upper bound for next work */
3031
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
3132
{
3233
SelectParams(CBaseChainParams::MAIN);
34+
const Consensus::Params& params = Params().GetConsensus();
3335

3436
int64_t nLastRetargetTime = 1231006505; // Block #0
3537
CBlockIndex pindexLast;
3638
pindexLast.nHeight = 2015;
3739
pindexLast.nTime = 1233061996; // Block #2015
3840
pindexLast.nBits = 0x1d00ffff;
39-
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime), 0x1d00ffff);
41+
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00ffff);
4042
}
4143

4244
/* Test the constraint on the lower bound for actual time taken */
4345
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
4446
{
4547
SelectParams(CBaseChainParams::MAIN);
48+
const Consensus::Params& params = Params().GetConsensus();
4649

4750
int64_t nLastRetargetTime = 1279008237; // Block #66528
4851
CBlockIndex pindexLast;
4952
pindexLast.nHeight = 68543;
5053
pindexLast.nTime = 1279297671; // Block #68543
5154
pindexLast.nBits = 0x1c05a3f4;
52-
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime), 0x1c0168fd);
55+
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c0168fd);
5356
}
5457

5558
/* Test the constraint on the upper bound for actual time taken */
5659
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
5760
{
5861
SelectParams(CBaseChainParams::MAIN);
62+
const Consensus::Params& params = Params().GetConsensus();
63+
5964
int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
6065
CBlockIndex pindexLast;
6166
pindexLast.nHeight = 46367;
6267
pindexLast.nTime = 1269211443; // Block #46367
6368
pindexLast.nBits = 0x1c387f6f;
64-
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime), 0x1d00e1fd);
69+
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00e1fd);
6570
}
6671

6772
BOOST_AUTO_TEST_SUITE_END()

src/txdb.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "txdb.h"
77

8+
#include "chainparams.h"
9+
#include "hash.h"
810
#include "main.h"
911
#include "pow.h"
1012
#include "uint256.h"
@@ -223,7 +225,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
223225
pindexNew->nStatus = diskindex.nStatus;
224226
pindexNew->nTx = diskindex.nTx;
225227

226-
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits))
228+
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, Params().GetConsensus()))
227229
return error("LoadBlockIndex(): CheckProofOfWork failed: %s", pindexNew->ToString());
228230

229231
pcursor->Next();

0 commit comments

Comments
 (0)