From f1b2a47717363a546d18cc80e0ccccb6e23e4add Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 22 Jul 2025 15:05:52 +0800 Subject: [PATCH] discovery: add new method `handleBadPeer` So we can use the same piece of code elsewhere. --- discovery/gossiper.go | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/discovery/gossiper.go b/discovery/gossiper.go index 2473eda24..44f36770e 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -2598,7 +2598,6 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(ctx context.Context, if closed { err = fmt.Errorf("ignoring closed channel %v", scid) - log.Error(err) // If this is an announcement from us, we'll just ignore it. if !nMsg.isRemote { @@ -2606,23 +2605,14 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(ctx context.Context, return nil, false } + log.Warnf("Increasing ban score for peer=%v due to outdated "+ + "channel announcement for channel %v", nMsg.peer, scid) + // Increment the peer's ban score if they are sending closed // channel announcements. - d.banman.incrementBanScore(nMsg.peer.PubKey()) - - // If the peer is banned and not a channel peer, we'll - // disconnect them. - shouldDc, dcErr := d.ShouldDisconnect(nMsg.peer.IdentityKey()) + dcErr := d.handleBadPeer(nMsg.peer) if dcErr != nil { - log.Errorf("failed to check if we should disconnect "+ - "peer: %v", dcErr) - nMsg.err <- dcErr - - return nil, false - } - - if shouldDc { - nMsg.peer.Disconnect(ErrPeerBanned) + err = dcErr } nMsg.err <- err @@ -3841,6 +3831,29 @@ func (d *AuthenticatedGossiper) validateFundingTransaction(_ context.Context, nil } +// handleBadPeer takes a misbehaving peer and increases its ban score. Once +// increased, it will disconnect the peer if its ban score has reached +// `banThreshold` and it doesn't have a channel with us. +func (d *AuthenticatedGossiper) handleBadPeer(peer lnpeer.Peer) error { + // Increment the peer's ban score for misbehavior. + d.banman.incrementBanScore(peer.PubKey()) + + // If the peer is banned and not a channel peer, we'll disconnect them. + shouldDc, dcErr := d.ShouldDisconnect(peer.IdentityKey()) + if dcErr != nil { + log.Errorf("failed to check if we should disconnect peer: %v", + dcErr) + + return dcErr + } + + if shouldDc { + peer.Disconnect(ErrPeerBanned) + } + + return nil +} + // makeFundingScript is used to make the funding script for both segwit v0 and // segwit v1 (taproot) channels. func makeFundingScript(bitcoinKey1, bitcoinKey2 []byte,