From 505fa7b5b685b7a07dadb2706dde98bfd04eb4d0 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Mon, 6 Nov 2023 14:30:48 +0200 Subject: [PATCH] multi: use lnwire.ChannelAnnouncement interface where possible --- discovery/gossiper.go | 64 ++++++++++++++++++++----------------- discovery/gossiper_test.go | 2 +- discovery/syncer.go | 13 ++++---- funding/manager.go | 2 +- funding/manager_test.go | 2 +- graph/validation_barrier.go | 25 ++++++++------- peer/brontide.go | 5 +-- 7 files changed, 62 insertions(+), 51 deletions(-) diff --git a/discovery/gossiper.go b/discovery/gossiper.go index bab7a450c..36f1be28c 100644 --- a/discovery/gossiper.go +++ b/discovery/gossiper.go @@ -853,13 +853,16 @@ func (d *AuthenticatedGossiper) ProcessRemoteAnnouncement(msg lnwire.Message, // To avoid inserting edges in the graph for our own channels that we // have already closed, we ignore such channel announcements coming // from the remote. - case *lnwire.ChannelAnnouncement1: + case lnwire.ChannelAnnouncement: ownKey := d.selfKey.SerializeCompressed() - ownErr := fmt.Errorf("ignoring remote ChannelAnnouncement1 " + + ownErr := fmt.Errorf("ignoring remote ChannelAnnouncement " + "for own channel") - if bytes.Equal(m.NodeID1[:], ownKey) || - bytes.Equal(m.NodeID2[:], ownKey) { + node1 := m.Node1KeyBytes() + node2 := m.Node2KeyBytes() + + if bytes.Equal(node1[:], ownKey) || + bytes.Equal(node2[:], ownKey) { log.Warn(ownErr) errChan <- ownErr @@ -1015,8 +1018,8 @@ func (d *deDupedAnnouncements) addMsg(message networkMsg) { switch msg := message.msg.(type) { // Channel announcements are identified by the short channel id field. - case *lnwire.ChannelAnnouncement1: - deDupKey := msg.ShortChannelID + case lnwire.ChannelAnnouncement: + deDupKey := msg.SCID() sender := route.NewVertex(message.source) mws, ok := d.channelAnnouncements[deDupKey] @@ -1597,8 +1600,8 @@ func (d *AuthenticatedGossiper) isRecentlyRejectedMsg(msg lnwire.Message, case *lnwire.ChannelUpdate1: scid = m.ShortChannelID.ToUint64() - case *lnwire.ChannelAnnouncement1: - scid = m.ShortChannelID.ToUint64() + case lnwire.ChannelAnnouncement: + scid = m.SCID().ToUint64() default: return false @@ -1854,14 +1857,14 @@ func remotePubFromChanInfo(chanInfo models.ChannelEdgeInfo, // to receive the remote peer's proof, while the remote peer is able to fully // assemble the proof and craft the ChannelAnnouncement. func (d *AuthenticatedGossiper) processRejectedEdge( - chanAnnMsg *lnwire.ChannelAnnouncement1, + chanAnnMsg lnwire.ChannelAnnouncement, proof models.ChannelAuthProof) ([]networkMsg, error) { + scid := chanAnnMsg.SCID() + // First, we'll fetch the state of the channel as we know if from the // database. - chanInfo, e1, e2, err := d.cfg.Graph.GetChannelByID( - chanAnnMsg.ShortChannelID, - ) + chanInfo, e1, e2, err := d.cfg.Graph.GetChannelByID(scid) if err != nil { return nil, err } @@ -1891,19 +1894,19 @@ func (d *AuthenticatedGossiper) processRejectedEdge( err = chanAnn.Validate(d.fetchPKScript) if err != nil { err := fmt.Errorf("assembled channel announcement proof "+ - "for shortChanID=%v isn't valid: %v", - chanAnnMsg.ShortChannelID, err) + "for shortChanID=%v isn't valid: %v", scid, err) log.Error(err) return nil, err } // If everything checks out, then we'll add the fully assembled proof // to the database. - err = d.cfg.Graph.AddProof(chanAnnMsg.ShortChannelID, proof) + err = d.cfg.Graph.AddProof(scid, proof) if err != nil { err := fmt.Errorf("unable add proof to shortChanID=%v: %w", - chanAnnMsg.ShortChannelID, err) + scid, err) log.Error(err) + return nil, err } @@ -2046,7 +2049,7 @@ func (d *AuthenticatedGossiper) processNetworkAnnouncement( // *creation* of a new channel within the network. This only advertises // the existence of a channel and not yet the routing policies in // either direction of the channel. - case *lnwire.ChannelAnnouncement1: + case lnwire.ChannelAnnouncement: return d.handleChanAnnouncement(nMsg, msg, schedulerOp) // A new authenticated channel edge update has arrived. This indicates @@ -2208,7 +2211,7 @@ func (d *AuthenticatedGossiper) isMsgStale(msg lnwire.Message) bool { // updateChannel creates a new fully signed update for the channel, and updates // the underlying graph with the new state. func (d *AuthenticatedGossiper) updateChannel(edgeInfo models.ChannelEdgeInfo, - edge *models.ChannelEdgePolicy1) (*lnwire.ChannelAnnouncement1, + edge *models.ChannelEdgePolicy1) (lnwire.ChannelAnnouncement, *lnwire.ChannelUpdate1, error) { // Parse the unsigned edge into a channel update. @@ -2252,7 +2255,7 @@ func (d *AuthenticatedGossiper) updateChannel(edgeInfo models.ChannelEdgeInfo, // We'll also create the original channel announcement so the two can // be broadcast along side each other (if necessary), but only if we // have a full channel announcement for this channel. - var chanAnn *lnwire.ChannelAnnouncement1 + var chanAnn lnwire.ChannelAnnouncement if edgeInfo.GetAuthProof() != nil { switch info := edgeInfo.(type) { case *models.ChannelEdgeInfo1: @@ -2445,22 +2448,25 @@ func (d *AuthenticatedGossiper) handleNodeAnnouncement(nMsg *networkMsg, return announcements, true } +// handleChanAnnouncement processes a new channel announcement. // handleChanAnnouncement processes a new channel announcement. func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg, - ann *lnwire.ChannelAnnouncement1, + ann lnwire.ChannelAnnouncement, ops []batch.SchedulerOption) ([]networkMsg, bool) { - scid := ann.ShortChannelID + var ( + scid = ann.SCID() + chainHash = ann.GetChainHash() + ) - log.Debugf("Processing ChannelAnnouncement1: peer=%v, short_chan_id=%v", - nMsg.peer, scid.ToUint64()) + log.Debugf("Processing %s: peer=%v, short_chan_id=%v", + nMsg.msg.MsgType(), nMsg.peer, scid.ToUint64()) // We'll ignore any channel announcements that target any chain other // than the set of chains we know of. - if !bytes.Equal(ann.ChainHash[:], d.cfg.ChainHash[:]) { - err := fmt.Errorf("ignoring ChannelAnnouncement1 from chain=%v"+ - ", gossiper on chain=%v", ann.ChainHash, - d.cfg.ChainHash) + if !bytes.Equal(chainHash[:], d.cfg.ChainHash[:]) { + err := fmt.Errorf("ignoring %s from chain=%v, gossiper on "+ + "chain=%v", ann.MsgType(), chainHash, d.cfg.ChainHash) log.Errorf(err.Error()) key := newRejectCacheKey( @@ -2815,8 +2821,8 @@ func (d *AuthenticatedGossiper) handleChanAnnouncement(nMsg *networkMsg, nMsg.err <- nil - log.Debugf("Processed ChannelAnnouncement1: peer=%v, short_chan_id=%v", - nMsg.peer, scid.ToUint64()) + log.Debugf("Processed %s: peer=%v, short_chan_id=%v", + nMsg.msg.MsgType(), nMsg.peer, scid.ToUint64()) return announcements, true } diff --git a/discovery/gossiper_test.go b/discovery/gossiper_test.go index 0f5524939..c328de59f 100644 --- a/discovery/gossiper_test.go +++ b/discovery/gossiper_test.go @@ -2829,7 +2829,7 @@ func TestRetransmit(t *testing.T) { var chanAnn, chanUpd, nodeAnn int for _, msg := range anns { switch msg.(type) { - case *lnwire.ChannelAnnouncement1: + case lnwire.ChannelAnnouncement: chanAnn++ case *lnwire.ChannelUpdate1: chanUpd++ diff --git a/discovery/syncer.go b/discovery/syncer.go index a3322f298..886aa4be0 100644 --- a/discovery/syncer.go +++ b/discovery/syncer.go @@ -1455,20 +1455,21 @@ func (g *GossipSyncer) FilterGossipMsgs(msgs ...msgWithSenders) { // For each channel announcement message, we'll only send this // message if the channel updates for the channel are between // our time range. - case *lnwire.ChannelAnnouncement1: + case lnwire.ChannelAnnouncement: + scid := msg.SCID() + // First, we'll check if the channel updates are in // this message batch. - chanUpdates, ok := chanUpdateIndex[msg.ShortChannelID] + chanUpdates, ok := chanUpdateIndex[scid] if !ok { // If not, we'll attempt to query the database // to see if we know of the updates. chanUpdates, err = g.cfg.channelSeries.FetchChanUpdates( - g.cfg.chainHash, msg.ShortChannelID, + g.cfg.chainHash, scid, ) if err != nil { - log.Warnf("no channel updates found for "+ - "short_chan_id=%v", - msg.ShortChannelID) + log.Warnf("no channel updates found "+ + "for short_chan_id=%v", scid) continue } } diff --git a/funding/manager.go b/funding/manager.go index 2b8a94adc..063ba7594 100644 --- a/funding/manager.go +++ b/funding/manager.go @@ -4143,7 +4143,7 @@ func (f *Manager) ensureInitialForwardingPolicy(chanID lnwire.ChannelID, // chanAnnouncement encapsulates the two authenticated announcements that we // send out to the network after a new channel has been created locally. type chanAnnouncement struct { - chanAnn *lnwire.ChannelAnnouncement1 + chanAnn lnwire.ChannelAnnouncement chanUpdateAnn *lnwire.ChannelUpdate1 chanProof *lnwire.AnnounceSignatures1 } diff --git a/funding/manager_test.go b/funding/manager_test.go index d3a08c167..8d8b81c1d 100644 --- a/funding/manager_test.go +++ b/funding/manager_test.go @@ -1208,7 +1208,7 @@ func assertChannelAnnouncements(t *testing.T, alice, bob *testNode, gotChannelUpdate := false for _, msg := range announcements { switch m := msg.(type) { - case *lnwire.ChannelAnnouncement1: + case lnwire.ChannelAnnouncement: gotChannelAnnouncement = true case *lnwire.ChannelUpdate1: diff --git a/graph/validation_barrier.go b/graph/validation_barrier.go index 775061732..fcd40d0f2 100644 --- a/graph/validation_barrier.go +++ b/graph/validation_barrier.go @@ -102,7 +102,8 @@ func (v *ValidationBarrier) InitJobDependencies(job interface{}) { // ChannelUpdates for the same channel, or NodeAnnouncements of nodes // that are involved in this channel. This goes for both the wire // type,s and also the types that we use within the database. - case *lnwire.ChannelAnnouncement1: + case lnwire.ChannelAnnouncement: + scid := msg.SCID() // We ensure that we only create a new announcement signal iff, // one doesn't already exist, as there may be duplicate @@ -110,7 +111,7 @@ func (v *ValidationBarrier) InitJobDependencies(job interface{}) { // ChannelAnnouncement has been validated. This will result in // all the dependent jobs being unlocked so they can finish // execution themselves. - if _, ok := v.chanAnnFinSignal[msg.ShortChannelID]; !ok { + if _, ok := v.chanAnnFinSignal[scid]; !ok { // We'll create the channel that we close after we // validate this announcement. All dependants will // point to this same channel, so they'll be unblocked @@ -120,11 +121,11 @@ func (v *ValidationBarrier) InitJobDependencies(job interface{}) { deny: make(chan struct{}), } - v.chanAnnFinSignal[msg.ShortChannelID] = signals - v.chanEdgeDependencies[msg.ShortChannelID] = signals + v.chanAnnFinSignal[scid] = signals + v.chanEdgeDependencies[scid] = signals - v.nodeAnnDependencies[route.Vertex(msg.NodeID1)] = signals - v.nodeAnnDependencies[route.Vertex(msg.NodeID2)] = signals + v.nodeAnnDependencies[msg.Node1KeyBytes()] = signals + v.nodeAnnDependencies[msg.Node2KeyBytes()] = signals } case models.ChannelEdgeInfo: shortID := lnwire.NewShortChanIDFromInt(msg.GetChanID()) @@ -218,7 +219,7 @@ func (v *ValidationBarrier) WaitForDependants(job interface{}) error { case *lnwire.AnnounceSignatures1: // TODO(roasbeef): need to wait on chan ann? case models.ChannelEdgeInfo: - case *lnwire.ChannelAnnouncement1: + case lnwire.ChannelAnnouncement: } // Release the lock once the above read is finished. @@ -274,18 +275,20 @@ func (v *ValidationBarrier) SignalDependants(job interface{}, allow bool) { } delete(v.chanAnnFinSignal, shortID) } - case *lnwire.ChannelAnnouncement1: - finSignals, ok := v.chanAnnFinSignal[msg.ShortChannelID] + case lnwire.ChannelAnnouncement: + scid := msg.SCID() + + finSignals, ok := v.chanAnnFinSignal[scid] if ok { if allow { close(finSignals.allow) } else { close(finSignals.deny) } - delete(v.chanAnnFinSignal, msg.ShortChannelID) + delete(v.chanAnnFinSignal, scid) } - delete(v.chanEdgeDependencies, msg.ShortChannelID) + delete(v.chanEdgeDependencies, scid) // For all other job types, we'll delete the tracking entries from the // map, as if we reach this point, then all dependants have already diff --git a/peer/brontide.go b/peer/brontide.go index 5133e5a02..89c2d4a04 100644 --- a/peer/brontide.go +++ b/peer/brontide.go @@ -1965,6 +1965,7 @@ out: case *lnwire.ChannelUpdate1, *lnwire.ChannelAnnouncement1, + *lnwire.ChannelAnnouncement2, *lnwire.NodeAnnouncement, *lnwire.AnnounceSignatures1, *lnwire.GossipTimestampRange, @@ -2228,9 +2229,9 @@ func messageSummary(msg lnwire.Message) string { return fmt.Sprintf("chan_id=%v, short_chan_id=%v", msg.ChannelID, msg.ShortChannelID.ToUint64()) - case *lnwire.ChannelAnnouncement1: + case lnwire.ChannelAnnouncement: return fmt.Sprintf("chain_hash=%v, short_chan_id=%v", - msg.ChainHash, msg.ShortChannelID.ToUint64()) + msg.GetChainHash(), msg.SCID().ToUint64()) case *lnwire.ChannelUpdate1: return fmt.Sprintf("chain_hash=%v, short_chan_id=%v, "+