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:
Vendored
+42
-12
@@ -45,12 +45,25 @@ async function getListing(author, id) {
|
||||
var HttpError = class extends Error {
|
||||
status;
|
||||
detail;
|
||||
constructor(status, detail) {
|
||||
/** For a refused message: the proof-of-work, in bits, the listing's queue asks right now. */
|
||||
requiredBits;
|
||||
constructor(status, detail, requiredBits) {
|
||||
super(`${status} ${detail}`);
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
this.requiredBits = requiredBits;
|
||||
}
|
||||
};
|
||||
async function getRequiredBits(author, id) {
|
||||
try {
|
||||
const res = await fetch(`/api/v1/messages/required?author=${author}&id=${id}`);
|
||||
if (!res.ok) return null;
|
||||
const { bits } = await res.json();
|
||||
return typeof bits === "number" && Number.isInteger(bits) && bits > 0 && bits < 256 ? bits : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
async function postMessage(envelope) {
|
||||
const res = await fetch("/api/v1/messages", {
|
||||
method: "POST",
|
||||
@@ -60,12 +73,14 @@ async function postMessage(envelope) {
|
||||
});
|
||||
if (res.ok) return;
|
||||
let detail = res.statusText;
|
||||
let requiredBits;
|
||||
try {
|
||||
const body = await res.json();
|
||||
if (typeof body.error === "string") detail = body.error;
|
||||
if (typeof body.required_bits === "number" && Number.isInteger(body.required_bits)) requiredBits = body.required_bits;
|
||||
} catch {
|
||||
}
|
||||
throw new HttpError(res.status, detail);
|
||||
throw new HttpError(res.status, detail, requiredBits);
|
||||
}
|
||||
async function pollMailbox(tag, since) {
|
||||
const data = await getJSON(`/api/v1/messages?mailbox=${encodeURIComponent(tag)}&since=${since}`);
|
||||
@@ -173,6 +188,7 @@ var de = {
|
||||
"problem.link": "Die erste Nachricht darf keine Links oder Webadressen enthalten (auch kein \u201EHallo.Ich\u201C ohne Leerzeichen nach dem Punkt).",
|
||||
"send.404": "Dieser Knoten kennt die Anzeige nicht (mehr), oder der Anbieter hat sein Ger\xE4t 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\xE4ter noch einmal.",
|
||||
"send.other": "Senden fehlgeschlagen: {detail}",
|
||||
"send.network": "Der Knoten ist nicht erreichbar.",
|
||||
"chat.back": "\u2190 Zur Anzeige",
|
||||
@@ -430,6 +446,7 @@ var en = {
|
||||
"problem.link": 'The first message must not contain links or web addresses (that includes "Hello.I" without a space after the full stop).',
|
||||
"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": "\u2190 Back to the listing",
|
||||
@@ -4439,6 +4456,7 @@ function describe(e2) {
|
||||
if (e2 instanceof InputError) return t(`problem.${e2.problem}`);
|
||||
if (e2 instanceof HttpError) {
|
||||
if (e2.status === 404) return t("send.404");
|
||||
if (e2.status === 429 && e2.requiredBits) return t("send.busy");
|
||||
if (e2.status === 429) return t("send.429");
|
||||
return t("send.other", { detail: e2.detail });
|
||||
}
|
||||
@@ -4493,19 +4511,31 @@ function composer(opts) {
|
||||
);
|
||||
return form;
|
||||
}
|
||||
var MAX_WORK_BITS = 24;
|
||||
async function sendTo(c, listing, text, first) {
|
||||
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);
|
||||
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 (e2) {
|
||||
const asked = e2 instanceof HttpError && e2.status === 429 ? e2.requiredBits : void 0;
|
||||
if (asked === void 0 || asked <= bits || attempt >= 2) throw e2;
|
||||
bits = asked;
|
||||
}
|
||||
}
|
||||
c.messages.push({ dir: "out", n, sent_at: now, t: "text", text });
|
||||
c.updated = now;
|
||||
saveConversation(c);
|
||||
|
||||
Reference in New Issue
Block a user