Skip to content

Commit df852d2

Browse files
committed
Refactor proof of work related functions out of main
1 parent 36db663 commit df852d2

File tree

9 files changed

+155
-124
lines changed

9 files changed

+155
-124
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ BITCOIN_CORE_H = \
7171
netbase.h \
7272
net.h \
7373
noui.h \
74+
pow.h \
7475
protocol.h \
7576
rpcclient.h \
7677
rpcprotocol.h \
@@ -121,6 +122,7 @@ libbitcoin_server_a_SOURCES = \
121122
miner.cpp \
122123
net.cpp \
123124
noui.cpp \
125+
pow.cpp \
124126
rpcblockchain.cpp \
125127
rpcmining.cpp \
126128
rpcmisc.cpp \

src/main.cpp

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "checkqueue.h"
1313
#include "init.h"
1414
#include "net.h"
15+
#include "pow.h"
1516
#include "txdb.h"
1617
#include "txmempool.h"
1718
#include "ui_interface.h"
@@ -1194,118 +1195,6 @@ int64_t GetBlockValue(int nHeight, int64_t nFees)
11941195
return nSubsidy + nFees;
11951196
}
11961197

1197-
static const int64_t nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
1198-
static const int64_t nTargetSpacing = 10 * 60;
1199-
static const int64_t nInterval = nTargetTimespan / nTargetSpacing;
1200-
1201-
//
1202-
// minimum amount of work that could possibly be required nTime after
1203-
// minimum work required was nBase
1204-
//
1205-
unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
1206-
{
1207-
const uint256 &bnLimit = Params().ProofOfWorkLimit();
1208-
// Testnet has min-difficulty blocks
1209-
// after nTargetSpacing*2 time between blocks:
1210-
if (Params().AllowMinDifficultyBlocks() && nTime > nTargetSpacing*2)
1211-
return bnLimit.GetCompact();
1212-
1213-
uint256 bnResult;
1214-
bnResult.SetCompact(nBase);
1215-
while (nTime > 0 && bnResult < bnLimit)
1216-
{
1217-
// Maximum 400% adjustment...
1218-
bnResult *= 4;
1219-
// ... in best-case exactly 4-times-normal target time
1220-
nTime -= nTargetTimespan*4;
1221-
}
1222-
if (bnResult > bnLimit)
1223-
bnResult = bnLimit;
1224-
return bnResult.GetCompact();
1225-
}
1226-
1227-
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
1228-
{
1229-
unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact();
1230-
1231-
// Genesis block
1232-
if (pindexLast == NULL)
1233-
return nProofOfWorkLimit;
1234-
1235-
// Only change once per interval
1236-
if ((pindexLast->nHeight+1) % nInterval != 0)
1237-
{
1238-
if (Params().AllowMinDifficultyBlocks())
1239-
{
1240-
// Special difficulty rule for testnet:
1241-
// If the new block's timestamp is more than 2* 10 minutes
1242-
// then allow mining of a min-difficulty block.
1243-
if (pblock->nTime > pindexLast->nTime + nTargetSpacing*2)
1244-
return nProofOfWorkLimit;
1245-
else
1246-
{
1247-
// Return the last non-special-min-difficulty-rules-block
1248-
const CBlockIndex* pindex = pindexLast;
1249-
while (pindex->pprev && pindex->nHeight % nInterval != 0 && pindex->nBits == nProofOfWorkLimit)
1250-
pindex = pindex->pprev;
1251-
return pindex->nBits;
1252-
}
1253-
}
1254-
return pindexLast->nBits;
1255-
}
1256-
1257-
// Go back by what we want to be 14 days worth of blocks
1258-
const CBlockIndex* pindexFirst = pindexLast;
1259-
for (int i = 0; pindexFirst && i < nInterval-1; i++)
1260-
pindexFirst = pindexFirst->pprev;
1261-
assert(pindexFirst);
1262-
1263-
// Limit adjustment step
1264-
int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
1265-
LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan);
1266-
if (nActualTimespan < nTargetTimespan/4)
1267-
nActualTimespan = nTargetTimespan/4;
1268-
if (nActualTimespan > nTargetTimespan*4)
1269-
nActualTimespan = nTargetTimespan*4;
1270-
1271-
// Retarget
1272-
uint256 bnNew;
1273-
uint256 bnOld;
1274-
bnNew.SetCompact(pindexLast->nBits);
1275-
bnOld = bnNew;
1276-
bnNew *= nActualTimespan;
1277-
bnNew /= nTargetTimespan;
1278-
1279-
if (bnNew > Params().ProofOfWorkLimit())
1280-
bnNew = Params().ProofOfWorkLimit();
1281-
1282-
/// debug print
1283-
LogPrintf("GetNextWorkRequired RETARGET\n");
1284-
LogPrintf("nTargetTimespan = %d nActualTimespan = %d\n", nTargetTimespan, nActualTimespan);
1285-
LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
1286-
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
1287-
1288-
return bnNew.GetCompact();
1289-
}
1290-
1291-
bool CheckProofOfWork(uint256 hash, unsigned int nBits)
1292-
{
1293-
bool fNegative;
1294-
bool fOverflow;
1295-
uint256 bnTarget;
1296-
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
1297-
1298-
// Check range
1299-
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit())
1300-
return error("CheckProofOfWork() : nBits below minimum work");
1301-
1302-
// Check proof of work matches claimed amount
1303-
if (hash > bnTarget)
1304-
return error("CheckProofOfWork() : hash doesn't match nBits");
1305-
1306-
return true;
1307-
}
1308-
13091198
bool IsInitialBlockDownload()
13101199
{
13111200
LOCK(cs_main);

src/main.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,6 @@ bool ProcessMessages(CNode* pfrom);
146146
bool SendMessages(CNode* pto, bool fSendTrickle);
147147
/** Run an instance of the script checking thread */
148148
void ThreadScriptCheck();
149-
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
150-
bool CheckProofOfWork(uint256 hash, unsigned int nBits);
151-
/** Calculate the minimum amount of work a received block needs, without knowing its direct parent */
152-
unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime);
153149
/** Check whether we are doing an initial block download (synchronizing from disk or network) */
154150
bool IsInitialBlockDownload();
155151
/** Format a string that describes several potential problems detected by the core */
@@ -159,7 +155,6 @@ bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, b
159155
/** Find the best known block, and make it the tip of the block chain */
160156
bool ActivateBestChain(CValidationState &state);
161157
int64_t GetBlockValue(int nHeight, int64_t nFees);
162-
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock);
163158

164159
void UpdateTime(CBlockHeader& block, const CBlockIndex* pindexPrev);
165160

@@ -812,11 +807,6 @@ class CBlockIndex
812807
return (~bnTarget / (bnTarget + 1)) + 1;
813808
}
814809

815-
bool CheckIndex() const
816-
{
817-
return CheckProofOfWork(GetBlockHash(), nBits);
818-
}
819-
820810
enum { nMedianTimeSpan=11 };
821811

822812
int64_t GetMedianTimePast() const

src/miner.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "hash.h"
1010
#include "main.h"
1111
#include "net.h"
12+
#include "pow.h"
1213
#ifdef ENABLE_WALLET
1314
#include "wallet.h"
1415
#endif

src/pow.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2014 The Bitcoin developers
3+
// Distributed under the MIT/X11 software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include "pow.h"
7+
8+
#include "chainparams.h"
9+
#include "core.h"
10+
#include "main.h"
11+
#include "uint256.h"
12+
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+
17+
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
18+
{
19+
unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact();
20+
21+
// Genesis block
22+
if (pindexLast == NULL)
23+
return nProofOfWorkLimit;
24+
25+
// Only change once per interval
26+
if ((pindexLast->nHeight+1) % nInterval != 0)
27+
{
28+
if (Params().AllowMinDifficultyBlocks())
29+
{
30+
// Special difficulty rule for testnet:
31+
// If the new block's timestamp is more than 2* 10 minutes
32+
// then allow mining of a min-difficulty block.
33+
if (pblock->nTime > pindexLast->nTime + nTargetSpacing*2)
34+
return nProofOfWorkLimit;
35+
else
36+
{
37+
// Return the last non-special-min-difficulty-rules-block
38+
const CBlockIndex* pindex = pindexLast;
39+
while (pindex->pprev && pindex->nHeight % nInterval != 0 && pindex->nBits == nProofOfWorkLimit)
40+
pindex = pindex->pprev;
41+
return pindex->nBits;
42+
}
43+
}
44+
return pindexLast->nBits;
45+
}
46+
47+
// Go back by what we want to be 14 days worth of blocks
48+
const CBlockIndex* pindexFirst = pindexLast;
49+
for (int i = 0; pindexFirst && i < nInterval-1; i++)
50+
pindexFirst = pindexFirst->pprev;
51+
assert(pindexFirst);
52+
53+
// Limit adjustment step
54+
int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
55+
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;
60+
61+
// Retarget
62+
uint256 bnNew;
63+
uint256 bnOld;
64+
bnNew.SetCompact(pindexLast->nBits);
65+
bnOld = bnNew;
66+
bnNew *= nActualTimespan;
67+
bnNew /= nTargetTimespan;
68+
69+
if (bnNew > Params().ProofOfWorkLimit())
70+
bnNew = Params().ProofOfWorkLimit();
71+
72+
/// debug print
73+
LogPrintf("GetNextWorkRequired RETARGET\n");
74+
LogPrintf("nTargetTimespan = %d nActualTimespan = %d\n", nTargetTimespan, nActualTimespan);
75+
LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
76+
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
77+
78+
return bnNew.GetCompact();
79+
}
80+
81+
bool CheckProofOfWork(uint256 hash, unsigned int nBits)
82+
{
83+
bool fNegative;
84+
bool fOverflow;
85+
uint256 bnTarget;
86+
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
87+
88+
// Check range
89+
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit())
90+
return error("CheckProofOfWork() : nBits below minimum work");
91+
92+
// Check proof of work matches claimed amount
93+
if (hash > bnTarget)
94+
return error("CheckProofOfWork() : hash doesn't match nBits");
95+
96+
return true;
97+
}
98+
99+
//
100+
// minimum amount of work that could possibly be required nTime after
101+
// minimum work required was nBase
102+
//
103+
unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
104+
{
105+
const uint256 &bnLimit = Params().ProofOfWorkLimit();
106+
// Testnet has min-difficulty blocks
107+
// after nTargetSpacing*2 time between blocks:
108+
if (Params().AllowMinDifficultyBlocks() && nTime > nTargetSpacing*2)
109+
return bnLimit.GetCompact();
110+
111+
uint256 bnResult;
112+
bnResult.SetCompact(nBase);
113+
while (nTime > 0 && bnResult < bnLimit)
114+
{
115+
// Maximum 400% adjustment...
116+
bnResult *= 4;
117+
// ... in best-case exactly 4-times-normal target time
118+
nTime -= nTargetTimespan*4;
119+
}
120+
if (bnResult > bnLimit)
121+
bnResult = bnLimit;
122+
return bnResult.GetCompact();
123+
}

src/pow.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
// Copyright (c) 2009-2010 Satoshi Nakamoto
3+
// Copyright (c) 2009-2014 The Bitcoin developers
4+
// Distributed under the MIT/X11 software license, see the accompanying
5+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6+
7+
#ifndef BITCOIN_POW_H
8+
#define BITCOIN_POW_H
9+
10+
#include <stdint.h>
11+
12+
class CBlockIndex;
13+
class CBlockHeader;
14+
class uint256;
15+
16+
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock);
17+
18+
/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */
19+
bool CheckProofOfWork(uint256 hash, unsigned int nBits);
20+
/** Calculate the minimum amount of work a received block needs, without knowing its direct parent */
21+
unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime);
22+
23+
#endif

src/rpcmining.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "net.h"
1010
#include "main.h"
1111
#include "miner.h"
12+
#include "pow.h"
1213
#ifdef ENABLE_WALLET
1314
#include "db.h"
1415
#include "wallet.h"

src/test/DoS_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "keystore.h"
1212
#include "main.h"
1313
#include "net.h"
14+
#include "pow.h"
1415
#include "script.h"
1516
#include "serialize.h"
1617

src/txdb.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "txdb.h"
77

88
#include "core.h"
9+
#include "pow.h"
910
#include "uint256.h"
1011

1112
#include <stdint.h>
@@ -212,8 +213,8 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
212213
pindexNew->nStatus = diskindex.nStatus;
213214
pindexNew->nTx = diskindex.nTx;
214215

215-
if (!pindexNew->CheckIndex())
216-
return error("LoadBlockIndex() : CheckIndex failed: %s", pindexNew->ToString());
216+
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits))
217+
return error("LoadBlockIndex() : CheckProofOfWork failed: %s", pindexNew->ToString());
217218

218219
pcursor->Next();
219220
} else {

0 commit comments

Comments
 (0)