From 9cae62dcd7c6ae25e58869b29b949b7610079f2d Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 23 Jul 2025 17:54:25 +0800 Subject: [PATCH] discovery: only send `node_announcement` associated with channels If a node doesn't have any channels, there's little point to send its node_announcement as it cannot be used for routing. --- discovery/chan_series.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/discovery/chan_series.go b/discovery/chan_series.go index 525bf8203..ed7140d7d 100644 --- a/discovery/chan_series.go +++ b/discovery/chan_series.go @@ -120,6 +120,10 @@ func (c *ChanSeries) UpdatesInHorizon(chain chainhash.Hash, if err != nil { return nil, err } + + // nodesFromChan records the nodes seen from the channels. + nodesFromChan := make(map[[33]byte]struct{}, len(chansInHorizon)*2) + for _, channel := range chansInHorizon { // If the channel hasn't been fully advertised yet, or is a // private channel, then we'll skip it as we can't construct a @@ -181,6 +185,10 @@ func (c *ChanSeries) UpdatesInHorizon(chain chainhash.Hash, // Append the all the msgs to the slice. updates = append(updates, chanUpdates...) + + // Record the nodes seen. + nodesFromChan[channel.Info.NodeKey1Bytes] = struct{}{} + nodesFromChan[channel.Info.NodeKey2Bytes] = struct{}{} } // Next, we'll send out all the node announcements that have an update @@ -192,8 +200,15 @@ func (c *ChanSeries) UpdatesInHorizon(chain chainhash.Hash, if err != nil { return nil, err } + for _, nodeAnn := range nodeAnnsInHorizon { - nodeAnn := nodeAnn + // If this node has not been seen in the above channels, we can + // skip sending its NodeAnnouncement. + if _, seen := nodesFromChan[nodeAnn.PubKeyBytes]; !seen { + log.Debugf("Skipping forwarding as node %x not found "+ + "in channel announcement", nodeAnn.PubKeyBytes) + continue + } // Ensure we only forward nodes that are publicly advertised to // prevent leaking information about nodes.