mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-04 07:07:45 +01:00
Merge bitcoin/bitcoin#22619: test: refactor: use consistent bytes <-> hex-string conversion in functional test framework
5a1bef60a0
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 ACK5a1bef60a0
🔢 Tree-SHA512: 8f28076cf0580a0d02a156f3e1e94c9badd3d41c3fbdfb2b87cd8a761dde2c94faa5f4c448d6747b1ccc9111c3ef1a1d7b42a11c806b241fa0410b7529e2445f
This commit is contained in:
commit
f4328ebef5
5 changed files with 10 additions and 14 deletions
|
@ -4,7 +4,6 @@
|
||||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
"""Test the REST API."""
|
"""Test the REST API."""
|
||||||
|
|
||||||
import binascii
|
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
@ -235,13 +234,13 @@ class RESTTest (BitcoinTestFramework):
|
||||||
response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
response_hex = self.test_rest_request("/block/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
||||||
assert_greater_than(int(response_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
|
assert_greater_than(int(response_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
|
||||||
response_hex_bytes = response_hex.read().strip(b'\n')
|
response_hex_bytes = response_hex.read().strip(b'\n')
|
||||||
assert_equal(binascii.hexlify(response_bytes), response_hex_bytes)
|
assert_equal(response_bytes.hex().encode(), response_hex_bytes)
|
||||||
|
|
||||||
# Compare with hex block header
|
# Compare with hex block header
|
||||||
response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
response_header_hex = self.test_rest_request("/headers/1/{}".format(bb_hash), req_type=ReqType.HEX, ret_type=RetType.OBJ)
|
||||||
assert_greater_than(int(response_header_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
|
assert_greater_than(int(response_header_hex.getheader('content-length')), BLOCK_HEADER_SIZE*2)
|
||||||
response_header_hex_bytes = response_header_hex.read(BLOCK_HEADER_SIZE*2)
|
response_header_hex_bytes = response_header_hex.read(BLOCK_HEADER_SIZE*2)
|
||||||
assert_equal(binascii.hexlify(response_bytes[:BLOCK_HEADER_SIZE]), response_header_hex_bytes)
|
assert_equal(response_bytes[:BLOCK_HEADER_SIZE].hex().encode(), response_header_hex_bytes)
|
||||||
|
|
||||||
# Check json format
|
# Check json format
|
||||||
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))
|
block_json_obj = self.test_rest_request("/block/{}".format(bb_hash))
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
# Distributed under the MIT software license, see the accompanying
|
# Distributed under the MIT software license, see the accompanying
|
||||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
"""Test multisig RPCs"""
|
"""Test multisig RPCs"""
|
||||||
import binascii
|
|
||||||
import decimal
|
import decimal
|
||||||
import itertools
|
import itertools
|
||||||
import json
|
import json
|
||||||
|
@ -66,9 +65,9 @@ class RpcCreateMultiSigTest(BitcoinTestFramework):
|
||||||
|
|
||||||
# decompress pk2
|
# decompress pk2
|
||||||
pk_obj = ECPubKey()
|
pk_obj = ECPubKey()
|
||||||
pk_obj.set(binascii.unhexlify(pk2))
|
pk_obj.set(bytes.fromhex(pk2))
|
||||||
pk_obj.compressed = False
|
pk_obj.compressed = False
|
||||||
pk2 = binascii.hexlify(pk_obj.get_bytes()).decode()
|
pk2 = pk_obj.get_bytes().hex()
|
||||||
|
|
||||||
node0.createwallet(wallet_name='wmulti0', disable_private_keys=True)
|
node0.createwallet(wallet_name='wmulti0', disable_private_keys=True)
|
||||||
wmulti0 = node0.get_wallet_rpc('wmulti0')
|
wmulti0 = node0.get_wallet_rpc('wmulti0')
|
||||||
|
|
|
@ -24,7 +24,6 @@ transactions.
|
||||||
`db_dump -da wallet.dat` is useful to see the data in a wallet.dat BDB file
|
`db_dump -da wallet.dat` is useful to see the data in a wallet.dat BDB file
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import binascii
|
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
# Important constants
|
# Important constants
|
||||||
|
@ -96,7 +95,7 @@ def dump_meta_page(page):
|
||||||
metadata['key_count'] = key_count
|
metadata['key_count'] = key_count
|
||||||
metadata['record_count'] = record_count
|
metadata['record_count'] = record_count
|
||||||
metadata['flags'] = flags
|
metadata['flags'] = flags
|
||||||
metadata['uid'] = binascii.hexlify(uid)
|
metadata['uid'] = uid.hex().encode()
|
||||||
|
|
||||||
assert magic == BTREE_MAGIC, 'bdb magic does not match bdb btree magic'
|
assert magic == BTREE_MAGIC, 'bdb magic does not match bdb btree magic'
|
||||||
assert pg_type == BTREE_META, 'Metadata page is not a btree metadata page'
|
assert pg_type == BTREE_META, 'Metadata page is not a btree metadata page'
|
||||||
|
@ -110,8 +109,9 @@ def dump_meta_page(page):
|
||||||
metadata['re_pad'] = re_pad
|
metadata['re_pad'] = re_pad
|
||||||
metadata['root'] = root
|
metadata['root'] = root
|
||||||
metadata['crypto_magic'] = crypto_magic
|
metadata['crypto_magic'] = crypto_magic
|
||||||
metadata['iv'] = binascii.hexlify(iv)
|
metadata['iv'] = iv.hex().encode()
|
||||||
metadata['chksum'] = binascii.hexlify(chksum)
|
metadata['chksum'] = chksum.hex().encode()
|
||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
|
|
||||||
# Given the dict from dump_leaf_page, get the key-value pairs and put them into a dict
|
# Given the dict from dump_leaf_page, get the key-value pairs and put them into a dict
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
"""Utilities for manipulating blocks and transactions."""
|
"""Utilities for manipulating blocks and transactions."""
|
||||||
|
|
||||||
from binascii import a2b_hex
|
|
||||||
import struct
|
import struct
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -74,7 +73,7 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
|
||||||
block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600)
|
block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600)
|
||||||
block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10)
|
block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10)
|
||||||
if tmpl and not tmpl.get('bits') is None:
|
if tmpl and not tmpl.get('bits') is None:
|
||||||
block.nBits = struct.unpack('>I', a2b_hex(tmpl['bits']))[0]
|
block.nBits = struct.unpack('>I', bytes.fromhex(tmpl['bits']))[0]
|
||||||
else:
|
else:
|
||||||
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
|
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
|
||||||
if coinbase is None:
|
if coinbase is None:
|
||||||
|
|
|
@ -12,7 +12,6 @@ import socket
|
||||||
import struct
|
import struct
|
||||||
import array
|
import array
|
||||||
import os
|
import os
|
||||||
from binascii import unhexlify
|
|
||||||
|
|
||||||
# STATE_ESTABLISHED = '01'
|
# STATE_ESTABLISHED = '01'
|
||||||
# STATE_SYN_SENT = '02'
|
# STATE_SYN_SENT = '02'
|
||||||
|
@ -44,7 +43,7 @@ def _remove_empty(array):
|
||||||
def _convert_ip_port(array):
|
def _convert_ip_port(array):
|
||||||
host,port = array.split(':')
|
host,port = array.split(':')
|
||||||
# convert host from mangled-per-four-bytes form as used by kernel
|
# convert host from mangled-per-four-bytes form as used by kernel
|
||||||
host = unhexlify(host)
|
host = bytes.fromhex(host)
|
||||||
host_out = ''
|
host_out = ''
|
||||||
for x in range(0, len(host) // 4):
|
for x in range(0, len(host) // 4):
|
||||||
(val,) = struct.unpack('=I', host[x*4:(x+1)*4])
|
(val,) = struct.unpack('=I', host[x*4:(x+1)*4])
|
||||||
|
|
Loading…
Add table
Reference in a new issue