discovery: pass banThreshold to banman

So we can configure it via gossip's config in a following commit.
This commit is contained in:
yyforyongyu
2025-07-31 10:18:59 +08:00
parent 343418cfdf
commit 3109a8f3fe
3 changed files with 17 additions and 12 deletions

View File

@@ -13,15 +13,14 @@ import (
) )
const ( const (
// DefaultBanThreshold is the default value to be used for banThreshold.
DefaultBanThreshold = 100
// maxBannedPeers limits the maximum number of banned pubkeys that // maxBannedPeers limits the maximum number of banned pubkeys that
// we'll store. // we'll store.
// TODO(eugene): tune. // TODO(eugene): tune.
maxBannedPeers = 10_000 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 // banTime is the amount of time that the non-channel peer will be
// banned for. Channel announcements from channel peers will be dropped // banned for. Channel announcements from channel peers will be dropped
// if it's not one of our channels. // 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. // 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 return c.score >= banThreshold
} }
@@ -144,15 +143,19 @@ type banman struct {
wg sync.WaitGroup wg sync.WaitGroup
quit chan struct{} 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. // newBanman creates a new banman with the default maxBannedPeers.
func newBanman() *banman { func newBanman(banThreshold uint64) *banman {
return &banman{ return &banman{
peerBanIndex: lru.NewCache[[33]byte, *cachedBanInfo]( peerBanIndex: lru.NewCache[[33]byte, *cachedBanInfo](
maxBannedPeers, maxBannedPeers,
), ),
quit: make(chan struct{}), quit: make(chan struct{}),
banThreshold: banThreshold,
} }
} }
@@ -193,7 +196,7 @@ func (b *banman) purgeBanEntries() {
keysToRemove := make([][33]byte, 0) keysToRemove := make([][33]byte, 0)
sweepEntries := func(pubkey [33]byte, banInfo *cachedBanInfo) bool { 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 // If the peer is banned, check if the ban timer has
// expired. // expired.
if banInfo.lastUpdate.Add(banTime).Before(time.Now()) { if banInfo.lastUpdate.Add(banTime).Before(time.Now()) {
@@ -227,7 +230,7 @@ func (b *banman) isBanned(pubkey [33]byte) bool {
return false return false
default: default:
return banInfo.isBanned() return banInfo.isBanned(b.banThreshold)
} }
} }

View File

@@ -12,12 +12,14 @@ import (
func TestPurgeBanEntries(t *testing.T) { func TestPurgeBanEntries(t *testing.T) {
t.Parallel() t.Parallel()
b := newBanman() testBanThreshold := uint64(10)
b := newBanman(testBanThreshold)
// Ban a peer by repeatedly incrementing its ban score. // Ban a peer by repeatedly incrementing its ban score.
peer1 := [33]byte{0x00} peer1 := [33]byte{0x00}
for i := 0; i < banThreshold; i++ { for range testBanThreshold {
b.incrementBanScore(peer1) b.incrementBanScore(peer1)
} }

View File

@@ -586,7 +586,7 @@ func New(cfg Config, selfKeyDesc *keychain.KeyDescriptor) *AuthenticatedGossiper
maxRejectedUpdates, maxRejectedUpdates,
), ),
chanUpdateRateLimiter: make(map[uint64][2]*rate.Limiter), chanUpdateRateLimiter: make(map[uint64][2]*rate.Limiter),
banman: newBanman(), banman: newBanman(DefaultBanThreshold),
} }
gossiper.vb = NewValidationBarrier(1000, gossiper.quit) gossiper.vb = NewValidationBarrier(1000, gossiper.quit)