Skip to content

Commit f4328eb

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22619: test: refactor: use consistent bytes <-> hex-string conversion in functional test framework
5a1bef6 test: refactor: remove binascii from test_framework (Zero-1729) Pull request description: This PR continues the work started in PR #22593, regarding using the `bytes` built-in module. In this PR specifically, instances of `binascii`'s methods `hexlify`, `unhexlify`, and `a2b_hex` have been replaced with the build-in `bytes` module's `hex` and `fromhex` methods where appropriate to make bytes <-> hex-string conversions consistent across the functional test files and test_framework. Additionally, certain changes made are based on the following assumption: ``` bytes.hex(data) == binascii.hexlify(data).decode() bytes.hex(data).encode() == binascii.hexlify(data) ``` Ran the functional tests to ensure behaviour is still consistent and changes didn't break existing tests. closes #22605 ACKs for top commit: theStack: Code-review ACK 5a1bef6 🔢 Tree-SHA512: 8f28076cf0580a0d02a156f3e1e94c9badd3d41c3fbdfb2b87cd8a761dde2c94faa5f4c448d6747b1ccc9111c3ef1a1d7b42a11c806b241fa0410b7529e2445f
2 parents c4b42aa + 5a1bef6 commit f4328eb

File tree

5 files changed

+10
-14
lines changed

5 files changed

+10
-14
lines changed

test/functional/interface_rest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the REST API."""
66

7-
import binascii
87
from decimal import Decimal
98
from enum import Enum
109
from io import BytesIO
@@ -235,13 +234,13 @@ def run_test(self):
235234
response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
236235
assert_greater_than(int(response_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
237236
response_hex_bytes = response_hex.read().strip(b'\n')
238-
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes)
237+
assert_equal(response_bytes.hex().encode(), response_hex_bytes)
239238

240239
# Compare with hex block header
241240
response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
242241
assert_greater_than(int(response_header_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
243242
response_header_hex_bytes = response_header_hex.read(BLOCK_HEADER_SIZE*2)
244-
assert_equal(binascii.hexlify(response_bytes[:BLOCK_HEADER_SIZE]), response_header_hex_bytes)
243+
assert_equal(response_bytes[:BLOCK_HEADER_SIZE].hex().encode(), response_header_hex_bytes)
245244

246245
# Check json format
247246
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))

test/functional/rpc_createmultisig.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test multisig RPCs"""
6-
import binascii
76
import decimal
87
import itertools
98
import json
@@ -66,9 +65,9 @@ def run_test(self):
6665

6766
# decompress pk2
6867
pk_obj = ECPubKey()
69-
pk_obj.set(binascii.unhexlify(pk2))
68+
pk_obj.set(bytes.fromhex(pk2))
7069
pk_obj.compressed = False
71-
pk2 = binascii.hexlify(pk_obj.get_bytes()).decode()
70+
pk2 = pk_obj.get_bytes().hex()
7271

7372
node0.createwallet(wallet_name='wmulti0', disable_private_keys=True)
7473
wmulti0 = node0.get_wallet_rpc('wmulti0')

test/functional/test_framework/bdb.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
`db_dump -da wallet.dat` is useful to see the data in a wallet.dat BDB file
2525
"""
2626

27-
import binascii
2827
import struct
2928

3029
# Important constants
@@ -96,7 +95,7 @@ def dump_meta_page(page):
9695
metadata['key_count'] = key_count
9796
metadata['record_count'] = record_count
9897
metadata['flags'] = flags
99-
metadata['uid'] = binascii.hexlify(uid)
98+
metadata['uid'] = uid.hex().encode()
10099

101100
assert magic == BTREE_MAGIC, 'bdb magic does not match bdb btree magic'
102101
assert pg_type == BTREE_META, 'Metadata page is not a btree metadata page'
@@ -110,8 +109,9 @@ def dump_meta_page(page):
110109
metadata['re_pad'] = re_pad
111110
metadata['root'] = root
112111
metadata['crypto_magic'] = crypto_magic
113-
metadata['iv'] = binascii.hexlify(iv)
114-
metadata['chksum'] = binascii.hexlify(chksum)
112+
metadata['iv'] = iv.hex().encode()
113+
metadata['chksum'] = chksum.hex().encode()
114+
115115
return metadata
116116

117117
# Given the dict from dump_leaf_page, get the key-value pairs and put them into a dict

test/functional/test_framework/blocktools.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Utilities for manipulating blocks and transactions."""
66

7-
from binascii import a2b_hex
87
import struct
98
import time
109
import unittest
@@ -74,7 +73,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
7473
block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600)
7574
block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10)
7675
if tmpl and not tmpl.get('bits') is None:
77-
block.nBits = struct.unpack('>I', a2b_hex(tmpl['bits']))[0]
76+
block.nBits = struct.unpack('>I', bytes.fromhex(tmpl['bits']))[0]
7877
else:
7978
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
8079
if coinbase is None:

test/functional/test_framework/netutil.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import struct
1313
import array
1414
import os
15-
from binascii import unhexlify
1615

1716
# STATE_ESTABLISHED = '01'
1817
# STATE_SYN_SENT = '02'
@@ -44,7 +43,7 @@ def _remove_empty(array):
4443
def _convert_ip_port(array):
4544
host,port = array.split(':')
4645
# convert host from mangled-per-four-bytes form as used by kernel
47-
host = unhexlify(host)
46+
host = bytes.fromhex(host)
4847
host_out = ''
4948
for x in range(0, len(host) // 4):
5049
(val,) = struct.unpack('=I', host[x*4:(x+1)*4])

0 commit comments

Comments
 (0)