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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user