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
+59 -1
View File
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import { after, before, test } from "node:test";
import type { Envelope } from "../src/message.ts";
import { mailbox } from "../src/message.ts";
import { envelopeHash, mailbox } from "../src/message.ts";
import { NOW, newParty, openAsOwner, openPage, sealReply, startGateway, type Gateway, type Page } from "./helpers.ts";
const owner = newParty({ title: "Altbau in Ottensen" });
@@ -110,6 +110,64 @@ test("a node that refuses the message is reported and the text is kept", async (
}
});
/** Leading zero bits of an envelope's hash: the work its sender did. */
function workOf(env: Envelope): number {
let bits = 0;
for (const c of envelopeHash(env)) {
const v = parseInt(c, 16);
if (v === 0) {
bits += 4;
continue;
}
return bits + Math.clz32(v) - 28;
}
return bits;
}
test("the page asks what the listing's queue costs and mines to that price", async () => {
gw.requiredBits = 10; // the node's usual price is 6
const before = gw.posted.length;
const p = openPage(gw.url, detail);
try {
await write(p, "Ist die Wohnung noch frei?");
await p.waitFor(() => gw.posted.length === before + 1, "the message to arrive");
assert.ok(workOf(JSON.parse(gw.posted[before]!) as Envelope) >= 10, "it did the work the node asked for");
} finally {
gw.requiredBits = undefined;
p.close();
}
});
test("a price that rose while mining is paid, once more, and the message gets through", async () => {
gw.refuseOnceAskingFor = 9; // the browser mined 6, then somebody else's message raised the price
const before = gw.posted.length;
const p = openPage(gw.url, detail);
try {
await write(p, "Hallo, noch frei?");
await p.waitFor(() => gw.posted.length === before + 1, "the retried message to arrive");
assert.ok(workOf(JSON.parse(gw.posted[before]!) as Envelope) >= 9);
await p.waitFor(() => p.window.location.hash.startsWith("#/c/"), "the conversation view");
} finally {
gw.refuseOnceAskingFor = undefined;
p.close();
}
});
test("a flooded listing is not mined for: the sender is told to come back later", async () => {
gw.requiredBits = 40; // far more than a browser should be asked to do
const before = gw.posted.length;
const p = openPage(gw.url, detail);
try {
await write(p, "Hallo?");
await p.waitFor(() => /receiving a lot of messages/.test(text(p)), "the busy message");
assert.equal(gw.posted.length, before, "nothing was sent");
assert.equal(p.document.querySelector("textarea")!.value, "Hallo?");
} finally {
gw.requiredBits = undefined;
p.close();
}
});
test("verified replies and grants are shown; forged and expired ones are not", async () => {
const secret = "5".repeat(64);
// The thread public key follows from the secret; compute it with the same code the page uses.
+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" });
});