mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-18 22:35:39 +01:00
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)`.
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user