Skip to content

Commit 171f6f2

Browse files
committed
Merge bitcoin/bitcoin#24374: contrib: refactor: simplify linearize scripts
254a63e contrib: refactor: replace `hex_switchEndian` in linearize scripts (Sebastian Falbesoner) 3f863cf contrib: refactor: simplify block header string routine in linearize-data.py (Sebastian Falbesoner) Pull request description: This PR simplifies the linearization scripts `linearize-data.py` and `linearize-hashes.py` by replacing overly complicated cruft (block header hash string calculation, hex string reverse) with means of the Python3 standard library. ACKs for top commit: laanwj: Code review ACK 254a63e Tree-SHA512: 4a0e20e63bd11f23a190480b22dbc2f2a3070e2a4f3a01b8797f99bb5fc830185e91e6712c8ae97562f9a24a98aa4f19d52f02a3f5fcbe4c578ee88a41382d1d
2 parents 50c806f + 254a63e commit 171f6f2

File tree

2 files changed

+4
-49
lines changed

2 files changed

+4
-49
lines changed

contrib/linearize/linearize-data.py

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,9 @@
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-
28-
def uint32(x):
29-
return x & 0xffffffff
30-
31-
def bytereverse(x):
32-
return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) |
33-
(((x) >> 8) & 0x0000ff00) | ((x) >> 24) ))
34-
35-
def bufreverse(in_buf):
36-
out_words = []
37-
for i in range(0, len(in_buf), 4):
38-
word = struct.unpack('@I', in_buf[i:i+4])[0]
39-
out_words.append(struct.pack('@I', bytereverse(word)))
40-
return b''.join(out_words)
41-
42-
def wordreverse(in_buf):
43-
out_words = []
44-
for i in range(0, len(in_buf), 4):
45-
out_words.append(in_buf[i:i+4])
46-
out_words.reverse()
47-
return b''.join(out_words)
48-
49-
def calc_hdr_hash(blk_hdr):
50-
hash1 = hashlib.sha256()
51-
hash1.update(blk_hdr)
52-
hash1_o = hash1.digest()
53-
54-
hash2 = hashlib.sha256()
55-
hash2.update(hash1_o)
56-
hash2_o = hash2.digest()
57-
58-
return hash2_o
59-
6023
def calc_hash_str(blk_hdr):
61-
hash = calc_hdr_hash(blk_hdr)
62-
hash = bufreverse(hash)
63-
hash = wordreverse(hash)
64-
hash_str = hash.hex()
65-
return hash_str
24+
blk_hdr_hash = hashlib.sha256(hashlib.sha256(blk_hdr).digest()).digest()
25+
return blk_hdr_hash[::-1].hex()
6626

6727
def get_blk_dt(blk_hdr):
6828
members = struct.unpack("<I", blk_hdr[68:68+4])
@@ -78,7 +38,7 @@ def get_block_hashes(settings):
7838
for line in f:
7939
line = line.rstrip()
8040
if settings['rev_hash_bytes'] == 'true':
81-
line = hex_switchEndian(line)
41+
line = bytes.fromhex(line)[::-1].hex()
8242
blkindex.append(line)
8343

8444
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)