limits: sync streams, listings per author, inbound connections per IP
A peer may hold 4 sync streams and open 1500 a minute; banned peers get none and cannot redial. A node keeps at most 100 unexpired listings per author (--max-per-author); updates, renewals and deletions of held ones still work. At most 16 inbound connections per public IP or IPv6 /64 (--max-conns-per-ip); local and private addresses are exempt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
)
|
||||
|
||||
func newQuotaEnv(t *testing.T, max int) *env {
|
||||
t.Helper()
|
||||
e := &env{now: t0}
|
||||
e.root, _, _ = identity.NewRoot()
|
||||
e.dev = newDevice(t, e.root)
|
||||
s, err := Open(":memory:", Options{Difficulty: difficulty, MaxPerAuthor: max, Clock: func() time.Time { return e.now }})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { s.Close() })
|
||||
e.s = s
|
||||
return e
|
||||
}
|
||||
|
||||
func TestAnAuthorCannotHoldMoreNewListingsThanTheQuota(t *testing.T) {
|
||||
e := newQuotaEnv(t, 3)
|
||||
var held []*listing.Listing
|
||||
for i := 0; i < 3; i++ {
|
||||
l := e.listing(t, nil)
|
||||
e.put(t, l)
|
||||
held = append(held, l)
|
||||
}
|
||||
if _, err := e.s.Put(ctx, e.listing(t, nil)); !errors.Is(err, ErrQuota) {
|
||||
t.Fatalf("a fourth listing: err = %v, want ErrQuota", err)
|
||||
}
|
||||
// Another author is unaffected.
|
||||
other := e.sibling(t)
|
||||
other.root, _, _ = identity.NewRoot()
|
||||
other.dev = newDevice(t, other.root)
|
||||
other.s.maxPerAuthor = 3
|
||||
other.put(t, other.listing(t, nil))
|
||||
|
||||
// What the node already holds can still be changed, renewed and deleted.
|
||||
upd := e.listing(t, func(l *listing.Listing) { l.ID, l.Seq, l.Title = held[0].ID, 2, "Neu" })
|
||||
if o := e.put(t, upd); o != Replaced {
|
||||
t.Fatalf("update at the quota: %v, want replaced", o)
|
||||
}
|
||||
if o := e.put(t, e.tombstone(t, held[1])); o != Replaced {
|
||||
t.Fatalf("deleting at the quota: %v, want replaced", o)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpiredListingsDoNotCountAgainstTheQuota(t *testing.T) {
|
||||
e := newQuotaEnv(t, 2)
|
||||
e.put(t, e.listing(t, nil))
|
||||
e.put(t, e.listing(t, nil))
|
||||
if _, err := e.s.Put(ctx, e.listing(t, nil)); !errors.Is(err, ErrQuota) {
|
||||
t.Fatalf("err = %v, want ErrQuota", err)
|
||||
}
|
||||
e.now = e.now.Add(8 * 24 * time.Hour) // both have expired
|
||||
if _, err := e.s.Put(ctx, e.listing(t, nil)); err != nil {
|
||||
t.Fatalf("after the old ones expired: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheDefaultQuotaIsHundredAndNegativeMeansNone(t *testing.T) {
|
||||
if e := newEnv(t); e.s.maxPerAuthor != DefaultMaxPerAuthor || DefaultMaxPerAuthor != 100 {
|
||||
t.Fatalf("default quota %d", e.s.maxPerAuthor)
|
||||
}
|
||||
if e := newQuotaEnv(t, -1); e.s.maxPerAuthor != 0 {
|
||||
t.Fatal("a negative quota must switch the cap off")
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,10 @@ var (
|
||||
ErrNotFound = errors.New("store: not found")
|
||||
// ErrRevoked is returned for listings signed by a revoked device.
|
||||
ErrRevoked = errors.New("store: device revoked")
|
||||
// ErrQuota is returned for a new listing from an author who already has
|
||||
// the most this node keeps (Options.MaxPerAuthor). Updates, renewals and
|
||||
// deletions of listings the node holds are never refused for this.
|
||||
ErrQuota = errors.New("store: this author has too many listings on this node")
|
||||
)
|
||||
|
||||
// Outcome tells the caller what Put did, so the p2p layer knows whether a
|
||||
@@ -57,8 +61,19 @@ type Options struct {
|
||||
|
||||
// Trust says whose abuse reports hide listings on this node (see Trust).
|
||||
Trust Trust
|
||||
|
||||
// MaxPerAuthor caps the unexpired listings (tombstones included) this node
|
||||
// keeps for one author; 0 means DefaultMaxPerAuthor, negative no cap. A
|
||||
// listing past the cap is not stored and so not passed on. Each node
|
||||
// decides for itself, so nodes with different caps hold different sets.
|
||||
MaxPerAuthor int
|
||||
}
|
||||
|
||||
// DefaultMaxPerAuthor is generous for a person or a small agency and makes one
|
||||
// identity a poor way to fill a node: more takes more identities, and each
|
||||
// listing of each costs its own proof-of-work.
|
||||
const DefaultMaxPerAuthor = 100
|
||||
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
difficulty int
|
||||
@@ -68,6 +83,7 @@ type Store struct {
|
||||
ownerQueue int
|
||||
mailboxQueue int
|
||||
trust Trust
|
||||
maxPerAuthor int // 0: no cap
|
||||
}
|
||||
|
||||
const schema = `
|
||||
@@ -217,6 +233,12 @@ func Open(path string, opt Options) (*Store, error) {
|
||||
msgDifficulty: opt.MessageDifficulty, maxMessages: opt.MaxMessages,
|
||||
ownerQueue: opt.OwnerQueue, mailboxQueue: opt.MailboxQueue}
|
||||
s.trust = opt.Trust
|
||||
switch {
|
||||
case opt.MaxPerAuthor == 0:
|
||||
s.maxPerAuthor = DefaultMaxPerAuthor
|
||||
case opt.MaxPerAuthor > 0:
|
||||
s.maxPerAuthor = opt.MaxPerAuthor
|
||||
}
|
||||
if s.trust.Threshold <= 0 {
|
||||
s.trust.Threshold = DefaultHideThreshold
|
||||
}
|
||||
@@ -284,6 +306,18 @@ func (s *Store) Put(ctx context.Context, l *listing.Listing) (Outcome, error) {
|
||||
outcome := Inserted
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
if s.maxPerAuthor > 0 {
|
||||
var held int
|
||||
err = tx.QueryRowContext(ctx,
|
||||
`SELECT COUNT(*) FROM listings WHERE author = ? AND expires_at > ?`,
|
||||
l.Author, s.clock().Unix()).Scan(&held)
|
||||
if err != nil {
|
||||
return Ignored, err
|
||||
}
|
||||
if held >= s.maxPerAuthor {
|
||||
return Ignored, ErrQuota
|
||||
}
|
||||
}
|
||||
case err != nil:
|
||||
return Ignored, err
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user