|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """A limited-functionality wallet, which may replace a real wallet in tests"""
|
6 | 6 |
|
| 7 | +from copy import deepcopy |
7 | 8 | from decimal import Decimal
|
8 | 9 | from enum import Enum
|
| 10 | +from random import choice |
9 | 11 | from typing import Optional
|
10 | 12 | from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE
|
11 | 13 | from test_framework.key import ECKey
|
|
28 | 30 | )
|
29 | 31 | from test_framework.util import (
|
30 | 32 | assert_equal,
|
| 33 | + assert_greater_than_or_equal, |
31 | 34 | satoshi_round,
|
32 | 35 | )
|
33 | 36 |
|
@@ -229,3 +232,23 @@ def create_raw_chain(node, first_coin, address, privkeys, chain_length=25):
|
229 | 232 | chain_txns.append(tx)
|
230 | 233 |
|
231 | 234 | 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