0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-10 10:52:31 -05:00

Merge bitcoin/bitcoin#22633: refactor: Replace remaining binascii method calls

021daedfa1 refactor: replace remaining binascii method calls (Zero-1729)

Pull request description:

  This PR removes the remaining `binascii` method calls outside `test/functional` and `test_framework`, as pointed out here  https://github.com/bitcoin/bitcoin/pull/22619#pullrequestreview-722153458.

  Follow-up to #22593 and #22619
  Closes #22605

ACKs for top commit:
  josibake:
    re-ACK 021daedfa1
  theStack:
    re-ACK 021daedfa1

Tree-SHA512: 2ae9fee8917112c91a5406f219ca70f24cd8902b903db5a61fc2de85ad640d669a772f5c05970be0fcee6ef1cdd32fae2ca5d1ec6dc9798b43352c8160ddde6f
This commit is contained in:
MarcoFalke 2021-08-21 16:29:48 +02:00
commit f5a406f003
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
7 changed files with 14 additions and 19 deletions

View file

@ -17,7 +17,6 @@ import datetime
import time import time
import glob import glob
from collections import namedtuple from collections import namedtuple
from binascii import unhexlify
settings = {} settings = {}
@ -332,7 +331,7 @@ if __name__ == '__main__':
settings['max_out_sz'] = int(settings['max_out_sz']) settings['max_out_sz'] = int(settings['max_out_sz'])
settings['split_timestamp'] = int(settings['split_timestamp']) settings['split_timestamp'] = int(settings['split_timestamp'])
settings['file_timestamp'] = int(settings['file_timestamp']) settings['file_timestamp'] = int(settings['file_timestamp'])
settings['netmagic'] = unhexlify(settings['netmagic'].encode('utf-8')) settings['netmagic'] = bytes.fromhex(settings['netmagic'])
settings['out_of_order_cache_sz'] = int(settings['out_of_order_cache_sz']) settings['out_of_order_cache_sz'] = int(settings['out_of_order_cache_sz'])
settings['debug_output'] = settings['debug_output'].lower() settings['debug_output'] = settings['debug_output'].lower()

View file

@ -15,7 +15,6 @@ import sys
import time import time
import subprocess import subprocess
from binascii import unhexlify
from io import BytesIO from io import BytesIO
PATH_BASE_CONTRIB_SIGNET = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) PATH_BASE_CONTRIB_SIGNET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
@ -202,7 +201,7 @@ def finish_block(block, signet_solution, grind_cmd):
def generate_psbt(tmpl, reward_spk, *, blocktime=None): def generate_psbt(tmpl, reward_spk, *, blocktime=None):
signet_spk = tmpl["signet_challenge"] signet_spk = tmpl["signet_challenge"]
signet_spk_bin = unhexlify(signet_spk) signet_spk_bin = bytes.fromhex(signet_spk)
cbtx = create_coinbase(height=tmpl["height"], value=tmpl["coinbasevalue"], spk=reward_spk) cbtx = create_coinbase(height=tmpl["height"], value=tmpl["coinbasevalue"], spk=reward_spk)
cbtx.vin[0].nSequence = 2**32-2 cbtx.vin[0].nSequence = 2**32-2
@ -258,7 +257,7 @@ def get_reward_addr_spk(args, height):
return args.address, args.reward_spk return args.address, args.reward_spk
reward_addr = get_reward_address(args, height) reward_addr = get_reward_address(args, height)
reward_spk = unhexlify(json.loads(args.bcli("getaddressinfo", reward_addr))["scriptPubKey"]) reward_spk = bytes.fromhex(json.loads(args.bcli("getaddressinfo", reward_addr))["scriptPubKey"])
if args.address is not None: if args.address is not None:
# will always be the same, so cache # will always be the same, so cache
args.reward_spk = reward_spk args.reward_spk = reward_spk

View file

@ -23,7 +23,6 @@
https://github.com/bitcoin/bitcoin/blob/37a7fe9e440b83e2364d5498931253937abe9294/contrib/zmq/zmq_sub.py https://github.com/bitcoin/bitcoin/blob/37a7fe9e440b83e2364d5498931253937abe9294/contrib/zmq/zmq_sub.py
""" """
import binascii
import asyncio import asyncio
import zmq import zmq
import zmq.asyncio import zmq.asyncio
@ -58,18 +57,18 @@ class ZMQHandler():
sequence = str(struct.unpack('<I', seq)[-1]) sequence = str(struct.unpack('<I', seq)[-1])
if topic == b"hashblock": if topic == b"hashblock":
print('- HASH BLOCK ('+sequence+') -') print('- HASH BLOCK ('+sequence+') -')
print(binascii.hexlify(body)) print(body.hex())
elif topic == b"hashtx": elif topic == b"hashtx":
print('- HASH TX ('+sequence+') -') print('- HASH TX ('+sequence+') -')
print(binascii.hexlify(body)) print(body.hex())
elif topic == b"rawblock": elif topic == b"rawblock":
print('- RAW BLOCK HEADER ('+sequence+') -') print('- RAW BLOCK HEADER ('+sequence+') -')
print(binascii.hexlify(body[:80])) print(body[:80].hex())
elif topic == b"rawtx": elif topic == b"rawtx":
print('- RAW TX ('+sequence+') -') print('- RAW TX ('+sequence+') -')
print(binascii.hexlify(body)) print(body.hex())
elif topic == b"sequence": elif topic == b"sequence":
hash = binascii.hexlify(body[:32]) hash = body[:32].hex()
label = chr(body[32]) label = chr(body[32])
mempool_sequence = None if len(body) != 32+1+8 else struct.unpack("<Q", body[32+1:])[0] mempool_sequence = None if len(body) != 32+1+8 else struct.unpack("<Q", body[32+1:])[0]
print('- SEQUENCE ('+sequence+') -') print('- SEQUENCE ('+sequence+') -')

View file

@ -5,7 +5,6 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from base64 import urlsafe_b64encode from base64 import urlsafe_b64encode
from binascii import hexlify
from getpass import getpass from getpass import getpass
from os import urandom from os import urandom
@ -13,7 +12,7 @@ import hmac
def generate_salt(size): def generate_salt(size):
"""Create size byte hex salt""" """Create size byte hex salt"""
return hexlify(urandom(size)).decode() return urandom(size).hex()
def generate_password(): def generate_password():
"""Create 32 byte b64 password""" """Create 32 byte b64 password"""

View file

@ -102,11 +102,12 @@ BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
Python code to generate the below hashes: Python code to generate the below hashes:
def reversed_hex(x): def reversed_hex(x):
return binascii.hexlify(''.join(reversed(x))) return bytes(reversed(x)).hex()
def dsha256(x): def dsha256(x):
return hashlib.sha256(hashlib.sha256(x).digest()).digest() return hashlib.sha256(hashlib.sha256(x).digest()).digest()
reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96' reversed_hex(dsha256(b''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
*/ */
BOOST_AUTO_TEST_CASE(doubles) BOOST_AUTO_TEST_CASE(doubles)
{ {

View file

@ -19,7 +19,6 @@ Classes use __slots__ to ensure extraneous attributes aren't accidentally added
by tests, compromising their intended effect. by tests, compromising their intended effect.
""" """
from base64 import b32decode, b32encode from base64 import b32decode, b32encode
from codecs import encode
import copy import copy
import hashlib import hashlib
from io import BytesIO from io import BytesIO
@ -681,7 +680,7 @@ class CBlockHeader:
r += struct.pack("<I", self.nBits) r += struct.pack("<I", self.nBits)
r += struct.pack("<I", self.nNonce) r += struct.pack("<I", self.nNonce)
self.sha256 = uint256_from_str(hash256(r)) self.sha256 = uint256_from_str(hash256(r))
self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii') self.hash = hash256(r)[::-1].hex()
def rehash(self): def rehash(self):
self.sha256 = None self.sha256 = None

View file

@ -10,7 +10,6 @@ Runs automatically during `make check`.
Can also be run manually.""" Can also be run manually."""
import argparse import argparse
import binascii
import configparser import configparser
import difflib import difflib
import json import json
@ -167,7 +166,7 @@ def parse_output(a, fmt):
if fmt == 'json': # json: compare parsed data if fmt == 'json': # json: compare parsed data
return json.loads(a) return json.loads(a)
elif fmt == 'hex': # hex: parse and compare binary data elif fmt == 'hex': # hex: parse and compare binary data
return binascii.a2b_hex(a.strip()) return bytes.fromhex(a.strip())
else: else:
raise NotImplementedError("Don't know how to compare %s" % fmt) raise NotImplementedError("Don't know how to compare %s" % fmt)