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:
+24
-3
@@ -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. */
|
||||
|
||||
+32
-11
@@ -2,7 +2,7 @@
|
||||
// and the list of conversations. Everything from the network is shown as text
|
||||
// only, and owner messages only after message.ts verified them.
|
||||
|
||||
import { HttpError, getListing, pollMailbox, postMessage } from "./api.ts";
|
||||
import { HttpError, getListing, getRequiredBits, pollMailbox, postMessage } from "./api.ts";
|
||||
import { append, clear, h, sendOnCtrlEnter } from "./dom.ts";
|
||||
import { keepFresh } from "./fresh.ts";
|
||||
import { date } from "./format.ts";
|
||||
@@ -46,6 +46,7 @@ function describe(e: unknown): string {
|
||||
if (e instanceof InputError) return t(`problem.${e.problem}` as Key);
|
||||
if (e instanceof HttpError) {
|
||||
if (e.status === 404) return t("send.404");
|
||||
if (e.status === 429 && e.requiredBits) return t("send.busy");
|
||||
if (e.status === 429) return t("send.429");
|
||||
return t("send.other", { detail: e.detail });
|
||||
}
|
||||
@@ -113,20 +114,40 @@ function composer(opts: {
|
||||
return form;
|
||||
}
|
||||
|
||||
/**
|
||||
* The most proof-of-work a browser mines for one message: 2^24 hashes, about
|
||||
* half a minute. A listing whose queue asks for more is being flooded, and the
|
||||
* sender is told to try again later rather than left waiting.
|
||||
*/
|
||||
export const MAX_WORK_BITS = 24;
|
||||
|
||||
/** Sends the next message of a conversation and records it. */
|
||||
async function sendTo(c: Conversation, listing: Listing, text: string, first: boolean): Promise<void> {
|
||||
const n = c.messages.filter((m) => m.dir === "out").length + 1;
|
||||
const now = nowSeconds();
|
||||
const env = await buildToOwner({
|
||||
listing,
|
||||
threadSecret: fromHex(c.secret),
|
||||
text,
|
||||
n,
|
||||
now,
|
||||
difficulty: difficulty(),
|
||||
first,
|
||||
});
|
||||
await postMessage(env);
|
||||
// The price rises as a listing's queue fills: ask first, and if it rose while
|
||||
// mining (someone else sent a message) mine again at the new price.
|
||||
let bits = Math.max(difficulty(), (await getRequiredBits(listing.author, listing.id)) ?? 0);
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
if (bits > MAX_WORK_BITS) throw new HttpError(429, "this listing is receiving too many messages right now", bits);
|
||||
const env = await buildToOwner({
|
||||
listing,
|
||||
threadSecret: fromHex(c.secret),
|
||||
text,
|
||||
n,
|
||||
now,
|
||||
difficulty: bits,
|
||||
first,
|
||||
});
|
||||
try {
|
||||
await postMessage(env);
|
||||
break;
|
||||
} catch (e) {
|
||||
const asked = e instanceof HttpError && e.status === 429 ? e.requiredBits : undefined;
|
||||
if (asked === undefined || asked <= bits || attempt >= 2) throw e;
|
||||
bits = asked;
|
||||
}
|
||||
}
|
||||
c.messages.push({ dir: "out", n, sent_at: now, t: "text", text });
|
||||
c.updated = now;
|
||||
saveConversation(c);
|
||||
|
||||
@@ -71,6 +71,7 @@ const de = {
|
||||
"send.404":
|
||||
"Dieser Knoten kennt die Anzeige nicht (mehr), oder der Anbieter hat sein Gerät gewechselt. Lade die Anzeige neu.",
|
||||
"send.429": "Zu viele Nachrichten. Bitte warte einen Moment.",
|
||||
"send.busy": "Diese Anzeige bekommt gerade sehr viele Nachrichten. Versuche es später noch einmal.",
|
||||
"send.other": "Senden fehlgeschlagen: {detail}",
|
||||
"send.network": "Der Knoten ist nicht erreichbar.",
|
||||
"chat.back": "← Zur Anzeige",
|
||||
@@ -353,6 +354,7 @@ const en: Record<keyof typeof de, string> = {
|
||||
"send.404":
|
||||
"This node does not know the listing (anymore), or the owner changed device. Reload the listing.",
|
||||
"send.429": "Too many messages. Please wait a moment.",
|
||||
"send.busy": "This listing is receiving a lot of messages right now. Please try again later.",
|
||||
"send.other": "Sending failed: {detail}",
|
||||
"send.network": "The node is not reachable.",
|
||||
"chat.back": "← Back to the listing",
|
||||
|
||||
Reference in New Issue
Block a user