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
+1
View File
@@ -105,6 +105,7 @@ func New(cfg Config) http.Handler {
// Messaging: public relay endpoints for buyers' browsers...
mux.HandleFunc("POST /api/v1/messages", s.postMessage)
mux.HandleFunc("GET /api/v1/messages", s.getMailbox)
mux.HandleFunc("GET /api/v1/messages/required", s.getRequiredWork)
// ...and the owner's inbox and private address, behind the admin token.
if cfg.Inbox != nil {
mux.HandleFunc("GET /api/v1/mine", s.admin(s.mine))
+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")
+47
View File
@@ -429,6 +429,53 @@ func TestStatusReportsMessageDifficulty(t *testing.T) {
}
}
func TestTheMessagePriceRisesWithTheQueueAndBuyersCanAskForIt(t *testing.T) {
f := newFixture(t, token)
l := f.publish(t)
required := func() int {
code, body := f.do(t, "GET", "/api/v1/messages/required?author="+l.Author+"&id="+l.ID, "", false)
var r struct {
Bits int `json:"bits"`
}
if code != 200 || json.Unmarshal(body, &r) != nil {
t.Fatalf("required: %d %s", code, body)
}
return r.Bits
}
if got := required(); got != msgDifficulty {
t.Fatalf("a quiet listing asks %d bits, want %d", got, msgDifficulty)
}
if code, _ := f.do(t, "GET", "/api/v1/messages/required?author=zz&id=1", "", false); code != 400 {
t.Fatalf("bad parameters: %d", code)
}
// Half of the 200-envelope queue costs one bit more.
for i := 0; i < 100; i++ {
if _, err := f.s.PutMessage(t.Context(), listing.Region(l.Geohash), buyerHello(t, l, newThread(t), "Hallo")); err != nil {
t.Fatal(err)
}
}
if got := required(); got != msgDifficulty+1 {
t.Fatalf("a half-full queue asks %d bits, want %d", got, msgDifficulty+1)
}
// A message at the old price is refused, and the answer names the new one.
var weak *message.Envelope
for {
weak = buyerHello(t, l, newThread(t), "Hallo")
if w, _ := weak.Work(); w == msgDifficulty {
break
}
}
code, body := f.send(t, weak)
var refusal struct {
Required int `json:"required_bits"`
}
if code != 429 || json.Unmarshal(body, &refusal) != nil || refusal.Required != msgDifficulty+1 {
t.Fatalf("a message at the old price: %d %s", code, body)
}
}
func TestBlockingAConversationOverTheAPI(t *testing.T) {
f := newFixture(t, token)
l := f.publish(t)