All checks were successful
wai-app and Create / wai-app · does this change touch it (push) Successful in 21s
joule-moq-remote host agent / joule-moq-remote · does this change touch it (push) Successful in 22s
wai-audit collector and player integration / does this change touch it (push) Successful in 21s
joule-moq-web browser client / joule-moq-web · does this change touch it (push) Successful in 12s
WAI operation catalogue / does this change touch it (push) Successful in 20s
wai-fleet service / does this change touch it (push) Successful in 26s
joule-moq-room room service / joule-moq-room · does this change touch it (push) Successful in 36s
wai-key-proxy / does this change touch it (push) Successful in 32s
wai-ledger service / does this change touch it (push) Successful in 36s
wai-sink-android native sink library / does this change touch it (push) Successful in 41s
wai-quantum for JavaScript / does this change touch it (push) Successful in 43s
wai-quantum studio and desktop / does this change touch it (push) Successful in 42s
wai-sink-swift native sink package / does this change touch it (push) Successful in 32s
WAI Studio / does this change touch it (push) Successful in 1m26s
joule-moq-remote host agent / joule-moq-remote, every target (linux-arm64) (push) Successful in 3m9s
repository checks / lockfiles, advisories, tests in CI, workflow hygiene, spec and section references, public text (push) Successful in 4m33s
wai-sink-android native sink library / the JNI layer and the platform-free half on the host JVM · linux-arm64 (push) Successful in 4m25s
joule-moq-room room service / joule-moq-room, every target (push) Successful in 7m26s
WAI breaking changes and the conformance runner / WAI breaking · does this change touch WAI (push) Successful in 9s
wai-quantum energy-landscape jobs / does this change touch it (push) Successful in 8s
wai-app and Create / wai-app · tests, clippy, the line language's verifier (linux) (push) Successful in 55s
wai-key-proxy / proxy · linux (push) Successful in 1m39s
WAI Studio / the catalogue's actions, the unit tests and the core · linux (push) Successful in 58s
wai-fleet service / service · linux (push) Successful in 2m23s
wai-audit collector and player integration / collector · linux (push) Successful in 4m16s
wai-ledger service / service · linux (push) Successful in 4m44s
wai-quantum for JavaScript / javascript · linux (push) Successful in 6m3s
wai-quantum studio and desktop / studio · browser (push) Successful in 6m52s
wai-quantum studio and desktop / desktop app · linux (push) Successful in 11m4s
wai-quantum energy-landscape jobs / the same bits · linux-arm64 (push) Successful in 12m34s
WAI operation catalogue / the catalogue and every door · linux (push) Successful in 13m8s
WAI Studio / Studio in WebKit · linux (push) Successful in 21m51s
WAI breaking changes and the conformance runner / WAI breaking · wire, record and API against main · linux (push) Successful in 3m17s
wai-quantum energy-landscape jobs / native against WebAssembly, over many seeds (push) Successful in 4m57s
WAI breaking changes and the conformance runner / WAI breaking and conform · their tests, and the independent verifiers through wai conform · linux (push) Successful in 5m41s
wai-quantum energy-landscape jobs / reader, kernels, answers and the stated result · linux, release (push) Successful in 8m57s
wai-quantum energy-landscape jobs / the module under wasm32-wasip2 (wasmtime) (push) Successful in 20m35s
wai-quantum studio and desktop / desktop app · windows (push) Successful in 9m58s
joule-moq-remote host agent / joule-moq-remote, every target (macos) (push) Successful in 1m17s
wai-audit collector and player integration / collector, player integration and the browser end to end · macos (push) Successful in 2m13s
wai-fleet service / service · macos (push) Successful in 50s
wai-ledger service / service · macos (push) Successful in 1m12s
wai-app and Create / wai-app · Create, browser end-to-end (macos) (push) Successful in 7m48s
wai-quantum studio and desktop / desktop app · macos (push) Successful in 4m7s
wai-sink-android native sink library / every ABI, the AAR, and the instrumented tests in a headless emulator · macos (push) Successful in 6m40s
joule-moq-web browser client / joule-moq-web · browser end-to-end (macos) (push) Successful in 14m55s
WAI Studio / Studio in Chromium and Firefox · macos (push) Successful in 10m22s
wai-sink-swift native sink package / libwai slices, desktop tests, simulator tests · macos (push) Successful in 15m8s
wai-quantum energy-landscape jobs / the same bits · macos (push) Successful in 7m7s
Holdings said who holds what; nothing fetched from them. extensions/peer-delivery.md is the exchange, over records WAI already had: a neighbour's holdings record says where to ask, the publisher's receipts say what the answer must be, and the asker's settlement acknowledgement says what was served. It registers no capability and no record, and is in the delivery profile. Joining is the sink's choice: asking a peer and serving one are each off unless the operator's policy and the viewer's setting both allow it, and the setting is off until the person using the sink turns it on. The exchange is eight frames over a link that keeps message boundaries (a data channel among them) and three HTTP requests for a neighbour reached that way. Peers are not trusted. An asker asks only for an object whose object receipt it holds, never names to a holder an object that holder has not listed, and takes nothing before the bytes verify. A holder that served other bytes is asked nothing more (peer_bytes_refused); a record that listed what its holder then said it lacks is set aside (holdings_overstated); a record that misstates a group's size was already refused whole. The asker signs one acknowledgement a holder a window for exactly what it verified, of class 2 with no grant, and the holder checks it against what it served. That settles what cache-authority §9 left open. §9 of the new text says what a peer learns of a viewer: an address, which of the objects it listed were asked for and when, and a session key where the asker acknowledges; and nothing else. wai-rs: feature peer_delivery (the gate, the frames, the asker, a holder's answer and its check of an acknowledgement), the corpus live-conformance/peer-delivery/ (92 cases) and its second implementation peer_delivery_verify.py, which rebuilds every acknowledgement byte for byte: 92 cases agree, 18 of 18 mutated copies and 22 of 22 weakened verifiers caught. wai-web: the peer bundle (src/peer.rs) and peer/: the client over data channels and fetch, objects kept in the Cache API, and two runs with faults planted on the holders' side. Under node, 33 checks (wai-web-wasm.yml runs it and has the Python verifier recompute the session from its signed acknowledgements). In a browser, 23 checks over seven pages: altered bytes refused and never acknowledged, an overstating record never asked, a peer that lists what it lacks set aside, and pages whose setting is off opening no connection. Co-Authored-By: Claude <[email protected]>
238 lines
12 KiB
JavaScript
238 lines
12 KiB
JavaScript
// The reference page of peer delivery (extensions/peer-delivery.md §7): an asker or a holder,
|
|
// by its query, over data channels and the HTTP holders its configuration names.
|
|
//
|
|
// ?role=asker&id=a1&peers=h1,h2 ask those holders, in that order, then the HTTP holders
|
|
// ?role=holder&id=h1&holds=0:0-3;1:1 hold those objects of the world (group:positions)
|
|
//
|
|
// The operator's policy comes from `config.json`, served beside the page; the viewer's setting
|
|
// is the two checkboxes, off until someone checks them. Introductions go through the mailbox
|
|
// the configuration names: one page posts its session description for another, which answers.
|
|
//
|
|
// A holder page takes three more parameters, which plant the faults run.mjs holds an asker to.
|
|
// None is anything a holder may do (§4.3): `alter=0:0` serves those objects with a byte
|
|
// changed, `lists=0:0-5` lists objects in its record beyond what it holds, and `count=1`
|
|
// states each group one object larger than it is.
|
|
|
|
import init, * as w from "./pkg/wai_peer.js";
|
|
import { PeerClient, PeerServer, cacheStore, channelLink, hex, overHttp, overLink } from "./peer.js";
|
|
|
|
const q = new URLSearchParams(location.search);
|
|
const id = q.get("id") || "page";
|
|
const role = q.get("role") || "asker";
|
|
const hook = (window.__wai_peer = { state: "loading", role, id, error: null });
|
|
const $ = (x) => document.getElementById(x);
|
|
const status = (text) => ($("status").textContent = text);
|
|
|
|
// "0:0-3;1:1" → [[0, 0], [0, 1], [0, 2], [0, 3], [1, 1]]
|
|
const objects = (text) =>
|
|
(text || "").split(";").filter(Boolean).flatMap((part) => {
|
|
const [g, list] = part.split(":");
|
|
return list.split(",").flatMap((r) => {
|
|
const [a, b = a] = r.split("-").map(Number);
|
|
return Array.from({ length: b - a + 1 }, (_, k) => [Number(g), a + k]);
|
|
});
|
|
});
|
|
const sha256 = async (bytes) => hex(new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)));
|
|
const seed = () => crypto.getRandomValues(new Uint8Array(32));
|
|
const json = async (url) => (await fetch(url, { cache: "no-store" })).json();
|
|
|
|
let config;
|
|
let world;
|
|
|
|
async function fromOrigin(group, index) {
|
|
const res = await fetch(`${config.origin}object/${group}/${index}?c=${encodeURIComponent(id)}`, { cache: "no-store" });
|
|
if (!res.ok) throw new Error(`the origin answered ${res.status}`);
|
|
return new Uint8Array(await res.arrayBuffer());
|
|
}
|
|
|
|
// ───────────────────────────── introductions ─────────────────────────────
|
|
|
|
const mailbox = new Map();
|
|
const arrivals = [];
|
|
let polling = false;
|
|
function poll() {
|
|
if (polling) return;
|
|
polling = true;
|
|
(async () => {
|
|
while (polling) {
|
|
try {
|
|
for (const m of await json(`${config.signal}recv/${id}`)) {
|
|
const wait = mailbox.get(m.from);
|
|
if (m.description.type === "answer" && wait) wait(m);
|
|
else arrivals.push(m);
|
|
}
|
|
} catch {
|
|
// the mailbox is gone: the run is over
|
|
}
|
|
await new Promise((ok) => setTimeout(ok, 100));
|
|
}
|
|
})();
|
|
}
|
|
const post = (to, description) => fetch(`${config.signal}send/${to}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ from: id, description }) });
|
|
|
|
function connection() {
|
|
// §7: with `relay_only`, relayed candidates alone, and no link where there is no relay.
|
|
const relays = config.relays || [];
|
|
if (config.policy.relay_only && relays.length === 0) throw new Error("relay_only and no relay");
|
|
return new RTCPeerConnection({ iceServers: relays, iceTransportPolicy: config.policy.relay_only ? "relay" : "all" });
|
|
}
|
|
const gathered = (pc) =>
|
|
pc.iceGatheringState === "complete" ? Promise.resolve() : new Promise((ok) => pc.addEventListener("icegatheringstatechange", () => pc.iceGatheringState === "complete" && ok()));
|
|
const within = (promise, ms, what) => Promise.race([promise, new Promise((_, no) => setTimeout(() => no(new Error(`${what}: no answer in ${ms} ms`)), ms))]);
|
|
|
|
/** The asker's side: a data channel to the holder page `peer`, as a connection. */
|
|
async function connect(peer) {
|
|
const pc = connection();
|
|
const channel = pc.createDataChannel("wai-peer", { ordered: true, protocol: "wai-peer-1" });
|
|
await pc.setLocalDescription(await pc.createOffer());
|
|
await gathered(pc);
|
|
const answer = new Promise((ok) => mailbox.set(peer, ok));
|
|
await post(peer, pc.localDescription);
|
|
try {
|
|
await pc.setRemoteDescription((await within(answer, 20000, `an answer from ${peer}`)).description);
|
|
if (channel.readyState !== "open") await within(new Promise((ok) => channel.addEventListener("open", ok, { once: true })), 20000, `the channel to ${peer}`);
|
|
} catch (e) {
|
|
// What the connection had to work with, for whoever reads the failure: how many candidates
|
|
// this end offered and of which kinds, never the addresses themselves.
|
|
const kinds = (pc.localDescription?.sdp.match(/^a=candidate:.*$/gm) || []).map((c) => `${c.split(" ")[7]}${/\.local /.test(c) ? " (a local name)" : ""}`);
|
|
pc.close();
|
|
throw new Error(`${e.message}; connection ${pc.iceConnectionState}; candidates offered: ${kinds.join(", ") || "none"}`);
|
|
}
|
|
const link = channelLink(channel);
|
|
const conn = overLink(w, link);
|
|
return { ...conn, close: () => (conn.close(), pc.close()) };
|
|
}
|
|
|
|
/** The holder's side: answer each offer, and serve the channel it opens. */
|
|
async function accept(server) {
|
|
for (;;) {
|
|
const m = arrivals.shift();
|
|
if (!m) {
|
|
await new Promise((ok) => setTimeout(ok, 50));
|
|
continue;
|
|
}
|
|
if (m.description.type !== "offer") continue;
|
|
const pc = connection();
|
|
pc.addEventListener("datachannel", (e) => {
|
|
if (e.channel.label !== "wai-peer" || e.channel.protocol !== "wai-peer-1") return e.channel.close();
|
|
hook.links++;
|
|
server.serve(channelLink(e.channel)).finally(() => pc.close());
|
|
});
|
|
await pc.setRemoteDescription(m.description);
|
|
await pc.setLocalDescription(await pc.createAnswer());
|
|
await gathered(pc);
|
|
await post(m.from, pc.localDescription);
|
|
}
|
|
}
|
|
|
|
// ───────────────────────────── the holder ─────────────────────────────
|
|
|
|
async function holder(setting) {
|
|
// Pages of one origin share its caches: this page's objects are in a cache of its own.
|
|
const inner = await cacheStore(`wai-peer-1-${id}`);
|
|
const altered = objects(q.get("alter")).map(([g, i]) => `${world.groups[g].link}/${i}`);
|
|
const store = {
|
|
...inner,
|
|
get: async (group, index) => {
|
|
const bytes = await inner.get(group, index);
|
|
if (bytes && altered.includes(`${group}/${index}`)) bytes[bytes.length - 1] ^= 1;
|
|
return bytes;
|
|
},
|
|
};
|
|
const server = new PeerServer(w, { seed: seed(), policy: config.policy, setting, store });
|
|
Object.assign(hook, { mayServe: server.mayServe, links: 0, stats: server.stats, key: server.mayServe ? server.key : null });
|
|
if (!server.mayServe) {
|
|
// §3.3: it signs no record, accepts no link and opens no connection.
|
|
hook.state = "off";
|
|
return status("Serving is off: this page tells no neighbour what it holds.");
|
|
}
|
|
const held = objects(q.get("holds"));
|
|
for (const [g, i] of held) {
|
|
const group = world.groups[g];
|
|
const bytes = await fromOrigin(group.link, i);
|
|
const refusal = w.peerVerifyObject(JSON.stringify(group.receipt), JSON.stringify(group.objects[i].receipt), bytes);
|
|
if (refusal) throw new Error(`the origin's object ${g}:${i} fails its receipt: ${refusal}`);
|
|
await inner.put(group.link, i, bytes);
|
|
}
|
|
const listed = [...held, ...objects(q.get("lists"))];
|
|
const entries = world.groups
|
|
.map((group, g) => ({ group: group.link, leaf_count: group.objects.length + (q.get("count") ? 1 : 0), held: listed.filter(([x]) => x === g).map(([, i]) => i) }))
|
|
.filter((e) => e.held.length);
|
|
const now = Date.now();
|
|
hook.record = hex(server.publish(now - 5000, now + 3600000, entries));
|
|
$("caption").textContent = `Holding ${held.length} objects as ${server.key.slice(0, 12)}…`;
|
|
poll();
|
|
accept(server);
|
|
hook.state = "serving";
|
|
status("Serving.");
|
|
}
|
|
|
|
// ───────────────────────────── the asker ─────────────────────────────
|
|
|
|
async function asker(setting) {
|
|
const started = Date.now();
|
|
const client = new PeerClient(w, { seed: seed(), policy: config.policy, setting, origin: fromOrigin });
|
|
Object.assign(hook, { mayAsk: client.mayAsk, key: client.key });
|
|
for (const g of world.groups) client.holdGroup(g.receipt, g.objects.map((o) => o.receipt));
|
|
if (client.mayAsk) poll();
|
|
const peers = {};
|
|
hook.failures = [];
|
|
const reported = (peer, open) => () =>
|
|
open().catch((e) => {
|
|
hook.failures.push(`${peer}: ${e?.message || e}`);
|
|
throw e;
|
|
});
|
|
for (const peer of (q.get("peers") || "").split(",").filter(Boolean)) peers[peer] = await client.addNeighbour(reported(peer, () => connect(peer)));
|
|
for (const base of config.http_holders || []) peers[base] = await client.addNeighbour(() => overHttp(w, base));
|
|
hook.peers = peers;
|
|
hook.before = client.records();
|
|
const name = (key) => Object.keys(peers).find((p) => peers[p] === key) || key;
|
|
$("caption").textContent = client.mayAsk ? `Asking ${Object.keys(peers).length} neighbours as ${client.key.slice(0, 12)}…` : "Fetching from peers is off: every object is the origin's.";
|
|
hook.objects = [];
|
|
const wanted = q.get("want") ? objects(q.get("want")) : world.groups.flatMap((g, k) => g.objects.map((_, i) => [k, i]));
|
|
for (const [g, i] of wanted) {
|
|
status(`Object ${g}:${i}…`);
|
|
const r = await client.get(world.groups[g].link, i);
|
|
const row = { group: g, index: i, from: r.from === "origin" ? "origin" : name(r.from), route: r.route, attempts: r.attempts.map((a) => ({ holder: name(a.holder), outcome: a.outcome })), verified: r.verified, length: r.bytes ? r.bytes.length : 0, sha256: r.bytes ? await sha256(r.bytes) : null };
|
|
hook.objects.push(row);
|
|
const tr = $("rows").insertRow();
|
|
tr.insertCell().textContent = `${g}:${i}`;
|
|
tr.insertCell().textContent = row.from;
|
|
tr.insertCell().textContent = row.attempts.map((a) => `${a.holder}: ${a.outcome}`).join("; ") || row.route;
|
|
const cell = tr.insertCell();
|
|
cell.textContent = row.verified ? "yes" : "no";
|
|
cell.className = row.verified ? "ok" : "no";
|
|
}
|
|
status("Signing for what the peers served…");
|
|
hook.acks = (await client.acknowledge(started * 1000, Date.now() * 1000 + 1)).map((a) => ({ ...a, upstream_name: name(a.upstream) }));
|
|
hook.after = client.records();
|
|
hook.asks = Object.fromEntries(Object.entries(client.asks()).map(([k, n]) => [name(k), n]));
|
|
polling = false;
|
|
hook.state = "done";
|
|
status(`Done: ${hook.objects.filter((o) => o.from !== "origin").length} of ${hook.objects.length} objects from peers, every one verified before use.`);
|
|
}
|
|
|
|
async function start() {
|
|
$("start").disabled = true;
|
|
$("ask").disabled = $("serve").disabled = true;
|
|
const setting = { ask: $("ask").checked, serve: $("serve").checked };
|
|
try {
|
|
await (role === "holder" ? holder(setting) : asker(setting));
|
|
} catch (e) {
|
|
hook.error = String(e?.message || e);
|
|
hook.state = "failed";
|
|
status(`Failed: ${hook.error}`);
|
|
}
|
|
}
|
|
|
|
try {
|
|
await init();
|
|
config = await json("config.json");
|
|
world = await json(`${config.origin}world.json`);
|
|
$("start").addEventListener("click", start);
|
|
hook.state = "ready";
|
|
} catch (e) {
|
|
hook.error = String(e?.message || e);
|
|
hook.state = "failed";
|
|
status(`Failed: ${hook.error}`);
|
|
}
|