Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/txtools.py
from .cdefs import MIN_TX_SIZE, MAX_TXOUT_PUBKEY_SCRIPT | from .cdefs import MIN_TX_SIZE, MAX_TXOUT_PUBKEY_SCRIPT | ||||
from .mininode import CTransaction, FromHex, ToHex, CTxOut | from .mininode import CTransaction, FromHex, ToHex, CTxOut | ||||
from .script import OP_RETURN, CScript | from .script import OP_RETURN, CScript | ||||
import random | import random | ||||
from binascii import hexlify, unhexlify | from binascii import hexlify, unhexlify | ||||
def pad_tx(tx, pad_to_size=MIN_TX_SIZE): | def pad_tx(tx, pad_to_size=MIN_TX_SIZE): | ||||
""" | """ | ||||
Pad a transaction with op_return junk data until it is at least pad_to_size, or | Pad a transaction with op_return junk data until it is at least pad_to_size, or | ||||
leave it alone if it's already bigger than that. | leave it alone if it's already bigger than that. | ||||
""" | """ | ||||
curr_size = len(tx.serialize()) | curr_size = len(tx.serialize()) | ||||
if curr_size >= pad_to_size: | if curr_size >= pad_to_size: | ||||
# Bail early txn is already big enough | # Bail early txn is already big enough | ||||
return | return | ||||
# This code attempts to pad a transaction with opreturn vouts such that | # This code attempts to pad a transaction with opreturn vouts such that | ||||
Show All 28 Lines | while required_padding > 0: | ||||
if padding_len <= extra_bytes: | if padding_len <= extra_bytes: | ||||
tx.vout.append(CTxOut(0, CScript([OP_RETURN]))) | tx.vout.append(CTxOut(0, CScript([OP_RETURN]))) | ||||
else: | else: | ||||
# Subtract the overhead for the TxOut | # Subtract the overhead for the TxOut | ||||
padding_len -= extra_bytes | padding_len -= extra_bytes | ||||
padding = random.randrange( | padding = random.randrange( | ||||
1 << 8 * padding_len - 2, 1 << 8 * padding_len - 1) | 1 << 8 * padding_len - 2, 1 << 8 * padding_len - 1) | ||||
tx.vout.append( | tx.vout.append( | ||||
CTxOut(0, CScript([padding, OP_RETURN]))) | CTxOut(0, CScript([OP_RETURN, padding]))) | ||||
curr_size = len(tx.serialize()) | curr_size = len(tx.serialize()) | ||||
required_padding = pad_to_size - curr_size | required_padding = pad_to_size - curr_size | ||||
assert curr_size >= pad_to_size, "{} !>= {}".format(curr_size, pad_to_size) | assert curr_size >= pad_to_size, "{} !>= {}".format(curr_size, pad_to_size) | ||||
tx.rehash() | tx.rehash() | ||||
def pad_raw_tx(rawtx_hex, min_size=MIN_TX_SIZE): | def pad_raw_tx(rawtx_hex, min_size=MIN_TX_SIZE): | ||||
""" | """ | ||||
Pad a raw transaction with OP_RETURN data until it reaches at least min_size | Pad a raw transaction with OP_RETURN data until it reaches at least min_size | ||||
""" | """ | ||||
tx = CTransaction() | tx = CTransaction() | ||||
FromHex(tx, rawtx_hex) | FromHex(tx, rawtx_hex) | ||||
pad_tx(tx, min_size) | pad_tx(tx, min_size) | ||||
return ToHex(tx) | return ToHex(tx) |