From 5d932e14dbe41c349ab41f88088398e0ab10d335 Mon Sep 17 00:00:00 2001 From: ishaanam Date: Sat, 26 Jul 2025 02:41:29 -0400 Subject: [PATCH] test: extract `bulk_vout` from `bulk_tx` so it can be used by wallet tests --- test/functional/test_framework/script_util.py | 13 +++++++++++++ test/functional/test_framework/wallet.py | 15 +++------------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/test/functional/test_framework/script_util.py b/test/functional/test_framework/script_util.py index 975785798f3..5736eb6bc49 100755 --- a/test/functional/test_framework/script_util.py +++ b/test/functional/test_framework/script_util.py @@ -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 diff --git a/test/functional/test_framework/wallet.py b/test/functional/test_framework/wallet.py index ab462f79a2e..a47ccab01cf 100644 --- a/test/functional/test_framework/wallet.py +++ b/test/functional/test_framework/wallet.py @@ -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)