Skip to content

Commit 2ce7b47

Browse files
committed
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)`.
1 parent 965e937 commit 2ce7b47

22 files changed

+257
-152
lines changed

contrib/signet/miner

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PATH_BASE_TEST_FUNCTIONAL = os.path.abspath(os.path.join(PATH_BASE_CONTRIB_SIGNE
2323
sys.path.insert(0, PATH_BASE_TEST_FUNCTIONAL)
2424

2525
from test_framework.blocktools import WITNESS_COMMITMENT_HEADER, script_BIP34_coinbase_height # noqa: E402
26-
from test_framework.messages import CBlock, CBlockHeader, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, ToHex, deser_string, hash256, ser_compact_size, ser_string, ser_uint256, uint256_from_str # noqa: E402
26+
from test_framework.messages import CBlock, CBlockHeader, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, ToHex, deser_string, hash256, ser_compact_size, ser_string, ser_uint256, tx_from_hex, uint256_from_str # noqa: E402
2727
from test_framework.script import CScriptOp # noqa: E402
2828

2929
logging.basicConfig(
@@ -216,7 +216,7 @@ def generate_psbt(tmpl, reward_spk, *, blocktime=None):
216216
block.nTime = tmpl["mintime"]
217217
block.nBits = int(tmpl["bits"], 16)
218218
block.nNonce = 0
219-
block.vtx = [cbtx] + [FromHex(CTransaction(), t["data"]) for t in tmpl["transactions"]]
219+
block.vtx = [cbtx] + [tx_from_hex(t["data"]) for t in tmpl["transactions"]]
220220

221221
witnonce = 0
222222
witroot = block.calc_witness_merkle_root()

test/functional/feature_bip68_sequence.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@
66

77
import time
88

9-
from test_framework.blocktools import create_block, NORMAL_GBT_REQUEST_PARAMS, add_witness_commitment
10-
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, ToHex
9+
from test_framework.blocktools import (
10+
NORMAL_GBT_REQUEST_PARAMS,
11+
add_witness_commitment,
12+
create_block,
13+
)
14+
from test_framework.messages import (
15+
COIN,
16+
COutPoint,
17+
CTransaction,
18+
CTxIn,
19+
CTxOut,
20+
ToHex,
21+
tx_from_hex,
22+
)
1123
from test_framework.test_framework import BitcoinTestFramework
1224
from test_framework.util import (
1325
assert_equal,
@@ -215,7 +227,7 @@ def test_sequence_lock_unconfirmed_inputs(self):
215227

216228
# Create a mempool tx.
217229
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2)
218-
tx1 = FromHex(CTransaction(), self.nodes[0].getrawtransaction(txid))
230+
tx1 = tx_from_hex(self.nodes[0].getrawtransaction(txid))
219231
tx1.rehash()
220232

221233
# Anyone-can-spend mempool tx.
@@ -225,7 +237,7 @@ def test_sequence_lock_unconfirmed_inputs(self):
225237
tx2.vin = [CTxIn(COutPoint(tx1.sha256, 0), nSequence=0)]
226238
tx2.vout = [CTxOut(int(tx1.vout[0].nValue - self.relayfee*COIN), DUMMY_P2WPKH_SCRIPT)]
227239
tx2_raw = self.nodes[0].signrawtransactionwithwallet(ToHex(tx2))["hex"]
228-
tx2 = FromHex(tx2, tx2_raw)
240+
tx2 = tx_from_hex(tx2_raw)
229241
tx2.rehash()
230242

231243
self.nodes[0].sendrawtransaction(tx2_raw)
@@ -348,7 +360,7 @@ def test_bip68_not_consensus(self):
348360
assert not softfork_active(self.nodes[0], 'csv')
349361
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 2)
350362

351-
tx1 = FromHex(CTransaction(), self.nodes[0].getrawtransaction(txid))
363+
tx1 = tx_from_hex(self.nodes[0].getrawtransaction(txid))
352364
tx1.rehash()
353365

354366
# Make an anyone-can-spend transaction
@@ -359,7 +371,7 @@ def test_bip68_not_consensus(self):
359371

360372
# sign tx2
361373
tx2_raw = self.nodes[0].signrawtransactionwithwallet(ToHex(tx2))["hex"]
362-
tx2 = FromHex(tx2, tx2_raw)
374+
tx2 = tx_from_hex(tx2_raw)
363375
tx2.rehash()
364376

365377
self.nodes[0].sendrawtransaction(ToHex(tx2))
@@ -404,7 +416,7 @@ def test_version2_relay(self):
404416
outputs = { self.nodes[1].getnewaddress() : 1.0 }
405417
rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
406418
rawtxfund = self.nodes[1].fundrawtransaction(rawtx)['hex']
407-
tx = FromHex(CTransaction(), rawtxfund)
419+
tx = tx_from_hex(rawtxfund)
408420
tx.nVersion = 2
409421
tx_signed = self.nodes[1].signrawtransactionwithwallet(ToHex(tx))["hex"]
410422
self.nodes[1].sendrawtransaction(tx_signed)

test/functional/feature_segwit.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"""Test the SegWit changeover logic."""
66

77
from decimal import Decimal
8-
from io import BytesIO
98

109
from test_framework.address import (
1110
key_to_p2pkh,
@@ -14,9 +13,35 @@
1413
script_to_p2sh_p2wsh,
1514
script_to_p2wsh,
1615
)
17-
from test_framework.blocktools import witness_script, send_to_witness
18-
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, sha256, ToHex
19-
from test_framework.script import CScript, OP_HASH160, OP_CHECKSIG, OP_0, hash160, OP_EQUAL, OP_DUP, OP_EQUALVERIFY, OP_1, OP_2, OP_CHECKMULTISIG, OP_TRUE, OP_DROP
16+
from test_framework.blocktools import (
17+
send_to_witness,
18+
witness_script,
19+
)
20+
from test_framework.messages import (
21+
COIN,
22+
COutPoint,
23+
CTransaction,
24+
CTxIn,
25+
CTxOut,
26+
ToHex,
27+
sha256,
28+
tx_from_hex,
29+
)
30+
from test_framework.script import (
31+
CScript,
32+
OP_0,
33+
OP_1,
34+
OP_2,
35+
OP_CHECKMULTISIG,
36+
OP_CHECKSIG,
37+
OP_DROP,
38+
OP_DUP,
39+
OP_EQUAL,
40+
OP_EQUALVERIFY,
41+
OP_HASH160,
42+
OP_TRUE,
43+
hash160,
44+
)
2045
from test_framework.test_framework import BitcoinTestFramework
2146
from test_framework.util import (
2247
assert_equal,
@@ -179,7 +204,7 @@ def run_test(self):
179204
assert self.nodes[1].getblock(blockhash, False) == self.nodes[2].getblock(blockhash, False)
180205

181206
for tx_id in segwit_tx_list:
182-
tx = FromHex(CTransaction(), self.nodes[2].gettransaction(tx_id)["hex"])
207+
tx = tx_from_hex(self.nodes[2].gettransaction(tx_id)["hex"])
183208
assert self.nodes[2].getrawtransaction(tx_id, False, blockhash) != self.nodes[0].getrawtransaction(tx_id, False, blockhash)
184209
assert self.nodes[1].getrawtransaction(tx_id, False, blockhash) == self.nodes[2].getrawtransaction(tx_id, False, blockhash)
185210
assert self.nodes[0].getrawtransaction(tx_id, False, blockhash) != self.nodes[2].gettransaction(tx_id)["hex"]
@@ -225,12 +250,12 @@ def run_test(self):
225250
# tx1 is allowed to appear in the block, but no others.
226251
txid1 = send_to_witness(1, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[0], False, Decimal("49.996"))
227252
hex_tx = self.nodes[0].gettransaction(txid)['hex']
228-
tx = FromHex(CTransaction(), hex_tx)
253+
tx = tx_from_hex(hex_tx)
229254
assert tx.wit.is_null() # This should not be a segwit input
230255
assert txid1 in self.nodes[0].getrawmempool()
231256

232257
tx1_hex = self.nodes[0].gettransaction(txid1)['hex']
233-
tx1 = FromHex(CTransaction(), tx1_hex)
258+
tx1 = tx_from_hex(tx1_hex)
234259

235260
# Check that wtxid is properly reported in mempool entry (txid1)
236261
assert_equal(int(self.nodes[0].getmempoolentry(txid1)["wtxid"], 16), tx1.calc_sha256(True))
@@ -245,7 +270,7 @@ def run_test(self):
245270
tx.vout.append(CTxOut(int(49.99 * COIN), CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
246271
tx2_hex = self.nodes[0].signrawtransactionwithwallet(ToHex(tx))['hex']
247272
txid2 = self.nodes[0].sendrawtransaction(tx2_hex)
248-
tx = FromHex(CTransaction(), tx2_hex)
273+
tx = tx_from_hex(tx2_hex)
249274
assert not tx.wit.is_null()
250275

251276
# Check that wtxid is properly reported in mempool entry (txid2)
@@ -611,10 +636,8 @@ def p2pkh_address_to_script(self, v):
611636
def create_and_mine_tx_from_txids(self, txids, success=True):
612637
tx = CTransaction()
613638
for i in txids:
614-
txtmp = CTransaction()
615639
txraw = self.nodes[0].getrawtransaction(i, 0, txs_mined[i])
616-
f = BytesIO(hex_str_to_bytes(txraw))
617-
txtmp.deserialize(f)
640+
txtmp = tx_from_hex(txraw)
618641
for j in range(len(txtmp.vout)):
619642
tx.vin.append(CTxIn(COutPoint(int('0x' + i, 0), j)))
620643
tx.vout.append(CTxOut(0, CScript()))

test/functional/interface_zmq.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE, ADDRESS_BCRT1_P2WSH_OP_TRUE
99
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment
1010
from test_framework.test_framework import BitcoinTestFramework
11-
from test_framework.messages import CTransaction, hash256, FromHex
11+
from test_framework.messages import (
12+
CTransaction,
13+
hash256,
14+
tx_from_hex,
15+
)
1216
from test_framework.util import (
1317
assert_equal,
1418
assert_raises_rpc_error,
@@ -393,10 +397,10 @@ def test_sequence(self):
393397
bump_info = self.nodes[0].bumpfee(orig_txid)
394398
# Mine the pre-bump tx
395399
block = create_block(int(self.nodes[0].getbestblockhash(), 16), create_coinbase(self.nodes[0].getblockcount()+1))
396-
tx = FromHex(CTransaction(), raw_tx)
400+
tx = tx_from_hex(raw_tx)
397401
block.vtx.append(tx)
398402
for txid in more_tx:
399-
tx = FromHex(CTransaction(), self.nodes[0].getrawtransaction(txid))
403+
tx = tx_from_hex(self.nodes[0].getrawtransaction(txid))
400404
block.vtx.append(tx)
401405
add_witness_commitment(block)
402406
block.solve()

0 commit comments

Comments
 (0)