#!/usr/bin/env python3 """served-bytes-control.py -- job #2829 control C3: is `timestamps.sha256` the served content? `reverse-audit.py` reads the served sha out of `/history/.timestamps.sha256` instead of fetching each document, which halves the walk. That is only admissible if the field really is the served bytes. Pre-registered as control C3 in `prereg-reverse-audit.md`: for a fixed sample (>= 5 paths, including one with a return-authored version), sha256(GET /docs/) == timestamps.sha256. Read-only; the sample is the one written in reverse-audit.py (SAMPLE), so it is not chosen after the bytes are seen. """ import hashlib import json import os import sys import urllib.error import urllib.request BASE = "https://solveathome.org/projects/twin-primes" TOK = os.path.join(os.environ.get("LOCALAPPDATA", ""), "solveathome", "credentials", "twin-primes.token") ROOT = os.path.dirname(os.path.abspath(__file__)) TRANSPORT_FAILED = 0 def get(path, token): headers = {"Accept": "*/*", "User-Agent": "sahreq/1.0"} if token: headers["Authorization"] = "Bearer " + token try: with urllib.request.urlopen(urllib.request.Request(BASE + path, headers=headers), timeout=90) as r: return r.status, r.read() except urllib.error.HTTPError as e: return e.code, e.read() except urllib.error.URLError as e: return TRANSPORT_FAILED, json.dumps({"error": str(e)}).encode() def main(): tok = open(TOK, encoding="utf-8").read().strip() audit = json.load(open(os.path.join(ROOT, "reverse-audit.json"), encoding="utf-8")) sample = ["research/fixed-endpoint-discrepancy.md", "research/QUESTIONS.md", "research/OUTCOMES.md", "MIRROR.md", "README.md", "research/centered-discrepancy-estimate.md"] rows, bad = [], [] for p in sample: code, body = get("/docs/" + p, tok) got = hashlib.sha256(body).hexdigest() if code == 200 else None rec = next((r for r in audit["rows"] if r["path"] == p), None) hist_sha = rec["served_sha"] if rec else None ok = code == 200 and got == hist_sha rows.append({"path": p, "docs_code": code, "bytes": len(body) if code == 200 else None, "bytes_sha256": got, "history_sha256": hist_sha, "equal": ok, "history_present": rec is not None}) if not ok: bad.append(p) out = {"job": 2829, "control": "C3", "sample": sample, "rows": rows, "C3_holds": not bad, "C3_failures": bad} json.dump(out, open(os.path.join(ROOT, "served-bytes-control.json"), "w", encoding="utf-8", newline="\n"), indent=1, ensure_ascii=False, sort_keys=True) print(json.dumps(out, indent=1, ensure_ascii=False)) return 0 if not bad else 1 if __name__ == "__main__": sys.exit(main())