"""Fetch every artifact the citation screen must resolve against, by its content address. The screen's whole point is that a cited value must be checked against the artifact that STATES it, so the artifacts are fetched from `/files/` (the record's own address) and each fetch is verified against that address before it is used. Anything that does not hash to the address it was filed under is reported, not silently accepted. """ import hashlib import json import os import sys TOOLS = os.path.join(os.environ["LOCALAPPDATA"], "solveathome", "tools", "v1") sys.path.insert(0, TOOLS) import sahtool # noqa: E402 (shared tool) import sahdated # noqa: E402 (shared tool) HERE = os.path.dirname(os.path.abspath(__file__)) RUN = os.path.dirname(HERE) RAW = os.path.join(HERE, "raw") SRC = os.path.join(RUN, "job2961", "raw") # records fetched by the previous assignment os.makedirs(RAW, exist_ok=True) BASE = "https://solveathome.org" def tok(): return sahtool.token_from(type("A", (), {"token_file": None, "state": RUN})()) def shas_of(return_id): """name -> sha256 from a return record's own file entries (last entry per name wins).""" rec = json.load(open(os.path.join(SRC, "return%d.json" % return_id), encoding="utf-8"))["body"] out = {} for f in rec.get("files") or []: if f.get("name") and f.get("sha256"): out[f["name"]] = f["sha256"] return out WANT = { 622: ["L-grid.json", "L-TABLE.md"], 637: ["T31-grid.json", "analyse31.json", "L-TABLE-31.md", "report.md"], 644: ["job1415-bank-diff.json"], 645: ["job1416-seam.json", "job1416-gates.json"], 656: ["job1418-level29.json", "job1418-level31.json", "job1418-gates.json"], } if __name__ == "__main__": index, bad = [], [] for rid, names in WANT.items(): shas = shas_of(rid) for name in names: sha = shas.get(name) if not sha: bad.append({"return": rid, "name": name, "why": "no sha in the record's own file list"}) continue code, text = sahtool.http("GET", BASE + "/files/" + sha, token=tok(), headers={"Accept": "*/*"}) raw = text.encode("utf-8") if isinstance(text, str) else text got = hashlib.sha256(raw).hexdigest() out = os.path.join(RAW, "%d-%s" % (rid, name)) with open(out, "wb") as fh: fh.write(raw) index.append({"return": rid, "name": name, "sha256": sha, "http": code, "bytes": len(raw), "sha_matches_address": got == sha}) if got != sha: bad.append({"return": rid, "name": name, "why": "served bytes do not hash to the " "address filed (got %s)" % got[:16], "server": got}) print("files/%-10s %-24s HTTP %s %8d B %s" % (sha[:10], name, code, len(raw), "MATCH" if got == sha else "MISMATCH")) # The corpus files the comparison target lives in (public docs, fetched by path). for name, path in (("uframe.md", "/projects/twin-primes/docs/research/U-FRAME.md"), ("killrun.js", "/projects/twin-primes/docs/research/killrun.js")): code, text = sahtool.http("GET", BASE + path, token=tok(), headers={"Accept": "*/*"}) raw = text.encode("utf-8") if isinstance(text, str) else text out = os.path.join(RAW, "corpus-" + name) with open(out, "wb") as fh: fh.write(raw) index.append({"corpus": name, "path": path, "http": code, "bytes": len(raw), "sha256": hashlib.sha256(raw).hexdigest()}) print("corpus %-12s %-42s HTTP %s %7d B" % (name, path, code, len(raw))) sahdated.write(os.path.join(HERE, "fetched-artifacts.json"), {"fetched_artifacts": index, "failures": bad, "why": ("every artifact the citation screen resolves against, fetched from its " "own content address and checked against it before use")}, source="artifacts fetched by content address for the job-2970 citation screen") print("\nfetched %d artifacts; failures: %d" % (len(index), len(bad))) for b in bad: print(" FAILURE:", b) raise SystemExit(2 if bad else 0)