discovery: revamp recent rejects map

Similar to the prior commit, in this commit, we move to using a basic
LRU cache to store the set of prior rejected messages.
This commit is contained in:
Olaoluwa Osuntokun 2021-11-03 18:58:03 -07:00
parent 8627b5d128
commit 5a8255550b
No known key found for this signature in database
GPG Key ID: 3BBD59E99B280306

View File

@ -44,6 +44,11 @@ const (
// maxPrematureUpdates tracks the max amount of premature channel // maxPrematureUpdates tracks the max amount of premature channel
// updates that we'll hold onto. // updates that we'll hold onto.
maxPrematureUpdates = 100 maxPrematureUpdates = 100
// maxRejectedUpdates tracks the max amount of rejected channel updates
// we'll maintain. This is the global size across all peers. We'll
// allocate ~3 MB max to the cache.
maxRejectedUpdates = 10_000
) )
var ( var (
@ -287,6 +292,33 @@ func (c *cachedNetworkMsg) Size() (uint64, error) {
return uint64(len(c.msgs)), nil return uint64(len(c.msgs)), nil
} }
// rejectCacheKey is the cache key that we'll use to track announcements we've
// recently rejected.
type rejectCacheKey struct {
pubkey [33]byte
chanID uint64
}
// newRejectCacheKey returns a new cache key for the reject cache.
func newRejectCacheKey(cid uint64, pub [33]byte) rejectCacheKey {
k := rejectCacheKey{
chanID: cid,
pubkey: pub,
}
return k
}
// cachedReject is the empty value used to track the value for rejects.
type cachedReject struct {
}
// Size returns the "size" of an entry. We return 1 as we just want to limit
// the total size.
func (c *cachedReject) Size() (uint64, error) {
return 1, nil
}
// AuthenticatedGossiper is a subsystem which is responsible for receiving // AuthenticatedGossiper is a subsystem which is responsible for receiving
// announcements, validating them and applying the changes to router, syncing // announcements, validating them and applying the changes to router, syncing
// lightning network with newly connected nodes, broadcasting announcements // lightning network with newly connected nodes, broadcasting announcements
@ -345,8 +377,7 @@ type AuthenticatedGossiper struct {
// consistent between when the DB is first read until it's written. // consistent between when the DB is first read until it's written.
channelMtx *multimutex.Mutex channelMtx *multimutex.Mutex
rejectMtx sync.RWMutex recentRejects *lru.Cache
recentRejects map[uint64]struct{}
// syncMgr is a subsystem responsible for managing the gossip syncers // syncMgr is a subsystem responsible for managing the gossip syncers
// for peers currently connected. When a new peer is connected, the // for peers currently connected. When a new peer is connected, the
@ -388,7 +419,7 @@ func New(cfg Config, selfKeyDesc *keychain.KeyDescriptor) *AuthenticatedGossiper
chanPolicyUpdates: make(chan *chanPolicyUpdateRequest), chanPolicyUpdates: make(chan *chanPolicyUpdateRequest),
prematureChannelUpdates: lru.NewCache(maxPrematureUpdates), prematureChannelUpdates: lru.NewCache(maxPrematureUpdates),
channelMtx: multimutex.NewMutex(), channelMtx: multimutex.NewMutex(),
recentRejects: make(map[uint64]struct{}), recentRejects: lru.NewCache(maxRejectedUpdates),
chanUpdateRateLimiter: make(map[uint64][2]*rate.Limiter), chanUpdateRateLimiter: make(map[uint64][2]*rate.Limiter),
} }
@ -1036,7 +1067,9 @@ func (d *AuthenticatedGossiper) networkHandler() {
// If this message was recently rejected, then we won't // If this message was recently rejected, then we won't
// attempt to re-process it. // attempt to re-process it.
if d.isRecentlyRejectedMsg(announcement.msg) { if announcement.isRemote && d.isRecentlyRejectedMsg(
announcement.msg, announcement.peer.PubKey(),
) {
announcement.err <- fmt.Errorf("recently " + announcement.err <- fmt.Errorf("recently " +
"rejected") "rejected")
continue continue
@ -1202,22 +1235,23 @@ func (d *AuthenticatedGossiper) PruneSyncState(peer route.Vertex) {
// isRecentlyRejectedMsg returns true if we recently rejected a message, and // isRecentlyRejectedMsg returns true if we recently rejected a message, and
// false otherwise, This avoids expensive reprocessing of the message. // false otherwise, This avoids expensive reprocessing of the message.
func (d *AuthenticatedGossiper) isRecentlyRejectedMsg(msg lnwire.Message) bool { func (d *AuthenticatedGossiper) isRecentlyRejectedMsg(msg lnwire.Message,
d.rejectMtx.RLock() peerPub [33]byte) bool {
defer d.rejectMtx.RUnlock()
var scid uint64
switch m := msg.(type) { switch m := msg.(type) {
case *lnwire.ChannelUpdate: case *lnwire.ChannelUpdate:
_, ok := d.recentRejects[m.ShortChannelID.ToUint64()] scid = m.ShortChannelID.ToUint64()
return ok
case *lnwire.ChannelAnnouncement: case *lnwire.ChannelAnnouncement:
_, ok := d.recentRejects[m.ShortChannelID.ToUint64()] scid = m.ShortChannelID.ToUint64()
return ok
default: default:
return false return false
} }
_, err := d.recentRejects.Get(newRejectCacheKey(scid, peerPub))
return err != cache.ErrElementNotFound
} }
// retransmitStaleAnns examines all outgoing channels that the source node is // retransmitStaleAnns examines all outgoing channels that the source node is
@ -1640,9 +1674,11 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
d.cfg.ChainHash) d.cfg.ChainHash)
log.Errorf(err.Error()) log.Errorf(err.Error())
d.rejectMtx.Lock() key := newRejectCacheKey(
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{} msg.ShortChannelID.ToUint64(),
d.rejectMtx.Unlock() nMsg.peer.PubKey(),
)
_, _ = d.recentRejects.Put(key, &cachedReject{})
nMsg.err <- err nMsg.err <- err
return nil, false return nil, false
@ -1680,9 +1716,12 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
if err := routing.ValidateChannelAnn(msg); err != nil { if err := routing.ValidateChannelAnn(msg); err != nil {
err := fmt.Errorf("unable to validate "+ err := fmt.Errorf("unable to validate "+
"announcement: %v", err) "announcement: %v", err)
d.rejectMtx.Lock()
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{} key := newRejectCacheKey(
d.rejectMtx.Unlock() msg.ShortChannelID.ToUint64(),
nMsg.peer.PubKey(),
)
_, _ = d.recentRejects.Put(key, &cachedReject{})
log.Error(err) log.Error(err)
nMsg.err <- err nMsg.err <- err
@ -1753,9 +1792,13 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
// see if we get any new announcements. // see if we get any new announcements.
anns, rErr := d.processRejectedEdge(msg, proof) anns, rErr := d.processRejectedEdge(msg, proof)
if rErr != nil { if rErr != nil {
d.rejectMtx.Lock()
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{} key := newRejectCacheKey(
d.rejectMtx.Unlock() msg.ShortChannelID.ToUint64(),
nMsg.peer.PubKey(),
)
_, _ = d.recentRejects.Put(key, &cachedReject{})
nMsg.err <- rErr nMsg.err <- rErr
return nil, false return nil, false
} }
@ -1777,9 +1820,11 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
log.Tracef("Router rejected channel "+ log.Tracef("Router rejected channel "+
"edge: %v", err) "edge: %v", err)
d.rejectMtx.Lock() key := newRejectCacheKey(
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{} msg.ShortChannelID.ToUint64(),
d.rejectMtx.Unlock() nMsg.peer.PubKey(),
)
_, _ = d.recentRejects.Put(key, &cachedReject{})
} }
nMsg.err <- err nMsg.err <- err
@ -1862,9 +1907,11 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
d.cfg.ChainHash) d.cfg.ChainHash)
log.Errorf(err.Error()) log.Errorf(err.Error())
d.rejectMtx.Lock() key := newRejectCacheKey(
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{} msg.ShortChannelID.ToUint64(),
d.rejectMtx.Unlock() nMsg.peer.PubKey(),
)
_, _ = d.recentRejects.Put(key, &cachedReject{})
nMsg.err <- err nMsg.err <- err
return nil, false return nil, false
@ -1952,21 +1999,23 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
shortChanID, shortChanID,
) )
switch { switch {
// Nothing in the cache yeyt, we can just directly // Nothing in the cache yet, we can just directly
// insert this element. // insert this element.
case err == cache.ErrElementNotFound: case err == cache.ErrElementNotFound:
d.prematureChannelUpdates.Put(shortChanID, &cachedNetworkMsg{ _, _ = d.prematureChannelUpdates.Put(
msgs: []*networkMsg{nMsg}, shortChanID, &cachedNetworkMsg{
}) msgs: []*networkMsg{nMsg},
})
// There's already something in the cache, so we'll // There's already something in the cache, so we'll
// combine the set of messagesa into a single value. // combine the set of messages into a single value.
default: default:
msgs := earlyMsgs.(*cachedNetworkMsg).msgs msgs := earlyMsgs.(*cachedNetworkMsg).msgs
msgs = append(msgs, nMsg) msgs = append(msgs, nMsg)
d.prematureChannelUpdates.Put(shortChanID, &cachedNetworkMsg{ _, _ = d.prematureChannelUpdates.Put(
msgs: msgs, shortChanID, &cachedNetworkMsg{
}) msgs: msgs,
})
} }
log.Debugf("Got ChannelUpdate for edge not found in "+ log.Debugf("Got ChannelUpdate for edge not found in "+
@ -1984,9 +2033,12 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
log.Error(err) log.Error(err)
nMsg.err <- err nMsg.err <- err
d.rejectMtx.Lock() key := newRejectCacheKey(
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{} msg.ShortChannelID.ToUint64(),
d.rejectMtx.Unlock() nMsg.peer.PubKey(),
)
_, _ = d.recentRejects.Put(key, &cachedReject{})
return nil, false return nil, false
} }
@ -2089,9 +2141,13 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement(
routing.ErrIgnored) { routing.ErrIgnored) {
log.Debug(err) log.Debug(err)
} else if err != routing.ErrVBarrierShuttingDown { } else if err != routing.ErrVBarrierShuttingDown {
d.rejectMtx.Lock()
d.recentRejects[msg.ShortChannelID.ToUint64()] = struct{}{} key := newRejectCacheKey(
d.rejectMtx.Unlock() msg.ShortChannelID.ToUint64(),
nMsg.peer.PubKey(),
)
_, _ = d.recentRejects.Put(key, &cachedReject{})
log.Error(err) log.Error(err)
} }