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

lightningnetwork / lnd / 16152866413

08 Jul 2025 07:44PM UTC coverage: 57.72% (-9.8%) from 67.503%
16152866413

Pull #10015

github

web-flow
Merge f6affb695 into 47dce0894
Pull Request #10015: graph/db: add zombie channels cleanup routine

32 of 63 new or added lines in 2 files covered. (50.79%)

28432 existing lines in 455 files now uncovered.

98528 of 170699 relevant lines covered (57.72%)

1.79 hits per line

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

74.69
/accessman.go
1
package lnd
2

3
import (
4
        "context"
5
        "fmt"
6
        "maps"
7
        "sync"
8

9
        "github.com/btcsuite/btcd/btcec/v2"
10
        "github.com/btcsuite/btclog/v2"
11
        "github.com/lightningnetwork/lnd/channeldb"
12
        "github.com/lightningnetwork/lnd/lnutils"
13
)
14

15
// accessMan is responsible for managing the server's access permissions.
16
type accessMan struct {
17
        cfg *accessManConfig
18

19
        // banScoreMtx is used for the server's ban tracking. If the server
20
        // mutex is also going to be locked, ensure that this is locked after
21
        // the server mutex.
22
        banScoreMtx sync.RWMutex
23

24
        // peerChanInfo is a mapping from remote public key to {bool, uint64}
25
        // where the bool indicates that we have an open/closed channel with the
26
        // peer and where the uint64 indicates the number of pending-open
27
        // channels we currently have with them. This mapping will be used to
28
        // determine access permissions for the peer. The map key is the
29
        // string-version of the serialized public key.
30
        //
31
        // NOTE: This MUST be accessed with the banScoreMtx held.
32
        peerChanInfo map[string]channeldb.ChanCount
33

34
        // peerScores stores each connected peer's access status. The map key
35
        // is the string-version of the serialized public key.
36
        //
37
        // NOTE: This MUST be accessed with the banScoreMtx held.
38
        //
39
        // TODO(yy): unify `peerScores` and `peerChanInfo` - there's no need to
40
        // create two maps tracking essentially the same info. `numRestricted`
41
        // can also be derived from `peerChanInfo`.
42
        peerScores map[string]peerSlotStatus
43

44
        // numRestricted tracks the number of peers with restricted access in
45
        // peerScores. This MUST be accessed with the banScoreMtx held.
46
        numRestricted int64
47
}
48

49
type accessManConfig struct {
50
        // initAccessPerms checks the channeldb for initial access permissions
51
        // and then populates the peerChanInfo and peerScores maps.
52
        initAccessPerms func() (map[string]channeldb.ChanCount, error)
53

54
        // shouldDisconnect determines whether we should disconnect a peer or
55
        // not.
56
        shouldDisconnect func(*btcec.PublicKey) (bool, error)
57

58
        // maxRestrictedSlots is the number of restricted slots we'll allocate.
59
        maxRestrictedSlots int64
60
}
61

62
func newAccessMan(cfg *accessManConfig) (*accessMan, error) {
3✔
63
        a := &accessMan{
3✔
64
                cfg:          cfg,
3✔
65
                peerChanInfo: make(map[string]channeldb.ChanCount),
3✔
66
                peerScores:   make(map[string]peerSlotStatus),
3✔
67
        }
3✔
68

3✔
69
        counts, err := a.cfg.initAccessPerms()
3✔
70
        if err != nil {
3✔
71
                return nil, err
×
72
        }
×
73

74
        // We'll populate the server's peerChanInfo map with the counts fetched
75
        // via initAccessPerms. Also note that we haven't yet connected to the
76
        // peers.
77
        maps.Copy(a.peerChanInfo, counts)
3✔
78

3✔
79
        acsmLog.Info("Access Manager initialized")
3✔
80

3✔
81
        return a, nil
3✔
82
}
83

84
// hasPeer checks whether a given peer already exists in the internal maps.
85
func (a *accessMan) hasPeer(ctx context.Context,
86
        pub string) (peerAccessStatus, bool) {
3✔
87

3✔
88
        // Lock banScoreMtx for reading so that we can read the banning maps
3✔
89
        // below.
3✔
90
        a.banScoreMtx.RLock()
3✔
91
        defer a.banScoreMtx.RUnlock()
3✔
92

3✔
93
        count, found := a.peerChanInfo[pub]
3✔
94
        if found {
6✔
95
                if count.HasOpenOrClosedChan {
6✔
96
                        acsmLog.DebugS(ctx, "Peer has open/closed channel, "+
3✔
97
                                "assigning protected access")
3✔
98

3✔
99
                        // Exit early if the peer is no longer restricted.
3✔
100
                        return peerStatusProtected, true
3✔
101
                }
3✔
102

103
                if count.PendingOpenCount != 0 {
6✔
104
                        acsmLog.DebugS(ctx, "Peer has pending channel(s), "+
3✔
105
                                "assigning temporary access")
3✔
106

3✔
107
                        // Exit early if the peer is no longer restricted.
3✔
108
                        return peerStatusTemporary, true
3✔
109
                }
3✔
110

UNCOV
111
                return peerStatusRestricted, true
×
112
        }
113

114
        // Check if the peer is found in the scores map.
115
        status, found := a.peerScores[pub]
3✔
116
        if found {
3✔
UNCOV
117
                acsmLog.DebugS(ctx, "Peer already has access", "access",
×
UNCOV
118
                        status.state)
×
UNCOV
119

×
UNCOV
120
                return status.state, true
×
UNCOV
121
        }
×
122

123
        return peerStatusRestricted, false
3✔
124
}
125

126
// assignPeerPerms assigns a new peer its permissions. This does not track the
127
// access in the maps. This is intentional.
128
func (a *accessMan) assignPeerPerms(remotePub *btcec.PublicKey) (
129
        peerAccessStatus, error) {
3✔
130

3✔
131
        ctx := btclog.WithCtx(
3✔
132
                context.TODO(), lnutils.LogPubKey("peer", remotePub),
3✔
133
        )
3✔
134

3✔
135
        peerMapKey := string(remotePub.SerializeCompressed())
3✔
136

3✔
137
        acsmLog.DebugS(ctx, "Assigning permissions")
3✔
138

3✔
139
        // Default is restricted unless the below filters say otherwise.
3✔
140
        access, peerExist := a.hasPeer(ctx, peerMapKey)
3✔
141

3✔
142
        // Exit early if the peer is not restricted.
3✔
143
        if access != peerStatusRestricted {
6✔
144
                return access, nil
3✔
145
        }
3✔
146

147
        // If we are here, it means the peer has peerStatusRestricted.
148
        //
149
        // Check whether this peer is banned.
150
        shouldDisconnect, err := a.cfg.shouldDisconnect(remotePub)
3✔
151
        if err != nil {
3✔
152
                acsmLog.ErrorS(ctx, "Error checking disconnect status", err)
×
153

×
154
                // Access is restricted here.
×
155
                return access, err
×
156
        }
×
157

158
        if shouldDisconnect {
3✔
UNCOV
159
                acsmLog.WarnS(ctx, "Peer is banned, assigning restricted access",
×
UNCOV
160
                        ErrGossiperBan)
×
UNCOV
161

×
UNCOV
162
                // Access is restricted here.
×
UNCOV
163
                return access, ErrGossiperBan
×
UNCOV
164
        }
×
165

166
        // If we've reached this point and access hasn't changed from
167
        // restricted, then we need to check if we even have a slot for this
168
        // peer.
169
        acsmLog.DebugS(ctx, "Peer has no channels, assigning restricted access")
3✔
170

3✔
171
        // If this is an existing peer, there's no need to check for slot limit.
3✔
172
        if peerExist {
3✔
UNCOV
173
                acsmLog.DebugS(ctx, "Skipped slot check for existing peer")
×
UNCOV
174
                return access, nil
×
UNCOV
175
        }
×
176

177
        a.banScoreMtx.RLock()
3✔
178
        defer a.banScoreMtx.RUnlock()
3✔
179

3✔
180
        if a.numRestricted >= a.cfg.maxRestrictedSlots {
6✔
181
                acsmLog.WarnS(ctx, "No more restricted slots available, "+
3✔
182
                        "denying peer", ErrNoMoreRestrictedAccessSlots,
3✔
183
                        "num_restricted", a.numRestricted, "max_restricted",
3✔
184
                        a.cfg.maxRestrictedSlots)
3✔
185

3✔
186
                return access, ErrNoMoreRestrictedAccessSlots
3✔
187
        }
3✔
188

189
        return access, nil
3✔
190
}
191

192
// newPendingOpenChan is called after the pending-open channel has been
193
// committed to the database. This may transition a restricted-access peer to a
194
// temporary-access peer.
195
func (a *accessMan) newPendingOpenChan(remotePub *btcec.PublicKey) error {
3✔
196
        a.banScoreMtx.Lock()
3✔
197
        defer a.banScoreMtx.Unlock()
3✔
198

3✔
199
        ctx := btclog.WithCtx(
3✔
200
                context.TODO(), lnutils.LogPubKey("peer", remotePub),
3✔
201
        )
3✔
202

3✔
203
        acsmLog.DebugS(ctx, "Processing new pending open channel")
3✔
204

3✔
205
        peerMapKey := string(remotePub.SerializeCompressed())
3✔
206

3✔
207
        // Fetch the peer's access status from peerScores.
3✔
208
        status, found := a.peerScores[peerMapKey]
3✔
209
        if !found {
3✔
210
                acsmLog.ErrorS(ctx, "Peer score not found", ErrNoPeerScore)
×
211

×
212
                // If we didn't find the peer, we'll return an error.
×
213
                return ErrNoPeerScore
×
214
        }
×
215

216
        switch status.state {
3✔
217
        case peerStatusProtected:
3✔
218
                acsmLog.DebugS(ctx, "Peer already protected, no change")
3✔
219

3✔
220
                // If this peer's access status is protected, we don't need to
3✔
221
                // do anything.
3✔
222
                return nil
3✔
223

224
        case peerStatusTemporary:
3✔
225
                // If this peer's access status is temporary, we'll need to
3✔
226
                // update the peerChanInfo map. The peer's access status will
3✔
227
                // stay temporary.
3✔
228
                peerCount, found := a.peerChanInfo[peerMapKey]
3✔
229
                if !found {
3✔
230
                        // Error if we did not find any info in peerChanInfo.
×
231
                        acsmLog.ErrorS(ctx, "Pending peer info not found",
×
232
                                ErrNoPendingPeerInfo)
×
233

×
234
                        return ErrNoPendingPeerInfo
×
235
                }
×
236

237
                // Increment the pending channel amount.
238
                peerCount.PendingOpenCount += 1
3✔
239
                a.peerChanInfo[peerMapKey] = peerCount
3✔
240

3✔
241
                acsmLog.DebugS(ctx, "Peer is temporary, incremented "+
3✔
242
                        "pending count",
3✔
243
                        "pending_count", peerCount.PendingOpenCount)
3✔
244

245
        case peerStatusRestricted:
3✔
246
                // If the peer's access status is restricted, then we can
3✔
247
                // transition it to a temporary-access peer. We'll need to
3✔
248
                // update numRestricted and also peerScores. We'll also need to
3✔
249
                // update peerChanInfo.
3✔
250
                peerCount := channeldb.ChanCount{
3✔
251
                        HasOpenOrClosedChan: false,
3✔
252
                        PendingOpenCount:    1,
3✔
253
                }
3✔
254

3✔
255
                a.peerChanInfo[peerMapKey] = peerCount
3✔
256

3✔
257
                // A restricted-access slot has opened up.
3✔
258
                oldRestricted := a.numRestricted
3✔
259
                a.numRestricted -= 1
3✔
260

3✔
261
                a.peerScores[peerMapKey] = peerSlotStatus{
3✔
262
                        state: peerStatusTemporary,
3✔
263
                }
3✔
264

3✔
265
                acsmLog.InfoS(ctx, "Peer transitioned restricted -> "+
3✔
266
                        "temporary (pending open)",
3✔
267
                        "old_restricted", oldRestricted,
3✔
268
                        "new_restricted", a.numRestricted)
3✔
269

270
        default:
×
271
                // This should not be possible.
×
272
                err := fmt.Errorf("invalid peer access status %v for %x",
×
273
                        status.state, peerMapKey)
×
274
                acsmLog.ErrorS(ctx, "Invalid peer access status", err)
×
275

×
276
                return err
×
277
        }
278

279
        return nil
3✔
280
}
281

282
// newPendingCloseChan is called when a pending-open channel prematurely closes
283
// before the funding transaction has confirmed. This potentially demotes a
284
// temporary-access peer to a restricted-access peer. If no restricted-access
285
// slots are available, the peer will be disconnected.
286
func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error {
3✔
287
        a.banScoreMtx.Lock()
3✔
288
        defer a.banScoreMtx.Unlock()
3✔
289

3✔
290
        ctx := btclog.WithCtx(
3✔
291
                context.TODO(), lnutils.LogPubKey("peer", remotePub),
3✔
292
        )
3✔
293

3✔
294
        acsmLog.DebugS(ctx, "Processing pending channel close")
3✔
295

3✔
296
        peerMapKey := string(remotePub.SerializeCompressed())
3✔
297

3✔
298
        // Fetch the peer's access status from peerScores.
3✔
299
        status, found := a.peerScores[peerMapKey]
3✔
300
        if !found {
3✔
301
                acsmLog.ErrorS(ctx, "Peer score not found", ErrNoPeerScore)
×
302

×
303
                return ErrNoPeerScore
×
304
        }
×
305

306
        switch status.state {
3✔
307
        case peerStatusProtected:
×
308
                // If this peer is protected, we don't do anything.
×
309
                acsmLog.DebugS(ctx, "Peer is protected, no change")
×
310

×
311
                return nil
×
312

313
        case peerStatusTemporary:
3✔
314
                // If this peer is temporary, we need to check if it will
3✔
315
                // revert to a restricted-access peer.
3✔
316
                peerCount, found := a.peerChanInfo[peerMapKey]
3✔
317
                if !found {
3✔
318
                        acsmLog.ErrorS(ctx, "Pending peer info not found",
×
319
                                ErrNoPendingPeerInfo)
×
320

×
321
                        // Error if we did not find any info in peerChanInfo.
×
322
                        return ErrNoPendingPeerInfo
×
323
                }
×
324

325
                currentNumPending := peerCount.PendingOpenCount - 1
3✔
326

3✔
327
                acsmLog.DebugS(ctx, "Peer is temporary, decrementing "+
3✔
328
                        "pending count",
3✔
329
                        "pending_count", currentNumPending)
3✔
330

3✔
331
                if currentNumPending == 0 {
6✔
332
                        // Remove the entry from peerChanInfo.
3✔
333
                        delete(a.peerChanInfo, peerMapKey)
3✔
334

3✔
335
                        // If this is the only pending-open channel for this
3✔
336
                        // peer and it's getting removed, attempt to demote
3✔
337
                        // this peer to a restricted peer.
3✔
338
                        if a.numRestricted == a.cfg.maxRestrictedSlots {
3✔
UNCOV
339
                                // There are no available restricted slots, so
×
UNCOV
340
                                // we need to disconnect this peer. We leave
×
UNCOV
341
                                // this up to the caller.
×
UNCOV
342
                                acsmLog.WarnS(ctx, "Peer last pending "+
×
UNCOV
343
                                        "channel closed: ",
×
UNCOV
344
                                        ErrNoMoreRestrictedAccessSlots,
×
UNCOV
345
                                        "num_restricted", a.numRestricted,
×
UNCOV
346
                                        "max_restricted", a.cfg.maxRestrictedSlots)
×
UNCOV
347

×
UNCOV
348
                                return ErrNoMoreRestrictedAccessSlots
×
UNCOV
349
                        }
×
350

351
                        // Otherwise, there is an available restricted-access
352
                        // slot, so we can demote this peer.
353
                        a.peerScores[peerMapKey] = peerSlotStatus{
3✔
354
                                state: peerStatusRestricted,
3✔
355
                        }
3✔
356

3✔
357
                        // Update numRestricted.
3✔
358
                        oldRestricted := a.numRestricted
3✔
359
                        a.numRestricted++
3✔
360

3✔
361
                        acsmLog.InfoS(ctx, "Peer transitioned "+
3✔
362
                                "temporary -> restricted "+
3✔
363
                                "(last pending closed)",
3✔
364
                                "old_restricted", oldRestricted,
3✔
365
                                "new_restricted", a.numRestricted)
3✔
366

3✔
367
                        return nil
3✔
368
                }
369

370
                // Else, we don't need to demote this peer since it has other
371
                // pending-open channels with us.
372
                peerCount.PendingOpenCount = currentNumPending
×
373
                a.peerChanInfo[peerMapKey] = peerCount
×
374

×
375
                acsmLog.DebugS(ctx, "Peer still has other pending channels",
×
376
                        "pending_count", currentNumPending)
×
377

×
378
                return nil
×
379

380
        case peerStatusRestricted:
×
381
                // This should not be possible. This indicates an error.
×
382
                err := fmt.Errorf("invalid peer access state transition: "+
×
383
                        "pending close for restricted peer %x", peerMapKey)
×
384
                acsmLog.ErrorS(ctx, "Invalid peer access state transition", err)
×
385

×
386
                return err
×
387

388
        default:
×
389
                // This should not be possible.
×
390
                err := fmt.Errorf("invalid peer access status %v for %x",
×
391
                        status.state, peerMapKey)
×
392
                acsmLog.ErrorS(ctx, "Invalid peer access status", err)
×
393

×
394
                return err
×
395
        }
396
}
397

398
// newOpenChan is called when a pending-open channel becomes an open channel
399
// (i.e. the funding transaction has confirmed). If the remote peer is a
400
// temporary-access peer, it will be promoted to a protected-access peer.
401
func (a *accessMan) newOpenChan(remotePub *btcec.PublicKey) error {
3✔
402
        a.banScoreMtx.Lock()
3✔
403
        defer a.banScoreMtx.Unlock()
3✔
404

3✔
405
        ctx := btclog.WithCtx(
3✔
406
                context.TODO(), lnutils.LogPubKey("peer", remotePub),
3✔
407
        )
3✔
408

3✔
409
        acsmLog.DebugS(ctx, "Processing new open channel")
3✔
410

3✔
411
        peerMapKey := string(remotePub.SerializeCompressed())
3✔
412

3✔
413
        // Fetch the peer's access status from peerScores.
3✔
414
        status, found := a.peerScores[peerMapKey]
3✔
415
        if !found {
6✔
416
                // If we didn't find the peer, we'll return an error.
3✔
417
                acsmLog.ErrorS(ctx, "Peer score not found", ErrNoPeerScore)
3✔
418

3✔
419
                return ErrNoPeerScore
3✔
420
        }
3✔
421

422
        switch status.state {
3✔
423
        case peerStatusProtected:
3✔
424
                acsmLog.DebugS(ctx, "Peer already protected, no change")
3✔
425

3✔
426
                // If the peer's state is already protected, we don't need to do
3✔
427
                // anything more.
3✔
428
                return nil
3✔
429

430
        case peerStatusTemporary:
3✔
431
                // If the peer's state is temporary, we'll upgrade the peer to
3✔
432
                // a protected peer.
3✔
433
                peerCount, found := a.peerChanInfo[peerMapKey]
3✔
434
                if !found {
3✔
435
                        // Error if we did not find any info in peerChanInfo.
×
436
                        acsmLog.ErrorS(ctx, "Pending peer info not found",
×
437
                                ErrNoPendingPeerInfo)
×
438

×
439
                        return ErrNoPendingPeerInfo
×
440
                }
×
441

442
                peerCount.HasOpenOrClosedChan = true
3✔
443
                peerCount.PendingOpenCount -= 1
3✔
444

3✔
445
                a.peerChanInfo[peerMapKey] = peerCount
3✔
446

3✔
447
                newStatus := peerSlotStatus{
3✔
448
                        state: peerStatusProtected,
3✔
449
                }
3✔
450
                a.peerScores[peerMapKey] = newStatus
3✔
451

3✔
452
                acsmLog.InfoS(ctx, "Peer transitioned temporary -> "+
3✔
453
                        "protected (channel opened)")
3✔
454

3✔
455
                return nil
3✔
456

457
        case peerStatusRestricted:
×
458
                // This should not be possible. For the server to receive a
×
459
                // state-transition event via NewOpenChan, the server must have
×
460
                // previously granted this peer "temporary" access. This
×
461
                // temporary access would not have been revoked or downgraded
×
462
                // without `CloseChannel` being called with the pending
×
463
                // argument set to true. This means that an open-channel state
×
464
                // transition would be impossible. Therefore, we can return an
×
465
                // error.
×
466
                err := fmt.Errorf("invalid peer access status: new open "+
×
467
                        "channel for restricted peer %x", peerMapKey)
×
468

×
469
                acsmLog.ErrorS(ctx, "Invalid peer access status", err)
×
470

×
471
                return err
×
472

473
        default:
×
474
                // This should not be possible.
×
475
                err := fmt.Errorf("invalid peer access status %v for %x",
×
476
                        status.state, peerMapKey)
×
477

×
478
                acsmLog.ErrorS(ctx, "Invalid peer access status", err)
×
479

×
480
                return err
×
481
        }
482
}
483

484
// checkAcceptIncomingConn checks whether, given the remote's public hex-
485
// encoded key, we should not accept this incoming connection or immediately
486
// disconnect. This does not assign to the server's peerScores maps. This is
487
// just an inbound filter that the brontide listeners use.
488
//
489
// TODO(yy): We should also consider removing this `checkAcceptIncomingConn`
490
// check as a) it doesn't check for ban score; and b) we should, and already
491
// have this check when we handle incoming connection in `InboundPeerConnected`.
492
func (a *accessMan) checkAcceptIncomingConn(remotePub *btcec.PublicKey) (
493
        bool, error) {
3✔
494

3✔
495
        ctx := btclog.WithCtx(
3✔
496
                context.TODO(), lnutils.LogPubKey("peer", remotePub),
3✔
497
        )
3✔
498

3✔
499
        peerMapKey := string(remotePub.SerializeCompressed())
3✔
500

3✔
501
        acsmLog.TraceS(ctx, "Checking incoming connection ban score")
3✔
502

3✔
503
        a.banScoreMtx.RLock()
3✔
504
        defer a.banScoreMtx.RUnlock()
3✔
505

3✔
506
        _, found := a.peerChanInfo[peerMapKey]
3✔
507

3✔
508
        // Exit early if found.
3✔
509
        if found {
6✔
510
                acsmLog.DebugS(ctx, "Peer found (protected/temporary), "+
3✔
511
                        "accepting")
3✔
512

3✔
513
                return true, nil
3✔
514
        }
3✔
515

516
        _, found = a.peerScores[peerMapKey]
3✔
517

3✔
518
        // Exit early if found.
3✔
519
        if found {
6✔
520
                acsmLog.DebugS(ctx, "Found existing peer, accepting")
3✔
521

3✔
522
                return true, nil
3✔
523
        }
3✔
524

525
        acsmLog.DebugS(ctx, "Peer not found in counts, checking restricted "+
3✔
526
                "slots")
3✔
527

3✔
528
        // Check numRestricted to see if there is an available slot. In
3✔
529
        // the future, it's possible to add better heuristics.
3✔
530
        if a.numRestricted < a.cfg.maxRestrictedSlots {
6✔
531
                // There is an available slot.
3✔
532
                acsmLog.DebugS(ctx, "Restricted slot available, accepting ",
3✔
533
                        "num_restricted", a.numRestricted, "max_restricted",
3✔
534
                        a.cfg.maxRestrictedSlots)
3✔
535

3✔
536
                return true, nil
3✔
537
        }
3✔
538

539
        // If there are no slots left, then we reject this connection.
540
        acsmLog.WarnS(ctx, "No restricted slots available, rejecting ",
3✔
541
                ErrNoMoreRestrictedAccessSlots, "num_restricted",
3✔
542
                a.numRestricted, "max_restricted", a.cfg.maxRestrictedSlots)
3✔
543

3✔
544
        return false, ErrNoMoreRestrictedAccessSlots
3✔
545
}
546

547
// addPeerAccess tracks a peer's access in the maps. This should be called when
548
// the peer has fully connected.
549
func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey,
550
        access peerAccessStatus, inbound bool) {
3✔
551

3✔
552
        ctx := btclog.WithCtx(
3✔
553
                context.TODO(), lnutils.LogPubKey("peer", remotePub),
3✔
554
        )
3✔
555

3✔
556
        acsmLog.DebugS(ctx, "Adding peer access", "access", access)
3✔
557

3✔
558
        // Add the remote public key to peerScores.
3✔
559
        a.banScoreMtx.Lock()
3✔
560
        defer a.banScoreMtx.Unlock()
3✔
561

3✔
562
        peerMapKey := string(remotePub.SerializeCompressed())
3✔
563

3✔
564
        // Exit early if this is an existing peer, which means it won't take
3✔
565
        // another slot.
3✔
566
        _, found := a.peerScores[peerMapKey]
3✔
567
        if found {
3✔
UNCOV
568
                acsmLog.DebugS(ctx, "Skipped taking restricted slot for "+
×
UNCOV
569
                        "existing peer")
×
UNCOV
570

×
UNCOV
571
                return
×
UNCOV
572
        }
×
573

574
        a.peerScores[peerMapKey] = peerSlotStatus{state: access}
3✔
575

3✔
576
        // Exit early if this is not a restricted peer.
3✔
577
        if access != peerStatusRestricted {
6✔
578
                acsmLog.DebugS(ctx, "Skipped taking restricted slot as peer "+
3✔
579
                        "already has access", "access", access)
3✔
580

3✔
581
                return
3✔
582
        }
3✔
583

584
        // Increment numRestricted if this is an inbound connection.
585
        if inbound {
6✔
586
                oldRestricted := a.numRestricted
3✔
587
                a.numRestricted++
3✔
588

3✔
589
                acsmLog.DebugS(ctx, "Incremented restricted slots",
3✔
590
                        "old_restricted", oldRestricted,
3✔
591
                        "new_restricted", a.numRestricted)
3✔
592

3✔
593
                return
3✔
594
        }
3✔
595

596
        // Otherwise, this is a newly created outbound connection. We won't
597
        // place any restriction on it, instead, we will do a hot upgrade here
598
        // to move it from restricted to temporary.
599
        peerCount := channeldb.ChanCount{
3✔
600
                HasOpenOrClosedChan: false,
3✔
601
                PendingOpenCount:    0,
3✔
602
        }
3✔
603

3✔
604
        a.peerChanInfo[peerMapKey] = peerCount
3✔
605
        a.peerScores[peerMapKey] = peerSlotStatus{
3✔
606
                state: peerStatusTemporary,
3✔
607
        }
3✔
608

3✔
609
        acsmLog.InfoS(ctx, "Upgraded outbound peer: restricted -> temporary")
3✔
610
}
611

612
// removePeerAccess removes the peer's access from the maps. This should be
613
// called when the peer has been disconnected.
614
func (a *accessMan) removePeerAccess(ctx context.Context, peerPubKey string) {
3✔
615
        acsmLog.DebugS(ctx, "Removing access:")
3✔
616

3✔
617
        a.banScoreMtx.Lock()
3✔
618
        defer a.banScoreMtx.Unlock()
3✔
619

3✔
620
        status, found := a.peerScores[peerPubKey]
3✔
621
        if !found {
3✔
UNCOV
622
                acsmLog.InfoS(ctx, "Peer score not found during removal")
×
UNCOV
623
                return
×
UNCOV
624
        }
×
625

626
        if status.state == peerStatusRestricted {
6✔
627
                // If the status is restricted, then we decrement from
3✔
628
                // numRestrictedSlots.
3✔
629
                oldRestricted := a.numRestricted
3✔
630
                a.numRestricted--
3✔
631

3✔
632
                acsmLog.DebugS(ctx, "Decremented restricted slots",
3✔
633
                        "old_restricted", oldRestricted,
3✔
634
                        "new_restricted", a.numRestricted)
3✔
635
        }
3✔
636

637
        acsmLog.TraceS(ctx, "Deleting from peerScores:")
3✔
638

3✔
639
        delete(a.peerScores, peerPubKey)
3✔
640

3✔
641
        // We now check whether this peer has channels with us or not.
3✔
642
        info, found := a.peerChanInfo[peerPubKey]
3✔
643
        if !found {
6✔
644
                acsmLog.DebugS(ctx, "Chan info not found during removal:")
3✔
645
                return
3✔
646
        }
3✔
647

648
        // Exit early if the peer has channel(s) with us.
649
        if info.HasOpenOrClosedChan {
6✔
650
                acsmLog.DebugS(ctx, "Skipped removing peer with channels:")
3✔
651
                return
3✔
652
        }
3✔
653

654
        // Skip removing the peer if it has pending open/close with us.
655
        if info.PendingOpenCount != 0 {
6✔
656
                acsmLog.DebugS(ctx, "Skipped removing peer with pending "+
3✔
657
                        "channels:")
3✔
658
                return
3✔
659
        }
3✔
660

661
        // Given this peer has no channels with us, we can now remove it.
662
        delete(a.peerChanInfo, peerPubKey)
3✔
663
        acsmLog.TraceS(ctx, "Removed peer from peerChanInfo:")
3✔
664
}
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