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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

73.17
/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
        // filterSemaSize is the capacity of gossipFilterSema.
29
        filterSemaSize = 5
30

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

161
        start sync.Once
162
        stop  sync.Once
163

164
        cfg SyncManagerCfg
165

166
        // newSyncers is a channel we'll use to process requests to create
167
        // GossipSyncers for newly connected peers.
168
        newSyncers chan *newSyncer
169

170
        // staleSyncers is a channel we'll use to process requests to tear down
171
        // GossipSyncers for disconnected peers.
172
        staleSyncers chan *staleSyncer
173

174
        // syncersMu guards the read and write access to the activeSyncers and
175
        // inactiveSyncers maps below.
176
        syncersMu sync.Mutex
177

178
        // activeSyncers is the set of all syncers for which we are currently
179
        // receiving graph updates from. The number of possible active syncers
180
        // is bounded by NumActiveSyncers.
181
        activeSyncers map[route.Vertex]*GossipSyncer
182

183
        // inactiveSyncers is the set of all syncers for which we are not
184
        // currently receiving new graph updates from.
185
        inactiveSyncers map[route.Vertex]*GossipSyncer
186

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

193
        // gossipFilterSema contains semaphores for the gossip timestamp
194
        // queries.
195
        gossipFilterSema chan struct{}
196

197
        // rateLimiter dictates the frequency with which we will reply to gossip
198
        // queries from a peer. This is used to delay responses to peers to
199
        // prevent DOS vulnerabilities if they are spamming with an unreasonable
200
        // number of queries.
201
        rateLimiter *rate.Limiter
202

203
        wg   sync.WaitGroup
204
        quit chan struct{}
205
}
206

207
// newSyncManager constructs a new SyncManager backed by the given config.
208
func newSyncManager(cfg *SyncManagerCfg) *SyncManager {
3✔
209

3✔
210
        filterSema := make(chan struct{}, filterSemaSize)
3✔
211
        for i := 0; i < filterSemaSize; i++ {
6✔
212
                filterSema <- struct{}{}
3✔
213
        }
3✔
214

215
        bytesPerSecond := cfg.AllotedMsgBytesPerSecond
3✔
216
        if bytesPerSecond == 0 {
3✔
UNCOV
217
                bytesPerSecond = DefaultMsgBytesPerSecond
×
UNCOV
218
        }
×
219

220
        bytesBurst := cfg.AllotedMsgBytesBurst
3✔
221
        if bytesBurst == 0 {
3✔
UNCOV
222
                bytesBurst = DefaultMsgBytesBurst
×
UNCOV
223
        }
×
224

225
        // We'll use this rate limiter to limit our total outbound bandwidth for
226
        // gossip queries peers.
227
        rateLimiter := rate.NewLimiter(
3✔
228
                rate.Limit(bytesPerSecond), int(bytesBurst),
3✔
229
        )
3✔
230

3✔
231
        return &SyncManager{
3✔
232
                cfg:          *cfg,
3✔
233
                rateLimiter:  rateLimiter,
3✔
234
                newSyncers:   make(chan *newSyncer),
3✔
235
                staleSyncers: make(chan *staleSyncer),
3✔
236
                activeSyncers: make(
3✔
237
                        map[route.Vertex]*GossipSyncer, cfg.NumActiveSyncers,
3✔
238
                ),
3✔
239
                inactiveSyncers: make(map[route.Vertex]*GossipSyncer),
3✔
240
                pinnedActiveSyncers: make(
3✔
241
                        map[route.Vertex]*GossipSyncer, len(cfg.PinnedSyncers),
3✔
242
                ),
3✔
243
                gossipFilterSema: filterSema,
3✔
244
                quit:             make(chan struct{}),
3✔
245
        }
3✔
246
}
247

248
// Start starts the SyncManager in order to properly carry out its duties.
249
func (m *SyncManager) Start() {
3✔
250
        m.start.Do(func() {
6✔
251
                m.wg.Add(1)
3✔
252
                go m.syncerHandler()
3✔
253
        })
3✔
254
}
255

256
// Stop stops the SyncManager from performing its duties.
257
func (m *SyncManager) Stop() {
3✔
258
        m.stop.Do(func() {
6✔
259
                log.Debugf("SyncManager is stopping")
3✔
260
                defer log.Debugf("SyncManager stopped")
3✔
261

3✔
262
                close(m.quit)
3✔
263
                m.wg.Wait()
3✔
264

3✔
265
                for _, syncer := range m.inactiveSyncers {
6✔
266
                        syncer.Stop()
3✔
267
                }
3✔
268
                for _, syncer := range m.activeSyncers {
6✔
269
                        syncer.Stop()
3✔
270
                }
3✔
271
        })
272
}
273

274
// syncerHandler is the SyncManager's main event loop responsible for:
275
//
276
// 1. Creating and tearing down GossipSyncers for connected/disconnected peers.
277

278
// 2. Finding new peers to receive graph updates from to ensure we don't only
279
//    receive them from the same set of peers.
280

281
//  3. Finding new peers to force a historical sync with to ensure we have as
282
//     much of the public network as possible.
283
//
284
// NOTE: This must be run as a goroutine.
285
func (m *SyncManager) syncerHandler() {
3✔
286
        defer m.wg.Done()
3✔
287

3✔
288
        m.cfg.RotateTicker.Resume()
3✔
289
        defer m.cfg.RotateTicker.Stop()
3✔
290

3✔
291
        defer m.cfg.HistoricalSyncTicker.Stop()
3✔
292

3✔
293
        var (
3✔
294
                // initialHistoricalSyncer is the syncer we are currently
3✔
295
                // performing an initial historical sync with.
3✔
296
                initialHistoricalSyncer *GossipSyncer
3✔
297

3✔
298
                // initialHistoricalSyncSignal is a signal that will fire once
3✔
299
                // the initial historical sync has been completed. This is
3✔
300
                // crucial to ensure that another historical sync isn't
3✔
301
                // attempted just because the initialHistoricalSyncer was
3✔
302
                // disconnected.
3✔
303
                initialHistoricalSyncSignal chan struct{}
3✔
304
        )
3✔
305

3✔
306
        setInitialHistoricalSyncer := func(s *GossipSyncer) {
6✔
307
                initialHistoricalSyncer = s
3✔
308
                initialHistoricalSyncSignal = s.ResetSyncedSignal()
3✔
309

3✔
310
                // Restart the timer for our new historical sync peer. This will
3✔
311
                // ensure that all initial syncers receive an equivalent
3✔
312
                // duration before attempting the next sync. Without doing so we
3✔
313
                // might attempt two historical sync back to back if a peer
3✔
314
                // disconnects just before the ticker fires.
3✔
315
                m.cfg.HistoricalSyncTicker.Pause()
3✔
316
                m.cfg.HistoricalSyncTicker.Resume()
3✔
317
        }
3✔
318

319
        for {
6✔
320
                select {
3✔
321
                // A new peer has been connected, so we'll create its
322
                // accompanying GossipSyncer.
323
                case newSyncer := <-m.newSyncers:
3✔
324
                        // If we already have a syncer, then we'll exit early as
3✔
325
                        // we don't want to override it.
3✔
326
                        if _, ok := m.GossipSyncer(newSyncer.peer.PubKey()); ok {
3✔
327
                                close(newSyncer.doneChan)
×
328
                                continue
×
329
                        }
330

331
                        s := m.createGossipSyncer(newSyncer.peer)
3✔
332

3✔
333
                        isPinnedSyncer := m.isPinnedSyncer(s)
3✔
334

3✔
335
                        // attemptHistoricalSync determines whether we should
3✔
336
                        // attempt an initial historical sync when a new peer
3✔
337
                        // connects.
3✔
338
                        attemptHistoricalSync := false
3✔
339

3✔
340
                        m.syncersMu.Lock()
3✔
341
                        switch {
3✔
342
                        // For pinned syncers, we will immediately transition
343
                        // the peer into an active (pinned) sync state.
344
                        case isPinnedSyncer:
3✔
345
                                attemptHistoricalSync = true
3✔
346
                                s.setSyncType(PinnedSync)
3✔
347
                                s.setSyncState(syncerIdle)
3✔
348
                                m.pinnedActiveSyncers[s.cfg.peerPub] = s
3✔
349

350
                        // Regardless of whether the initial historical sync
351
                        // has completed, we'll re-trigger a historical sync if
352
                        // we no longer have any syncers. This might be
353
                        // necessary if we lost all our peers at one point, and
354
                        // now we finally have one again.
355
                        case len(m.activeSyncers) == 0 &&
356
                                len(m.inactiveSyncers) == 0:
3✔
357

3✔
358
                                attemptHistoricalSync =
3✔
359
                                        m.cfg.NumActiveSyncers > 0
3✔
360
                                fallthrough
3✔
361

362
                        // If we've exceeded our total number of active syncers,
363
                        // we'll initialize this GossipSyncer as passive.
364
                        case len(m.activeSyncers) >= m.cfg.NumActiveSyncers:
3✔
365
                                fallthrough
3✔
366

367
                        // If the initial historical sync has yet to complete,
368
                        // then we'll declare it as passive and attempt to
369
                        // transition it when the initial historical sync
370
                        // completes.
371
                        case !m.IsGraphSynced():
3✔
372
                                s.setSyncType(PassiveSync)
3✔
373
                                m.inactiveSyncers[s.cfg.peerPub] = s
3✔
374

375
                        // The initial historical sync has completed, so we can
376
                        // immediately start the GossipSyncer as active.
377
                        default:
3✔
378
                                s.setSyncType(ActiveSync)
3✔
379
                                m.activeSyncers[s.cfg.peerPub] = s
3✔
380
                        }
381
                        m.syncersMu.Unlock()
3✔
382

3✔
383
                        s.Start()
3✔
384

3✔
385
                        // Once we create the GossipSyncer, we'll signal to the
3✔
386
                        // caller that they can proceed since the SyncManager's
3✔
387
                        // internal state has been updated.
3✔
388
                        close(newSyncer.doneChan)
3✔
389

3✔
390
                        // We'll force a historical sync with the first peer we
3✔
391
                        // connect to, to ensure we get as much of the graph as
3✔
392
                        // possible.
3✔
393
                        if !attemptHistoricalSync {
6✔
394
                                continue
3✔
395
                        }
396

397
                        log.Debugf("Attempting initial historical sync with "+
3✔
398
                                "GossipSyncer(%x)", s.cfg.peerPub)
3✔
399

3✔
400
                        if err := s.historicalSync(); err != nil {
3✔
401
                                log.Errorf("Unable to attempt initial "+
×
402
                                        "historical sync with "+
×
403
                                        "GossipSyncer(%x): %v", s.cfg.peerPub,
×
404
                                        err)
×
405
                                continue
×
406
                        }
407

408
                        // Once the historical sync has started, we'll get a
409
                        // keep track of the corresponding syncer to properly
410
                        // handle disconnects. We'll also use a signal to know
411
                        // when the historical sync completed.
412
                        if !isPinnedSyncer {
6✔
413
                                setInitialHistoricalSyncer(s)
3✔
414
                        }
3✔
415

416
                // An existing peer has disconnected, so we'll tear down its
417
                // corresponding GossipSyncer.
418
                case staleSyncer := <-m.staleSyncers:
3✔
419
                        // Once the corresponding GossipSyncer has been stopped
3✔
420
                        // and removed, we'll signal to the caller that they can
3✔
421
                        // proceed since the SyncManager's internal state has
3✔
422
                        // been updated.
3✔
423
                        m.removeGossipSyncer(staleSyncer.peer)
3✔
424
                        close(staleSyncer.doneChan)
3✔
425

3✔
426
                        // If we don't have an initialHistoricalSyncer, or we do
3✔
427
                        // but it is not the peer being disconnected, then we
3✔
428
                        // have nothing left to do and can proceed.
3✔
429
                        switch {
3✔
430
                        case initialHistoricalSyncer == nil:
3✔
431
                                fallthrough
3✔
432
                        case staleSyncer.peer != initialHistoricalSyncer.cfg.peerPub:
3✔
433
                                fallthrough
3✔
434
                        case m.cfg.NumActiveSyncers == 0:
3✔
435
                                continue
3✔
436
                        }
437

438
                        // Otherwise, our initialHistoricalSyncer corresponds to
439
                        // the peer being disconnected, so we'll have to find a
440
                        // replacement.
UNCOV
441
                        log.Debug("Finding replacement for initial " +
×
UNCOV
442
                                "historical sync")
×
UNCOV
443

×
UNCOV
444
                        s := m.forceHistoricalSync()
×
UNCOV
445
                        if s == nil {
×
446
                                log.Debug("No eligible replacement found " +
×
447
                                        "for initial historical sync")
×
448
                                continue
×
449
                        }
450

UNCOV
451
                        log.Debugf("Replaced initial historical "+
×
UNCOV
452
                                "GossipSyncer(%v) with GossipSyncer(%x)",
×
UNCOV
453
                                staleSyncer.peer, s.cfg.peerPub)
×
UNCOV
454

×
UNCOV
455
                        setInitialHistoricalSyncer(s)
×
456

457
                // Our initial historical sync signal has completed, so we'll
458
                // nil all of the relevant fields as they're no longer needed.
459
                case <-initialHistoricalSyncSignal:
3✔
460
                        initialHistoricalSyncer = nil
3✔
461
                        initialHistoricalSyncSignal = nil
3✔
462

3✔
463
                        log.Debug("Initial historical sync completed")
3✔
464

3✔
465
                        // With the initial historical sync complete, we can
3✔
466
                        // begin receiving new graph updates at tip. We'll
3✔
467
                        // determine whether we can have any more active
3✔
468
                        // GossipSyncers. If we do, we'll randomly select some
3✔
469
                        // that are currently passive to transition.
3✔
470
                        m.syncersMu.Lock()
3✔
471
                        numActiveLeft := m.cfg.NumActiveSyncers - len(m.activeSyncers)
3✔
472
                        if numActiveLeft <= 0 {
3✔
473
                                m.syncersMu.Unlock()
×
474
                                continue
×
475
                        }
476

477
                        // We may not even have enough inactive syncers to be
478
                        // transitted. In that case, we will transit all the
479
                        // inactive syncers.
480
                        if len(m.inactiveSyncers) < numActiveLeft {
6✔
481
                                numActiveLeft = len(m.inactiveSyncers)
3✔
482
                        }
3✔
483

484
                        log.Debugf("Attempting to transition %v passive "+
3✔
485
                                "GossipSyncers to active", numActiveLeft)
3✔
486

3✔
487
                        for i := 0; i < numActiveLeft; i++ {
6✔
488
                                chooseRandomSyncer(
3✔
489
                                        m.inactiveSyncers, m.transitionPassiveSyncer,
3✔
490
                                )
3✔
491
                        }
3✔
492

493
                        m.syncersMu.Unlock()
3✔
494

495
                // Our RotateTicker has ticked, so we'll attempt to rotate a
496
                // single active syncer with a passive one.
UNCOV
497
                case <-m.cfg.RotateTicker.Ticks():
×
UNCOV
498
                        m.rotateActiveSyncerCandidate()
×
499

500
                // Our HistoricalSyncTicker has ticked, so we'll randomly select
501
                // a peer and force a historical sync with them.
UNCOV
502
                case <-m.cfg.HistoricalSyncTicker.Ticks():
×
UNCOV
503
                        // To be extra cautious, gate the forceHistoricalSync
×
UNCOV
504
                        // call such that it can only execute if we are
×
UNCOV
505
                        // configured to have a non-zero number of sync peers.
×
UNCOV
506
                        // This way even if the historical sync ticker manages
×
UNCOV
507
                        // to tick we can be sure that a historical sync won't
×
UNCOV
508
                        // accidentally begin.
×
UNCOV
509
                        if m.cfg.NumActiveSyncers == 0 {
×
UNCOV
510
                                continue
×
511
                        }
512

513
                        // If we don't have a syncer available we have nothing
514
                        // to do.
UNCOV
515
                        s := m.forceHistoricalSync()
×
UNCOV
516
                        if s == nil {
×
517
                                continue
×
518
                        }
519

520
                        // If we've already completed a historical sync, we'll
521
                        // skip setting the initial historical syncer.
UNCOV
522
                        if m.IsGraphSynced() {
×
523
                                continue
×
524
                        }
525

526
                        // Otherwise, we'll track the peer we've performed a
527
                        // historical sync with in order to handle the case
528
                        // where our previous historical sync peer did not
529
                        // respond to our queries and we haven't ingested as
530
                        // much of the graph as we should.
UNCOV
531
                        setInitialHistoricalSyncer(s)
×
532

533
                case <-m.quit:
3✔
534
                        return
3✔
535
                }
536
        }
537
}
538

539
// isPinnedSyncer returns true if the passed GossipSyncer is one of our pinned
540
// sync peers.
541
func (m *SyncManager) isPinnedSyncer(s *GossipSyncer) bool {
3✔
542
        _, isPinnedSyncer := m.cfg.PinnedSyncers[s.cfg.peerPub]
3✔
543
        return isPinnedSyncer
3✔
544
}
3✔
545

546
// deriveRateLimitReservation will take the current message and derive a
547
// reservation that can be used to wait on the rate limiter.
548
func (m *SyncManager) deriveRateLimitReservation(msg lnwire.Message,
549
) (*rate.Reservation, error) {
3✔
550

3✔
551
        var (
3✔
552
                msgSize uint32
3✔
553
                err     error
3✔
554
        )
3✔
555

3✔
556
        // Figure out the serialized size of the message. If we can't easily
3✔
557
        // compute it, then we'll used the assumed msg size.
3✔
558
        if sMsg, ok := msg.(lnwire.SizeableMessage); ok {
6✔
559
                msgSize, err = sMsg.SerializedSize()
3✔
560
                if err != nil {
3✔
UNCOV
561
                        return nil, err
×
UNCOV
562
                }
×
563
        } else {
×
564
                log.Warnf("Unable to compute serialized size of %T", msg)
×
565

×
566
                msgSize = assumedMsgSize
×
567
        }
×
568

569
        return m.rateLimiter.ReserveN(time.Now(), int(msgSize)), nil
3✔
570
}
571

572
// waitMsgDelay takes a delay, and waits until it has finished.
573
func (m *SyncManager) waitMsgDelay(ctx context.Context, peerPub [33]byte,
574
        limitReservation *rate.Reservation) error {
3✔
575

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

×
588
                select {
×
589
                case <-time.After(delay):
×
590

591
                case <-ctx.Done():
×
592
                        limitReservation.Cancel()
×
593

×
594
                        return ErrGossipSyncerExiting
×
595

596
                case <-m.quit:
×
597
                        limitReservation.Cancel()
×
598

×
599
                        return ErrGossipSyncerExiting
×
600
                }
601
        }
602

603
        return nil
3✔
604
}
605

606
// maybeRateLimitMsg takes a message, and may wait a period of time to rate
607
// limit the msg.
608
func (m *SyncManager) maybeRateLimitMsg(ctx context.Context, peerPub [33]byte,
609
        msg lnwire.Message) error {
3✔
610

3✔
611
        delay, err := m.deriveRateLimitReservation(msg)
3✔
612
        if err != nil {
3✔
613
                return nil
×
614
        }
×
615

616
        return m.waitMsgDelay(ctx, peerPub, delay)
3✔
617
}
618

619
// sendMessages sends a set of messages to the remote peer.
620
func (m *SyncManager) sendMessages(ctx context.Context, sync bool,
621
        peer lnpeer.Peer, nodeID route.Vertex, msgs ...lnwire.Message) error {
3✔
622

3✔
623
        for _, msg := range msgs {
6✔
624
                if err := m.maybeRateLimitMsg(ctx, nodeID, msg); err != nil {
3✔
625
                        return err
×
626
                }
×
627
                if err := peer.SendMessageLazy(sync, msg); err != nil {
3✔
628
                        return err
×
629
                }
×
630
        }
631

632
        return nil
3✔
633
}
634

635
// createGossipSyncer creates the GossipSyncer for a newly connected peer.
636
func (m *SyncManager) createGossipSyncer(peer lnpeer.Peer) *GossipSyncer {
3✔
637
        nodeID := route.Vertex(peer.PubKey())
3✔
638
        log.Infof("Creating new GossipSyncer for peer=%x", nodeID[:])
3✔
639

3✔
640
        encoding := lnwire.EncodingSortedPlain
3✔
641
        s := newGossipSyncer(gossipSyncerCfg{
3✔
642
                chainHash:     m.cfg.ChainHash,
3✔
643
                peerPub:       nodeID,
3✔
644
                channelSeries: m.cfg.ChanSeries,
3✔
645
                encodingType:  encoding,
3✔
646
                chunkSize:     encodingTypeToChunkSize[encoding],
3✔
647
                batchSize:     requestBatchSize,
3✔
648
                sendToPeer: func(ctx context.Context,
3✔
649
                        msgs ...lnwire.Message) error {
6✔
650

3✔
651
                        return m.sendMessages(ctx, false, peer, nodeID, msgs...)
3✔
652
                },
3✔
653
                sendToPeerSync: func(ctx context.Context,
654
                        msgs ...lnwire.Message) error {
3✔
655

3✔
656
                        return m.sendMessages(ctx, true, peer, nodeID, msgs...)
3✔
657
                },
3✔
658
                ignoreHistoricalFilters:  m.cfg.IgnoreHistoricalFilters,
659
                bestHeight:               m.cfg.BestHeight,
660
                markGraphSynced:          m.markGraphSynced,
661
                maxQueryChanRangeReplies: maxQueryChanRangeReplies,
662
                noTimestampQueryOption:   m.cfg.NoTimestampQueries,
663
                isStillZombieChannel:     m.cfg.IsStillZombieChannel,
664
        }, m.gossipFilterSema)
665

666
        // Gossip syncers are initialized by default in a PassiveSync type
667
        // and chansSynced state so that they can reply to any peer queries or
668
        // handle any sync transitions.
669
        s.setSyncState(chansSynced)
3✔
670
        s.setSyncType(PassiveSync)
3✔
671

3✔
672
        log.Debugf("Created new GossipSyncer[state=%s type=%s] for peer=%x",
3✔
673
                s.syncState(), s.SyncType(), peer.PubKey())
3✔
674

3✔
675
        return s
3✔
676
}
677

678
// removeGossipSyncer removes all internal references to the disconnected peer's
679
// GossipSyncer and stops it. In the event of an active GossipSyncer being
680
// disconnected, a passive GossipSyncer, if any, will take its place.
681
func (m *SyncManager) removeGossipSyncer(peer route.Vertex) {
3✔
682
        m.syncersMu.Lock()
3✔
683
        defer m.syncersMu.Unlock()
3✔
684

3✔
685
        s, ok := m.gossipSyncer(peer)
3✔
686
        if !ok {
6✔
687
                return
3✔
688
        }
3✔
689

690
        log.Infof("Removing GossipSyncer for peer=%v", peer)
3✔
691

3✔
692
        // We'll stop the GossipSyncer for the disconnected peer in a goroutine
3✔
693
        // to prevent blocking the SyncManager.
3✔
694
        go s.Stop()
3✔
695

3✔
696
        // If it's a non-active syncer, then we can just exit now.
3✔
697
        if _, ok := m.inactiveSyncers[peer]; ok {
6✔
698
                delete(m.inactiveSyncers, peer)
3✔
699
                return
3✔
700
        }
3✔
701

702
        // If it's a pinned syncer, then we can just exit as this doesn't
703
        // affect our active syncer count.
704
        if _, ok := m.pinnedActiveSyncers[peer]; ok {
6✔
705
                delete(m.pinnedActiveSyncers, peer)
3✔
706
                return
3✔
707
        }
3✔
708

709
        // Otherwise, we'll need find a new one to replace it, if any.
710
        delete(m.activeSyncers, peer)
3✔
711
        newActiveSyncer := chooseRandomSyncer(
3✔
712
                m.inactiveSyncers, m.transitionPassiveSyncer,
3✔
713
        )
3✔
714
        if newActiveSyncer == nil {
6✔
715
                return
3✔
716
        }
3✔
717

718
        log.Debugf("Replaced active GossipSyncer(%v) with GossipSyncer(%x)",
2✔
719
                peer, newActiveSyncer.cfg.peerPub)
2✔
720
}
721

722
// rotateActiveSyncerCandidate rotates a single active syncer. In order to
723
// achieve this, the active syncer must be in a chansSynced state in order to
724
// process the sync transition.
UNCOV
725
func (m *SyncManager) rotateActiveSyncerCandidate() {
×
UNCOV
726
        m.syncersMu.Lock()
×
UNCOV
727
        defer m.syncersMu.Unlock()
×
UNCOV
728

×
UNCOV
729
        // If we couldn't find an eligible active syncer to rotate, we can
×
UNCOV
730
        // return early.
×
UNCOV
731
        activeSyncer := chooseRandomSyncer(m.activeSyncers, nil)
×
UNCOV
732
        if activeSyncer == nil {
×
733
                log.Debug("No eligible active syncer to rotate")
×
734
                return
×
735
        }
×
736

737
        // Similarly, if we don't have a candidate to rotate with, we can return
738
        // early as well.
UNCOV
739
        candidate := chooseRandomSyncer(m.inactiveSyncers, nil)
×
UNCOV
740
        if candidate == nil {
×
UNCOV
741
                log.Debug("No eligible candidate to rotate active syncer")
×
UNCOV
742
                return
×
UNCOV
743
        }
×
744

745
        // Otherwise, we'll attempt to transition each syncer to their
746
        // respective new sync type.
UNCOV
747
        log.Debugf("Rotating active GossipSyncer(%x) with GossipSyncer(%x)",
×
UNCOV
748
                activeSyncer.cfg.peerPub, candidate.cfg.peerPub)
×
UNCOV
749

×
UNCOV
750
        if err := m.transitionActiveSyncer(activeSyncer); err != nil {
×
751
                log.Errorf("Unable to transition active GossipSyncer(%x): %v",
×
752
                        activeSyncer.cfg.peerPub, err)
×
753
                return
×
754
        }
×
755

UNCOV
756
        if err := m.transitionPassiveSyncer(candidate); err != nil {
×
757
                log.Errorf("Unable to transition passive GossipSyncer(%x): %v",
×
758
                        activeSyncer.cfg.peerPub, err)
×
759
                return
×
760
        }
×
761
}
762

763
// transitionActiveSyncer transitions an active syncer to a passive one.
764
//
765
// NOTE: This must be called with the syncersMu lock held.
UNCOV
766
func (m *SyncManager) transitionActiveSyncer(s *GossipSyncer) error {
×
UNCOV
767
        log.Debugf("Transitioning active GossipSyncer(%x) to passive",
×
UNCOV
768
                s.cfg.peerPub)
×
UNCOV
769

×
UNCOV
770
        if err := s.ProcessSyncTransition(PassiveSync); err != nil {
×
771
                return err
×
772
        }
×
773

UNCOV
774
        delete(m.activeSyncers, s.cfg.peerPub)
×
UNCOV
775
        m.inactiveSyncers[s.cfg.peerPub] = s
×
UNCOV
776

×
UNCOV
777
        return nil
×
778
}
779

780
// transitionPassiveSyncer transitions a passive syncer to an active one.
781
//
782
// NOTE: This must be called with the syncersMu lock held.
783
func (m *SyncManager) transitionPassiveSyncer(s *GossipSyncer) error {
3✔
784
        log.Debugf("Transitioning passive GossipSyncer(%x) to active",
3✔
785
                s.cfg.peerPub)
3✔
786

3✔
787
        if err := s.ProcessSyncTransition(ActiveSync); err != nil {
3✔
788
                return err
×
789
        }
×
790

791
        delete(m.inactiveSyncers, s.cfg.peerPub)
3✔
792
        m.activeSyncers[s.cfg.peerPub] = s
3✔
793

3✔
794
        return nil
3✔
795
}
796

797
// forceHistoricalSync chooses a syncer with a remote peer at random and forces
798
// a historical sync with it.
UNCOV
799
func (m *SyncManager) forceHistoricalSync() *GossipSyncer {
×
UNCOV
800
        m.syncersMu.Lock()
×
UNCOV
801
        defer m.syncersMu.Unlock()
×
UNCOV
802

×
UNCOV
803
        // We'll sample from both sets of active and inactive syncers in the
×
UNCOV
804
        // event that we don't have any inactive syncers.
×
UNCOV
805
        return chooseRandomSyncer(m.gossipSyncers(), func(s *GossipSyncer) error {
×
UNCOV
806
                return s.historicalSync()
×
UNCOV
807
        })
×
808
}
809

810
// chooseRandomSyncer iterates through the set of syncers given and returns the
811
// first one which was able to successfully perform the action enclosed in the
812
// function closure.
813
//
814
// NOTE: It's possible for a nil value to be returned if there are no eligible
815
// candidate syncers.
816
func chooseRandomSyncer(syncers map[route.Vertex]*GossipSyncer,
817
        action func(*GossipSyncer) error) *GossipSyncer {
3✔
818

3✔
819
        for _, s := range syncers {
6✔
820
                // Only syncers in a chansSynced state are viable for sync
3✔
821
                // transitions, so skip any that aren't.
3✔
822
                if s.syncState() != chansSynced {
3✔
UNCOV
823
                        continue
×
824
                }
825

826
                if action != nil {
6✔
827
                        if err := action(s); err != nil {
3✔
828
                                log.Debugf("Skipping eligible candidate "+
×
829
                                        "GossipSyncer(%x): %v", s.cfg.peerPub,
×
830
                                        err)
×
831
                                continue
×
832
                        }
833
                }
834

835
                return s
3✔
836
        }
837

838
        return nil
3✔
839
}
840

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

3✔
852
        select {
3✔
853
        case m.newSyncers <- &newSyncer{
854
                peer:     peer,
855
                doneChan: done,
856
        }:
3✔
857
        case <-m.quit:
×
858
                return ErrSyncManagerExiting
×
859
        }
860

861
        select {
3✔
862
        case <-done:
3✔
863
                return nil
3✔
864
        case <-m.quit:
×
865
                return ErrSyncManagerExiting
×
866
        }
867
}
868

869
// PruneSyncState is called by outside sub-systems once a peer that we were
870
// previously connected to has been disconnected. In this case we can stop the
871
// existing GossipSyncer assigned to the peer and free up resources.
872
func (m *SyncManager) PruneSyncState(peer route.Vertex) {
3✔
873
        done := make(chan struct{})
3✔
874

3✔
875
        // We avoid returning an error when the SyncManager is stopped since the
3✔
876
        // GossipSyncer will be stopped then anyway.
3✔
877
        select {
3✔
878
        case m.staleSyncers <- &staleSyncer{
879
                peer:     peer,
880
                doneChan: done,
881
        }:
3✔
882
        case <-m.quit:
×
883
                return
×
884
        }
885

886
        select {
3✔
887
        case <-done:
3✔
888
        case <-m.quit:
×
889
        }
890
}
891

892
// GossipSyncer returns the associated gossip syncer of a peer. The boolean
893
// returned signals whether there exists a gossip syncer for the peer.
894
func (m *SyncManager) GossipSyncer(peer route.Vertex) (*GossipSyncer, bool) {
3✔
895
        m.syncersMu.Lock()
3✔
896
        defer m.syncersMu.Unlock()
3✔
897
        return m.gossipSyncer(peer)
3✔
898
}
3✔
899

900
// gossipSyncer returns the associated gossip syncer of a peer. The boolean
901
// returned signals whether there exists a gossip syncer for the peer.
902
func (m *SyncManager) gossipSyncer(peer route.Vertex) (*GossipSyncer, bool) {
3✔
903
        syncer, ok := m.inactiveSyncers[peer]
3✔
904
        if ok {
6✔
905
                return syncer, true
3✔
906
        }
3✔
907
        syncer, ok = m.activeSyncers[peer]
3✔
908
        if ok {
6✔
909
                return syncer, true
3✔
910
        }
3✔
911
        syncer, ok = m.pinnedActiveSyncers[peer]
3✔
912
        if ok {
6✔
913
                return syncer, true
3✔
914
        }
3✔
915
        return nil, false
3✔
916
}
917

918
// GossipSyncers returns all of the currently initialized gossip syncers.
919
func (m *SyncManager) GossipSyncers() map[route.Vertex]*GossipSyncer {
3✔
920
        m.syncersMu.Lock()
3✔
921
        defer m.syncersMu.Unlock()
3✔
922
        return m.gossipSyncers()
3✔
923
}
3✔
924

925
// gossipSyncers returns all of the currently initialized gossip syncers.
926
func (m *SyncManager) gossipSyncers() map[route.Vertex]*GossipSyncer {
3✔
927
        numSyncers := len(m.inactiveSyncers) + len(m.activeSyncers)
3✔
928
        syncers := make(map[route.Vertex]*GossipSyncer, numSyncers)
3✔
929

3✔
930
        for _, syncer := range m.inactiveSyncers {
6✔
931
                syncers[syncer.cfg.peerPub] = syncer
3✔
932
        }
3✔
933
        for _, syncer := range m.activeSyncers {
6✔
934
                syncers[syncer.cfg.peerPub] = syncer
3✔
935
        }
3✔
936

937
        return syncers
3✔
938
}
939

940
// markGraphSynced allows us to report that the initial historical sync has
941
// completed.
942
func (m *SyncManager) markGraphSynced() {
3✔
943
        atomic.StoreInt32(&m.initialHistoricalSyncCompleted, 1)
3✔
944
}
3✔
945

946
// IsGraphSynced determines whether we've completed our initial historical sync.
947
// The initial historical sync is done to ensure we've ingested as much of the
948
// public graph as possible.
949
func (m *SyncManager) IsGraphSynced() bool {
3✔
950
        return atomic.LoadInt32(&m.initialHistoricalSyncCompleted) == 1
3✔
951
}
3✔
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