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
+29
View File
@@ -68,6 +68,15 @@ func (s *server) postMessage(w http.ResponseWriter, r *http.Request) {
case errors.Is(err, store.ErrNoListing):
fail(w, http.StatusNotFound, "that listing is not live here (anymore), or its owner changed device")
case errors.Is(err, store.ErrQueueFull):
var we *store.WorkError
if errors.As(err, &we) {
// The price of this listing's queue has risen: say what it is now.
writeJSON(w, http.StatusTooManyRequests, map[string]any{
"error": "this listing is receiving many messages; it now needs more proof-of-work",
"required_bits": we.Required,
})
return
}
fail(w, http.StatusTooManyRequests, "the queue for this listing is full of messages with more proof-of-work; mine more and retry")
default:
fail(w, http.StatusInternalServerError, "internal error")
@@ -78,6 +87,26 @@ func (s *server) postMessage(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusAccepted, map[string]string{"hash": h})
}
// getRequiredWork tells a buyer's browser how much proof-of-work a message to
// a listing needs right now. The price rises while the listing's queue fills,
// so the browser asks before it mines.
func (s *server) getRequiredWork(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
author, id := q.Get("author"), q.Get("id")
if len(author) != 64 || !isLowerHex(author) || len(id) != 32 || !isLowerHex(id) {
fail(w, http.StatusBadRequest, "author must be 64 and id 32 hex characters")
return
}
bits, err := s.Store.RequiredWork(r.Context(), message.Ref{Author: author, ID: id})
if err != nil {
fail(w, http.StatusInternalServerError, "internal error")
return
}
w.Header().Set("Access-Control-Allow-Origin", "*")
noStore(w)
writeJSON(w, http.StatusOK, map[string]int{"bits": bits})
}
func (s *server) getMailbox(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
tag := q.Get("mailbox")