#!/usr/bin/env python3 """patch-sha-probe.py -- is the accepted text of the queue half retrievable BY ITS SHA? The user's question, made precise. Each return in the queue carries `patch` (the unified diff) and `patch_hash`. Two carriers are possible and they are not the same thing: in-band, credentialed `GET /return/` returns the `patch` field (measured: 26/26 readable) content-addressed `GET /files/` returns the same bytes with NO credential, which would put the queue half on the same footing as the store half (#1447: 10/10 accepted revision texts answer anonymously at /files/) Also probes `revision_sha` when a return supplied a whole revision, since that sha names the *document* the return accepted. Read-only; nothing is written except the report. """ 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__)) ENUM = ".solveathome/twin-primes/runs/bf5-8fb00a6176edcba2/scratch-records/r1373/fresh2744.json" PENDING = "pending integration" def get(path, token=None): 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=120) 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 0, json.dumps({"error": str(e)}).encode() def main(): enum = json.load(open(os.path.join(os.getcwd(), ENUM), encoding="utf-8")) rows = [r for r in enum["rows"] if (r.get("patch_status") or "").startswith(PENDING)] returns = sorted({r["return"] for r in rows}) tok = open(TOK, encoding="utf-8").read().strip() out = [] for rid in returns: code, body = get("/return/%d" % rid, tok) if code != 200: out.append({"return": rid, "in_band": False, "code": code}) continue rr = json.loads(body) patch = rr.get("patch") or "" ph = rr.get("patch_hash") pcode, pblob = get("/files/%s" % ph, None) if ph else (0, b"") psha = hashlib.sha256(pblob).hexdigest() if pcode == 200 else None rsha = rr.get("revision_sha") rec = { "return": rid, "type": rr.get("type"), "status": rr.get("status"), "handle": rr.get("handle"), "model": rr.get("model"), "in_band": bool(patch), "patch_chars": len(patch), "patch_hash": ph, "patch_hash_len": len(ph or ""), "patch_sha_is_sha256_of_patch": bool( ph and hashlib.sha256(patch.encode("utf-8")).hexdigest() == ph), "anon_files_code": pcode, "anon_files_sha": psha, "patch_retrievable_by_sha": bool(pcode == 200 and psha == ph), "revision_path": rr.get("revision_path"), "revision_sha": rsha, "patch_status": rr.get("patch_status"), } if rsha: rcode, rblob = get("/files/%s" % rsha, None) rec["revision_anon_code"] = rcode rec["revision_retrievable_by_sha"] = bool( rcode == 200 and hashlib.sha256(rblob).hexdigest() == rsha) out.append(rec) res = { "job": "2829-continuation", "route": 114, "tool": "patch-sha-probe.py/1.0", "n_returns": len(returns), "n_in_band": sum(1 for r in out if r.get("in_band")), "n_patch_hash_equals_sha256_of_patch": sum( 1 for r in out if r.get("patch_sha_is_sha256_of_patch")), "n_patch_retrievable_by_sha": sum(1 for r in out if r.get("patch_retrievable_by_sha")), "n_revision_retrievable_by_sha": sum(1 for r in out if r.get("revision_retrievable_by_sha")), "rows": out, } json.dump(res, open(os.path.join(ROOT, "patch-sha-probe.json"), "w", encoding="utf-8", newline="\n"), indent=1, ensure_ascii=False, sort_keys=True) print(json.dumps({k: v for k, v in res.items() if k != "rows"}, indent=1, ensure_ascii=False)) for r in out: print("%-4s %-9s in_band=%-5s patch_hash_match=%-5s anon=%s retr_by_sha=%-5s rev_retr=%s" % ( r.get("return"), r.get("type"), r.get("in_band"), r.get("patch_sha_is_sha256_of_patch"), r.get("anon_files_code"), r.get("patch_retrievable_by_sha"), r.get("revision_retrievable_by_sha"))) return 0 if __name__ == "__main__": sys.exit(main())