• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 17112255878

20 Aug 2025 10:53PM UTC coverage: 66.751% (+9.4%) from 57.321%
17112255878

Pull #10167

github

web-flow
Merge 747f80c69 into 0c2f045f5
Pull Request #10167: multi: bump Go to 1.24.6

6 of 17 new or added lines in 7 files covered. (35.29%)

22 existing lines in 9 files now uncovered.

135966 of 203692 relevant lines covered (66.75%)

21503.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.27
/discovery/chan_series.go
1
package discovery
2

3
import (
4
        "context"
5
        "time"
6

7
        "github.com/btcsuite/btcd/chaincfg/chainhash"
8
        graphdb "github.com/lightningnetwork/lnd/graph/db"
9
        "github.com/lightningnetwork/lnd/lnwire"
10
        "github.com/lightningnetwork/lnd/netann"
11
        "github.com/lightningnetwork/lnd/routing/route"
12
)
13

14
// ChannelGraphTimeSeries is an interface that provides time and block based
15
// querying into our view of the channel graph. New channels will have
16
// monotonically increasing block heights, and new channel updates will have
17
// increasing timestamps. Once we connect to a peer, we'll use the methods in
18
// this interface to determine if we're already in sync, or need to request
19
// some new information from them.
20
type ChannelGraphTimeSeries interface {
21
        // HighestChanID should return the channel ID of the channel we know of
22
        // that's furthest in the target chain. This channel will have a block
23
        // height that's close to the current tip of the main chain as we
24
        // know it.  We'll use this to start our QueryChannelRange dance with
25
        // the remote node.
26
        HighestChanID(ctx context.Context,
27
                chain chainhash.Hash) (*lnwire.ShortChannelID, error)
28

29
        // UpdatesInHorizon returns all known channel and node updates with an
30
        // update timestamp between the start time and end time. We'll use this
31
        // to catch up a remote node to the set of channel updates that they
32
        // may have missed out on within the target chain.
33
        UpdatesInHorizon(chain chainhash.Hash,
34
                startTime time.Time, endTime time.Time) ([]lnwire.Message, error)
35

36
        // FilterKnownChanIDs takes a target chain, and a set of channel ID's,
37
        // and returns a filtered set of chan ID's. This filtered set of chan
38
        // ID's represents the ID's that we don't know of which were in the
39
        // passed superSet.
40
        FilterKnownChanIDs(chain chainhash.Hash,
41
                superSet []graphdb.ChannelUpdateInfo,
42
                isZombieChan func(time.Time, time.Time) bool) (
43
                []lnwire.ShortChannelID, error)
44

45
        // FilterChannelRange returns the set of channels that we created
46
        // between the start height and the end height. The channel IDs are
47
        // grouped by their common block height. We'll use this to to a remote
48
        // peer's QueryChannelRange message.
49
        FilterChannelRange(chain chainhash.Hash, startHeight, endHeight uint32,
50
                withTimestamps bool) ([]graphdb.BlockChannelRange, error)
51

52
        // FetchChanAnns returns a full set of channel announcements as well as
53
        // their updates that match the set of specified short channel ID's.
54
        // We'll use this to reply to a QueryShortChanIDs message sent by a
55
        // remote peer. The response will contain a unique set of
56
        // ChannelAnnouncements, the latest ChannelUpdate for each of the
57
        // announcements, and a unique set of NodeAnnouncements.
58
        FetchChanAnns(chain chainhash.Hash,
59
                shortChanIDs []lnwire.ShortChannelID) ([]lnwire.Message, error)
60

61
        // FetchChanUpdates returns the latest channel update messages for the
62
        // specified short channel ID. If no channel updates are known for the
63
        // channel, then an empty slice will be returned.
64
        FetchChanUpdates(chain chainhash.Hash,
65
                shortChanID lnwire.ShortChannelID) ([]*lnwire.ChannelUpdate1,
66
                error)
67
}
68

69
// ChanSeries is an implementation of the ChannelGraphTimeSeries
70
// interface backed by the channeldb ChannelGraph database. We'll provide this
71
// implementation to the AuthenticatedGossiper so it can properly use the
72
// in-protocol channel range queries to quickly and efficiently synchronize our
73
// channel state with all peers.
74
type ChanSeries struct {
75
        graph *graphdb.ChannelGraph
76
}
77

78
// NewChanSeries constructs a new ChanSeries backed by a channeldb.ChannelGraph.
79
// The returned ChanSeries implements the ChannelGraphTimeSeries interface.
80
func NewChanSeries(graph *graphdb.ChannelGraph) *ChanSeries {
3✔
81
        return &ChanSeries{
3✔
82
                graph: graph,
3✔
83
        }
3✔
84
}
3✔
85

86
// HighestChanID should return is the channel ID of the channel we know of
87
// that's furthest in the target chain. This channel will have a block height
88
// that's close to the current tip of the main chain as we know it.  We'll use
89
// this to start our QueryChannelRange dance with the remote node.
90
//
91
// NOTE: This is part of the ChannelGraphTimeSeries interface.
92
func (c *ChanSeries) HighestChanID(ctx context.Context,
93
        _ chainhash.Hash) (*lnwire.ShortChannelID, error) {
3✔
94

3✔
95
        chanID, err := c.graph.HighestChanID(ctx)
3✔
96
        if err != nil {
3✔
97
                return nil, err
×
98
        }
×
99

100
        shortChanID := lnwire.NewShortChanIDFromInt(chanID)
3✔
101
        return &shortChanID, nil
3✔
102
}
103

104
// UpdatesInHorizon returns all known channel and node updates with an update
105
// timestamp between the start time and end time. We'll use this to catch up a
106
// remote node to the set of channel updates that they may have missed out on
107
// within the target chain.
108
//
109
// NOTE: This is part of the ChannelGraphTimeSeries interface.
110
func (c *ChanSeries) UpdatesInHorizon(chain chainhash.Hash,
111
        startTime time.Time, endTime time.Time) ([]lnwire.Message, error) {
3✔
112

3✔
113
        var updates []lnwire.Message
3✔
114

3✔
115
        // First, we'll query for all the set of channels that have an update
3✔
116
        // that falls within the specified horizon.
3✔
117
        chansInHorizon, err := c.graph.ChanUpdatesInHorizon(
3✔
118
                startTime, endTime,
3✔
119
        )
3✔
120
        if err != nil {
3✔
121
                return nil, err
×
122
        }
×
123

124
        // nodesFromChan records the nodes seen from the channels.
125
        nodesFromChan := make(map[[33]byte]struct{}, len(chansInHorizon)*2)
3✔
126

3✔
127
        for _, channel := range chansInHorizon {
6✔
128
                // If the channel hasn't been fully advertised yet, or is a
3✔
129
                // private channel, then we'll skip it as we can't construct a
3✔
130
                // full authentication proof if one is requested.
3✔
131
                if channel.Info.AuthProof == nil {
6✔
132
                        continue
3✔
133
                }
134

135
                chanAnn, edge1, edge2, err := netann.CreateChanAnnouncement(
3✔
136
                        channel.Info.AuthProof, channel.Info, channel.Policy1,
3✔
137
                        channel.Policy2,
3✔
138
                )
3✔
139
                if err != nil {
3✔
140
                        return nil, err
×
141
                }
×
142

143
                // Create a slice to hold the `channel_announcement` and
144
                // potentially two `channel_update` msgs.
145
                //
146
                // NOTE: Based on BOLT7, if a channel_announcement has no
147
                // corresponding channel_updates, we must not send the
148
                // channel_announcement. Thus we use this slice to decide we
149
                // want to send this `channel_announcement` or not. By the end
150
                // of the operation, if the len of the slice is 1, we will not
151
                // send the `channel_announcement`. Otherwise, when sending the
152
                // msgs, the `channel_announcement` must be sent prior to any
153
                // corresponding `channel_update` or `node_annoucement`, that's
154
                // why we create a slice here to maintain the order.
155
                chanUpdates := make([]lnwire.Message, 0, 3)
3✔
156
                chanUpdates = append(chanUpdates, chanAnn)
3✔
157

3✔
158
                if edge1 != nil {
6✔
159
                        // We don't want to send channel updates that don't
3✔
160
                        // conform to the spec (anymore).
3✔
161
                        err := netann.ValidateChannelUpdateFields(0, edge1)
3✔
162
                        if err != nil {
3✔
163
                                log.Errorf("not sending invalid channel "+
×
164
                                        "update %v: %v", edge1, err)
×
165
                        } else {
3✔
166
                                chanUpdates = append(chanUpdates, edge1)
3✔
167
                        }
3✔
168
                }
169

170
                if edge2 != nil {
6✔
171
                        err := netann.ValidateChannelUpdateFields(0, edge2)
3✔
172
                        if err != nil {
3✔
173
                                log.Errorf("not sending invalid channel "+
×
174
                                        "update %v: %v", edge2, err)
×
175
                        } else {
3✔
176
                                chanUpdates = append(chanUpdates, edge2)
3✔
177
                        }
3✔
178
                }
179

180
                // If there's no corresponding `channel_update` to send, skip
181
                // sending this `channel_announcement`.
182
                if len(chanUpdates) < 2 {
3✔
183
                        continue
×
184
                }
185

186
                // Append the all the msgs to the slice.
187
                updates = append(updates, chanUpdates...)
3✔
188

3✔
189
                // Record the nodes seen.
3✔
190
                nodesFromChan[channel.Info.NodeKey1Bytes] = struct{}{}
3✔
191
                nodesFromChan[channel.Info.NodeKey2Bytes] = struct{}{}
3✔
192
        }
193

194
        // Next, we'll send out all the node announcements that have an update
195
        // within the horizon as well. We send these second to ensure that they
196
        // follow any active channels they have.
197
        nodeAnnsInHorizon, err := c.graph.NodeUpdatesInHorizon(
3✔
198
                startTime, endTime,
3✔
199
        )
3✔
200
        if err != nil {
3✔
201
                return nil, err
×
202
        }
×
203

204
        for _, nodeAnn := range nodeAnnsInHorizon {
6✔
205
                // If this node has not been seen in the above channels, we can
3✔
206
                // skip sending its NodeAnnouncement.
3✔
207
                if _, seen := nodesFromChan[nodeAnn.PubKeyBytes]; !seen {
6✔
208
                        log.Debugf("Skipping forwarding as node %x not found "+
3✔
209
                                "in channel announcement", nodeAnn.PubKeyBytes)
3✔
210
                        continue
3✔
211
                }
212

213
                // Ensure we only forward nodes that are publicly advertised to
214
                // prevent leaking information about nodes.
UNCOV
215
                isNodePublic, err := c.graph.IsPublicNode(nodeAnn.PubKeyBytes)
×
UNCOV
216
                if err != nil {
×
217
                        log.Errorf("Unable to determine if node %x is "+
×
218
                                "advertised: %v", nodeAnn.PubKeyBytes, err)
×
219
                        continue
×
220
                }
221

UNCOV
222
                if !isNodePublic {
×
223
                        log.Tracef("Skipping forwarding announcement for "+
×
224
                                "node %x due to being unadvertised",
×
225
                                nodeAnn.PubKeyBytes)
×
226
                        continue
×
227
                }
228

UNCOV
229
                nodeUpdate, err := nodeAnn.NodeAnnouncement(true)
×
UNCOV
230
                if err != nil {
×
231
                        return nil, err
×
232
                }
×
233

UNCOV
234
                updates = append(updates, nodeUpdate)
×
235
        }
236

237
        return updates, nil
3✔
238
}
239

240
// FilterKnownChanIDs takes a target chain, and a set of channel ID's, and
241
// returns a filtered set of chan ID's. This filtered set of chan ID's
242
// represents the ID's that we don't know of which were in the passed superSet.
243
//
244
// NOTE: This is part of the ChannelGraphTimeSeries interface.
245
func (c *ChanSeries) FilterKnownChanIDs(_ chainhash.Hash,
246
        superSet []graphdb.ChannelUpdateInfo,
247
        isZombieChan func(time.Time, time.Time) bool) (
248
        []lnwire.ShortChannelID, error) {
3✔
249

3✔
250
        newChanIDs, err := c.graph.FilterKnownChanIDs(superSet, isZombieChan)
3✔
251
        if err != nil {
3✔
252
                return nil, err
×
253
        }
×
254

255
        filteredIDs := make([]lnwire.ShortChannelID, 0, len(newChanIDs))
3✔
256
        for _, chanID := range newChanIDs {
6✔
257
                filteredIDs = append(
3✔
258
                        filteredIDs, lnwire.NewShortChanIDFromInt(chanID),
3✔
259
                )
3✔
260
        }
3✔
261

262
        return filteredIDs, nil
3✔
263
}
264

265
// FilterChannelRange returns the set of channels that we created between the
266
// start height and the end height. The channel IDs are grouped by their common
267
// block height. We'll use this respond to a remote peer's QueryChannelRange
268
// message.
269
//
270
// NOTE: This is part of the ChannelGraphTimeSeries interface.
271
func (c *ChanSeries) FilterChannelRange(_ chainhash.Hash, startHeight,
272
        endHeight uint32, withTimestamps bool) ([]graphdb.BlockChannelRange,
273
        error) {
3✔
274

3✔
275
        return c.graph.FilterChannelRange(
3✔
276
                startHeight, endHeight, withTimestamps,
3✔
277
        )
3✔
278
}
3✔
279

280
// FetchChanAnns returns a full set of channel announcements as well as their
281
// updates that match the set of specified short channel ID's.  We'll use this
282
// to reply to a QueryShortChanIDs message sent by a remote peer. The response
283
// will contain a unique set of ChannelAnnouncements, the latest ChannelUpdate
284
// for each of the announcements, and a unique set of NodeAnnouncements.
285
//
286
// NOTE: This is part of the ChannelGraphTimeSeries interface.
287
func (c *ChanSeries) FetchChanAnns(chain chainhash.Hash,
288
        shortChanIDs []lnwire.ShortChannelID) ([]lnwire.Message, error) {
3✔
289

3✔
290
        chanIDs := make([]uint64, 0, len(shortChanIDs))
3✔
291
        for _, chanID := range shortChanIDs {
6✔
292
                chanIDs = append(chanIDs, chanID.ToUint64())
3✔
293
        }
3✔
294

295
        channels, err := c.graph.FetchChanInfos(chanIDs)
3✔
296
        if err != nil {
3✔
297
                return nil, err
×
298
        }
×
299

300
        // We'll use this map to ensure we don't send the same node
301
        // announcement more than one time as one node may have many channel
302
        // anns we'll need to send.
303
        nodePubsSent := make(map[route.Vertex]struct{})
3✔
304

3✔
305
        chanAnns := make([]lnwire.Message, 0, len(channels)*3)
3✔
306
        for _, channel := range channels {
6✔
307
                // If the channel doesn't have an authentication proof, then we
3✔
308
                // won't send it over as it may not yet be finalized, or be a
3✔
309
                // non-advertised channel.
3✔
310
                if channel.Info.AuthProof == nil {
3✔
311
                        continue
×
312
                }
313

314
                chanAnn, edge1, edge2, err := netann.CreateChanAnnouncement(
3✔
315
                        channel.Info.AuthProof, channel.Info, channel.Policy1,
3✔
316
                        channel.Policy2,
3✔
317
                )
3✔
318
                if err != nil {
3✔
319
                        return nil, err
×
320
                }
×
321

322
                chanAnns = append(chanAnns, chanAnn)
3✔
323
                if edge1 != nil {
6✔
324
                        chanAnns = append(chanAnns, edge1)
3✔
325

3✔
326
                        // If this edge has a validated node announcement, that
3✔
327
                        // we haven't yet sent, then we'll send that as well.
3✔
328
                        nodePub := channel.Node2.PubKeyBytes
3✔
329
                        hasNodeAnn := channel.Node2.HaveNodeAnnouncement
3✔
330
                        if _, ok := nodePubsSent[nodePub]; !ok && hasNodeAnn {
6✔
331
                                nodeAnn, err := channel.Node2.NodeAnnouncement(
3✔
332
                                        true,
3✔
333
                                )
3✔
334
                                if err != nil {
3✔
335
                                        return nil, err
×
336
                                }
×
337

338
                                chanAnns = append(chanAnns, nodeAnn)
3✔
339
                                nodePubsSent[nodePub] = struct{}{}
3✔
340
                        }
341
                }
342
                if edge2 != nil {
6✔
343
                        chanAnns = append(chanAnns, edge2)
3✔
344

3✔
345
                        // If this edge has a validated node announcement, that
3✔
346
                        // we haven't yet sent, then we'll send that as well.
3✔
347
                        nodePub := channel.Node1.PubKeyBytes
3✔
348
                        hasNodeAnn := channel.Node1.HaveNodeAnnouncement
3✔
349
                        if _, ok := nodePubsSent[nodePub]; !ok && hasNodeAnn {
6✔
350
                                nodeAnn, err := channel.Node1.NodeAnnouncement(
3✔
351
                                        true,
3✔
352
                                )
3✔
353
                                if err != nil {
3✔
354
                                        return nil, err
×
355
                                }
×
356

357
                                chanAnns = append(chanAnns, nodeAnn)
3✔
358
                                nodePubsSent[nodePub] = struct{}{}
3✔
359
                        }
360
                }
361
        }
362

363
        return chanAnns, nil
3✔
364
}
365

366
// FetchChanUpdates returns the latest channel update messages for the
367
// specified short channel ID. If no channel updates are known for the channel,
368
// then an empty slice will be returned.
369
//
370
// NOTE: This is part of the ChannelGraphTimeSeries interface.
371
func (c *ChanSeries) FetchChanUpdates(chain chainhash.Hash,
372
        shortChanID lnwire.ShortChannelID) ([]*lnwire.ChannelUpdate1, error) {
3✔
373

3✔
374
        chanInfo, e1, e2, err := c.graph.FetchChannelEdgesByID(
3✔
375
                shortChanID.ToUint64(),
3✔
376
        )
3✔
377
        if err != nil {
3✔
378
                return nil, err
×
379
        }
×
380

381
        chanUpdates := make([]*lnwire.ChannelUpdate1, 0, 2)
3✔
382
        if e1 != nil {
6✔
383
                chanUpdate, err := netann.ChannelUpdateFromEdge(chanInfo, e1)
3✔
384
                if err != nil {
3✔
385
                        return nil, err
×
386
                }
×
387

388
                chanUpdates = append(chanUpdates, chanUpdate)
3✔
389
        }
390
        if e2 != nil {
6✔
391
                chanUpdate, err := netann.ChannelUpdateFromEdge(chanInfo, e2)
3✔
392
                if err != nil {
3✔
393
                        return nil, err
×
394
                }
×
395

396
                chanUpdates = append(chanUpdates, chanUpdate)
3✔
397
        }
398

399
        return chanUpdates, nil
3✔
400
}
401

402
// A compile-time assertion to ensure that ChanSeries meets the
403
// ChannelGraphTimeSeries interface.
404
var _ ChannelGraphTimeSeries = (*ChanSeries)(nil)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc