messaging: the price of writing to a listing rises as its queue fills

Flooding a listing's inbox now gets dearer with every envelope. Relays ask
more proof-of-work from half full on, the node tells browsers the current
price, and the browser mines to it (or says the listing is too busy).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-18 20:59:11 +01:00
co-authored by Claude Sonnet 5.5
parent 13c82f8d59
commit 253eb6e75c
12 changed files with 379 additions and 35 deletions
+24 -3
View File
@@ -42,10 +42,29 @@ export async function getListing(author: string, id: string): Promise<unknown |
export class HttpError extends Error {
readonly status: number;
readonly detail: string;
constructor(status: number, detail: string) {
/** For a refused message: the proof-of-work, in bits, the listing's queue asks right now. */
readonly requiredBits?: number;
constructor(status: number, detail: string, requiredBits?: number) {
super(`${status} ${detail}`);
this.status = status;
this.detail = detail;
this.requiredBits = requiredBits;
}
}
/**
* The proof-of-work a message to this listing needs right now. It rises while
* the listing's queue fills, so a sender asks before mining. Null if the node
* cannot say (an older node): the caller then uses the node's usual difficulty.
*/
export async function getRequiredBits(author: string, id: string): Promise<number | null> {
try {
const res = await fetch(`/api/v1/messages/required?author=${author}&id=${id}`);
if (!res.ok) return null;
const { bits } = (await res.json()) as { bits?: unknown };
return typeof bits === "number" && Number.isInteger(bits) && bits > 0 && bits < 256 ? bits : null;
} catch {
return null;
}
}
@@ -58,13 +77,15 @@ export async function postMessage(envelope: Envelope): Promise<void> {
});
if (res.ok) return;
let detail = res.statusText;
let requiredBits: number | undefined;
try {
const body = (await res.json()) as { error?: unknown };
const body = (await res.json()) as { error?: unknown; required_bits?: unknown };
if (typeof body.error === "string") detail = body.error;
if (typeof body.required_bits === "number" && Number.isInteger(body.required_bits)) requiredBits = body.required_bits;
} catch {
// keep the status text
}
throw new HttpError(res.status, detail);
throw new HttpError(res.status, detail, requiredBits);
}
/** Envelopes in a mailbox created at or after `since` (unix seconds). Unverified. */