diff --git a/discovery/chan_series.go b/discovery/chan_series.go index a6787edf9..525bf8203 100644 --- a/discovery/chan_series.go +++ b/discovery/chan_series.go @@ -136,7 +136,21 @@ func (c *ChanSeries) UpdatesInHorizon(chain chainhash.Hash, return nil, err } - updates = append(updates, chanAnn) + // Create a slice to hold the `channel_announcement` and + // potentially two `channel_update` msgs. + // + // NOTE: Based on BOLT7, if a channel_announcement has no + // corresponding channel_updates, we must not send the + // channel_announcement. Thus we use this slice to decide we + // want to send this `channel_announcement` or not. By the end + // of the operation, if the len of the slice is 1, we will not + // send the `channel_announcement`. Otherwise, when sending the + // msgs, the `channel_announcement` must be sent prior to any + // corresponding `channel_update` or `node_annoucement`, that's + // why we create a slice here to maintain the order. + chanUpdates := make([]lnwire.Message, 0, 3) + chanUpdates = append(chanUpdates, chanAnn) + if edge1 != nil { // We don't want to send channel updates that don't // conform to the spec (anymore). @@ -145,18 +159,28 @@ func (c *ChanSeries) UpdatesInHorizon(chain chainhash.Hash, log.Errorf("not sending invalid channel "+ "update %v: %v", edge1, err) } else { - updates = append(updates, edge1) + chanUpdates = append(chanUpdates, edge1) } } + if edge2 != nil { err := netann.ValidateChannelUpdateFields(0, edge2) if err != nil { log.Errorf("not sending invalid channel "+ "update %v: %v", edge2, err) } else { - updates = append(updates, edge2) + chanUpdates = append(chanUpdates, edge2) } } + + // If there's no corresponding `channel_update` to send, skip + // sending this `channel_announcement`. + if len(chanUpdates) < 2 { + continue + } + + // Append the all the msgs to the slice. + updates = append(updates, chanUpdates...) } // Next, we'll send out all the node announcements that have an update