rpc: add nBits to getmininginfo

Also expands nBits test coverage.
This commit is contained in:
Sjors Provoost
2025-01-08 13:13:08 +01:00
parent ba7b9f3d7b
commit 7ddbed4f9f
5 changed files with 18 additions and 6 deletions

View File

@@ -16,6 +16,8 @@ from test_framework.blocktools import (
get_witness_script,
NORMAL_GBT_REQUEST_PARAMS,
TIME_GENESIS_BLOCK,
REGTEST_N_BITS,
nbits_str,
)
from test_framework.messages import (
BLOCK_HEADER_SIZE,
@@ -206,6 +208,7 @@ class MiningTest(BitcoinTestFramework):
assert_equal(mining_info['chain'], self.chain)
assert 'currentblocktx' not in mining_info
assert 'currentblockweight' not in mining_info
assert_equal(mining_info['bits'], nbits_str(REGTEST_N_BITS))
assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10'))
assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
assert_equal(mining_info['pooledtx'], 0)

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2022 The Bitcoin Core developers
# Copyright (c) 2014-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test RPCs related to blockchainstate.
@@ -30,9 +30,11 @@ import textwrap
from test_framework.blocktools import (
MAX_FUTURE_BLOCK_TIME,
TIME_GENESIS_BLOCK,
REGTEST_N_BITS,
create_block,
create_coinbase,
create_tx_with_script,
nbits_str,
)
from test_framework.messages import (
CBlockHeader,
@@ -412,7 +414,7 @@ class BlockchainTest(BitcoinTestFramework):
assert_is_hash_string(header['hash'])
assert_is_hash_string(header['previousblockhash'])
assert_is_hash_string(header['merkleroot'])
assert_is_hash_string(header['bits'], length=None)
assert_equal(header['bits'], nbits_str(REGTEST_N_BITS))
assert isinstance(header['time'], int)
assert_equal(header['mediantime'], TIME_RANGE_MTP)
assert isinstance(header['nonce'], int)

View File

@@ -65,6 +65,10 @@ NORMAL_GBT_REQUEST_PARAMS = {"rules": ["segwit"]}
VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4
MIN_BLOCKS_TO_KEEP = 288
REGTEST_N_BITS = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams"
def nbits_str(nbits):
return f"{nbits:08x}"
def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None):
"""Create a block (with regtest difficulty)."""
@@ -77,7 +81,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
if tmpl and tmpl.get('bits') is not None:
block.nBits = struct.unpack('>I', bytes.fromhex(tmpl['bits']))[0]
else:
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
block.nBits = REGTEST_N_BITS
if coinbase is None:
coinbase = create_coinbase(height=tmpl['height'])
block.vtx.append(coinbase)