diff options
author | Sebastian Falbesoner <sebastian.falbesoner@gmail.com> | 2021-06-15 23:02:28 +0200 |
---|---|---|
committer | Sebastian Falbesoner <sebastian.falbesoner@gmail.com> | 2021-06-21 14:28:05 +0200 |
commit | 2ce7b47958c4a10ba20dc86c011d71cda4b070a5 (patch) | |
tree | 9d3ad9433f7b5b054f5b072ebf33462399d9f502 /test/functional/test_framework | |
parent | 965e93743454112c0c3c66bf24852f63ee07b862 (diff) | |
download | bitcoin-2ce7b47958c4a10ba20dc86c011d71cda4b070a5.tar.xz |
test: introduce `tx_from_hex` helper for tx deserialization
`FromHex` is mostly used for transactions, so we introduce a
shortcut `tx_from_hex` for `FromHex(CTransaction, hex_str)`.
Diffstat (limited to 'test/functional/test_framework')
-rw-r--r-- | test/functional/test_framework/blocktools.py | 8 | ||||
-rwxr-xr-x | test/functional/test_framework/messages.py | 7 | ||||
-rw-r--r-- | test/functional/test_framework/util.py | 6 |
3 files changed, 13 insertions, 8 deletions
diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py index f35ea6c122..0019b7a426 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -23,12 +23,12 @@ from .messages import ( CTxIn, CTxInWitness, CTxOut, - FromHex, ToHex, hash256, hex_str_to_bytes, ser_uint256, sha256, + tx_from_hex, uint256_from_str, ) from .script import ( @@ -79,7 +79,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl if txlist: for tx in txlist: if not hasattr(tx, 'calc_sha256'): - tx = FromHex(CTransaction(), tx) + tx = tx_from_hex(tx) block.vtx.append(tx) block.hashMerkleRoot = block.calc_merkle_root() block.calc_sha256() @@ -166,7 +166,7 @@ def create_transaction(node, txid, to_address, *, amount): sign for the output that is being spent. """ raw_tx = create_raw_transaction(node, txid, to_address, amount=amount) - tx = FromHex(CTransaction(), raw_tx) + tx = tx_from_hex(raw_tx) return tx def create_raw_transaction(node, txid, to_address, *, amount): @@ -248,7 +248,7 @@ def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=Tru return node.sendrawtransaction(signed["hex"]) else: if (insert_redeem_script): - tx = FromHex(CTransaction(), tx_to_witness) + tx = tx_from_hex(tx_to_witness) tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)]) tx_to_witness = ToHex(tx) diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 3d79174356..43c80fa7a8 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -195,10 +195,17 @@ def FromHex(obj, hex_string): obj.deserialize(BytesIO(hex_str_to_bytes(hex_string))) return obj + # Convert a binary-serializable object to hex (eg for submission via RPC) def ToHex(obj): return obj.serialize().hex() + +def tx_from_hex(hex_string): + """Deserialize from hex string to a transaction object""" + return FromHex(CTransaction(), hex_string) + + # Objects that map to bitcoind objects, which can be serialized/deserialized diff --git a/test/functional/test_framework/util.py b/test/functional/test_framework/util.py index f354ae0075..35dbfbba8d 100644 --- a/test/functional/test_framework/util.py +++ b/test/functional/test_framework/util.py @@ -19,7 +19,6 @@ import unittest from . import coverage from .authproxy import AuthServiceProxy, JSONRPCException -from io import BytesIO from typing import Callable, Optional logger = logging.getLogger("TestFramework.utils") @@ -528,7 +527,7 @@ def gen_return_txouts(): def create_lots_of_big_transactions(node, txouts, utxos, num, fee): addr = node.getnewaddress() txids = [] - from .messages import CTransaction + from .messages import tx_from_hex for _ in range(num): t = utxos.pop() inputs = [{"txid": t["txid"], "vout": t["vout"]}] @@ -536,8 +535,7 @@ def create_lots_of_big_transactions(node, txouts, utxos, num, fee): change = t['amount'] - fee outputs[addr] = satoshi_round(change) rawtx = node.createrawtransaction(inputs, outputs) - tx = CTransaction() - tx.deserialize(BytesIO(hex_str_to_bytes(rawtx))) + tx = tx_from_hex(rawtx) for txout in txouts: tx.vout.append(txout) newtx = tx.serialize().hex() |