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
199 lines
5.2 KiB
Go
199 lines
5.2 KiB
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
)
|
|
|
|
type guardFixture struct {
|
|
g *peerGuard
|
|
now time.Time
|
|
verdict pubsub.ValidationResult
|
|
ran int
|
|
dropped []peer.ID
|
|
v pubsub.ValidatorEx
|
|
}
|
|
|
|
func newGuardFixture(t *testing.T) *guardFixture {
|
|
t.Helper()
|
|
f := &guardFixture{now: time.Unix(1_800_000_000, 0), verdict: pubsub.ValidationAccept}
|
|
g, err := newPeerGuard("self", func() time.Time { return f.now },
|
|
slog.New(slog.NewTextHandler(io.Discard, nil)), func(p peer.ID) { f.dropped = append(f.dropped, p) })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.g = g
|
|
f.v = g.wrap(func(context.Context, peer.ID, *pubsub.Message) pubsub.ValidationResult {
|
|
f.ran++
|
|
return f.verdict
|
|
})
|
|
return f
|
|
}
|
|
|
|
func (f *guardFixture) send(from peer.ID) pubsub.ValidationResult {
|
|
return f.v(context.Background(), from, &pubsub.Message{})
|
|
}
|
|
|
|
func TestAPeerThatSendsInvalidMessagesIsBannedAndDisconnected(t *testing.T) {
|
|
f := newGuardFixture(t)
|
|
f.verdict = pubsub.ValidationReject
|
|
for i := 0; i < maxBadPerWindow-1; i++ {
|
|
f.send("mallory")
|
|
}
|
|
if f.g.banned("mallory") || len(f.dropped) != 0 {
|
|
t.Fatal("banned before the limit")
|
|
}
|
|
f.send("mallory")
|
|
if !f.g.banned("mallory") {
|
|
t.Fatalf("not banned after %d invalid messages", maxBadPerWindow)
|
|
}
|
|
if len(f.dropped) != 1 || f.dropped[0] != "mallory" {
|
|
t.Fatalf("disconnected %v, want mallory once", f.dropped)
|
|
}
|
|
f.send("mallory") // more invalid ones must not disconnect again and again
|
|
if len(f.dropped) != 1 {
|
|
t.Fatalf("disconnected %d times", len(f.dropped))
|
|
}
|
|
if f.g.banned("alice") {
|
|
t.Fatal("an innocent peer is banned")
|
|
}
|
|
}
|
|
|
|
func TestInvalidMessagesSpreadOverTimeDoNotBan(t *testing.T) {
|
|
f := newGuardFixture(t)
|
|
f.verdict = pubsub.ValidationReject
|
|
for i := 0; i < 5*maxBadPerWindow; i++ {
|
|
f.send("slow")
|
|
f.now = f.now.Add(guardWindow / (maxBadPerWindow / 2)) // half the limit per window
|
|
}
|
|
if f.g.banned("slow") {
|
|
t.Fatal("banned for a rate below the limit")
|
|
}
|
|
}
|
|
|
|
func TestIgnoredMessagesAreNotHeldAgainstThePeer(t *testing.T) {
|
|
f := newGuardFixture(t)
|
|
f.verdict = pubsub.ValidationIgnore // clock trouble, unknown listing
|
|
for i := 0; i < 10*maxBadPerWindow; i++ {
|
|
f.send("honest")
|
|
}
|
|
if f.g.banned("honest") {
|
|
t.Fatal("ignored messages must not count as bad")
|
|
}
|
|
}
|
|
|
|
func TestAFloodIsDroppedUnreadAndTheNextMinuteStartsFresh(t *testing.T) {
|
|
f := newGuardFixture(t)
|
|
for i := 0; i < maxPerWindow; i++ {
|
|
if res := f.send("busy"); res != pubsub.ValidationAccept {
|
|
t.Fatalf("message %d within the limit got %v", i, res)
|
|
}
|
|
}
|
|
ran := f.ran
|
|
if res := f.send("busy"); res != pubsub.ValidationIgnore {
|
|
t.Fatalf("over the limit got %v, want Ignore", res)
|
|
}
|
|
if f.ran != ran {
|
|
t.Fatal("a message over the limit was still validated")
|
|
}
|
|
if f.g.banned("busy") {
|
|
t.Fatal("a flood of valid messages is dropped, not banned")
|
|
}
|
|
if res := f.send("calm"); res != pubsub.ValidationAccept {
|
|
t.Fatal("one peer's flood must not limit another")
|
|
}
|
|
f.now = f.now.Add(guardWindow)
|
|
if res := f.send("busy"); res != pubsub.ValidationAccept {
|
|
t.Fatalf("after the window the peer got %v", res)
|
|
}
|
|
}
|
|
|
|
func TestOurOwnMessagesAreNeverLimited(t *testing.T) {
|
|
f := newGuardFixture(t)
|
|
f.verdict = pubsub.ValidationReject
|
|
for i := 0; i < 3*maxPerWindow; i++ {
|
|
if f.send("self") == pubsub.ValidationIgnore {
|
|
t.Fatal("limited our own publish")
|
|
}
|
|
}
|
|
if f.g.banned("self") {
|
|
t.Fatal("banned ourselves")
|
|
}
|
|
}
|
|
|
|
func TestAPeerMayOnlyHoldAFewSyncStreamsAtOnce(t *testing.T) {
|
|
f := newGuardFixture(t)
|
|
var ends []func()
|
|
for i := 0; i < maxSyncStreams; i++ {
|
|
end, ok := f.g.beginSync("greedy")
|
|
if !ok {
|
|
t.Fatalf("stream %d refused within the limit", i)
|
|
}
|
|
ends = append(ends, end)
|
|
}
|
|
if _, ok := f.g.beginSync("greedy"); ok {
|
|
t.Fatal("a stream over the limit was accepted")
|
|
}
|
|
if _, ok := f.g.beginSync("other"); !ok {
|
|
t.Fatal("one peer's streams must not use up another's")
|
|
}
|
|
ends[0]()
|
|
end, ok := f.g.beginSync("greedy")
|
|
if !ok {
|
|
t.Fatal("a finished stream must free its place")
|
|
}
|
|
end()
|
|
for _, e := range ends[1:] {
|
|
e()
|
|
}
|
|
if len(f.g.streams) != 1 { // only "other" is still open
|
|
t.Fatalf("%d peers still have streams recorded", len(f.g.streams))
|
|
}
|
|
}
|
|
|
|
func TestSyncRequestsAreRatedAndBannedPeersAreRefused(t *testing.T) {
|
|
f := newGuardFixture(t)
|
|
for i := 0; i < maxSyncPerWindow; i++ {
|
|
end, ok := f.g.beginSync("busy")
|
|
if !ok {
|
|
t.Fatalf("request %d within the rate refused", i)
|
|
}
|
|
end()
|
|
}
|
|
if _, ok := f.g.beginSync("busy"); ok {
|
|
t.Fatal("a request over the rate was accepted")
|
|
}
|
|
f.now = f.now.Add(guardWindow)
|
|
if end, ok := f.g.beginSync("busy"); !ok {
|
|
t.Fatal("the next minute must start fresh")
|
|
} else {
|
|
end()
|
|
}
|
|
|
|
f.verdict = pubsub.ValidationReject
|
|
for i := 0; i < maxBadPerWindow; i++ {
|
|
f.send("mallory")
|
|
}
|
|
if _, ok := f.g.beginSync("mallory"); ok {
|
|
t.Fatal("a banned peer got a sync stream")
|
|
}
|
|
}
|
|
|
|
func TestFinishedWindowsAreForgotten(t *testing.T) {
|
|
f := newGuardFixture(t)
|
|
for i := 0; i < 5000; i++ {
|
|
f.send(peer.ID("p" + string(rune('a'+i%26)) + time.Duration(i).String()))
|
|
}
|
|
f.now = f.now.Add(2 * guardWindow)
|
|
f.send("fresh")
|
|
if n := len(f.g.peers); n > 10 {
|
|
t.Fatalf("%d peers still tracked after their windows ended", n)
|
|
}
|
|
}
|