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

lightningnetwork / lnd / 18016273007

25 Sep 2025 05:55PM UTC coverage: 54.653% (-12.0%) from 66.622%
18016273007

Pull #10248

github

web-flow
Merge 128443298 into b09b20c69
Pull Request #10248: Enforce TLV when creating a Route

25 of 30 new or added lines in 4 files covered. (83.33%)

23906 existing lines in 281 files now uncovered.

109536 of 200421 relevant lines covered (54.65%)

21816.97 hits per line

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

69.9
/discovery/gossiper.go
1
package discovery
2

3
import (
4
        "bytes"
5
        "context"
6
        "errors"
7
        "fmt"
8
        "strings"
9
        "sync"
10
        "sync/atomic"
11
        "time"
12

13
        "github.com/btcsuite/btcd/btcec/v2"
14
        "github.com/btcsuite/btcd/btcec/v2/ecdsa"
15
        "github.com/btcsuite/btcd/btcutil"
16
        "github.com/btcsuite/btcd/chaincfg"
17
        "github.com/btcsuite/btcd/chaincfg/chainhash"
18
        "github.com/btcsuite/btcd/txscript"
19
        "github.com/btcsuite/btcd/wire"
20
        "github.com/lightninglabs/neutrino/cache"
21
        "github.com/lightninglabs/neutrino/cache/lru"
22
        "github.com/lightningnetwork/lnd/batch"
23
        "github.com/lightningnetwork/lnd/chainntnfs"
24
        "github.com/lightningnetwork/lnd/channeldb"
25
        "github.com/lightningnetwork/lnd/fn/v2"
26
        "github.com/lightningnetwork/lnd/graph"
27
        graphdb "github.com/lightningnetwork/lnd/graph/db"
28
        "github.com/lightningnetwork/lnd/graph/db/models"
29
        "github.com/lightningnetwork/lnd/input"
30
        "github.com/lightningnetwork/lnd/keychain"
31
        "github.com/lightningnetwork/lnd/lnpeer"
32
        "github.com/lightningnetwork/lnd/lnutils"
33
        "github.com/lightningnetwork/lnd/lnwallet"
34
        "github.com/lightningnetwork/lnd/lnwallet/btcwallet"
35
        "github.com/lightningnetwork/lnd/lnwallet/chanvalidate"
36
        "github.com/lightningnetwork/lnd/lnwire"
37
        "github.com/lightningnetwork/lnd/multimutex"
38
        "github.com/lightningnetwork/lnd/netann"
39
        "github.com/lightningnetwork/lnd/routing/route"
40
        "github.com/lightningnetwork/lnd/ticker"
41
        "golang.org/x/time/rate"
42
)
43

44
const (
45
        // DefaultMaxChannelUpdateBurst is the default maximum number of updates
46
        // for a specific channel and direction that we'll accept over an
47
        // interval.
48
        DefaultMaxChannelUpdateBurst = 10
49

50
        // DefaultChannelUpdateInterval is the default interval we'll use to
51
        // determine how often we should allow a new update for a specific
52
        // channel and direction.
53
        DefaultChannelUpdateInterval = time.Minute
54

55
        // maxPrematureUpdates tracks the max amount of premature channel
56
        // updates that we'll hold onto.
57
        maxPrematureUpdates = 100
58

59
        // maxFutureMessages tracks the max amount of future messages that
60
        // we'll hold onto.
61
        maxFutureMessages = 1000
62

63
        // DefaultSubBatchDelay is the default delay we'll use when
64
        // broadcasting the next announcement batch.
65
        DefaultSubBatchDelay = 5 * time.Second
66

67
        // maxRejectedUpdates tracks the max amount of rejected channel updates
68
        // we'll maintain. This is the global size across all peers. We'll
69
        // allocate ~3 MB max to the cache.
70
        maxRejectedUpdates = 10_000
71

72
        // DefaultProofMatureDelta specifies the default value used for
73
        // ProofMatureDelta, which is the number of confirmations needed before
74
        // processing the announcement signatures.
75
        DefaultProofMatureDelta = 6
76
)
77

78
var (
79
        // ErrGossiperShuttingDown is an error that is returned if the gossiper
80
        // is in the process of being shut down.
81
        ErrGossiperShuttingDown = errors.New("gossiper is shutting down")
82

83
        // ErrGossipSyncerNotFound signals that we were unable to find an active
84
        // gossip syncer corresponding to a gossip query message received from
85
        // the remote peer.
86
        ErrGossipSyncerNotFound = errors.New("gossip syncer not found")
87

88
        // ErrNoFundingTransaction is returned when we are unable to find the
89
        // funding transaction described by the short channel ID on chain.
90
        ErrNoFundingTransaction = errors.New(
91
                "unable to find the funding transaction",
92
        )
93

94
        // ErrInvalidFundingOutput is returned if the channel funding output
95
        // fails validation.
96
        ErrInvalidFundingOutput = errors.New(
97
                "channel funding output validation failed",
98
        )
99

100
        // ErrChannelSpent is returned when we go to validate a channel, but
101
        // the purported funding output has actually already been spent on
102
        // chain.
103
        ErrChannelSpent = errors.New("channel output has been spent")
104

105
        // emptyPubkey is used to compare compressed pubkeys against an empty
106
        // byte array.
107
        emptyPubkey [33]byte
108
)
109

110
// optionalMsgFields is a set of optional message fields that external callers
111
// can provide that serve useful when processing a specific network
112
// announcement.
113
type optionalMsgFields struct {
114
        capacity      *btcutil.Amount
115
        channelPoint  *wire.OutPoint
116
        remoteAlias   *lnwire.ShortChannelID
117
        tapscriptRoot fn.Option[chainhash.Hash]
118
}
119

120
// apply applies the optional fields within the functional options.
121
func (f *optionalMsgFields) apply(optionalMsgFields ...OptionalMsgField) {
47✔
122
        for _, optionalMsgField := range optionalMsgFields {
52✔
123
                optionalMsgField(f)
5✔
124
        }
5✔
125
}
126

127
// OptionalMsgField is a functional option parameter that can be used to provide
128
// external information that is not included within a network message but serves
129
// useful when processing it.
130
type OptionalMsgField func(*optionalMsgFields)
131

132
// ChannelCapacity is an optional field that lets the gossiper know of the
133
// capacity of a channel.
134
func ChannelCapacity(capacity btcutil.Amount) OptionalMsgField {
27✔
135
        return func(f *optionalMsgFields) {
28✔
136
                f.capacity = &capacity
1✔
137
        }
1✔
138
}
139

140
// ChannelPoint is an optional field that lets the gossiper know of the outpoint
141
// of a channel.
142
func ChannelPoint(op wire.OutPoint) OptionalMsgField {
30✔
143
        return func(f *optionalMsgFields) {
34✔
144
                f.channelPoint = &op
4✔
145
        }
4✔
146
}
147

148
// TapscriptRoot is an optional field that lets the gossiper know of the root of
149
// the tapscript tree for a custom channel.
150
func TapscriptRoot(root fn.Option[chainhash.Hash]) OptionalMsgField {
26✔
151
        return func(f *optionalMsgFields) {
26✔
UNCOV
152
                f.tapscriptRoot = root
×
UNCOV
153
        }
×
154
}
155

156
// RemoteAlias is an optional field that lets the gossiper know that a locally
157
// sent channel update is actually an update for the peer that should replace
158
// the ShortChannelID field with the remote's alias. This is only used for
159
// channels with peers where the option-scid-alias feature bit was negotiated.
160
// The channel update will be added to the graph under the original SCID, but
161
// will be modified and re-signed with this alias.
162
func RemoteAlias(alias *lnwire.ShortChannelID) OptionalMsgField {
26✔
163
        return func(f *optionalMsgFields) {
26✔
UNCOV
164
                f.remoteAlias = alias
×
UNCOV
165
        }
×
166
}
167

168
// networkMsg couples a routing related wire message with the peer that
169
// originally sent it.
170
type networkMsg struct {
171
        peer              lnpeer.Peer
172
        source            *btcec.PublicKey
173
        msg               lnwire.Message
174
        optionalMsgFields *optionalMsgFields
175

176
        isRemote bool
177

178
        err chan error
179
}
180

181
// chanPolicyUpdateRequest is a request that is sent to the server when a caller
182
// wishes to update a particular set of channels. New ChannelUpdate messages
183
// will be crafted to be sent out during the next broadcast epoch and the fee
184
// updates committed to the lower layer.
185
type chanPolicyUpdateRequest struct {
186
        edgesToUpdate []EdgeWithInfo
187
        errChan       chan error
188
}
189

190
// PinnedSyncers is a set of node pubkeys for which we will maintain an active
191
// syncer at all times.
192
type PinnedSyncers map[route.Vertex]struct{}
193

194
// Config defines the configuration for the service. ALL elements within the
195
// configuration MUST be non-nil for the service to carry out its duties.
196
type Config struct {
197
        // ChainParams holds the chain parameters for the active network this
198
        // node is participating on.
199
        ChainParams *chaincfg.Params
200

201
        // Graph is the subsystem which is responsible for managing the
202
        // topology of lightning network. After incoming channel, node, channel
203
        // updates announcements are validated they are sent to the router in
204
        // order to be included in the LN graph.
205
        Graph graph.ChannelGraphSource
206

207
        // ChainIO represents an abstraction over a source that can query the
208
        // blockchain.
209
        ChainIO lnwallet.BlockChainIO
210

211
        // ChanSeries is an interfaces that provides access to a time series
212
        // view of the current known channel graph. Each GossipSyncer enabled
213
        // peer will utilize this in order to create and respond to channel
214
        // graph time series queries.
215
        ChanSeries ChannelGraphTimeSeries
216

217
        // Notifier is used for receiving notifications of incoming blocks.
218
        // With each new incoming block found we process previously premature
219
        // announcements.
220
        //
221
        // TODO(roasbeef): could possibly just replace this with an epoch
222
        // channel.
223
        Notifier chainntnfs.ChainNotifier
224

225
        // Broadcast broadcasts a particular set of announcements to all peers
226
        // that the daemon is connected to. If supplied, the exclude parameter
227
        // indicates that the target peer should be excluded from the
228
        // broadcast.
229
        Broadcast func(skips map[route.Vertex]struct{},
230
                msg ...lnwire.Message) error
231

232
        // NotifyWhenOnline is a function that allows the gossiper to be
233
        // notified when a certain peer comes online, allowing it to
234
        // retry sending a peer message.
235
        //
236
        // NOTE: The peerChan channel must be buffered.
237
        NotifyWhenOnline func(peerPubKey [33]byte, peerChan chan<- lnpeer.Peer)
238

239
        // NotifyWhenOffline is a function that allows the gossiper to be
240
        // notified when a certain peer disconnects, allowing it to request a
241
        // notification for when it reconnects.
242
        NotifyWhenOffline func(peerPubKey [33]byte) <-chan struct{}
243

244
        // FetchSelfAnnouncement retrieves our current node announcement, for
245
        // use when determining whether we should update our peers about our
246
        // presence in the network.
247
        FetchSelfAnnouncement func() lnwire.NodeAnnouncement
248

249
        // UpdateSelfAnnouncement produces a new announcement for our node with
250
        // an updated timestamp which can be broadcast to our peers.
251
        UpdateSelfAnnouncement func() (lnwire.NodeAnnouncement, error)
252

253
        // ProofMatureDelta the number of confirmations which is needed before
254
        // exchange the channel announcement proofs.
255
        ProofMatureDelta uint32
256

257
        // TrickleDelay the period of trickle timer which flushes to the
258
        // network the pending batch of new announcements we've received since
259
        // the last trickle tick.
260
        TrickleDelay time.Duration
261

262
        // RetransmitTicker is a ticker that ticks with a period which
263
        // indicates that we should check if we need re-broadcast any of our
264
        // personal channels.
265
        RetransmitTicker ticker.Ticker
266

267
        // RebroadcastInterval is the maximum time we wait between sending out
268
        // channel updates for our active channels and our own node
269
        // announcement. We do this to ensure our active presence on the
270
        // network is known, and we are not being considered a zombie node or
271
        // having zombie channels.
272
        RebroadcastInterval time.Duration
273

274
        // WaitingProofStore is a persistent storage of partial channel proof
275
        // announcement messages. We use it to buffer half of the material
276
        // needed to reconstruct a full authenticated channel announcement.
277
        // Once we receive the other half the channel proof, we'll be able to
278
        // properly validate it and re-broadcast it out to the network.
279
        //
280
        // TODO(wilmer): make interface to prevent channeldb dependency.
281
        WaitingProofStore *channeldb.WaitingProofStore
282

283
        // MessageStore is a persistent storage of gossip messages which we will
284
        // use to determine which messages need to be resent for a given peer.
285
        MessageStore GossipMessageStore
286

287
        // AnnSigner is an instance of the MessageSigner interface which will
288
        // be used to manually sign any outgoing channel updates. The signer
289
        // implementation should be backed by the public key of the backing
290
        // Lightning node.
291
        //
292
        // TODO(roasbeef): extract ann crafting + sign from fundingMgr into
293
        // here?
294
        AnnSigner lnwallet.MessageSigner
295

296
        // ScidCloser is an instance of ClosedChannelTracker that helps the
297
        // gossiper cut down on spam channel announcements for already closed
298
        // channels.
299
        ScidCloser ClosedChannelTracker
300

301
        // NumActiveSyncers is the number of peers for which we should have
302
        // active syncers with. After reaching NumActiveSyncers, any future
303
        // gossip syncers will be passive.
304
        NumActiveSyncers int
305

306
        // NoTimestampQueries will prevent the GossipSyncer from querying
307
        // timestamps of announcement messages from the peer and from replying
308
        // to timestamp queries.
309
        NoTimestampQueries bool
310

311
        // RotateTicker is a ticker responsible for notifying the SyncManager
312
        // when it should rotate its active syncers. A single active syncer with
313
        // a chansSynced state will be exchanged for a passive syncer in order
314
        // to ensure we don't keep syncing with the same peers.
315
        RotateTicker ticker.Ticker
316

317
        // HistoricalSyncTicker is a ticker responsible for notifying the
318
        // syncManager when it should attempt a historical sync with a gossip
319
        // sync peer.
320
        HistoricalSyncTicker ticker.Ticker
321

322
        // ActiveSyncerTimeoutTicker is a ticker responsible for notifying the
323
        // syncManager when it should attempt to start the next pending
324
        // activeSyncer due to the current one not completing its state machine
325
        // within the timeout.
326
        ActiveSyncerTimeoutTicker ticker.Ticker
327

328
        // MinimumBatchSize is minimum size of a sub batch of announcement
329
        // messages.
330
        MinimumBatchSize int
331

332
        // SubBatchDelay is the delay between sending sub batches of
333
        // gossip messages.
334
        SubBatchDelay time.Duration
335

336
        // IgnoreHistoricalFilters will prevent syncers from replying with
337
        // historical data when the remote peer sets a gossip_timestamp_range.
338
        // This prevents ranges with old start times from causing us to dump the
339
        // graph on connect.
340
        IgnoreHistoricalFilters bool
341

342
        // PinnedSyncers is a set of peers that will always transition to
343
        // ActiveSync upon connection. These peers will never transition to
344
        // PassiveSync.
345
        PinnedSyncers PinnedSyncers
346

347
        // MaxChannelUpdateBurst specifies the maximum number of updates for a
348
        // specific channel and direction that we'll accept over an interval.
349
        MaxChannelUpdateBurst int
350

351
        // ChannelUpdateInterval specifies the interval we'll use to determine
352
        // how often we should allow a new update for a specific channel and
353
        // direction.
354
        ChannelUpdateInterval time.Duration
355

356
        // IsAlias returns true if a given ShortChannelID is an alias for
357
        // option_scid_alias channels.
358
        IsAlias func(scid lnwire.ShortChannelID) bool
359

360
        // SignAliasUpdate is used to re-sign a channel update using the
361
        // remote's alias if the option-scid-alias feature bit was negotiated.
362
        SignAliasUpdate func(u *lnwire.ChannelUpdate1) (*ecdsa.Signature,
363
                error)
364

365
        // FindBaseByAlias finds the SCID stored in the graph by an alias SCID.
366
        // This is used for channels that have negotiated the option-scid-alias
367
        // feature bit.
368
        FindBaseByAlias func(alias lnwire.ShortChannelID) (
369
                lnwire.ShortChannelID, error)
370

371
        // GetAlias allows the gossiper to look up the peer's alias for a given
372
        // ChannelID. This is used to sign updates for them if the channel has
373
        // no AuthProof and the option-scid-alias feature bit was negotiated.
374
        GetAlias func(lnwire.ChannelID) (lnwire.ShortChannelID, error)
375

376
        // FindChannel allows the gossiper to find a channel that we're party
377
        // to without iterating over the entire set of open channels.
378
        FindChannel func(node *btcec.PublicKey, chanID lnwire.ChannelID) (
379
                *channeldb.OpenChannel, error)
380

381
        // IsStillZombieChannel takes the timestamps of the latest channel
382
        // updates for a channel and returns true if the channel should be
383
        // considered a zombie based on these timestamps.
384
        IsStillZombieChannel func(time.Time, time.Time) bool
385

386
        // AssumeChannelValid toggles whether the gossiper will check for
387
        // spent-ness of channel outpoints. For neutrino, this saves long
388
        // rescans from blocking initial usage of the daemon.
389
        AssumeChannelValid bool
390

391
        // MsgRateBytes is the rate limit for the number of bytes per second
392
        // that we'll allocate to outbound gossip messages.
393
        MsgRateBytes uint64
394

395
        // MsgBurstBytes is the allotted burst amount in bytes. This is the
396
        // number of starting tokens in our token bucket algorithm.
397
        MsgBurstBytes uint64
398

399
        // FilterConcurrency is the maximum number of concurrent gossip filter
400
        // applications that can be processed.
401
        FilterConcurrency int
402

403
        // BanThreshold is the score used to decide whether a given peer is
404
        // banned or not.
405
        BanThreshold uint64
406

407
        // PeerMsgRateBytes is the rate limit for the number of bytes per second
408
        // that we'll allocate to outbound gossip messages for a single peer.
409
        PeerMsgRateBytes uint64
410
}
411

412
// processedNetworkMsg is a wrapper around networkMsg and a boolean. It is
413
// used to let the caller of the lru.Cache know if a message has already been
414
// processed or not.
415
type processedNetworkMsg struct {
416
        processed bool
417
        msg       *networkMsg
418
}
419

420
// cachedNetworkMsg is a wrapper around a network message that can be used with
421
// *lru.Cache.
422
//
423
// NOTE: This struct is not thread safe which means you need to assure no
424
// concurrent read write access to it and all its contents which are pointers
425
// as well.
426
type cachedNetworkMsg struct {
427
        msgs []*processedNetworkMsg
428
}
429

430
// Size returns the "size" of an entry. We return the number of items as we
431
// just want to limit the total amount of entries rather than do accurate size
432
// accounting.
433
func (c *cachedNetworkMsg) Size() (uint64, error) {
2✔
434
        return uint64(len(c.msgs)), nil
2✔
435
}
2✔
436

437
// rejectCacheKey is the cache key that we'll use to track announcements we've
438
// recently rejected.
439
type rejectCacheKey struct {
440
        pubkey [33]byte
441
        chanID uint64
442
}
443

444
// newRejectCacheKey returns a new cache key for the reject cache.
445
func newRejectCacheKey(cid uint64, pub [33]byte) rejectCacheKey {
472✔
446
        k := rejectCacheKey{
472✔
447
                chanID: cid,
472✔
448
                pubkey: pub,
472✔
449
        }
472✔
450

472✔
451
        return k
472✔
452
}
472✔
453

454
// sourceToPub returns a serialized-compressed public key for use in the reject
455
// cache.
456
func sourceToPub(pk *btcec.PublicKey) [33]byte {
486✔
457
        var pub [33]byte
486✔
458
        copy(pub[:], pk.SerializeCompressed())
486✔
459
        return pub
486✔
460
}
486✔
461

462
// cachedReject is the empty value used to track the value for rejects.
463
type cachedReject struct {
464
}
465

466
// Size returns the "size" of an entry. We return 1 as we just want to limit
467
// the total size.
468
func (c *cachedReject) Size() (uint64, error) {
206✔
469
        return 1, nil
206✔
470
}
206✔
471

472
// AuthenticatedGossiper is a subsystem which is responsible for receiving
473
// announcements, validating them and applying the changes to router, syncing
474
// lightning network with newly connected nodes, broadcasting announcements
475
// after validation, negotiating the channel announcement proofs exchange and
476
// handling the premature announcements. All outgoing announcements are
477
// expected to be properly signed as dictated in BOLT#7, additionally, all
478
// incoming message are expected to be well formed and signed. Invalid messages
479
// will be rejected by this struct.
480
type AuthenticatedGossiper struct {
481
        // Parameters which are needed to properly handle the start and stop of
482
        // the service.
483
        started sync.Once
484
        stopped sync.Once
485

486
        // bestHeight is the height of the block at the tip of the main chain
487
        // as we know it. Accesses *MUST* be done with the gossiper's lock
488
        // held.
489
        bestHeight uint32
490

491
        // cfg is a copy of the configuration struct that the gossiper service
492
        // was initialized with.
493
        cfg *Config
494

495
        // blockEpochs encapsulates a stream of block epochs that are sent at
496
        // every new block height.
497
        blockEpochs *chainntnfs.BlockEpochEvent
498

499
        // prematureChannelUpdates is a map of ChannelUpdates we have received
500
        // that wasn't associated with any channel we know about.  We store
501
        // them temporarily, such that we can reprocess them when a
502
        // ChannelAnnouncement for the channel is received.
503
        prematureChannelUpdates *lru.Cache[uint64, *cachedNetworkMsg]
504

505
        // banman tracks our peer's ban status.
506
        banman *banman
507

508
        // networkMsgs is a channel that carries new network broadcasted
509
        // message from outside the gossiper service to be processed by the
510
        // networkHandler.
511
        networkMsgs chan *networkMsg
512

513
        // futureMsgs is a list of premature network messages that have a block
514
        // height specified in the future. We will save them and resend it to
515
        // the chan networkMsgs once the block height has reached. The cached
516
        // map format is,
517
        //   {msgID1: msg1, msgID2: msg2, ...}
518
        futureMsgs *futureMsgCache
519

520
        // chanPolicyUpdates is a channel that requests to update the
521
        // forwarding policy of a set of channels is sent over.
522
        chanPolicyUpdates chan *chanPolicyUpdateRequest
523

524
        // selfKey is the identity public key of the backing Lightning node.
525
        selfKey *btcec.PublicKey
526

527
        // selfKeyLoc is the locator for the identity public key of the backing
528
        // Lightning node.
529
        selfKeyLoc keychain.KeyLocator
530

531
        // channelMtx is used to restrict the database access to one
532
        // goroutine per channel ID. This is done to ensure that when
533
        // the gossiper is handling an announcement, the db state stays
534
        // consistent between when the DB is first read until it's written.
535
        channelMtx *multimutex.Mutex[uint64]
536

537
        recentRejects *lru.Cache[rejectCacheKey, *cachedReject]
538

539
        // syncMgr is a subsystem responsible for managing the gossip syncers
540
        // for peers currently connected. When a new peer is connected, the
541
        // manager will create its accompanying gossip syncer and determine
542
        // whether it should have an activeSync or passiveSync sync type based
543
        // on how many other gossip syncers are currently active. Any activeSync
544
        // gossip syncers are started in a round-robin manner to ensure we're
545
        // not syncing with multiple peers at the same time.
546
        syncMgr *SyncManager
547

548
        // reliableSender is a subsystem responsible for handling reliable
549
        // message send requests to peers. This should only be used for channels
550
        // that are unadvertised at the time of handling the message since if it
551
        // is advertised, then peers should be able to get the message from the
552
        // network.
553
        reliableSender *reliableSender
554

555
        // chanUpdateRateLimiter contains rate limiters for each direction of
556
        // a channel update we've processed. We'll use these to determine
557
        // whether we should accept a new update for a specific channel and
558
        // direction.
559
        //
560
        // NOTE: This map must be synchronized with the main
561
        // AuthenticatedGossiper lock.
562
        chanUpdateRateLimiter map[uint64][2]*rate.Limiter
563

564
        // vb is used to enforce job dependency ordering of gossip messages.
565
        vb *ValidationBarrier
566

567
        sync.Mutex
568

569
        cancel fn.Option[context.CancelFunc]
570
        quit   chan struct{}
571
        wg     sync.WaitGroup
572
}
573

574
// New creates a new AuthenticatedGossiper instance, initialized with the
575
// passed configuration parameters.
576
func New(cfg Config, selfKeyDesc *keychain.KeyDescriptor) *AuthenticatedGossiper {
30✔
577
        gossiper := &AuthenticatedGossiper{
30✔
578
                selfKey:           selfKeyDesc.PubKey,
30✔
579
                selfKeyLoc:        selfKeyDesc.KeyLocator,
30✔
580
                cfg:               &cfg,
30✔
581
                networkMsgs:       make(chan *networkMsg),
30✔
582
                futureMsgs:        newFutureMsgCache(maxFutureMessages),
30✔
583
                quit:              make(chan struct{}),
30✔
584
                chanPolicyUpdates: make(chan *chanPolicyUpdateRequest),
30✔
585
                prematureChannelUpdates: lru.NewCache[uint64, *cachedNetworkMsg]( //nolint: ll
30✔
586
                        maxPrematureUpdates,
30✔
587
                ),
30✔
588
                channelMtx: multimutex.NewMutex[uint64](),
30✔
589
                recentRejects: lru.NewCache[rejectCacheKey, *cachedReject](
30✔
590
                        maxRejectedUpdates,
30✔
591
                ),
30✔
592
                chanUpdateRateLimiter: make(map[uint64][2]*rate.Limiter),
30✔
593
                banman:                newBanman(cfg.BanThreshold),
30✔
594
        }
30✔
595

30✔
596
        gossiper.vb = NewValidationBarrier(1000, gossiper.quit)
30✔
597

30✔
598
        gossiper.syncMgr = newSyncManager(&SyncManagerCfg{
30✔
599
                ChainHash:                *cfg.ChainParams.GenesisHash,
30✔
600
                ChanSeries:               cfg.ChanSeries,
30✔
601
                RotateTicker:             cfg.RotateTicker,
30✔
602
                HistoricalSyncTicker:     cfg.HistoricalSyncTicker,
30✔
603
                NumActiveSyncers:         cfg.NumActiveSyncers,
30✔
604
                NoTimestampQueries:       cfg.NoTimestampQueries,
30✔
605
                IgnoreHistoricalFilters:  cfg.IgnoreHistoricalFilters,
30✔
606
                BestHeight:               gossiper.latestHeight,
30✔
607
                PinnedSyncers:            cfg.PinnedSyncers,
30✔
608
                IsStillZombieChannel:     cfg.IsStillZombieChannel,
30✔
609
                AllotedMsgBytesPerSecond: cfg.MsgRateBytes,
30✔
610
                AllotedMsgBytesBurst:     cfg.MsgBurstBytes,
30✔
611
                FilterConcurrency:        cfg.FilterConcurrency,
30✔
612
                PeerMsgBytesPerSecond:    cfg.PeerMsgRateBytes,
30✔
613
        })
30✔
614

30✔
615
        gossiper.reliableSender = newReliableSender(&reliableSenderCfg{
30✔
616
                NotifyWhenOnline:  cfg.NotifyWhenOnline,
30✔
617
                NotifyWhenOffline: cfg.NotifyWhenOffline,
30✔
618
                MessageStore:      cfg.MessageStore,
30✔
619
                IsMsgStale:        gossiper.isMsgStale,
30✔
620
        })
30✔
621

30✔
622
        return gossiper
30✔
623
}
30✔
624

625
// EdgeWithInfo contains the information that is required to update an edge.
626
type EdgeWithInfo struct {
627
        // Info describes the channel.
628
        Info *models.ChannelEdgeInfo
629

630
        // Edge describes the policy in one direction of the channel.
631
        Edge *models.ChannelEdgePolicy
632
}
633

634
// PropagateChanPolicyUpdate signals the AuthenticatedGossiper to perform the
635
// specified edge updates. Updates are done in two stages: first, the
636
// AuthenticatedGossiper ensures the update has been committed by dependent
637
// sub-systems, then it signs and broadcasts new updates to the network. A
638
// mapping between outpoints and updated channel policies is returned, which is
639
// used to update the forwarding policies of the underlying links.
640
func (d *AuthenticatedGossiper) PropagateChanPolicyUpdate(
641
        edgesToUpdate []EdgeWithInfo) error {
1✔
642

1✔
643
        errChan := make(chan error, 1)
1✔
644
        policyUpdate := &chanPolicyUpdateRequest{
1✔
645
                edgesToUpdate: edgesToUpdate,
1✔
646
                errChan:       errChan,
1✔
647
        }
1✔
648

1✔
649
        select {
1✔
650
        case d.chanPolicyUpdates <- policyUpdate:
1✔
651
                err := <-errChan
1✔
652
                return err
1✔
653
        case <-d.quit:
×
654
                return fmt.Errorf("AuthenticatedGossiper shutting down")
×
655
        }
656
}
657

658
// Start spawns network messages handler goroutine and registers on new block
659
// notifications in order to properly handle the premature announcements.
660
func (d *AuthenticatedGossiper) Start() error {
30✔
661
        var err error
30✔
662
        d.started.Do(func() {
60✔
663
                ctx, cancel := context.WithCancel(context.Background())
30✔
664
                d.cancel = fn.Some(cancel)
30✔
665

30✔
666
                log.Info("Authenticated Gossiper starting")
30✔
667
                err = d.start(ctx)
30✔
668
        })
30✔
669
        return err
30✔
670
}
671

672
func (d *AuthenticatedGossiper) start(ctx context.Context) error {
30✔
673
        // First we register for new notifications of newly discovered blocks.
30✔
674
        // We do this immediately so we'll later be able to consume any/all
30✔
675
        // blocks which were discovered.
30✔
676
        blockEpochs, err := d.cfg.Notifier.RegisterBlockEpochNtfn(nil)
30✔
677
        if err != nil {
30✔
678
                return err
×
679
        }
×
680
        d.blockEpochs = blockEpochs
30✔
681

30✔
682
        height, err := d.cfg.Graph.CurrentBlockHeight()
30✔
683
        if err != nil {
30✔
684
                return err
×
685
        }
×
686
        d.bestHeight = height
30✔
687

30✔
688
        // Start the reliable sender. In case we had any pending messages ready
30✔
689
        // to be sent when the gossiper was last shut down, we must continue on
30✔
690
        // our quest to deliver them to their respective peers.
30✔
691
        if err := d.reliableSender.Start(); err != nil {
30✔
692
                return err
×
693
        }
×
694

695
        d.syncMgr.Start()
30✔
696

30✔
697
        d.banman.start()
30✔
698

30✔
699
        // Start receiving blocks in its dedicated goroutine.
30✔
700
        d.wg.Add(2)
30✔
701
        go d.syncBlockHeight()
30✔
702
        go d.networkHandler(ctx)
30✔
703

30✔
704
        return nil
30✔
705
}
706

707
// syncBlockHeight syncs the best block height for the gossiper by reading
708
// blockEpochs.
709
//
710
// NOTE: must be run as a goroutine.
711
func (d *AuthenticatedGossiper) syncBlockHeight() {
30✔
712
        defer d.wg.Done()
30✔
713

30✔
714
        for {
60✔
715
                select {
30✔
716
                // A new block has arrived, so we can re-process the previously
717
                // premature announcements.
UNCOV
718
                case newBlock, ok := <-d.blockEpochs.Epochs:
×
UNCOV
719
                        // If the channel has been closed, then this indicates
×
UNCOV
720
                        // the daemon is shutting down, so we exit ourselves.
×
UNCOV
721
                        if !ok {
×
UNCOV
722
                                return
×
UNCOV
723
                        }
×
724

725
                        // Once a new block arrives, we update our running
726
                        // track of the height of the chain tip.
UNCOV
727
                        d.Lock()
×
UNCOV
728
                        blockHeight := uint32(newBlock.Height)
×
UNCOV
729
                        d.bestHeight = blockHeight
×
UNCOV
730
                        d.Unlock()
×
UNCOV
731

×
UNCOV
732
                        log.Debugf("New block: height=%d, hash=%s", blockHeight,
×
UNCOV
733
                                newBlock.Hash)
×
UNCOV
734

×
UNCOV
735
                        // Resend future messages, if any.
×
UNCOV
736
                        d.resendFutureMessages(blockHeight)
×
737

738
                case <-d.quit:
30✔
739
                        return
30✔
740
                }
741
        }
742
}
743

744
// futureMsgCache embeds a `lru.Cache` with a message counter that's served as
745
// the unique ID when saving the message.
746
type futureMsgCache struct {
747
        *lru.Cache[uint64, *cachedFutureMsg]
748

749
        // msgID is a monotonically increased integer.
750
        msgID atomic.Uint64
751
}
752

753
// nextMsgID returns a unique message ID.
754
func (f *futureMsgCache) nextMsgID() uint64 {
3✔
755
        return f.msgID.Add(1)
3✔
756
}
3✔
757

758
// newFutureMsgCache creates a new future message cache with the underlying lru
759
// cache being initialized with the specified capacity.
760
func newFutureMsgCache(capacity uint64) *futureMsgCache {
31✔
761
        // Create a new cache.
31✔
762
        cache := lru.NewCache[uint64, *cachedFutureMsg](capacity)
31✔
763

31✔
764
        return &futureMsgCache{
31✔
765
                Cache: cache,
31✔
766
        }
31✔
767
}
31✔
768

769
// cachedFutureMsg is a future message that's saved to the `futureMsgCache`.
770
type cachedFutureMsg struct {
771
        // msg is the network message.
772
        msg *networkMsg
773

774
        // height is the block height.
775
        height uint32
776
}
777

778
// Size returns the size of the message.
779
func (c *cachedFutureMsg) Size() (uint64, error) {
4✔
780
        // Return a constant 1.
4✔
781
        return 1, nil
4✔
782
}
4✔
783

784
// resendFutureMessages takes a block height, resends all the future messages
785
// found below and equal to that height and deletes those messages found in the
786
// gossiper's futureMsgs.
UNCOV
787
func (d *AuthenticatedGossiper) resendFutureMessages(height uint32) {
×
UNCOV
788
        var (
×
UNCOV
789
                // msgs are the target messages.
×
UNCOV
790
                msgs []*networkMsg
×
UNCOV
791

×
UNCOV
792
                // keys are the target messages' caching keys.
×
UNCOV
793
                keys []uint64
×
UNCOV
794
        )
×
UNCOV
795

×
UNCOV
796
        // filterMsgs is the visitor used when iterating the future cache.
×
UNCOV
797
        filterMsgs := func(k uint64, cmsg *cachedFutureMsg) bool {
×
UNCOV
798
                if cmsg.height <= height {
×
UNCOV
799
                        msgs = append(msgs, cmsg.msg)
×
UNCOV
800
                        keys = append(keys, k)
×
UNCOV
801
                }
×
802

UNCOV
803
                return true
×
804
        }
805

806
        // Filter out the target messages.
UNCOV
807
        d.futureMsgs.Range(filterMsgs)
×
UNCOV
808

×
UNCOV
809
        // Return early if no messages found.
×
UNCOV
810
        if len(msgs) == 0 {
×
UNCOV
811
                return
×
UNCOV
812
        }
×
813

814
        // Remove the filtered messages.
UNCOV
815
        for _, key := range keys {
×
UNCOV
816
                d.futureMsgs.Delete(key)
×
UNCOV
817
        }
×
818

UNCOV
819
        log.Debugf("Resending %d network messages at height %d",
×
UNCOV
820
                len(msgs), height)
×
UNCOV
821

×
UNCOV
822
        for _, msg := range msgs {
×
UNCOV
823
                select {
×
UNCOV
824
                case d.networkMsgs <- msg:
×
825
                case <-d.quit:
×
826
                        msg.err <- ErrGossiperShuttingDown
×
827
                }
828
        }
829
}
830

831
// Stop signals any active goroutines for a graceful closure.
832
func (d *AuthenticatedGossiper) Stop() error {
31✔
833
        d.stopped.Do(func() {
61✔
834
                log.Info("Authenticated gossiper shutting down...")
30✔
835
                defer log.Debug("Authenticated gossiper shutdown complete")
30✔
836

30✔
837
                d.stop()
30✔
838
        })
30✔
839
        return nil
31✔
840
}
841

842
func (d *AuthenticatedGossiper) stop() {
30✔
843
        log.Debug("Authenticated Gossiper is stopping")
30✔
844
        defer log.Debug("Authenticated Gossiper stopped")
30✔
845

30✔
846
        // `blockEpochs` is only initialized in the start routine so we make
30✔
847
        // sure we don't panic here in the case where the `Stop` method is
30✔
848
        // called when the `Start` method does not complete.
30✔
849
        if d.blockEpochs != nil {
60✔
850
                d.blockEpochs.Cancel()
30✔
851
        }
30✔
852

853
        d.syncMgr.Stop()
30✔
854

30✔
855
        d.banman.stop()
30✔
856

30✔
857
        d.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
60✔
858
        close(d.quit)
30✔
859
        d.wg.Wait()
30✔
860

30✔
861
        // We'll stop our reliable sender after all of the gossiper's goroutines
30✔
862
        // have exited to ensure nothing can cause it to continue executing.
30✔
863
        d.reliableSender.Stop()
30✔
864
}
865

866
// TODO(roasbeef): need method to get current gossip timestamp?
867
//  * using mtx, check time rotate forward is needed?
868

869
// ProcessRemoteAnnouncement sends a new remote announcement message along with
870
// the peer that sent the routing message. The announcement will be processed
871
// then added to a queue for batched trickled announcement to all connected
872
// peers.  Remote channel announcements should contain the announcement proof
873
// and be fully validated.
874
func (d *AuthenticatedGossiper) ProcessRemoteAnnouncement(ctx context.Context,
875
        msg lnwire.Message, peer lnpeer.Peer) chan error {
291✔
876

291✔
877
        errChan := make(chan error, 1)
291✔
878

291✔
879
        // For messages in the known set of channel series queries, we'll
291✔
880
        // dispatch the message directly to the GossipSyncer, and skip the main
291✔
881
        // processing loop.
291✔
882
        switch m := msg.(type) {
291✔
883
        case *lnwire.QueryShortChanIDs,
884
                *lnwire.QueryChannelRange,
885
                *lnwire.ReplyChannelRange,
UNCOV
886
                *lnwire.ReplyShortChanIDsEnd:
×
UNCOV
887

×
UNCOV
888
                syncer, ok := d.syncMgr.GossipSyncer(peer.PubKey())
×
UNCOV
889
                if !ok {
×
890
                        log.Warnf("Gossip syncer for peer=%x not found",
×
891
                                peer.PubKey())
×
892

×
893
                        errChan <- ErrGossipSyncerNotFound
×
894
                        return errChan
×
895
                }
×
896

897
                // If we've found the message target, then we'll dispatch the
898
                // message directly to it.
UNCOV
899
                err := syncer.ProcessQueryMsg(m, peer.QuitSignal())
×
UNCOV
900
                if err != nil {
×
901
                        log.Errorf("Process query msg from peer %x got %v",
×
902
                                peer.PubKey(), err)
×
903
                }
×
904

UNCOV
905
                errChan <- err
×
UNCOV
906
                return errChan
×
907

908
        // If a peer is updating its current update horizon, then we'll dispatch
909
        // that directly to the proper GossipSyncer.
UNCOV
910
        case *lnwire.GossipTimestampRange:
×
UNCOV
911
                syncer, ok := d.syncMgr.GossipSyncer(peer.PubKey())
×
UNCOV
912
                if !ok {
×
913
                        log.Warnf("Gossip syncer for peer=%x not found",
×
914
                                peer.PubKey())
×
915

×
916
                        errChan <- ErrGossipSyncerNotFound
×
917
                        return errChan
×
918
                }
×
919

920
                // Queue the message for asynchronous processing to prevent
921
                // blocking the gossiper when rate limiting is active.
UNCOV
922
                if !syncer.QueueTimestampRange(m) {
×
923
                        log.Warnf("Unable to queue gossip filter for peer=%x: "+
×
924
                                "queue full", peer.PubKey())
×
925

×
926
                        // Return nil to indicate we've handled the message,
×
927
                        // even though it was dropped. This prevents the peer
×
928
                        // from being disconnected.
×
929
                        errChan <- nil
×
930
                        return errChan
×
931
                }
×
932

UNCOV
933
                errChan <- nil
×
UNCOV
934
                return errChan
×
935

936
        // To avoid inserting edges in the graph for our own channels that we
937
        // have already closed, we ignore such channel announcements coming
938
        // from the remote.
939
        case *lnwire.ChannelAnnouncement1:
220✔
940
                ownKey := d.selfKey.SerializeCompressed()
220✔
941
                ownErr := fmt.Errorf("ignoring remote ChannelAnnouncement1 " +
220✔
942
                        "for own channel")
220✔
943

220✔
944
                if bytes.Equal(m.NodeID1[:], ownKey) ||
220✔
945
                        bytes.Equal(m.NodeID2[:], ownKey) {
222✔
946

2✔
947
                        log.Warn(ownErr)
2✔
948
                        errChan <- ownErr
2✔
949
                        return errChan
2✔
950
                }
2✔
951
        }
952

953
        nMsg := &networkMsg{
289✔
954
                msg:      msg,
289✔
955
                isRemote: true,
289✔
956
                peer:     peer,
289✔
957
                source:   peer.IdentityKey(),
289✔
958
                err:      errChan,
289✔
959
        }
289✔
960

289✔
961
        select {
289✔
962
        case d.networkMsgs <- nMsg:
289✔
963

964
        // If the peer that sent us this error is quitting, then we don't need
965
        // to send back an error and can return immediately.
966
        // TODO(elle): the peer should now just rely on canceling the passed
967
        //  context.
968
        case <-peer.QuitSignal():
×
969
                return nil
×
970
        case <-ctx.Done():
×
971
                return nil
×
972
        case <-d.quit:
×
973
                nMsg.err <- ErrGossiperShuttingDown
×
974
        }
975

976
        return nMsg.err
289✔
977
}
978

979
// ProcessLocalAnnouncement sends a new remote announcement message along with
980
// the peer that sent the routing message. The announcement will be processed
981
// then added to a queue for batched trickled announcement to all connected
982
// peers.  Local channel announcements don't contain the announcement proof and
983
// will not be fully validated. Once the channel proofs are received, the
984
// entire channel announcement and update messages will be re-constructed and
985
// broadcast to the rest of the network.
986
func (d *AuthenticatedGossiper) ProcessLocalAnnouncement(msg lnwire.Message,
987
        optionalFields ...OptionalMsgField) chan error {
47✔
988

47✔
989
        optionalMsgFields := &optionalMsgFields{}
47✔
990
        optionalMsgFields.apply(optionalFields...)
47✔
991

47✔
992
        nMsg := &networkMsg{
47✔
993
                msg:               msg,
47✔
994
                optionalMsgFields: optionalMsgFields,
47✔
995
                isRemote:          false,
47✔
996
                source:            d.selfKey,
47✔
997
                err:               make(chan error, 1),
47✔
998
        }
47✔
999

47✔
1000
        select {
47✔
1001
        case d.networkMsgs <- nMsg:
47✔
1002
        case <-d.quit:
×
1003
                nMsg.err <- ErrGossiperShuttingDown
×
1004
        }
1005

1006
        return nMsg.err
47✔
1007
}
1008

1009
// channelUpdateID is a unique identifier for ChannelUpdate messages, as
1010
// channel updates can be identified by the (ShortChannelID, ChannelFlags)
1011
// tuple.
1012
type channelUpdateID struct {
1013
        // channelID represents the set of data which is needed to
1014
        // retrieve all necessary data to validate the channel existence.
1015
        channelID lnwire.ShortChannelID
1016

1017
        // Flags least-significant bit must be set to 0 if the creating node
1018
        // corresponds to the first node in the previously sent channel
1019
        // announcement and 1 otherwise.
1020
        flags lnwire.ChanUpdateChanFlags
1021
}
1022

1023
// msgWithSenders is a wrapper struct around a message, and the set of peers
1024
// that originally sent us this message. Using this struct, we can ensure that
1025
// we don't re-send a message to the peer that sent it to us in the first
1026
// place.
1027
type msgWithSenders struct {
1028
        // msg is the wire message itself.
1029
        msg lnwire.Message
1030

1031
        // isLocal is true if this was a message that originated locally. We'll
1032
        // use this to bypass our normal checks to ensure we prioritize sending
1033
        // out our own updates.
1034
        isLocal bool
1035

1036
        // sender is the set of peers that sent us this message.
1037
        senders map[route.Vertex]struct{}
1038
}
1039

1040
// mergeSyncerMap is used to merge the set of senders of a particular message
1041
// with peers that we have an active GossipSyncer with. We do this to ensure
1042
// that we don't broadcast messages to any peers that we have active gossip
1043
// syncers for.
1044
func (m *msgWithSenders) mergeSyncerMap(syncers map[route.Vertex]*GossipSyncer) {
29✔
1045
        for peerPub := range syncers {
29✔
UNCOV
1046
                m.senders[peerPub] = struct{}{}
×
UNCOV
1047
        }
×
1048
}
1049

1050
// deDupedAnnouncements de-duplicates announcements that have been added to the
1051
// batch. Internally, announcements are stored in three maps
1052
// (one each for channel announcements, channel updates, and node
1053
// announcements). These maps keep track of unique announcements and ensure no
1054
// announcements are duplicated. We keep the three message types separate, such
1055
// that we can send channel announcements first, then channel updates, and
1056
// finally node announcements when it's time to broadcast them.
1057
type deDupedAnnouncements struct {
1058
        // channelAnnouncements are identified by the short channel id field.
1059
        channelAnnouncements map[lnwire.ShortChannelID]msgWithSenders
1060

1061
        // channelUpdates are identified by the channel update id field.
1062
        channelUpdates map[channelUpdateID]msgWithSenders
1063

1064
        // nodeAnnouncements are identified by the Vertex field.
1065
        nodeAnnouncements map[route.Vertex]msgWithSenders
1066

1067
        sync.Mutex
1068
}
1069

1070
// Reset operates on deDupedAnnouncements to reset the storage of
1071
// announcements.
1072
func (d *deDupedAnnouncements) Reset() {
32✔
1073
        d.Lock()
32✔
1074
        defer d.Unlock()
32✔
1075

32✔
1076
        d.reset()
32✔
1077
}
32✔
1078

1079
// reset is the private version of the Reset method. We have this so we can
1080
// call this method within method that are already holding the lock.
1081
func (d *deDupedAnnouncements) reset() {
327✔
1082
        // Storage of each type of announcement (channel announcements, channel
327✔
1083
        // updates, node announcements) is set to an empty map where the
327✔
1084
        // appropriate key points to the corresponding lnwire.Message.
327✔
1085
        d.channelAnnouncements = make(map[lnwire.ShortChannelID]msgWithSenders)
327✔
1086
        d.channelUpdates = make(map[channelUpdateID]msgWithSenders)
327✔
1087
        d.nodeAnnouncements = make(map[route.Vertex]msgWithSenders)
327✔
1088
}
327✔
1089

1090
// addMsg adds a new message to the current batch. If the message is already
1091
// present in the current batch, then this new instance replaces the latter,
1092
// and the set of senders is updated to reflect which node sent us this
1093
// message.
1094
func (d *deDupedAnnouncements) addMsg(message networkMsg) {
91✔
1095
        log.Tracef("Adding network message: %v to batch", message.msg.MsgType())
91✔
1096

91✔
1097
        // Depending on the message type (channel announcement, channel update,
91✔
1098
        // or node announcement), the message is added to the corresponding map
91✔
1099
        // in deDupedAnnouncements. Because each identifying key can have at
91✔
1100
        // most one value, the announcements are de-duplicated, with newer ones
91✔
1101
        // replacing older ones.
91✔
1102
        switch msg := message.msg.(type) {
91✔
1103

1104
        // Channel announcements are identified by the short channel id field.
1105
        case *lnwire.ChannelAnnouncement1:
23✔
1106
                deDupKey := msg.ShortChannelID
23✔
1107
                sender := route.NewVertex(message.source)
23✔
1108

23✔
1109
                mws, ok := d.channelAnnouncements[deDupKey]
23✔
1110
                if !ok {
45✔
1111
                        mws = msgWithSenders{
22✔
1112
                                msg:     msg,
22✔
1113
                                isLocal: !message.isRemote,
22✔
1114
                                senders: make(map[route.Vertex]struct{}),
22✔
1115
                        }
22✔
1116
                        mws.senders[sender] = struct{}{}
22✔
1117

22✔
1118
                        d.channelAnnouncements[deDupKey] = mws
22✔
1119

22✔
1120
                        return
22✔
1121
                }
22✔
1122

1123
                mws.msg = msg
1✔
1124
                mws.senders[sender] = struct{}{}
1✔
1125
                d.channelAnnouncements[deDupKey] = mws
1✔
1126

1127
        // Channel updates are identified by the (short channel id,
1128
        // channelflags) tuple.
1129
        case *lnwire.ChannelUpdate1:
46✔
1130
                sender := route.NewVertex(message.source)
46✔
1131
                deDupKey := channelUpdateID{
46✔
1132
                        msg.ShortChannelID,
46✔
1133
                        msg.ChannelFlags,
46✔
1134
                }
46✔
1135

46✔
1136
                oldTimestamp := uint32(0)
46✔
1137
                mws, ok := d.channelUpdates[deDupKey]
46✔
1138
                if ok {
49✔
1139
                        // If we already have seen this message, record its
3✔
1140
                        // timestamp.
3✔
1141
                        update, ok := mws.msg.(*lnwire.ChannelUpdate1)
3✔
1142
                        if !ok {
3✔
1143
                                log.Errorf("Expected *lnwire.ChannelUpdate1, "+
×
1144
                                        "got: %T", mws.msg)
×
1145

×
1146
                                return
×
1147
                        }
×
1148

1149
                        oldTimestamp = update.Timestamp
3✔
1150
                }
1151

1152
                // If we already had this message with a strictly newer
1153
                // timestamp, then we'll just discard the message we got.
1154
                if oldTimestamp > msg.Timestamp {
47✔
1155
                        log.Debugf("Ignored outdated network message: "+
1✔
1156
                                "peer=%v, msg=%s", message.peer, msg.MsgType())
1✔
1157
                        return
1✔
1158
                }
1✔
1159

1160
                // If the message we just got is newer than what we previously
1161
                // have seen, or this is the first time we see it, then we'll
1162
                // add it to our map of announcements.
1163
                if oldTimestamp < msg.Timestamp {
89✔
1164
                        mws = msgWithSenders{
44✔
1165
                                msg:     msg,
44✔
1166
                                isLocal: !message.isRemote,
44✔
1167
                                senders: make(map[route.Vertex]struct{}),
44✔
1168
                        }
44✔
1169

44✔
1170
                        // We'll mark the sender of the message in the
44✔
1171
                        // senders map.
44✔
1172
                        mws.senders[sender] = struct{}{}
44✔
1173

44✔
1174
                        d.channelUpdates[deDupKey] = mws
44✔
1175

44✔
1176
                        return
44✔
1177
                }
44✔
1178

1179
                // Lastly, if we had seen this exact message from before, with
1180
                // the same timestamp, we'll add the sender to the map of
1181
                // senders, such that we can skip sending this message back in
1182
                // the next batch.
1183
                mws.msg = msg
1✔
1184
                mws.senders[sender] = struct{}{}
1✔
1185
                d.channelUpdates[deDupKey] = mws
1✔
1186

1187
        // Node announcements are identified by the Vertex field.  Use the
1188
        // NodeID to create the corresponding Vertex.
1189
        case *lnwire.NodeAnnouncement:
22✔
1190
                sender := route.NewVertex(message.source)
22✔
1191
                deDupKey := route.Vertex(msg.NodeID)
22✔
1192

22✔
1193
                // We do the same for node announcements as we did for channel
22✔
1194
                // updates, as they also carry a timestamp.
22✔
1195
                oldTimestamp := uint32(0)
22✔
1196
                mws, ok := d.nodeAnnouncements[deDupKey]
22✔
1197
                if ok {
27✔
1198
                        oldTimestamp = mws.msg.(*lnwire.NodeAnnouncement).Timestamp
5✔
1199
                }
5✔
1200

1201
                // Discard the message if it's old.
1202
                if oldTimestamp > msg.Timestamp {
22✔
UNCOV
1203
                        return
×
UNCOV
1204
                }
×
1205

1206
                // Replace if it's newer.
1207
                if oldTimestamp < msg.Timestamp {
40✔
1208
                        mws = msgWithSenders{
18✔
1209
                                msg:     msg,
18✔
1210
                                isLocal: !message.isRemote,
18✔
1211
                                senders: make(map[route.Vertex]struct{}),
18✔
1212
                        }
18✔
1213

18✔
1214
                        mws.senders[sender] = struct{}{}
18✔
1215

18✔
1216
                        d.nodeAnnouncements[deDupKey] = mws
18✔
1217

18✔
1218
                        return
18✔
1219
                }
18✔
1220

1221
                // Add to senders map if it's the same as we had.
1222
                mws.msg = msg
4✔
1223
                mws.senders[sender] = struct{}{}
4✔
1224
                d.nodeAnnouncements[deDupKey] = mws
4✔
1225
        }
1226
}
1227

1228
// AddMsgs is a helper method to add multiple messages to the announcement
1229
// batch.
1230
func (d *deDupedAnnouncements) AddMsgs(msgs ...networkMsg) {
59✔
1231
        d.Lock()
59✔
1232
        defer d.Unlock()
59✔
1233

59✔
1234
        for _, msg := range msgs {
150✔
1235
                d.addMsg(msg)
91✔
1236
        }
91✔
1237
}
1238

1239
// msgsToBroadcast is returned by Emit() and partitions the messages we'd like
1240
// to broadcast next into messages that are locally sourced and those that are
1241
// sourced remotely.
1242
type msgsToBroadcast struct {
1243
        // localMsgs is the set of messages we created locally.
1244
        localMsgs []msgWithSenders
1245

1246
        // remoteMsgs is the set of messages that we received from a remote
1247
        // party.
1248
        remoteMsgs []msgWithSenders
1249
}
1250

1251
// addMsg adds a new message to the appropriate sub-slice.
1252
func (m *msgsToBroadcast) addMsg(msg msgWithSenders) {
76✔
1253
        if msg.isLocal {
123✔
1254
                m.localMsgs = append(m.localMsgs, msg)
47✔
1255
        } else {
76✔
1256
                m.remoteMsgs = append(m.remoteMsgs, msg)
29✔
1257
        }
29✔
1258
}
1259

1260
// isEmpty returns true if the batch is empty.
1261
func (m *msgsToBroadcast) isEmpty() bool {
294✔
1262
        return len(m.localMsgs) == 0 && len(m.remoteMsgs) == 0
294✔
1263
}
294✔
1264

1265
// length returns the length of the combined message set.
1266
func (m *msgsToBroadcast) length() int {
1✔
1267
        return len(m.localMsgs) + len(m.remoteMsgs)
1✔
1268
}
1✔
1269

1270
// Emit returns the set of de-duplicated announcements to be sent out during
1271
// the next announcement epoch, in the order of channel announcements, channel
1272
// updates, and node announcements. Each message emitted, contains the set of
1273
// peers that sent us the message. This way, we can ensure that we don't waste
1274
// bandwidth by re-sending a message to the peer that sent it to us in the
1275
// first place. Additionally, the set of stored messages are reset.
1276
func (d *deDupedAnnouncements) Emit() msgsToBroadcast {
295✔
1277
        d.Lock()
295✔
1278
        defer d.Unlock()
295✔
1279

295✔
1280
        // Get the total number of announcements.
295✔
1281
        numAnnouncements := len(d.channelAnnouncements) + len(d.channelUpdates) +
295✔
1282
                len(d.nodeAnnouncements)
295✔
1283

295✔
1284
        // Create an empty array of lnwire.Messages with a length equal to
295✔
1285
        // the total number of announcements.
295✔
1286
        msgs := msgsToBroadcast{
295✔
1287
                localMsgs:  make([]msgWithSenders, 0, numAnnouncements),
295✔
1288
                remoteMsgs: make([]msgWithSenders, 0, numAnnouncements),
295✔
1289
        }
295✔
1290

295✔
1291
        // Add the channel announcements to the array first.
295✔
1292
        for _, message := range d.channelAnnouncements {
314✔
1293
                msgs.addMsg(message)
19✔
1294
        }
19✔
1295

1296
        // Then add the channel updates.
1297
        for _, message := range d.channelUpdates {
335✔
1298
                msgs.addMsg(message)
40✔
1299
        }
40✔
1300

1301
        // Finally add the node announcements.
1302
        for _, message := range d.nodeAnnouncements {
312✔
1303
                msgs.addMsg(message)
17✔
1304
        }
17✔
1305

1306
        d.reset()
295✔
1307

295✔
1308
        // Return the array of lnwire.messages.
295✔
1309
        return msgs
295✔
1310
}
1311

1312
// calculateSubBatchSize is a helper function that calculates the size to break
1313
// down the batchSize into.
1314
func calculateSubBatchSize(totalDelay, subBatchDelay time.Duration,
1315
        minimumBatchSize, batchSize int) int {
13✔
1316
        if subBatchDelay > totalDelay {
15✔
1317
                return batchSize
2✔
1318
        }
2✔
1319

1320
        subBatchSize := (batchSize*int(subBatchDelay) +
11✔
1321
                int(totalDelay) - 1) / int(totalDelay)
11✔
1322

11✔
1323
        if subBatchSize < minimumBatchSize {
12✔
1324
                return minimumBatchSize
1✔
1325
        }
1✔
1326

1327
        return subBatchSize
10✔
1328
}
1329

1330
// batchSizeCalculator maps to the function `calculateSubBatchSize`. We create
1331
// this variable so the function can be mocked in our test.
1332
var batchSizeCalculator = calculateSubBatchSize
1333

1334
// splitAnnouncementBatches takes an exiting list of announcements and
1335
// decomposes it into sub batches controlled by the `subBatchSize`.
1336
func (d *AuthenticatedGossiper) splitAnnouncementBatches(
1337
        announcementBatch []msgWithSenders) [][]msgWithSenders {
75✔
1338

75✔
1339
        subBatchSize := batchSizeCalculator(
75✔
1340
                d.cfg.TrickleDelay, d.cfg.SubBatchDelay,
75✔
1341
                d.cfg.MinimumBatchSize, len(announcementBatch),
75✔
1342
        )
75✔
1343

75✔
1344
        var splitAnnouncementBatch [][]msgWithSenders
75✔
1345

75✔
1346
        for subBatchSize < len(announcementBatch) {
196✔
1347
                // For slicing with minimal allocation
121✔
1348
                // https://github.com/golang/go/wiki/SliceTricks
121✔
1349
                announcementBatch, splitAnnouncementBatch =
121✔
1350
                        announcementBatch[subBatchSize:],
121✔
1351
                        append(splitAnnouncementBatch,
121✔
1352
                                announcementBatch[0:subBatchSize:subBatchSize])
121✔
1353
        }
121✔
1354
        splitAnnouncementBatch = append(
75✔
1355
                splitAnnouncementBatch, announcementBatch,
75✔
1356
        )
75✔
1357

75✔
1358
        return splitAnnouncementBatch
75✔
1359
}
1360

1361
// splitAndSendAnnBatch takes a batch of messages, computes the proper batch
1362
// split size, and then sends out all items to the set of target peers. Locally
1363
// generated announcements are always sent before remotely generated
1364
// announcements.
1365
func (d *AuthenticatedGossiper) splitAndSendAnnBatch(ctx context.Context,
1366
        annBatch msgsToBroadcast) {
34✔
1367

34✔
1368
        // delayNextBatch is a helper closure that blocks for `SubBatchDelay`
34✔
1369
        // duration to delay the sending of next announcement batch.
34✔
1370
        delayNextBatch := func() {
102✔
1371
                select {
68✔
1372
                case <-time.After(d.cfg.SubBatchDelay):
51✔
1373
                case <-d.quit:
17✔
1374
                        return
17✔
1375
                }
1376
        }
1377

1378
        // Fetch the local and remote announcements.
1379
        localBatches := d.splitAnnouncementBatches(annBatch.localMsgs)
34✔
1380
        remoteBatches := d.splitAnnouncementBatches(annBatch.remoteMsgs)
34✔
1381

34✔
1382
        d.wg.Add(1)
34✔
1383
        go func() {
68✔
1384
                defer d.wg.Done()
34✔
1385

34✔
1386
                log.Debugf("Broadcasting %v new local announcements in %d "+
34✔
1387
                        "sub batches", len(annBatch.localMsgs),
34✔
1388
                        len(localBatches))
34✔
1389

34✔
1390
                // Send out the local announcements first.
34✔
1391
                for _, annBatch := range localBatches {
68✔
1392
                        d.sendLocalBatch(annBatch)
34✔
1393
                        delayNextBatch()
34✔
1394
                }
34✔
1395

1396
                log.Debugf("Broadcasting %v new remote announcements in %d "+
34✔
1397
                        "sub batches", len(annBatch.remoteMsgs),
34✔
1398
                        len(remoteBatches))
34✔
1399

34✔
1400
                // Now send the remote announcements.
34✔
1401
                for _, annBatch := range remoteBatches {
68✔
1402
                        d.sendRemoteBatch(ctx, annBatch)
34✔
1403
                        delayNextBatch()
34✔
1404
                }
34✔
1405
        }()
1406
}
1407

1408
// sendLocalBatch broadcasts a list of locally generated announcements to our
1409
// peers. For local announcements, we skip the filter and dedup logic and just
1410
// send the announcements out to all our coonnected peers.
1411
func (d *AuthenticatedGossiper) sendLocalBatch(annBatch []msgWithSenders) {
34✔
1412
        msgsToSend := lnutils.Map(
34✔
1413
                annBatch, func(m msgWithSenders) lnwire.Message {
77✔
1414
                        return m.msg
43✔
1415
                },
43✔
1416
        )
1417

1418
        err := d.cfg.Broadcast(nil, msgsToSend...)
34✔
1419
        if err != nil {
34✔
1420
                log.Errorf("Unable to send local batch announcements: %v", err)
×
1421
        }
×
1422
}
1423

1424
// sendRemoteBatch broadcasts a list of remotely generated announcements to our
1425
// peers.
1426
func (d *AuthenticatedGossiper) sendRemoteBatch(ctx context.Context,
1427
        annBatch []msgWithSenders) {
34✔
1428

34✔
1429
        syncerPeers := d.syncMgr.GossipSyncers()
34✔
1430

34✔
1431
        // We'll first attempt to filter out this new message for all peers
34✔
1432
        // that have active gossip syncers active.
34✔
1433
        for pub, syncer := range syncerPeers {
34✔
UNCOV
1434
                log.Tracef("Sending messages batch to GossipSyncer(%s)", pub)
×
UNCOV
1435
                syncer.FilterGossipMsgs(ctx, annBatch...)
×
UNCOV
1436
        }
×
1437

1438
        for _, msgChunk := range annBatch {
63✔
1439
                msgChunk := msgChunk
29✔
1440

29✔
1441
                // With the syncers taken care of, we'll merge the sender map
29✔
1442
                // with the set of syncers, so we don't send out duplicate
29✔
1443
                // messages.
29✔
1444
                msgChunk.mergeSyncerMap(syncerPeers)
29✔
1445

29✔
1446
                err := d.cfg.Broadcast(msgChunk.senders, msgChunk.msg)
29✔
1447
                if err != nil {
29✔
1448
                        log.Errorf("Unable to send batch "+
×
1449
                                "announcements: %v", err)
×
1450
                        continue
×
1451
                }
1452
        }
1453
}
1454

1455
// networkHandler is the primary goroutine that drives this service. The roles
1456
// of this goroutine includes answering queries related to the state of the
1457
// network, syncing up newly connected peers, and also periodically
1458
// broadcasting our latest topology state to all connected peers.
1459
//
1460
// NOTE: This MUST be run as a goroutine.
1461
func (d *AuthenticatedGossiper) networkHandler(ctx context.Context) {
30✔
1462
        defer d.wg.Done()
30✔
1463

30✔
1464
        // Initialize empty deDupedAnnouncements to store announcement batch.
30✔
1465
        announcements := deDupedAnnouncements{}
30✔
1466
        announcements.Reset()
30✔
1467

30✔
1468
        d.cfg.RetransmitTicker.Resume()
30✔
1469
        defer d.cfg.RetransmitTicker.Stop()
30✔
1470

30✔
1471
        trickleTimer := time.NewTicker(d.cfg.TrickleDelay)
30✔
1472
        defer trickleTimer.Stop()
30✔
1473

30✔
1474
        // To start, we'll first check to see if there are any stale channel or
30✔
1475
        // node announcements that we need to re-transmit.
30✔
1476
        if err := d.retransmitStaleAnns(ctx, time.Now()); err != nil {
30✔
1477
                log.Errorf("Unable to rebroadcast stale announcements: %v", err)
×
1478
        }
×
1479

1480
        for {
694✔
1481
                select {
664✔
1482
                // A new policy update has arrived. We'll commit it to the
1483
                // sub-systems below us, then craft, sign, and broadcast a new
1484
                // ChannelUpdate for the set of affected clients.
1485
                case policyUpdate := <-d.chanPolicyUpdates:
1✔
1486
                        log.Tracef("Received channel %d policy update requests",
1✔
1487
                                len(policyUpdate.edgesToUpdate))
1✔
1488

1✔
1489
                        // First, we'll now create new fully signed updates for
1✔
1490
                        // the affected channels and also update the underlying
1✔
1491
                        // graph with the new state.
1✔
1492
                        newChanUpdates, err := d.processChanPolicyUpdate(
1✔
1493
                                ctx, policyUpdate.edgesToUpdate,
1✔
1494
                        )
1✔
1495
                        policyUpdate.errChan <- err
1✔
1496
                        if err != nil {
1✔
1497
                                log.Errorf("Unable to craft policy updates: %v",
×
1498
                                        err)
×
1499
                                continue
×
1500
                        }
1501

1502
                        // Finally, with the updates committed, we'll now add
1503
                        // them to the announcement batch to be flushed at the
1504
                        // start of the next epoch.
1505
                        announcements.AddMsgs(newChanUpdates...)
1✔
1506

1507
                case announcement := <-d.networkMsgs:
338✔
1508
                        log.Tracef("Received network message: "+
338✔
1509
                                "peer=%v, msg=%s, is_remote=%v",
338✔
1510
                                announcement.peer, announcement.msg.MsgType(),
338✔
1511
                                announcement.isRemote)
338✔
1512

338✔
1513
                        switch announcement.msg.(type) {
338✔
1514
                        // Channel announcement signatures are amongst the only
1515
                        // messages that we'll process serially.
1516
                        case *lnwire.AnnounceSignatures1:
21✔
1517
                                emittedAnnouncements, _ := d.processNetworkAnnouncement(
21✔
1518
                                        ctx, announcement,
21✔
1519
                                )
21✔
1520
                                log.Debugf("Processed network message %s, "+
21✔
1521
                                        "returned len(announcements)=%v",
21✔
1522
                                        announcement.msg.MsgType(),
21✔
1523
                                        len(emittedAnnouncements))
21✔
1524

21✔
1525
                                if emittedAnnouncements != nil {
31✔
1526
                                        announcements.AddMsgs(
10✔
1527
                                                emittedAnnouncements...,
10✔
1528
                                        )
10✔
1529
                                }
10✔
1530
                                continue
21✔
1531
                        }
1532

1533
                        // If this message was recently rejected, then we won't
1534
                        // attempt to re-process it.
1535
                        if announcement.isRemote && d.isRecentlyRejectedMsg(
317✔
1536
                                announcement.msg,
317✔
1537
                                sourceToPub(announcement.source),
317✔
1538
                        ) {
318✔
1539

1✔
1540
                                announcement.err <- fmt.Errorf("recently " +
1✔
1541
                                        "rejected")
1✔
1542
                                continue
1✔
1543
                        }
1544

1545
                        // We'll set up any dependent, and wait until a free
1546
                        // slot for this job opens up, this allow us to not
1547
                        // have thousands of goroutines active.
1548
                        annJobID, err := d.vb.InitJobDependencies(
316✔
1549
                                announcement.msg,
316✔
1550
                        )
316✔
1551
                        if err != nil {
316✔
1552
                                announcement.err <- err
×
1553
                                continue
×
1554
                        }
1555

1556
                        d.wg.Add(1)
316✔
1557
                        go d.handleNetworkMessages(
316✔
1558
                                ctx, announcement, &announcements, annJobID,
316✔
1559
                        )
316✔
1560

1561
                // The trickle timer has ticked, which indicates we should
1562
                // flush to the network the pending batch of new announcements
1563
                // we've received since the last trickle tick.
1564
                case <-trickleTimer.C:
294✔
1565
                        // Emit the current batch of announcements from
294✔
1566
                        // deDupedAnnouncements.
294✔
1567
                        announcementBatch := announcements.Emit()
294✔
1568

294✔
1569
                        // If the current announcements batch is nil, then we
294✔
1570
                        // have no further work here.
294✔
1571
                        if announcementBatch.isEmpty() {
554✔
1572
                                continue
260✔
1573
                        }
1574

1575
                        // At this point, we have the set of local and remote
1576
                        // announcements we want to send out. We'll do the
1577
                        // batching as normal for both, but for local
1578
                        // announcements, we'll blast them out w/o regard for
1579
                        // our peer's policies so we ensure they propagate
1580
                        // properly.
1581
                        d.splitAndSendAnnBatch(ctx, announcementBatch)
34✔
1582

1583
                // The retransmission timer has ticked which indicates that we
1584
                // should check if we need to prune or re-broadcast any of our
1585
                // personal channels or node announcement. This addresses the
1586
                // case of "zombie" channels and channel advertisements that
1587
                // have been dropped, or not properly propagated through the
1588
                // network.
1589
                case tick := <-d.cfg.RetransmitTicker.Ticks():
1✔
1590
                        if err := d.retransmitStaleAnns(ctx, tick); err != nil {
1✔
1591
                                log.Errorf("unable to rebroadcast stale "+
×
1592
                                        "announcements: %v", err)
×
1593
                        }
×
1594

1595
                // The gossiper has been signalled to exit, to we exit our
1596
                // main loop so the wait group can be decremented.
1597
                case <-d.quit:
30✔
1598
                        return
30✔
1599
                }
1600
        }
1601
}
1602

1603
// handleNetworkMessages is responsible for waiting for dependencies for a
1604
// given network message and processing the message. Once processed, it will
1605
// signal its dependants and add the new announcements to the announce batch.
1606
//
1607
// NOTE: must be run as a goroutine.
1608
func (d *AuthenticatedGossiper) handleNetworkMessages(ctx context.Context,
1609
        nMsg *networkMsg, deDuped *deDupedAnnouncements, jobID JobID) {
316✔
1610

316✔
1611
        defer d.wg.Done()
316✔
1612
        defer d.vb.CompleteJob()
316✔
1613

316✔
1614
        // We should only broadcast this message forward if it originated from
316✔
1615
        // us or it wasn't received as part of our initial historical sync.
316✔
1616
        shouldBroadcast := !nMsg.isRemote || d.syncMgr.IsGraphSynced()
316✔
1617

316✔
1618
        // If this message has an existing dependency, then we'll wait until
316✔
1619
        // that has been fully validated before we proceed.
316✔
1620
        err := d.vb.WaitForParents(jobID, nMsg.msg)
316✔
1621
        if err != nil {
316✔
1622
                log.Debugf("Validating network message %s got err: %v",
×
1623
                        nMsg.msg.MsgType(), err)
×
1624

×
1625
                if errors.Is(err, ErrVBarrierShuttingDown) {
×
1626
                        log.Warnf("unexpected error during validation "+
×
1627
                                "barrier shutdown: %v", err)
×
1628
                }
×
1629
                nMsg.err <- err
×
1630

×
1631
                return
×
1632
        }
1633

1634
        // Process the network announcement to determine if this is either a
1635
        // new announcement from our PoV or an edges to a prior vertex/edge we
1636
        // previously proceeded.
1637
        newAnns, allow := d.processNetworkAnnouncement(ctx, nMsg)
316✔
1638

316✔
1639
        log.Tracef("Processed network message %s, returned "+
316✔
1640
                "len(announcements)=%v, allowDependents=%v",
316✔
1641
                nMsg.msg.MsgType(), len(newAnns), allow)
316✔
1642

316✔
1643
        // If this message had any dependencies, then we can now signal them to
316✔
1644
        // continue.
316✔
1645
        err = d.vb.SignalDependents(nMsg.msg, jobID)
316✔
1646
        if err != nil {
316✔
1647
                // Something is wrong if SignalDependents returns an error.
×
1648
                log.Errorf("SignalDependents returned error for msg=%v with "+
×
1649
                        "JobID=%v", lnutils.SpewLogClosure(nMsg.msg), jobID)
×
1650

×
1651
                nMsg.err <- err
×
1652

×
1653
                return
×
1654
        }
×
1655

1656
        // If the announcement was accepted, then add the emitted announcements
1657
        // to our announce batch to be broadcast once the trickle timer ticks
1658
        // gain.
1659
        if newAnns != nil && shouldBroadcast {
353✔
1660
                // TODO(roasbeef): exclude peer that sent.
37✔
1661
                deDuped.AddMsgs(newAnns...)
37✔
1662
        } else if newAnns != nil {
317✔
1663
                log.Trace("Skipping broadcast of announcements received " +
1✔
1664
                        "during initial graph sync")
1✔
1665
        }
1✔
1666
}
1667

1668
// TODO(roasbeef): d/c peers that send updates not on our chain
1669

1670
// InitSyncState is called by outside sub-systems when a connection is
1671
// established to a new peer that understands how to perform channel range
1672
// queries. We'll allocate a new gossip syncer for it, and start any goroutines
1673
// needed to handle new queries.
UNCOV
1674
func (d *AuthenticatedGossiper) InitSyncState(syncPeer lnpeer.Peer) {
×
UNCOV
1675
        d.syncMgr.InitSyncState(syncPeer)
×
UNCOV
1676
}
×
1677

1678
// PruneSyncState is called by outside sub-systems once a peer that we were
1679
// previously connected to has been disconnected. In this case we can stop the
1680
// existing GossipSyncer assigned to the peer and free up resources.
UNCOV
1681
func (d *AuthenticatedGossiper) PruneSyncState(peer route.Vertex) {
×
UNCOV
1682
        d.syncMgr.PruneSyncState(peer)
×
UNCOV
1683
}
×
1684

1685
// isRecentlyRejectedMsg returns true if we recently rejected a message, and
1686
// false otherwise, This avoids expensive reprocessing of the message.
1687
func (d *AuthenticatedGossiper) isRecentlyRejectedMsg(msg lnwire.Message,
1688
        peerPub [33]byte) bool {
280✔
1689

280✔
1690
        var scid uint64
280✔
1691
        switch m := msg.(type) {
280✔
1692
        case *lnwire.ChannelUpdate1:
48✔
1693
                scid = m.ShortChannelID.ToUint64()
48✔
1694

1695
        case *lnwire.ChannelAnnouncement1:
218✔
1696
                scid = m.ShortChannelID.ToUint64()
218✔
1697

1698
        default:
14✔
1699
                return false
14✔
1700
        }
1701

1702
        _, err := d.recentRejects.Get(newRejectCacheKey(scid, peerPub))
266✔
1703
        return err != cache.ErrElementNotFound
266✔
1704
}
1705

1706
// retransmitStaleAnns examines all outgoing channels that the source node is
1707
// known to maintain to check to see if any of them are "stale". A channel is
1708
// stale iff, the last timestamp of its rebroadcast is older than the
1709
// RebroadcastInterval. We also check if a refreshed node announcement should
1710
// be resent.
1711
func (d *AuthenticatedGossiper) retransmitStaleAnns(ctx context.Context,
1712
        now time.Time) error {
31✔
1713

31✔
1714
        // Iterate over all of our channels and check if any of them fall
31✔
1715
        // within the prune interval or re-broadcast interval.
31✔
1716
        type updateTuple struct {
31✔
1717
                info *models.ChannelEdgeInfo
31✔
1718
                edge *models.ChannelEdgePolicy
31✔
1719
        }
31✔
1720

31✔
1721
        var (
31✔
1722
                havePublicChannels bool
31✔
1723
                edgesToUpdate      []updateTuple
31✔
1724
        )
31✔
1725
        err := d.cfg.Graph.ForAllOutgoingChannels(ctx, func(
31✔
1726
                info *models.ChannelEdgeInfo,
31✔
1727
                edge *models.ChannelEdgePolicy) error {
33✔
1728

2✔
1729
                // If there's no auth proof attached to this edge, it means
2✔
1730
                // that it is a private channel not meant to be announced to
2✔
1731
                // the greater network, so avoid sending channel updates for
2✔
1732
                // this channel to not leak its
2✔
1733
                // existence.
2✔
1734
                if info.AuthProof == nil {
3✔
1735
                        log.Debugf("Skipping retransmission of channel "+
1✔
1736
                                "without AuthProof: %v", info.ChannelID)
1✔
1737
                        return nil
1✔
1738
                }
1✔
1739

1740
                // We make a note that we have at least one public channel. We
1741
                // use this to determine whether we should send a node
1742
                // announcement below.
1743
                havePublicChannels = true
1✔
1744

1✔
1745
                // If this edge has a ChannelUpdate that was created before the
1✔
1746
                // introduction of the MaxHTLC field, then we'll update this
1✔
1747
                // edge to propagate this information in the network.
1✔
1748
                if !edge.MessageFlags.HasMaxHtlc() {
1✔
1749
                        // We'll make sure we support the new max_htlc field if
×
1750
                        // not already present.
×
1751
                        edge.MessageFlags |= lnwire.ChanUpdateRequiredMaxHtlc
×
1752
                        edge.MaxHTLC = lnwire.NewMSatFromSatoshis(info.Capacity)
×
1753

×
1754
                        edgesToUpdate = append(edgesToUpdate, updateTuple{
×
1755
                                info: info,
×
1756
                                edge: edge,
×
1757
                        })
×
1758
                        return nil
×
1759
                }
×
1760

1761
                timeElapsed := now.Sub(edge.LastUpdate)
1✔
1762

1✔
1763
                // If it's been longer than RebroadcastInterval since we've
1✔
1764
                // re-broadcasted the channel, add the channel to the set of
1✔
1765
                // edges we need to update.
1✔
1766
                if timeElapsed >= d.cfg.RebroadcastInterval {
2✔
1767
                        edgesToUpdate = append(edgesToUpdate, updateTuple{
1✔
1768
                                info: info,
1✔
1769
                                edge: edge,
1✔
1770
                        })
1✔
1771
                }
1✔
1772

1773
                return nil
1✔
UNCOV
1774
        }, func() {
×
UNCOV
1775
                havePublicChannels = false
×
UNCOV
1776
                edgesToUpdate = nil
×
UNCOV
1777
        })
×
1778
        if err != nil && !errors.Is(err, graphdb.ErrGraphNoEdgesFound) {
31✔
1779
                return fmt.Errorf("unable to retrieve outgoing channels: %w",
×
1780
                        err)
×
1781
        }
×
1782

1783
        var signedUpdates []lnwire.Message
31✔
1784
        for _, chanToUpdate := range edgesToUpdate {
32✔
1785
                // Re-sign and update the channel on disk and retrieve our
1✔
1786
                // ChannelUpdate to broadcast.
1✔
1787
                chanAnn, chanUpdate, err := d.updateChannel(
1✔
1788
                        ctx, chanToUpdate.info, chanToUpdate.edge,
1✔
1789
                )
1✔
1790
                if err != nil {
1✔
1791
                        return fmt.Errorf("unable to update channel: %w", err)
×
1792
                }
×
1793

1794
                // If we have a valid announcement to transmit, then we'll send
1795
                // that along with the update.
1796
                if chanAnn != nil {
2✔
1797
                        signedUpdates = append(signedUpdates, chanAnn)
1✔
1798
                }
1✔
1799

1800
                signedUpdates = append(signedUpdates, chanUpdate)
1✔
1801
        }
1802

1803
        // If we don't have any public channels, we return as we don't want to
1804
        // broadcast anything that would reveal our existence.
1805
        if !havePublicChannels {
61✔
1806
                return nil
30✔
1807
        }
30✔
1808

1809
        // We'll also check that our NodeAnnouncement is not too old.
1810
        currentNodeAnn := d.cfg.FetchSelfAnnouncement()
1✔
1811
        timestamp := time.Unix(int64(currentNodeAnn.Timestamp), 0)
1✔
1812
        timeElapsed := now.Sub(timestamp)
1✔
1813

1✔
1814
        // If it's been a full day since we've re-broadcasted the
1✔
1815
        // node announcement, refresh it and resend it.
1✔
1816
        nodeAnnStr := ""
1✔
1817
        if timeElapsed >= d.cfg.RebroadcastInterval {
2✔
1818
                newNodeAnn, err := d.cfg.UpdateSelfAnnouncement()
1✔
1819
                if err != nil {
1✔
1820
                        return fmt.Errorf("unable to get refreshed node "+
×
1821
                                "announcement: %v", err)
×
1822
                }
×
1823

1824
                signedUpdates = append(signedUpdates, &newNodeAnn)
1✔
1825
                nodeAnnStr = " and our refreshed node announcement"
1✔
1826

1✔
1827
                // Before broadcasting the refreshed node announcement, add it
1✔
1828
                // to our own graph.
1✔
1829
                if err := d.addNode(ctx, &newNodeAnn); err != nil {
2✔
1830
                        log.Errorf("Unable to add refreshed node announcement "+
1✔
1831
                                "to graph: %v", err)
1✔
1832
                }
1✔
1833
        }
1834

1835
        // If we don't have any updates to re-broadcast, then we'll exit
1836
        // early.
1837
        if len(signedUpdates) == 0 {
1✔
UNCOV
1838
                return nil
×
UNCOV
1839
        }
×
1840

1841
        log.Infof("Retransmitting %v outgoing channels%v",
1✔
1842
                len(edgesToUpdate), nodeAnnStr)
1✔
1843

1✔
1844
        // With all the wire announcements properly crafted, we'll broadcast
1✔
1845
        // our known outgoing channels to all our immediate peers.
1✔
1846
        if err := d.cfg.Broadcast(nil, signedUpdates...); err != nil {
1✔
1847
                return fmt.Errorf("unable to re-broadcast channels: %w", err)
×
1848
        }
×
1849

1850
        return nil
1✔
1851
}
1852

1853
// processChanPolicyUpdate generates a new set of channel updates for the
1854
// provided list of edges and updates the backing ChannelGraphSource.
1855
func (d *AuthenticatedGossiper) processChanPolicyUpdate(ctx context.Context,
1856
        edgesToUpdate []EdgeWithInfo) ([]networkMsg, error) {
1✔
1857

1✔
1858
        var chanUpdates []networkMsg
1✔
1859
        for _, edgeInfo := range edgesToUpdate {
4✔
1860
                // Now that we've collected all the channels we need to update,
3✔
1861
                // we'll re-sign and update the backing ChannelGraphSource, and
3✔
1862
                // retrieve our ChannelUpdate to broadcast.
3✔
1863
                _, chanUpdate, err := d.updateChannel(
3✔
1864
                        ctx, edgeInfo.Info, edgeInfo.Edge,
3✔
1865
                )
3✔
1866
                if err != nil {
3✔
1867
                        return nil, err
×
1868
                }
×
1869

1870
                // We'll avoid broadcasting any updates for private channels to
1871
                // avoid directly giving away their existence. Instead, we'll
1872
                // send the update directly to the remote party.
1873
                if edgeInfo.Info.AuthProof == nil {
4✔
1874
                        // If AuthProof is nil and an alias was found for this
1✔
1875
                        // ChannelID (meaning the option-scid-alias feature was
1✔
1876
                        // negotiated), we'll replace the ShortChannelID in the
1✔
1877
                        // update with the peer's alias. We do this after
1✔
1878
                        // updateChannel so that the alias isn't persisted to
1✔
1879
                        // the database.
1✔
1880
                        chanID := lnwire.NewChanIDFromOutPoint(
1✔
1881
                                edgeInfo.Info.ChannelPoint,
1✔
1882
                        )
1✔
1883

1✔
1884
                        var defaultAlias lnwire.ShortChannelID
1✔
1885
                        foundAlias, _ := d.cfg.GetAlias(chanID)
1✔
1886
                        if foundAlias != defaultAlias {
1✔
UNCOV
1887
                                chanUpdate.ShortChannelID = foundAlias
×
UNCOV
1888

×
UNCOV
1889
                                sig, err := d.cfg.SignAliasUpdate(chanUpdate)
×
UNCOV
1890
                                if err != nil {
×
1891
                                        log.Errorf("Unable to sign alias "+
×
1892
                                                "update: %v", err)
×
1893
                                        continue
×
1894
                                }
1895

UNCOV
1896
                                lnSig, err := lnwire.NewSigFromSignature(sig)
×
UNCOV
1897
                                if err != nil {
×
1898
                                        log.Errorf("Unable to create sig: %v",
×
1899
                                                err)
×
1900
                                        continue
×
1901
                                }
1902

UNCOV
1903
                                chanUpdate.Signature = lnSig
×
1904
                        }
1905

1906
                        remotePubKey := remotePubFromChanInfo(
1✔
1907
                                edgeInfo.Info, chanUpdate.ChannelFlags,
1✔
1908
                        )
1✔
1909
                        err := d.reliableSender.sendMessage(
1✔
1910
                                ctx, chanUpdate, remotePubKey,
1✔
1911
                        )
1✔
1912
                        if err != nil {
1✔
1913
                                log.Errorf("Unable to reliably send %v for "+
×
1914
                                        "channel=%v to peer=%x: %v",
×
1915
                                        chanUpdate.MsgType(),
×
1916
                                        chanUpdate.ShortChannelID,
×
1917
                                        remotePubKey, err)
×
1918
                        }
×
1919
                        continue
1✔
1920
                }
1921

1922
                // We set ourselves as the source of this message to indicate
1923
                // that we shouldn't skip any peers when sending this message.
1924
                chanUpdates = append(chanUpdates, networkMsg{
2✔
1925
                        source:   d.selfKey,
2✔
1926
                        isRemote: false,
2✔
1927
                        msg:      chanUpdate,
2✔
1928
                })
2✔
1929
        }
1930

1931
        return chanUpdates, nil
1✔
1932
}
1933

1934
// remotePubFromChanInfo returns the public key of the remote peer given a
1935
// ChannelEdgeInfo that describe a channel we have with them.
1936
func remotePubFromChanInfo(chanInfo *models.ChannelEdgeInfo,
1937
        chanFlags lnwire.ChanUpdateChanFlags) [33]byte {
12✔
1938

12✔
1939
        var remotePubKey [33]byte
12✔
1940
        switch {
12✔
1941
        case chanFlags&lnwire.ChanUpdateDirection == 0:
12✔
1942
                remotePubKey = chanInfo.NodeKey2Bytes
12✔
UNCOV
1943
        case chanFlags&lnwire.ChanUpdateDirection == 1:
×
UNCOV
1944
                remotePubKey = chanInfo.NodeKey1Bytes
×
1945
        }
1946

1947
        return remotePubKey
12✔
1948
}
1949

1950
// processRejectedEdge examines a rejected edge to see if we can extract any
1951
// new announcements from it.  An edge will get rejected if we already added
1952
// the same edge without AuthProof to the graph. If the received announcement
1953
// contains a proof, we can add this proof to our edge.  We can end up in this
1954
// situation in the case where we create a channel, but for some reason fail
1955
// to receive the remote peer's proof, while the remote peer is able to fully
1956
// assemble the proof and craft the ChannelAnnouncement.
1957
func (d *AuthenticatedGossiper) processRejectedEdge(_ context.Context,
1958
        chanAnnMsg *lnwire.ChannelAnnouncement1,
UNCOV
1959
        proof *models.ChannelAuthProof) ([]networkMsg, error) {
×
UNCOV
1960

×
UNCOV
1961
        // First, we'll fetch the state of the channel as we know if from the
×
UNCOV
1962
        // database.
×
UNCOV
1963
        chanInfo, e1, e2, err := d.cfg.Graph.GetChannelByID(
×
UNCOV
1964
                chanAnnMsg.ShortChannelID,
×
UNCOV
1965
        )
×
UNCOV
1966
        if err != nil {
×
1967
                return nil, err
×
1968
        }
×
1969

1970
        // The edge is in the graph, and has a proof attached, then we'll just
1971
        // reject it as normal.
UNCOV
1972
        if chanInfo.AuthProof != nil {
×
UNCOV
1973
                return nil, nil
×
UNCOV
1974
        }
×
1975

1976
        // Otherwise, this means that the edge is within the graph, but it
1977
        // doesn't yet have a proper proof attached. If we did not receive
1978
        // the proof such that we now can add it, there's nothing more we
1979
        // can do.
1980
        if proof == nil {
×
1981
                return nil, nil
×
1982
        }
×
1983

1984
        // We'll then create then validate the new fully assembled
1985
        // announcement.
1986
        chanAnn, e1Ann, e2Ann, err := netann.CreateChanAnnouncement(
×
1987
                proof, chanInfo, e1, e2,
×
1988
        )
×
1989
        if err != nil {
×
1990
                return nil, err
×
1991
        }
×
1992
        err = netann.ValidateChannelAnn(chanAnn, d.fetchPKScript)
×
1993
        if err != nil {
×
1994
                err := fmt.Errorf("assembled channel announcement proof "+
×
1995
                        "for shortChanID=%v isn't valid: %v",
×
1996
                        chanAnnMsg.ShortChannelID, err)
×
1997
                log.Error(err)
×
1998
                return nil, err
×
1999
        }
×
2000

2001
        // If everything checks out, then we'll add the fully assembled proof
2002
        // to the database.
2003
        err = d.cfg.Graph.AddProof(chanAnnMsg.ShortChannelID, proof)
×
2004
        if err != nil {
×
2005
                err := fmt.Errorf("unable add proof to shortChanID=%v: %w",
×
2006
                        chanAnnMsg.ShortChannelID, err)
×
2007
                log.Error(err)
×
2008
                return nil, err
×
2009
        }
×
2010

2011
        // As we now have a complete channel announcement for this channel,
2012
        // we'll construct the announcement so they can be broadcast out to all
2013
        // our peers.
2014
        announcements := make([]networkMsg, 0, 3)
×
2015
        announcements = append(announcements, networkMsg{
×
2016
                source: d.selfKey,
×
2017
                msg:    chanAnn,
×
2018
        })
×
2019
        if e1Ann != nil {
×
2020
                announcements = append(announcements, networkMsg{
×
2021
                        source: d.selfKey,
×
2022
                        msg:    e1Ann,
×
2023
                })
×
2024
        }
×
2025
        if e2Ann != nil {
×
2026
                announcements = append(announcements, networkMsg{
×
2027
                        source: d.selfKey,
×
2028
                        msg:    e2Ann,
×
2029
                })
×
2030

×
2031
        }
×
2032

2033
        return announcements, nil
×
2034
}
2035

2036
// fetchPKScript fetches the output script for the given SCID.
2037
func (d *AuthenticatedGossiper) fetchPKScript(chanID lnwire.ShortChannelID) (
2038
        txscript.ScriptClass, btcutil.Address, error) {
×
2039

×
2040
        pkScript, err := lnwallet.FetchPKScriptWithQuit(
×
2041
                d.cfg.ChainIO, chanID, d.quit,
×
2042
        )
×
2043
        if err != nil {
×
2044
                return txscript.WitnessUnknownTy, nil, err
×
2045
        }
×
2046

2047
        scriptClass, addrs, _, err := txscript.ExtractPkScriptAddrs(
×
2048
                pkScript, d.cfg.ChainParams,
×
2049
        )
×
2050
        if err != nil {
×
2051
                return txscript.WitnessUnknownTy, nil, err
×
2052
        }
×
2053

2054
        if len(addrs) != 1 {
×
2055
                return txscript.WitnessUnknownTy, nil, fmt.Errorf("expected "+
×
2056
                        "1 address, got: %d", len(addrs))
×
2057
        }
×
2058

2059
        return scriptClass, addrs[0], nil
×
2060
}
2061

2062
// addNode processes the given node announcement, and adds it to our channel
2063
// graph.
2064
func (d *AuthenticatedGossiper) addNode(ctx context.Context,
2065
        msg *lnwire.NodeAnnouncement, op ...batch.SchedulerOption) error {
17✔
2066

17✔
2067
        if err := netann.ValidateNodeAnn(msg); err != nil {
18✔
2068
                return fmt.Errorf("unable to validate node announcement: %w",
1✔
2069
                        err)
1✔
2070
        }
1✔
2071

2072
        return d.cfg.Graph.AddNode(
16✔
2073
                ctx, models.NodeFromWireAnnouncement(msg), op...,
16✔
2074
        )
16✔
2075
}
2076

2077
// isPremature decides whether a given network message has a block height+delta
2078
// value specified in the future. If so, the message will be added to the
2079
// future message map and be processed when the block height as reached.
2080
//
2081
// NOTE: must be used inside a lock.
2082
func (d *AuthenticatedGossiper) isPremature(chanID lnwire.ShortChannelID,
2083
        delta uint32, msg *networkMsg) bool {
289✔
2084

289✔
2085
        // The channel is already confirmed at chanID.BlockHeight so we minus
289✔
2086
        // one block. For instance, if the required confirmation for this
289✔
2087
        // channel announcement is 6, we then only need to wait for 5 more
289✔
2088
        // blocks once the funding tx is confirmed.
289✔
2089
        if delta > 0 {
289✔
UNCOV
2090
                delta--
×
UNCOV
2091
        }
×
2092

2093
        msgHeight := chanID.BlockHeight + delta
289✔
2094

289✔
2095
        // The message height is smaller or equal to our best known height,
289✔
2096
        // thus the message is mature.
289✔
2097
        if msgHeight <= d.bestHeight {
577✔
2098
                return false
288✔
2099
        }
288✔
2100

2101
        // Add the premature message to our future messages which will be
2102
        // resent once the block height has reached.
2103
        //
2104
        // Copy the networkMsgs since the old message's err chan will be
2105
        // consumed.
2106
        copied := &networkMsg{
1✔
2107
                peer:              msg.peer,
1✔
2108
                source:            msg.source,
1✔
2109
                msg:               msg.msg,
1✔
2110
                optionalMsgFields: msg.optionalMsgFields,
1✔
2111
                isRemote:          msg.isRemote,
1✔
2112
                err:               make(chan error, 1),
1✔
2113
        }
1✔
2114

1✔
2115
        // Create the cached message.
1✔
2116
        cachedMsg := &cachedFutureMsg{
1✔
2117
                msg:    copied,
1✔
2118
                height: msgHeight,
1✔
2119
        }
1✔
2120

1✔
2121
        // Increment the msg ID and add it to the cache.
1✔
2122
        nextMsgID := d.futureMsgs.nextMsgID()
1✔
2123
        _, err := d.futureMsgs.Put(nextMsgID, cachedMsg)
1✔
2124
        if err != nil {
1✔
2125
                log.Errorf("Adding future message got error: %v", err)
×
2126
        }
×
2127

2128
        log.Debugf("Network message: %v added to future messages for "+
1✔
2129
                "msgHeight=%d, bestHeight=%d", msg.msg.MsgType(),
1✔
2130
                msgHeight, d.bestHeight)
1✔
2131

1✔
2132
        return true
1✔
2133
}
2134

2135
// processNetworkAnnouncement processes a new network relate authenticated
2136
// channel or node announcement or announcements proofs. If the announcement
2137
// didn't affect the internal state due to either being out of date, invalid,
2138
// or redundant, then nil is returned. Otherwise, the set of announcements will
2139
// be returned which should be broadcasted to the rest of the network. The
2140
// boolean returned indicates whether any dependents of the announcement should
2141
// attempt to be processed as well.
2142
func (d *AuthenticatedGossiper) processNetworkAnnouncement(ctx context.Context,
2143
        nMsg *networkMsg) ([]networkMsg, bool) {
337✔
2144

337✔
2145
        // If this is a remote update, we set the scheduler option to lazily
337✔
2146
        // add it to the graph.
337✔
2147
        var schedulerOp []batch.SchedulerOption
337✔
2148
        if nMsg.isRemote {
627✔
2149
                schedulerOp = append(schedulerOp, batch.LazyAdd())
290✔
2150
        }
290✔
2151

2152
        switch msg := nMsg.msg.(type) {
337✔
2153
        // A new node announcement has arrived which either presents new
2154
        // information about a node in one of the channels we know about, or a
2155
        // updating previously advertised information.
2156
        case *lnwire.NodeAnnouncement:
24✔
2157
                return d.handleNodeAnnouncement(ctx, nMsg, msg, schedulerOp)
24✔
2158

2159
        // A new channel announcement has arrived, this indicates the
2160
        // *creation* of a new channel within the network. This only advertises
2161
        // the existence of a channel and not yet the routing policies in
2162
        // either direction of the channel.
2163
        case *lnwire.ChannelAnnouncement1:
231✔
2164
                return d.handleChanAnnouncement(ctx, nMsg, msg, schedulerOp...)
231✔
2165

2166
        // A new authenticated channel edge update has arrived. This indicates
2167
        // that the directional information for an already known channel has
2168
        // been updated.
2169
        case *lnwire.ChannelUpdate1:
61✔
2170
                return d.handleChanUpdate(ctx, nMsg, msg, schedulerOp)
61✔
2171

2172
        // A new signature announcement has been received. This indicates
2173
        // willingness of nodes involved in the funding of a channel to
2174
        // announce this new channel to the rest of the world.
2175
        case *lnwire.AnnounceSignatures1:
21✔
2176
                return d.handleAnnSig(ctx, nMsg, msg)
21✔
2177

2178
        default:
×
2179
                err := errors.New("wrong type of the announcement")
×
2180
                nMsg.err <- err
×
2181
                return nil, false
×
2182
        }
2183
}
2184

2185
// processZombieUpdate determines whether the provided channel update should
2186
// resurrect a given zombie edge.
2187
//
2188
// NOTE: only the NodeKey1Bytes and NodeKey2Bytes members of the ChannelEdgeInfo
2189
// should be inspected.
2190
func (d *AuthenticatedGossiper) processZombieUpdate(_ context.Context,
2191
        chanInfo *models.ChannelEdgeInfo, scid lnwire.ShortChannelID,
2192
        msg *lnwire.ChannelUpdate1) error {
3✔
2193

3✔
2194
        // The least-significant bit in the flag on the channel update tells us
3✔
2195
        // which edge is being updated.
3✔
2196
        isNode1 := msg.ChannelFlags&lnwire.ChanUpdateDirection == 0
3✔
2197

3✔
2198
        // Since we've deemed the update as not stale above, before marking it
3✔
2199
        // live, we'll make sure it has been signed by the correct party. If we
3✔
2200
        // have both pubkeys, either party can resurrect the channel. If we've
3✔
2201
        // already marked this with the stricter, single-sided resurrection we
3✔
2202
        // will only have the pubkey of the node with the oldest timestamp.
3✔
2203
        var pubKey *btcec.PublicKey
3✔
2204
        switch {
3✔
2205
        case isNode1 && chanInfo.NodeKey1Bytes != emptyPubkey:
×
2206
                pubKey, _ = chanInfo.NodeKey1()
×
2207
        case !isNode1 && chanInfo.NodeKey2Bytes != emptyPubkey:
2✔
2208
                pubKey, _ = chanInfo.NodeKey2()
2✔
2209
        }
2210
        if pubKey == nil {
4✔
2211
                return fmt.Errorf("incorrect pubkey to resurrect zombie "+
1✔
2212
                        "with chan_id=%v", msg.ShortChannelID)
1✔
2213
        }
1✔
2214

2215
        err := netann.VerifyChannelUpdateSignature(msg, pubKey)
2✔
2216
        if err != nil {
3✔
2217
                return fmt.Errorf("unable to verify channel "+
1✔
2218
                        "update signature: %v", err)
1✔
2219
        }
1✔
2220

2221
        // With the signature valid, we'll proceed to mark the
2222
        // edge as live and wait for the channel announcement to
2223
        // come through again.
2224
        err = d.cfg.Graph.MarkEdgeLive(scid)
1✔
2225
        switch {
1✔
2226
        case errors.Is(err, graphdb.ErrZombieEdgeNotFound):
×
2227
                log.Errorf("edge with chan_id=%v was not found in the "+
×
2228
                        "zombie index: %v", err)
×
2229

×
2230
                return nil
×
2231

2232
        case err != nil:
×
2233
                return fmt.Errorf("unable to remove edge with "+
×
2234
                        "chan_id=%v from zombie index: %v",
×
2235
                        msg.ShortChannelID, err)
×
2236

2237
        default:
1✔
2238
        }
2239

2240
        log.Debugf("Removed edge with chan_id=%v from zombie "+
1✔
2241
                "index", msg.ShortChannelID)
1✔
2242

1✔
2243
        return nil
1✔
2244
}
2245

2246
// fetchNodeAnn fetches the latest signed node announcement from our point of
2247
// view for the node with the given public key. It also validates the node
2248
// announcement fields and returns an error if they are invalid to prevent
2249
// forwarding invalid node announcements to our peers.
2250
func (d *AuthenticatedGossiper) fetchNodeAnn(ctx context.Context,
2251
        pubKey [33]byte) (*lnwire.NodeAnnouncement, error) {
20✔
2252

20✔
2253
        node, err := d.cfg.Graph.FetchNode(ctx, pubKey)
20✔
2254
        if err != nil {
26✔
2255
                return nil, err
6✔
2256
        }
6✔
2257

2258
        nodeAnn, err := node.NodeAnnouncement(true)
14✔
2259
        if err != nil {
14✔
UNCOV
2260
                return nil, err
×
UNCOV
2261
        }
×
2262

2263
        return nodeAnn, netann.ValidateNodeAnnFields(nodeAnn)
14✔
2264
}
2265

2266
// isMsgStale determines whether a message retrieved from the backing
2267
// MessageStore is seen as stale by the current graph.
2268
func (d *AuthenticatedGossiper) isMsgStale(_ context.Context,
2269
        msg lnwire.Message) bool {
12✔
2270

12✔
2271
        switch msg := msg.(type) {
12✔
2272
        case *lnwire.AnnounceSignatures1:
2✔
2273
                chanInfo, _, _, err := d.cfg.Graph.GetChannelByID(
2✔
2274
                        msg.ShortChannelID,
2✔
2275
                )
2✔
2276

2✔
2277
                // If the channel cannot be found, it is most likely a leftover
2✔
2278
                // message for a channel that was closed, so we can consider it
2✔
2279
                // stale.
2✔
2280
                if errors.Is(err, graphdb.ErrEdgeNotFound) {
2✔
UNCOV
2281
                        return true
×
UNCOV
2282
                }
×
2283
                if err != nil {
2✔
2284
                        log.Debugf("Unable to retrieve channel=%v from graph: "+
×
2285
                                "%v", msg.ShortChannelID, err)
×
2286
                        return false
×
2287
                }
×
2288

2289
                // If the proof exists in the graph, then we have successfully
2290
                // received the remote proof and assembled the full proof, so we
2291
                // can safely delete the local proof from the database.
2292
                return chanInfo.AuthProof != nil
2✔
2293

2294
        case *lnwire.ChannelUpdate1:
10✔
2295
                _, p1, p2, err := d.cfg.Graph.GetChannelByID(msg.ShortChannelID)
10✔
2296

10✔
2297
                // If the channel cannot be found, it is most likely a leftover
10✔
2298
                // message for a channel that was closed, so we can consider it
10✔
2299
                // stale.
10✔
2300
                if errors.Is(err, graphdb.ErrEdgeNotFound) {
10✔
UNCOV
2301
                        return true
×
UNCOV
2302
                }
×
2303
                if err != nil {
10✔
2304
                        log.Debugf("Unable to retrieve channel=%v from graph: "+
×
2305
                                "%v", msg.ShortChannelID, err)
×
2306
                        return false
×
2307
                }
×
2308

2309
                // Otherwise, we'll retrieve the correct policy that we
2310
                // currently have stored within our graph to check if this
2311
                // message is stale by comparing its timestamp.
2312
                var p *models.ChannelEdgePolicy
10✔
2313
                if msg.ChannelFlags&lnwire.ChanUpdateDirection == 0 {
20✔
2314
                        p = p1
10✔
2315
                } else {
10✔
UNCOV
2316
                        p = p2
×
UNCOV
2317
                }
×
2318

2319
                // If the policy is still unknown, then we can consider this
2320
                // policy fresh.
2321
                if p == nil {
10✔
2322
                        return false
×
2323
                }
×
2324

2325
                timestamp := time.Unix(int64(msg.Timestamp), 0)
10✔
2326
                return p.LastUpdate.After(timestamp)
10✔
2327

2328
        default:
×
2329
                // We'll make sure to not mark any unsupported messages as stale
×
2330
                // to ensure they are not removed.
×
2331
                return false
×
2332
        }
2333
}
2334

2335
// updateChannel creates a new fully signed update for the channel, and updates
2336
// the underlying graph with the new state.
2337
func (d *AuthenticatedGossiper) updateChannel(ctx context.Context,
2338
        info *models.ChannelEdgeInfo,
2339
        edge *models.ChannelEdgePolicy) (*lnwire.ChannelAnnouncement1,
2340
        *lnwire.ChannelUpdate1, error) {
4✔
2341

4✔
2342
        // Parse the unsigned edge into a channel update.
4✔
2343
        chanUpdate := netann.UnsignedChannelUpdateFromEdge(info, edge)
4✔
2344

4✔
2345
        // We'll generate a new signature over a digest of the channel
4✔
2346
        // announcement itself and update the timestamp to ensure it propagate.
4✔
2347
        err := netann.SignChannelUpdate(
4✔
2348
                d.cfg.AnnSigner, d.selfKeyLoc, chanUpdate,
4✔
2349
                netann.ChanUpdSetTimestamp,
4✔
2350
        )
4✔
2351
        if err != nil {
4✔
2352
                return nil, nil, err
×
2353
        }
×
2354

2355
        // Next, we'll set the new signature in place, and update the reference
2356
        // in the backing slice.
2357
        edge.LastUpdate = time.Unix(int64(chanUpdate.Timestamp), 0)
4✔
2358
        edge.SigBytes = chanUpdate.Signature.ToSignatureBytes()
4✔
2359

4✔
2360
        // To ensure that our signature is valid, we'll verify it ourself
4✔
2361
        // before committing it to the slice returned.
4✔
2362
        err = netann.ValidateChannelUpdateAnn(
4✔
2363
                d.selfKey, info.Capacity, chanUpdate,
4✔
2364
        )
4✔
2365
        if err != nil {
4✔
2366
                return nil, nil, fmt.Errorf("generated invalid channel "+
×
2367
                        "update sig: %v", err)
×
2368
        }
×
2369

2370
        // Finally, we'll write the new edge policy to disk.
2371
        if err := d.cfg.Graph.UpdateEdge(ctx, edge); err != nil {
4✔
2372
                return nil, nil, err
×
2373
        }
×
2374

2375
        // We'll also create the original channel announcement so the two can
2376
        // be broadcast along side each other (if necessary), but only if we
2377
        // have a full channel announcement for this channel.
2378
        var chanAnn *lnwire.ChannelAnnouncement1
4✔
2379
        if info.AuthProof != nil {
7✔
2380
                chanID := lnwire.NewShortChanIDFromInt(info.ChannelID)
3✔
2381
                chanAnn = &lnwire.ChannelAnnouncement1{
3✔
2382
                        ShortChannelID:  chanID,
3✔
2383
                        NodeID1:         info.NodeKey1Bytes,
3✔
2384
                        NodeID2:         info.NodeKey2Bytes,
3✔
2385
                        ChainHash:       info.ChainHash,
3✔
2386
                        BitcoinKey1:     info.BitcoinKey1Bytes,
3✔
2387
                        Features:        lnwire.NewRawFeatureVector(),
3✔
2388
                        BitcoinKey2:     info.BitcoinKey2Bytes,
3✔
2389
                        ExtraOpaqueData: info.ExtraOpaqueData,
3✔
2390
                }
3✔
2391
                chanAnn.NodeSig1, err = lnwire.NewSigFromECDSARawSignature(
3✔
2392
                        info.AuthProof.NodeSig1Bytes,
3✔
2393
                )
3✔
2394
                if err != nil {
3✔
2395
                        return nil, nil, err
×
2396
                }
×
2397
                chanAnn.NodeSig2, err = lnwire.NewSigFromECDSARawSignature(
3✔
2398
                        info.AuthProof.NodeSig2Bytes,
3✔
2399
                )
3✔
2400
                if err != nil {
3✔
2401
                        return nil, nil, err
×
2402
                }
×
2403
                chanAnn.BitcoinSig1, err = lnwire.NewSigFromECDSARawSignature(
3✔
2404
                        info.AuthProof.BitcoinSig1Bytes,
3✔
2405
                )
3✔
2406
                if err != nil {
3✔
2407
                        return nil, nil, err
×
2408
                }
×
2409
                chanAnn.BitcoinSig2, err = lnwire.NewSigFromECDSARawSignature(
3✔
2410
                        info.AuthProof.BitcoinSig2Bytes,
3✔
2411
                )
3✔
2412
                if err != nil {
3✔
2413
                        return nil, nil, err
×
2414
                }
×
2415
        }
2416

2417
        return chanAnn, chanUpdate, err
4✔
2418
}
2419

2420
// SyncManager returns the gossiper's SyncManager instance.
UNCOV
2421
func (d *AuthenticatedGossiper) SyncManager() *SyncManager {
×
UNCOV
2422
        return d.syncMgr
×
UNCOV
2423
}
×
2424

2425
// IsKeepAliveUpdate determines whether this channel update is considered a
2426
// keep-alive update based on the previous channel update processed for the same
2427
// direction.
2428
func IsKeepAliveUpdate(update *lnwire.ChannelUpdate1,
2429
        prev *models.ChannelEdgePolicy) bool {
17✔
2430

17✔
2431
        // Both updates should be from the same direction.
17✔
2432
        if update.ChannelFlags&lnwire.ChanUpdateDirection !=
17✔
2433
                prev.ChannelFlags&lnwire.ChanUpdateDirection {
17✔
2434

×
2435
                return false
×
2436
        }
×
2437

2438
        // The timestamp should always increase for a keep-alive update.
2439
        timestamp := time.Unix(int64(update.Timestamp), 0)
17✔
2440
        if !timestamp.After(prev.LastUpdate) {
17✔
2441
                return false
×
2442
        }
×
2443

2444
        // None of the remaining fields should change for a keep-alive update.
2445
        if update.ChannelFlags.IsDisabled() != prev.ChannelFlags.IsDisabled() {
17✔
UNCOV
2446
                return false
×
UNCOV
2447
        }
×
2448
        if lnwire.MilliSatoshi(update.BaseFee) != prev.FeeBaseMSat {
32✔
2449
                return false
15✔
2450
        }
15✔
2451
        if lnwire.MilliSatoshi(update.FeeRate) != prev.FeeProportionalMillionths {
2✔
UNCOV
2452
                return false
×
UNCOV
2453
        }
×
2454
        if update.TimeLockDelta != prev.TimeLockDelta {
2✔
2455
                return false
×
2456
        }
×
2457
        if update.HtlcMinimumMsat != prev.MinHTLC {
2✔
2458
                return false
×
2459
        }
×
2460
        if update.MessageFlags.HasMaxHtlc() && !prev.MessageFlags.HasMaxHtlc() {
2✔
2461
                return false
×
2462
        }
×
2463
        if update.HtlcMaximumMsat != prev.MaxHTLC {
2✔
2464
                return false
×
2465
        }
×
2466
        if !bytes.Equal(update.ExtraOpaqueData, prev.ExtraOpaqueData) {
2✔
UNCOV
2467
                return false
×
UNCOV
2468
        }
×
2469
        return true
2✔
2470
}
2471

2472
// latestHeight returns the gossiper's latest height known of the chain.
UNCOV
2473
func (d *AuthenticatedGossiper) latestHeight() uint32 {
×
UNCOV
2474
        d.Lock()
×
UNCOV
2475
        defer d.Unlock()
×
UNCOV
2476
        return d.bestHeight
×
UNCOV
2477
}
×
2478

2479
// handleNodeAnnouncement processes a new node announcement.
2480
func (d *AuthenticatedGossiper) handleNodeAnnouncement(ctx context.Context,
2481
        nMsg *networkMsg, nodeAnn *lnwire.NodeAnnouncement,
2482
        ops []batch.SchedulerOption) ([]networkMsg, bool) {
24✔
2483

24✔
2484
        timestamp := time.Unix(int64(nodeAnn.Timestamp), 0)
24✔
2485

24✔
2486
        log.Debugf("Processing NodeAnnouncement: peer=%v, timestamp=%v, "+
24✔
2487
                "node=%x, source=%x", nMsg.peer, timestamp, nodeAnn.NodeID,
24✔
2488
                nMsg.source.SerializeCompressed())
24✔
2489

24✔
2490
        // We'll quickly ask the router if it already has a newer update for
24✔
2491
        // this node so we can skip validating signatures if not required.
24✔
2492
        if d.cfg.Graph.IsStaleNode(ctx, nodeAnn.NodeID, timestamp) {
32✔
2493
                log.Debugf("Skipped processing stale node: %x", nodeAnn.NodeID)
8✔
2494
                nMsg.err <- nil
8✔
2495
                return nil, true
8✔
2496
        }
8✔
2497

2498
        if err := d.addNode(ctx, nodeAnn, ops...); err != nil {
16✔
UNCOV
2499
                log.Debugf("Adding node: %x got error: %v", nodeAnn.NodeID,
×
UNCOV
2500
                        err)
×
UNCOV
2501

×
UNCOV
2502
                if !graph.IsError(
×
UNCOV
2503
                        err,
×
UNCOV
2504
                        graph.ErrOutdated,
×
UNCOV
2505
                        graph.ErrIgnored,
×
UNCOV
2506
                ) {
×
2507

×
2508
                        log.Error(err)
×
2509
                }
×
2510

UNCOV
2511
                nMsg.err <- err
×
UNCOV
2512
                return nil, false
×
2513
        }
2514

2515
        // In order to ensure we don't leak unadvertised nodes, we'll make a
2516
        // quick check to ensure this node intends to publicly advertise itself
2517
        // to the network.
2518
        isPublic, err := d.cfg.Graph.IsPublicNode(nodeAnn.NodeID)
16✔
2519
        if err != nil {
16✔
2520
                log.Errorf("Unable to determine if node %x is advertised: %v",
×
2521
                        nodeAnn.NodeID, err)
×
2522
                nMsg.err <- err
×
2523
                return nil, false
×
2524
        }
×
2525

2526
        var announcements []networkMsg
16✔
2527

16✔
2528
        // If it does, we'll add their announcement to our batch so that it can
16✔
2529
        // be broadcast to the rest of our peers.
16✔
2530
        if isPublic {
19✔
2531
                announcements = append(announcements, networkMsg{
3✔
2532
                        peer:     nMsg.peer,
3✔
2533
                        isRemote: nMsg.isRemote,
3✔
2534
                        source:   nMsg.source,
3✔
2535
                        msg:      nodeAnn,
3✔
2536
                })
3✔
2537
        } else {
16✔
2538
                log.Tracef("Skipping broadcasting node announcement for %x "+
13✔
2539
                        "due to being unadvertised", nodeAnn.NodeID)
13✔
2540
        }
13✔
2541

2542
        nMsg.err <- nil
16✔
2543
        // TODO(roasbeef): get rid of the above
16✔
2544

16✔
2545
        log.Debugf("Processed NodeAnnouncement: peer=%v, timestamp=%v, "+
16✔
2546
                "node=%x, source=%x", nMsg.peer, timestamp, nodeAnn.NodeID,
16✔
2547
                nMsg.source.SerializeCompressed())
16✔
2548

16✔
2549
        return announcements, true
16✔
2550
}
2551

2552
// handleChanAnnouncement processes a new channel announcement.
2553
//
2554
//nolint:funlen
2555
func (d *AuthenticatedGossiper) handleChanAnnouncement(ctx context.Context,
2556
        nMsg *networkMsg, ann *lnwire.ChannelAnnouncement1,
2557
        ops ...batch.SchedulerOption) ([]networkMsg, bool) {
234✔
2558

234✔
2559
        scid := ann.ShortChannelID
234✔
2560
        chainHash := d.cfg.ChainParams.GenesisHash
234✔
2561

234✔
2562
        log.Debugf("Processing ChannelAnnouncement1: peer=%v, short_chan_id=%v",
234✔
2563
                nMsg.peer, scid.ToUint64())
234✔
2564

234✔
2565
        // We'll ignore any channel announcements that target any chain other
234✔
2566
        // than the set of chains we know of.
234✔
2567
        if !bytes.Equal(ann.ChainHash[:], chainHash[:]) {
234✔
2568
                err := fmt.Errorf("ignoring ChannelAnnouncement1 from chain=%v"+
×
2569
                        ", gossiper on chain=%v", ann.ChainHash, chainHash)
×
2570
                log.Errorf(err.Error())
×
2571

×
2572
                key := newRejectCacheKey(
×
2573
                        scid.ToUint64(),
×
2574
                        sourceToPub(nMsg.source),
×
2575
                )
×
2576
                _, _ = d.recentRejects.Put(key, &cachedReject{})
×
2577

×
2578
                nMsg.err <- err
×
2579
                return nil, false
×
2580
        }
×
2581

2582
        // If this is a remote ChannelAnnouncement with an alias SCID, we'll
2583
        // reject the announcement. Since the router accepts alias SCIDs,
2584
        // not erroring out would be a DoS vector.
2585
        if nMsg.isRemote && d.cfg.IsAlias(scid) {
234✔
2586
                err := fmt.Errorf("ignoring remote alias channel=%v", scid)
×
2587
                log.Errorf(err.Error())
×
2588

×
2589
                key := newRejectCacheKey(
×
2590
                        scid.ToUint64(),
×
2591
                        sourceToPub(nMsg.source),
×
2592
                )
×
2593
                _, _ = d.recentRejects.Put(key, &cachedReject{})
×
2594

×
2595
                nMsg.err <- err
×
2596
                return nil, false
×
2597
        }
×
2598

2599
        // If the advertised inclusionary block is beyond our knowledge of the
2600
        // chain tip, then we'll ignore it for now.
2601
        d.Lock()
234✔
2602
        if nMsg.isRemote && d.isPremature(scid, 0, nMsg) {
235✔
2603
                log.Warnf("Announcement for chan_id=(%v), is premature: "+
1✔
2604
                        "advertises height %v, only height %v is known",
1✔
2605
                        scid.ToUint64(), scid.BlockHeight, d.bestHeight)
1✔
2606
                d.Unlock()
1✔
2607
                nMsg.err <- nil
1✔
2608
                return nil, false
1✔
2609
        }
1✔
2610
        d.Unlock()
233✔
2611

233✔
2612
        // At this point, we'll now ask the router if this is a zombie/known
233✔
2613
        // edge. If so we can skip all the processing below.
233✔
2614
        if d.cfg.Graph.IsKnownEdge(scid) {
234✔
2615
                nMsg.err <- nil
1✔
2616
                return nil, true
1✔
2617
        }
1✔
2618

2619
        // Check if the channel is already closed in which case we can ignore
2620
        // it.
2621
        closed, err := d.cfg.ScidCloser.IsClosedScid(scid)
232✔
2622
        if err != nil {
232✔
2623
                log.Errorf("failed to check if scid %v is closed: %v", scid,
×
2624
                        err)
×
2625
                nMsg.err <- err
×
2626

×
2627
                return nil, false
×
2628
        }
×
2629

2630
        if closed {
233✔
2631
                err = fmt.Errorf("ignoring closed channel %v", scid)
1✔
2632

1✔
2633
                // If this is an announcement from us, we'll just ignore it.
1✔
2634
                if !nMsg.isRemote {
1✔
2635
                        nMsg.err <- err
×
2636
                        return nil, false
×
2637
                }
×
2638

2639
                log.Warnf("Increasing ban score for peer=%v due to outdated "+
1✔
2640
                        "channel announcement for channel %v", nMsg.peer, scid)
1✔
2641

1✔
2642
                // Increment the peer's ban score if they are sending closed
1✔
2643
                // channel announcements.
1✔
2644
                dcErr := d.handleBadPeer(nMsg.peer)
1✔
2645
                if dcErr != nil {
1✔
2646
                        err = dcErr
×
2647
                }
×
2648

2649
                nMsg.err <- err
1✔
2650

1✔
2651
                return nil, false
1✔
2652
        }
2653

2654
        // If this is a remote channel announcement, then we'll validate all
2655
        // the signatures within the proof as it should be well formed.
2656
        var proof *models.ChannelAuthProof
231✔
2657
        if nMsg.isRemote {
448✔
2658
                err := netann.ValidateChannelAnn(ann, d.fetchPKScript)
217✔
2659
                if err != nil {
217✔
2660
                        err := fmt.Errorf("unable to validate announcement: "+
×
2661
                                "%v", err)
×
2662

×
2663
                        key := newRejectCacheKey(
×
2664
                                scid.ToUint64(),
×
2665
                                sourceToPub(nMsg.source),
×
2666
                        )
×
2667
                        _, _ = d.recentRejects.Put(key, &cachedReject{})
×
2668

×
2669
                        log.Error(err)
×
2670
                        nMsg.err <- err
×
2671
                        return nil, false
×
2672
                }
×
2673

2674
                // If the proof checks out, then we'll save the proof itself to
2675
                // the database so we can fetch it later when gossiping with
2676
                // other nodes.
2677
                proof = &models.ChannelAuthProof{
217✔
2678
                        NodeSig1Bytes:    ann.NodeSig1.ToSignatureBytes(),
217✔
2679
                        NodeSig2Bytes:    ann.NodeSig2.ToSignatureBytes(),
217✔
2680
                        BitcoinSig1Bytes: ann.BitcoinSig1.ToSignatureBytes(),
217✔
2681
                        BitcoinSig2Bytes: ann.BitcoinSig2.ToSignatureBytes(),
217✔
2682
                }
217✔
2683
        }
2684

2685
        // With the proof validated (if necessary), we can now store it within
2686
        // the database for our path finding and syncing needs.
2687
        edge := &models.ChannelEdgeInfo{
231✔
2688
                ChannelID:        scid.ToUint64(),
231✔
2689
                ChainHash:        ann.ChainHash,
231✔
2690
                NodeKey1Bytes:    ann.NodeID1,
231✔
2691
                NodeKey2Bytes:    ann.NodeID2,
231✔
2692
                BitcoinKey1Bytes: ann.BitcoinKey1,
231✔
2693
                BitcoinKey2Bytes: ann.BitcoinKey2,
231✔
2694
                AuthProof:        proof,
231✔
2695
                Features: lnwire.NewFeatureVector(
231✔
2696
                        ann.Features, lnwire.Features,
231✔
2697
                ),
231✔
2698
                ExtraOpaqueData: ann.ExtraOpaqueData,
231✔
2699
        }
231✔
2700

231✔
2701
        // If there were any optional message fields provided, we'll include
231✔
2702
        // them in its serialized disk representation now.
231✔
2703
        var tapscriptRoot fn.Option[chainhash.Hash]
231✔
2704
        if nMsg.optionalMsgFields != nil {
245✔
2705
                if nMsg.optionalMsgFields.capacity != nil {
15✔
2706
                        edge.Capacity = *nMsg.optionalMsgFields.capacity
1✔
2707
                }
1✔
2708
                if nMsg.optionalMsgFields.channelPoint != nil {
18✔
2709
                        cp := *nMsg.optionalMsgFields.channelPoint
4✔
2710
                        edge.ChannelPoint = cp
4✔
2711
                }
4✔
2712

2713
                // Optional tapscript root for custom channels.
2714
                tapscriptRoot = nMsg.optionalMsgFields.tapscriptRoot
14✔
2715
        }
2716

2717
        // Before we start validation or add the edge to the database, we obtain
2718
        // the mutex for this channel ID. We do this to ensure no other
2719
        // goroutine has read the database and is now making decisions based on
2720
        // this DB state, before it writes to the DB. It also ensures that we
2721
        // don't perform the expensive validation check on the same channel
2722
        // announcement at the same time.
2723
        d.channelMtx.Lock(scid.ToUint64())
231✔
2724

231✔
2725
        // If AssumeChannelValid is present, then we are unable to perform any
231✔
2726
        // of the expensive checks below, so we'll short-circuit our path
231✔
2727
        // straight to adding the edge to our graph. If the passed
231✔
2728
        // ShortChannelID is an alias, then we'll skip validation as it will
231✔
2729
        // not map to a legitimate tx. This is not a DoS vector as only we can
231✔
2730
        // add an alias ChannelAnnouncement from the gossiper.
231✔
2731
        if !(d.cfg.AssumeChannelValid || d.cfg.IsAlias(scid)) {
460✔
2732
                op, capacity, script, err := d.validateFundingTransaction(
229✔
2733
                        ctx, ann, tapscriptRoot,
229✔
2734
                )
229✔
2735
                if err != nil {
433✔
2736
                        defer d.channelMtx.Unlock(scid.ToUint64())
204✔
2737

204✔
2738
                        switch {
204✔
2739
                        case errors.Is(err, ErrNoFundingTransaction),
2740
                                errors.Is(err, ErrInvalidFundingOutput):
202✔
2741

202✔
2742
                                key := newRejectCacheKey(
202✔
2743
                                        scid.ToUint64(),
202✔
2744
                                        sourceToPub(nMsg.source),
202✔
2745
                                )
202✔
2746
                                _, _ = d.recentRejects.Put(
202✔
2747
                                        key, &cachedReject{},
202✔
2748
                                )
202✔
2749

2750
                        case errors.Is(err, ErrChannelSpent):
2✔
2751
                                key := newRejectCacheKey(
2✔
2752
                                        scid.ToUint64(),
2✔
2753
                                        sourceToPub(nMsg.source),
2✔
2754
                                )
2✔
2755
                                _, _ = d.recentRejects.Put(key, &cachedReject{})
2✔
2756

2✔
2757
                                // Since this channel has already been closed,
2✔
2758
                                // we'll add it to the graph's closed channel
2✔
2759
                                // index such that we won't attempt to do
2✔
2760
                                // expensive validation checks on it again.
2✔
2761
                                // TODO: Populate the ScidCloser by using closed
2✔
2762
                                // channel notifications.
2✔
2763
                                dbErr := d.cfg.ScidCloser.PutClosedScid(scid)
2✔
2764
                                if dbErr != nil {
2✔
2765
                                        log.Errorf("failed to mark scid(%v) "+
×
2766
                                                "as closed: %v", scid, dbErr)
×
2767

×
2768
                                        nMsg.err <- dbErr
×
2769

×
2770
                                        return nil, false
×
2771
                                }
×
2772

2773
                        default:
×
2774
                                // Otherwise, this is just a regular rejected
×
2775
                                // edge. We won't increase the ban score for the
×
2776
                                // remote peer.
×
2777
                                key := newRejectCacheKey(
×
2778
                                        scid.ToUint64(),
×
2779
                                        sourceToPub(nMsg.source),
×
2780
                                )
×
2781
                                _, _ = d.recentRejects.Put(key, &cachedReject{})
×
2782

×
2783
                                nMsg.err <- err
×
2784

×
2785
                                return nil, false
×
2786
                        }
2787

2788
                        if !nMsg.isRemote {
204✔
2789
                                log.Errorf("failed to add edge for local "+
×
2790
                                        "channel: %v", err)
×
2791
                                nMsg.err <- err
×
2792

×
2793
                                return nil, false
×
2794
                        }
×
2795

2796
                        log.Warnf("Increasing ban score for peer=%v due to "+
204✔
2797
                                "invalid channel announcement for channel %v",
204✔
2798
                                nMsg.peer, scid)
204✔
2799

204✔
2800
                        // Increment the peer's ban score if they are sending
204✔
2801
                        // us invalid channel announcements.
204✔
2802
                        dcErr := d.handleBadPeer(nMsg.peer)
204✔
2803
                        if dcErr != nil {
204✔
2804
                                err = dcErr
×
2805
                        }
×
2806

2807
                        nMsg.err <- err
204✔
2808

204✔
2809
                        return nil, false
204✔
2810
                }
2811

2812
                edge.FundingScript = fn.Some(script)
25✔
2813

25✔
2814
                // TODO(roasbeef): this is a hack, needs to be removed after
25✔
2815
                //  commitment fees are dynamic.
25✔
2816
                edge.Capacity = capacity
25✔
2817
                edge.ChannelPoint = op
25✔
2818
        }
2819

2820
        log.Debugf("Adding edge for short_chan_id: %v", scid.ToUint64())
27✔
2821

27✔
2822
        // We will add the edge to the channel router. If the nodes present in
27✔
2823
        // this channel are not present in the database, a partial node will be
27✔
2824
        // added to represent each node while we wait for a node announcement.
27✔
2825
        err = d.cfg.Graph.AddEdge(ctx, edge, ops...)
27✔
2826
        if err != nil {
28✔
2827
                log.Debugf("Graph rejected edge for short_chan_id(%v): %v",
1✔
2828
                        scid.ToUint64(), err)
1✔
2829

1✔
2830
                defer d.channelMtx.Unlock(scid.ToUint64())
1✔
2831

1✔
2832
                // If the edge was rejected due to already being known, then it
1✔
2833
                // may be the case that this new message has a fresh channel
1✔
2834
                // proof, so we'll check.
1✔
2835
                if graph.IsError(err, graph.ErrIgnored) {
1✔
UNCOV
2836
                        // Attempt to process the rejected message to see if we
×
UNCOV
2837
                        // get any new announcements.
×
UNCOV
2838
                        anns, rErr := d.processRejectedEdge(ctx, ann, proof)
×
UNCOV
2839
                        if rErr != nil {
×
2840
                                key := newRejectCacheKey(
×
2841
                                        scid.ToUint64(),
×
2842
                                        sourceToPub(nMsg.source),
×
2843
                                )
×
2844
                                cr := &cachedReject{}
×
2845
                                _, _ = d.recentRejects.Put(key, cr)
×
2846

×
2847
                                nMsg.err <- rErr
×
2848

×
2849
                                return nil, false
×
2850
                        }
×
2851

UNCOV
2852
                        log.Debugf("Extracted %v announcements from rejected "+
×
UNCOV
2853
                                "msgs", len(anns))
×
UNCOV
2854

×
UNCOV
2855
                        // If while processing this rejected edge, we realized
×
UNCOV
2856
                        // there's a set of announcements we could extract,
×
UNCOV
2857
                        // then we'll return those directly.
×
UNCOV
2858
                        //
×
UNCOV
2859
                        // NOTE: since this is an ErrIgnored, we can return
×
UNCOV
2860
                        // true here to signal "allow" to its dependants.
×
UNCOV
2861
                        nMsg.err <- nil
×
UNCOV
2862

×
UNCOV
2863
                        return anns, true
×
2864
                }
2865

2866
                // Otherwise, this is just a regular rejected edge.
2867
                key := newRejectCacheKey(
1✔
2868
                        scid.ToUint64(),
1✔
2869
                        sourceToPub(nMsg.source),
1✔
2870
                )
1✔
2871
                _, _ = d.recentRejects.Put(key, &cachedReject{})
1✔
2872

1✔
2873
                if !nMsg.isRemote {
1✔
2874
                        log.Errorf("failed to add edge for local channel: %v",
×
2875
                                err)
×
2876
                        nMsg.err <- err
×
2877

×
2878
                        return nil, false
×
2879
                }
×
2880

2881
                shouldDc, dcErr := d.ShouldDisconnect(nMsg.peer.IdentityKey())
1✔
2882
                if dcErr != nil {
1✔
2883
                        log.Errorf("failed to check if we should disconnect "+
×
2884
                                "peer: %v", dcErr)
×
2885
                        nMsg.err <- dcErr
×
2886

×
2887
                        return nil, false
×
2888
                }
×
2889

2890
                if shouldDc {
1✔
2891
                        nMsg.peer.Disconnect(ErrPeerBanned)
×
2892
                }
×
2893

2894
                nMsg.err <- err
1✔
2895

1✔
2896
                return nil, false
1✔
2897
        }
2898

2899
        // If err is nil, release the lock immediately.
2900
        d.channelMtx.Unlock(scid.ToUint64())
26✔
2901

26✔
2902
        log.Debugf("Finish adding edge for short_chan_id: %v", scid.ToUint64())
26✔
2903

26✔
2904
        // If we earlier received any ChannelUpdates for this channel, we can
26✔
2905
        // now process them, as the channel is added to the graph.
26✔
2906
        var channelUpdates []*processedNetworkMsg
26✔
2907

26✔
2908
        earlyChanUpdates, err := d.prematureChannelUpdates.Get(scid.ToUint64())
26✔
2909
        if err == nil {
28✔
2910
                // There was actually an entry in the map, so we'll accumulate
2✔
2911
                // it. We don't worry about deletion, since it'll eventually
2✔
2912
                // fall out anyway.
2✔
2913
                chanMsgs := earlyChanUpdates
2✔
2914
                channelUpdates = append(channelUpdates, chanMsgs.msgs...)
2✔
2915
        }
2✔
2916

2917
        // Launch a new goroutine to handle each ChannelUpdate, this is to
2918
        // ensure we don't block here, as we can handle only one announcement
2919
        // at a time.
2920
        for _, cu := range channelUpdates {
28✔
2921
                // Skip if already processed.
2✔
2922
                if cu.processed {
2✔
UNCOV
2923
                        continue
×
2924
                }
2925

2926
                // Mark the ChannelUpdate as processed. This ensures that a
2927
                // subsequent announcement in the option-scid-alias case does
2928
                // not re-use an old ChannelUpdate.
2929
                cu.processed = true
2✔
2930

2✔
2931
                d.wg.Add(1)
2✔
2932
                go func(updMsg *networkMsg) {
4✔
2933
                        defer d.wg.Done()
2✔
2934

2✔
2935
                        switch msg := updMsg.msg.(type) {
2✔
2936
                        // Reprocess the message, making sure we return an
2937
                        // error to the original caller in case the gossiper
2938
                        // shuts down.
2939
                        case *lnwire.ChannelUpdate1:
2✔
2940
                                log.Debugf("Reprocessing ChannelUpdate for "+
2✔
2941
                                        "shortChanID=%v", scid.ToUint64())
2✔
2942

2✔
2943
                                select {
2✔
2944
                                case d.networkMsgs <- updMsg:
2✔
2945
                                case <-d.quit:
×
2946
                                        updMsg.err <- ErrGossiperShuttingDown
×
2947
                                }
2948

2949
                        // We don't expect any other message type than
2950
                        // ChannelUpdate to be in this cache.
2951
                        default:
×
2952
                                log.Errorf("Unsupported message type found "+
×
2953
                                        "among ChannelUpdates: %T", msg)
×
2954
                        }
2955
                }(cu.msg)
2956
        }
2957

2958
        // Channel announcement was successfully processed and now it might be
2959
        // broadcast to other connected nodes if it was an announcement with
2960
        // proof (remote).
2961
        var announcements []networkMsg
26✔
2962

26✔
2963
        if proof != nil {
38✔
2964
                announcements = append(announcements, networkMsg{
12✔
2965
                        peer:     nMsg.peer,
12✔
2966
                        isRemote: nMsg.isRemote,
12✔
2967
                        source:   nMsg.source,
12✔
2968
                        msg:      ann,
12✔
2969
                })
12✔
2970
        }
12✔
2971

2972
        nMsg.err <- nil
26✔
2973

26✔
2974
        log.Debugf("Processed ChannelAnnouncement1: peer=%v, short_chan_id=%v",
26✔
2975
                nMsg.peer, scid.ToUint64())
26✔
2976

26✔
2977
        return announcements, true
26✔
2978
}
2979

2980
// handleChanUpdate processes a new channel update.
2981
//
2982
//nolint:funlen
2983
func (d *AuthenticatedGossiper) handleChanUpdate(ctx context.Context,
2984
        nMsg *networkMsg, upd *lnwire.ChannelUpdate1,
2985
        ops []batch.SchedulerOption) ([]networkMsg, bool) {
61✔
2986

61✔
2987
        log.Debugf("Processing ChannelUpdate: peer=%v, short_chan_id=%v, ",
61✔
2988
                nMsg.peer, upd.ShortChannelID.ToUint64())
61✔
2989

61✔
2990
        chainHash := d.cfg.ChainParams.GenesisHash
61✔
2991

61✔
2992
        // We'll ignore any channel updates that target any chain other than
61✔
2993
        // the set of chains we know of.
61✔
2994
        if !bytes.Equal(upd.ChainHash[:], chainHash[:]) {
61✔
2995
                err := fmt.Errorf("ignoring ChannelUpdate from chain=%v, "+
×
2996
                        "gossiper on chain=%v", upd.ChainHash, chainHash)
×
2997
                log.Errorf(err.Error())
×
2998

×
2999
                key := newRejectCacheKey(
×
3000
                        upd.ShortChannelID.ToUint64(),
×
3001
                        sourceToPub(nMsg.source),
×
3002
                )
×
3003
                _, _ = d.recentRejects.Put(key, &cachedReject{})
×
3004

×
3005
                nMsg.err <- err
×
3006
                return nil, false
×
3007
        }
×
3008

3009
        blockHeight := upd.ShortChannelID.BlockHeight
61✔
3010
        shortChanID := upd.ShortChannelID.ToUint64()
61✔
3011

61✔
3012
        // If the advertised inclusionary block is beyond our knowledge of the
61✔
3013
        // chain tip, then we'll put the announcement in limbo to be fully
61✔
3014
        // verified once we advance forward in the chain. If the update has an
61✔
3015
        // alias SCID, we'll skip the isPremature check. This is necessary
61✔
3016
        // since aliases start at block height 16_000_000.
61✔
3017
        d.Lock()
61✔
3018
        if nMsg.isRemote && !d.cfg.IsAlias(upd.ShortChannelID) &&
61✔
3019
                d.isPremature(upd.ShortChannelID, 0, nMsg) {
61✔
UNCOV
3020

×
UNCOV
3021
                log.Warnf("Update announcement for short_chan_id(%v), is "+
×
UNCOV
3022
                        "premature: advertises height %v, only height %v is "+
×
UNCOV
3023
                        "known", shortChanID, blockHeight, d.bestHeight)
×
UNCOV
3024
                d.Unlock()
×
UNCOV
3025
                nMsg.err <- nil
×
UNCOV
3026
                return nil, false
×
UNCOV
3027
        }
×
3028
        d.Unlock()
61✔
3029

61✔
3030
        // Before we perform any of the expensive checks below, we'll check
61✔
3031
        // whether this update is stale or is for a zombie channel in order to
61✔
3032
        // quickly reject it.
61✔
3033
        timestamp := time.Unix(int64(upd.Timestamp), 0)
61✔
3034

61✔
3035
        // Fetch the SCID we should be using to lock the channelMtx and make
61✔
3036
        // graph queries with.
61✔
3037
        graphScid, err := d.cfg.FindBaseByAlias(upd.ShortChannelID)
61✔
3038
        if err != nil {
122✔
3039
                // Fallback and set the graphScid to the peer-provided SCID.
61✔
3040
                // This will occur for non-option-scid-alias channels and for
61✔
3041
                // public option-scid-alias channels after 6 confirmations.
61✔
3042
                // Once public option-scid-alias channels have 6 confs, we'll
61✔
3043
                // ignore ChannelUpdates with one of their aliases.
61✔
3044
                graphScid = upd.ShortChannelID
61✔
3045
        }
61✔
3046

3047
        // We make sure to obtain the mutex for this channel ID before we access
3048
        // the database. This ensures the state we read from the database has
3049
        // not changed between this point and when we call UpdateEdge() later.
3050
        d.channelMtx.Lock(graphScid.ToUint64())
61✔
3051
        defer d.channelMtx.Unlock(graphScid.ToUint64())
61✔
3052

61✔
3053
        if d.cfg.Graph.IsStaleEdgePolicy(
61✔
3054
                graphScid, timestamp, upd.ChannelFlags,
61✔
3055
        ) {
64✔
3056

3✔
3057
                log.Debugf("Ignored stale edge policy for short_chan_id(%v): "+
3✔
3058
                        "peer=%v, msg=%s, is_remote=%v", shortChanID,
3✔
3059
                        nMsg.peer, nMsg.msg.MsgType(), nMsg.isRemote,
3✔
3060
                )
3✔
3061

3✔
3062
                nMsg.err <- nil
3✔
3063
                return nil, true
3✔
3064
        }
3✔
3065

3066
        // Check that the ChanUpdate is not too far into the future, this could
3067
        // reveal some faulty implementation therefore we log an error.
3068
        if time.Until(timestamp) > graph.DefaultChannelPruneExpiry {
58✔
3069
                err := fmt.Errorf("skewed timestamp of edge policy, "+
×
3070
                        "timestamp too far in the future: %v", timestamp.Unix())
×
3071

×
3072
                // If this is a channel_update from us, we'll just ignore it.
×
3073
                if !nMsg.isRemote {
×
3074
                        nMsg.err <- err
×
3075
                        return nil, false
×
3076
                }
×
3077

3078
                log.Errorf("Increasing ban score for peer=%v due to bad "+
×
3079
                        "channel_update with short_chan_id(%v): timestamp(%v) "+
×
3080
                        "too far in the future", nMsg.peer, shortChanID,
×
3081
                        timestamp.Unix())
×
3082

×
3083
                // Increment the peer's ban score if they are skewed channel
×
3084
                // updates.
×
3085
                dcErr := d.handleBadPeer(nMsg.peer)
×
3086
                if dcErr != nil {
×
3087
                        err = dcErr
×
3088
                }
×
3089

3090
                nMsg.err <- err
×
3091

×
3092
                return nil, false
×
3093
        }
3094

3095
        // Get the node pub key as far since we don't have it in the channel
3096
        // update announcement message. We'll need this to properly verify the
3097
        // message's signature.
3098
        chanInfo, e1, e2, err := d.cfg.Graph.GetChannelByID(graphScid)
58✔
3099
        switch {
58✔
3100
        // No error, break.
3101
        case err == nil:
54✔
3102
                break
54✔
3103

3104
        case errors.Is(err, graphdb.ErrZombieEdge):
3✔
3105
                err = d.processZombieUpdate(ctx, chanInfo, graphScid, upd)
3✔
3106
                if err != nil {
5✔
3107
                        log.Debug(err)
2✔
3108
                        nMsg.err <- err
2✔
3109
                        return nil, false
2✔
3110
                }
2✔
3111

3112
                // We'll fallthrough to ensure we stash the update until we
3113
                // receive its corresponding ChannelAnnouncement. This is
3114
                // needed to ensure the edge exists in the graph before
3115
                // applying the update.
3116
                fallthrough
1✔
3117
        case errors.Is(err, graphdb.ErrGraphNotFound):
1✔
3118
                fallthrough
1✔
3119
        case errors.Is(err, graphdb.ErrGraphNoEdgesFound):
1✔
3120
                fallthrough
1✔
3121
        case errors.Is(err, graphdb.ErrEdgeNotFound):
2✔
3122
                // If the edge corresponding to this ChannelUpdate was not
2✔
3123
                // found in the graph, this might be a channel in the process
2✔
3124
                // of being opened, and we haven't processed our own
2✔
3125
                // ChannelAnnouncement yet, hence it is not not found in the
2✔
3126
                // graph. This usually gets resolved after the channel proofs
2✔
3127
                // are exchanged and the channel is broadcasted to the rest of
2✔
3128
                // the network, but in case this is a private channel this
2✔
3129
                // won't ever happen. This can also happen in the case of a
2✔
3130
                // zombie channel with a fresh update for which we don't have a
2✔
3131
                // ChannelAnnouncement for since we reject them. Because of
2✔
3132
                // this, we temporarily add it to a map, and reprocess it after
2✔
3133
                // our own ChannelAnnouncement has been processed.
2✔
3134
                //
2✔
3135
                // The shortChanID may be an alias, but it is fine to use here
2✔
3136
                // since we don't have an edge in the graph and if the peer is
2✔
3137
                // not buggy, we should be able to use it once the gossiper
2✔
3138
                // receives the local announcement.
2✔
3139
                pMsg := &processedNetworkMsg{msg: nMsg}
2✔
3140

2✔
3141
                earlyMsgs, err := d.prematureChannelUpdates.Get(shortChanID)
2✔
3142
                switch {
2✔
3143
                // Nothing in the cache yet, we can just directly insert this
3144
                // element.
3145
                case err == cache.ErrElementNotFound:
2✔
3146
                        _, _ = d.prematureChannelUpdates.Put(
2✔
3147
                                shortChanID, &cachedNetworkMsg{
2✔
3148
                                        msgs: []*processedNetworkMsg{pMsg},
2✔
3149
                                })
2✔
3150

3151
                // There's already something in the cache, so we'll combine the
3152
                // set of messages into a single value.
UNCOV
3153
                default:
×
UNCOV
3154
                        msgs := earlyMsgs.msgs
×
UNCOV
3155
                        msgs = append(msgs, pMsg)
×
UNCOV
3156
                        _, _ = d.prematureChannelUpdates.Put(
×
UNCOV
3157
                                shortChanID, &cachedNetworkMsg{
×
UNCOV
3158
                                        msgs: msgs,
×
UNCOV
3159
                                })
×
3160
                }
3161

3162
                log.Debugf("Got ChannelUpdate for edge not found in graph"+
2✔
3163
                        "(shortChanID=%v), saving for reprocessing later",
2✔
3164
                        shortChanID)
2✔
3165

2✔
3166
                // NOTE: We don't return anything on the error channel for this
2✔
3167
                // message, as we expect that will be done when this
2✔
3168
                // ChannelUpdate is later reprocessed. This might never happen
2✔
3169
                // if the corresponding ChannelAnnouncement is never received
2✔
3170
                // or the LRU cache is filled up and the entry is evicted.
2✔
3171
                return nil, false
2✔
3172

3173
        default:
×
3174
                err := fmt.Errorf("unable to validate channel update "+
×
3175
                        "short_chan_id=%v: %v", shortChanID, err)
×
3176
                log.Error(err)
×
3177
                nMsg.err <- err
×
3178

×
3179
                key := newRejectCacheKey(
×
3180
                        upd.ShortChannelID.ToUint64(),
×
3181
                        sourceToPub(nMsg.source),
×
3182
                )
×
3183
                _, _ = d.recentRejects.Put(key, &cachedReject{})
×
3184

×
3185
                return nil, false
×
3186
        }
3187

3188
        // The least-significant bit in the flag on the channel update
3189
        // announcement tells us "which" side of the channels directed edge is
3190
        // being updated.
3191
        var (
54✔
3192
                pubKey       *btcec.PublicKey
54✔
3193
                edgeToUpdate *models.ChannelEdgePolicy
54✔
3194
        )
54✔
3195
        direction := upd.ChannelFlags & lnwire.ChanUpdateDirection
54✔
3196
        switch direction {
54✔
3197
        case 0:
38✔
3198
                pubKey, _ = chanInfo.NodeKey1()
38✔
3199
                edgeToUpdate = e1
38✔
3200
        case 1:
16✔
3201
                pubKey, _ = chanInfo.NodeKey2()
16✔
3202
                edgeToUpdate = e2
16✔
3203
        }
3204

3205
        log.Debugf("Validating ChannelUpdate: channel=%v, for node=%x, has "+
54✔
3206
                "edge policy=%v", chanInfo.ChannelID,
54✔
3207
                pubKey.SerializeCompressed(), edgeToUpdate != nil)
54✔
3208

54✔
3209
        // Validate the channel announcement with the expected public key and
54✔
3210
        // channel capacity. In the case of an invalid channel update, we'll
54✔
3211
        // return an error to the caller and exit early.
54✔
3212
        err = netann.ValidateChannelUpdateAnn(pubKey, chanInfo.Capacity, upd)
54✔
3213
        if err != nil {
58✔
3214
                rErr := fmt.Errorf("unable to validate channel update "+
4✔
3215
                        "announcement for short_chan_id=%v: %v",
4✔
3216
                        lnutils.SpewLogClosure(upd.ShortChannelID), err)
4✔
3217

4✔
3218
                log.Error(rErr)
4✔
3219
                nMsg.err <- rErr
4✔
3220
                return nil, false
4✔
3221
        }
4✔
3222

3223
        // If we have a previous version of the edge being updated, we'll want
3224
        // to rate limit its updates to prevent spam throughout the network.
3225
        if nMsg.isRemote && edgeToUpdate != nil {
67✔
3226
                // If it's a keep-alive update, we'll only propagate one if
17✔
3227
                // it's been a day since the previous. This follows our own
17✔
3228
                // heuristic of sending keep-alive updates after the same
17✔
3229
                // duration (see retransmitStaleAnns).
17✔
3230
                timeSinceLastUpdate := timestamp.Sub(edgeToUpdate.LastUpdate)
17✔
3231
                if IsKeepAliveUpdate(upd, edgeToUpdate) {
19✔
3232
                        if timeSinceLastUpdate < d.cfg.RebroadcastInterval {
3✔
3233
                                log.Debugf("Ignoring keep alive update not "+
1✔
3234
                                        "within %v period for channel %v",
1✔
3235
                                        d.cfg.RebroadcastInterval, shortChanID)
1✔
3236
                                nMsg.err <- nil
1✔
3237
                                return nil, false
1✔
3238
                        }
1✔
3239
                } else {
15✔
3240
                        // If it's not, we'll allow an update per minute with a
15✔
3241
                        // maximum burst of 10. If we haven't seen an update
15✔
3242
                        // for this channel before, we'll need to initialize a
15✔
3243
                        // rate limiter for each direction.
15✔
3244
                        //
15✔
3245
                        // Since the edge exists in the graph, we'll create a
15✔
3246
                        // rate limiter for chanInfo.ChannelID rather then the
15✔
3247
                        // SCID the peer sent. This is because there may be
15✔
3248
                        // multiple aliases for a channel and we may otherwise
15✔
3249
                        // rate-limit only a single alias of the channel,
15✔
3250
                        // instead of the whole channel.
15✔
3251
                        baseScid := chanInfo.ChannelID
15✔
3252
                        d.Lock()
15✔
3253
                        rls, ok := d.chanUpdateRateLimiter[baseScid]
15✔
3254
                        if !ok {
17✔
3255
                                r := rate.Every(d.cfg.ChannelUpdateInterval)
2✔
3256
                                b := d.cfg.MaxChannelUpdateBurst
2✔
3257
                                rls = [2]*rate.Limiter{
2✔
3258
                                        rate.NewLimiter(r, b),
2✔
3259
                                        rate.NewLimiter(r, b),
2✔
3260
                                }
2✔
3261
                                d.chanUpdateRateLimiter[baseScid] = rls
2✔
3262
                        }
2✔
3263
                        d.Unlock()
15✔
3264

15✔
3265
                        if !rls[direction].Allow() {
21✔
3266
                                log.Debugf("Rate limiting update for channel "+
6✔
3267
                                        "%v from direction %x", shortChanID,
6✔
3268
                                        pubKey.SerializeCompressed())
6✔
3269
                                nMsg.err <- nil
6✔
3270
                                return nil, false
6✔
3271
                        }
6✔
3272
                }
3273
        }
3274

3275
        // We'll use chanInfo.ChannelID rather than the peer-supplied
3276
        // ShortChannelID in the ChannelUpdate to avoid the router having to
3277
        // lookup the stored SCID. If we're sending the update, we'll always
3278
        // use the SCID stored in the database rather than a potentially
3279
        // different alias. This might mean that SigBytes is incorrect as it
3280
        // signs a different SCID than the database SCID, but since there will
3281
        // only be a difference if AuthProof == nil, this is fine.
3282
        update := &models.ChannelEdgePolicy{
43✔
3283
                SigBytes:                  upd.Signature.ToSignatureBytes(),
43✔
3284
                ChannelID:                 chanInfo.ChannelID,
43✔
3285
                LastUpdate:                timestamp,
43✔
3286
                MessageFlags:              upd.MessageFlags,
43✔
3287
                ChannelFlags:              upd.ChannelFlags,
43✔
3288
                TimeLockDelta:             upd.TimeLockDelta,
43✔
3289
                MinHTLC:                   upd.HtlcMinimumMsat,
43✔
3290
                MaxHTLC:                   upd.HtlcMaximumMsat,
43✔
3291
                FeeBaseMSat:               lnwire.MilliSatoshi(upd.BaseFee),
43✔
3292
                FeeProportionalMillionths: lnwire.MilliSatoshi(upd.FeeRate),
43✔
3293
                InboundFee:                upd.InboundFee.ValOpt(),
43✔
3294
                ExtraOpaqueData:           upd.ExtraOpaqueData,
43✔
3295
        }
43✔
3296

43✔
3297
        if err := d.cfg.Graph.UpdateEdge(ctx, update, ops...); err != nil {
43✔
3298
                if graph.IsError(
×
3299
                        err, graph.ErrOutdated,
×
3300
                        graph.ErrIgnored,
×
3301
                ) {
×
3302

×
3303
                        log.Debugf("Update edge for short_chan_id(%v) got: %v",
×
3304
                                shortChanID, err)
×
3305
                } else {
×
3306
                        // Since we know the stored SCID in the graph, we'll
×
3307
                        // cache that SCID.
×
3308
                        key := newRejectCacheKey(
×
3309
                                chanInfo.ChannelID,
×
3310
                                sourceToPub(nMsg.source),
×
3311
                        )
×
3312
                        _, _ = d.recentRejects.Put(key, &cachedReject{})
×
3313

×
3314
                        log.Errorf("Update edge for short_chan_id(%v) got: %v",
×
3315
                                shortChanID, err)
×
3316
                }
×
3317

3318
                nMsg.err <- err
×
3319
                return nil, false
×
3320
        }
3321

3322
        // If this is a local ChannelUpdate without an AuthProof, it means it
3323
        // is an update to a channel that is not (yet) supposed to be announced
3324
        // to the greater network. However, our channel counter party will need
3325
        // to be given the update, so we'll try sending the update directly to
3326
        // the remote peer.
3327
        if !nMsg.isRemote && chanInfo.AuthProof == nil {
54✔
3328
                if nMsg.optionalMsgFields != nil {
22✔
3329
                        remoteAlias := nMsg.optionalMsgFields.remoteAlias
11✔
3330
                        if remoteAlias != nil {
11✔
UNCOV
3331
                                // The remoteAlias field was specified, meaning
×
UNCOV
3332
                                // that we should replace the SCID in the
×
UNCOV
3333
                                // update with the remote's alias. We'll also
×
UNCOV
3334
                                // need to re-sign the channel update. This is
×
UNCOV
3335
                                // required for option-scid-alias feature-bit
×
UNCOV
3336
                                // negotiated channels.
×
UNCOV
3337
                                upd.ShortChannelID = *remoteAlias
×
UNCOV
3338

×
UNCOV
3339
                                sig, err := d.cfg.SignAliasUpdate(upd)
×
UNCOV
3340
                                if err != nil {
×
3341
                                        log.Error(err)
×
3342
                                        nMsg.err <- err
×
3343
                                        return nil, false
×
3344
                                }
×
3345

UNCOV
3346
                                lnSig, err := lnwire.NewSigFromSignature(sig)
×
UNCOV
3347
                                if err != nil {
×
3348
                                        log.Error(err)
×
3349
                                        nMsg.err <- err
×
3350
                                        return nil, false
×
3351
                                }
×
3352

UNCOV
3353
                                upd.Signature = lnSig
×
3354
                        }
3355
                }
3356

3357
                // Get our peer's public key.
3358
                remotePubKey := remotePubFromChanInfo(
11✔
3359
                        chanInfo, upd.ChannelFlags,
11✔
3360
                )
11✔
3361

11✔
3362
                log.Debugf("The message %v has no AuthProof, sending the "+
11✔
3363
                        "update to remote peer %x", upd.MsgType(), remotePubKey)
11✔
3364

11✔
3365
                // Now we'll attempt to send the channel update message
11✔
3366
                // reliably to the remote peer in the background, so that we
11✔
3367
                // don't block if the peer happens to be offline at the moment.
11✔
3368
                err := d.reliableSender.sendMessage(ctx, upd, remotePubKey)
11✔
3369
                if err != nil {
11✔
3370
                        err := fmt.Errorf("unable to reliably send %v for "+
×
3371
                                "channel=%v to peer=%x: %v", upd.MsgType(),
×
3372
                                upd.ShortChannelID, remotePubKey, err)
×
3373
                        nMsg.err <- err
×
3374
                        return nil, false
×
3375
                }
×
3376
        }
3377

3378
        // Channel update announcement was successfully processed and now it
3379
        // can be broadcast to the rest of the network. However, we'll only
3380
        // broadcast the channel update announcement if it has an attached
3381
        // authentication proof. We also won't broadcast the update if it
3382
        // contains an alias because the network would reject this.
3383
        var announcements []networkMsg
43✔
3384
        if chanInfo.AuthProof != nil && !d.cfg.IsAlias(upd.ShortChannelID) {
66✔
3385
                announcements = append(announcements, networkMsg{
23✔
3386
                        peer:     nMsg.peer,
23✔
3387
                        source:   nMsg.source,
23✔
3388
                        isRemote: nMsg.isRemote,
23✔
3389
                        msg:      upd,
23✔
3390
                })
23✔
3391
        }
23✔
3392

3393
        nMsg.err <- nil
43✔
3394

43✔
3395
        log.Debugf("Processed ChannelUpdate: peer=%v, short_chan_id=%v, "+
43✔
3396
                "timestamp=%v", nMsg.peer, upd.ShortChannelID.ToUint64(),
43✔
3397
                timestamp)
43✔
3398
        return announcements, true
43✔
3399
}
3400

3401
// handleAnnSig processes a new announcement signatures message.
3402
//
3403
//nolint:funlen
3404
func (d *AuthenticatedGossiper) handleAnnSig(ctx context.Context,
3405
        nMsg *networkMsg, ann *lnwire.AnnounceSignatures1) ([]networkMsg,
3406
        bool) {
21✔
3407

21✔
3408
        needBlockHeight := ann.ShortChannelID.BlockHeight +
21✔
3409
                d.cfg.ProofMatureDelta
21✔
3410
        shortChanID := ann.ShortChannelID.ToUint64()
21✔
3411

21✔
3412
        prefix := "local"
21✔
3413
        if nMsg.isRemote {
32✔
3414
                prefix = "remote"
11✔
3415
        }
11✔
3416

3417
        log.Infof("Received new %v announcement signature for %v", prefix,
21✔
3418
                ann.ShortChannelID)
21✔
3419

21✔
3420
        // By the specification, channel announcement proofs should be sent
21✔
3421
        // after some number of confirmations after channel was registered in
21✔
3422
        // bitcoin blockchain. Therefore, we check if the proof is mature.
21✔
3423
        d.Lock()
21✔
3424
        premature := d.isPremature(
21✔
3425
                ann.ShortChannelID, d.cfg.ProofMatureDelta, nMsg,
21✔
3426
        )
21✔
3427
        if premature {
21✔
UNCOV
3428
                log.Warnf("Premature proof announcement, current block height"+
×
UNCOV
3429
                        "lower than needed: %v < %v", d.bestHeight,
×
UNCOV
3430
                        needBlockHeight)
×
UNCOV
3431
                d.Unlock()
×
UNCOV
3432
                nMsg.err <- nil
×
UNCOV
3433
                return nil, false
×
UNCOV
3434
        }
×
3435
        d.Unlock()
21✔
3436

21✔
3437
        // Ensure that we know of a channel with the target channel ID before
21✔
3438
        // proceeding further.
21✔
3439
        //
21✔
3440
        // We must acquire the mutex for this channel ID before getting the
21✔
3441
        // channel from the database, to ensure what we read does not change
21✔
3442
        // before we call AddProof() later.
21✔
3443
        d.channelMtx.Lock(ann.ShortChannelID.ToUint64())
21✔
3444
        defer d.channelMtx.Unlock(ann.ShortChannelID.ToUint64())
21✔
3445

21✔
3446
        chanInfo, e1, e2, err := d.cfg.Graph.GetChannelByID(
21✔
3447
                ann.ShortChannelID,
21✔
3448
        )
21✔
3449
        if err != nil {
22✔
3450
                _, err = d.cfg.FindChannel(nMsg.source, ann.ChannelID)
1✔
3451
                if err != nil {
1✔
UNCOV
3452
                        err := fmt.Errorf("unable to store the proof for "+
×
UNCOV
3453
                                "short_chan_id=%v: %v", shortChanID, err)
×
UNCOV
3454
                        log.Error(err)
×
UNCOV
3455
                        nMsg.err <- err
×
UNCOV
3456

×
UNCOV
3457
                        return nil, false
×
UNCOV
3458
                }
×
3459

3460
                proof := channeldb.NewWaitingProof(nMsg.isRemote, ann)
1✔
3461
                err := d.cfg.WaitingProofStore.Add(proof)
1✔
3462
                if err != nil {
1✔
3463
                        err := fmt.Errorf("unable to store the proof for "+
×
3464
                                "short_chan_id=%v: %v", shortChanID, err)
×
3465
                        log.Error(err)
×
3466
                        nMsg.err <- err
×
3467
                        return nil, false
×
3468
                }
×
3469

3470
                log.Infof("Orphan %v proof announcement with short_chan_id=%v"+
1✔
3471
                        ", adding to waiting batch", prefix, shortChanID)
1✔
3472
                nMsg.err <- nil
1✔
3473
                return nil, false
1✔
3474
        }
3475

3476
        nodeID := nMsg.source.SerializeCompressed()
20✔
3477
        isFirstNode := bytes.Equal(nodeID, chanInfo.NodeKey1Bytes[:])
20✔
3478
        isSecondNode := bytes.Equal(nodeID, chanInfo.NodeKey2Bytes[:])
20✔
3479

20✔
3480
        // Ensure that channel that was retrieved belongs to the peer which
20✔
3481
        // sent the proof announcement.
20✔
3482
        if !(isFirstNode || isSecondNode) {
20✔
3483
                err := fmt.Errorf("channel that was received doesn't belong "+
×
3484
                        "to the peer which sent the proof, short_chan_id=%v",
×
3485
                        shortChanID)
×
3486
                log.Error(err)
×
3487
                nMsg.err <- err
×
3488
                return nil, false
×
3489
        }
×
3490

3491
        // If proof was sent by a local sub-system, then we'll send the
3492
        // announcement signature to the remote node so they can also
3493
        // reconstruct the full channel announcement.
3494
        if !nMsg.isRemote {
30✔
3495
                var remotePubKey [33]byte
10✔
3496
                if isFirstNode {
20✔
3497
                        remotePubKey = chanInfo.NodeKey2Bytes
10✔
3498
                } else {
10✔
UNCOV
3499
                        remotePubKey = chanInfo.NodeKey1Bytes
×
UNCOV
3500
                }
×
3501

3502
                // Since the remote peer might not be online we'll call a
3503
                // method that will attempt to deliver the proof when it comes
3504
                // online.
3505
                err := d.reliableSender.sendMessage(ctx, ann, remotePubKey)
10✔
3506
                if err != nil {
10✔
3507
                        err := fmt.Errorf("unable to reliably send %v for "+
×
3508
                                "channel=%v to peer=%x: %v", ann.MsgType(),
×
3509
                                ann.ShortChannelID, remotePubKey, err)
×
3510
                        nMsg.err <- err
×
3511
                        return nil, false
×
3512
                }
×
3513
        }
3514

3515
        // Check if we already have the full proof for this channel.
3516
        if chanInfo.AuthProof != nil {
21✔
3517
                // If we already have the fully assembled proof, then the peer
1✔
3518
                // sending us their proof has probably not received our local
1✔
3519
                // proof yet. So be kind and send them the full proof.
1✔
3520
                if nMsg.isRemote {
2✔
3521
                        peerID := nMsg.source.SerializeCompressed()
1✔
3522
                        log.Debugf("Got AnnounceSignatures for channel with " +
1✔
3523
                                "full proof.")
1✔
3524

1✔
3525
                        d.wg.Add(1)
1✔
3526
                        go func() {
2✔
3527
                                defer d.wg.Done()
1✔
3528

1✔
3529
                                log.Debugf("Received half proof for channel "+
1✔
3530
                                        "%v with existing full proof. Sending"+
1✔
3531
                                        " full proof to peer=%x",
1✔
3532
                                        ann.ChannelID, peerID)
1✔
3533

1✔
3534
                                ca, _, _, err := netann.CreateChanAnnouncement(
1✔
3535
                                        chanInfo.AuthProof, chanInfo, e1, e2,
1✔
3536
                                )
1✔
3537
                                if err != nil {
1✔
3538
                                        log.Errorf("unable to gen ann: %v",
×
3539
                                                err)
×
3540
                                        return
×
3541
                                }
×
3542

3543
                                err = nMsg.peer.SendMessage(false, ca)
1✔
3544
                                if err != nil {
1✔
3545
                                        log.Errorf("Failed sending full proof"+
×
3546
                                                " to peer=%x: %v", peerID, err)
×
3547
                                        return
×
3548
                                }
×
3549

3550
                                log.Debugf("Full proof sent to peer=%x for "+
1✔
3551
                                        "chanID=%v", peerID, ann.ChannelID)
1✔
3552
                        }()
3553
                }
3554

3555
                log.Debugf("Already have proof for channel with chanID=%v",
1✔
3556
                        ann.ChannelID)
1✔
3557
                nMsg.err <- nil
1✔
3558
                return nil, true
1✔
3559
        }
3560

3561
        // Check that we received the opposite proof. If so, then we're now
3562
        // able to construct the full proof, and create the channel
3563
        // announcement. If we didn't receive the opposite half of the proof
3564
        // then we should store this one, and wait for the opposite to be
3565
        // received.
3566
        proof := channeldb.NewWaitingProof(nMsg.isRemote, ann)
19✔
3567
        oppProof, err := d.cfg.WaitingProofStore.Get(proof.OppositeKey())
19✔
3568
        if err != nil && err != channeldb.ErrWaitingProofNotFound {
19✔
3569
                err := fmt.Errorf("unable to get the opposite proof for "+
×
3570
                        "short_chan_id=%v: %v", shortChanID, err)
×
3571
                log.Error(err)
×
3572
                nMsg.err <- err
×
3573
                return nil, false
×
3574
        }
×
3575

3576
        if err == channeldb.ErrWaitingProofNotFound {
28✔
3577
                err := d.cfg.WaitingProofStore.Add(proof)
9✔
3578
                if err != nil {
9✔
3579
                        err := fmt.Errorf("unable to store the proof for "+
×
3580
                                "short_chan_id=%v: %v", shortChanID, err)
×
3581
                        log.Error(err)
×
3582
                        nMsg.err <- err
×
3583
                        return nil, false
×
3584
                }
×
3585

3586
                log.Infof("1/2 of channel ann proof received for "+
9✔
3587
                        "short_chan_id=%v, waiting for other half",
9✔
3588
                        shortChanID)
9✔
3589

9✔
3590
                nMsg.err <- nil
9✔
3591
                return nil, false
9✔
3592
        }
3593

3594
        // We now have both halves of the channel announcement proof, then
3595
        // we'll reconstruct the initial announcement so we can validate it
3596
        // shortly below.
3597
        var dbProof models.ChannelAuthProof
10✔
3598
        if isFirstNode {
11✔
3599
                dbProof.NodeSig1Bytes = ann.NodeSignature.ToSignatureBytes()
1✔
3600
                dbProof.NodeSig2Bytes = oppProof.NodeSignature.ToSignatureBytes()
1✔
3601
                dbProof.BitcoinSig1Bytes = ann.BitcoinSignature.ToSignatureBytes()
1✔
3602
                dbProof.BitcoinSig2Bytes = oppProof.BitcoinSignature.ToSignatureBytes()
1✔
3603
        } else {
10✔
3604
                dbProof.NodeSig1Bytes = oppProof.NodeSignature.ToSignatureBytes()
9✔
3605
                dbProof.NodeSig2Bytes = ann.NodeSignature.ToSignatureBytes()
9✔
3606
                dbProof.BitcoinSig1Bytes = oppProof.BitcoinSignature.ToSignatureBytes()
9✔
3607
                dbProof.BitcoinSig2Bytes = ann.BitcoinSignature.ToSignatureBytes()
9✔
3608
        }
9✔
3609

3610
        chanAnn, e1Ann, e2Ann, err := netann.CreateChanAnnouncement(
10✔
3611
                &dbProof, chanInfo, e1, e2,
10✔
3612
        )
10✔
3613
        if err != nil {
10✔
3614
                log.Error(err)
×
3615
                nMsg.err <- err
×
3616
                return nil, false
×
3617
        }
×
3618

3619
        // With all the necessary components assembled validate the full
3620
        // channel announcement proof.
3621
        err = netann.ValidateChannelAnn(chanAnn, d.fetchPKScript)
10✔
3622
        if err != nil {
10✔
3623
                err := fmt.Errorf("channel announcement proof for "+
×
3624
                        "short_chan_id=%v isn't valid: %v", shortChanID, err)
×
3625

×
3626
                log.Error(err)
×
3627
                nMsg.err <- err
×
3628
                return nil, false
×
3629
        }
×
3630

3631
        // If the channel was returned by the router it means that existence of
3632
        // funding point and inclusion of nodes bitcoin keys in it already
3633
        // checked by the router. In this stage we should check that node keys
3634
        // attest to the bitcoin keys by validating the signatures of
3635
        // announcement. If proof is valid then we'll populate the channel edge
3636
        // with it, so we can announce it on peer connect.
3637
        err = d.cfg.Graph.AddProof(ann.ShortChannelID, &dbProof)
10✔
3638
        if err != nil {
10✔
3639
                err := fmt.Errorf("unable add proof to the channel chanID=%v:"+
×
3640
                        " %v", ann.ChannelID, err)
×
3641
                log.Error(err)
×
3642
                nMsg.err <- err
×
3643
                return nil, false
×
3644
        }
×
3645

3646
        err = d.cfg.WaitingProofStore.Remove(proof.OppositeKey())
10✔
3647
        if err != nil {
10✔
3648
                err := fmt.Errorf("unable to remove opposite proof for the "+
×
3649
                        "channel with chanID=%v: %v", ann.ChannelID, err)
×
3650
                log.Error(err)
×
3651
                nMsg.err <- err
×
3652
                return nil, false
×
3653
        }
×
3654

3655
        // Proof was successfully created and now can announce the channel to
3656
        // the remain network.
3657
        log.Infof("Fully valid channel proof for short_chan_id=%v constructed"+
10✔
3658
                ", adding to next ann batch", shortChanID)
10✔
3659

10✔
3660
        // Assemble the necessary announcements to add to the next broadcasting
10✔
3661
        // batch.
10✔
3662
        var announcements []networkMsg
10✔
3663
        announcements = append(announcements, networkMsg{
10✔
3664
                peer:   nMsg.peer,
10✔
3665
                source: nMsg.source,
10✔
3666
                msg:    chanAnn,
10✔
3667
        })
10✔
3668
        if src, err := chanInfo.NodeKey1(); err == nil && e1Ann != nil {
19✔
3669
                announcements = append(announcements, networkMsg{
9✔
3670
                        peer:   nMsg.peer,
9✔
3671
                        source: src,
9✔
3672
                        msg:    e1Ann,
9✔
3673
                })
9✔
3674
        }
9✔
3675
        if src, err := chanInfo.NodeKey2(); err == nil && e2Ann != nil {
18✔
3676
                announcements = append(announcements, networkMsg{
8✔
3677
                        peer:   nMsg.peer,
8✔
3678
                        source: src,
8✔
3679
                        msg:    e2Ann,
8✔
3680
                })
8✔
3681
        }
8✔
3682

3683
        // We'll also send along the node announcements for each channel
3684
        // participant if we know of them. To ensure our node announcement
3685
        // propagates to our channel counterparty, we'll set the source for
3686
        // each announcement to the node it belongs to, otherwise we won't send
3687
        // it since the source gets skipped. This isn't necessary for channel
3688
        // updates and announcement signatures since we send those directly to
3689
        // our channel counterparty through the gossiper's reliable sender.
3690
        node1Ann, err := d.fetchNodeAnn(ctx, chanInfo.NodeKey1Bytes)
10✔
3691
        if err != nil {
12✔
3692
                log.Debugf("Unable to fetch node announcement for %x: %v",
2✔
3693
                        chanInfo.NodeKey1Bytes, err)
2✔
3694
        } else {
10✔
3695
                if nodeKey1, err := chanInfo.NodeKey1(); err == nil {
16✔
3696
                        announcements = append(announcements, networkMsg{
8✔
3697
                                peer:   nMsg.peer,
8✔
3698
                                source: nodeKey1,
8✔
3699
                                msg:    node1Ann,
8✔
3700
                        })
8✔
3701
                }
8✔
3702
        }
3703

3704
        node2Ann, err := d.fetchNodeAnn(ctx, chanInfo.NodeKey2Bytes)
10✔
3705
        if err != nil {
14✔
3706
                log.Debugf("Unable to fetch node announcement for %x: %v",
4✔
3707
                        chanInfo.NodeKey2Bytes, err)
4✔
3708
        } else {
10✔
3709
                if nodeKey2, err := chanInfo.NodeKey2(); err == nil {
12✔
3710
                        announcements = append(announcements, networkMsg{
6✔
3711
                                peer:   nMsg.peer,
6✔
3712
                                source: nodeKey2,
6✔
3713
                                msg:    node2Ann,
6✔
3714
                        })
6✔
3715
                }
6✔
3716
        }
3717

3718
        nMsg.err <- nil
10✔
3719
        return announcements, true
10✔
3720
}
3721

3722
// isBanned returns true if the peer identified by pubkey is banned for sending
3723
// invalid channel announcements.
3724
func (d *AuthenticatedGossiper) isBanned(pubkey [33]byte) bool {
208✔
3725
        return d.banman.isBanned(pubkey)
208✔
3726
}
208✔
3727

3728
// ShouldDisconnect returns true if we should disconnect the peer identified by
3729
// pubkey.
3730
func (d *AuthenticatedGossiper) ShouldDisconnect(pubkey *btcec.PublicKey) (
3731
        bool, error) {
206✔
3732

206✔
3733
        pubkeySer := pubkey.SerializeCompressed()
206✔
3734

206✔
3735
        var pubkeyBytes [33]byte
206✔
3736
        copy(pubkeyBytes[:], pubkeySer)
206✔
3737

206✔
3738
        // If the public key is banned, check whether or not this is a channel
206✔
3739
        // peer.
206✔
3740
        if d.isBanned(pubkeyBytes) {
208✔
3741
                isChanPeer, err := d.cfg.ScidCloser.IsChannelPeer(pubkey)
2✔
3742
                if err != nil {
2✔
3743
                        return false, err
×
3744
                }
×
3745

3746
                // We should only disconnect non-channel peers.
3747
                if !isChanPeer {
3✔
3748
                        return true, nil
1✔
3749
                }
1✔
3750
        }
3751

3752
        return false, nil
205✔
3753
}
3754

3755
// validateFundingTransaction fetches the channel announcements claimed funding
3756
// transaction from chain to ensure that it exists, is not spent and matches
3757
// the channel announcement proof. The transaction's outpoint and value are
3758
// returned if we can glean them from the work done in this method.
3759
func (d *AuthenticatedGossiper) validateFundingTransaction(_ context.Context,
3760
        ann *lnwire.ChannelAnnouncement1,
3761
        tapscriptRoot fn.Option[chainhash.Hash]) (wire.OutPoint, btcutil.Amount,
3762
        []byte, error) {
229✔
3763

229✔
3764
        scid := ann.ShortChannelID
229✔
3765

229✔
3766
        // Before we can add the channel to the channel graph, we need to obtain
229✔
3767
        // the full funding outpoint that's encoded within the channel ID.
229✔
3768
        fundingTx, err := lnwallet.FetchFundingTxWrapper(
229✔
3769
                d.cfg.ChainIO, scid, d.quit,
229✔
3770
        )
229✔
3771
        if err != nil {
230✔
3772
                //nolint:ll
1✔
3773
                //
1✔
3774
                // In order to ensure we don't erroneously mark a channel as a
1✔
3775
                // zombie due to an RPC failure, we'll attempt to string match
1✔
3776
                // for the relevant errors.
1✔
3777
                //
1✔
3778
                // * btcd:
1✔
3779
                //    * https://github.com/btcsuite/btcd/blob/master/rpcserver.go#L1316
1✔
3780
                //    * https://github.com/btcsuite/btcd/blob/master/rpcserver.go#L1086
1✔
3781
                // * bitcoind:
1✔
3782
                //    * https://github.com/bitcoin/bitcoin/blob/7fcf53f7b4524572d1d0c9a5fdc388e87eb02416/src/rpc/blockchain.cpp#L770
1✔
3783
                //     * https://github.com/bitcoin/bitcoin/blob/7fcf53f7b4524572d1d0c9a5fdc388e87eb02416/src/rpc/blockchain.cpp#L954
1✔
3784
                switch {
1✔
3785
                case strings.Contains(err.Error(), "not found"):
1✔
3786
                        fallthrough
1✔
3787

3788
                case strings.Contains(err.Error(), "out of range"):
1✔
3789
                        // If the funding transaction isn't found at all, then
1✔
3790
                        // we'll mark the edge itself as a zombie so we don't
1✔
3791
                        // continue to request it. We use the "zero key" for
1✔
3792
                        // both node pubkeys so this edge can't be resurrected.
1✔
3793
                        zErr := d.cfg.Graph.MarkZombieEdge(scid.ToUint64())
1✔
3794
                        if zErr != nil {
1✔
3795
                                return wire.OutPoint{}, 0, nil, zErr
×
3796
                        }
×
3797

3798
                default:
×
3799
                }
3800

3801
                return wire.OutPoint{}, 0, nil, fmt.Errorf("%w: %w",
1✔
3802
                        ErrNoFundingTransaction, err)
1✔
3803
        }
3804

3805
        // Recreate witness output to be sure that declared in channel edge
3806
        // bitcoin keys and channel value corresponds to the reality.
3807
        fundingPkScript, err := makeFundingScript(
228✔
3808
                ann.BitcoinKey1[:], ann.BitcoinKey2[:], ann.Features,
228✔
3809
                tapscriptRoot,
228✔
3810
        )
228✔
3811
        if err != nil {
228✔
3812
                return wire.OutPoint{}, 0, nil, err
×
3813
        }
×
3814

3815
        // Next we'll validate that this channel is actually well formed. If
3816
        // this check fails, then this channel either doesn't exist, or isn't
3817
        // the one that was meant to be created according to the passed channel
3818
        // proofs.
3819
        fundingPoint, err := chanvalidate.Validate(
228✔
3820
                &chanvalidate.Context{
228✔
3821
                        Locator: &chanvalidate.ShortChanIDChanLocator{
228✔
3822
                                ID: scid,
228✔
3823
                        },
228✔
3824
                        MultiSigPkScript: fundingPkScript,
228✔
3825
                        FundingTx:        fundingTx,
228✔
3826
                },
228✔
3827
        )
228✔
3828
        if err != nil {
429✔
3829
                // Mark the edge as a zombie so we won't try to re-validate it
201✔
3830
                // on start up.
201✔
3831
                zErr := d.cfg.Graph.MarkZombieEdge(scid.ToUint64())
201✔
3832
                if zErr != nil {
201✔
3833
                        return wire.OutPoint{}, 0, nil, zErr
×
3834
                }
×
3835

3836
                return wire.OutPoint{}, 0, nil, fmt.Errorf("%w: %w",
201✔
3837
                        ErrInvalidFundingOutput, err)
201✔
3838
        }
3839

3840
        // Now that we have the funding outpoint of the channel, ensure
3841
        // that it hasn't yet been spent. If so, then this channel has
3842
        // been closed so we'll ignore it.
3843
        chanUtxo, err := d.cfg.ChainIO.GetUtxo(
27✔
3844
                fundingPoint, fundingPkScript, scid.BlockHeight, d.quit,
27✔
3845
        )
27✔
3846
        if err != nil {
29✔
3847
                if errors.Is(err, btcwallet.ErrOutputSpent) {
4✔
3848
                        zErr := d.cfg.Graph.MarkZombieEdge(scid.ToUint64())
2✔
3849
                        if zErr != nil {
2✔
3850
                                return wire.OutPoint{}, 0, nil, zErr
×
3851
                        }
×
3852
                }
3853

3854
                return wire.OutPoint{}, 0, nil, fmt.Errorf("%w: unable to "+
2✔
3855
                        "fetch utxo for chan_id=%v, chan_point=%v: %w",
2✔
3856
                        ErrChannelSpent, scid.ToUint64(), fundingPoint, err)
2✔
3857
        }
3858

3859
        return *fundingPoint, btcutil.Amount(chanUtxo.Value), fundingPkScript,
25✔
3860
                nil
25✔
3861
}
3862

3863
// handleBadPeer takes a misbehaving peer and increases its ban score. Once
3864
// increased, it will disconnect the peer if its ban score has reached
3865
// `banThreshold` and it doesn't have a channel with us.
3866
func (d *AuthenticatedGossiper) handleBadPeer(peer lnpeer.Peer) error {
205✔
3867
        // Increment the peer's ban score for misbehavior.
205✔
3868
        d.banman.incrementBanScore(peer.PubKey())
205✔
3869

205✔
3870
        // If the peer is banned and not a channel peer, we'll disconnect them.
205✔
3871
        shouldDc, dcErr := d.ShouldDisconnect(peer.IdentityKey())
205✔
3872
        if dcErr != nil {
205✔
3873
                log.Errorf("failed to check if we should disconnect peer: %v",
×
3874
                        dcErr)
×
3875

×
3876
                return dcErr
×
3877
        }
×
3878

3879
        if shouldDc {
206✔
3880
                peer.Disconnect(ErrPeerBanned)
1✔
3881
        }
1✔
3882

3883
        return nil
205✔
3884
}
3885

3886
// makeFundingScript is used to make the funding script for both segwit v0 and
3887
// segwit v1 (taproot) channels.
3888
func makeFundingScript(bitcoinKey1, bitcoinKey2 []byte,
3889
        features *lnwire.RawFeatureVector,
3890
        tapscriptRoot fn.Option[chainhash.Hash]) ([]byte, error) {
228✔
3891

228✔
3892
        legacyFundingScript := func() ([]byte, error) {
456✔
3893
                witnessScript, err := input.GenMultiSigScript(
228✔
3894
                        bitcoinKey1, bitcoinKey2,
228✔
3895
                )
228✔
3896
                if err != nil {
228✔
3897
                        return nil, err
×
3898
                }
×
3899
                pkScript, err := input.WitnessScriptHash(witnessScript)
228✔
3900
                if err != nil {
228✔
3901
                        return nil, err
×
3902
                }
×
3903

3904
                return pkScript, nil
228✔
3905
        }
3906

3907
        if features.IsEmpty() {
456✔
3908
                return legacyFundingScript()
228✔
3909
        }
228✔
3910

UNCOV
3911
        chanFeatureBits := lnwire.NewFeatureVector(features, lnwire.Features)
×
UNCOV
3912
        if chanFeatureBits.HasFeature(
×
UNCOV
3913
                lnwire.SimpleTaprootChannelsOptionalStaging,
×
UNCOV
3914
        ) {
×
UNCOV
3915

×
UNCOV
3916
                pubKey1, err := btcec.ParsePubKey(bitcoinKey1)
×
UNCOV
3917
                if err != nil {
×
3918
                        return nil, err
×
3919
                }
×
UNCOV
3920
                pubKey2, err := btcec.ParsePubKey(bitcoinKey2)
×
UNCOV
3921
                if err != nil {
×
3922
                        return nil, err
×
3923
                }
×
3924

UNCOV
3925
                fundingScript, _, err := input.GenTaprootFundingScript(
×
UNCOV
3926
                        pubKey1, pubKey2, 0, tapscriptRoot,
×
UNCOV
3927
                )
×
UNCOV
3928
                if err != nil {
×
3929
                        return nil, err
×
3930
                }
×
3931

3932
                // TODO(roasbeef): add tapscript root to gossip v1.5
3933

UNCOV
3934
                return fundingScript, nil
×
3935
        }
3936

3937
        return legacyFundingScript()
×
3938
}
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