Skip to content

Commit ebec731

Browse files
committed
Drop the chain argument to GetDifficulty
This removes the need to include rpc/blockchain.cpp in order to put GetDifficulty under test. GetDifficulty was called in two ways: * with a guaranteed non-null blockindex * with no argument Change the latter case to be provided chainActive.Tip() explicitly.
1 parent d792e47 commit ebec731

File tree

4 files changed

+10
-69
lines changed

4 files changed

+10
-69
lines changed

src/rpc/blockchain.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <rpc/blockchain.h>
77

88
#include <amount.h>
9-
#include <chain.h>
109
#include <chainparams.h>
1110
#include <checkpoints.h>
1211
#include <coins.h>
@@ -47,17 +46,13 @@ static std::mutex cs_blockchange;
4746
static std::condition_variable cond_blockchange;
4847
static CUpdatedBlock latestblock;
4948

50-
/* Calculate the difficulty for a given block index,
51-
* or the block index of the given chain.
49+
/* Calculate the difficulty for a given block index.
5250
*/
53-
double GetDifficulty(const CChain& chain, const CBlockIndex* blockindex)
51+
double GetDifficulty(const CBlockIndex* blockindex)
5452
{
5553
if (blockindex == nullptr)
5654
{
57-
if (chain.Tip() == nullptr)
58-
return 1.0;
59-
else
60-
blockindex = chain.Tip();
55+
return 1.0;
6156
}
6257

6358
int nShift = (blockindex->nBits >> 24) & 0xff;
@@ -78,11 +73,6 @@ double GetDifficulty(const CChain& chain, const CBlockIndex* blockindex)
7873
return dDiff;
7974
}
8075

81-
double GetDifficulty(const CBlockIndex* blockindex)
82-
{
83-
return GetDifficulty(chainActive, blockindex);
84-
}
85-
8676
UniValue blockheaderToJSON(const CBlockIndex* blockindex)
8777
{
8878
AssertLockHeld(cs_main);
@@ -352,7 +342,7 @@ static UniValue getdifficulty(const JSONRPCRequest& request)
352342
);
353343

354344
LOCK(cs_main);
355-
return GetDifficulty();
345+
return GetDifficulty(chainActive.Tip());
356346
}
357347

358348
static std::string EntryDescriptionString()
@@ -1229,7 +1219,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
12291219
obj.pushKV("blocks", (int)chainActive.Height());
12301220
obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1);
12311221
obj.pushKV("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex());
1232-
obj.pushKV("difficulty", (double)GetDifficulty());
1222+
obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip()));
12331223
obj.pushKV("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast());
12341224
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip()));
12351225
obj.pushKV("initialblockdownload", IsInitialBlockDownload());

src/rpc/blockchain.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class UniValue;
1616
* @return A floating point number that is a multiple of the main net minimum
1717
* difficulty (4295032833 hashes).
1818
*/
19-
double GetDifficulty(const CBlockIndex* blockindex = nullptr);
19+
double GetDifficulty(const CBlockIndex* blockindex);
2020

2121
/** Callback for when block tip changed. */
2222
void RPCNotifyBlockChange(bool ibd, const CBlockIndex *);
@@ -34,4 +34,3 @@ UniValue mempoolToJSON(bool fVerbose = false);
3434
UniValue blockheaderToJSON(const CBlockIndex* blockindex);
3535

3636
#endif
37-

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request)
214214
obj.pushKV("blocks", (int)chainActive.Height());
215215
obj.pushKV("currentblockweight", (uint64_t)nLastBlockWeight);
216216
obj.pushKV("currentblocktx", (uint64_t)nLastBlockTx);
217-
obj.pushKV("difficulty", (double)GetDifficulty());
217+
obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip()));
218218
obj.pushKV("networkhashps", getnetworkhashps(request));
219219
obj.pushKV("pooledtx", (uint64_t)mempool.size());
220220
obj.pushKV("chain", Params().NetworkIDString());

src/test/blockchain_tests.cpp

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "stdlib.h"
44

5-
#include "rpc/blockchain.cpp"
5+
#include "rpc/blockchain.h"
66
#include "test/test_bitcoin.h"
77

88
/* Equality between doubles is imprecise. Comparison should be done
@@ -22,14 +22,6 @@ static CBlockIndex* CreateBlockIndexWithNbits(uint32_t nbits)
2222
return block_index;
2323
}
2424

25-
static CChain CreateChainWithNbits(uint32_t nbits)
26-
{
27-
CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits);
28-
CChain chain;
29-
chain.SetTip(block_index);
30-
return chain;
31-
}
32-
3325
static void RejectDifficultyMismatch(double difficulty, double expected_difficulty) {
3426
BOOST_CHECK_MESSAGE(
3527
DoubleEquals(difficulty, expected_difficulty, 0.00001),
@@ -43,12 +35,7 @@ static void RejectDifficultyMismatch(double difficulty, double expected_difficul
4335
static void TestDifficulty(uint32_t nbits, double expected_difficulty)
4436
{
4537
CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits);
46-
/* Since we are passing in block index explicitly,
47-
* there is no need to set up anything within the chain itself.
48-
*/
49-
CChain chain;
50-
51-
double difficulty = GetDifficulty(chain, block_index);
38+
double difficulty = GetDifficulty(block_index);
5239
delete block_index;
5340

5441
RejectDifficultyMismatch(difficulty, expected_difficulty);
@@ -84,43 +71,8 @@ BOOST_AUTO_TEST_CASE(get_difficulty_for_very_high_target)
8471
// Verify that difficulty is 1.0 for an empty chain.
8572
BOOST_AUTO_TEST_CASE(get_difficulty_for_null_tip)
8673
{
87-
CChain chain;
88-
double difficulty = GetDifficulty(chain, nullptr);
74+
double difficulty = GetDifficulty(nullptr);
8975
RejectDifficultyMismatch(difficulty, 1.0);
9076
}
9177

92-
/* Verify that if difficulty is based upon the block index
93-
* in the chain, if no block index is explicitly specified.
94-
*/
95-
BOOST_AUTO_TEST_CASE(get_difficulty_for_null_block_index)
96-
{
97-
CChain chain = CreateChainWithNbits(0x1df88f6f);
98-
99-
double difficulty = GetDifficulty(chain, nullptr);
100-
delete chain.Tip();
101-
102-
double expected_difficulty = 0.004023;
103-
104-
RejectDifficultyMismatch(difficulty, expected_difficulty);
105-
}
106-
107-
/* Verify that difficulty is based upon the explicitly specified
108-
* block index rather than being taken from the provided chain,
109-
* when both are present.
110-
*/
111-
BOOST_AUTO_TEST_CASE(get_difficulty_for_block_index_overrides_tip)
112-
{
113-
CChain chain = CreateChainWithNbits(0x1df88f6f);
114-
/* This block index's nbits should be used
115-
* instead of the chain's when calculating difficulty.
116-
*/
117-
CBlockIndex* override_block_index = CreateBlockIndexWithNbits(0x12345678);
118-
119-
double difficulty = GetDifficulty(chain, override_block_index);
120-
delete chain.Tip();
121-
delete override_block_index;
122-
123-
RejectDifficultyMismatch(difficulty, 5913134931067755359633408.0);
124-
}
125-
12678
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)