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

lightningnetwork / lnd / 15869455718

25 Jun 2025 06:53AM UTC coverage: 55.806% (-12.2%) from 67.978%
15869455718

Pull #9148

github

web-flow
Merge c64e3a6c3 into 4335d9cfb
Pull Request #9148: DynComms [2/n]: lnwire: add authenticated wire messages for Dyn*

232 of 270 new or added lines in 5 files covered. (85.93%)

23628 existing lines in 289 files now uncovered.

108377 of 194204 relevant lines covered (55.81%)

22552.93 hits per line

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

0.0
/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.
UNCOV
80
func NewChanSeries(graph *graphdb.ChannelGraph) *ChanSeries {
×
UNCOV
81
        return &ChanSeries{
×
UNCOV
82
                graph: graph,
×
UNCOV
83
        }
×
UNCOV
84
}
×
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,
UNCOV
93
        _ chainhash.Hash) (*lnwire.ShortChannelID, error) {
×
UNCOV
94

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

UNCOV
100
        shortChanID := lnwire.NewShortChanIDFromInt(chanID)
×
UNCOV
101
        return &shortChanID, nil
×
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,
UNCOV
111
        startTime time.Time, endTime time.Time) ([]lnwire.Message, error) {
×
UNCOV
112

×
UNCOV
113
        var updates []lnwire.Message
×
UNCOV
114

×
UNCOV
115
        // First, we'll query for all the set of channels that have an update
×
UNCOV
116
        // that falls within the specified horizon.
×
UNCOV
117
        chansInHorizon, err := c.graph.ChanUpdatesInHorizon(
×
UNCOV
118
                startTime, endTime,
×
UNCOV
119
        )
×
UNCOV
120
        if err != nil {
×
121
                return nil, err
×
122
        }
×
UNCOV
123
        for _, channel := range chansInHorizon {
×
UNCOV
124
                // If the channel hasn't been fully advertised yet, or is a
×
UNCOV
125
                // private channel, then we'll skip it as we can't construct a
×
UNCOV
126
                // full authentication proof if one is requested.
×
UNCOV
127
                if channel.Info.AuthProof == nil {
×
UNCOV
128
                        continue
×
129
                }
130

UNCOV
131
                chanAnn, edge1, edge2, err := netann.CreateChanAnnouncement(
×
UNCOV
132
                        channel.Info.AuthProof, channel.Info, channel.Policy1,
×
UNCOV
133
                        channel.Policy2,
×
UNCOV
134
                )
×
UNCOV
135
                if err != nil {
×
136
                        return nil, err
×
137
                }
×
138

UNCOV
139
                updates = append(updates, chanAnn)
×
UNCOV
140
                if edge1 != nil {
×
UNCOV
141
                        // We don't want to send channel updates that don't
×
UNCOV
142
                        // conform to the spec (anymore).
×
UNCOV
143
                        err := netann.ValidateChannelUpdateFields(0, edge1)
×
UNCOV
144
                        if err != nil {
×
145
                                log.Errorf("not sending invalid channel "+
×
146
                                        "update %v: %v", edge1, err)
×
UNCOV
147
                        } else {
×
UNCOV
148
                                updates = append(updates, edge1)
×
UNCOV
149
                        }
×
150
                }
UNCOV
151
                if edge2 != nil {
×
UNCOV
152
                        err := netann.ValidateChannelUpdateFields(0, edge2)
×
UNCOV
153
                        if err != nil {
×
154
                                log.Errorf("not sending invalid channel "+
×
155
                                        "update %v: %v", edge2, err)
×
UNCOV
156
                        } else {
×
UNCOV
157
                                updates = append(updates, edge2)
×
UNCOV
158
                        }
×
159
                }
160
        }
161

162
        // Next, we'll send out all the node announcements that have an update
163
        // within the horizon as well. We send these second to ensure that they
164
        // follow any active channels they have.
UNCOV
165
        nodeAnnsInHorizon, err := c.graph.NodeUpdatesInHorizon(
×
UNCOV
166
                startTime, endTime,
×
UNCOV
167
        )
×
UNCOV
168
        if err != nil {
×
169
                return nil, err
×
170
        }
×
UNCOV
171
        for _, nodeAnn := range nodeAnnsInHorizon {
×
UNCOV
172
                nodeAnn := nodeAnn
×
UNCOV
173

×
UNCOV
174
                // Ensure we only forward nodes that are publicly advertised to
×
UNCOV
175
                // prevent leaking information about nodes.
×
UNCOV
176
                isNodePublic, err := c.graph.IsPublicNode(nodeAnn.PubKeyBytes)
×
UNCOV
177
                if err != nil {
×
178
                        log.Errorf("Unable to determine if node %x is "+
×
179
                                "advertised: %v", nodeAnn.PubKeyBytes, err)
×
180
                        continue
×
181
                }
182

UNCOV
183
                if !isNodePublic {
×
UNCOV
184
                        log.Tracef("Skipping forwarding announcement for "+
×
UNCOV
185
                                "node %x due to being unadvertised",
×
UNCOV
186
                                nodeAnn.PubKeyBytes)
×
UNCOV
187
                        continue
×
188
                }
189

UNCOV
190
                nodeUpdate, err := nodeAnn.NodeAnnouncement(true)
×
UNCOV
191
                if err != nil {
×
192
                        return nil, err
×
193
                }
×
194

UNCOV
195
                updates = append(updates, nodeUpdate)
×
196
        }
197

UNCOV
198
        return updates, nil
×
199
}
200

201
// FilterKnownChanIDs takes a target chain, and a set of channel ID's, and
202
// returns a filtered set of chan ID's. This filtered set of chan ID's
203
// represents the ID's that we don't know of which were in the passed superSet.
204
//
205
// NOTE: This is part of the ChannelGraphTimeSeries interface.
206
func (c *ChanSeries) FilterKnownChanIDs(_ chainhash.Hash,
207
        superSet []graphdb.ChannelUpdateInfo,
208
        isZombieChan func(time.Time, time.Time) bool) (
UNCOV
209
        []lnwire.ShortChannelID, error) {
×
UNCOV
210

×
UNCOV
211
        newChanIDs, err := c.graph.FilterKnownChanIDs(superSet, isZombieChan)
×
UNCOV
212
        if err != nil {
×
213
                return nil, err
×
214
        }
×
215

UNCOV
216
        filteredIDs := make([]lnwire.ShortChannelID, 0, len(newChanIDs))
×
UNCOV
217
        for _, chanID := range newChanIDs {
×
UNCOV
218
                filteredIDs = append(
×
UNCOV
219
                        filteredIDs, lnwire.NewShortChanIDFromInt(chanID),
×
UNCOV
220
                )
×
UNCOV
221
        }
×
222

UNCOV
223
        return filteredIDs, nil
×
224
}
225

226
// FilterChannelRange returns the set of channels that we created between the
227
// start height and the end height. The channel IDs are grouped by their common
228
// block height. We'll use this respond to a remote peer's QueryChannelRange
229
// message.
230
//
231
// NOTE: This is part of the ChannelGraphTimeSeries interface.
232
func (c *ChanSeries) FilterChannelRange(_ chainhash.Hash, startHeight,
233
        endHeight uint32, withTimestamps bool) ([]graphdb.BlockChannelRange,
UNCOV
234
        error) {
×
UNCOV
235

×
UNCOV
236
        return c.graph.FilterChannelRange(
×
UNCOV
237
                startHeight, endHeight, withTimestamps,
×
UNCOV
238
        )
×
UNCOV
239
}
×
240

241
// FetchChanAnns returns a full set of channel announcements as well as their
242
// updates that match the set of specified short channel ID's.  We'll use this
243
// to reply to a QueryShortChanIDs message sent by a remote peer. The response
244
// will contain a unique set of ChannelAnnouncements, the latest ChannelUpdate
245
// for each of the announcements, and a unique set of NodeAnnouncements.
246
//
247
// NOTE: This is part of the ChannelGraphTimeSeries interface.
248
func (c *ChanSeries) FetchChanAnns(chain chainhash.Hash,
UNCOV
249
        shortChanIDs []lnwire.ShortChannelID) ([]lnwire.Message, error) {
×
UNCOV
250

×
UNCOV
251
        chanIDs := make([]uint64, 0, len(shortChanIDs))
×
UNCOV
252
        for _, chanID := range shortChanIDs {
×
UNCOV
253
                chanIDs = append(chanIDs, chanID.ToUint64())
×
UNCOV
254
        }
×
255

UNCOV
256
        channels, err := c.graph.FetchChanInfos(chanIDs)
×
UNCOV
257
        if err != nil {
×
258
                return nil, err
×
259
        }
×
260

261
        // We'll use this map to ensure we don't send the same node
262
        // announcement more than one time as one node may have many channel
263
        // anns we'll need to send.
UNCOV
264
        nodePubsSent := make(map[route.Vertex]struct{})
×
UNCOV
265

×
UNCOV
266
        chanAnns := make([]lnwire.Message, 0, len(channels)*3)
×
UNCOV
267
        for _, channel := range channels {
×
UNCOV
268
                // If the channel doesn't have an authentication proof, then we
×
UNCOV
269
                // won't send it over as it may not yet be finalized, or be a
×
UNCOV
270
                // non-advertised channel.
×
UNCOV
271
                if channel.Info.AuthProof == nil {
×
272
                        continue
×
273
                }
274

UNCOV
275
                chanAnn, edge1, edge2, err := netann.CreateChanAnnouncement(
×
UNCOV
276
                        channel.Info.AuthProof, channel.Info, channel.Policy1,
×
UNCOV
277
                        channel.Policy2,
×
UNCOV
278
                )
×
UNCOV
279
                if err != nil {
×
280
                        return nil, err
×
281
                }
×
282

UNCOV
283
                chanAnns = append(chanAnns, chanAnn)
×
UNCOV
284
                if edge1 != nil {
×
UNCOV
285
                        chanAnns = append(chanAnns, edge1)
×
UNCOV
286

×
UNCOV
287
                        // If this edge has a validated node announcement, that
×
UNCOV
288
                        // we haven't yet sent, then we'll send that as well.
×
UNCOV
289
                        nodePub := channel.Node2.PubKeyBytes
×
UNCOV
290
                        hasNodeAnn := channel.Node2.HaveNodeAnnouncement
×
UNCOV
291
                        if _, ok := nodePubsSent[nodePub]; !ok && hasNodeAnn {
×
UNCOV
292
                                nodeAnn, err := channel.Node2.NodeAnnouncement(
×
UNCOV
293
                                        true,
×
UNCOV
294
                                )
×
UNCOV
295
                                if err != nil {
×
296
                                        return nil, err
×
297
                                }
×
298

UNCOV
299
                                chanAnns = append(chanAnns, nodeAnn)
×
UNCOV
300
                                nodePubsSent[nodePub] = struct{}{}
×
301
                        }
302
                }
UNCOV
303
                if edge2 != nil {
×
UNCOV
304
                        chanAnns = append(chanAnns, edge2)
×
UNCOV
305

×
UNCOV
306
                        // If this edge has a validated node announcement, that
×
UNCOV
307
                        // we haven't yet sent, then we'll send that as well.
×
UNCOV
308
                        nodePub := channel.Node1.PubKeyBytes
×
UNCOV
309
                        hasNodeAnn := channel.Node1.HaveNodeAnnouncement
×
UNCOV
310
                        if _, ok := nodePubsSent[nodePub]; !ok && hasNodeAnn {
×
UNCOV
311
                                nodeAnn, err := channel.Node1.NodeAnnouncement(
×
UNCOV
312
                                        true,
×
UNCOV
313
                                )
×
UNCOV
314
                                if err != nil {
×
315
                                        return nil, err
×
316
                                }
×
317

UNCOV
318
                                chanAnns = append(chanAnns, nodeAnn)
×
UNCOV
319
                                nodePubsSent[nodePub] = struct{}{}
×
320
                        }
321
                }
322
        }
323

UNCOV
324
        return chanAnns, nil
×
325
}
326

327
// FetchChanUpdates returns the latest channel update messages for the
328
// specified short channel ID. If no channel updates are known for the channel,
329
// then an empty slice will be returned.
330
//
331
// NOTE: This is part of the ChannelGraphTimeSeries interface.
332
func (c *ChanSeries) FetchChanUpdates(chain chainhash.Hash,
UNCOV
333
        shortChanID lnwire.ShortChannelID) ([]*lnwire.ChannelUpdate1, error) {
×
UNCOV
334

×
UNCOV
335
        chanInfo, e1, e2, err := c.graph.FetchChannelEdgesByID(
×
UNCOV
336
                shortChanID.ToUint64(),
×
UNCOV
337
        )
×
UNCOV
338
        if err != nil {
×
339
                return nil, err
×
340
        }
×
341

UNCOV
342
        chanUpdates := make([]*lnwire.ChannelUpdate1, 0, 2)
×
UNCOV
343
        if e1 != nil {
×
UNCOV
344
                chanUpdate, err := netann.ChannelUpdateFromEdge(chanInfo, e1)
×
UNCOV
345
                if err != nil {
×
346
                        return nil, err
×
347
                }
×
348

UNCOV
349
                chanUpdates = append(chanUpdates, chanUpdate)
×
350
        }
UNCOV
351
        if e2 != nil {
×
UNCOV
352
                chanUpdate, err := netann.ChannelUpdateFromEdge(chanInfo, e2)
×
UNCOV
353
                if err != nil {
×
354
                        return nil, err
×
355
                }
×
356

UNCOV
357
                chanUpdates = append(chanUpdates, chanUpdate)
×
358
        }
359

UNCOV
360
        return chanUpdates, nil
×
361
}
362

363
// A compile-time assertion to ensure that ChanSeries meets the
364
// ChannelGraphTimeSeries interface.
365
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