aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-06-24 12:47:04 +0200
committerMarcoFalke <falke.marco@gmail.com>2021-06-24 12:47:13 +0200
commitd6a59166a1879c1dd5b3a301847961f4b3f17742 (patch)
tree17408a0a952372e1cfe16b41ceca50dd866be085 /test/functional/test_framework
parentc31161f4f79b6e1dc0f8bfc2a5e7815e8c55ed91 (diff)
parentbdb8b9a347e68f80a2e8d44ce5590a2e8214b6bb (diff)
downloadbitcoin-d6a59166a1879c1dd5b3a301847961f4b3f17742.tar.xz
Merge bitcoin/bitcoin#22257: test: refactor: various (de)serialization helpers cleanups/improvements
bdb8b9a347e68f80a2e8d44ce5590a2e8214b6bb test: doc: improve doc for `from_hex` helper (mention `to_hex` alternative) (Sebastian Falbesoner) 191405420815d49ab50184513717a303fc2744d6 scripted-diff: test: rename `FromHex` to `from_hex` (Sebastian Falbesoner) a79396fe5f8f81c78cf84117a87074c6ff6c9d95 test: remove `ToHex` helper, use .serialize().hex() instead (Sebastian Falbesoner) 2ce7b47958c4a10ba20dc86c011d71cda4b070a5 test: introduce `tx_from_hex` helper for tx deserialization (Sebastian Falbesoner) Pull request description: There are still many functional tests that perform conversions from a hex-string to a message object (deserialization) manually. This PR identifies all those instances and replaces them with a newly introduced helper `tx_from_hex`. Instances were found via * `git grep "deserialize.*BytesIO"` and some of them manually, when it were not one-liners. Further, the helper `ToHex` was removed and simply replaced by `.serialize().hex()`, since now both variants are in use (sometimes even within the same test) and using the helper doesn't really have an advantage in readability. (see discussion https://github.com/bitcoin/bitcoin/pull/22257#discussion_r652404782) ACKs for top commit: MarcoFalke: review re-ACK bdb8b9a347e68f80a2e8d44ce5590a2e8214b6bb 😁 Tree-SHA512: e25d7dc85918de1d6755a5cea65471b07a743204c20ad1c2f71ff07ef48cc1b9ad3fe5f515c1efaba2b2e3d89384e7980380c5d81895f9826e2046808cd3266e
Diffstat (limited to 'test/functional/test_framework')
-rw-r--r--test/functional/test_framework/blocktools.py11
-rwxr-xr-xtest/functional/test_framework/messages.py16
-rw-r--r--test/functional/test_framework/util.py6
3 files changed, 18 insertions, 15 deletions
diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py
index e91b44e776..bc43438810 100644
--- a/test/functional/test_framework/blocktools.py
+++ b/test/functional/test_framework/blocktools.py
@@ -23,12 +23,11 @@ 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 +78,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 +165,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):
@@ -243,9 +242,9 @@ 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)
+ tx_to_witness = tx.serialize().hex()
return node.sendrawtransaction(tx_to_witness)
diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py
index 3d79174356..504c8c70d4 100755
--- a/test/functional/test_framework/messages.py
+++ b/test/functional/test_framework/messages.py
@@ -190,14 +190,20 @@ def ser_string_vector(l):
return r
-# Deserialize from a hex string representation (eg from RPC)
-def FromHex(obj, hex_string):
+def from_hex(obj, hex_string):
+ """Deserialize from a hex string representation (e.g. from RPC)
+
+ Note that there is no complementary helper like e.g. `to_hex` for the
+ inverse operation. To serialize a message object to a hex string, simply
+ use obj.serialize().hex()"""
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 from_hex(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()