test: refactor IPC mining test to use script_BIP34_coinbase_height

Needed in a later commit to correctly derive the BIP34 prefix
for heights <= 16.

Add a padding parameter to script_BIP34_coinbase_height() that
controls whether the OP_0 dummy extranonce is appended for
heights <= 16.

Use this helper with padding=False in the IPC mining test's
build_coinbase_test().
This commit is contained in:
Sjors Provoost
2026-03-19 08:56:22 +01:00
parent 82733e61de
commit 1966621b76
2 changed files with 8 additions and 10 deletions

View File

@@ -7,7 +7,7 @@ import asyncio
import time
from contextlib import AsyncExitStack
from io import BytesIO
from test_framework.blocktools import NULL_OUTPOINT
from test_framework.blocktools import NULL_OUTPOINT, script_BIP34_coinbase_height
from test_framework.messages import (
MAX_BLOCK_WEIGHT,
CBlockHeader,
@@ -20,10 +20,6 @@ from test_framework.messages import (
from_hex,
msg_headers,
)
from test_framework.script import (
CScript,
CScriptNum,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
@@ -79,8 +75,8 @@ class IPCMiningTest(BitcoinTestFramework):
# Verify there's no dummy extraNonce in the coinbase scriptSig
current_block_height = self.nodes[0].getchaintips()[0]["height"]
expected_scriptsig = CScript([CScriptNum(current_block_height + 1)])
assert_equal(coinbase_res.scriptSigPrefix.hex(), expected_scriptsig.hex())
bip34_prefix = script_BIP34_coinbase_height(current_block_height + 1, padding=False)
assert_equal(coinbase_res.scriptSigPrefix, bip34_prefix)
# Typically a mining pool appends its name and an extraNonce
coinbase_tx.vin[0].scriptSig = coinbase_res.scriptSigPrefix

View File

@@ -161,11 +161,13 @@ def add_witness_commitment(block, nonce=0):
block.hashMerkleRoot = block.calc_merkle_root()
def script_BIP34_coinbase_height(height):
def script_BIP34_coinbase_height(height, *, padding=True):
if height <= 16:
res = CScriptOp.encode_op_n(height)
# Append dummy extraNonce to increase scriptSig size to 2 (see bad-cb-length consensus rule)
return CScript([res, OP_0])
if padding:
# Append dummy extraNonce to increase scriptSig size to 2 (see bad-cb-length consensus rule)
return CScript([res, OP_0])
return CScript([res])
return CScript([CScriptNum(height)])