Skip to content

Commit a589f53

Browse files
committed
Merge #13288: rpc: Remove the need to include rpc/blockchain.cpp in order to put GetDifficulty under test
ebec731 Drop the chain argument to GetDifficulty (Ben Woosley) Pull request description: By dropping the chain argument to `GetDifficulty`. `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. Introduced in: #11748 Tree-SHA512: f2c97014be185f3e3de92db15848548650e4a67fab20a41bcfa851c5c63c245915cbe9380f84d9da2081e8756d31a41de417db1d35cfecf41ddb4f25070eb525
2 parents 264efdc + ebec731 commit a589f53

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>
@@ -49,17 +48,13 @@ static std::mutex cs_blockchange;
4948
static std::condition_variable cond_blockchange;
5049
static CUpdatedBlock latestblock;
5150

52-
/* Calculate the difficulty for a given block index,
53-
* or the block index of the given chain.
51+
/* Calculate the difficulty for a given block index.
5452
*/
55-
double GetDifficulty(const CChain& chain, const CBlockIndex* blockindex)
53+
double GetDifficulty(const CBlockIndex* blockindex)
5654
{
5755
if (blockindex == nullptr)
5856
{
59-
if (chain.Tip() == nullptr)
60-
return 1.0;
61-
else
62-
blockindex = chain.Tip();
57+
return 1.0;
6358
}
6459

6560
int nShift = (blockindex->nBits >> 24) & 0xff;
@@ -80,11 +75,6 @@ double GetDifficulty(const CChain& chain, const CBlockIndex* blockindex)
8075
return dDiff;
8176
}
8277

83-
double GetDifficulty(const CBlockIndex* blockindex)
84-
{
85-
return GetDifficulty(chainActive, blockindex);
86-
}
87-
8878
UniValue blockheaderToJSON(const CBlockIndex* blockindex)
8979
{
9080
AssertLockHeld(cs_main);
@@ -354,7 +344,7 @@ static UniValue getdifficulty(const JSONRPCRequest& request)
354344
);
355345

356346
LOCK(cs_main);
357-
return GetDifficulty();
347+
return GetDifficulty(chainActive.Tip());
358348
}
359349

360350
static std::string EntryDescriptionString()
@@ -1240,7 +1230,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
12401230
obj.pushKV("blocks", (int)chainActive.Height());
12411231
obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1);
12421232
obj.pushKV("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex());
1243-
obj.pushKV("difficulty", (double)GetDifficulty());
1233+
obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip()));
12441234
obj.pushKV("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast());
12451235
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip()));
12461236
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)