Skip to content

Commit 313c09f

Browse files
committed
[test] helper function to increase transaction weight
1 parent f8253d6 commit 313c09f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

test/functional/test_framework/wallet.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""A limited-functionality wallet, which may replace a real wallet in tests"""
66

7+
from copy import deepcopy
78
from decimal import Decimal
89
from enum import Enum
10+
from random import choice
911
from typing import Optional
1012
from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
1113
from test_framework.key import ECKey
@@ -28,6 +30,7 @@
2830
)
2931
from test_framework.util import (
3032
assert_equal,
33+
assert_greater_than_or_equal,
3134
satoshi_round,
3235
)
3336

@@ -229,3 +232,23 @@ def create_raw_chain(node, first_coin, address, privkeys, chain_length=25):
229232
chain_txns.append(tx)
230233

231234
return (chain_hex, chain_txns)
235+
236+
def bulk_transaction(tx, node, target_weight, privkeys, prevtxs=None):
237+
"""Pad a transaction with extra outputs until it reaches a target weight (or higher).
238+
returns CTransaction object
239+
"""
240+
tx_heavy = deepcopy(tx)
241+
assert_greater_than_or_equal(target_weight, tx_heavy.get_weight())
242+
while tx_heavy.get_weight() < target_weight:
243+
random_spk = "6a4d0200" # OP_RETURN OP_PUSH2 512 bytes
244+
for _ in range(512*2):
245+
random_spk += choice("0123456789ABCDEF")
246+
tx_heavy.vout.append(CTxOut(0, bytes.fromhex(random_spk)))
247+
# Re-sign the transaction
248+
if privkeys:
249+
signed = node.signrawtransactionwithkey(tx_heavy.serialize().hex(), privkeys, prevtxs)
250+
return tx_from_hex(signed["hex"])
251+
# OP_TRUE
252+
tx_heavy.wit.vtxinwit = [CTxInWitness()]
253+
tx_heavy.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])]
254+
return tx_heavy

0 commit comments

Comments
 (0)