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

lightningnetwork / lnd / 14358372723

09 Apr 2025 01:26PM UTC coverage: 56.696% (-12.3%) from 69.037%
14358372723

Pull #9696

github

web-flow
Merge e2837e400 into 867d27d68
Pull Request #9696: Add `development_guidelines.md` for both human and machine

107055 of 188823 relevant lines covered (56.7%)

22721.56 hits per line

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

81.91
/discovery/syncer.go
1
package discovery
2

3
import (
4
        "context"
5
        "errors"
6
        "fmt"
7
        "math"
8
        "math/rand"
9
        "sort"
10
        "sync"
11
        "sync/atomic"
12
        "time"
13

14
        "github.com/btcsuite/btcd/chaincfg/chainhash"
15
        "github.com/lightningnetwork/lnd/fn/v2"
16
        "github.com/lightningnetwork/lnd/graph"
17
        graphdb "github.com/lightningnetwork/lnd/graph/db"
18
        "github.com/lightningnetwork/lnd/lnpeer"
19
        "github.com/lightningnetwork/lnd/lnwire"
20
)
21

22
// SyncerType encapsulates the different types of syncing mechanisms for a
23
// gossip syncer.
24
type SyncerType uint8
25

26
const (
27
        // ActiveSync denotes that a gossip syncer:
28
        //
29
        // 1. Should not attempt to synchronize with the remote peer for
30
        //    missing channels.
31
        // 2. Should respond to queries from the remote peer.
32
        // 3. Should receive new updates from the remote peer.
33
        //
34
        // They are started in a chansSynced state in order to accomplish their
35
        // responsibilities above.
36
        ActiveSync SyncerType = iota
37

38
        // PassiveSync denotes that a gossip syncer:
39
        //
40
        // 1. Should not attempt to synchronize with the remote peer for
41
        //    missing channels.
42
        // 2. Should respond to queries from the remote peer.
43
        // 3. Should not receive new updates from the remote peer.
44
        //
45
        // They are started in a chansSynced state in order to accomplish their
46
        // responsibilities above.
47
        PassiveSync
48

49
        // PinnedSync denotes an ActiveSync that doesn't count towards the
50
        // default active syncer limits and is always active throughout the
51
        // duration of the peer's connection. Each pinned syncer will begin by
52
        // performing a historical sync to ensure we are well synchronized with
53
        // their routing table.
54
        PinnedSync
55
)
56

57
// String returns a human readable string describing the target SyncerType.
58
func (t SyncerType) String() string {
×
59
        switch t {
×
60
        case ActiveSync:
×
61
                return "ActiveSync"
×
62
        case PassiveSync:
×
63
                return "PassiveSync"
×
64
        case PinnedSync:
×
65
                return "PinnedSync"
×
66
        default:
×
67
                return fmt.Sprintf("unknown sync type %d", t)
×
68
        }
69
}
70

71
// IsActiveSync returns true if the SyncerType should set a GossipTimestampRange
72
// allowing new gossip messages to be received from the peer.
73
func (t SyncerType) IsActiveSync() bool {
44✔
74
        switch t {
44✔
75
        case ActiveSync, PinnedSync:
14✔
76
                return true
14✔
77
        default:
30✔
78
                return false
30✔
79
        }
80
}
81

82
// syncerState is an enum that represents the current state of the GossipSyncer.
83
// As the syncer is a state machine, we'll gate our actions based off of the
84
// current state and the next incoming message.
85
type syncerState uint32
86

87
const (
88
        // syncingChans is the default state of the GossipSyncer. We start in
89
        // this state when a new peer first connects and we don't yet know if
90
        // we're fully synchronized.
91
        syncingChans syncerState = iota
92

93
        // waitingQueryRangeReply is the second main phase of the GossipSyncer.
94
        // We enter this state after we send out our first QueryChannelRange
95
        // reply. We'll stay in this state until the remote party sends us a
96
        // ReplyShortChanIDsEnd message that indicates they've responded to our
97
        // query entirely. After this state, we'll transition to
98
        // waitingQueryChanReply after we send out requests for all the new
99
        // chan ID's to us.
100
        waitingQueryRangeReply
101

102
        // queryNewChannels is the third main phase of the GossipSyncer.  In
103
        // this phase we'll send out all of our QueryShortChanIDs messages in
104
        // response to the new channels that we don't yet know about.
105
        queryNewChannels
106

107
        // waitingQueryChanReply is the fourth main phase of the GossipSyncer.
108
        // We enter this phase once we've sent off a query chink to the remote
109
        // peer.  We'll stay in this phase until we receive a
110
        // ReplyShortChanIDsEnd message which indicates that the remote party
111
        // has responded to all of our requests.
112
        waitingQueryChanReply
113

114
        // chansSynced is the terminal stage of the GossipSyncer. Once we enter
115
        // this phase, we'll send out our update horizon, which filters out the
116
        // set of channel updates that we're interested in. In this state,
117
        // we'll be able to accept any outgoing messages from the
118
        // AuthenticatedGossiper, and decide if we should forward them to our
119
        // target peer based on its update horizon.
120
        chansSynced
121

122
        // syncerIdle is a state in which the gossip syncer can handle external
123
        // requests to transition or perform historical syncs. It is used as the
124
        // initial state for pinned syncers, as well as a fallthrough case for
125
        // chansSynced allowing fully synced peers to facilitate requests.
126
        syncerIdle
127
)
128

129
// String returns a human readable string describing the target syncerState.
130
func (s syncerState) String() string {
1✔
131
        switch s {
1✔
132
        case syncingChans:
×
133
                return "syncingChans"
×
134

135
        case waitingQueryRangeReply:
×
136
                return "waitingQueryRangeReply"
×
137

138
        case queryNewChannels:
×
139
                return "queryNewChannels"
×
140

141
        case waitingQueryChanReply:
×
142
                return "waitingQueryChanReply"
×
143

144
        case chansSynced:
1✔
145
                return "chansSynced"
1✔
146

147
        case syncerIdle:
×
148
                return "syncerIdle"
×
149

150
        default:
×
151
                return "UNKNOWN STATE"
×
152
        }
153
}
154

155
const (
156
        // maxQueryChanRangeReplies specifies the default limit of replies to
157
        // process for a single QueryChannelRange request.
158
        maxQueryChanRangeReplies = 500
159

160
        // maxQueryChanRangeRepliesZlibFactor specifies the factor applied to
161
        // the maximum number of replies allowed for zlib encoded replies.
162
        maxQueryChanRangeRepliesZlibFactor = 4
163

164
        // chanRangeQueryBuffer is the number of blocks back that we'll go when
165
        // asking the remote peer for their any channels they know of beyond
166
        // our highest known channel ID.
167
        chanRangeQueryBuffer = 144
168

169
        // syncTransitionTimeout is the default timeout in which we'll wait up
170
        // to when attempting to perform a sync transition.
171
        syncTransitionTimeout = 5 * time.Second
172

173
        // requestBatchSize is the maximum number of channels we will query the
174
        // remote peer for in a QueryShortChanIDs message.
175
        requestBatchSize = 500
176

177
        // syncerBufferSize is the size of the syncer's buffers.
178
        syncerBufferSize = 5
179
)
180

181
var (
182
        // encodingTypeToChunkSize maps an encoding type, to the max number of
183
        // short chan ID's using the encoding type that we can fit into a
184
        // single message safely.
185
        encodingTypeToChunkSize = map[lnwire.QueryEncoding]int32{
186
                lnwire.EncodingSortedPlain: 8000,
187
        }
188

189
        // ErrGossipSyncerExiting signals that the syncer has been killed.
190
        ErrGossipSyncerExiting = errors.New("gossip syncer exiting")
191

192
        // ErrSyncTransitionTimeout is an error returned when we've timed out
193
        // attempting to perform a sync transition.
194
        ErrSyncTransitionTimeout = errors.New("timed out attempting to " +
195
                "transition sync type")
196

197
        // zeroTimestamp is the timestamp we'll use when we want to indicate to
198
        // peers that we do not want to receive any new graph updates.
199
        zeroTimestamp time.Time
200
)
201

202
// syncTransitionReq encapsulates a request for a gossip syncer sync transition.
203
type syncTransitionReq struct {
204
        newSyncType SyncerType
205
        errChan     chan error
206
}
207

208
// historicalSyncReq encapsulates a request for a gossip syncer to perform a
209
// historical sync.
210
type historicalSyncReq struct {
211
        // doneChan is a channel that serves as a signal and is closed to ensure
212
        // the historical sync is attempted by the time we return to the caller.
213
        doneChan chan struct{}
214
}
215

216
// gossipSyncerCfg is a struct that packages all the information a GossipSyncer
217
// needs to carry out its duties.
218
type gossipSyncerCfg struct {
219
        // chainHash is the chain that this syncer is responsible for.
220
        chainHash chainhash.Hash
221

222
        // peerPub is the public key of the peer we're syncing with, serialized
223
        // in compressed format.
224
        peerPub [33]byte
225

226
        // channelSeries is the primary interface that we'll use to generate
227
        // our queries and respond to the queries of the remote peer.
228
        channelSeries ChannelGraphTimeSeries
229

230
        // encodingType is the current encoding type we're aware of. Requests
231
        // with different encoding types will be rejected.
232
        encodingType lnwire.QueryEncoding
233

234
        // chunkSize is the max number of short chan IDs using the syncer's
235
        // encoding type that we can fit into a single message safely.
236
        chunkSize int32
237

238
        // batchSize is the max number of channels the syncer will query from
239
        // the remote node in a single QueryShortChanIDs request.
240
        batchSize int32
241

242
        // sendToPeer sends a variadic number of messages to the remote peer.
243
        // This method should not block while waiting for sends to be written
244
        // to the wire.
245
        sendToPeer func(context.Context, ...lnwire.Message) error
246

247
        // sendToPeerSync sends a variadic number of messages to the remote
248
        // peer, blocking until all messages have been sent successfully or a
249
        // write error is encountered.
250
        sendToPeerSync func(context.Context, ...lnwire.Message) error
251

252
        // noSyncChannels will prevent the GossipSyncer from spawning a
253
        // channelGraphSyncer, meaning we will not try to reconcile unknown
254
        // channels with the remote peer.
255
        noSyncChannels bool
256

257
        // noReplyQueries will prevent the GossipSyncer from spawning a
258
        // replyHandler, meaning we will not reply to queries from our remote
259
        // peer.
260
        noReplyQueries bool
261

262
        // noTimestampQueryOption will prevent the GossipSyncer from querying
263
        // timestamps of announcement messages from the peer, and it will
264
        // prevent it from responding to timestamp queries.
265
        noTimestampQueryOption bool
266

267
        // ignoreHistoricalFilters will prevent syncers from replying with
268
        // historical data when the remote peer sets a gossip_timestamp_range.
269
        // This prevents ranges with old start times from causing us to dump the
270
        // graph on connect.
271
        ignoreHistoricalFilters bool
272

273
        // bestHeight returns the latest height known of the chain.
274
        bestHeight func() uint32
275

276
        // markGraphSynced updates the SyncManager's perception of whether we
277
        // have completed at least one historical sync.
278
        markGraphSynced func()
279

280
        // maxQueryChanRangeReplies is the maximum number of replies we'll allow
281
        // for a single QueryChannelRange request.
282
        maxQueryChanRangeReplies uint32
283

284
        // isStillZombieChannel takes the timestamps of the latest channel
285
        // updates for a channel and returns true if the channel should be
286
        // considered a zombie based on these timestamps.
287
        isStillZombieChannel func(time.Time, time.Time) bool
288
}
289

290
// GossipSyncer is a struct that handles synchronizing the channel graph state
291
// with a remote peer. The GossipSyncer implements a state machine that will
292
// progressively ensure we're synchronized with the channel state of the remote
293
// node. Once both nodes have been synchronized, we'll use an update filter to
294
// filter out which messages should be sent to a remote peer based on their
295
// update horizon. If the update horizon isn't specified, then we won't send
296
// them any channel updates at all.
297
type GossipSyncer struct {
298
        started sync.Once
299
        stopped sync.Once
300

301
        // state is the current state of the GossipSyncer.
302
        //
303
        // NOTE: This variable MUST be used atomically.
304
        state uint32
305

306
        // syncType denotes the SyncerType the gossip syncer is currently
307
        // exercising.
308
        //
309
        // NOTE: This variable MUST be used atomically.
310
        syncType uint32
311

312
        // remoteUpdateHorizon is the update horizon of the remote peer. We'll
313
        // use this to properly filter out any messages.
314
        remoteUpdateHorizon *lnwire.GossipTimestampRange
315

316
        // localUpdateHorizon is our local update horizon, we'll use this to
317
        // determine if we've already sent out our update.
318
        localUpdateHorizon *lnwire.GossipTimestampRange
319

320
        // syncTransitions is a channel through which new sync type transition
321
        // requests will be sent through. These requests should only be handled
322
        // when the gossip syncer is in a chansSynced state to ensure its state
323
        // machine behaves as expected.
324
        syncTransitionReqs chan *syncTransitionReq
325

326
        // historicalSyncReqs is a channel that serves as a signal for the
327
        // gossip syncer to perform a historical sync. These can only be done
328
        // once the gossip syncer is in a chansSynced state to ensure its state
329
        // machine behaves as expected.
330
        historicalSyncReqs chan *historicalSyncReq
331

332
        // genHistoricalChanRangeQuery when true signals to the gossip syncer
333
        // that it should request the remote peer for all of its known channel
334
        // IDs starting from the genesis block of the chain. This can only
335
        // happen if the gossip syncer receives a request to attempt a
336
        // historical sync. It can be unset if the syncer ever transitions from
337
        // PassiveSync to ActiveSync.
338
        genHistoricalChanRangeQuery bool
339

340
        // gossipMsgs is a channel that all responses to our queries from the
341
        // target peer will be sent over, these will be read by the
342
        // channelGraphSyncer.
343
        gossipMsgs chan lnwire.Message
344

345
        // queryMsgs is a channel that all queries from the remote peer will be
346
        // received over, these will be read by the replyHandler.
347
        queryMsgs chan lnwire.Message
348

349
        // curQueryRangeMsg keeps track of the latest QueryChannelRange message
350
        // we've sent to a peer to ensure we've consumed all expected replies.
351
        // This field is primarily used within the waitingQueryChanReply state.
352
        curQueryRangeMsg *lnwire.QueryChannelRange
353

354
        // prevReplyChannelRange keeps track of the previous ReplyChannelRange
355
        // message we've received from a peer to ensure they've fully replied to
356
        // our query by ensuring they covered our requested block range. This
357
        // field is primarily used within the waitingQueryChanReply state.
358
        prevReplyChannelRange *lnwire.ReplyChannelRange
359

360
        // bufferedChanRangeReplies is used in the waitingQueryChanReply to
361
        // buffer all the chunked response to our query.
362
        bufferedChanRangeReplies []graphdb.ChannelUpdateInfo
363

364
        // numChanRangeRepliesRcvd is used to track the number of replies
365
        // received as part of a QueryChannelRange. This field is primarily used
366
        // within the waitingQueryChanReply state.
367
        numChanRangeRepliesRcvd uint32
368

369
        // newChansToQuery is used to pass the set of channels we should query
370
        // for from the waitingQueryChanReply state to the queryNewChannels
371
        // state.
372
        newChansToQuery []lnwire.ShortChannelID
373

374
        cfg gossipSyncerCfg
375

376
        // syncedSignal is a channel that, if set, will be closed when the
377
        // GossipSyncer reaches its terminal chansSynced state.
378
        syncedSignal chan struct{}
379

380
        // syncerSema is used to more finely control the syncer's ability to
381
        // respond to gossip timestamp range messages.
382
        syncerSema chan struct{}
383

384
        sync.Mutex
385

386
        // cg is a helper that encapsulates a wait group and quit channel and
387
        // allows contexts that either block or cancel on those depending on
388
        // the use case.
389
        cg *fn.ContextGuard
390
}
391

392
// newGossipSyncer returns a new instance of the GossipSyncer populated using
393
// the passed config.
394
func newGossipSyncer(cfg gossipSyncerCfg, sema chan struct{}) *GossipSyncer {
49✔
395
        return &GossipSyncer{
49✔
396
                cfg:                cfg,
49✔
397
                syncTransitionReqs: make(chan *syncTransitionReq),
49✔
398
                historicalSyncReqs: make(chan *historicalSyncReq),
49✔
399
                gossipMsgs:         make(chan lnwire.Message, syncerBufferSize),
49✔
400
                queryMsgs:          make(chan lnwire.Message, syncerBufferSize),
49✔
401
                syncerSema:         sema,
49✔
402
                cg:                 fn.NewContextGuard(),
49✔
403
        }
49✔
404
}
49✔
405

406
// Start starts the GossipSyncer and any goroutines that it needs to carry out
407
// its duties.
408
func (g *GossipSyncer) Start() {
35✔
409
        g.started.Do(func() {
70✔
410
                log.Debugf("Starting GossipSyncer(%x)", g.cfg.peerPub[:])
35✔
411

35✔
412
                // TODO(conner): only spawn channelGraphSyncer if remote
35✔
413
                // supports gossip queries, and only spawn replyHandler if we
35✔
414
                // advertise support
35✔
415
                if !g.cfg.noSyncChannels {
69✔
416
                        g.cg.WgAdd(1)
34✔
417
                        go g.channelGraphSyncer()
34✔
418
                }
34✔
419
                if !g.cfg.noReplyQueries {
69✔
420
                        g.cg.WgAdd(1)
34✔
421
                        go g.replyHandler()
34✔
422
                }
34✔
423
        })
424
}
425

426
// Stop signals the GossipSyncer for a graceful exit, then waits until it has
427
// exited.
428
func (g *GossipSyncer) Stop() {
32✔
429
        g.stopped.Do(func() {
64✔
430
                log.Debugf("Stopping GossipSyncer(%x)", g.cfg.peerPub[:])
32✔
431
                defer log.Debugf("GossipSyncer(%x) stopped", g.cfg.peerPub[:])
32✔
432

32✔
433
                g.cg.Quit()
32✔
434
        })
32✔
435
}
436

437
// handleSyncingChans handles the state syncingChans for the GossipSyncer. When
438
// in this state, we will send a QueryChannelRange msg to our peer and advance
439
// the syncer's state to waitingQueryRangeReply.
440
func (g *GossipSyncer) handleSyncingChans() {
20✔
441
        // Prepare the query msg.
20✔
442
        queryRangeMsg, err := g.genChanRangeQuery(g.genHistoricalChanRangeQuery)
20✔
443
        if err != nil {
20✔
444
                log.Errorf("Unable to gen chan range query: %v", err)
×
445
                return
×
446
        }
×
447

448
        // Acquire a lock so the following state transition is atomic.
449
        //
450
        // NOTE: We must lock the following steps as it's possible we get an
451
        // immediate response (ReplyChannelRange) after sending the query msg.
452
        // The response is handled in ProcessQueryMsg, which requires the
453
        // current state to be waitingQueryRangeReply.
454
        g.Lock()
20✔
455
        defer g.Unlock()
20✔
456

20✔
457
        // Send the msg to the remote peer, which is non-blocking as
20✔
458
        // `sendToPeer` only queues the msg in Brontide.
20✔
459
        ctx, _ := g.cg.Create(context.Background())
20✔
460
        err = g.cfg.sendToPeer(ctx, queryRangeMsg)
20✔
461
        if err != nil {
20✔
462
                log.Errorf("Unable to send chan range query: %v", err)
×
463
                return
×
464
        }
×
465

466
        // With the message sent successfully, we'll transition into the next
467
        // state where we wait for their reply.
468
        g.setSyncState(waitingQueryRangeReply)
20✔
469
}
470

471
// channelGraphSyncer is the main goroutine responsible for ensuring that we
472
// properly channel graph state with the remote peer, and also that we only
473
// send them messages which actually pass their defined update horizon.
474
func (g *GossipSyncer) channelGraphSyncer() {
34✔
475
        defer g.cg.WgDone()
34✔
476

34✔
477
        for {
240✔
478
                state := g.syncState()
206✔
479
                syncType := g.SyncType()
206✔
480

206✔
481
                log.Debugf("GossipSyncer(%x): state=%v, type=%v",
206✔
482
                        g.cfg.peerPub[:], state, syncType)
206✔
483

206✔
484
                switch state {
206✔
485
                // When we're in this state, we're trying to synchronize our
486
                // view of the network with the remote peer. We'll kick off
487
                // this sync by asking them for the set of channels they
488
                // understand, as we'll as responding to any other queries by
489
                // them.
490
                case syncingChans:
20✔
491
                        g.handleSyncingChans()
20✔
492

493
                // In this state, we've sent out our initial channel range
494
                // query and are waiting for the final response from the remote
495
                // peer before we perform a diff to see with channels they know
496
                // of that we don't.
497
                case waitingQueryRangeReply:
122✔
498
                        // We'll wait to either process a new message from the
122✔
499
                        // remote party, or exit due to the gossiper exiting,
122✔
500
                        // or us being signalled to do so.
122✔
501
                        select {
122✔
502
                        case msg := <-g.gossipMsgs:
117✔
503
                                // The remote peer is sending a response to our
117✔
504
                                // initial query, we'll collate this response,
117✔
505
                                // and see if it's the final one in the series.
117✔
506
                                // If so, we can then transition to querying
117✔
507
                                // for the new channels.
117✔
508
                                queryReply, ok := msg.(*lnwire.ReplyChannelRange)
117✔
509
                                if ok {
234✔
510
                                        err := g.processChanRangeReply(queryReply)
117✔
511
                                        if err != nil {
117✔
512
                                                log.Errorf("Unable to "+
×
513
                                                        "process chan range "+
×
514
                                                        "query: %v", err)
×
515
                                                return
×
516
                                        }
×
517
                                        continue
117✔
518
                                }
519

520
                                log.Warnf("Unexpected message: %T in state=%v",
×
521
                                        msg, state)
×
522

523
                        case <-g.cg.Done():
5✔
524
                                return
5✔
525
                        }
526

527
                // We'll enter this state once we've discovered which channels
528
                // the remote party knows of that we don't yet know of
529
                // ourselves.
530
                case queryNewChannels:
3✔
531
                        // First, we'll attempt to continue our channel
3✔
532
                        // synchronization by continuing to send off another
3✔
533
                        // query chunk.
3✔
534
                        done := g.synchronizeChanIDs()
3✔
535

3✔
536
                        // If this wasn't our last query, then we'll need to
3✔
537
                        // transition to our waiting state.
3✔
538
                        if !done {
5✔
539
                                continue
2✔
540
                        }
541

542
                        // If we're fully synchronized, then we can transition
543
                        // to our terminal state.
544
                        g.setSyncState(chansSynced)
1✔
545

1✔
546
                        // Ensure that the sync manager becomes aware that the
1✔
547
                        // historical sync completed so synced_to_graph is
1✔
548
                        // updated over rpc.
1✔
549
                        g.cfg.markGraphSynced()
1✔
550

551
                // In this state, we've just sent off a new query for channels
552
                // that we don't yet know of. We'll remain in this state until
553
                // the remote party signals they've responded to our query in
554
                // totality.
555
                case waitingQueryChanReply:
2✔
556
                        // Once we've sent off our query, we'll wait for either
2✔
557
                        // an ending reply, or just another query from the
2✔
558
                        // remote peer.
2✔
559
                        select {
2✔
560
                        case msg := <-g.gossipMsgs:
2✔
561
                                // If this is the final reply to one of our
2✔
562
                                // queries, then we'll loop back into our query
2✔
563
                                // state to send of the remaining query chunks.
2✔
564
                                _, ok := msg.(*lnwire.ReplyShortChanIDsEnd)
2✔
565
                                if ok {
4✔
566
                                        g.setSyncState(queryNewChannels)
2✔
567
                                        continue
2✔
568
                                }
569

570
                                log.Warnf("Unexpected message: %T in state=%v",
×
571
                                        msg, state)
×
572

573
                        case <-g.cg.Done():
×
574
                                return
×
575
                        }
576

577
                // This is our final terminal state where we'll only reply to
578
                // any further queries by the remote peer.
579
                case chansSynced:
56✔
580
                        g.Lock()
56✔
581
                        if g.syncedSignal != nil {
64✔
582
                                close(g.syncedSignal)
8✔
583
                                g.syncedSignal = nil
8✔
584
                        }
8✔
585
                        g.Unlock()
56✔
586

56✔
587
                        // If we haven't yet sent out our update horizon, and
56✔
588
                        // we want to receive real-time channel updates, we'll
56✔
589
                        // do so now.
56✔
590
                        if g.localUpdateHorizon == nil &&
56✔
591
                                syncType.IsActiveSync() {
70✔
592

14✔
593
                                err := g.sendGossipTimestampRange(
14✔
594
                                        time.Now(), math.MaxUint32,
14✔
595
                                )
14✔
596
                                if err != nil {
14✔
597
                                        log.Errorf("Unable to send update "+
×
598
                                                "horizon to %x: %v",
×
599
                                                g.cfg.peerPub, err)
×
600
                                }
×
601
                        }
602
                        // With our horizon set, we'll simply reply to any new
603
                        // messages or process any state transitions and exit if
604
                        // needed.
605
                        fallthrough
56✔
606

607
                // Pinned peers will begin in this state, since they will
608
                // immediately receive a request to perform a historical sync.
609
                // Otherwise, we fall through after ending in chansSynced to
610
                // facilitate new requests.
611
                case syncerIdle:
59✔
612
                        select {
59✔
613
                        case req := <-g.syncTransitionReqs:
14✔
614
                                req.errChan <- g.handleSyncTransition(req)
14✔
615

616
                        case req := <-g.historicalSyncReqs:
16✔
617
                                g.handleHistoricalSync(req)
16✔
618

619
                        case <-g.cg.Done():
26✔
620
                                return
26✔
621
                        }
622
                }
623
        }
624
}
625

626
// replyHandler is an event loop whose sole purpose is to reply to the remote
627
// peers queries. Our replyHandler will respond to messages generated by their
628
// channelGraphSyncer, and vice versa. Each party's channelGraphSyncer drives
629
// the other's replyHandler, allowing the replyHandler to operate independently
630
// from the state machine maintained on the same node.
631
//
632
// NOTE: This method MUST be run as a goroutine.
633
func (g *GossipSyncer) replyHandler() {
34✔
634
        defer g.cg.WgDone()
34✔
635

34✔
636
        for {
73✔
637
                select {
39✔
638
                case msg := <-g.queryMsgs:
5✔
639
                        err := g.replyPeerQueries(msg)
5✔
640
                        switch {
5✔
641
                        case err == ErrGossipSyncerExiting:
×
642
                                return
×
643

644
                        case err == lnpeer.ErrPeerExiting:
×
645
                                return
×
646

647
                        case err != nil:
×
648
                                log.Errorf("Unable to reply to peer "+
×
649
                                        "query: %v", err)
×
650
                        }
651

652
                case <-g.cg.Done():
31✔
653
                        return
31✔
654
                }
655
        }
656
}
657

658
// sendGossipTimestampRange constructs and sets a GossipTimestampRange for the
659
// syncer and sends it to the remote peer.
660
func (g *GossipSyncer) sendGossipTimestampRange(firstTimestamp time.Time,
661
        timestampRange uint32) error {
28✔
662

28✔
663
        endTimestamp := firstTimestamp.Add(
28✔
664
                time.Duration(timestampRange) * time.Second,
28✔
665
        )
28✔
666

28✔
667
        log.Infof("GossipSyncer(%x): applying gossipFilter(start=%v, end=%v)",
28✔
668
                g.cfg.peerPub[:], firstTimestamp, endTimestamp)
28✔
669

28✔
670
        localUpdateHorizon := &lnwire.GossipTimestampRange{
28✔
671
                ChainHash:      g.cfg.chainHash,
28✔
672
                FirstTimestamp: uint32(firstTimestamp.Unix()),
28✔
673
                TimestampRange: timestampRange,
28✔
674
        }
28✔
675

28✔
676
        ctx, _ := g.cg.Create(context.Background())
28✔
677
        if err := g.cfg.sendToPeer(ctx, localUpdateHorizon); err != nil {
28✔
678
                return err
×
679
        }
×
680

681
        if firstTimestamp == zeroTimestamp && timestampRange == 0 {
30✔
682
                g.localUpdateHorizon = nil
2✔
683
        } else {
28✔
684
                g.localUpdateHorizon = localUpdateHorizon
26✔
685
        }
26✔
686

687
        return nil
28✔
688
}
689

690
// synchronizeChanIDs is called by the channelGraphSyncer when we need to query
691
// the remote peer for its known set of channel IDs within a particular block
692
// range. This method will be called continually until the entire range has
693
// been queried for with a response received. We'll chunk our requests as
694
// required to ensure they fit into a single message. We may re-renter this
695
// state in the case that chunking is required.
696
func (g *GossipSyncer) synchronizeChanIDs() bool {
6✔
697
        // If we're in this state yet there are no more new channels to query
6✔
698
        // for, then we'll transition to our final synced state and return true
6✔
699
        // to signal that we're fully synchronized.
6✔
700
        if len(g.newChansToQuery) == 0 {
7✔
701
                log.Infof("GossipSyncer(%x): no more chans to query",
1✔
702
                        g.cfg.peerPub[:])
1✔
703

1✔
704
                return true
1✔
705
        }
1✔
706

707
        // Otherwise, we'll issue our next chunked query to receive replies
708
        // for.
709
        var queryChunk []lnwire.ShortChannelID
5✔
710

5✔
711
        // If the number of channels to query for is less than the chunk size,
5✔
712
        // then we can issue a single query.
5✔
713
        if int32(len(g.newChansToQuery)) < g.cfg.batchSize {
7✔
714
                queryChunk = g.newChansToQuery
2✔
715
                g.newChansToQuery = nil
2✔
716

2✔
717
        } else {
5✔
718
                // Otherwise, we'll need to only query for the next chunk.
3✔
719
                // We'll slice into our query chunk, then slide down our main
3✔
720
                // pointer down by the chunk size.
3✔
721
                queryChunk = g.newChansToQuery[:g.cfg.batchSize]
3✔
722
                g.newChansToQuery = g.newChansToQuery[g.cfg.batchSize:]
3✔
723
        }
3✔
724

725
        log.Infof("GossipSyncer(%x): querying for %v new channels",
5✔
726
                g.cfg.peerPub[:], len(queryChunk))
5✔
727

5✔
728
        // Change the state before sending the query msg.
5✔
729
        g.setSyncState(waitingQueryChanReply)
5✔
730

5✔
731
        // With our chunk obtained, we'll send over our next query, then return
5✔
732
        // false indicating that we're net yet fully synced.
5✔
733
        ctx, _ := g.cg.Create(context.Background())
5✔
734
        err := g.cfg.sendToPeer(ctx, &lnwire.QueryShortChanIDs{
5✔
735
                ChainHash:    g.cfg.chainHash,
5✔
736
                EncodingType: lnwire.EncodingSortedPlain,
5✔
737
                ShortChanIDs: queryChunk,
5✔
738
        })
5✔
739
        if err != nil {
5✔
740
                log.Errorf("Unable to sync chan IDs: %v", err)
×
741
        }
×
742

743
        return false
5✔
744
}
745

746
// isLegacyReplyChannelRange determines where a ReplyChannelRange message is
747
// considered legacy. There was a point where lnd used to include the same query
748
// over multiple replies, rather than including the portion of the query the
749
// reply is handling. We'll use this as a way of detecting whether we are
750
// communicating with a legacy node so we can properly sync with them.
751
func isLegacyReplyChannelRange(query *lnwire.QueryChannelRange,
752
        reply *lnwire.ReplyChannelRange) bool {
250✔
753

250✔
754
        return (reply.ChainHash == query.ChainHash &&
250✔
755
                reply.FirstBlockHeight == query.FirstBlockHeight &&
250✔
756
                reply.NumBlocks == query.NumBlocks)
250✔
757
}
250✔
758

759
// processChanRangeReply is called each time the GossipSyncer receives a new
760
// reply to the initial range query to discover new channels that it didn't
761
// previously know of.
762
func (g *GossipSyncer) processChanRangeReply(msg *lnwire.ReplyChannelRange) error {
125✔
763
        // isStale returns whether the timestamp is too far into the past.
125✔
764
        isStale := func(timestamp time.Time) bool {
155✔
765
                return time.Since(timestamp) > graph.DefaultChannelPruneExpiry
30✔
766
        }
30✔
767

768
        // isSkewed returns whether the timestamp is too far into the future.
769
        isSkewed := func(timestamp time.Time) bool {
145✔
770
                return time.Until(timestamp) > graph.DefaultChannelPruneExpiry
20✔
771
        }
20✔
772

773
        // If we're not communicating with a legacy node, we'll apply some
774
        // further constraints on their reply to ensure it satisfies our query.
775
        if !isLegacyReplyChannelRange(g.curQueryRangeMsg, msg) {
235✔
776
                // The first block should be within our original request.
110✔
777
                if msg.FirstBlockHeight < g.curQueryRangeMsg.FirstBlockHeight {
110✔
778
                        return fmt.Errorf("reply includes channels for height "+
×
779
                                "%v prior to query %v", msg.FirstBlockHeight,
×
780
                                g.curQueryRangeMsg.FirstBlockHeight)
×
781
                }
×
782

783
                // The last block should also be. We don't need to check the
784
                // intermediate ones because they should already be in sorted
785
                // order.
786
                replyLastHeight := msg.LastBlockHeight()
110✔
787
                queryLastHeight := g.curQueryRangeMsg.LastBlockHeight()
110✔
788
                if replyLastHeight > queryLastHeight {
110✔
789
                        return fmt.Errorf("reply includes channels for height "+
×
790
                                "%v after query %v", replyLastHeight,
×
791
                                queryLastHeight)
×
792
                }
×
793

794
                // If we've previously received a reply for this query, look at
795
                // its last block to ensure the current reply properly follows
796
                // it.
797
                if g.prevReplyChannelRange != nil {
215✔
798
                        prevReply := g.prevReplyChannelRange
105✔
799
                        prevReplyLastHeight := prevReply.LastBlockHeight()
105✔
800

105✔
801
                        // The current reply can either start from the previous
105✔
802
                        // reply's last block, if there are still more channels
105✔
803
                        // for the same block, or the block after.
105✔
804
                        if msg.FirstBlockHeight != prevReplyLastHeight &&
105✔
805
                                msg.FirstBlockHeight != prevReplyLastHeight+1 {
105✔
806

×
807
                                return fmt.Errorf("first block of reply %v "+
×
808
                                        "does not continue from last block of "+
×
809
                                        "previous %v", msg.FirstBlockHeight,
×
810
                                        prevReplyLastHeight)
×
811
                        }
×
812
                }
813
        }
814

815
        g.prevReplyChannelRange = msg
125✔
816

125✔
817
        for i, scid := range msg.ShortChanIDs {
252✔
818
                info := graphdb.NewChannelUpdateInfo(
127✔
819
                        scid, time.Time{}, time.Time{},
127✔
820
                )
127✔
821

127✔
822
                if len(msg.Timestamps) != 0 {
139✔
823
                        t1 := time.Unix(int64(msg.Timestamps[i].Timestamp1), 0)
12✔
824
                        info.Node1UpdateTimestamp = t1
12✔
825

12✔
826
                        t2 := time.Unix(int64(msg.Timestamps[i].Timestamp2), 0)
12✔
827
                        info.Node2UpdateTimestamp = t2
12✔
828

12✔
829
                        // Sort out all channels with outdated or skewed
12✔
830
                        // timestamps. Both timestamps need to be out of
12✔
831
                        // boundaries for us to skip the channel and not query
12✔
832
                        // it later on.
12✔
833
                        switch {
12✔
834
                        case isStale(info.Node1UpdateTimestamp) &&
835
                                isStale(info.Node2UpdateTimestamp):
2✔
836

2✔
837
                                continue
2✔
838

839
                        case isSkewed(info.Node1UpdateTimestamp) &&
840
                                isSkewed(info.Node2UpdateTimestamp):
2✔
841

2✔
842
                                continue
2✔
843

844
                        case isStale(info.Node1UpdateTimestamp) &&
845
                                isSkewed(info.Node2UpdateTimestamp):
2✔
846

2✔
847
                                continue
2✔
848

849
                        case isStale(info.Node2UpdateTimestamp) &&
850
                                isSkewed(info.Node1UpdateTimestamp):
2✔
851

2✔
852
                                continue
2✔
853
                        }
854
                }
855

856
                g.bufferedChanRangeReplies = append(
119✔
857
                        g.bufferedChanRangeReplies, info,
119✔
858
                )
119✔
859
        }
860

861
        switch g.cfg.encodingType {
125✔
862
        case lnwire.EncodingSortedPlain:
125✔
863
                g.numChanRangeRepliesRcvd++
125✔
864
        case lnwire.EncodingSortedZlib:
×
865
                g.numChanRangeRepliesRcvd += maxQueryChanRangeRepliesZlibFactor
×
866
        default:
×
867
                return fmt.Errorf("unhandled encoding type %v", g.cfg.encodingType)
×
868
        }
869

870
        log.Infof("GossipSyncer(%x): buffering chan range reply of size=%v",
125✔
871
                g.cfg.peerPub[:], len(msg.ShortChanIDs))
125✔
872

125✔
873
        // If this isn't the last response and we can continue to receive more,
125✔
874
        // then we can exit as we've already buffered the latest portion of the
125✔
875
        // streaming reply.
125✔
876
        maxReplies := g.cfg.maxQueryChanRangeReplies
125✔
877
        switch {
125✔
878
        // If we're communicating with a legacy node, we'll need to look at the
879
        // complete field.
880
        case isLegacyReplyChannelRange(g.curQueryRangeMsg, msg):
15✔
881
                if msg.Complete == 0 && g.numChanRangeRepliesRcvd < maxReplies {
18✔
882
                        return nil
3✔
883
                }
3✔
884

885
        // Otherwise, we'll look at the reply's height range.
886
        default:
110✔
887
                replyLastHeight := msg.LastBlockHeight()
110✔
888
                queryLastHeight := g.curQueryRangeMsg.LastBlockHeight()
110✔
889

110✔
890
                // TODO(wilmer): This might require some padding if the remote
110✔
891
                // node is not aware of the last height we sent them, i.e., is
110✔
892
                // behind a few blocks from us.
110✔
893
                if replyLastHeight < queryLastHeight &&
110✔
894
                        g.numChanRangeRepliesRcvd < maxReplies {
215✔
895

105✔
896
                        return nil
105✔
897
                }
105✔
898
        }
899

900
        log.Infof("GossipSyncer(%x): filtering through %v chans",
17✔
901
                g.cfg.peerPub[:], len(g.bufferedChanRangeReplies))
17✔
902

17✔
903
        // Otherwise, this is the final response, so we'll now check to see
17✔
904
        // which channels they know of that we don't.
17✔
905
        newChans, err := g.cfg.channelSeries.FilterKnownChanIDs(
17✔
906
                g.cfg.chainHash, g.bufferedChanRangeReplies,
17✔
907
                g.cfg.isStillZombieChannel,
17✔
908
        )
17✔
909
        if err != nil {
17✔
910
                return fmt.Errorf("unable to filter chan ids: %w", err)
×
911
        }
×
912

913
        // As we've received the entirety of the reply, we no longer need to
914
        // hold on to the set of buffered replies or the original query that
915
        // prompted the replies, so we'll let that be garbage collected now.
916
        g.curQueryRangeMsg = nil
17✔
917
        g.prevReplyChannelRange = nil
17✔
918
        g.bufferedChanRangeReplies = nil
17✔
919
        g.numChanRangeRepliesRcvd = 0
17✔
920

17✔
921
        // If there aren't any channels that we don't know of, then we can
17✔
922
        // switch straight to our terminal state.
17✔
923
        if len(newChans) == 0 {
31✔
924
                log.Infof("GossipSyncer(%x): remote peer has no new chans",
14✔
925
                        g.cfg.peerPub[:])
14✔
926

14✔
927
                g.setSyncState(chansSynced)
14✔
928

14✔
929
                // Ensure that the sync manager becomes aware that the
14✔
930
                // historical sync completed so synced_to_graph is updated over
14✔
931
                // rpc.
14✔
932
                g.cfg.markGraphSynced()
14✔
933
                return nil
14✔
934
        }
14✔
935

936
        // Otherwise, we'll set the set of channels that we need to query for
937
        // the next state, and also transition our state.
938
        g.newChansToQuery = newChans
3✔
939
        g.setSyncState(queryNewChannels)
3✔
940

3✔
941
        log.Infof("GossipSyncer(%x): starting query for %v new chans",
3✔
942
                g.cfg.peerPub[:], len(newChans))
3✔
943

3✔
944
        return nil
3✔
945
}
946

947
// genChanRangeQuery generates the initial message we'll send to the remote
948
// party when we're kicking off the channel graph synchronization upon
949
// connection. The historicalQuery boolean can be used to generate a query from
950
// the genesis block of the chain.
951
func (g *GossipSyncer) genChanRangeQuery(
952
        historicalQuery bool) (*lnwire.QueryChannelRange, error) {
24✔
953

24✔
954
        // First, we'll query our channel graph time series for its highest
24✔
955
        // known channel ID.
24✔
956
        newestChan, err := g.cfg.channelSeries.HighestChanID(g.cfg.chainHash)
24✔
957
        if err != nil {
24✔
958
                return nil, err
×
959
        }
×
960

961
        // Once we have the chan ID of the newest, we'll obtain the block height
962
        // of the channel, then subtract our default horizon to ensure we don't
963
        // miss any channels. By default, we go back 1 day from the newest
964
        // channel, unless we're attempting a historical sync, where we'll
965
        // actually start from the genesis block instead.
966
        var startHeight uint32
24✔
967
        switch {
24✔
968
        case historicalQuery:
19✔
969
                fallthrough
19✔
970
        case newestChan.BlockHeight <= chanRangeQueryBuffer:
19✔
971
                startHeight = 0
19✔
972
        default:
5✔
973
                startHeight = newestChan.BlockHeight - chanRangeQueryBuffer
5✔
974
        }
975

976
        // Determine the number of blocks to request based on our best height.
977
        // We'll take into account any potential underflows and explicitly set
978
        // numBlocks to its minimum value of 1 if so.
979
        bestHeight := g.cfg.bestHeight()
24✔
980
        numBlocks := bestHeight - startHeight
24✔
981
        if int64(numBlocks) < 1 {
24✔
982
                numBlocks = 1
×
983
        }
×
984

985
        log.Infof("GossipSyncer(%x): requesting new chans from height=%v "+
24✔
986
                "and %v blocks after", g.cfg.peerPub[:], startHeight, numBlocks)
24✔
987

24✔
988
        // Finally, we'll craft the channel range query, using our starting
24✔
989
        // height, then asking for all known channels to the foreseeable end of
24✔
990
        // the main chain.
24✔
991
        query := &lnwire.QueryChannelRange{
24✔
992
                ChainHash:        g.cfg.chainHash,
24✔
993
                FirstBlockHeight: startHeight,
24✔
994
                NumBlocks:        numBlocks,
24✔
995
        }
24✔
996

24✔
997
        if !g.cfg.noTimestampQueryOption {
40✔
998
                query.QueryOptions = lnwire.NewTimestampQueryOption()
16✔
999
        }
16✔
1000

1001
        g.curQueryRangeMsg = query
24✔
1002

24✔
1003
        return query, nil
24✔
1004
}
1005

1006
// replyPeerQueries is called in response to any query by the remote peer.
1007
// We'll examine our state and send back our best response.
1008
func (g *GossipSyncer) replyPeerQueries(msg lnwire.Message) error {
5✔
1009
        switch msg := msg.(type) {
5✔
1010

1011
        // In this state, we'll also handle any incoming channel range queries
1012
        // from the remote peer as they're trying to sync their state as well.
1013
        case *lnwire.QueryChannelRange:
3✔
1014
                return g.replyChanRangeQuery(msg)
3✔
1015

1016
        // If the remote peer skips straight to requesting new channels that
1017
        // they don't know of, then we'll ensure that we also handle this case.
1018
        case *lnwire.QueryShortChanIDs:
2✔
1019
                return g.replyShortChanIDs(msg)
2✔
1020

1021
        default:
×
1022
                return fmt.Errorf("unknown message: %T", msg)
×
1023
        }
1024
}
1025

1026
// replyChanRangeQuery will be dispatched in response to a channel range query
1027
// by the remote node. We'll query the channel time series for channels that
1028
// meet the channel range, then chunk our responses to the remote node. We also
1029
// ensure that our final fragment carries the "complete" bit to indicate the
1030
// end of our streaming response.
1031
func (g *GossipSyncer) replyChanRangeQuery(query *lnwire.QueryChannelRange) error {
9✔
1032
        // Before responding, we'll check to ensure that the remote peer is
9✔
1033
        // querying for the same chain that we're on. If not, we'll send back a
9✔
1034
        // response with a complete value of zero to indicate we're on a
9✔
1035
        // different chain.
9✔
1036
        if g.cfg.chainHash != query.ChainHash {
10✔
1037
                log.Warnf("Remote peer requested QueryChannelRange for "+
1✔
1038
                        "chain=%v, we're on chain=%v", query.ChainHash,
1✔
1039
                        g.cfg.chainHash)
1✔
1040

1✔
1041
                ctx, _ := g.cg.Create(context.Background())
1✔
1042

1✔
1043
                return g.cfg.sendToPeerSync(ctx, &lnwire.ReplyChannelRange{
1✔
1044
                        ChainHash:        query.ChainHash,
1✔
1045
                        FirstBlockHeight: query.FirstBlockHeight,
1✔
1046
                        NumBlocks:        query.NumBlocks,
1✔
1047
                        Complete:         0,
1✔
1048
                        EncodingType:     g.cfg.encodingType,
1✔
1049
                        ShortChanIDs:     nil,
1✔
1050
                })
1✔
1051
        }
1✔
1052

1053
        log.Infof("GossipSyncer(%x): filtering chan range: start_height=%v, "+
8✔
1054
                "num_blocks=%v", g.cfg.peerPub[:], query.FirstBlockHeight,
8✔
1055
                query.NumBlocks)
8✔
1056

8✔
1057
        // Check if the query asked for timestamps. We will only serve
8✔
1058
        // timestamps if this has not been disabled with
8✔
1059
        // noTimestampQueryOption.
8✔
1060
        withTimestamps := query.WithTimestamps() &&
8✔
1061
                !g.cfg.noTimestampQueryOption
8✔
1062

8✔
1063
        // Next, we'll consult the time series to obtain the set of known
8✔
1064
        // channel ID's that match their query.
8✔
1065
        startBlock := query.FirstBlockHeight
8✔
1066
        endBlock := query.LastBlockHeight()
8✔
1067
        channelRanges, err := g.cfg.channelSeries.FilterChannelRange(
8✔
1068
                query.ChainHash, startBlock, endBlock, withTimestamps,
8✔
1069
        )
8✔
1070
        if err != nil {
8✔
1071
                return err
×
1072
        }
×
1073

1074
        // TODO(roasbeef): means can't send max uint above?
1075
        //  * or make internal 64
1076

1077
        // We'll send our response in a streaming manner, chunk-by-chunk. We do
1078
        // this as there's a transport message size limit which we'll need to
1079
        // adhere to. We also need to make sure all of our replies cover the
1080
        // expected range of the query.
1081
        sendReplyForChunk := func(channelChunk []graphdb.ChannelUpdateInfo,
8✔
1082
                firstHeight, lastHeight uint32, finalChunk bool) error {
21✔
1083

13✔
1084
                // The number of blocks contained in the current chunk (the
13✔
1085
                // total span) is the difference between the last channel ID and
13✔
1086
                // the first in the range. We add one as even if all channels
13✔
1087
                // returned are in the same block, we need to count that.
13✔
1088
                numBlocks := lastHeight - firstHeight + 1
13✔
1089
                complete := uint8(0)
13✔
1090
                if finalChunk {
21✔
1091
                        complete = 1
8✔
1092
                }
8✔
1093

1094
                var timestamps lnwire.Timestamps
13✔
1095
                if withTimestamps {
13✔
1096
                        timestamps = make(lnwire.Timestamps, len(channelChunk))
×
1097
                }
×
1098

1099
                scids := make([]lnwire.ShortChannelID, len(channelChunk))
13✔
1100
                for i, info := range channelChunk {
27✔
1101
                        scids[i] = info.ShortChannelID
14✔
1102

14✔
1103
                        if !withTimestamps {
28✔
1104
                                continue
14✔
1105
                        }
1106

1107
                        timestamps[i].Timestamp1 = uint32(
×
1108
                                info.Node1UpdateTimestamp.Unix(),
×
1109
                        )
×
1110

×
1111
                        timestamps[i].Timestamp2 = uint32(
×
1112
                                info.Node2UpdateTimestamp.Unix(),
×
1113
                        )
×
1114
                }
1115

1116
                ctx, _ := g.cg.Create(context.Background())
13✔
1117

13✔
1118
                return g.cfg.sendToPeerSync(ctx, &lnwire.ReplyChannelRange{
13✔
1119
                        ChainHash:        query.ChainHash,
13✔
1120
                        NumBlocks:        numBlocks,
13✔
1121
                        FirstBlockHeight: firstHeight,
13✔
1122
                        Complete:         complete,
13✔
1123
                        EncodingType:     g.cfg.encodingType,
13✔
1124
                        ShortChanIDs:     scids,
13✔
1125
                        Timestamps:       timestamps,
13✔
1126
                })
13✔
1127
        }
1128

1129
        var (
8✔
1130
                firstHeight  = query.FirstBlockHeight
8✔
1131
                lastHeight   uint32
8✔
1132
                channelChunk []graphdb.ChannelUpdateInfo
8✔
1133
        )
8✔
1134

8✔
1135
        // chunkSize is the maximum number of SCIDs that we can safely put in a
8✔
1136
        // single message. If we also need to include timestamps though, then
8✔
1137
        // this number is halved since encoding two timestamps takes the same
8✔
1138
        // number of bytes as encoding an SCID.
8✔
1139
        chunkSize := g.cfg.chunkSize
8✔
1140
        if withTimestamps {
8✔
1141
                chunkSize /= 2
×
1142
        }
×
1143

1144
        for _, channelRange := range channelRanges {
22✔
1145
                channels := channelRange.Channels
14✔
1146
                numChannels := int32(len(channels))
14✔
1147
                numLeftToAdd := chunkSize - int32(len(channelChunk))
14✔
1148

14✔
1149
                // Include the current block in the ongoing chunk if it can fit
14✔
1150
                // and move on to the next block.
14✔
1151
                if numChannels <= numLeftToAdd {
23✔
1152
                        channelChunk = append(channelChunk, channels...)
9✔
1153
                        continue
9✔
1154
                }
1155

1156
                // Otherwise, we need to send our existing channel chunk as is
1157
                // as its own reply and start a new one for the current block.
1158
                // We'll mark the end of our current chunk as the height before
1159
                // the current block to ensure the whole query range is replied
1160
                // to.
1161
                log.Infof("GossipSyncer(%x): sending range chunk of size=%v",
5✔
1162
                        g.cfg.peerPub[:], len(channelChunk))
5✔
1163

5✔
1164
                lastHeight = channelRange.Height - 1
5✔
1165
                err := sendReplyForChunk(
5✔
1166
                        channelChunk, firstHeight, lastHeight, false,
5✔
1167
                )
5✔
1168
                if err != nil {
5✔
1169
                        return err
×
1170
                }
×
1171

1172
                // With the reply constructed, we'll start tallying channels for
1173
                // our next one keeping in mind our chunk size. This may result
1174
                // in channels for this block being left out from the reply, but
1175
                // this isn't an issue since we'll randomly shuffle them and we
1176
                // assume a historical gossip sync is performed at a later time.
1177
                firstHeight = channelRange.Height
5✔
1178
                finalChunkSize := numChannels
5✔
1179
                exceedsChunkSize := numChannels > chunkSize
5✔
1180
                if exceedsChunkSize {
5✔
1181
                        rand.Shuffle(len(channels), func(i, j int) {
×
1182
                                channels[i], channels[j] = channels[j], channels[i]
×
1183
                        })
×
1184
                        finalChunkSize = chunkSize
×
1185
                }
1186
                channelChunk = channels[:finalChunkSize]
5✔
1187

5✔
1188
                // Sort the chunk once again if we had to shuffle it.
5✔
1189
                if exceedsChunkSize {
5✔
1190
                        sort.Slice(channelChunk, func(i, j int) bool {
×
1191
                                id1 := channelChunk[i].ShortChannelID.ToUint64()
×
1192
                                id2 := channelChunk[j].ShortChannelID.ToUint64()
×
1193

×
1194
                                return id1 < id2
×
1195
                        })
×
1196
                }
1197
        }
1198

1199
        // Send the remaining chunk as the final reply.
1200
        log.Infof("GossipSyncer(%x): sending final chan range chunk, size=%v",
8✔
1201
                g.cfg.peerPub[:], len(channelChunk))
8✔
1202

8✔
1203
        return sendReplyForChunk(
8✔
1204
                channelChunk, firstHeight, query.LastBlockHeight(), true,
8✔
1205
        )
8✔
1206
}
1207

1208
// replyShortChanIDs will be dispatched in response to a query by the remote
1209
// node for information concerning a set of short channel ID's. Our response
1210
// will be sent in a streaming chunked manner to ensure that we remain below
1211
// the current transport level message size.
1212
func (g *GossipSyncer) replyShortChanIDs(query *lnwire.QueryShortChanIDs) error {
4✔
1213
        // Before responding, we'll check to ensure that the remote peer is
4✔
1214
        // querying for the same chain that we're on. If not, we'll send back a
4✔
1215
        // response with a complete value of zero to indicate we're on a
4✔
1216
        // different chain.
4✔
1217
        if g.cfg.chainHash != query.ChainHash {
5✔
1218
                log.Warnf("Remote peer requested QueryShortChanIDs for "+
1✔
1219
                        "chain=%v, we're on chain=%v", query.ChainHash,
1✔
1220
                        g.cfg.chainHash)
1✔
1221

1✔
1222
                ctx, _ := g.cg.Create(context.Background())
1✔
1223

1✔
1224
                return g.cfg.sendToPeerSync(ctx, &lnwire.ReplyShortChanIDsEnd{
1✔
1225
                        ChainHash: query.ChainHash,
1✔
1226
                        Complete:  0,
1✔
1227
                })
1✔
1228
        }
1✔
1229

1230
        if len(query.ShortChanIDs) == 0 {
3✔
1231
                log.Infof("GossipSyncer(%x): ignoring query for blank short chan ID's",
×
1232
                        g.cfg.peerPub[:])
×
1233
                return nil
×
1234
        }
×
1235

1236
        log.Infof("GossipSyncer(%x): fetching chan anns for %v chans",
3✔
1237
                g.cfg.peerPub[:], len(query.ShortChanIDs))
3✔
1238

3✔
1239
        // Now that we know we're on the same chain, we'll query the channel
3✔
1240
        // time series for the set of messages that we know of which satisfies
3✔
1241
        // the requirement of being a chan ann, chan update, or a node ann
3✔
1242
        // related to the set of queried channels.
3✔
1243
        replyMsgs, err := g.cfg.channelSeries.FetchChanAnns(
3✔
1244
                query.ChainHash, query.ShortChanIDs,
3✔
1245
        )
3✔
1246
        if err != nil {
3✔
1247
                return fmt.Errorf("unable to fetch chan anns for %v..., %w",
×
1248
                        query.ShortChanIDs[0].ToUint64(), err)
×
1249
        }
×
1250

1251
        // Reply with any messages related to those channel ID's, we'll write
1252
        // each one individually and synchronously to throttle the sends and
1253
        // perform buffering of responses in the syncer as opposed to the peer.
1254
        for _, msg := range replyMsgs {
6✔
1255
                ctx, _ := g.cg.Create(context.Background())
3✔
1256
                err := g.cfg.sendToPeerSync(ctx, msg)
3✔
1257
                if err != nil {
3✔
1258
                        return err
×
1259
                }
×
1260
        }
1261

1262
        // Regardless of whether we had any messages to reply with, send over
1263
        // the sentinel message to signal that the stream has terminated.
1264
        ctx, _ := g.cg.Create(context.Background())
3✔
1265

3✔
1266
        return g.cfg.sendToPeerSync(ctx, &lnwire.ReplyShortChanIDsEnd{
3✔
1267
                ChainHash: query.ChainHash,
3✔
1268
                Complete:  1,
3✔
1269
        })
3✔
1270
}
1271

1272
// ApplyGossipFilter applies a gossiper filter sent by the remote node to the
1273
// state machine. Once applied, we'll ensure that we don't forward any messages
1274
// to the peer that aren't within the time range of the filter.
1275
func (g *GossipSyncer) ApplyGossipFilter(filter *lnwire.GossipTimestampRange) error {
3✔
1276
        g.Lock()
3✔
1277

3✔
1278
        g.remoteUpdateHorizon = filter
3✔
1279

3✔
1280
        startTime := time.Unix(int64(g.remoteUpdateHorizon.FirstTimestamp), 0)
3✔
1281
        endTime := startTime.Add(
3✔
1282
                time.Duration(g.remoteUpdateHorizon.TimestampRange) * time.Second,
3✔
1283
        )
3✔
1284

3✔
1285
        g.Unlock()
3✔
1286

3✔
1287
        // If requested, don't reply with historical gossip data when the remote
3✔
1288
        // peer sets their gossip timestamp range.
3✔
1289
        if g.cfg.ignoreHistoricalFilters {
4✔
1290
                return nil
1✔
1291
        }
1✔
1292

1293
        select {
2✔
1294
        case <-g.syncerSema:
2✔
1295
        case <-g.cg.Done():
×
1296
                return ErrGossipSyncerExiting
×
1297
        }
1298

1299
        // We don't put this in a defer because if the goroutine is launched,
1300
        // it needs to be called when the goroutine is stopped.
1301
        returnSema := func() {
4✔
1302
                g.syncerSema <- struct{}{}
2✔
1303
        }
2✔
1304

1305
        // Now that the remote peer has applied their filter, we'll query the
1306
        // database for all the messages that are beyond this filter.
1307
        newUpdatestoSend, err := g.cfg.channelSeries.UpdatesInHorizon(
2✔
1308
                g.cfg.chainHash, startTime, endTime,
2✔
1309
        )
2✔
1310
        if err != nil {
2✔
1311
                returnSema()
×
1312
                return err
×
1313
        }
×
1314

1315
        log.Infof("GossipSyncer(%x): applying new remote update horizon: "+
2✔
1316
                "start=%v, end=%v, backlog_size=%v", g.cfg.peerPub[:],
2✔
1317
                startTime, endTime, len(newUpdatestoSend))
2✔
1318

2✔
1319
        // If we don't have any to send, then we can return early.
2✔
1320
        if len(newUpdatestoSend) == 0 {
3✔
1321
                returnSema()
1✔
1322
                return nil
1✔
1323
        }
1✔
1324

1325
        // We'll conclude by launching a goroutine to send out any updates.
1326
        g.cg.WgAdd(1)
1✔
1327
        go func() {
2✔
1328
                defer g.cg.WgDone()
1✔
1329
                defer returnSema()
1✔
1330

1✔
1331
                for _, msg := range newUpdatestoSend {
2✔
1332
                        ctx, _ := g.cg.Create(context.Background())
1✔
1333
                        err := g.cfg.sendToPeerSync(ctx, msg)
1✔
1334
                        switch {
1✔
1335
                        case err == ErrGossipSyncerExiting:
×
1336
                                return
×
1337

1338
                        case err == lnpeer.ErrPeerExiting:
×
1339
                                return
×
1340

1341
                        case err != nil:
×
1342
                                log.Errorf("Unable to send message for "+
×
1343
                                        "peer catch up: %v", err)
×
1344
                        }
1345
                }
1346
        }()
1347

1348
        return nil
1✔
1349
}
1350

1351
// FilterGossipMsgs takes a set of gossip messages, and only send it to a peer
1352
// iff the message is within the bounds of their set gossip filter. If the peer
1353
// doesn't have a gossip filter set, then no messages will be forwarded.
1354
func (g *GossipSyncer) FilterGossipMsgs(msgs ...msgWithSenders) {
2✔
1355
        // If the peer doesn't have an update horizon set, then we won't send
2✔
1356
        // it any new update messages.
2✔
1357
        if g.remoteUpdateHorizon == nil {
3✔
1358
                log.Tracef("GossipSyncer(%x): skipped due to nil "+
1✔
1359
                        "remoteUpdateHorizon", g.cfg.peerPub[:])
1✔
1360
                return
1✔
1361
        }
1✔
1362

1363
        // If we've been signaled to exit, or are exiting, then we'll stop
1364
        // short.
1365
        select {
1✔
1366
        case <-g.cg.Done():
×
1367
                return
×
1368
        default:
1✔
1369
        }
1370

1371
        // TODO(roasbeef): need to ensure that peer still online...send msg to
1372
        // gossiper on peer termination to signal peer disconnect?
1373

1374
        var err error
1✔
1375

1✔
1376
        // Before we filter out the messages, we'll construct an index over the
1✔
1377
        // set of channel announcements and channel updates. This will allow us
1✔
1378
        // to quickly check if we should forward a chan ann, based on the known
1✔
1379
        // channel updates for a channel.
1✔
1380
        chanUpdateIndex := make(
1✔
1381
                map[lnwire.ShortChannelID][]*lnwire.ChannelUpdate1,
1✔
1382
        )
1✔
1383
        for _, msg := range msgs {
11✔
1384
                chanUpdate, ok := msg.msg.(*lnwire.ChannelUpdate1)
10✔
1385
                if !ok {
17✔
1386
                        continue
7✔
1387
                }
1388

1389
                chanUpdateIndex[chanUpdate.ShortChannelID] = append(
3✔
1390
                        chanUpdateIndex[chanUpdate.ShortChannelID], chanUpdate,
3✔
1391
                )
3✔
1392
        }
1393

1394
        // We'll construct a helper function that we'll us below to determine
1395
        // if a given messages passes the gossip msg filter.
1396
        g.Lock()
1✔
1397
        startTime := time.Unix(int64(g.remoteUpdateHorizon.FirstTimestamp), 0)
1✔
1398
        endTime := startTime.Add(
1✔
1399
                time.Duration(g.remoteUpdateHorizon.TimestampRange) * time.Second,
1✔
1400
        )
1✔
1401
        g.Unlock()
1✔
1402

1✔
1403
        passesFilter := func(timeStamp uint32) bool {
11✔
1404
                t := time.Unix(int64(timeStamp), 0)
10✔
1405
                return t.Equal(startTime) ||
10✔
1406
                        (t.After(startTime) && t.Before(endTime))
10✔
1407
        }
10✔
1408

1409
        msgsToSend := make([]lnwire.Message, 0, len(msgs))
1✔
1410
        for _, msg := range msgs {
11✔
1411
                // If the target peer is the peer that sent us this message,
10✔
1412
                // then we'll exit early as we don't need to filter this
10✔
1413
                // message.
10✔
1414
                if _, ok := msg.senders[g.cfg.peerPub]; ok {
10✔
1415
                        continue
×
1416
                }
1417

1418
                switch msg := msg.msg.(type) {
10✔
1419

1420
                // For each channel announcement message, we'll only send this
1421
                // message if the channel updates for the channel are between
1422
                // our time range.
1423
                case *lnwire.ChannelAnnouncement1:
4✔
1424
                        // First, we'll check if the channel updates are in
4✔
1425
                        // this message batch.
4✔
1426
                        chanUpdates, ok := chanUpdateIndex[msg.ShortChannelID]
4✔
1427
                        if !ok {
5✔
1428
                                // If not, we'll attempt to query the database
1✔
1429
                                // to see if we know of the updates.
1✔
1430
                                chanUpdates, err = g.cfg.channelSeries.FetchChanUpdates(
1✔
1431
                                        g.cfg.chainHash, msg.ShortChannelID,
1✔
1432
                                )
1✔
1433
                                if err != nil {
1✔
1434
                                        log.Warnf("no channel updates found for "+
×
1435
                                                "short_chan_id=%v",
×
1436
                                                msg.ShortChannelID)
×
1437
                                        continue
×
1438
                                }
1439
                        }
1440

1441
                        for _, chanUpdate := range chanUpdates {
8✔
1442
                                if passesFilter(chanUpdate.Timestamp) {
5✔
1443
                                        msgsToSend = append(msgsToSend, msg)
1✔
1444
                                        break
1✔
1445
                                }
1446
                        }
1447

1448
                        if len(chanUpdates) == 0 {
4✔
1449
                                msgsToSend = append(msgsToSend, msg)
×
1450
                        }
×
1451

1452
                // For each channel update, we'll only send if it the timestamp
1453
                // is between our time range.
1454
                case *lnwire.ChannelUpdate1:
3✔
1455
                        if passesFilter(msg.Timestamp) {
4✔
1456
                                msgsToSend = append(msgsToSend, msg)
1✔
1457
                        }
1✔
1458

1459
                // Similarly, we only send node announcements if the update
1460
                // timestamp ifs between our set gossip filter time range.
1461
                case *lnwire.NodeAnnouncement:
3✔
1462
                        if passesFilter(msg.Timestamp) {
4✔
1463
                                msgsToSend = append(msgsToSend, msg)
1✔
1464
                        }
1✔
1465
                }
1466
        }
1467

1468
        log.Tracef("GossipSyncer(%x): filtered gossip msgs: set=%v, sent=%v",
1✔
1469
                g.cfg.peerPub[:], len(msgs), len(msgsToSend))
1✔
1470

1✔
1471
        if len(msgsToSend) == 0 {
1✔
1472
                return
×
1473
        }
×
1474

1475
        ctx, _ := g.cg.Create(context.Background())
1✔
1476
        if err = g.cfg.sendToPeer(ctx, msgsToSend...); err != nil {
1✔
1477
                log.Errorf("unable to send gossip msgs: %v", err)
×
1478
        }
×
1479

1480
}
1481

1482
// ProcessQueryMsg is used by outside callers to pass new channel time series
1483
// queries to the internal processing goroutine.
1484
func (g *GossipSyncer) ProcessQueryMsg(msg lnwire.Message, peerQuit <-chan struct{}) error {
112✔
1485
        var msgChan chan lnwire.Message
112✔
1486
        switch msg.(type) {
112✔
1487
        case *lnwire.QueryChannelRange, *lnwire.QueryShortChanIDs:
×
1488
                msgChan = g.queryMsgs
×
1489

1490
        // Reply messages should only be expected in states where we're waiting
1491
        // for a reply.
1492
        case *lnwire.ReplyChannelRange, *lnwire.ReplyShortChanIDsEnd:
112✔
1493
                g.Lock()
112✔
1494
                syncState := g.syncState()
112✔
1495
                g.Unlock()
112✔
1496

112✔
1497
                if syncState != waitingQueryRangeReply &&
112✔
1498
                        syncState != waitingQueryChanReply {
113✔
1499

1✔
1500
                        return fmt.Errorf("unexpected msg %T received in "+
1✔
1501
                                "state %v", msg, syncState)
1✔
1502
                }
1✔
1503
                msgChan = g.gossipMsgs
111✔
1504

1505
        default:
×
1506
                msgChan = g.gossipMsgs
×
1507
        }
1508

1509
        select {
111✔
1510
        case msgChan <- msg:
111✔
1511
        case <-peerQuit:
×
1512
        case <-g.cg.Done():
×
1513
        }
1514

1515
        return nil
111✔
1516
}
1517

1518
// setSyncState sets the gossip syncer's state to the given state.
1519
func (g *GossipSyncer) setSyncState(state syncerState) {
94✔
1520
        atomic.StoreUint32(&g.state, uint32(state))
94✔
1521
}
94✔
1522

1523
// syncState returns the current syncerState of the target GossipSyncer.
1524
func (g *GossipSyncer) syncState() syncerState {
425✔
1525
        return syncerState(atomic.LoadUint32(&g.state))
425✔
1526
}
425✔
1527

1528
// ResetSyncedSignal returns a channel that will be closed in order to serve as
1529
// a signal for when the GossipSyncer has reached its chansSynced state.
1530
func (g *GossipSyncer) ResetSyncedSignal() chan struct{} {
14✔
1531
        g.Lock()
14✔
1532
        defer g.Unlock()
14✔
1533

14✔
1534
        syncedSignal := make(chan struct{})
14✔
1535

14✔
1536
        syncState := syncerState(atomic.LoadUint32(&g.state))
14✔
1537
        if syncState == chansSynced {
16✔
1538
                close(syncedSignal)
2✔
1539
                return syncedSignal
2✔
1540
        }
2✔
1541

1542
        g.syncedSignal = syncedSignal
12✔
1543
        return g.syncedSignal
12✔
1544
}
1545

1546
// ProcessSyncTransition sends a request to the gossip syncer to transition its
1547
// sync type to a new one.
1548
//
1549
// NOTE: This can only be done once the gossip syncer has reached its final
1550
// chansSynced state.
1551
func (g *GossipSyncer) ProcessSyncTransition(newSyncType SyncerType) error {
14✔
1552
        errChan := make(chan error, 1)
14✔
1553
        select {
14✔
1554
        case g.syncTransitionReqs <- &syncTransitionReq{
1555
                newSyncType: newSyncType,
1556
                errChan:     errChan,
1557
        }:
14✔
1558
        case <-time.After(syncTransitionTimeout):
×
1559
                return ErrSyncTransitionTimeout
×
1560
        case <-g.cg.Done():
×
1561
                return ErrGossipSyncerExiting
×
1562
        }
1563

1564
        select {
14✔
1565
        case err := <-errChan:
14✔
1566
                return err
14✔
1567
        case <-g.cg.Done():
×
1568
                return ErrGossipSyncerExiting
×
1569
        }
1570
}
1571

1572
// handleSyncTransition handles a new sync type transition request.
1573
//
1574
// NOTE: The gossip syncer might have another sync state as a result of this
1575
// transition.
1576
func (g *GossipSyncer) handleSyncTransition(req *syncTransitionReq) error {
14✔
1577
        // Return early from any NOP sync transitions.
14✔
1578
        syncType := g.SyncType()
14✔
1579
        if syncType == req.newSyncType {
14✔
1580
                return nil
×
1581
        }
×
1582

1583
        log.Debugf("GossipSyncer(%x): transitioning from %v to %v",
14✔
1584
                g.cfg.peerPub, syncType, req.newSyncType)
14✔
1585

14✔
1586
        var (
14✔
1587
                firstTimestamp time.Time
14✔
1588
                timestampRange uint32
14✔
1589
        )
14✔
1590

14✔
1591
        switch req.newSyncType {
14✔
1592
        // If an active sync has been requested, then we should resume receiving
1593
        // new graph updates from the remote peer.
1594
        case ActiveSync, PinnedSync:
12✔
1595
                firstTimestamp = time.Now()
12✔
1596
                timestampRange = math.MaxUint32
12✔
1597

1598
        // If a PassiveSync transition has been requested, then we should no
1599
        // longer receive any new updates from the remote peer. We can do this
1600
        // by setting our update horizon to a range in the past ensuring no
1601
        // graph updates match the timestamp range.
1602
        case PassiveSync:
2✔
1603
                firstTimestamp = zeroTimestamp
2✔
1604
                timestampRange = 0
2✔
1605

1606
        default:
×
1607
                return fmt.Errorf("unhandled sync transition %v",
×
1608
                        req.newSyncType)
×
1609
        }
1610

1611
        err := g.sendGossipTimestampRange(firstTimestamp, timestampRange)
14✔
1612
        if err != nil {
14✔
1613
                return fmt.Errorf("unable to send local update horizon: %w",
×
1614
                        err)
×
1615
        }
×
1616

1617
        g.setSyncType(req.newSyncType)
14✔
1618

14✔
1619
        return nil
14✔
1620
}
1621

1622
// setSyncType sets the gossip syncer's sync type to the given type.
1623
func (g *GossipSyncer) setSyncType(syncType SyncerType) {
67✔
1624
        atomic.StoreUint32(&g.syncType, uint32(syncType))
67✔
1625
}
67✔
1626

1627
// SyncType returns the current SyncerType of the target GossipSyncer.
1628
func (g *GossipSyncer) SyncType() SyncerType {
295✔
1629
        return SyncerType(atomic.LoadUint32(&g.syncType))
295✔
1630
}
295✔
1631

1632
// historicalSync sends a request to the gossip syncer to perofmr a historical
1633
// sync.
1634
//
1635
// NOTE: This can only be done once the gossip syncer has reached its final
1636
// chansSynced state.
1637
func (g *GossipSyncer) historicalSync() error {
16✔
1638
        done := make(chan struct{})
16✔
1639

16✔
1640
        select {
16✔
1641
        case g.historicalSyncReqs <- &historicalSyncReq{
1642
                doneChan: done,
1643
        }:
16✔
1644
        case <-time.After(syncTransitionTimeout):
×
1645
                return ErrSyncTransitionTimeout
×
1646
        case <-g.cg.Done():
×
1647
                return ErrGossiperShuttingDown
×
1648
        }
1649

1650
        select {
16✔
1651
        case <-done:
16✔
1652
                return nil
16✔
1653
        case <-g.cg.Done():
×
1654
                return ErrGossiperShuttingDown
×
1655
        }
1656
}
1657

1658
// handleHistoricalSync handles a request to the gossip syncer to perform a
1659
// historical sync.
1660
func (g *GossipSyncer) handleHistoricalSync(req *historicalSyncReq) {
16✔
1661
        // We'll go back to our initial syncingChans state in order to request
16✔
1662
        // the remote peer to give us all of the channel IDs they know of
16✔
1663
        // starting from the genesis block.
16✔
1664
        g.genHistoricalChanRangeQuery = true
16✔
1665
        g.setSyncState(syncingChans)
16✔
1666
        close(req.doneChan)
16✔
1667
}
16✔
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