Skip to content

Commit fd704c7

Browse files
committed
move pow constants to chainparams
1 parent df852d2 commit fd704c7

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/chainparams.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ class CMainParams : public CChainParams {
117117
nRejectBlockOutdatedMajority = 950;
118118
nToCheckBlockUpgradeMajority = 1000;
119119
nMinerThreads = 0;
120+
nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
121+
nTargetSpacing = 10 * 60;
120122

121123
// Build the genesis block. Note that the output of the genesis coinbase cannot
122124
// be spent as it did not originally exist in the database.
@@ -204,6 +206,9 @@ class CTestNetParams : public CMainParams {
204206
nEnforceBlockUpgradeMajority = 51;
205207
nRejectBlockOutdatedMajority = 75;
206208
nToCheckBlockUpgradeMajority = 100;
209+
nMinerThreads = 0;
210+
nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
211+
nTargetSpacing = 10 * 60;
207212
strDataDir = "testnet3";
208213

209214
// Modify the testnet genesis block so the timestamp is valid for a later start.
@@ -251,6 +256,8 @@ class CRegTestParams : public CTestNetParams {
251256
nRejectBlockOutdatedMajority = 950;
252257
nToCheckBlockUpgradeMajority = 1000;
253258
nMinerThreads = 1;
259+
nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
260+
nTargetSpacing = 10 * 60;
254261
bnProofOfWorkLimit = ~uint256(0) >> 1;
255262
genesis.nTime = 1296688602;
256263
genesis.nBits = 0x207fffff;

src/chainparams.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class CChainParams
7070
bool AllowMinDifficultyBlocks() const { return fAllowMinDifficultyBlocks; }
7171
/* Make standard checks */
7272
bool RequireStandard() const { return fRequireStandard; }
73+
int64_t TargetTimespan() const { return nTargetTimespan; }
74+
int64_t TargetSpacing() const { return nTargetSpacing; }
75+
int64_t Interval() const { return nTargetTimespan / nTargetSpacing; }
7376
const std::string& DataDir() const { return strDataDir; }
7477
/* Make miner stop after a block is found. In RPC, don't return
7578
* until nGenProcLimit blocks are generated */
@@ -95,6 +98,8 @@ class CChainParams
9598
int nEnforceBlockUpgradeMajority;
9699
int nRejectBlockOutdatedMajority;
97100
int nToCheckBlockUpgradeMajority;
101+
int64_t nTargetTimespan;
102+
int64_t nTargetSpacing;
98103
std::string strDataDir;
99104
int nMinerThreads;
100105
std::vector<CDNSSeedData> vSeeds;

src/pow.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
#include "main.h"
1111
#include "uint256.h"
1212

13-
static const int64_t nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
14-
static const int64_t nTargetSpacing = 10 * 60;
15-
static const int64_t nInterval = nTargetTimespan / nTargetSpacing;
16-
1713
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
1814
{
1915
unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact();
@@ -23,20 +19,20 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
2319
return nProofOfWorkLimit;
2420

2521
// Only change once per interval
26-
if ((pindexLast->nHeight+1) % nInterval != 0)
22+
if ((pindexLast->nHeight+1) % Params().Interval() != 0)
2723
{
2824
if (Params().AllowMinDifficultyBlocks())
2925
{
3026
// Special difficulty rule for testnet:
3127
// If the new block's timestamp is more than 2* 10 minutes
3228
// then allow mining of a min-difficulty block.
33-
if (pblock->nTime > pindexLast->nTime + nTargetSpacing*2)
29+
if (pblock->nTime > pindexLast->nTime + Params().TargetSpacing()*2)
3430
return nProofOfWorkLimit;
3531
else
3632
{
3733
// Return the last non-special-min-difficulty-rules-block
3834
const CBlockIndex* pindex = pindexLast;
39-
while (pindex->pprev && pindex->nHeight % nInterval != 0 && pindex->nBits == nProofOfWorkLimit)
35+
while (pindex->pprev && pindex->nHeight % Params().Interval() != 0 && pindex->nBits == nProofOfWorkLimit)
4036
pindex = pindex->pprev;
4137
return pindex->nBits;
4238
}
@@ -46,32 +42,32 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
4642

4743
// Go back by what we want to be 14 days worth of blocks
4844
const CBlockIndex* pindexFirst = pindexLast;
49-
for (int i = 0; pindexFirst && i < nInterval-1; i++)
45+
for (int i = 0; pindexFirst && i < Params().Interval()-1; i++)
5046
pindexFirst = pindexFirst->pprev;
5147
assert(pindexFirst);
5248

5349
// Limit adjustment step
5450
int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
5551
LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan);
56-
if (nActualTimespan < nTargetTimespan/4)
57-
nActualTimespan = nTargetTimespan/4;
58-
if (nActualTimespan > nTargetTimespan*4)
59-
nActualTimespan = nTargetTimespan*4;
52+
if (nActualTimespan < Params().TargetTimespan()/4)
53+
nActualTimespan = Params().TargetTimespan()/4;
54+
if (nActualTimespan > Params().TargetTimespan()*4)
55+
nActualTimespan = Params().TargetTimespan()*4;
6056

6157
// Retarget
6258
uint256 bnNew;
6359
uint256 bnOld;
6460
bnNew.SetCompact(pindexLast->nBits);
6561
bnOld = bnNew;
6662
bnNew *= nActualTimespan;
67-
bnNew /= nTargetTimespan;
63+
bnNew /= Params().TargetTimespan();
6864

6965
if (bnNew > Params().ProofOfWorkLimit())
7066
bnNew = Params().ProofOfWorkLimit();
7167

7268
/// debug print
7369
LogPrintf("GetNextWorkRequired RETARGET\n");
74-
LogPrintf("nTargetTimespan = %d nActualTimespan = %d\n", nTargetTimespan, nActualTimespan);
70+
LogPrintf("Params().TargetTimespan() = %d nActualTimespan = %d\n", Params().TargetTimespan(), nActualTimespan);
7571
LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
7672
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
7773

@@ -104,8 +100,8 @@ unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
104100
{
105101
const uint256 &bnLimit = Params().ProofOfWorkLimit();
106102
// Testnet has min-difficulty blocks
107-
// after nTargetSpacing*2 time between blocks:
108-
if (Params().AllowMinDifficultyBlocks() && nTime > nTargetSpacing*2)
103+
// after Params().TargetSpacing()*2 time between blocks:
104+
if (Params().AllowMinDifficultyBlocks() && nTime > Params().TargetSpacing()*2)
109105
return bnLimit.GetCompact();
110106

111107
uint256 bnResult;
@@ -115,7 +111,7 @@ unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
115111
// Maximum 400% adjustment...
116112
bnResult *= 4;
117113
// ... in best-case exactly 4-times-normal target time
118-
nTime -= nTargetTimespan*4;
114+
nTime -= Params().TargetTimespan()*4;
119115
}
120116
if (bnResult > bnLimit)
121117
bnResult = bnLimit;

0 commit comments

Comments
 (0)