Skip to content

Commit ca6c154

Browse files
committed
test: refactor: remove hex_str_to_bytes helper
Use the built-in class method bytes.fromhex() instead, which is available since Python 3.0.
1 parent f2e41d1 commit ca6c154

15 files changed

+40
-57
lines changed

test/functional/feature_dbcrash.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from test_framework.util import (
4242
assert_equal,
4343
create_confirmed_utxos,
44-
hex_str_to_bytes,
4544
)
4645

4746

@@ -204,7 +203,7 @@ def generate_small_transactions(self, node, count, utxo_list):
204203
continue
205204

206205
for _ in range(3):
207-
tx.vout.append(CTxOut(output_amount, hex_str_to_bytes(utxo['scriptPubKey'])))
206+
tx.vout.append(CTxOut(output_amount, bytes.fromhex(utxo['scriptPubKey'])))
208207

209208
# Sign and send the transaction to get into the mempool
210209
tx_signed_hex = node.signrawtransactionwithwallet(tx.serialize().hex())['hex']

test/functional/feature_segwit.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
assert_equal,
4747
assert_is_hex_string,
4848
assert_raises_rpc_error,
49-
hex_str_to_bytes,
5049
try_rpc,
5150
)
5251

@@ -140,7 +139,7 @@ def run_test(self):
140139
for i in range(3):
141140
newaddress = self.nodes[i].getnewaddress()
142141
self.pubkey.append(self.nodes[i].getaddressinfo(newaddress)["pubkey"])
143-
multiscript = CScript([OP_1, hex_str_to_bytes(self.pubkey[-1]), OP_1, OP_CHECKMULTISIG])
142+
multiscript = CScript([OP_1, bytes.fromhex(self.pubkey[-1]), OP_1, OP_CHECKMULTISIG])
144143
p2sh_ms_addr = self.nodes[i].addmultisigaddress(1, [self.pubkey[-1]], '', 'p2sh-segwit')['address']
145144
bip173_ms_addr = self.nodes[i].addmultisigaddress(1, [self.pubkey[-1]], '', 'bech32')['address']
146145
assert_equal(p2sh_ms_addr, script_to_p2sh_p2wsh(multiscript))
@@ -352,7 +351,7 @@ def run_test(self):
352351
# Money sent to P2SH of multisig of this should only be seen after importaddress with the BASE58 P2SH address.
353352

354353
multisig_without_privkey_address = self.nodes[0].addmultisigaddress(2, [pubkeys[3], pubkeys[4]])['address']
355-
script = CScript([OP_2, hex_str_to_bytes(pubkeys[3]), hex_str_to_bytes(pubkeys[4]), OP_2, OP_CHECKMULTISIG])
354+
script = CScript([OP_2, bytes.fromhex(pubkeys[3]), bytes.fromhex(pubkeys[4]), OP_2, OP_CHECKMULTISIG])
356355
solvable_after_importaddress.append(script_to_p2sh_script(script))
357356

358357
for i in compressed_spendable_address:
@@ -426,7 +425,7 @@ def run_test(self):
426425
op1 = CScript([OP_1])
427426
op0 = CScript([OP_0])
428427
# 2N7MGY19ti4KDMSzRfPAssP6Pxyuxoi6jLe is the P2SH(P2PKH) version of mjoE3sSrb8ByYEvgnC3Aox86u1CHnfJA4V
429-
unsolvable_address_key = hex_str_to_bytes("02341AEC7587A51CDE5279E0630A531AEA2615A9F80B17E8D9376327BAEAA59E3D")
428+
unsolvable_address_key = bytes.fromhex("02341AEC7587A51CDE5279E0630A531AEA2615A9F80B17E8D9376327BAEAA59E3D")
430429
unsolvablep2pkh = key_to_p2pkh_script(unsolvable_address_key)
431430
unsolvablep2wshp2pkh = script_to_p2wsh_script(unsolvablep2pkh)
432431
p2shop0 = script_to_p2sh_script(op0)
@@ -448,11 +447,11 @@ def run_test(self):
448447
for i in compressed_spendable_address + uncompressed_spendable_address + compressed_solvable_address + uncompressed_solvable_address:
449448
v = self.nodes[0].getaddressinfo(i)
450449
if (v['isscript']):
451-
bare = hex_str_to_bytes(v['hex'])
450+
bare = bytes.fromhex(v['hex'])
452451
importlist.append(bare.hex())
453452
importlist.append(script_to_p2wsh_script(bare).hex())
454453
else:
455-
pubkey = hex_str_to_bytes(v['pubkey'])
454+
pubkey = bytes.fromhex(v['pubkey'])
456455
p2pk = CScript([pubkey, OP_CHECKSIG])
457456
p2pkh = key_to_p2pkh_script(pubkey)
458457
importlist.append(p2pk.hex())
@@ -612,18 +611,18 @@ def mine_and_test_listunspent(self, script_list, ismine):
612611
return txid
613612

614613
def p2sh_address_to_script(self, v):
615-
bare = CScript(hex_str_to_bytes(v['hex']))
616-
p2sh = CScript(hex_str_to_bytes(v['scriptPubKey']))
614+
bare = CScript(bytes.fromhex(v['hex']))
615+
p2sh = CScript(bytes.fromhex(v['scriptPubKey']))
617616
p2wsh = script_to_p2wsh_script(bare)
618617
p2sh_p2wsh = script_to_p2sh_script(p2wsh)
619618
return([bare, p2sh, p2wsh, p2sh_p2wsh])
620619

621620
def p2pkh_address_to_script(self, v):
622-
pubkey = hex_str_to_bytes(v['pubkey'])
621+
pubkey = bytes.fromhex(v['pubkey'])
623622
p2wpkh = key_to_p2wpkh_script(pubkey)
624623
p2sh_p2wpkh = script_to_p2sh_script(p2wpkh)
625624
p2pk = CScript([pubkey, OP_CHECKSIG])
626-
p2pkh = CScript(hex_str_to_bytes(v['scriptPubKey']))
625+
p2pkh = CScript(bytes.fromhex(v['scriptPubKey']))
627626
p2sh_p2pk = script_to_p2sh_script(p2pk)
628627
p2sh_p2pkh = script_to_p2sh_script(p2pkh)
629628
p2wsh_p2pk = script_to_p2wsh_script(p2pk)

test/functional/interface_rest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
assert_equal,
2020
assert_greater_than,
2121
assert_greater_than_or_equal,
22-
hex_str_to_bytes,
2322
)
2423

2524
from test_framework.messages import BLOCK_HEADER_SIZE
@@ -147,7 +146,7 @@ def run_test(self):
147146

148147
bin_request = b'\x01\x02'
149148
for txid, n in [spending, spent]:
150-
bin_request += hex_str_to_bytes(txid)
149+
bin_request += bytes.fromhex(txid)
151150
bin_request += pack("i", n)
152151

153152
bin_response = self.test_rest_request("/getutxos", http_method='POST', req_type=ReqType.BIN, body=bin_request, ret_type=RetType.BYTES)

test/functional/p2p_invalid_messages.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from test_framework.test_framework import BitcoinTestFramework
2929
from test_framework.util import (
3030
assert_equal,
31-
hex_str_to_bytes,
3231
)
3332

3433
VALID_DATA_LIMIT = MAX_PROTOCOL_MESSAGE_LENGTH - 5 # Account for the 5-byte length prefix
@@ -187,7 +186,7 @@ def test_addrv2_no_addresses(self):
187186
[
188187
'received: addrv2 (1 bytes)',
189188
],
190-
hex_str_to_bytes('00'))
189+
bytes.fromhex('00'))
191190

192191
def test_addrv2_too_long_address(self):
193192
self.test_addrv2('too long address',
@@ -196,7 +195,7 @@ def test_addrv2_too_long_address(self):
196195
'ProcessMessages(addrv2, 525 bytes): Exception',
197196
'Address too long: 513 > 512',
198197
],
199-
hex_str_to_bytes(
198+
bytes.fromhex(
200199
'01' + # number of entries
201200
'61bc6649' + # time, Fri Jan 9 02:54:25 UTC 2009
202201
'00' + # service flags, COMPACTSIZE(NODE_NONE)
@@ -213,7 +212,7 @@ def test_addrv2_unrecognized_network(self):
213212
'IP 9.9.9.9 mapped',
214213
'Added 1 addresses',
215214
],
216-
hex_str_to_bytes(
215+
bytes.fromhex(
217216
'02' + # number of entries
218217
# this should be ignored without impeding acceptance of subsequent ones
219218
now_hex + # time

test/functional/p2p_segwit.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
from test_framework.util import (
8282
assert_equal,
8383
softfork_active,
84-
hex_str_to_bytes,
8584
assert_raises_rpc_error,
8685
)
8786

@@ -415,7 +414,7 @@ def test_block_relay(self):
415414
block = self.test_node.request_block(block_hash, 2)
416415
wit_block = self.test_node.request_block(block_hash, 2 | MSG_WITNESS_FLAG)
417416
assert_equal(block.serialize(), wit_block.serialize())
418-
assert_equal(block.serialize(), hex_str_to_bytes(rpc_block))
417+
assert_equal(block.serialize(), bytes.fromhex(rpc_block))
419418
else:
420419
# After activation, witness blocks and non-witness blocks should
421420
# be different. Verify rpc getblock() returns witness blocks, while
@@ -430,7 +429,7 @@ def test_block_relay(self):
430429
rpc_block = self.nodes[0].getblock(block.hash, False)
431430
non_wit_block = self.test_node.request_block(block.sha256, 2)
432431
wit_block = self.test_node.request_block(block.sha256, 2 | MSG_WITNESS_FLAG)
433-
assert_equal(wit_block.serialize(), hex_str_to_bytes(rpc_block))
432+
assert_equal(wit_block.serialize(), bytes.fromhex(rpc_block))
434433
assert_equal(wit_block.serialize(False), non_wit_block.serialize())
435434
assert_equal(wit_block.serialize(), block.serialize())
436435

test/functional/rpc_addresses_deprecation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from test_framework.test_framework import BitcoinTestFramework
1111
from test_framework.util import (
1212
assert_equal,
13-
hex_str_to_bytes
1413
)
1514

1615

@@ -36,7 +35,7 @@ def test_addresses_deprecation(self):
3635

3736
# This transaction is derived from test/util/data/txcreatemultisig1.json
3837
tx = tx_from_hex(signed)
39-
tx.vout[0].scriptPubKey = hex_str_to_bytes("522102a5613bd857b7048924264d1e70e08fb2a7e6527d32b7ab1bb993ac59964ff39721021ac43c7ff740014c3b33737ede99c967e4764553d1b2b83db77c83b8715fa72d2102df2089105c77f266fa11a9d33f05c735234075f2e8780824c6b709415f9fb48553ae")
38+
tx.vout[0].scriptPubKey = bytes.fromhex("522102a5613bd857b7048924264d1e70e08fb2a7e6527d32b7ab1bb993ac59964ff39721021ac43c7ff740014c3b33737ede99c967e4764553d1b2b83db77c83b8715fa72d2102df2089105c77f266fa11a9d33f05c735234075f2e8780824c6b709415f9fb48553ae")
4039
tx_signed = node.signrawtransactionwithwallet(tx.serialize().hex())['hex']
4140
txid = node.sendrawtransaction(hexstring=tx_signed, maxfeerate=0)
4241

test/functional/rpc_decodescript.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from test_framework.test_framework import BitcoinTestFramework
1212
from test_framework.util import (
1313
assert_equal,
14-
hex_str_to_bytes,
1514
)
1615

1716

@@ -86,7 +85,7 @@ def decodescript_script_pub_key(self):
8685
rpc_result = self.nodes[0].decodescript(multisig_script)
8786
assert_equal('2 ' + public_key + ' ' + public_key + ' ' + public_key + ' 3 OP_CHECKMULTISIG', rpc_result['asm'])
8887
# multisig in P2WSH
89-
multisig_script_hash = sha256(hex_str_to_bytes(multisig_script)).hex()
88+
multisig_script_hash = sha256(bytes.fromhex(multisig_script)).hex()
9089
assert_equal('0 ' + multisig_script_hash, rpc_result['segwit']['asm'])
9190

9291
# 4) P2SH scriptPubKey
@@ -124,7 +123,7 @@ def decodescript_script_pub_key(self):
124123
rpc_result = self.nodes[0].decodescript(cltv_script)
125124
assert_equal('OP_IF ' + public_key + ' OP_CHECKSIGVERIFY OP_ELSE 500000 OP_CHECKLOCKTIMEVERIFY OP_DROP OP_ENDIF ' + public_key + ' OP_CHECKSIG', rpc_result['asm'])
126125
# CLTV script in P2WSH
127-
cltv_script_hash = sha256(hex_str_to_bytes(cltv_script)).hex()
126+
cltv_script_hash = sha256(bytes.fromhex(cltv_script)).hex()
128127
assert_equal('0 ' + cltv_script_hash, rpc_result['segwit']['asm'])
129128

130129
# 7) P2PK scriptPubKey
@@ -209,23 +208,23 @@ def decoderawtransaction_asm_sighashtype(self):
209208
signature_2_sighash_decoded = der_signature + '[NONE|ANYONECANPAY]'
210209

211210
# 1) P2PK scriptSig
212-
txSave.vin[0].scriptSig = hex_str_to_bytes(push_signature)
211+
txSave.vin[0].scriptSig = bytes.fromhex(push_signature)
213212
rpc_result = self.nodes[0].decoderawtransaction(txSave.serialize().hex())
214213
assert_equal(signature_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
215214

216215
# make sure that the sighash decodes come out correctly for a more complex / lesser used case.
217-
txSave.vin[0].scriptSig = hex_str_to_bytes(push_signature_2)
216+
txSave.vin[0].scriptSig = bytes.fromhex(push_signature_2)
218217
rpc_result = self.nodes[0].decoderawtransaction(txSave.serialize().hex())
219218
assert_equal(signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
220219

221220
# 2) multisig scriptSig
222-
txSave.vin[0].scriptSig = hex_str_to_bytes('00' + push_signature + push_signature_2)
221+
txSave.vin[0].scriptSig = bytes.fromhex('00' + push_signature + push_signature_2)
223222
rpc_result = self.nodes[0].decoderawtransaction(txSave.serialize().hex())
224223
assert_equal('0 ' + signature_sighash_decoded + ' ' + signature_2_sighash_decoded, rpc_result['vin'][0]['scriptSig']['asm'])
225224

226225
# 3) test a scriptSig that contains more than push operations.
227226
# in fact, it contains an OP_RETURN with data specially crafted to cause improper decode if the code does not catch it.
228-
txSave.vin[0].scriptSig = hex_str_to_bytes('6a143011020701010101010101020601010101010101')
227+
txSave.vin[0].scriptSig = bytes.fromhex('6a143011020701010101010101020601010101010101')
229228
rpc_result = self.nodes[0].decoderawtransaction(txSave.serialize().hex())
230229
assert_equal('OP_RETURN 3011020701010101010101020601010101010101', rpc_result['vin'][0]['scriptSig']['asm'])
231230

test/functional/rpc_signrawtransaction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
assert_raises_rpc_error,
2121
find_vout_for_address,
2222
generate_to_height,
23-
hex_str_to_bytes,
2423
)
2524
from test_framework.messages import (
2625
CTxInWitness,
@@ -233,7 +232,7 @@ def verify_txn_with_witness_script(self, tx_type):
233232
embedded_pubkey = eckey.get_pubkey().get_bytes().hex()
234233
witness_script = {
235234
'P2PKH': key_to_p2pkh_script(embedded_pubkey).hex(),
236-
'P2PK': CScript([hex_str_to_bytes(embedded_pubkey), OP_CHECKSIG]).hex()
235+
'P2PK': CScript([bytes.fromhex(embedded_pubkey), OP_CHECKSIG]).hex()
237236
}.get(tx_type, "Invalid tx_type")
238237
redeem_script = script_to_p2wsh_script(witness_script).hex()
239238
addr = script_to_p2sh(redeem_script)

test/functional/test_framework/address.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from .script import hash256, hash160, sha256, CScript, OP_0
1414
from .segwit_addr import encode_segwit_address
15-
from .util import assert_equal, hex_str_to_bytes
15+
from .util import assert_equal
1616

1717
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
1818
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
@@ -33,7 +33,7 @@ def byte_to_base58(b, version):
3333
result = ''
3434
str = b.hex()
3535
str = chr(version).encode('latin-1').hex() + str
36-
checksum = hash256(hex_str_to_bytes(str)).hex()
36+
checksum = hash256(bytes.fromhex(str)).hex()
3737
str += checksum[:8]
3838
value = int('0x' + str, 0)
3939
while value > 0:
@@ -100,7 +100,7 @@ def key_to_p2sh_p2wpkh(key, main=False):
100100

101101
def program_to_witness(version, program, main=False):
102102
if (type(program) is str):
103-
program = hex_str_to_bytes(program)
103+
program = bytes.fromhex(program)
104104
assert 0 <= version <= 16
105105
assert 2 <= len(program) <= 40
106106
assert version > 0 or len(program) in [20, 32]
@@ -121,14 +121,14 @@ def script_to_p2sh_p2wsh(script, main=False):
121121

122122
def check_key(key):
123123
if (type(key) is str):
124-
key = hex_str_to_bytes(key) # Assuming this is hex string
124+
key = bytes.fromhex(key) # Assuming this is hex string
125125
if (type(key) is bytes and (len(key) == 33 or len(key) == 65)):
126126
return key
127127
assert False
128128

129129
def check_script(script):
130130
if (type(script) is str):
131-
script = hex_str_to_bytes(script) # Assuming this is hex string
131+
script = bytes.fromhex(script) # Assuming this is hex string
132132
if (type(script) is bytes or type(script) is CScript):
133133
return script
134134
assert False

test/functional/test_framework/blocktools.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
CTxInWitness,
2525
CTxOut,
2626
hash256,
27-
hex_str_to_bytes,
2827
ser_uint256,
2928
tx_from_hex,
3029
uint256_from_str,
@@ -214,7 +213,7 @@ def witness_script(use_p2wsh, pubkey):
214213
pkscript = key_to_p2wpkh_script(pubkey)
215214
else:
216215
# 1-of-1 multisig
217-
witness_script = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
216+
witness_script = CScript([OP_1, bytes.fromhex(pubkey), OP_1, OP_CHECKMULTISIG])
218217
pkscript = script_to_p2wsh_script(witness_script)
219218
return pkscript.hex()
220219

@@ -223,7 +222,7 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
223222
224223
Optionally wrap the segwit output using P2SH."""
225224
if use_p2wsh:
226-
program = CScript([OP_1, hex_str_to_bytes(pubkey), OP_1, OP_CHECKMULTISIG])
225+
program = CScript([OP_1, bytes.fromhex(pubkey), OP_1, OP_CHECKMULTISIG])
227226
addr = script_to_p2sh_p2wsh(program) if encode_p2sh else script_to_p2wsh(program)
228227
else:
229228
addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)
@@ -246,7 +245,7 @@ def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=Tru
246245
else:
247246
if (insert_redeem_script):
248247
tx = tx_from_hex(tx_to_witness)
249-
tx.vin[0].scriptSig += CScript([hex_str_to_bytes(insert_redeem_script)])
248+
tx.vin[0].scriptSig += CScript([bytes.fromhex(insert_redeem_script)])
250249
tx_to_witness = tx.serialize().hex()
251250

252251
return node.sendrawtransaction(tx_to_witness)

0 commit comments

Comments
 (0)