Skip to content

Commit bf7996c

Browse files
committed
rpc: fix getblock(header) returns target for tip
A target field was added to the getblock and getblockheader RPC calls in bitcoin#31583, but it mistakingly always used the tip value. Because regtest does not have difficulty adjustment, a test is added for mainnet instead.
1 parent 4c3c1f4 commit bf7996c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ UniValue blockheaderToJSON(const CBlockIndex& tip, const CBlockIndex& blockindex
166166
result.pushKV("mediantime", blockindex.GetMedianTimePast());
167167
result.pushKV("nonce", blockindex.nNonce);
168168
result.pushKV("bits", strprintf("%08x", blockindex.nBits));
169-
result.pushKV("target", GetTarget(tip, pow_limit).GetHex());
169+
result.pushKV("target", GetTarget(blockindex, pow_limit).GetHex());
170170
result.pushKV("difficulty", GetDifficulty(blockindex));
171171
result.pushKV("chainwork", blockindex.nChainWork.GetHex());
172172
result.pushKV("nTx", blockindex.nTx);

test/functional/mining_mainnet.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,17 @@ def run_test(self):
109109
height = 2016
110110
prev_hash = self.mine(height, prev_hash, blocks, node)
111111
assert_equal(node.getblockcount(), height)
112+
113+
mining_info = node.getmininginfo()
114+
assert_equal(mining_info['difficulty'], 4)
115+
116+
self.log.info("getblock RPC should show historical target")
117+
block_info = node.getblock(node.getblockhash(1))
118+
119+
assert_equal(block_info['difficulty'], 1)
120+
assert_equal(block_info['bits'], nbits_str(DIFF_1_N_BITS))
121+
assert_equal(block_info['target'], target_str(DIFF_1_TARGET))
122+
123+
112124
if __name__ == '__main__':
113125
MiningMainnetTest(__file__).main()

0 commit comments

Comments
 (0)