From 3109a8f3feed800f52d9c6d1fcdedfcd6986448a Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 31 Jul 2025 10:18:59 +0800 Subject: [PATCH] discovery: pass `banThreshold` to `banman` So we can configure it via gossip's config in a following commit. --- discovery/ban.go | 21 ++++++++++++--------- discovery/ban_test.go | 6 ++++-- discovery/gossiper.go | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/discovery/ban.go b/discovery/ban.go index cd70d7c38..c13a21b72 100644 --- a/discovery/ban.go +++ b/discovery/ban.go @@ -13,15 +13,14 @@ import ( ) const ( + // DefaultBanThreshold is the default value to be used for banThreshold. + DefaultBanThreshold = 100 + // maxBannedPeers limits the maximum number of banned pubkeys that // we'll store. // TODO(eugene): tune. maxBannedPeers = 10_000 - // banThreshold is the point at which non-channel peers will be banned. - // TODO(eugene): tune. - banThreshold = 100 - // banTime is the amount of time that the non-channel peer will be // banned for. Channel announcements from channel peers will be dropped // if it's not one of our channels. @@ -126,7 +125,7 @@ func (c *cachedBanInfo) Size() (uint64, error) { } // isBanned returns true if the ban score is greater than the ban threshold. -func (c *cachedBanInfo) isBanned() bool { +func (c *cachedBanInfo) isBanned(banThreshold uint64) bool { return c.score >= banThreshold } @@ -144,15 +143,19 @@ type banman struct { wg sync.WaitGroup quit chan struct{} + + // banThreshold is the point at which non-channel peers will be banned. + banThreshold uint64 } // newBanman creates a new banman with the default maxBannedPeers. -func newBanman() *banman { +func newBanman(banThreshold uint64) *banman { return &banman{ peerBanIndex: lru.NewCache[[33]byte, *cachedBanInfo]( maxBannedPeers, ), - quit: make(chan struct{}), + quit: make(chan struct{}), + banThreshold: banThreshold, } } @@ -193,7 +196,7 @@ func (b *banman) purgeBanEntries() { keysToRemove := make([][33]byte, 0) sweepEntries := func(pubkey [33]byte, banInfo *cachedBanInfo) bool { - if banInfo.isBanned() { + if banInfo.isBanned(b.banThreshold) { // If the peer is banned, check if the ban timer has // expired. if banInfo.lastUpdate.Add(banTime).Before(time.Now()) { @@ -227,7 +230,7 @@ func (b *banman) isBanned(pubkey [33]byte) bool { return false default: - return banInfo.isBanned() + return banInfo.isBanned(b.banThreshold) } } diff --git a/discovery/ban_test.go b/discovery/ban_test.go index e4149028b..72320ecb0 100644 --- a/discovery/ban_test.go +++ b/discovery/ban_test.go @@ -12,12 +12,14 @@ import ( func TestPurgeBanEntries(t *testing.T) { t.Parallel() - b := newBanman() + testBanThreshold := uint64(10) + + b := newBanman(testBanThreshold) // Ban a peer by repeatedly incrementing its ban score. peer1 := [33]byte{0x00} - for i := 0; i < banThreshold; i++ { + for range testBanThreshold { b.incrementBanScore(peer1) } diff --git a/discovery/gossiper.go b/discovery/gossiper.go index 13a0c737b..34cd2b75e 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -586,7 +586,7 @@ func New(cfg Config, selfKeyDesc *keychain.KeyDescriptor) *AuthenticatedGossiper maxRejectedUpdates, ), chanUpdateRateLimiter: make(map[uint64][2]*rate.Limiter), - banman: newBanman(), + banman: newBanman(DefaultBanThreshold), } gossiper.vb = NewValidationBarrier(1000, gossiper.quit)