Skip to content

Commit 3f863cf

Browse files
committed
contrib: refactor: simplify block header string routine in linearize-data.py
The string representation of a block header hash is simply the hexlified byte-reversed double SHA256 hash of its serialization.
1 parent 922c49a commit 3f863cf

File tree

1 file changed

+2
-37
lines changed

1 file changed

+2
-37
lines changed

contrib/linearize/linearize-data.py

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,9 @@ def hex_switchEndian(s):
2525
pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)]
2626
return b''.join(pairList[::-1]).decode()
2727

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-
6028
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
29+
blk_hdr_hash = hashlib.sha256(hashlib.sha256(blk_hdr).digest()).digest()
30+
return blk_hdr_hash[::-1].hex()
6631

6732
def get_blk_dt(blk_hdr):
6833
members = struct.unpack("<I", blk_hdr[68:68+4])

0 commit comments

Comments
 (0)