test: extract bulk_vout from bulk_tx so it can be used by wallet tests

This commit is contained in:
ishaanam
2025-07-26 02:41:29 -04:00
parent 2cb473d9f2
commit 5d932e14db
2 changed files with 16 additions and 12 deletions

View File

@@ -13,6 +13,7 @@ from test_framework.messages import (
CTxIn,
CTxInWitness,
CTxOut,
ser_compact_size,
sha256,
)
from test_framework.script import (
@@ -35,6 +36,8 @@ from test_framework.script import (
hash160,
)
from test_framework.util import assert_equal
# Maximum number of potentially executed legacy signature operations in validating a transaction.
MAX_STD_LEGACY_SIGOPS = 2_500
@@ -128,6 +131,16 @@ def script_to_p2sh_p2wsh_script(script):
p2shscript = CScript([OP_0, sha256(script)])
return script_to_p2sh_script(p2shscript)
def bulk_vout(tx, target_vsize):
if target_vsize < tx.get_vsize():
raise RuntimeError(f"target_vsize {target_vsize} is less than transaction virtual size {tx.get_vsize()}")
# determine number of needed padding bytes
dummy_vbytes = target_vsize - tx.get_vsize()
# compensate for the increase of the compact-size encoded script length
# (note that the length encoding of the unpadded output script needs one byte)
dummy_vbytes -= len(ser_compact_size(dummy_vbytes)) - 1
tx.vout[-1].scriptPubKey = CScript([OP_RETURN] + [OP_1] * dummy_vbytes)
assert_equal(tx.get_vsize(), target_vsize)
def output_key_to_p2tr_script(key):
assert len(key) == 32

View File

@@ -33,11 +33,9 @@ from test_framework.messages import (
CTxInWitness,
CTxOut,
hash256,
ser_compact_size,
)
from test_framework.script import (
CScript,
OP_1,
OP_NOP,
OP_RETURN,
OP_TRUE,
@@ -45,6 +43,7 @@ from test_framework.script import (
taproot_construct,
)
from test_framework.script_util import (
bulk_vout,
key_to_p2pk_script,
key_to_p2pkh_script,
key_to_p2sh_p2wpkh_script,
@@ -121,17 +120,9 @@ class MiniWallet:
"""Pad a transaction with extra outputs until it reaches a target vsize.
returns the tx
"""
if target_vsize < tx.get_vsize():
raise RuntimeError(f"target_vsize {target_vsize} is less than transaction virtual size {tx.get_vsize()}")
tx.vout.append(CTxOut(nValue=0, scriptPubKey=CScript([OP_RETURN])))
# determine number of needed padding bytes
dummy_vbytes = target_vsize - tx.get_vsize()
# compensate for the increase of the compact-size encoded script length
# (note that the length encoding of the unpadded output script needs one byte)
dummy_vbytes -= len(ser_compact_size(dummy_vbytes)) - 1
tx.vout[-1].scriptPubKey = CScript([OP_RETURN] + [OP_1] * dummy_vbytes)
assert_equal(tx.get_vsize(), target_vsize)
bulk_vout(tx, target_vsize)
def get_balance(self):
return sum(u['value'] for u in self._utxos)