Skip to content

Commit 254a63e

Browse files
committed
contrib: refactor: replace hex_switchEndian in linearize scripts
Switching the endianness of a hex string `str` can simply be achieved by `bytes.fromhex(str)[::-1].hex()`, i.e. we can use that and remove those helper methods.
1 parent 3f863cf commit 254a63e

File tree

2 files changed

+2
-12
lines changed

2 files changed

+2
-12
lines changed

contrib/linearize/linearize-data.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020

2121
settings = {}
2222

23-
def hex_switchEndian(s):
24-
""" Switches the endianness of a hex string (in pairs of hex chars) """
25-
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]
26-
return b''.join(pairList[::-1]).decode()
27-
2823
def calc_hash_str(blk_hdr):
2924
blk_hdr_hash = hashlib.sha256(hashlib.sha256(blk_hdr).digest()).digest()
3025
return blk_hdr_hash[::-1].hex()
@@ -43,7 +38,7 @@ def get_block_hashes(settings):
4338
for line in f:
4439
line = line.rstrip()
4540
if settings['rev_hash_bytes'] == 'true':
46-
line = hex_switchEndian(line)
41+
line = bytes.fromhex(line)[::-1].hex()
4742
blkindex.append(line)
4843

4944
print("Read " + str(len(blkindex)) + " hashes")

contrib/linearize/linearize-hashes.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717

1818
settings = {}
1919

20-
def hex_switchEndian(s):
21-
""" Switches the endianness of a hex string (in pairs of hex chars) """
22-
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]
23-
return b''.join(pairList[::-1]).decode()
24-
2520
class BitcoinRPC:
2621
def __init__(self, host, port, username, password):
2722
authpair = "%s:%s" % (username, password)
@@ -85,7 +80,7 @@ def get_block_hashes(settings, max_blocks_per_call=10000):
8580
sys.exit(1)
8681
assert(resp_obj['id'] == x) # assume replies are in-sequence
8782
if settings['rev_hash_bytes'] == 'true':
88-
resp_obj['result'] = hex_switchEndian(resp_obj['result'])
83+
resp_obj['result'] = bytes.fromhex(resp_obj['result'])[::-1].hex()
8984
print(resp_obj['result'])
9085

9186
height += num_blocks

0 commit comments

Comments
 (0)