"""Verify the accompanying blocks.csv and report.json using Python 3 (standard library). Run: python verify.py.txt blocks.csv report.json The header verifies its hash, proof of work and Merkle root. The explorer supplies the transaction count. The observation counts come from Litecoin.watch's dated archives. """ import csv import hashlib import json import sys from datetime import datetime, timezone from pathlib import Path def time_value(text): return datetime.fromisoformat(text.replace("Z","+00:00")).timestamp() def twice(data): return hashlib.sha256(hashlib.sha256(data).digest()).digest() def check(condition,message): if not condition: raise ValueError(message) def calculate(csv_path,report_path): raw=Path(csv_path).read_bytes() report=json.loads(Path(report_path).read_bytes()) check(hashlib.sha256(raw).hexdigest()==report["blocks_sha256"],"CSV fingerprint changed") bounds=[time_value(row["at_utc"]) for row in report["updates"]] check(len(bounds)==4 and bounds==sorted(bounds),"Unexpected update times") phases=[0,0,0] matches=heights=transaction_entries=0 last_id=None last_height=None inside=[] example_header=None example=report["sample_example"] for row in csv.DictReader(raw.decode("utf-8").splitlines()): height=int(row["height"]) header=bytes.fromhex(row["header_hex"]) ident=twice(header)[::-1].hex() check(len(header)==80 and ident==row["block_hash"],f"Header hash at {height}") if last_height is not None: check(height==last_height+1 and header[4:36][::-1].hex()==last_id,f"Chain link at {height}") last_id,last_height=ident,height compact=int.from_bytes(header[72:76],"little") exponent=compact>>24 mantissa=compact & 0x7fffff check(3<=exponent<=32 and mantissa>0 and not compact & 0x800000,"Invalid target for this header set") target=mantissa<<(8*(exponent-3)) work=int.from_bytes(hashlib.scrypt(header,salt=header,n=1024,r=1,p=1,dklen=32),"little") check(work<=target<2**236,f"Scrypt proof of work at {height}") t=int.from_bytes(header[68:72],"little") check(t==time_value(row["header_utc"]),f"Displayed timestamp at {height}") for i in range(3): if bounds[i]<=t