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:
@@ -31,6 +31,51 @@ var (
|
||||
ErrQueueFull = errors.New("store: message queue full")
|
||||
)
|
||||
|
||||
// WorkError means an owner-bound envelope did not carry enough proof-of-work
|
||||
// for the queue it is going into right now. It is not misbehaviour: the price
|
||||
// rises as a listing's queue fills, so the sender just has to mine more.
|
||||
// errors.Is(err, ErrQueueFull) is true for it.
|
||||
type WorkError struct{ Required int }
|
||||
|
||||
func (e *WorkError) Error() string {
|
||||
return fmt.Sprintf("store: this listing's queue needs %d bits of proof-of-work right now", e.Required)
|
||||
}
|
||||
func (e *WorkError) Is(target error) bool { return target == ErrQueueFull }
|
||||
|
||||
// RequiredWork is the proof-of-work, in bits, an envelope to the owner of ref
|
||||
// needs right now. It is the node's message difficulty while the queue is
|
||||
// quiet and rises with it: one bit more from half full, two from three
|
||||
// quarters. Once the queue is full it is one bit more than the weakest
|
||||
// envelope waiting, since a newcomer must beat what it would displace.
|
||||
// Flooding a listing therefore costs more with every envelope, instead of
|
||||
// locking honest senders out at a price they cannot see.
|
||||
func (s *Store) RequiredWork(ctx context.Context, ref message.Ref) (int, error) {
|
||||
return s.requiredWork(ctx, s.db, ref)
|
||||
}
|
||||
|
||||
func (s *Store) requiredWork(ctx context.Context, q rowQuerier, ref message.Ref) (int, error) {
|
||||
var n, least int
|
||||
err := q.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*), COALESCE(MIN(work), 0) FROM messages
|
||||
WHERE to_dir = 'owner' AND author = ? AND listing_id = ? AND expires_at > ?`,
|
||||
ref.Author, ref.ID, s.clock().Unix()).Scan(&n, &least)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
need := s.msgDifficulty
|
||||
switch {
|
||||
case n >= s.ownerQueue:
|
||||
if least+1 > need {
|
||||
need = least + 1
|
||||
}
|
||||
case 4*n >= 3*s.ownerQueue:
|
||||
need += 2
|
||||
case 2*n >= s.ownerQueue:
|
||||
need++
|
||||
}
|
||||
return need, nil
|
||||
}
|
||||
|
||||
// PutMessage verifies an envelope as a relay can and stores it in region.
|
||||
// Conflict-free: an envelope's identity is its hash, so duplicates are no-ops.
|
||||
//
|
||||
@@ -153,6 +198,17 @@ func (s *Store) route(ctx context.Context, q rowQuerier, now int64, region strin
|
||||
if e.To == message.ToOwner && (!known || tombstone != 0 || expires <= now || device != e.Rcpt) {
|
||||
return ErrNoListing
|
||||
}
|
||||
if e.To == message.ToOwner {
|
||||
need, err := s.requiredWork(ctx, q, e.Listing)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if work, err := e.Work(); err != nil {
|
||||
return err
|
||||
} else if work < need {
|
||||
return &WorkError{Required: need}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user