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

lightningnetwork / lnd / 17132206455

21 Aug 2025 03:56PM UTC coverage: 54.685% (-2.6%) from 57.321%
17132206455

Pull #10167

github

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

4 of 31 new or added lines in 10 files covered. (12.9%)

23854 existing lines in 284 files now uncovered.

108937 of 199210 relevant lines covered (54.68%)

22026.48 hits per line

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

84.85
/discovery/sync_manager.go
1
package discovery
2

3
import (
4
        "context"
5
        "errors"
6
        "sync"
7
        "sync/atomic"
8
        "time"
9

10
        "github.com/btcsuite/btcd/chaincfg/chainhash"
11
        "github.com/lightningnetwork/lnd/lnpeer"
12
        "github.com/lightningnetwork/lnd/lnwire"
13
        "github.com/lightningnetwork/lnd/routing/route"
14
        "github.com/lightningnetwork/lnd/ticker"
15
        "golang.org/x/time/rate"
16
)
17

18
const (
19
        // DefaultSyncerRotationInterval is the default interval in which we'll
20
        // rotate a single active syncer.
21
        DefaultSyncerRotationInterval = 20 * time.Minute
22

23
        // DefaultHistoricalSyncInterval is the default interval in which we'll
24
        // force a historical sync to ensure we have as much of the public
25
        // network as possible.
26
        DefaultHistoricalSyncInterval = time.Hour
27

28
        // DefaultFilterConcurrency is the default maximum number of concurrent
29
        // gossip filter applications that can be processed.
30
        DefaultFilterConcurrency = 5
31

32
        // DefaultMsgBytesBurst is the allotted burst in bytes we'll permit.
33
        // This is the most that can be sent in a given go. Requests beyond
34
        // this, will block indefinitely. Once tokens (bytes are depleted),
35
        // they'll be refilled at the DefaultMsgBytesPerSecond rate.
36
        DefaultMsgBytesBurst = 2 * 1000 * 1_024
37

38
        // DefaultMsgBytesPerSecond is the max bytes/s we'll permit for outgoing
39
        // messages. Once tokens (bytes) have been taken from the bucket,
40
        // they'll be refilled at this rate.
41
        DefaultMsgBytesPerSecond = 1000 * 1_024
42

43
        // assumedMsgSize is the assumed size of a message if we can't compute
44
        // its serialized size. This comes out to 1 KB.
45
        assumedMsgSize = 1_024
46
)
47

48
var (
49
        // ErrSyncManagerExiting is an error returned when we attempt to
50
        // start/stop a gossip syncer for a connected/disconnected peer, but the
51
        // SyncManager has already been stopped.
52
        ErrSyncManagerExiting = errors.New("sync manager exiting")
53
)
54

55
// newSyncer in an internal message we'll use within the SyncManager to signal
56
// that we should create a GossipSyncer for a newly connected peer.
57
type newSyncer struct {
58
        // peer is the newly connected peer.
59
        peer lnpeer.Peer
60

61
        // doneChan serves as a signal to the caller that the SyncManager's
62
        // internal state correctly reflects the stale active syncer.
63
        doneChan chan struct{}
64
}
65

66
// staleSyncer is an internal message we'll use within the SyncManager to signal
67
// that a peer has disconnected and its GossipSyncer should be removed.
68
type staleSyncer struct {
69
        // peer is the peer that has disconnected.
70
        peer route.Vertex
71

72
        // doneChan serves as a signal to the caller that the SyncManager's
73
        // internal state correctly reflects the stale active syncer. This is
74
        // needed to ensure we always create a new syncer for a flappy peer
75
        // after they disconnect if they happened to be an active syncer.
76
        doneChan chan struct{}
77
}
78

79
// SyncManagerCfg contains all of the dependencies required for the SyncManager
80
// to carry out its duties.
81
type SyncManagerCfg struct {
82
        // ChainHash is a hash that indicates the specific network of the active
83
        // chain.
84
        ChainHash chainhash.Hash
85

86
        // ChanSeries is an interface that provides access to a time series view
87
        // of the current known channel graph. Each GossipSyncer enabled peer
88
        // will utilize this in order to create and respond to channel graph
89
        // time series queries.
90
        ChanSeries ChannelGraphTimeSeries
91

92
        // NumActiveSyncers is the number of peers for which we should have
93
        // active syncers with. After reaching NumActiveSyncers, any future
94
        // gossip syncers will be passive.
95
        NumActiveSyncers int
96

97
        // NoTimestampQueries will prevent the GossipSyncer from querying
98
        // timestamps of announcement messages from the peer and from responding
99
        // to timestamp queries
100
        NoTimestampQueries bool
101

102
        // RotateTicker is a ticker responsible for notifying the SyncManager
103
        // when it should rotate its active syncers. A single active syncer with
104
        // a chansSynced state will be exchanged for a passive syncer in order
105
        // to ensure we don't keep syncing with the same peers.
106
        RotateTicker ticker.Ticker
107

108
        // HistoricalSyncTicker is a ticker responsible for notifying the
109
        // SyncManager when it should attempt a historical sync with a gossip
110
        // sync peer.
111
        HistoricalSyncTicker ticker.Ticker
112

113
        // IgnoreHistoricalFilters will prevent syncers from replying with
114
        // historical data when the remote peer sets a gossip_timestamp_range.
115
        // This prevents ranges with old start times from causing us to dump the
116
        // graph on connect.
117
        IgnoreHistoricalFilters bool
118

119
        // BestHeight returns the latest height known of the chain.
120
        BestHeight func() uint32
121

122
        // PinnedSyncers is a set of peers that will always transition to
123
        // ActiveSync upon connection. These peers will never transition to
124
        // PassiveSync.
125
        PinnedSyncers PinnedSyncers
126

127
        // IsStillZombieChannel takes the timestamps of the latest channel
128
        // updates for a channel and returns true if the channel should be
129
        // considered a zombie based on these timestamps.
130
        IsStillZombieChannel func(time.Time, time.Time) bool
131

132
        // AllotedMsgBytesPerSecond is the allotted bandwidth rate, expressed in
133
        // bytes/second that the gossip manager can consume. Once we exceed this
134
        // rate, message sending will block until we're below the rate.
135
        AllotedMsgBytesPerSecond uint64
136

137
        // AllotedMsgBytesBurst is the amount of burst bytes we'll permit, if
138
        // we've exceeded the hard upper limit.
139
        AllotedMsgBytesBurst uint64
140

141
        // FilterConcurrency is the maximum number of concurrent gossip filter
142
        // applications that can be processed. If not set, defaults to 5.
143
        FilterConcurrency int
144
}
145

146
// SyncManager is a subsystem of the gossiper that manages the gossip syncers
147
// for peers currently connected. When a new peer is connected, the manager will
148
// create its accompanying gossip syncer and determine whether it should have an
149
// ActiveSync or PassiveSync sync type based on how many other gossip syncers
150
// are currently active. Any ActiveSync gossip syncers are started in a
151
// round-robin manner to ensure we're not syncing with multiple peers at the
152
// same time. The first GossipSyncer registered with the SyncManager will
153
// attempt a historical sync to ensure we have as much of the public channel
154
// graph as possible.
155
type SyncManager struct {
156
        // initialHistoricalSyncCompleted serves as a barrier when initializing
157
        // new active GossipSyncers. If 0, the initial historical sync has not
158
        // completed, so we'll defer initializing any active GossipSyncers. If
159
        // 1, then we can transition the GossipSyncer immediately. We set up
160
        // this barrier to ensure we have most of the graph before attempting to
161
        // accept new updates at tip.
162
        //
163
        // NOTE: This must be used atomically.
164
        initialHistoricalSyncCompleted int32
165

166
        start sync.Once
167
        stop  sync.Once
168

169
        cfg SyncManagerCfg
170

171
        // newSyncers is a channel we'll use to process requests to create
172
        // GossipSyncers for newly connected peers.
173
        newSyncers chan *newSyncer
174

175
        // staleSyncers is a channel we'll use to process requests to tear down
176
        // GossipSyncers for disconnected peers.
177
        staleSyncers chan *staleSyncer
178

179
        // syncersMu guards the read and write access to the activeSyncers and
180
        // inactiveSyncers maps below.
181
        syncersMu sync.Mutex
182

183
        // activeSyncers is the set of all syncers for which we are currently
184
        // receiving graph updates from. The number of possible active syncers
185
        // is bounded by NumActiveSyncers.
186
        activeSyncers map[route.Vertex]*GossipSyncer
187

188
        // inactiveSyncers is the set of all syncers for which we are not
189
        // currently receiving new graph updates from.
190
        inactiveSyncers map[route.Vertex]*GossipSyncer
191

192
        // pinnedActiveSyncers is the set of all syncers which are pinned into
193
        // an active sync. Pinned peers performan an initial historical sync on
194
        // each connection and will continue to receive graph updates for the
195
        // duration of the connection.
196
        pinnedActiveSyncers map[route.Vertex]*GossipSyncer
197

198
        // gossipFilterSema contains semaphores for the gossip timestamp
199
        // queries.
200
        gossipFilterSema chan struct{}
201

202
        // rateLimiter dictates the frequency with which we will reply to gossip
203
        // queries from a peer. This is used to delay responses to peers to
204
        // prevent DOS vulnerabilities if they are spamming with an unreasonable
205
        // number of queries.
206
        rateLimiter *rate.Limiter
207

208
        wg   sync.WaitGroup
209
        quit chan struct{}
210
}
211

212
// newSyncManager constructs a new SyncManager backed by the given config.
213
func newSyncManager(cfg *SyncManagerCfg) *SyncManager {
39✔
214
        filterConcurrency := cfg.FilterConcurrency
39✔
215
        if filterConcurrency == 0 {
78✔
216
                filterConcurrency = DefaultFilterConcurrency
39✔
217
        }
39✔
218

219
        filterSema := make(chan struct{}, filterConcurrency)
39✔
220
        for i := 0; i < filterConcurrency; i++ {
234✔
221
                filterSema <- struct{}{}
195✔
222
        }
195✔
223

224
        bytesPerSecond := cfg.AllotedMsgBytesPerSecond
39✔
225
        if bytesPerSecond == 0 {
78✔
226
                bytesPerSecond = DefaultMsgBytesPerSecond
39✔
227
        }
39✔
228

229
        bytesBurst := cfg.AllotedMsgBytesBurst
39✔
230
        if bytesBurst == 0 {
78✔
231
                bytesBurst = DefaultMsgBytesBurst
39✔
232
        }
39✔
233

234
        // We'll use this rate limiter to limit our total outbound bandwidth for
235
        // gossip queries peers.
236
        rateLimiter := rate.NewLimiter(
39✔
237
                rate.Limit(bytesPerSecond), int(bytesBurst),
39✔
238
        )
39✔
239

39✔
240
        return &SyncManager{
39✔
241
                cfg:          *cfg,
39✔
242
                rateLimiter:  rateLimiter,
39✔
243
                newSyncers:   make(chan *newSyncer),
39✔
244
                staleSyncers: make(chan *staleSyncer),
39✔
245
                activeSyncers: make(
39✔
246
                        map[route.Vertex]*GossipSyncer, cfg.NumActiveSyncers,
39✔
247
                ),
39✔
248
                inactiveSyncers: make(map[route.Vertex]*GossipSyncer),
39✔
249
                pinnedActiveSyncers: make(
39✔
250
                        map[route.Vertex]*GossipSyncer, len(cfg.PinnedSyncers),
39✔
251
                ),
39✔
252
                gossipFilterSema: filterSema,
39✔
253
                quit:             make(chan struct{}),
39✔
254
        }
39✔
255
}
256

257
// Start starts the SyncManager in order to properly carry out its duties.
258
func (m *SyncManager) Start() {
39✔
259
        m.start.Do(func() {
78✔
260
                m.wg.Add(1)
39✔
261
                go m.syncerHandler()
39✔
262
        })
39✔
263
}
264

265
// Stop stops the SyncManager from performing its duties.
266
func (m *SyncManager) Stop() {
39✔
267
        m.stop.Do(func() {
78✔
268
                log.Debugf("SyncManager is stopping")
39✔
269
                defer log.Debugf("SyncManager stopped")
39✔
270

39✔
271
                close(m.quit)
39✔
272
                m.wg.Wait()
39✔
273

39✔
274
                for _, syncer := range m.inactiveSyncers {
45✔
275
                        syncer.Stop()
6✔
276
                }
6✔
277
                for _, syncer := range m.activeSyncers {
49✔
278
                        syncer.Stop()
10✔
279
                }
10✔
280
        })
281
}
282

283
// syncerHandler is the SyncManager's main event loop responsible for:
284
//
285
// 1. Creating and tearing down GossipSyncers for connected/disconnected peers.
286

287
// 2. Finding new peers to receive graph updates from to ensure we don't only
288
//    receive them from the same set of peers.
289

290
//  3. Finding new peers to force a historical sync with to ensure we have as
291
//     much of the public network as possible.
292
//
293
// NOTE: This must be run as a goroutine.
294
func (m *SyncManager) syncerHandler() {
39✔
295
        defer m.wg.Done()
39✔
296

39✔
297
        m.cfg.RotateTicker.Resume()
39✔
298
        defer m.cfg.RotateTicker.Stop()
39✔
299

39✔
300
        defer m.cfg.HistoricalSyncTicker.Stop()
39✔
301

39✔
302
        var (
39✔
303
                // initialHistoricalSyncer is the syncer we are currently
39✔
304
                // performing an initial historical sync with.
39✔
305
                initialHistoricalSyncer *GossipSyncer
39✔
306

39✔
307
                // initialHistoricalSyncSignal is a signal that will fire once
39✔
308
                // the initial historical sync has been completed. This is
39✔
309
                // crucial to ensure that another historical sync isn't
39✔
310
                // attempted just because the initialHistoricalSyncer was
39✔
311
                // disconnected.
39✔
312
                initialHistoricalSyncSignal chan struct{}
39✔
313
        )
39✔
314

39✔
315
        setInitialHistoricalSyncer := func(s *GossipSyncer) {
51✔
316
                initialHistoricalSyncer = s
12✔
317
                initialHistoricalSyncSignal = s.ResetSyncedSignal()
12✔
318

12✔
319
                // Restart the timer for our new historical sync peer. This will
12✔
320
                // ensure that all initial syncers receive an equivalent
12✔
321
                // duration before attempting the next sync. Without doing so we
12✔
322
                // might attempt two historical sync back to back if a peer
12✔
323
                // disconnects just before the ticker fires.
12✔
324
                m.cfg.HistoricalSyncTicker.Pause()
12✔
325
                m.cfg.HistoricalSyncTicker.Resume()
12✔
326
        }
12✔
327

328
        for {
122✔
329
                select {
83✔
330
                // A new peer has been connected, so we'll create its
331
                // accompanying GossipSyncer.
332
                case newSyncer := <-m.newSyncers:
25✔
333
                        // If we already have a syncer, then we'll exit early as
25✔
334
                        // we don't want to override it.
25✔
335
                        if _, ok := m.GossipSyncer(newSyncer.peer.PubKey()); ok {
25✔
336
                                close(newSyncer.doneChan)
×
337
                                continue
×
338
                        }
339

340
                        s := m.createGossipSyncer(newSyncer.peer)
25✔
341

25✔
342
                        isPinnedSyncer := m.isPinnedSyncer(s)
25✔
343

25✔
344
                        // attemptHistoricalSync determines whether we should
25✔
345
                        // attempt an initial historical sync when a new peer
25✔
346
                        // connects.
25✔
347
                        attemptHistoricalSync := false
25✔
348

25✔
349
                        m.syncersMu.Lock()
25✔
350
                        switch {
25✔
351
                        // For pinned syncers, we will immediately transition
352
                        // the peer into an active (pinned) sync state.
353
                        case isPinnedSyncer:
3✔
354
                                attemptHistoricalSync = true
3✔
355
                                s.setSyncType(PinnedSync)
3✔
356
                                s.setSyncState(syncerIdle)
3✔
357
                                m.pinnedActiveSyncers[s.cfg.peerPub] = s
3✔
358

359
                        // Regardless of whether the initial historical sync
360
                        // has completed, we'll re-trigger a historical sync if
361
                        // we no longer have any syncers. This might be
362
                        // necessary if we lost all our peers at one point, and
363
                        // now we finally have one again.
364
                        case len(m.activeSyncers) == 0 &&
365
                                len(m.inactiveSyncers) == 0:
10✔
366

10✔
367
                                attemptHistoricalSync =
10✔
368
                                        m.cfg.NumActiveSyncers > 0
10✔
369
                                fallthrough
10✔
370

371
                        // If we've exceeded our total number of active syncers,
372
                        // we'll initialize this GossipSyncer as passive.
373
                        case len(m.activeSyncers) >= m.cfg.NumActiveSyncers:
14✔
374
                                fallthrough
14✔
375

376
                        // If the initial historical sync has yet to complete,
377
                        // then we'll declare it as passive and attempt to
378
                        // transition it when the initial historical sync
379
                        // completes.
380
                        case !m.IsGraphSynced():
18✔
381
                                s.setSyncType(PassiveSync)
18✔
382
                                m.inactiveSyncers[s.cfg.peerPub] = s
18✔
383

384
                        // The initial historical sync has completed, so we can
385
                        // immediately start the GossipSyncer as active.
386
                        default:
4✔
387
                                s.setSyncType(ActiveSync)
4✔
388
                                m.activeSyncers[s.cfg.peerPub] = s
4✔
389
                        }
390
                        m.syncersMu.Unlock()
25✔
391

25✔
392
                        s.Start()
25✔
393

25✔
394
                        // Once we create the GossipSyncer, we'll signal to the
25✔
395
                        // caller that they can proceed since the SyncManager's
25✔
396
                        // internal state has been updated.
25✔
397
                        close(newSyncer.doneChan)
25✔
398

25✔
399
                        // We'll force a historical sync with the first peer we
25✔
400
                        // connect to, to ensure we get as much of the graph as
25✔
401
                        // possible.
25✔
402
                        if !attemptHistoricalSync {
38✔
403
                                continue
13✔
404
                        }
405

406
                        log.Debugf("Attempting initial historical sync with "+
12✔
407
                                "GossipSyncer(%x)", s.cfg.peerPub)
12✔
408

12✔
409
                        if err := s.historicalSync(); err != nil {
12✔
410
                                log.Errorf("Unable to attempt initial "+
×
411
                                        "historical sync with "+
×
412
                                        "GossipSyncer(%x): %v", s.cfg.peerPub,
×
413
                                        err)
×
414
                                continue
×
415
                        }
416

417
                        // Once the historical sync has started, we'll get a
418
                        // keep track of the corresponding syncer to properly
419
                        // handle disconnects. We'll also use a signal to know
420
                        // when the historical sync completed.
421
                        if !isPinnedSyncer {
21✔
422
                                setInitialHistoricalSyncer(s)
9✔
423
                        }
9✔
424

425
                // An existing peer has disconnected, so we'll tear down its
426
                // corresponding GossipSyncer.
427
                case staleSyncer := <-m.staleSyncers:
6✔
428
                        // Once the corresponding GossipSyncer has been stopped
6✔
429
                        // and removed, we'll signal to the caller that they can
6✔
430
                        // proceed since the SyncManager's internal state has
6✔
431
                        // been updated.
6✔
432
                        m.removeGossipSyncer(staleSyncer.peer)
6✔
433
                        close(staleSyncer.doneChan)
6✔
434

6✔
435
                        // If we don't have an initialHistoricalSyncer, or we do
6✔
436
                        // but it is not the peer being disconnected, then we
6✔
437
                        // have nothing left to do and can proceed.
6✔
438
                        switch {
6✔
439
                        case initialHistoricalSyncer == nil:
4✔
440
                                fallthrough
4✔
441
                        case staleSyncer.peer != initialHistoricalSyncer.cfg.peerPub:
5✔
442
                                fallthrough
5✔
443
                        case m.cfg.NumActiveSyncers == 0:
5✔
444
                                continue
5✔
445
                        }
446

447
                        // Otherwise, our initialHistoricalSyncer corresponds to
448
                        // the peer being disconnected, so we'll have to find a
449
                        // replacement.
450
                        log.Debug("Finding replacement for initial " +
1✔
451
                                "historical sync")
1✔
452

1✔
453
                        s := m.forceHistoricalSync()
1✔
454
                        if s == nil {
1✔
455
                                log.Debug("No eligible replacement found " +
×
456
                                        "for initial historical sync")
×
457
                                continue
×
458
                        }
459

460
                        log.Debugf("Replaced initial historical "+
1✔
461
                                "GossipSyncer(%v) with GossipSyncer(%x)",
1✔
462
                                staleSyncer.peer, s.cfg.peerPub)
1✔
463

1✔
464
                        setInitialHistoricalSyncer(s)
1✔
465

466
                // Our initial historical sync signal has completed, so we'll
467
                // nil all of the relevant fields as they're no longer needed.
468
                case <-initialHistoricalSyncSignal:
8✔
469
                        initialHistoricalSyncer = nil
8✔
470
                        initialHistoricalSyncSignal = nil
8✔
471

8✔
472
                        log.Debug("Initial historical sync completed")
8✔
473

8✔
474
                        // With the initial historical sync complete, we can
8✔
475
                        // begin receiving new graph updates at tip. We'll
8✔
476
                        // determine whether we can have any more active
8✔
477
                        // GossipSyncers. If we do, we'll randomly select some
8✔
478
                        // that are currently passive to transition.
8✔
479
                        m.syncersMu.Lock()
8✔
480
                        numActiveLeft := m.cfg.NumActiveSyncers - len(m.activeSyncers)
8✔
481
                        if numActiveLeft <= 0 {
8✔
482
                                m.syncersMu.Unlock()
×
483
                                continue
×
484
                        }
485

486
                        // We may not even have enough inactive syncers to be
487
                        // transitted. In that case, we will transit all the
488
                        // inactive syncers.
489
                        if len(m.inactiveSyncers) < numActiveLeft {
13✔
490
                                numActiveLeft = len(m.inactiveSyncers)
5✔
491
                        }
5✔
492

493
                        log.Debugf("Attempting to transition %v passive "+
8✔
494
                                "GossipSyncers to active", numActiveLeft)
8✔
495

8✔
496
                        for i := 0; i < numActiveLeft; i++ {
16✔
497
                                chooseRandomSyncer(
8✔
498
                                        m.inactiveSyncers, m.transitionPassiveSyncer,
8✔
499
                                )
8✔
500
                        }
8✔
501

502
                        m.syncersMu.Unlock()
8✔
503

504
                // Our RotateTicker has ticked, so we'll attempt to rotate a
505
                // single active syncer with a passive one.
506
                case <-m.cfg.RotateTicker.Ticks():
2✔
507
                        m.rotateActiveSyncerCandidate()
2✔
508

509
                // Our HistoricalSyncTicker has ticked, so we'll randomly select
510
                // a peer and force a historical sync with them.
511
                case <-m.cfg.HistoricalSyncTicker.Ticks():
3✔
512
                        // To be extra cautious, gate the forceHistoricalSync
3✔
513
                        // call such that it can only execute if we are
3✔
514
                        // configured to have a non-zero number of sync peers.
3✔
515
                        // This way even if the historical sync ticker manages
3✔
516
                        // to tick we can be sure that a historical sync won't
3✔
517
                        // accidentally begin.
3✔
518
                        if m.cfg.NumActiveSyncers == 0 {
4✔
519
                                continue
1✔
520
                        }
521

522
                        // If we don't have a syncer available we have nothing
523
                        // to do.
524
                        s := m.forceHistoricalSync()
2✔
525
                        if s == nil {
2✔
526
                                continue
×
527
                        }
528

529
                        // If we've already completed a historical sync, we'll
530
                        // skip setting the initial historical syncer.
531
                        if m.IsGraphSynced() {
2✔
532
                                continue
×
533
                        }
534

535
                        // Otherwise, we'll track the peer we've performed a
536
                        // historical sync with in order to handle the case
537
                        // where our previous historical sync peer did not
538
                        // respond to our queries and we haven't ingested as
539
                        // much of the graph as we should.
540
                        setInitialHistoricalSyncer(s)
2✔
541

542
                case <-m.quit:
39✔
543
                        return
39✔
544
                }
545
        }
546
}
547

548
// isPinnedSyncer returns true if the passed GossipSyncer is one of our pinned
549
// sync peers.
550
func (m *SyncManager) isPinnedSyncer(s *GossipSyncer) bool {
25✔
551
        _, isPinnedSyncer := m.cfg.PinnedSyncers[s.cfg.peerPub]
25✔
552
        return isPinnedSyncer
25✔
553
}
25✔
554

555
// deriveRateLimitReservation will take the current message and derive a
556
// reservation that can be used to wait on the rate limiter.
557
func (m *SyncManager) deriveRateLimitReservation(msg lnwire.Message,
558
) (*rate.Reservation, error) {
42✔
559

42✔
560
        var (
42✔
561
                msgSize uint32
42✔
562
                err     error
42✔
563
        )
42✔
564

42✔
565
        // Figure out the serialized size of the message. If we can't easily
42✔
566
        // compute it, then we'll used the assumed msg size.
42✔
567
        if sMsg, ok := msg.(lnwire.SizeableMessage); ok {
84✔
568
                msgSize, err = sMsg.SerializedSize()
42✔
569
                if err != nil {
43✔
570
                        return nil, err
1✔
571
                }
1✔
572
        } else {
×
573
                log.Warnf("Unable to compute serialized size of %T", msg)
×
574

×
575
                msgSize = assumedMsgSize
×
576
        }
×
577

578
        return m.rateLimiter.ReserveN(time.Now(), int(msgSize)), nil
41✔
579
}
580

581
// waitMsgDelay takes a delay, and waits until it has finished.
582
func (m *SyncManager) waitMsgDelay(ctx context.Context, peerPub [33]byte,
583
        limitReservation *rate.Reservation) error {
34✔
584

34✔
585
        // If we've already replied a handful of times, we will start to delay
34✔
586
        // responses back to the remote peer. This can help prevent DOS attacks
34✔
587
        // where the remote peer spams us endlessly.
34✔
588
        //
34✔
589
        // We skip checking for reservation.OK() here, as during config
34✔
590
        // validation, we ensure that the burst is enough for a single message
34✔
591
        // to be sent.
34✔
592
        delay := limitReservation.Delay()
34✔
593
        if delay > 0 {
34✔
594
                log.Debugf("GossipSyncer(%x): rate limiting gossip replies, "+
×
595
                        "responding in %s", peerPub, delay)
×
596

×
597
                select {
×
598
                case <-time.After(delay):
×
599

600
                case <-ctx.Done():
×
601
                        limitReservation.Cancel()
×
602

×
603
                        return ErrGossipSyncerExiting
×
604

605
                case <-m.quit:
×
606
                        limitReservation.Cancel()
×
607

×
608
                        return ErrGossipSyncerExiting
×
609
                }
610
        }
611

612
        return nil
34✔
613
}
614

615
// maybeRateLimitMsg takes a message, and may wait a period of time to rate
616
// limit the msg.
617
func (m *SyncManager) maybeRateLimitMsg(ctx context.Context, peerPub [33]byte,
618
        msg lnwire.Message) error {
34✔
619

34✔
620
        delay, err := m.deriveRateLimitReservation(msg)
34✔
621
        if err != nil {
34✔
622
                return nil
×
623
        }
×
624

625
        return m.waitMsgDelay(ctx, peerPub, delay)
34✔
626
}
627

628
// sendMessages sends a set of messages to the remote peer.
629
func (m *SyncManager) sendMessages(ctx context.Context, sync bool,
630
        peer lnpeer.Peer, nodeID route.Vertex, msgs ...lnwire.Message) error {
34✔
631

34✔
632
        for _, msg := range msgs {
68✔
633
                if err := m.maybeRateLimitMsg(ctx, nodeID, msg); err != nil {
34✔
634
                        return err
×
635
                }
×
636
                if err := peer.SendMessageLazy(sync, msg); err != nil {
34✔
637
                        return err
×
638
                }
×
639
        }
640

641
        return nil
34✔
642
}
643

644
// createGossipSyncer creates the GossipSyncer for a newly connected peer.
645
func (m *SyncManager) createGossipSyncer(peer lnpeer.Peer) *GossipSyncer {
25✔
646
        nodeID := route.Vertex(peer.PubKey())
25✔
647
        log.Infof("Creating new GossipSyncer for peer=%x", nodeID[:])
25✔
648

25✔
649
        encoding := lnwire.EncodingSortedPlain
25✔
650
        s := newGossipSyncer(gossipSyncerCfg{
25✔
651
                chainHash:     m.cfg.ChainHash,
25✔
652
                peerPub:       nodeID,
25✔
653
                channelSeries: m.cfg.ChanSeries,
25✔
654
                encodingType:  encoding,
25✔
655
                chunkSize:     encodingTypeToChunkSize[encoding],
25✔
656
                batchSize:     requestBatchSize,
25✔
657
                sendToPeer: func(ctx context.Context,
25✔
658
                        msgs ...lnwire.Message) error {
59✔
659

34✔
660
                        return m.sendMessages(ctx, false, peer, nodeID, msgs...)
34✔
661
                },
34✔
662
                sendToPeerSync: func(ctx context.Context,
UNCOV
663
                        msgs ...lnwire.Message) error {
×
UNCOV
664

×
UNCOV
665
                        return m.sendMessages(ctx, true, peer, nodeID, msgs...)
×
UNCOV
666
                },
×
667
                ignoreHistoricalFilters:  m.cfg.IgnoreHistoricalFilters,
668
                bestHeight:               m.cfg.BestHeight,
669
                markGraphSynced:          m.markGraphSynced,
670
                maxQueryChanRangeReplies: maxQueryChanRangeReplies,
671
                noTimestampQueryOption:   m.cfg.NoTimestampQueries,
672
                isStillZombieChannel:     m.cfg.IsStillZombieChannel,
673
        }, m.gossipFilterSema)
674

675
        // Gossip syncers are initialized by default in a PassiveSync type
676
        // and chansSynced state so that they can reply to any peer queries or
677
        // handle any sync transitions.
678
        s.setSyncState(chansSynced)
25✔
679
        s.setSyncType(PassiveSync)
25✔
680

25✔
681
        log.Debugf("Created new GossipSyncer[state=%s type=%s] for peer=%x",
25✔
682
                s.syncState(), s.SyncType(), peer.PubKey())
25✔
683

25✔
684
        return s
25✔
685
}
686

687
// removeGossipSyncer removes all internal references to the disconnected peer's
688
// GossipSyncer and stops it. In the event of an active GossipSyncer being
689
// disconnected, a passive GossipSyncer, if any, will take its place.
690
func (m *SyncManager) removeGossipSyncer(peer route.Vertex) {
6✔
691
        m.syncersMu.Lock()
6✔
692
        defer m.syncersMu.Unlock()
6✔
693

6✔
694
        s, ok := m.gossipSyncer(peer)
6✔
695
        if !ok {
6✔
UNCOV
696
                return
×
UNCOV
697
        }
×
698

699
        log.Infof("Removing GossipSyncer for peer=%v", peer)
6✔
700

6✔
701
        // We'll stop the GossipSyncer for the disconnected peer in a goroutine
6✔
702
        // to prevent blocking the SyncManager.
6✔
703
        go s.Stop()
6✔
704

6✔
705
        // If it's a non-active syncer, then we can just exit now.
6✔
706
        if _, ok := m.inactiveSyncers[peer]; ok {
8✔
707
                delete(m.inactiveSyncers, peer)
2✔
708
                return
2✔
709
        }
2✔
710

711
        // If it's a pinned syncer, then we can just exit as this doesn't
712
        // affect our active syncer count.
713
        if _, ok := m.pinnedActiveSyncers[peer]; ok {
4✔
UNCOV
714
                delete(m.pinnedActiveSyncers, peer)
×
UNCOV
715
                return
×
UNCOV
716
        }
×
717

718
        // Otherwise, we'll need find a new one to replace it, if any.
719
        delete(m.activeSyncers, peer)
4✔
720
        newActiveSyncer := chooseRandomSyncer(
4✔
721
                m.inactiveSyncers, m.transitionPassiveSyncer,
4✔
722
        )
4✔
723
        if newActiveSyncer == nil {
6✔
724
                return
2✔
725
        }
2✔
726

727
        log.Debugf("Replaced active GossipSyncer(%v) with GossipSyncer(%x)",
2✔
728
                peer, newActiveSyncer.cfg.peerPub)
2✔
729
}
730

731
// rotateActiveSyncerCandidate rotates a single active syncer. In order to
732
// achieve this, the active syncer must be in a chansSynced state in order to
733
// process the sync transition.
734
func (m *SyncManager) rotateActiveSyncerCandidate() {
2✔
735
        m.syncersMu.Lock()
2✔
736
        defer m.syncersMu.Unlock()
2✔
737

2✔
738
        // If we couldn't find an eligible active syncer to rotate, we can
2✔
739
        // return early.
2✔
740
        activeSyncer := chooseRandomSyncer(m.activeSyncers, nil)
2✔
741
        if activeSyncer == nil {
2✔
742
                log.Debug("No eligible active syncer to rotate")
×
743
                return
×
744
        }
×
745

746
        // Similarly, if we don't have a candidate to rotate with, we can return
747
        // early as well.
748
        candidate := chooseRandomSyncer(m.inactiveSyncers, nil)
2✔
749
        if candidate == nil {
3✔
750
                log.Debug("No eligible candidate to rotate active syncer")
1✔
751
                return
1✔
752
        }
1✔
753

754
        // Otherwise, we'll attempt to transition each syncer to their
755
        // respective new sync type.
756
        log.Debugf("Rotating active GossipSyncer(%x) with GossipSyncer(%x)",
1✔
757
                activeSyncer.cfg.peerPub, candidate.cfg.peerPub)
1✔
758

1✔
759
        if err := m.transitionActiveSyncer(activeSyncer); err != nil {
1✔
760
                log.Errorf("Unable to transition active GossipSyncer(%x): %v",
×
761
                        activeSyncer.cfg.peerPub, err)
×
762
                return
×
763
        }
×
764

765
        if err := m.transitionPassiveSyncer(candidate); err != nil {
1✔
766
                log.Errorf("Unable to transition passive GossipSyncer(%x): %v",
×
767
                        activeSyncer.cfg.peerPub, err)
×
768
                return
×
769
        }
×
770
}
771

772
// transitionActiveSyncer transitions an active syncer to a passive one.
773
//
774
// NOTE: This must be called with the syncersMu lock held.
775
func (m *SyncManager) transitionActiveSyncer(s *GossipSyncer) error {
1✔
776
        log.Debugf("Transitioning active GossipSyncer(%x) to passive",
1✔
777
                s.cfg.peerPub)
1✔
778

1✔
779
        if err := s.ProcessSyncTransition(PassiveSync); err != nil {
1✔
780
                return err
×
781
        }
×
782

783
        delete(m.activeSyncers, s.cfg.peerPub)
1✔
784
        m.inactiveSyncers[s.cfg.peerPub] = s
1✔
785

1✔
786
        return nil
1✔
787
}
788

789
// transitionPassiveSyncer transitions a passive syncer to an active one.
790
//
791
// NOTE: This must be called with the syncersMu lock held.
792
func (m *SyncManager) transitionPassiveSyncer(s *GossipSyncer) error {
11✔
793
        log.Debugf("Transitioning passive GossipSyncer(%x) to active",
11✔
794
                s.cfg.peerPub)
11✔
795

11✔
796
        if err := s.ProcessSyncTransition(ActiveSync); err != nil {
11✔
797
                return err
×
798
        }
×
799

800
        delete(m.inactiveSyncers, s.cfg.peerPub)
11✔
801
        m.activeSyncers[s.cfg.peerPub] = s
11✔
802

11✔
803
        return nil
11✔
804
}
805

806
// forceHistoricalSync chooses a syncer with a remote peer at random and forces
807
// a historical sync with it.
808
func (m *SyncManager) forceHistoricalSync() *GossipSyncer {
3✔
809
        m.syncersMu.Lock()
3✔
810
        defer m.syncersMu.Unlock()
3✔
811

3✔
812
        // We'll sample from both sets of active and inactive syncers in the
3✔
813
        // event that we don't have any inactive syncers.
3✔
814
        return chooseRandomSyncer(m.gossipSyncers(), func(s *GossipSyncer) error {
6✔
815
                return s.historicalSync()
3✔
816
        })
3✔
817
}
818

819
// chooseRandomSyncer iterates through the set of syncers given and returns the
820
// first one which was able to successfully perform the action enclosed in the
821
// function closure.
822
//
823
// NOTE: It's possible for a nil value to be returned if there are no eligible
824
// candidate syncers.
825
func chooseRandomSyncer(syncers map[route.Vertex]*GossipSyncer,
826
        action func(*GossipSyncer) error) *GossipSyncer {
19✔
827

19✔
828
        for _, s := range syncers {
38✔
829
                // Only syncers in a chansSynced state are viable for sync
19✔
830
                // transitions, so skip any that aren't.
19✔
831
                if s.syncState() != chansSynced {
22✔
832
                        continue
3✔
833
                }
834

835
                if action != nil {
29✔
836
                        if err := action(s); err != nil {
13✔
837
                                log.Debugf("Skipping eligible candidate "+
×
838
                                        "GossipSyncer(%x): %v", s.cfg.peerPub,
×
839
                                        err)
×
840
                                continue
×
841
                        }
842
                }
843

844
                return s
16✔
845
        }
846

847
        return nil
3✔
848
}
849

850
// InitSyncState is called by outside sub-systems when a connection is
851
// established to a new peer that understands how to perform channel range
852
// queries. We'll allocate a new GossipSyncer for it, and start any goroutines
853
// needed to handle new queries. The first GossipSyncer registered with the
854
// SyncManager will attempt a historical sync to ensure we have as much of the
855
// public channel graph as possible.
856
//
857
// TODO(wilmer): Only mark as ActiveSync if this isn't a channel peer.
858
func (m *SyncManager) InitSyncState(peer lnpeer.Peer) error {
25✔
859
        done := make(chan struct{})
25✔
860

25✔
861
        select {
25✔
862
        case m.newSyncers <- &newSyncer{
863
                peer:     peer,
864
                doneChan: done,
865
        }:
25✔
866
        case <-m.quit:
×
867
                return ErrSyncManagerExiting
×
868
        }
869

870
        select {
25✔
871
        case <-done:
25✔
872
                return nil
25✔
873
        case <-m.quit:
×
874
                return ErrSyncManagerExiting
×
875
        }
876
}
877

878
// PruneSyncState is called by outside sub-systems once a peer that we were
879
// previously connected to has been disconnected. In this case we can stop the
880
// existing GossipSyncer assigned to the peer and free up resources.
881
func (m *SyncManager) PruneSyncState(peer route.Vertex) {
6✔
882
        done := make(chan struct{})
6✔
883

6✔
884
        // We avoid returning an error when the SyncManager is stopped since the
6✔
885
        // GossipSyncer will be stopped then anyway.
6✔
886
        select {
6✔
887
        case m.staleSyncers <- &staleSyncer{
888
                peer:     peer,
889
                doneChan: done,
890
        }:
6✔
891
        case <-m.quit:
×
892
                return
×
893
        }
894

895
        select {
6✔
896
        case <-done:
6✔
897
        case <-m.quit:
×
898
        }
899
}
900

901
// GossipSyncer returns the associated gossip syncer of a peer. The boolean
902
// returned signals whether there exists a gossip syncer for the peer.
903
func (m *SyncManager) GossipSyncer(peer route.Vertex) (*GossipSyncer, bool) {
45✔
904
        m.syncersMu.Lock()
45✔
905
        defer m.syncersMu.Unlock()
45✔
906
        return m.gossipSyncer(peer)
45✔
907
}
45✔
908

909
// gossipSyncer returns the associated gossip syncer of a peer. The boolean
910
// returned signals whether there exists a gossip syncer for the peer.
911
func (m *SyncManager) gossipSyncer(peer route.Vertex) (*GossipSyncer, bool) {
51✔
912
        syncer, ok := m.inactiveSyncers[peer]
51✔
913
        if ok {
66✔
914
                return syncer, true
15✔
915
        }
15✔
916
        syncer, ok = m.activeSyncers[peer]
36✔
917
        if ok {
44✔
918
                return syncer, true
8✔
919
        }
8✔
920
        syncer, ok = m.pinnedActiveSyncers[peer]
28✔
921
        if ok {
31✔
922
                return syncer, true
3✔
923
        }
3✔
924
        return nil, false
25✔
925
}
926

927
// GossipSyncers returns all of the currently initialized gossip syncers.
928
func (m *SyncManager) GossipSyncers() map[route.Vertex]*GossipSyncer {
34✔
929
        m.syncersMu.Lock()
34✔
930
        defer m.syncersMu.Unlock()
34✔
931
        return m.gossipSyncers()
34✔
932
}
34✔
933

934
// gossipSyncers returns all of the currently initialized gossip syncers.
935
func (m *SyncManager) gossipSyncers() map[route.Vertex]*GossipSyncer {
37✔
936
        numSyncers := len(m.inactiveSyncers) + len(m.activeSyncers)
37✔
937
        syncers := make(map[route.Vertex]*GossipSyncer, numSyncers)
37✔
938

37✔
939
        for _, syncer := range m.inactiveSyncers {
42✔
940
                syncers[syncer.cfg.peerPub] = syncer
5✔
941
        }
5✔
942
        for _, syncer := range m.activeSyncers {
37✔
UNCOV
943
                syncers[syncer.cfg.peerPub] = syncer
×
UNCOV
944
        }
×
945

946
        return syncers
37✔
947
}
948

949
// markGraphSynced allows us to report that the initial historical sync has
950
// completed.
951
func (m *SyncManager) markGraphSynced() {
42✔
952
        atomic.StoreInt32(&m.initialHistoricalSyncCompleted, 1)
42✔
953
}
42✔
954

955
// IsGraphSynced determines whether we've completed our initial historical sync.
956
// The initial historical sync is done to ensure we've ingested as much of the
957
// public graph as possible.
958
func (m *SyncManager) IsGraphSynced() bool {
295✔
959
        return atomic.LoadInt32(&m.initialHistoricalSyncCompleted) == 1
295✔
960
}
295✔
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