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
+12
View File
@@ -176,6 +176,10 @@ export interface Gateway {
mailboxes: Map<string, unknown[]>;
/** Make POST /api/v1/messages answer with this status and error. */
postFails?: { status: number; error: string };
/** What GET /api/v1/messages/required answers (bits); unset, the route does not exist (an older node). */
requiredBits?: number;
/** Refuse the next POST once with 429, asking for this many bits of proof-of-work. */
refuseOnceAskingFor?: number;
/** Plays the node's owner endpoints (/mine, /inbox, .../address); unset, they answer 404. */
owner?: (req: {
method: string;
@@ -235,12 +239,20 @@ export async function startGateway(opts: GatewayOptions): Promise<Gateway> {
});
return;
}
if (url.pathname === "/api/v1/messages/required" && gateway.requiredBits !== undefined) {
return json(200, { bits: gateway.requiredBits });
}
if (url.pathname === "/api/v1/messages") {
if (req.method === "POST") {
let body = "";
req.on("data", (c) => (body += c));
req.on("end", () => {
if (gateway.postFails) return json(gateway.postFails.status, { error: gateway.postFails.error });
if (gateway.refuseOnceAskingFor !== undefined) {
const bits = gateway.refuseOnceAskingFor;
gateway.refuseOnceAskingFor = undefined;
return json(429, { error: "needs more work", required_bits: bits });
}
posted.push(body);
json(202, { hash: "x" });
});