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

lightningnetwork / lnd / 16918135633

12 Aug 2025 06:56PM UTC coverage: 56.955% (-9.9%) from 66.9%
16918135633

push

github

web-flow
Merge pull request #9871 from GeorgeTsagk/htlc-noop-add

Add `NoopAdd` HTLCs

48 of 147 new or added lines in 3 files covered. (32.65%)

29154 existing lines in 462 files now uncovered.

98265 of 172532 relevant lines covered (56.95%)

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

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

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

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

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

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

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

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

2✔
107
                        // Exit early if the peer is no longer restricted.
2✔
108
                        return peerStatusTemporary, true
2✔
109
                }
2✔
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]
2✔
116
        if found {
2✔
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
2✔
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) {
2✔
130

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

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

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

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

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

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

158
        if shouldDisconnect {
2✔
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")
2✔
170

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

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

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

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

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

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

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

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

2✔
207
        // Fetch the peer's access status from peerScores.
2✔
208
        status, found := a.peerScores[peerMapKey]
2✔
209
        if !found {
2✔
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 {
2✔
217
        case peerStatusProtected:
2✔
218
                acsmLog.DebugS(ctx, "Peer already protected, no change")
2✔
219

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

224
        case peerStatusTemporary:
2✔
225
                // If this peer's access status is temporary, we'll need to
2✔
226
                // update the peerChanInfo map. The peer's access status will
2✔
227
                // stay temporary.
2✔
228
                peerCount, found := a.peerChanInfo[peerMapKey]
2✔
229
                if !found {
2✔
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
2✔
239
                a.peerChanInfo[peerMapKey] = peerCount
2✔
240

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

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

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

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

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

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

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

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

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

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

×
303
                return ErrNoPeerScore
×
304
        }
×
305

306
        switch status.state {
2✔
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:
2✔
314
                // If this peer is temporary, we need to check if it will
2✔
315
                // revert to a restricted-access peer.
2✔
316
                peerCount, found := a.peerChanInfo[peerMapKey]
2✔
317
                if !found {
2✔
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
2✔
326

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

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

2✔
335
                        // If this is the only pending-open channel for this
2✔
336
                        // peer and it's getting removed, attempt to demote
2✔
337
                        // this peer to a restricted peer.
2✔
338
                        if a.numRestricted == a.cfg.maxRestrictedSlots {
2✔
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{
2✔
354
                                state: peerStatusRestricted,
2✔
355
                        }
2✔
356

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

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

2✔
367
                        return nil
2✔
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 {
2✔
402
        a.banScoreMtx.Lock()
2✔
403
        defer a.banScoreMtx.Unlock()
2✔
404

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

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

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

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

2✔
419
                return ErrNoPeerScore
2✔
420
        }
2✔
421

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

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

430
        case peerStatusTemporary:
2✔
431
                // If the peer's state is temporary, we'll upgrade the peer to
2✔
432
                // a protected peer.
2✔
433
                peerCount, found := a.peerChanInfo[peerMapKey]
2✔
434
                if !found {
2✔
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
2✔
443
                peerCount.PendingOpenCount -= 1
2✔
444

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2✔
544
        return false, ErrNoMoreRestrictedAccessSlots
2✔
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) {
2✔
551

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

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

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

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

2✔
564
        // Exit early if this is an existing peer, which means it won't take
2✔
565
        // another slot.
2✔
566
        _, found := a.peerScores[peerMapKey]
2✔
567
        if found {
2✔
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}
2✔
575

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

2✔
581
                return
2✔
582
        }
2✔
583

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

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

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

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

2✔
609
        acsmLog.InfoS(ctx, "Upgraded outbound peer: restricted -> temporary")
2✔
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) {
2✔
615
        acsmLog.DebugS(ctx, "Removing access:")
2✔
616

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

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

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

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

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

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

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

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

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

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