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:
+59
-1
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user