diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -8,6 +8,7 @@ from .script import CScript, OP_TRUE, OP_CHECKSIG, OP_RETURN, OP_PUSHDATA2, OP_DUP, OP_HASH160, OP_EQUALVERIFY from .mininode import CTransaction, CTxOut, CTxIn from .util import satoshi_round +from .txtools import pad_tx # Create a block (with regtest difficulty) @@ -62,9 +63,7 @@ coinbase.vout = [coinbaseoutput] # Make sure the coinbase is at least 100 bytes - coinbase_size = len(coinbase.serialize()) - if coinbase_size < 100: - coinbase.vin[0].scriptSig += b'x' * (100 - coinbase_size) + pad_tx(coinbase) coinbase.calc_sha256() return coinbase @@ -78,6 +77,7 @@ assert(n < len(prevtx.vout)) tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff)) tx.vout.append(CTxOut(value, scriptPubKey)) + pad_tx(tx) tx.calc_sha256() return tx diff --git a/test/functional/test_framework/cdefs.py b/test/functional/test_framework/cdefs.py --- a/test/functional/test_framework/cdefs.py +++ b/test/functional/test_framework/cdefs.py @@ -88,6 +88,9 @@ # Minimum size a transaction can have. MIN_TX_SIZE = 100 +# Maximum bytes in a TxOut pubkey script +MAX_TXOUT_PUBKEY_SCRIPT = 10000 + if __name__ == "__main__": # Output values if run standalone to verify print("DEFAULT_MAX_BLOCK_SIZE = %d (bytes)" % DEFAULT_MAX_BLOCK_SIZE) diff --git a/test/functional/test_framework/txtools.py b/test/functional/test_framework/txtools.py new file mode 100644 --- /dev/null +++ b/test/functional/test_framework/txtools.py @@ -0,0 +1,41 @@ +from .cdefs import MIN_TX_SIZE, MAX_TXOUT_PUBKEY_SCRIPT +from .mininode import CTransaction, FromHex, ToHex, CTxOut +from .script import OP_RETURN, CScript + +import random +from binascii import hexlify, unhexlify + +# Pad outputs until it reaches at least min_size + + +def pad_tx(tx, min_size=None): + if min_size is None: + min_size = MIN_TX_SIZE + + curr_size = len(tx.serialize()) + + while curr_size < min_size: + # txout.value + txout.pk_script bytes + op_return + extra_bytes = 8 + 1 + 1 + padding_len = max(0, min_size - curr_size - extra_bytes) + padding_len = min(padding_len, MAX_TXOUT_PUBKEY_SCRIPT) + if padding_len == 0: + tx.vout.append(CTxOut(0, CScript([OP_RETURN]))) + else: + padding = random.randrange( + 1 << 8 * padding_len - 1, 1 << 8 * padding_len) + tx.vout.append( + CTxOut(0, CScript([padding, OP_RETURN]))) + curr_size = len(tx.serialize()) + + tx.rehash() + +# Pad outputs until it reaches at least min_size + + +def pad_raw_tx(rawtx_hex, min_size=None): + + tx = CTransaction() + FromHex(tx, rawtx_hex) + pad_tx(tx, min_size) + return ToHex(tx)