Skip to content

Commit 7ddbed4

Browse files
committed
rpc: add nBits to getmininginfo
Also expands nBits test coverage.
1 parent ba7b9f3 commit 7ddbed4

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

src/rpc/blockchain.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ static RPCHelpMan getblockheader()
553553
{RPCResult::Type::NUM_TIME, "time", "The block time expressed in " + UNIX_EPOCH_TIME},
554554
{RPCResult::Type::NUM_TIME, "mediantime", "The median block time expressed in " + UNIX_EPOCH_TIME},
555555
{RPCResult::Type::NUM, "nonce", "The nonce"},
556-
{RPCResult::Type::STR_HEX, "bits", "The bits"},
556+
{RPCResult::Type::STR_HEX, "bits", "nBits: compact representation of the block difficulty target"},
557557
{RPCResult::Type::NUM, "difficulty", "The difficulty"},
558558
{RPCResult::Type::STR_HEX, "chainwork", "Expected number of hashes required to produce the current chain"},
559559
{RPCResult::Type::NUM, "nTx", "The number of transactions in the block"},
@@ -727,7 +727,7 @@ static RPCHelpMan getblock()
727727
{RPCResult::Type::NUM_TIME, "time", "The block time expressed in " + UNIX_EPOCH_TIME},
728728
{RPCResult::Type::NUM_TIME, "mediantime", "The median block time expressed in " + UNIX_EPOCH_TIME},
729729
{RPCResult::Type::NUM, "nonce", "The nonce"},
730-
{RPCResult::Type::STR_HEX, "bits", "The bits"},
730+
{RPCResult::Type::STR_HEX, "bits", "nBits: compact representation of the block difficulty target"},
731731
{RPCResult::Type::NUM, "difficulty", "The difficulty"},
732732
{RPCResult::Type::STR_HEX, "chainwork", "Expected number of hashes required to produce the chain up to this block (in hex)"},
733733
{RPCResult::Type::NUM, "nTx", "The number of transactions in the block"},

src/rpc/mining.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ static RPCHelpMan getmininginfo()
421421
{RPCResult::Type::NUM, "blocks", "The current block"},
422422
{RPCResult::Type::NUM, "currentblockweight", /*optional=*/true, "The block weight of the last assembled block (only present if a block was ever assembled)"},
423423
{RPCResult::Type::NUM, "currentblocktx", /*optional=*/true, "The number of block transactions of the last assembled block (only present if a block was ever assembled)"},
424+
{RPCResult::Type::STR_HEX, "bits", "The current nBits, compact representation of the block difficulty target"},
424425
{RPCResult::Type::NUM, "difficulty", "The current difficulty"},
425426
{RPCResult::Type::NUM, "networkhashps", "The network hashes per second"},
426427
{RPCResult::Type::NUM, "pooledtx", "The size of the mempool"},
@@ -446,12 +447,14 @@ static RPCHelpMan getmininginfo()
446447
ChainstateManager& chainman = EnsureChainman(node);
447448
LOCK(cs_main);
448449
const CChain& active_chain = chainman.ActiveChain();
450+
CBlockIndex& tip{*CHECK_NONFATAL(active_chain.Tip())};
449451

450452
UniValue obj(UniValue::VOBJ);
451453
obj.pushKV("blocks", active_chain.Height());
452454
if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight);
453455
if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs);
454-
obj.pushKV("difficulty", GetDifficulty(*CHECK_NONFATAL(active_chain.Tip())));
456+
obj.pushKV("bits", strprintf("%08x", tip.nBits));
457+
obj.pushKV("difficulty", GetDifficulty(tip));
455458
obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
456459
obj.pushKV("pooledtx", (uint64_t)mempool.size());
457460
obj.pushKV("chain", chainman.GetParams().GetChainTypeString());

test/functional/mining_basic.py

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
get_witness_script,
1717
NORMAL_GBT_REQUEST_PARAMS,
1818
TIME_GENESIS_BLOCK,
19+
REGTEST_N_BITS,
20+
nbits_str,
1921
)
2022
from test_framework.messages import (
2123
BLOCK_HEADER_SIZE,
@@ -206,6 +208,7 @@ def assert_submitblock(block, result_str_1, result_str_2=None):
206208
assert_equal(mining_info['chain'], self.chain)
207209
assert 'currentblocktx' not in mining_info
208210
assert 'currentblockweight' not in mining_info
211+
assert_equal(mining_info['bits'], nbits_str(REGTEST_N_BITS))
209212
assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10'))
210213
assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
211214
assert_equal(mining_info['pooledtx'], 0)

test/functional/rpc_blockchain.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2014-2022 The Bitcoin Core developers
2+
# Copyright (c) 2014-present The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test RPCs related to blockchainstate.
@@ -30,9 +30,11 @@
3030
from test_framework.blocktools import (
3131
MAX_FUTURE_BLOCK_TIME,
3232
TIME_GENESIS_BLOCK,
33+
REGTEST_N_BITS,
3334
create_block,
3435
create_coinbase,
3536
create_tx_with_script,
37+
nbits_str,
3638
)
3739
from test_framework.messages import (
3840
CBlockHeader,
@@ -412,7 +414,7 @@ def _test_getblockheader(self):
412414
assert_is_hash_string(header['hash'])
413415
assert_is_hash_string(header['previousblockhash'])
414416
assert_is_hash_string(header['merkleroot'])
415-
assert_is_hash_string(header['bits'], length=None)
417+
assert_equal(header['bits'], nbits_str(REGTEST_N_BITS))
416418
assert isinstance(header['time'], int)
417419
assert_equal(header['mediantime'], TIME_RANGE_MTP)
418420
assert isinstance(header['nonce'], int)

test/functional/test_framework/blocktools.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4
6666
MIN_BLOCKS_TO_KEEP = 288
6767

68+
REGTEST_N_BITS = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams"
69+
70+
def nbits_str(nbits):
71+
return f"{nbits:08x}"
6872

6973
def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None):
7074
"""Create a block (with regtest difficulty)."""
@@ -77,7 +81,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
7781
if tmpl and tmpl.get('bits') is not None:
7882
block.nBits = struct.unpack('>I', bytes.fromhex(tmpl['bits']))[0]
7983
else:
80-
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
84+
block.nBits = REGTEST_N_BITS
8185
if coinbase is None:
8286
coinbase = create_coinbase(height=tmpl['height'])
8387
block.vtx.append(coinbase)

0 commit comments

Comments
 (0)