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

lightningnetwork / lnd / 15753697886

19 Jun 2025 08:49AM UTC coverage: 68.177% (+0.02%) from 68.161%
15753697886

Pull #9880

github

web-flow
Merge 32d8e2123 into e0a9705d5
Pull Request #9880: Improve access control in peer connections

186 of 234 new or added lines in 7 files covered. (79.49%)

129 existing lines in 24 files now uncovered.

134608 of 197440 relevant lines covered (68.18%)

22181.41 hits per line

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

80.5
/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) {
14✔
63
        a := &accessMan{
14✔
64
                cfg:          cfg,
14✔
65
                peerChanInfo: make(map[string]channeldb.ChanCount),
14✔
66
                peerScores:   make(map[string]peerSlotStatus),
14✔
67
        }
14✔
68

14✔
69
        counts, err := a.cfg.initAccessPerms()
14✔
70
        if err != nil {
14✔
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)
14✔
78

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

14✔
81
        return a, nil
14✔
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) {
25✔
87

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

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

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

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

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

111
                return peerStatusRestricted, true
8✔
112
        }
113

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

2✔
120
                return status.state, true
2✔
121
        }
2✔
122

123
        return peerStatusRestricted, false
7✔
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) {
20✔
130

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

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

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

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

20✔
142
        // Exit early if the peer is not restricted.
20✔
143
        if access != peerStatusRestricted {
33✔
144
                return access, nil
13✔
145
        }
13✔
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)
10✔
151
        if err != nil {
10✔
152
                acsmLog.ErrorS(ctx, "Error checking disconnect status", err)
×
153

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

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

1✔
162
                // Access is restricted here.
1✔
163
                return access, ErrGossiperBan
1✔
164
        }
1✔
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")
9✔
170

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

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

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

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

189
        return access, nil
5✔
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 {
4✔
196
        a.banScoreMtx.Lock()
4✔
197
        defer a.banScoreMtx.Unlock()
4✔
198

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

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

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

4✔
207
        // Fetch the peer's access status from peerScores.
4✔
208
        status, found := a.peerScores[peerMapKey]
4✔
209
        if !found {
4✔
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 {
4✔
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✔
NEW
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:
4✔
246
                // If the peer's access status is restricted, then we can
4✔
247
                // transition it to a temporary-access peer. We'll need to
4✔
248
                // update numRestricted and also peerScores. We'll also need to
4✔
249
                // update peerChanInfo.
4✔
250
                peerCount := channeldb.ChanCount{
4✔
251
                        HasOpenOrClosedChan: false,
4✔
252
                        PendingOpenCount:    1,
4✔
253
                }
4✔
254

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

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

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

4✔
265
                acsmLog.InfoS(ctx, "Peer transitioned restricted -> "+
4✔
266
                        "temporary (pending open)",
4✔
267
                        "old_restricted", oldRestricted,
4✔
268
                        "new_restricted", a.numRestricted)
4✔
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
4✔
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 {
4✔
287
        a.banScoreMtx.Lock()
4✔
288
        defer a.banScoreMtx.Unlock()
4✔
289

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

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

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

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

×
303
                return ErrNoPeerScore
×
304
        }
×
305

306
        switch status.state {
4✔
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:
4✔
314
                // If this peer is temporary, we need to check if it will
4✔
315
                // revert to a restricted-access peer.
4✔
316
                peerCount, found := a.peerChanInfo[peerMapKey]
4✔
317
                if !found {
4✔
318
                        acsmLog.ErrorS(ctx, "Pending peer info not found",
×
319
                                ErrNoPendingPeerInfo)
×
320

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

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

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

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

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

1✔
348
                                return ErrNoMoreRestrictedAccessSlots
1✔
349
                        }
1✔
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
×
NEW
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 {
4✔
402
        a.banScoreMtx.Lock()
4✔
403
        defer a.banScoreMtx.Unlock()
4✔
404

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

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

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

4✔
413
        // Fetch the peer's access status from peerScores.
4✔
414
        status, found := a.peerScores[peerMapKey]
4✔
415
        if !found {
7✔
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 {
4✔
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:
4✔
431
                // If the peer's state is temporary, we'll upgrade the peer to
4✔
432
                // a protected peer.
4✔
433
                peerCount, found := a.peerChanInfo[peerMapKey]
4✔
434
                if !found {
4✔
NEW
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
4✔
443
                peerCount.PendingOpenCount -= 1
4✔
444

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

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

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

4✔
455
                return nil
4✔
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) {
8✔
494

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

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

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

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

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

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

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

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

5✔
518
        // Exit early if found.
5✔
519
        if found {
8✔
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 "+
5✔
526
                "slots")
5✔
527

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

5✔
536
                return true, nil
5✔
537
        }
5✔
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) {
14✔
551

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

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

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

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

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

2✔
571
                return
2✔
572
        }
2✔
573

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

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

8✔
581
                return
8✔
582
        }
8✔
583

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

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

6✔
593
                return
6✔
594
        }
6✔
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{
4✔
600
                HasOpenOrClosedChan: false,
4✔
601
                PendingOpenCount:    0,
4✔
602
        }
4✔
603

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

4✔
609
        acsmLog.InfoS(ctx, "Upgraded outbound peer: restricted -> temporary")
4✔
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(remotePub *btcec.PublicKey) {
3✔
615
        ctx := btclog.WithCtx(
3✔
616
                context.TODO(), lnutils.LogPubKey("peer", remotePub),
3✔
617
        )
3✔
618
        acsmLog.DebugS(ctx, "Removing peer access")
3✔
619

3✔
620
        a.banScoreMtx.Lock()
3✔
621
        defer a.banScoreMtx.Unlock()
3✔
622

3✔
623
        peerMapKey := string(remotePub.SerializeCompressed())
3✔
624

3✔
625
        status, found := a.peerScores[peerMapKey]
3✔
626
        if !found {
3✔
UNCOV
627
                acsmLog.InfoS(ctx, "Peer score not found during removal")
×
UNCOV
628
                return
×
UNCOV
629
        }
×
630

631
        if status.state == peerStatusRestricted {
6✔
632
                // If the status is restricted, then we decrement from
3✔
633
                // numRestrictedSlots.
3✔
634
                oldRestricted := a.numRestricted
3✔
635
                a.numRestricted--
3✔
636

3✔
637
                acsmLog.DebugS(ctx, "Decremented restricted slots",
3✔
638
                        "old_restricted", oldRestricted,
3✔
639
                        "new_restricted", a.numRestricted)
3✔
640
        }
3✔
641

642
        acsmLog.TraceS(ctx, "Deleting peer from peerScores")
3✔
643

3✔
644
        delete(a.peerScores, peerMapKey)
3✔
645
}
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