test: Allow to set height in create_block
What changed, and why it matters
This is a harmless test-framework cleanup. It lets Bitcoin Core's internal test helper create_block accept a block height directly, instead of forcing test writers to manually build a coinbase transaction first. It only touches test code and adds a unit test for the new convenience option.
No security action needed. This is a routine test-only refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies test/functional/test_framework/blocktools.py so create_block takes an optional keyword-only height parameter. When no coinbase is supplied, create_coinbase is called with height or tmpl[‘height’] as a fallback. A unit test verifies the explicit height overrides the template height. One functional test (interface_zmq.py) is updated to use the new parameter. No consensus, networking, or wallet code is changed.
Changed components
test/functional/test_framework/blocktools.pytest/functional/interface_zmq.pyInspect captured patch +11 / −4
diff --git a/test/functional/interface_zmq.py b/test/functional/interface_zmq.py
index f8a6bfc6..c9baad33 100755
--- a/test/functional/interface_zmq.py
+++ b/test/functional/interface_zmq.py
@@ -15,7 +15,6 @@ from test_framework.address import (
from test_framework.blocktools import (
add_witness_commitment,
create_block,
- create_coinbase,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.messages import (
@@ -419,7 +418,7 @@ class ZMQTest (BitcoinTestFramework):
bump_txid = self.nodes[0].sendrawtransaction(orig_tx['tx'].serialize().hex())
# Mine the pre-bump tx
txs_to_add = [orig_tx['hex']] + [tx['hex'] for tx in more_tx]
- block = create_block(int(self.nodes[0].getbestblockhash(), 16), create_coinbase(self.nodes[0].getblockcount()+1), txlist=txs_to_add)
+ block = create_block(int(self.nodes[0].getbestblockhash(), 16), height=self.nodes[0].getblockcount() + 1, txlist=txs_to_add)
add_witness_commitment(block)
block.solve()
assert_equal(self.nodes[0].submitblock(block.serialize().hex()), None)
diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py
index cd2caa18..eb1baadf 100644
--- a/test/functional/test_framework/blocktools.py
+++ b/test/functional/test_framework/blocktools.py
@@ -95,7 +95,7 @@ def nbits_str(nbits):
def target_str(target):
return f"{target:064x}"
-def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None):
+def create_block(hashprev=None, coinbase=None, ntime=None, *, height=None, version=None, tmpl=None, txlist=None):
"""Create a block (with regtest difficulty)."""
block = CBlock()
if tmpl is None:
@@ -108,7 +108,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
else:
block.nBits = REGTEST_N_BITS
if coinbase is None:
- coinbase = create_coinbase(height=tmpl['height'])
+ coinbase = create_coinbase(height=height or tmpl["height"])
block.vtx.append(coinbase)
if txlist:
for tx in txlist:
@@ -278,6 +278,14 @@ def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=Tru
return node.sendrawtransaction(tx_to_witness)
class TestFrameworkBlockTools(unittest.TestCase):
+ def test_create_block_prefers_explicit_height(self):
+ block = create_block(
+ hashprev=1,
+ tmpl={"height": 100},
+ height=200,
+ )
+ assert_equal(CScriptNum.decode(block.vtx[0].vin[0].scriptSig), 200)
+
def test_create_coinbase(self):
height = 20
coinbase_tx = create_coinbase(height=height)
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.