Add target to getblock(header) in RPC and REST

This commit is contained in:
Sjors Provoost
2025-01-03 10:54:45 +01:00
parent 341f932516
commit 2a7bfebd5e
7 changed files with 30 additions and 16 deletions

View File

@@ -289,7 +289,7 @@ class RESTTest (BitcoinTestFramework):
# Compare with normal RPC block response
rpc_block_json = self.nodes[0].getblock(bb_hash)
for key in ['hash', 'confirmations', 'height', 'version', 'merkleroot', 'time', 'nonce', 'bits', 'difficulty', 'chainwork', 'previousblockhash']:
for key in ['hash', 'confirmations', 'height', 'version', 'merkleroot', 'time', 'nonce', 'bits', 'target', 'difficulty', 'chainwork', 'previousblockhash']:
assert_equal(json_obj[0][key], rpc_block_json[key])
# See if we can get 5 headers in one response

View File

@@ -31,10 +31,12 @@ from test_framework.blocktools import (
MAX_FUTURE_BLOCK_TIME,
TIME_GENESIS_BLOCK,
REGTEST_N_BITS,
REGTEST_TARGET,
create_block,
create_coinbase,
create_tx_with_script,
nbits_str,
target_str,
)
from test_framework.messages import (
CBlockHeader,
@@ -415,6 +417,7 @@ class BlockchainTest(BitcoinTestFramework):
assert_is_hash_string(header['previousblockhash'])
assert_is_hash_string(header['merkleroot'])
assert_equal(header['bits'], nbits_str(REGTEST_N_BITS))
assert_equal(header['target'], target_str(REGTEST_TARGET))
assert isinstance(header['time'], int)
assert_equal(header['mediantime'], TIME_RANGE_MTP)
assert isinstance(header['nonce'], int)

View File

@@ -27,6 +27,7 @@ from .messages import (
hash256,
ser_uint256,
tx_from_hex,
uint256_from_compact,
uint256_from_str,
WITNESS_SCALE_FACTOR,
)
@@ -66,10 +67,15 @@ VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4
MIN_BLOCKS_TO_KEEP = 288
REGTEST_N_BITS = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams"
REGTEST_TARGET = 0x7fffff0000000000000000000000000000000000000000000000000000000000
assert_equal(uint256_from_compact(REGTEST_N_BITS), REGTEST_TARGET)
def nbits_str(nbits):
return f"{nbits:08x}"
def target_str(target):
return f"{target:064x}"
def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None):
"""Create a block (with regtest difficulty)."""
block = CBlock()