diff --git a/js/net/src/lite/answer.test.ts b/js/net/src/lite/answer.test.ts new file mode 100644 index 0000000000..2b151c8543 --- /dev/null +++ b/js/net/src/lite/answer.test.ts @@ -0,0 +1,399 @@ +/** + * The lite-07 answer checked over random grant changes, claims, and edits, interleaved with its + * stream's sends, mirroring the Rust `lite::publisher::routed::tests`. + * + * Two promises, each judged as the peer reads the stream: + * + * - When a prefix's SYNCED goes out, every claim the grant shows under it has had its Start. The + * grant and the claims are those the answer last read; a fresh answer under them tells what + * the grant shows. + * - A claim that leaves while the origin still advertises it ends as lost, never as withdrawn. + * + * `MOQ_ANSWER_SEED` and `MOQ_ANSWER_CASES` pick the cases. + */ + +import { afterAll, beforeAll, expect, spyOn, test } from "bun:test"; +import { type Getter, Once, Signal } from "@moq/signals"; +import fc from "fast-check"; +import type { Grant } from "../auth.ts"; +import { WireRoute } from "../hop.ts"; +import * as Instance from "../instance.ts"; +import * as Path from "../path.ts"; +import type { Advertised, Advertisements } from "../wire.ts"; +import type { AnnounceBroadcast } from "./announce.ts"; +import type { AnnounceStream } from "./announce_stream.ts"; +import { Answer } from "./answer.ts"; +import { Version } from "./version.ts"; + +/** Where claims are advertised: some above what grants and prefixes name, and one hidden. */ +const PATHS = ["a", "a/x", "a/q/x", "a/.h", "b", "b/c"].map((path) => Path.from(path)); +/** The scopes claims are advertised with, which bound what each may serve. */ +const SCOPES: (string[] | undefined)[] = [undefined, ["a/*/x"], ["*/c/**"]]; +/** The grants: widen, narrow, move, and revoke between them. */ +const GRANTS: (string[] | undefined)[] = [undefined, ["**"], ["a/**"], ["b/**"], ["a/*"], ["a/x/**"], []]; +/** The prefixes the peer asks for. */ +const PREFIXES = ["", "a", "a/x", "a/q", "b"].map((path) => Path.from(path)); +/** The handle's own scope. */ +const HANDLES: (string[] | undefined)[] = [undefined, ["a/**", "b/**"], ["*/q/**", "b/**"]]; + +function patterns(members: string[]): Path.Patterns { + return new Path.Patterns(members.map((member) => Path.Pattern.parse(member))); +} + +function grant(index: number): Grant | undefined { + const members = GRANTS[index]; + return members && { publish: patterns(members), subscribe: patterns([]) }; +} + +type Op = + | { kind: "grant"; index: number } + | { kind: "advertise"; path: number; scope: number } + | { kind: "withdraw"; path: number } + | { kind: "reprice"; path: number; cost: number } + | { kind: "edit"; remove: number; add: [number, boolean][] } + /** The oldest send in flight completes. */ + | { kind: "send" } + /** The answer runs as far as it can without a send completing. */ + | { kind: "run" }; + +interface Case { + handle: number; + initial: number; + claims: [number, number][]; + ops: Op[]; +} + +const op: fc.Arbitrary = fc.oneof( + { weight: 4, arbitrary: fc.nat(GRANTS.length - 1).map((index) => ({ kind: "grant", index }) as const) }, + { + weight: 2, + arbitrary: fc + .tuple(fc.nat(PATHS.length - 1), fc.nat(SCOPES.length - 1)) + .map(([path, scope]) => ({ kind: "advertise", path, scope }) as const), + }, + { weight: 1, arbitrary: fc.nat(PATHS.length - 1).map((path) => ({ kind: "withdraw", path }) as const) }, + { + weight: 3, + arbitrary: fc + .tuple(fc.nat(PATHS.length - 1), fc.integer({ min: 1, max: 3 })) + .map(([path, cost]) => ({ kind: "reprice", path, cost }) as const), + }, + { + weight: 3, + arbitrary: fc + .tuple(fc.nat(255), fc.array(fc.tuple(fc.nat(PREFIXES.length - 1), fc.boolean()), { maxLength: 2 })) + .map(([remove, add]) => ({ kind: "edit", remove, add }) as const), + }, + { weight: 5, arbitrary: fc.constant({ kind: "send" } as const) }, + { weight: 3, arbitrary: fc.constant({ kind: "run" } as const) }, +); + +const cases: fc.Arbitrary = fc.record({ + handle: fc.nat(HANDLES.length - 1), + initial: fc.nat(GRANTS.length - 1), + claims: fc.array(fc.tuple(fc.nat(PATHS.length - 1), fc.nat(SCOPES.length - 1)), { maxLength: 5 }), + ops: fc.array(op, { minLength: 1, maxLength: 32 }), +}); + +/** Let the answer's loop run until it waits on something outside it. */ +async function settle() { + for (let i = 0; i < 64; i++) await Promise.resolve(); +} + +/** A getter that remembers the value its reader last read. */ +class Watched { + signal: Signal; + read: T; + getter: Getter; + + constructor(value: T) { + this.signal = new Signal(value); + this.read = value; + const signal = this.signal; + this.getter = { + peek: () => { + this.read = signal.peek(); + return this.read; + }, + changed: signal.changed.bind(signal), + subscribe: signal.subscribe.bind(signal), + }; + } +} + +/** The claims the peer holds, by Announce ID: each its path and instance. */ +class Peer { + live = new Map(); + #next = 0n; + + start(path: Path.Valid, instance: Instance.Valid) { + this.live.set(this.#next++, [path, instance]); + } + + end(id: bigint): [Path.Valid, Instance.Valid] { + const held = this.live.get(id); + if (!held) throw new Error(`ends ${id}, which is not live`); + this.live.delete(id); + return held; + } + + /** Both ends drop every claim within a prefix the edit removed. */ + dropWithin(removed: Path.Valid[]) { + for (const [id, [path]] of [...this.live]) { + if (removed.some((prefix) => Path.hasPrefix(prefix, path))) this.live.delete(id); + } + } + + holds(): Set { + return new Set([...this.live.values()].map(([path, instance]) => `${path} ${instance}`)); + } +} + +/** A stream whose sends complete only when the test says, recording each as it is made. */ +function stream(onSend: (msgs: AnnounceBroadcast[]) => void, gated: boolean) { + const pending: (() => void)[] = []; + const fake = { + send: (...msgs: AnnounceBroadcast[]) => { + onSend(msgs); + if (!gated) return Promise.resolve(); + return new Promise((resolve) => pending.push(resolve)); + }, + } as unknown as AnnounceStream; + return { stream: fake, pending }; +} + +function answer( + advertised: Getter, + grant: Getter, + allowed: Path.Patterns | undefined, + stream: AnnounceStream, +) { + return new Answer({ + version: Version.DRAFT_07, + advertised, + origin: { covers: () => true, allowed }, + grant, + ready: Promise.resolve(), + closing: new Once(), + stream, + }); +} + +/** What a fresh answer under `grant` tells for `prefix` from `table`, once it settles. */ +async function shown( + table: Advertisements, + grant: Grant | undefined, + allowed: Path.Patterns | undefined, + prefix: Path.Valid, + hidden: boolean, +): Promise> { + const peer = new Peer(); + let synced = false; + const { stream: fake } = stream((msgs) => { + for (const msg of msgs) { + if (msg.status === "start") peer.start(msg.suffix, msg.instance); + else if (msg.status === "endedId") peer.end(msg.id); + else if (msg.status === "synced") synced = true; + } + }, false); + const granted = new Signal(grant); + const fresh = answer(new Signal(table), granted, allowed, fake); + void fresh.request(false, [], [{ prefix, hidden }]); + await settle(); + expect(synced).toBe(true); + // What it settles to, judged again from scratch, so its own SYNCED proves nothing. + granted.set(grant, true); + await settle(); + fresh.close(); + return peer.holds(); +} + +async function run(c: Case) { + const members = HANDLES[c.handle]; + const allowed = members && patterns(members); + const table = new Watched(new Map()); + const granted = new Watched(grant(c.initial)); + const claims = new Map(); + let nextInstance = 0; + const peer = new Peer(); + const prefixes = new Map(); + let nextPrefix = 0n; + let edit: { remove: bigint[]; add: [bigint, Path.Valid, boolean][] } | undefined; + // Each send's SYNCED checks, run once the send is recorded, outside the answer's call. + const checks: Promise[] = []; + + const publish = () => { + const out = new Map(); + for (const [index, snap] of claims) out.set(PATHS[index], [snap]); + table.signal.set(out); + }; + const advertise = (path: number, scope: number) => { + if (claims.has(path)) return; + const members = SCOPES[scope]; + const instance = Instance.parse(`i${++nextInstance}`); + claims.set(path, { + identity: {}, + route: WireRoute.normalize({ instance, epoch: 0n }), + claim: members && patterns(members), + }); + publish(); + }; + + const { stream: fake, pending } = stream((msgs) => { + const advertised = new Set([...claims.values()].map((snap) => snap.route.instance)); + for (const msg of msgs) { + switch (msg.status) { + case "start": + peer.start(msg.suffix, msg.instance); + break; + case "change": + break; + case "endedId": { + const [path, instance] = peer.end(msg.id); + if (!msg.lost && advertised.has(instance)) + throw new Error(`${path} (${instance}) ended as withdrawn while still advertised`); + break; + } + case "ok": { + if (!edit) throw new Error("an ANNOUNCE_OK answers no edit"); + const removed = edit.remove.flatMap((id) => { + const asked = prefixes.get(id); + prefixes.delete(id); + return asked ? [asked.path] : []; + }); + peer.dropWithin(removed); + for (const [id, path, hidden] of edit.add) prefixes.set(id, { path, hidden }); + edit = undefined; + break; + } + case "synced": { + const asked = prefixes.get(msg.prefix); + if (!asked) throw new Error(`SYNCED for ${msg.prefix}, which is not in effect`); + const read = table.read ?? new Map(); + const grant = granted.read; + const held = peer.holds(); + checks.push( + shown(read, grant, allowed, asked.path, asked.hidden).then((owed) => { + for (const claim of owed) { + if (!held.has(claim)) + throw new Error(`SYNCED for "${asked.path}" went out before ${claim}`); + } + }), + ); + break; + } + default: + throw new Error(`a claims-only peer is sent ${msg.status}`); + } + } + }, true); + + for (const [path, scope] of c.claims) advertise(path, scope); + const subject = answer(table.getter, granted.getter, allowed, fake); + let running: Promise | undefined; + try { + for (const step of c.ops) { + switch (step.kind) { + case "grant": + granted.signal.set(grant(step.index)); + break; + case "advertise": + advertise(step.path, step.scope); + break; + case "withdraw": + if (claims.delete(step.path)) publish(); + break; + case "reprice": { + const snap = claims.get(step.path); + if (!snap) break; + claims.set(step.path, { ...snap, route: { ...snap.route, cost: BigInt(step.cost) } }); + publish(); + break; + } + case "edit": { + if (edit) break; + const remove = [...prefixes.keys()].filter((_, bit) => step.remove & (1 << bit)); + // Only what a peer may ask for: no prefix twice, or at or below another in the set. + const set = [...prefixes].filter(([id]) => !remove.includes(id)).map(([, asked]) => asked.path); + const add: { prefix: Path.Valid; hidden: boolean }[] = []; + for (const [index, hidden] of step.add) { + const prefix = PREFIXES[index]; + if (set.some((other) => Path.hasPrefix(other, prefix) || Path.hasPrefix(prefix, other))) + continue; + set.push(prefix); + add.push({ prefix, hidden }); + } + running = subject.request(false, remove, add); + edit = { remove, add: add.map(({ prefix, hidden }) => [nextPrefix++, prefix, hidden]) }; + break; + } + case "send": + pending.shift()?.(); + break; + case "run": + break; + } + await settle(); + } + // Then everything queued goes out. + while (pending.length > 0) { + pending.shift()?.(); + await settle(); + } + await Promise.all(checks); + } finally { + subject.close(); + for (const release of pending) release(); + await running; + } +} + +function setting(name: string, fallback: number, min: number): number { + const raw = process.env[name]; + if (raw !== undefined && !/^\d+$/.test(raw)) throw new Error(`${name} must be an integer`); + const value = Number(raw ?? fallback); + if (!Number.isInteger(value) || value < min || value > 0x7fffffff) { + throw new Error(`${name} must be an integer between ${min} and 2147483647`); + } + return value; +} + +// The answer logs every claim it tells. +let quiet: ReturnType | undefined; +beforeAll(() => { + quiet = spyOn(console, "debug").mockImplementation(() => {}); +}); +afterAll(() => quiet?.mockRestore()); + +test( + "synced follows what the grant shows", + async () => { + const seed = setting("MOQ_ANSWER_SEED", 0x616e73, 0); + const numRuns = setting("MOQ_ANSWER_CASES", 1024, 1); + await fc.assert(fc.asyncProperty(cases, run), { seed, numRuns }); + }, + { timeout: 3_600_000 }, +); + +// A claim is shown only where its own scope and the handle's share a path it covers, granted or +// not: one at `a` serving `a/q/x` and `a/r/y`, through a handle admitting `a/q/y` and `a/r/y`, +// serves nothing under `a/q`. +test("a claim is shown only where its scope and the handle's share a path", async () => { + const table: Advertisements = new Map([ + [ + Path.from("a"), + [ + { + identity: {}, + route: WireRoute.normalize({ instance: Instance.parse("i1"), epoch: 0n }), + claim: patterns(["a/q/x", "a/r/y"]), + }, + ], + ], + ]); + const allowed = patterns(["a/q/y", "a/r/y"]); + for (const grant of [undefined, { publish: patterns(["**"]), subscribe: patterns([]) }]) { + expect(await shown(table, grant, allowed, Path.from("a/q"), false)).toEqual(new Set()); + expect(await shown(table, grant, allowed, Path.from("a/r"), false)).toEqual(new Set(["a/r i1"])); + } +}); diff --git a/js/net/src/lite/answer.ts b/js/net/src/lite/answer.ts index bc8e9b9191..e8dfdedfa6 100644 --- a/js/net/src/lite/answer.ts +++ b/js/net/src/lite/answer.ts @@ -227,18 +227,18 @@ export class Answer { // The claims the peer is shown, by their path from the session's root: the table under each // prefix, less the hidden ones it did not ask for and whatever our grant does not let us publish. const latest = new Map(); - // A claim is shown where the grant lets us publish some path it covers that the claim's own - // scope and the handle's both serve, as one at `a` serves `a/*` though `a` itself is not - // granted. An intersection too large to spell covers nothing, never more. + // A claim is shown where some path it covers is served by the claim's own scope and the + // handle's, and our grant, if any, lets us publish it, as one at `a` serves `a/*` though `a` + // itself is not granted. An intersection too large to spell covers nothing, never more. const shows = (snap: Advertised, path: Path.Valid) => { - if (!grant) return true; let beneath = new Path.Patterns([Path.Pattern.subtree(path)]); try { for (const scope of [snap.claim, origin?.allowed]) if (scope) beneath = scope.intersect(beneath); } catch { return false; } - return [...beneath].some((pattern) => grant.publish.overlaps(pattern)); + const paths = [...beneath]; + return grant ? paths.some((pattern) => grant.publish.overlaps(pattern)) : paths.length > 0; }; // Whether the advertisement a claim the peer holds came from is still in the table, as when // its presentation moved: the claim is lost to the peer rather than ended, since its publisher diff --git a/quest/m2/lite07-claim-reach-routes.md b/quest/m2/lite07-claim-reach-routes.md new file mode 100644 index 0000000000..17c91f18b9 --- /dev/null +++ b/quest/m2/lite07-claim-reach-routes.md @@ -0,0 +1,66 @@ +# [L] A lite-07 claim is shown no broader than the grants it crossed + +## Goal + +Every lite-07 node files and presents a claim no broader than its route can serve under the +grants that route crossed, at every hop. So a peer is never shown a claim at a path no route of +it can serve, through any number of relays. Nothing about it is local-only: the path a claim is +presented at, with its Above, carries its reach on the wire. + +Today the Rust answer (`Answer::shows`, `rs/moq-net/src/lite/publisher/routed.rs`) judges the +handle's scope and the permit but never the route's reach. A producer granted `a/b` that +announces at `a` is shown to a peer asking for `a/q`, though a request there fails Unroutable. +The JS answer judges reach in-process, but no node can judge it through a relay. A relay that +takes the claim over a session granted everything files it at `a`, and shows it at `a/q` to its +own peers. The request is refused Unroutable upstream while the claim stays shown, which strands +the reader as the splice-stall did. + +## Plan + +Decided 2026-11-20 (maintainer, on [moq-dev/moq#9229](https://github.com/moq-dev/moq/pull/9229)): +✅ the lite-07 cut waits on this quest, and #9229 lands without it (rejected: fixing it after the +cut; holding #9229 until it is fixed). ✅ Grants become prefixes +([prefix grants](/quest/m2/prefix-grants.md)), so a route serves whole subtrees and a claim's +reach is where it is filed and presented (rejected: carrying the reach as patterns on +ANNOUNCE_START and ANNOUNCE_UPDATE, a wire field and its machinery that only wildcard grants +needed; judging reach from the routes a node knows, which a relay cannot do, so in-process and +relayed answers differ; never judging reach, which shows claims a request then fails Unroutable +on). + +- A route serves the claim's subtree within its producer's grant prefixes. So: + - The origin files a producer's claim at each of its grant's prefixes beneath the claim's own + prefix, with Above, and as now when a grant prefix lies at or above it + (`rs/moq-net/src/model/origin.rs`, `RouteEntry`, `sync_route`). + - A session files a neighbour's claim where the session's grant meets the path the claim + entered at, the same way (`lite/subscriber/routed.rs`, `admits` and `serve`). + - An answer presents a claim no higher than where it is filed, as the draft's region rule + already presents a claim above a region at that region. The draft says so in Claims, so a + relay passes on what it was shown. + - `Answer::shows` then judges only the handle's scope and the permit, and the JS answer's reach + check becomes the same rule. Delete the reach code the filing replaces, in Rust and JS. +- One claim filed at several grant prefixes is one claim: one Announce ID per presented path, + one Instance, and the specificity of its own prefix (`RouteEntry::specificity`). A grant + change re-files it under #9229's SYNCED hold (`Answer::sync`). +- Exclusions (`Producer::exclude`): the relay's one caller keeps publishers out of its stats + prefix, which the relay's own more specific claim owns, so that claim wins its paths. Test it. + Recommended: refuse announcing a claim over an exclusion no more specific claim can own, + rather than show a hole. +- The tests: + - A producer granted `a/b` that announces at `a` is shown at `a/b` with Above 1. A peer + asking for `a/q` is shown nothing, and a peer asking for `a/b/c` is shown it. + - The same claim relayed over a session granted everything is shown the same way one and two + hops on (the multi-hop case). + - A neighbour granted `customer` gives a claim at the root, and it is presented onward at + `customer` with Above 1. + - A mount names the claim beneath its point. + - The property test in `lite/publisher/routed/tests.rs` (`synced_follows_what_the_grant_shows`) + announces through granted producers, so its oracle covers reach. +- Bench filing over grant prefixes per claim and claims per prefix, both swept, and the answer's + judgment over sessions and claim churn. + +Public API: none. Wire: the draft's Claims rule for where a claim is presented. + +## Required + +- [Prefix grants](/quest/m2/prefix-grants.md) - a grant is a set of prefixes, so a route serves + whole subtrees diff --git a/quest/m2/lite07-claim-reach.md b/quest/m2/lite07-claim-reach.md deleted file mode 100644 index 472156600e..0000000000 --- a/quest/m2/lite07-claim-reach.md +++ /dev/null @@ -1,23 +0,0 @@ -# [S] A Rust lite-07 answer shows a claim only where its route can serve - -## Goal - -A Rust lite-07 publisher shows a peer a claim only where the claim's own route can -serve some path the peer may ask for, as the JS answer already does. Today a route -announced through a producer scoped to `a/*/x` at the prefix `a` is shown at `a/q/y` -(Above 2) to a peer that asks for `a/q/y`, and at `a` to a peer confined to `a/*`, -though the route serves neither: a request there fails. - -## Plan - -- `origin::RouteEntry::reach` already holds what a route may serve (its producer's - scope intersected with its prefix). The answer (`lite/publisher/routed.rs` - `Answer::shows`) judges the handle's scope and the permit together - (`Consumer::reaches_any`, `Permit::beneath`) but never the route's reach, since the - node tells a claim without it. Carry the reach, or a predicate over it, to the - answer, and intersect it with what `shows` already judges. -- JS selects among candidates with the same predicate (`presented`'s `shows`), so a - narrower cheap route gives way to one the peer can use; the Rust node presents one - claim per instance, so check whether it needs the same. -- The test: a producer scoped to `a/*/x` announcing at `a`, asked for `a/q/y` with no - grant and for the root under `a/*`, shows nothing; asked for `a/q/x`, shows it. diff --git a/quest/m2/lite07-finalize.md b/quest/m2/lite07-finalize.md index 430e9bc508..71afcac469 100644 --- a/quest/m2/lite07-finalize.md +++ b/quest/m2/lite07-finalize.md @@ -214,6 +214,7 @@ the version that ships them. ## Required - [Cache-aware multicast](/quest/m2/multicast/README.md) - lite-07 carries the redesigned route layer: routes to forwarding nodes, no holds or doubt, claims crossing trust boundaries by a chain, and no Route Key +- [A lite-07 claim is shown no broader than the grants it crossed](/quest/m2/lite07-claim-reach-routes.md) - a peer is never shown a claim at a path no route of it can serve, through any number of relays (maintainer, 2026-11-20) ## Related diff --git a/quest/m2/prefix-grants.md b/quest/m2/prefix-grants.md new file mode 100644 index 0000000000..f1b1518a05 --- /dev/null +++ b/quest/m2/prefix-grants.md @@ -0,0 +1,46 @@ +# [L] A grant is a set of path prefixes again + +## Goal + +Every grant is a set of path prefixes, each covering its whole subtree, as before +[moq-dev/moq#3746](https://github.com/moq-dev/moq/pull/3746). That covers a token's publish +and subscribe lists, `moq auth serve` rules, the relay's public rules, an origin handle's +scope, and lite-07's AUTH_OK. A wildcard grant (`live/*`, `*/chat`, `cam-*.hang`) is refused +where it is made. So a route a grant admits always serves whole subtrees, and a claim's reach +is the path it is shown at ([claim reach](/quest/m2/lite07-claim-reach-routes.md)). + +## Plan + +Decided 2026-11-20 (maintainer, on [moq-dev/moq#9229](https://github.com/moq-dev/moq/pull/9229)): +✅ revert to prefix grants before the lite-07 cut. No use case needed a wildcard grant, and one +makes a claim's reach inexpressible through a relay (rejected: keeping wildcard grants and +carrying a claim's reach as patterns on ANNOUNCE_START and ANNOUNCE_UPDATE). + +- Back out the grant half of #3746 and the pattern-valued scopes of + [moq-dev/moq#3672](https://github.com/moq-dev/moq/pull/3672): + - `Producer::scope`, `Consumer::scope`, and their `allowed` take and return prefixes, in + Rust (`rs/moq-net/src/model/origin.rs`, `OriginScope`) and JS (`js/net`). + - `Permit` (`rs/moq-net/src/auth.rs`) holds prefixes. + - moq-auth and @moq/auth tokens, `moq auth serve` rules, the relay's `public*` config, + moq-ffi, and the bindings carry prefixes. + - Anything that only existed to intersect wildcard grants goes with it, such as + `Consumer::reaches_any` where a prefix test now does. + - Discovery filters, which choose the claims a reader looks at (announcement captures), keep + their patterns: reach does not concern them. +- Wire: lite-07's AUTH_OK carries Publish and Subscribe Prefixes, and the region rule's regions + (`drafts/draft-lcurley-moq-lite.md`, Announce Request) are the scope's prefixes. The Path + Pattern section goes, with any other use the draft keeps. lite-07 is unpublished, so nothing + stays compatible with the pattern form. +- Syntax, recommended: a member `foo` covers `foo` and its subtree, and the empty member covers + everything. A trailing `/**` (and a bare `**`) parses as the same prefix, since it names exactly + that set, so deployed `anon/**` tokens and configs keep their meaning. Every other wildcard is + refused where it is parsed. The migration note warns that a bare member minted since #3746 + widens from one broadcast to its subtree. +- Align the quests that assume wildcard members: `quest/m4/regrant-route-recheck.md` (a member's + literal prefix before its first wildcard) and `quest/m5/peer-grant-bound.md` (the Orthogonal + Vectors bound, which prefixes remove). +- Docs: `doc/bin/relay/auth.md` (Path matching and the rule tables), and each library page that + shows a wildcard grant. + +Public API: breaking (grant types and token contents, in Rust, JS, and the bindings). Wire: +lite-07 AUTH_OK. diff --git a/rs/moq-net/src/lite/publisher/routed.rs b/rs/moq-net/src/lite/publisher/routed.rs index 1af956677b..c58a46b774 100644 --- a/rs/moq-net/src/lite/publisher/routed.rs +++ b/rs/moq-net/src/lite/publisher/routed.rs @@ -167,7 +167,8 @@ pub(in crate::lite) struct Answer { /// The latest marker answered the peer's edit, so the SYNCED after it count. answering: bool, /// How many reads a grant change added have yet to send their initial set, before - /// what it no longer shows goes: a claim a wider read tells again is kept. + /// what it no longer shows goes and what it now shows starts: a claim a wider read tells + /// again is kept. Every prefix's SYNCED waits for that too. regranting: Option, /// The ANNOUNCE_REQUESTs of an edit that continues. parts: Option, @@ -530,17 +531,18 @@ impl Answer { let holds = std::mem::replace(&mut mount.holds, Holds::Nothing); holds.synced().then_some(mount.prefix) } - // What a grant change reads starts after its prefix's SYNCED, and once all - // of it is in, what the change no longer shows goes. + // What a grant change reads: once all of it is in, the claims and the SYNCED + // held back for it settle. A prefix not yet synced waits for its own reads too. None if !self.answering => { if let Some(left) = self.regranting.as_mut() { *left = left.saturating_sub(1); if *left == 0 { - self.regranting = None; - self.resync(writer)?; + self.settle(writer)?; } } - None + (self.prefixes.iter()) + .find(|(_, prefix)| !prefix.synced && prefix.reads.contains(&path)) + .map(|(id, _)| *id) } None => (self.prefixes.iter()) .find(|(_, prefix)| { @@ -668,6 +670,9 @@ impl Answer { added: usize, regions: Option<&[Path]>, ) -> Result<(), Error> { + // The mounts that go below may release a prefix, whose SYNCED waits for the claims + // this change shows in the reads it keeps. + self.regranting = Some(added); let mut gone: Vec = (self.told.iter()) .filter(|(key, (source, _))| *source == MAIN && removed.iter().any(|read| route::within(&key.0, read))) .map(|(key, _)| key.clone()) @@ -680,20 +685,21 @@ impl Answer { .unwrap_or_default(); let dropped: Vec = prefix.reads.difference(&reads).cloned().collect(); let added: Vec = reads.difference(&prefix.reads).cloned().collect(); - // Before its SYNCED, the prefix waits for the mounts read in place of those that go, - // counted before the ones that go release it. + // Before its SYNCED, the prefix waits for what is read in place of what goes, the + // session's key and the mounts beneath each read alike, counted before the mounts + // that go release it. let holds = match prefix.synced { true => Holds::Main, false => Holds::Both, }; - let mut mounted = 0; + let mut waits = added.len(); for read in added { - mounted += self.mount(id, read, holds)?; + waits += self.mount(id, read, holds)?; } let prefix = self.prefixes.get_mut(&id).expect("listed above"); prefix.reads = reads; if !prefix.synced { - prefix.waiting += mounted; + prefix.waiting += waits; } // The mounts beneath a read the grant no longer shows go with it. let sources: Vec = (self.mounts.iter()) @@ -719,14 +725,26 @@ impl Answer { } self.main.flush(); match added { - 0 => self.resync(writer), - added => { - self.regranting = Some(added); - Ok(()) - } + 0 => self.settle(writer), + _ => Ok(()), } } + /// All a grant change reads is in: bring every claim in line with it, then send each + /// SYNCED held back for it. + fn settle(&mut self, writer: &mut Writer) -> Result<(), Error> { + self.regranting = None; + self.resync(writer)?; + let ready: Vec = (self.prefixes.iter()) + .filter(|(_, prefix)| !prefix.synced && prefix.waiting == 0) + .map(|(id, _)| *id) + .collect(); + for id in ready { + self.synced(writer, id, 0)?; + } + Ok(()) + } + /// Bring every claim the peer holds or the node told in line with the grant. fn resync(&mut self, writer: &mut Writer) -> Result<(), Error> { let mut keys: Vec = self.told.keys().cloned().collect(); @@ -739,13 +757,14 @@ impl Answer { } /// `keys` of those answering the prefix with Prefix ID `id` sent their initial set: - /// SYNCED goes out once every one has. + /// SYNCED goes out once every one has, and no grant change is still reading. fn synced(&mut self, writer: &mut Writer, id: u64, keys: usize) -> Result<(), Error> { + let regranting = self.regranting.is_some(); let Some(prefix) = self.prefixes.get_mut(&id).filter(|prefix| !prefix.synced) else { return Ok(()); }; prefix.waiting = prefix.waiting.saturating_sub(keys); - if prefix.waiting > 0 { + if prefix.waiting > 0 || regranting { return Ok(()); } prefix.synced = true; @@ -923,6 +942,10 @@ impl Answer { } (Some(_), None) => { let (id, _) = self.live.remove(&key).expect("matched above"); + // Still told, it left because the answer no longer shows it, as when a change + // queued before a grant change is judged by the new grant: its publisher never + // ended it. + let lost = lost || self.told.contains_key(&key); writer.buffer(&Wire::End { id, lost })?; self.encoder.end(id); } @@ -970,3 +993,6 @@ impl Answer { } } } + +#[cfg(test)] +mod tests; diff --git a/rs/moq-net/src/lite/publisher/routed/tests.rs b/rs/moq-net/src/lite/publisher/routed/tests.rs new file mode 100644 index 0000000000..8fe5a262f5 --- /dev/null +++ b/rs/moq-net/src/lite/publisher/routed/tests.rs @@ -0,0 +1,774 @@ +//! The lite-07 answer checked over random grant changes, claims, mounts, and edits, +//! interleaved with the order its node keys' output is sent in. +//! +//! Two promises, each judged as a peer sees the stream: +//! +//! - When a prefix's SYNCED goes out, every claim the grant in effect shows under it has had +//! its Start. The grant in effect is the one whose change the session's key answered last; +//! a change queued behind it may only take claims away. A claim is owed only when it was +//! there before the read that tells it started: the edit adding the prefix, or the grant +//! change in effect. What a fresh answer under that grant tells for the prefix is what the +//! grant shows. +//! - A claim that leaves while its publisher still announces it ends as lost, never as +//! withdrawn. +//! +//! `MOQ_ANSWER_SEED` and `MOQ_ANSWER_CASES` pick the cases. + +use std::collections::{BTreeMap, BTreeSet}; +use std::sync::{Arc, Mutex}; + +use proptest::prelude::*; +use proptest::test_runner::{Config, RngSeed, TestCaseError, TestRunner}; + +use super::Answer; +use crate::coding::Decode; +use crate::lite::{self, AnnounceBroadcast, Version, subscriber::route_stream::Writer, test_transport::*}; +use crate::model::ProduceTest; +use crate::{Hop, Instance, Path, Pattern, Patterns, origin}; + +/// Where claims are announced: some above what grants and prefixes name, some deeper than a +/// grant's `*` shows, one hidden, and some beneath the mount target `s`. +const PATHS: &[&str] = &["a", "a/x", "a/x/y", "a/.h", "a/m/x", "b", "b/c", "b/c/d", "s", "s/x"]; + +/// The scopes claims are announced through, which bound what each may serve. +const SCOPES: &[&[&str]] = &[&["**"], &["*/x"]]; + +/// The grants the peer gives: widen, narrow, move, and revoke between them. +const GRANTS: &[&[&str]] = &[ + &["**"], + &["a/**"], + &["b/**"], + &["a/x/**"], + &["a/*"], + &["a/m/**"], + &["a/**", "b/**"], + &["a/**", "b/*"], + &[], +]; + +/// The prefixes the peer asks for. +const PREFIXES: &[&str] = &["", "a", "a/x", "b", "a/m", "s"]; + +/// The handle's mount, if any: its point and target. +const MOUNTS: &[Option<(&str, &str)>] = &[None, Some(("a", "s")), Some(("a/m", "s"))]; + +/// The handle's own scope. +const HANDLES: &[&[&str]] = &[&["**"], &["a/**", "b/**", "s/**"]]; + +fn path(path: &str) -> Path { + Path::new(path).unwrap() +} + +fn patterns(members: &[&str]) -> Patterns { + members + .iter() + .map(|member| member.parse::().unwrap()) + .collect() +} + +fn grant(index: usize) -> crate::auth::Grant { + crate::auth::Grant { + publish: Patterns::default(), + subscribe: patterns(GRANTS[index]), + expires: None, + } +} + +#[derive(Clone, Debug)] +enum Op { + /// The grant changes and the session notices. + Grant(usize), + /// The grant changes, noticed only by the peer's next edit. + Quiet(usize), + Announce { + path: usize, + scope: usize, + }, + Withdraw(usize), + Reprice { + path: usize, + cost: u64, + }, + /// Removes the prefixes in effect whose bit is set, and adds these. + Edit { + remove: u8, + add: Vec<(usize, bool)>, + }, + /// Send one thing the session's key queued. + Main, + /// Send everything the session's key queued. + Drain, + /// Send one thing a mount queued. + Mount(usize), +} + +#[derive(Clone, Debug)] +struct Case { + mount: usize, + handle: usize, + initial: Option, + /// Claims announced before anything else, each a path and a scope. + claims: Vec<(usize, usize)>, + ops: Vec, + /// Whether what is left goes out the mounts' first, rather than the session's key's + /// first as a session sends it. + mounts_first: bool, +} + +/// A prefix to ask for, the root most often: it holds every region a grant moves between. +fn prefix() -> impl Strategy { + prop_oneof![2 => Just(0), 3 => 1..PREFIXES.len()] +} + +fn op() -> impl Strategy { + prop_oneof![ + 6 => (0..GRANTS.len()).prop_map(Op::Grant), + 1 => (0..GRANTS.len()).prop_map(Op::Quiet), + 2 => (0..PATHS.len(), 0..SCOPES.len()).prop_map(|(path, scope)| Op::Announce { path, scope }), + 1 => (0..PATHS.len()).prop_map(Op::Withdraw), + 4 => (0..PATHS.len(), 1..4u64).prop_map(|(path, cost)| Op::Reprice { path, cost }), + 3 => (any::(), proptest::collection::vec((prefix(), any::()), 0..3)) + .prop_map(|(remove, add)| Op::Edit { remove, add }), + 5 => Just(Op::Main), + 2 => Just(Op::Drain), + 3 => (0..4usize).prop_map(Op::Mount), + ] +} + +fn case() -> impl Strategy { + ( + prop_oneof![1 => Just(0), 4 => 1..MOUNTS.len()], + 0..HANDLES.len(), + proptest::option::weighted(0.8, 0..GRANTS.len()), + proptest::collection::vec((0..PATHS.len(), 0..SCOPES.len()), 0..6), + proptest::collection::vec(op(), 1..24), + any::(), + ) + .prop_map(|(mount, handle, initial, claims, ops, mounts_first)| Case { + mount, + handle, + initial, + claims, + ops, + mounts_first, + }) +} + +/// A claim as the peer holds it: its path from the session's root, and its instance. +type Held = (Path, Instance); + +/// The announce stream's bytes, decoded as the peer reads them. +struct Peer { + writes: Arc>>, + cursor: usize, + decoder: lite::AnnounceDecoder, + /// The claims it holds, by Announce ID. + live: BTreeMap, +} + +impl Peer { + fn new(log: &Log) -> Self { + Self { + writes: log.writes.clone(), + cursor: 0, + decoder: lite::AnnounceDecoder::default(), + live: BTreeMap::new(), + } + } + + /// Every message written since the last call. + fn take(&mut self) -> Vec { + let buf = self.writes.lock().unwrap()[self.cursor..].to_vec(); + let mut slice = &buf[..]; + let mut msgs = Vec::new(); + while !slice.is_empty() { + let msg = crate::coding::decode_buf(&mut slice, Version::Lite07, |r, v| AnnounceBroadcast::decode(r, v)); + msgs.push(msg.expect("announce message")); + } + self.cursor += buf.len(); + msgs + } + + fn start(&mut self, suffix: &lite::PathRef, instance: &Instance) -> Held { + let path = self + .decoder + .start(Some(suffix.clone())) + .unwrap() + .expect("a path the peer holds"); + let held = (path, instance.clone()); + self.live.insert(self.decoder.last(), held.clone()); + held + } + + fn end(&mut self, id: u64) -> Held { + self.decoder.end(id).unwrap(); + self.live.remove(&id).expect("ends a live claim") + } + + /// Both ends drop every claim within a prefix the edit removed. + fn drop_within(&mut self, removed: &[Path]) { + let gone: Vec = (self.live.iter()) + .filter(|(_, (path, _))| removed.iter().any(|prefix| path.has_prefix(prefix))) + .map(|(id, _)| *id) + .collect(); + for id in gone { + self.end(id); + } + } + + fn holds(&self) -> BTreeSet { + self.live.values().cloned().collect() + } +} + +/// A writer to a fresh log, and the peer reading it. +fn stream() -> (Writer, Peer) { + let log = Log::default(); + let peer = Peer::new(&log); + let writer = Writer::new( + crate::transport::boxed::SendStream::new(SinkSend::new(log)), + Version::Lite07, + ); + (writer, peer) +} + +fn flush(writer: &mut Writer) { + assert!(writer.poll_flush(&kio::Waiter::noop()).is_ready()); +} + +/// What a fresh answer on `consumer` under `grant` reads and tells for `prefix`, once it settles. +struct View { + reads: BTreeSet, + shown: BTreeSet, +} + +/// What a fresh answer on `consumer` under `grant` reads and tells for `prefix`, once it settles. +fn shown(consumer: &origin::Consumer, grant: Option, prefix: &Path, hidden: bool) -> View { + let auth = crate::auth::Link::new(false); + if let Some(index) = grant { + auth.handle().authorize(&self::grant(index)); + } + let mut answer = Answer::alone_on(consumer.clone(), auth); + let (mut writer, mut peer) = stream(); + answer + .request(false, Vec::new(), vec![(prefix.clone(), hidden)]) + .unwrap(); + answer.drain(&mut writer).unwrap(); + // What it settles to, judged again from scratch, so its own SYNCED proves nothing. + answer.resync(&mut writer).unwrap(); + flush(&mut writer); + for msg in peer.take() { + match msg { + AnnounceBroadcast::Start { suffix, instance, .. } => { + peer.start(&suffix, &instance); + } + AnnounceBroadcast::End { id, .. } => { + peer.end(id); + } + _ => {} + } + } + let reads = (answer.prefixes.values().next()) + .map(|prefix| prefix.reads.clone()) + .unwrap_or_default(); + View { + reads, + shown: peer.holds(), + } +} + +/// A prefix in effect: its path, its Hidden, and the step that asked for it. +struct Asked { + path: Path, + hidden: bool, + at: usize, +} + +/// An edit not answered yet: the Prefix IDs it removes, the prefixes it adds, and its step. +struct Edit { + remove: Vec, + add: Vec<(u64, Path, bool)>, + at: usize, +} + +/// A claim the origin holds: its route, instance, and the step that announced it. +struct Claimed { + route: crate::model::AnnounceProducer, + instance: Instance, + at: usize, +} + +struct Sim { + origin: origin::Producer, + consumer: origin::Consumer, + auth: crate::auth::Link, + answer: Answer, + writer: Writer, + peer: Peer, + /// The step of each op that is no drain, so a claim's age compares with a read's. + step: usize, + claims: BTreeMap, + next_instance: u64, + /// The grant the answer started from, then each change it queued, with its step. + grants: Vec<(usize, Option)>, + /// The grant last given, queued or not. + given: Option, + prefixes: BTreeMap, + next_prefix: u64, + edit: Option, + /// The prefixes in effect whose SYNCED went out. + synced: BTreeSet, +} + +impl Sim { + fn new(case: &Case) -> Self { + let origin = Hop::new(1).unwrap().produce(); + let handle = patterns(HANDLES[case.handle]); + let consumer = match MOUNTS[case.mount] { + Some((at, target)) => origin.mount(at, target).unwrap().scope("", &handle).unwrap().consume(), + None => origin.consume().scope("", &handle).unwrap(), + }; + let auth = crate::auth::Link::new(false); + if let Some(index) = case.initial { + auth.handle().authorize(&grant(index)); + } + let answer = Answer::alone_on(consumer.clone(), auth.clone()); + let (writer, peer) = stream(); + Self { + origin, + consumer, + auth, + answer, + writer, + peer, + step: 0, + claims: BTreeMap::new(), + next_instance: 0, + grants: vec![(0, case.initial)], + given: case.initial, + prefixes: BTreeMap::new(), + next_prefix: 0, + edit: None, + synced: BTreeSet::new(), + } + } + + /// How many grant changes the session's key queued and has not answered. + fn unanswered(&self) -> usize { + (self.answer.markers.iter()) + .filter(|marker| matches!(marker, super::Marker::Grant { .. })) + .count() + } + + /// Run `f`, recording a grant change it queued. + fn queues(&mut self, f: impl FnOnce(&mut Self) -> T) -> T { + let before = self.unanswered(); + let out = f(self); + if self.unanswered() > before { + self.grants.push((self.step, self.given)); + } + out + } + + fn apply(&mut self, op: &Op) -> Result<(), TestCaseError> { + if !matches!(op, Op::Main | Op::Drain | Op::Mount(_)) { + self.step += 1; + } + match op { + Op::Grant(index) | Op::Quiet(index) => { + self.given = Some(*index); + self.auth.handle().authorize(&grant(*index)); + if matches!(op, Op::Grant(_)) { + self.queues(|sim| sim.answer.poll_permit(&kio::Waiter::noop())) + .map_err(|err| TestCaseError::fail(format!("poll_permit: {err:?}")))?; + } + } + Op::Announce { path: index, scope } => { + if self.claims.contains_key(index) { + return Ok(()); + } + let Ok(producer) = self.origin.scope("", &patterns(SCOPES[*scope])) else { + return Ok(()); + }; + self.next_instance += 1; + let instance: Instance = format!("i{}", self.next_instance).parse().unwrap(); + let info = origin::RouteInfo::new().with_instance(instance.clone(), 0); + if let Ok(route) = producer.announce(PATHS[*index], info) { + let at = self.step; + self.claims.insert(*index, Claimed { route, instance, at }); + } + } + Op::Withdraw(index) => { + self.claims.remove(index); + } + Op::Reprice { path: index, cost } => { + if let Some(held) = self.claims.get(index) { + let info = origin::RouteInfo::new() + .with_instance(held.instance.clone(), 0) + .with_cost(*cost); + held.route.update(info).unwrap(); + } + } + Op::Edit { remove, add } => { + if self.edit.is_some() { + return Ok(()); + } + let remove: Vec = (self.prefixes.keys().enumerate()) + .filter(|(bit, _)| *bit < 8 && remove & (1 << bit) != 0) + .map(|(_, id)| *id) + .collect(); + // Only what a peer may ask for: no prefix twice, or at or below another in the set. + let mut set: Vec = (self.prefixes.iter()) + .filter(|(id, _)| !remove.contains(id)) + .map(|(_, asked)| asked.path.clone()) + .collect(); + let mut adds = Vec::new(); + for (index, hidden) in add { + let prefix = path(PREFIXES[*index]); + if set + .iter() + .all(|other| !other.has_prefix(&prefix) && !prefix.has_prefix(other)) + { + set.push(prefix.clone()); + adds.push((prefix, *hidden)); + } + } + let add = adds; + let asked = self.queues(|sim| sim.answer.request(false, remove.clone(), add.clone())); + prop_assert!(asked.is_ok(), "a valid edit is refused: {asked:?}"); + { + let mut next = self.next_prefix; + let add = (add.into_iter()) + .map(|(path, hidden)| { + next += 1; + (next - 1, path, hidden) + }) + .collect(); + self.next_prefix = next; + self.edit = Some(Edit { + remove, + add, + at: self.step, + }); + } + } + Op::Main | Op::Mount(_) => { + self.send(op)?; + return Ok(()); + } + Op::Drain => { + while self.send(&Op::Main)? {} + return Ok(()); + } + } + // The origin feeds its node what changed, as its driver would. + self.origin.route_node(); + flush(&mut self.writer); + self.read() + } + + /// Send one thing the session's key, or the `nth` mount, queued, returning whether there + /// was one. The key waits while it is held. + fn send(&mut self, op: &Op) -> Result { + let waiter = kio::Waiter::noop(); + self.answer.main.flush(); + let (source, msg) = match op { + Op::Main if self.answer.holding() => return Ok(false), + Op::Main => match self.answer.main.try_wire() { + Some(msg) => (super::MAIN, msg), + None => return Ok(false), + }, + Op::Mount(nth) => { + let Some(source) = (self.answer.mounts.keys()) + .nth(nth % self.answer.mounts.len().max(1)) + .copied() + else { + return Ok(false); + }; + match self.answer.mounts[&source].stream.poll_wire(&waiter) { + std::task::Poll::Ready(msg) => (source, msg), + std::task::Poll::Pending => return Ok(false), + } + } + _ => unreachable!("only a drain sends"), + }; + (self.answer.sourced(&mut self.writer, source, msg)) + .map_err(|err| TestCaseError::fail(format!("output: {err:?}")))?; + self.origin.route_node(); + flush(&mut self.writer); + self.read()?; + Ok(true) + } + + /// Read what the answer sent, checking each promise as the peer meets it. + fn read(&mut self) -> Result<(), TestCaseError> { + for msg in self.peer.take() { + match msg { + AnnounceBroadcast::Start { suffix, instance, .. } => { + self.peer.start(&suffix, &instance); + } + AnnounceBroadcast::Change { id, .. } => { + prop_assert!(self.peer.live.contains_key(&id), "a change to {id}, which is not live"); + } + AnnounceBroadcast::End { id, lost } => { + let (path, instance) = self.peer.end(id); + let announced = self.claims.values().any(|held| held.instance == instance); + prop_assert!( + lost || !announced, + "{path} ({instance}) ended as withdrawn while its publisher still announces it" + ); + } + AnnounceBroadcast::Ok => { + let edit = self.edit.take().expect("an ANNOUNCE_OK answers an edit"); + for id in &edit.remove { + self.synced.remove(id); + } + let removed: Vec = (edit.remove.iter()) + .filter_map(|id| self.prefixes.remove(id)) + .map(|asked| asked.path) + .collect(); + self.peer.drop_within(&removed); + for (id, path, hidden) in edit.add { + self.prefixes.insert( + id, + Asked { + path, + hidden, + at: edit.at, + }, + ); + } + } + AnnounceBroadcast::Synced { prefix } => { + prop_assert!(self.synced.insert(prefix), "a second SYNCED for {prefix}"); + self.synced(prefix)?; + } + msg => prop_assert!(false, "a claims-only peer is sent {msg:?}"), + } + } + Ok(()) + } + + /// SYNCED for `id` went out: every claim the grant in effect shows under it, there before + /// the read that tells it started, has had its Start. That read started with the edit + /// adding the prefix, or with the latest grant change in effect that read it when the one + /// before did not. A change that keeps the read only judges again what it told, so a claim + /// announced after the read started is a live change, whenever it comes. + fn synced(&self, id: u64) -> Result<(), TestCaseError> { + let asked = self.prefixes.get(&id).expect("SYNCED names a prefix in effect"); + let effect = self.grants.len() - 1 - self.unanswered(); + // The grant the edit was read under, then each change answered or queued since. + let first = (self.grants.iter().rposition(|(step, _)| *step <= asked.at)) + .unwrap_or(0) + .min(effect); + let views: Vec = (self.grants[first..].iter()) + .map(|(_, grant)| shown(&self.consumer, *grant, &asked.path, asked.hidden)) + .collect(); + let view = |index: usize| &views[index - first]; + let held = self.peer.holds(); + for claim in &view(effect).shown { + // A change queued behind the one in effect may only take claims away. + if (effect + 1..self.grants.len()).any(|later| !view(later).shown.contains(claim)) { + continue; + } + let (path, instance) = claim; + // The read that tells it: the one it lies in, or the one it is presented at. + let read = + |index: usize| (view(index).reads.iter()).find(|read| path.has_prefix(read) || read.has_prefix(path)); + let since = (first + 1..=effect) + .rev() + .find(|index| read(*index).is_none_or(|read| !view(index - 1).reads.contains(read))) + .map_or(asked.at, |index| self.grants[index].0); + let old = (self.claims.values()).any(|held| held.instance == *instance && held.at < since); + prop_assert!( + !old || held.contains(claim), + "SYNCED for {:?} went out before {path} ({instance}) under grant {:?}", + asked.path, + self.grants[effect].1.map(|index| GRANTS[index]), + ); + } + Ok(()) + } +} + +fn run(case: &Case) -> Result<(), TestCaseError> { + let mut sim = Sim::new(case); + for (path, scope) in &case.claims { + sim.apply(&Op::Announce { + path: *path, + scope: *scope, + })?; + } + for op in &case.ops { + sim.apply(op)?; + } + // Then everything queued goes out, one side drained before the other. + loop { + let mut sent = false; + if !case.mounts_first { + while sim.send(&Op::Main)? { + sent = true; + } + } + for nth in 0..sim.answer.mounts.len() { + while sim.send(&Op::Mount(nth))? { + sent = true; + } + } + if !sent && !sim.send(&Op::Main)? { + break; + } + } + // Once everything is out, every prefix in effect has had its SYNCED. + for (id, asked) in &sim.prefixes { + prop_assert!(sim.synced.contains(id), "no SYNCED for {:?} once drained", asked.path); + } + Ok(()) +} + +/// Runs the cases `MOQ_ANSWER_SEED` and `MOQ_ANSWER_CASES` pick. +#[test] +fn synced_follows_what_the_grant_shows() { + let setting = |name: &str, fallback: u64| std::env::var(name).map_or(fallback, |value| value.parse().expect(name)); + let seed = setting("MOQ_ANSWER_SEED", 0x616e73); + let count = setting("MOQ_ANSWER_CASES", 1024) as u32; + let mut runner = TestRunner::new(Config { + cases: count, + rng_seed: RngSeed::Fixed(seed), + failure_persistence: None, + ..Default::default() + }); + runner.run(&case(), |case| run(&case)).unwrap(); +} + +/// Each message `answer` sends once everything queued goes out, the session's key first. +fn sent(answer: &mut Answer, writer: &mut Writer, peer: &mut Peer) -> Vec { + answer.drain(writer).unwrap(); + flush(writer); + peer.take() +} + +/// A root request under `a/**` still waiting on the mount at `a` when the grant moves to +/// `b/**` holds its SYNCED for the initial set of `b`, read in place of `a`. +#[test] +fn a_grant_change_before_synced_holds_it_for_what_is_read_instead() { + let origin = Hop::new(1).unwrap().produce(); + let _bc = origin.announce("b/c", origin::RouteInfo::new()).unwrap(); + let _cam = origin.announce("s/cam", origin::RouteInfo::new()).unwrap(); + let consumer = origin + .mount("a", "s") + .unwrap() + .scope("", &patterns(&["**"])) + .unwrap() + .consume(); + let auth = crate::auth::Link::new(false); + auth.handle().authorize(&grant(1)); + let mut answer = Answer::alone_on(consumer, auth.clone()); + let (mut writer, mut peer) = stream(); + answer.request(false, Vec::new(), vec![(path(""), false)]).unwrap(); + // The session's key answers the edit and sends what it reads of `a` before the mount at + // `a` sends its initial set; the grant moves meanwhile. + let mut msgs = Vec::new(); + while let Some(msg) = answer.main.try_wire() { + answer.output(&mut writer, msg).unwrap(); + } + auth.handle().authorize(&grant(2)); + answer.poll_permit(&kio::Waiter::noop()).unwrap(); + msgs.extend(sent(&mut answer, &mut writer, &mut peer)); + assert_eq!(events(&mut peer, &msgs), ["b/c", "SYNCED 0"], "{msgs:?}"); +} + +/// What the peer reads in `msgs`, in order: the path of each Start, and each SYNCED. +fn events(peer: &mut Peer, msgs: &[AnnounceBroadcast]) -> Vec { + let mut events = Vec::new(); + for msg in msgs { + match msg { + AnnounceBroadcast::Start { suffix, instance, .. } => { + events.push(peer.start(suffix, instance).0.to_string()); + } + AnnounceBroadcast::End { id, .. } => { + peer.end(*id); + } + AnnounceBroadcast::Synced { prefix } => events.push(format!("SYNCED {prefix}")), + _ => {} + } + } + events +} + +/// A re-price queued before the grant revokes the claim is judged by the new grant, and +/// the claim it hides ends as lost, since its publisher never withdrew it. +#[test] +fn a_change_queued_before_a_revocation_ends_its_claim_as_lost() { + let origin = Hop::new(1).unwrap().produce(); + let ac = origin.announce("a/c", origin::RouteInfo::new()).unwrap(); + let auth = crate::auth::Link::new(false); + auth.handle().authorize(&grant(1)); + let mut answer = Answer::alone_on(origin.consume(), auth.clone()); + let (mut writer, mut peer) = stream(); + answer.request(false, Vec::new(), vec![(path(""), false)]).unwrap(); + let first = sent(&mut answer, &mut writer, &mut peer); + let AnnounceBroadcast::Start { instance, .. } = first[1].clone() else { + panic!("{first:?}"); + }; + ac.update(origin::RouteInfo::new().with_instance(instance, 0).with_cost(5)) + .unwrap(); + origin.route_node(); + auth.handle().authorize(&grant(2)); + answer.poll_permit(&kio::Waiter::noop()).unwrap(); + let revoked = sent(&mut answer, &mut writer, &mut peer); + let ends: Vec = (revoked.iter()) + .filter_map(|msg| match msg { + AnnounceBroadcast::End { lost, .. } => Some(*lost), + _ => None, + }) + .collect(); + assert_eq!(ends, [true], "{revoked:?}"); +} + +/// A claim the old grant hid inside a read the new grant keeps is started before the SYNCED +/// of a prefix that another read of the change releases first: `a` waits on the mount at +/// `a/drop` when the grant moves from `a/u/*/old` to `a/u/*/new`, and SYNCED for `a` follows +/// `a/u/z/new`, though the read of `a/v` finishes before that of `b`. +#[test] +fn a_grant_change_shows_what_it_keeps_before_synced() { + let origin = Hop::new(1).unwrap().produce(); + let _new = origin.announce("a/u/z/new", origin::RouteInfo::new()).unwrap(); + let consumer = origin + .mount("a/drop", "s") + .unwrap() + .scope("", &patterns(&["**"])) + .unwrap() + .consume(); + let auth = crate::auth::Link::new(false); + let authorize = |subscribe: &[&str]| { + auth.handle().authorize(&crate::auth::Grant { + publish: Patterns::default(), + subscribe: patterns(subscribe), + expires: None, + }) + }; + authorize(&["a/u/*/old", "a/drop/**"]); + let mut answer = Answer::alone_on(consumer, auth.clone()); + let (mut writer, mut peer) = stream(); + answer + .request(false, Vec::new(), vec![(path("a"), false), (path("b"), false)]) + .unwrap(); + // The session's key answers the edit; the mount at `a/drop` has yet to send its initial set. + answer.main.flush(); + while let Some(msg) = answer.main.try_wire() { + answer.output(&mut writer, msg).unwrap(); + } + flush(&mut writer); + let first = peer.take(); + assert_eq!(events(&mut peer, &first), ["SYNCED 1"], "{first:?}"); + + authorize(&["a/u/*/new", "a/v/**", "b/**"]); + answer.poll_permit(&kio::Waiter::noop()).unwrap(); + let regranted = sent(&mut answer, &mut writer, &mut peer); + assert_eq!( + events(&mut peer, ®ranted), + ["a/u/z/new", "SYNCED 0"], + "{regranted:?}" + ); +}