From ab90f3ff2e855a6abce13cbe92be972747c6efa1 Mon Sep 17 00:00:00 2001 From: 0xb10c Date: Fri, 29 Aug 2025 17:10:25 +0200 Subject: [PATCH] add: sanity check of block files Previously, the block files weren't checked at all. Importing them to a Bitcoin Core node only showed if they were accepted or not, but not that the filename and contents match. Could extend this to check the merkleroot and witness commitment (where possible). --- ci/sanity-check.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ci/sanity-check.py b/ci/sanity-check.py index 9797e54..4a76be1 100644 --- a/ci/sanity-check.py +++ b/ci/sanity-check.py @@ -4,9 +4,11 @@ # - file is ordered by block height in descending order # - header hashes match header (by hashing) # - header hashes are unique +# - binary block files have a correct bitcoin block header import csv import hashlib +import os EXPECTED_COLUMNS = 3 @@ -42,6 +44,17 @@ def dsha256(d): hash_count[header_hash] = hash_count.get(header_hash, 0) + 1 + blockfile = f"blocks/{height}-{header_hash}.bin" + if os.path.exists(blockfile): + print(f"checking block file: {blockfile}") + with open(blockfile, "rb") as block: + header_bytes = block.read(80) + calculated_header_hash = bytes(reversed(dsha256(header_bytes))).hex() + assert header_hash == calculated_header_hash + + header_hex = header_bytes.hex() + assert header == header_hex + hash_appeared_multiple_times = False for e in hash_count.items(): header_hash, count = e