Skip to content

Commit c7d773c

Browse files
author
Carl
committed
fix(relay): retain writer routing when hint confirmations retire
Signed-off-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz>
1 parent 5b0fe1b commit c7d773c

4 files changed

Lines changed: 112 additions & 108 deletions

File tree

‎docs/relay-queries.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ is interrupted, including its metadata phase, its next pass retains writer routi
8383
existing cooldown; a successful pass returns later refreshes to ordinary routing.
8484
Signed membership hints use writer-backed exact reads or the existing full-roster
8585
fallback, including metadata. A replica pass superseding pending exact hint
86-
confirmations queues a writer-backed pass to settle those grants.
86+
confirmations queues a writer-backed pass to settle those grants. If list failure,
87+
disconnect or cache clear retires queued or in-flight hint confirmations, the store
88+
retains their writer requirement for the next deliberate refresh, Retry or
89+
establishment, without starting an automatic recovery pass or bypassing cooldown.
8790
Ordinary startup, browsing, reconnect, and DM visibility refresh stay replica-
8891
eligible. The details editor and member-administration capability use writer-backed
8992
state for their shared load/preflight/confirmation reads; the member dialog's

‎src/features/relay/live-demand.test.ts‎

Lines changed: 98 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,37 @@ function exactFilters(h: ReturnType<typeof setup>, ids: string[]) {
291291
];
292292
}
293293

294+
// A retired hint must survive to deliberate recovery, without making ordinary
295+
// refreshes writer-backed once that obligation has been fulfilled.
296+
async function recoverRetiredHint(h: ReturnType<typeof setup>) {
297+
expect(h.wire.pending).toHaveLength(1);
298+
const recovery = h.wire.next();
299+
expect(recovery.filters[0]?.kinds).toEqual([39002]);
300+
expect(recovery.filters[0]?.["#d"]).toBeUndefined();
301+
expect(recovery.filters[0]?.consistency).toBe("strong");
302+
recovery.respond(
303+
recovery.filters[0]?.consistency === "strong"
304+
? [
305+
metadata(h.relay, "a", "Alpha"),
306+
roster(h.relay, "a", [h.viewer.pubkey]),
307+
]
308+
: [],
309+
);
310+
await flush();
311+
expect(h.owner.session.channels.list()).toMatchObject({
312+
status: "ready",
313+
channels: [{ id: "a", name: "Alpha" }],
314+
});
315+
expect(h.owner.session.live.snapshot().roster.state).toBe("verified");
316+
expect(h.wire.pending).toHaveLength(0);
317+
h.owner.session.channels.refreshList?.();
318+
const ordinary = h.wire.next();
319+
expect(ordinary.filters[0]).not.toHaveProperty("consistency");
320+
ordinary.respond([]);
321+
await flush();
322+
expect(h.wire.pending).toHaveLength(0);
323+
}
324+
294325
it("a named member-added hint confirms only that channel and preserves verified roster state", async () => {
295326
const h = setup();
296327
try {
@@ -590,97 +621,82 @@ it("membership hints from another signer or for another viewer do not read disco
590621
}
591622
});
592623

593-
it("cache-clear retires an exact membership confirmation without automatic recovery", async () => {
594-
const h = setup();
595-
try {
596-
await readyRoster(h);
597-
h.live.receive([membershipHint(h, "a")]);
598-
await flush();
599-
const exact = h.wire.next();
600-
expect(exact.filters).toEqual(exactFilters(h, ["a"]));
601-
await h.owner.clearCache();
602-
exact.respond([
603-
metadata(h.relay, "a", "Alpha"),
604-
roster(h.relay, "a", [h.viewer.pubkey]),
605-
]);
606-
await flush();
607-
await flush();
608-
expect(exact.signal?.aborted).toBe(true);
609-
expect(h.owner.session.channels.get?.("a")).toBeUndefined();
610-
expect(h.wire.pending).toHaveLength(0);
611-
expect(h.owner.session.live.snapshot().roster.state).toBe("verified");
612-
} finally {
613-
h.owner.dispose();
614-
}
615-
});
616-
617-
it("disconnect retires an exact membership confirmation until new establishment", async () => {
618-
const h = setup();
619-
try {
620-
await readyRoster(h);
621-
h.live.receive([membershipHint(h, "a")]);
622-
await flush();
623-
const exact = h.wire.next();
624-
h.live.state({ status: "retrying", routes: [] });
625-
exact.respond([
626-
metadata(h.relay, "a", "Alpha"),
627-
roster(h.relay, "a", [h.viewer.pubkey]),
628-
]);
629-
await flush();
630-
await flush();
631-
expect(exact.signal?.aborted).toBe(true);
632-
expect(h.owner.session.channels.get?.("a")).toBeUndefined();
633-
expect(h.wire.pending).toHaveLength(0);
634-
h.connected();
635-
h.live.established();
636-
await flush();
637-
const full = h.wire.next();
638-
expect(full.filters[0]?.["#d"]).toBeUndefined();
639-
full.respond([]);
640-
await flush();
641-
expect(h.owner.session.live.snapshot().roster.state).toBe("verified");
642-
expect(h.wire.pending).toHaveLength(0);
643-
} finally {
644-
h.owner.dispose();
645-
}
646-
});
647-
648-
it.each(["cache-clear", "disconnect"])(
649-
"a %s before a queued hint dispatches retires it without an exact or fallback read",
650-
async (reset) => {
624+
it.each([
625+
{ reset: "cache-clear", inFlight: false },
626+
{ reset: "cache-clear", inFlight: true },
627+
{ reset: "disconnect", inFlight: false },
628+
{ reset: "disconnect", inFlight: true },
629+
])(
630+
"$reset retires a hint (in flight: $inFlight) but preserves writer routing for deliberate recovery",
631+
async ({ reset, inFlight }) => {
651632
const h = setup();
652633
try {
653634
await readyRoster(h);
654-
// The broker drains traffic before connection state in one loop, so the
655-
// hint is still queued behind its timer when the reset arrives. The reset
656-
// leaves the list ready; only the queue itself can stop the dispatch.
657635
h.live.receive([membershipHint(h, "a")]);
636+
if (inFlight) await flush();
637+
const exact = inFlight ? h.wire.next() : undefined;
638+
if (exact) expect(exact.filters).toEqual(exactFilters(h, ["a"]));
658639
if (reset === "cache-clear") await h.owner.clearCache();
659640
else h.live.state({ status: "retrying", routes: [] });
641+
if (exact) {
642+
expect(exact.signal?.aborted).toBe(true);
643+
exact.respond([
644+
metadata(h.relay, "a", "Alpha"),
645+
roster(h.relay, "a", [h.viewer.pubkey]),
646+
]);
647+
}
660648
await flush();
661649
await flush();
650+
// Retirement must neither admit a delayed grant nor start a recovery pass.
662651
expect(h.wire.pending).toHaveLength(0);
663652
expect(h.owner.session.channels.get?.("a")).toBeUndefined();
664653
expect(h.owner.session.channels.list().status).toBe("ready");
654+
expect(h.owner.session.live.snapshot().roster.state).toBe("verified");
665655
if (reset === "disconnect") {
666656
h.connected();
667657
h.live.established();
668-
await flush();
669-
const full = h.wire.next();
670-
expect(full.filters[0]?.kinds).toEqual([39002]);
671-
expect(full.filters[0]?.["#d"]).toBeUndefined();
672-
full.respond([
673-
metadata(h.relay, "a", "Alpha"),
674-
roster(h.relay, "a", [h.viewer.pubkey]),
675-
]);
676-
await flush();
677-
expect(h.owner.session.channels.list()).toMatchObject({
678-
status: "ready",
679-
channels: [{ id: "a", name: "Alpha" }],
680-
});
681-
}
682-
expect(h.owner.session.live.snapshot().roster.state).toBe("verified");
658+
} else h.owner.session.channels.refreshList?.();
659+
await flush();
660+
await recoverRetiredHint(h);
661+
} finally {
662+
h.owner.dispose();
663+
}
664+
},
665+
);
666+
667+
it.each(["cache-clear", "disconnect"])(
668+
"%s during a replica pass retains a later hint without automatically rerunning the interrupted pass",
669+
async (reset) => {
670+
const h = setup();
671+
try {
672+
await readyRoster(h);
673+
h.owner.session.channels.refreshList?.();
674+
const full = h.wire.next();
675+
expect(full.filters[0]).not.toHaveProperty("consistency");
676+
h.live.receive([membershipHint(h, "a")]);
677+
await flush();
678+
const exact = h.wire.next();
679+
expect(exact.filters).toEqual(exactFilters(h, ["a"]));
680+
if (reset === "cache-clear") await h.owner.clearCache();
681+
else h.live.state({ status: "retrying", routes: [] });
682+
expect(exact.signal?.aborted).toBe(true);
683+
expect(full.signal?.aborted).toBe(true);
684+
exact.respond([
685+
metadata(h.relay, "a", "Alpha"),
686+
roster(h.relay, "a", [h.viewer.pubkey]),
687+
]);
688+
full.respond([]);
689+
await flush();
690+
await flush();
691+
expect(h.owner.session.live.snapshot().roster.state).toBe("deferred");
692+
expect(h.owner.session.channels.get?.("a")).toBeUndefined();
683693
expect(h.wire.pending).toHaveLength(0);
694+
if (reset === "disconnect") {
695+
h.connected();
696+
h.live.established();
697+
} else h.owner.session.live.retry();
698+
await flush();
699+
await recoverRetiredHint(h);
684700
} finally {
685701
h.owner.dispose();
686702
}
@@ -714,22 +730,14 @@ it("an exact membership confirmation cannot overwrite a concurrent roster error"
714730
});
715731
expect(h.owner.session.live.snapshot().roster.state).toBe("error");
716732
expect(h.wire.pending).toHaveLength(0);
717-
vi.spyOn(performance, "now").mockReturnValue(performance.now() + 61000);
718733
h.owner.session.live.retry();
719734
await flush();
720-
h.wire
721-
.next()
722-
.respond([
723-
metadata(h.relay, "a", "Alpha"),
724-
roster(h.relay, "a", [h.viewer.pubkey]),
725-
]);
726-
await flush();
727-
expect(h.owner.session.channels.list()).toMatchObject({
728-
status: "ready",
729-
channels: [{ id: "a", name: "Alpha" }],
730-
});
731-
expect(h.owner.session.live.snapshot().roster.state).toBe("verified");
732735
expect(h.wire.pending).toHaveLength(0);
736+
expect(h.owner.session.channels.list().status).toBe("error");
737+
vi.spyOn(performance, "now").mockReturnValue(performance.now() + 61000);
738+
h.owner.session.live.retry();
739+
await flush();
740+
await recoverRetiredHint(h);
733741
} finally {
734742
h.owner.dispose();
735743
}
@@ -769,21 +777,7 @@ it("a concurrent roster failure without a cooldown retires the exact confirmatio
769777
expect(h.wire.pending).toHaveLength(0);
770778
h.owner.session.live.retry();
771779
await flush();
772-
expect(h.wire.pending).toHaveLength(1);
773-
const recovery = h.wire.next();
774-
expect(recovery.filters[0]?.kinds).toEqual([39002]);
775-
expect(recovery.filters[0]?.["#d"]).toBeUndefined();
776-
recovery.respond([
777-
metadata(h.relay, "a", "Alpha"),
778-
roster(h.relay, "a", [h.viewer.pubkey]),
779-
]);
780-
await flush();
781-
expect(h.owner.session.channels.list()).toMatchObject({
782-
status: "ready",
783-
channels: [{ id: "a", name: "Alpha" }],
784-
});
785-
expect(h.owner.session.live.snapshot().roster.state).toBe("verified");
786-
expect(h.wire.pending).toHaveLength(0);
780+
await recoverRetiredHint(h);
787781
} finally {
788782
h.owner.dispose();
789783
}

‎src/features/relay/session.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,10 +1905,12 @@ export function createRelaySession(
19051905
}
19061906
/** A cache clear, a disconnect or a list that leaves ready drops hints with
19071907
* the rest of the session's reader work, before a queued batch's timer can
1908-
* dispatch it into the new epoch, and releases any inherited grants: a ready
1909-
* commit must not hide a failed discovery, and the next establishment's full
1910-
* pass or Retry owns recovery. */
1908+
* dispatch it into the new epoch. Transfer their writer requirement to the
1909+
* store without scheduling work: a ready commit must not hide a failed
1910+
* discovery, and the next establishment's full pass or Retry owns recovery. */
19111911
function dropHintConfirmations() {
1912+
if (hintQueue.size > 0 || hintReads.size > 0)
1913+
channels.requireStrongListRead();
19121914
rosterOwesGrants = false;
19131915
retireHintConfirmations();
19141916
}

‎src/features/relay/store.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,11 @@ export function createChannelStore(
20172017
return {
20182018
queries,
20192019
roster: () => rosterRefresh,
2020+
/** Preserve a retired exact confirmation's routing without scheduling work.
2021+
* The next refresh/Retry owns dispatch and the existing cooldown. */
2022+
requireStrongListRead() {
2023+
if (!disposed) strongListAgain = true;
2024+
},
20202025
retryList() {
20212026
if (rosterRefresh.state === "error" || rosterRefresh.state === "deferred")
20222027
void discover(true);

0 commit comments

Comments
 (0)