Skip to content

Commit 32d8e21

Browse files
committed
channeldb+lnd: rename peerCounts to peerChanInfo for clarity
1 parent 03de0f9 commit 32d8e21

File tree

4 files changed

+76
-76
lines changed

4 files changed

+76
-76
lines changed

accessman.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ type accessMan struct {
2121
// the server mutex.
2222
banScoreMtx sync.RWMutex
2323

24-
// peerCounts is a mapping from remote public key to {bool, uint64}
25-
// where the bool indicates that we have an open/closed channel with
26-
// the peer and where the uint64 indicates the number of pending-open
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
2727
// channels we currently have with them. This mapping will be used to
2828
// determine access permissions for the peer. The map key is the
2929
// string-version of the serialized public key.
3030
//
3131
// NOTE: This MUST be accessed with the banScoreMtx held.
32-
peerCounts map[string]channeldb.ChanCount
32+
peerChanInfo map[string]channeldb.ChanCount
3333

3434
// peerScores stores each connected peer's access status. The map key
3535
// is the string-version of the serialized public key.
3636
//
3737
// NOTE: This MUST be accessed with the banScoreMtx held.
3838
//
39-
// TODO(yy): unify `peerScores` and `peerCounts` - there's no need to
39+
// TODO(yy): unify `peerScores` and `peerChanInfo` - there's no need to
4040
// create two maps tracking essentially the same info. `numRestricted`
41-
// can also be derived from `peerCounts`.
41+
// can also be derived from `peerChanInfo`.
4242
peerScores map[string]peerSlotStatus
4343

4444
// numRestricted tracks the number of peers with restricted access in
@@ -48,7 +48,7 @@ type accessMan struct {
4848

4949
type accessManConfig struct {
5050
// initAccessPerms checks the channeldb for initial access permissions
51-
// and then populates the peerCounts and peerScores maps.
51+
// and then populates the peerChanInfo and peerScores maps.
5252
initAccessPerms func() (map[string]channeldb.ChanCount, error)
5353

5454
// shouldDisconnect determines whether we should disconnect a peer or
@@ -61,20 +61,20 @@ type accessManConfig struct {
6161

6262
func newAccessMan(cfg *accessManConfig) (*accessMan, error) {
6363
a := &accessMan{
64-
cfg: cfg,
65-
peerCounts: make(map[string]channeldb.ChanCount),
66-
peerScores: make(map[string]peerSlotStatus),
64+
cfg: cfg,
65+
peerChanInfo: make(map[string]channeldb.ChanCount),
66+
peerScores: make(map[string]peerSlotStatus),
6767
}
6868

6969
counts, err := a.cfg.initAccessPerms()
7070
if err != nil {
7171
return nil, err
7272
}
7373

74-
// We'll populate the server's peerCounts map with the counts fetched
74+
// We'll populate the server's peerChanInfo map with the counts fetched
7575
// via initAccessPerms. Also note that we haven't yet connected to the
7676
// peers.
77-
maps.Copy(a.peerCounts, counts)
77+
maps.Copy(a.peerChanInfo, counts)
7878

7979
acsmLog.Info("Access Manager initialized")
8080

@@ -90,7 +90,7 @@ func (a *accessMan) hasPeer(ctx context.Context,
9090
a.banScoreMtx.RLock()
9191
defer a.banScoreMtx.RUnlock()
9292

93-
count, found := a.peerCounts[pub]
93+
count, found := a.peerChanInfo[pub]
9494
if found {
9595
if count.HasOpenOrClosedChan {
9696
acsmLog.DebugS(ctx, "Peer has open/closed channel, "+
@@ -223,11 +223,11 @@ func (a *accessMan) newPendingOpenChan(remotePub *btcec.PublicKey) error {
223223

224224
case peerStatusTemporary:
225225
// If this peer's access status is temporary, we'll need to
226-
// update the peerCounts map. The peer's access status will stay
227-
// temporary.
228-
peerCount, found := a.peerCounts[peerMapKey]
226+
// update the peerChanInfo map. The peer's access status will
227+
// stay temporary.
228+
peerCount, found := a.peerChanInfo[peerMapKey]
229229
if !found {
230-
// Error if we did not find any info in peerCounts.
230+
// Error if we did not find any info in peerChanInfo.
231231
acsmLog.ErrorS(ctx, "Pending peer info not found",
232232
ErrNoPendingPeerInfo)
233233

@@ -236,7 +236,7 @@ func (a *accessMan) newPendingOpenChan(remotePub *btcec.PublicKey) error {
236236

237237
// Increment the pending channel amount.
238238
peerCount.PendingOpenCount += 1
239-
a.peerCounts[peerMapKey] = peerCount
239+
a.peerChanInfo[peerMapKey] = peerCount
240240

241241
acsmLog.DebugS(ctx, "Peer is temporary, incremented "+
242242
"pending count",
@@ -246,13 +246,13 @@ func (a *accessMan) newPendingOpenChan(remotePub *btcec.PublicKey) error {
246246
// If the peer's access status is restricted, then we can
247247
// transition it to a temporary-access peer. We'll need to
248248
// update numRestricted and also peerScores. We'll also need to
249-
// update peerCounts.
249+
// update peerChanInfo.
250250
peerCount := channeldb.ChanCount{
251251
HasOpenOrClosedChan: false,
252252
PendingOpenCount: 1,
253253
}
254254

255-
a.peerCounts[peerMapKey] = peerCount
255+
a.peerChanInfo[peerMapKey] = peerCount
256256

257257
// A restricted-access slot has opened up.
258258
oldRestricted := a.numRestricted
@@ -313,12 +313,12 @@ func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error {
313313
case peerStatusTemporary:
314314
// If this peer is temporary, we need to check if it will
315315
// revert to a restricted-access peer.
316-
peerCount, found := a.peerCounts[peerMapKey]
316+
peerCount, found := a.peerChanInfo[peerMapKey]
317317
if !found {
318318
acsmLog.ErrorS(ctx, "Pending peer info not found",
319319
ErrNoPendingPeerInfo)
320320

321-
// Error if we did not find any info in peerCounts.
321+
// Error if we did not find any info in peerChanInfo.
322322
return ErrNoPendingPeerInfo
323323
}
324324

@@ -329,8 +329,8 @@ func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error {
329329
"pending_count", currentNumPending)
330330

331331
if currentNumPending == 0 {
332-
// Remove the entry from peerCounts.
333-
delete(a.peerCounts, peerMapKey)
332+
// Remove the entry from peerChanInfo.
333+
delete(a.peerChanInfo, peerMapKey)
334334

335335
// If this is the only pending-open channel for this
336336
// peer and it's getting removed, attempt to demote
@@ -370,7 +370,7 @@ func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error {
370370
// Else, we don't need to demote this peer since it has other
371371
// pending-open channels with us.
372372
peerCount.PendingOpenCount = currentNumPending
373-
a.peerCounts[peerMapKey] = peerCount
373+
a.peerChanInfo[peerMapKey] = peerCount
374374

375375
acsmLog.DebugS(ctx, "Peer still has other pending channels",
376376
"pending_count", currentNumPending)
@@ -430,9 +430,9 @@ func (a *accessMan) newOpenChan(remotePub *btcec.PublicKey) error {
430430
case peerStatusTemporary:
431431
// If the peer's state is temporary, we'll upgrade the peer to
432432
// a protected peer.
433-
peerCount, found := a.peerCounts[peerMapKey]
433+
peerCount, found := a.peerChanInfo[peerMapKey]
434434
if !found {
435-
// Error if we did not find any info in peerCounts.
435+
// Error if we did not find any info in peerChanInfo.
436436
acsmLog.ErrorS(ctx, "Pending peer info not found",
437437
ErrNoPendingPeerInfo)
438438

@@ -442,7 +442,7 @@ func (a *accessMan) newOpenChan(remotePub *btcec.PublicKey) error {
442442
peerCount.HasOpenOrClosedChan = true
443443
peerCount.PendingOpenCount -= 1
444444

445-
a.peerCounts[peerMapKey] = peerCount
445+
a.peerChanInfo[peerMapKey] = peerCount
446446

447447
newStatus := peerSlotStatus{
448448
state: peerStatusProtected,
@@ -503,7 +503,7 @@ func (a *accessMan) checkAcceptIncomingConn(remotePub *btcec.PublicKey) (
503503
a.banScoreMtx.RLock()
504504
defer a.banScoreMtx.RUnlock()
505505

506-
_, found := a.peerCounts[peerMapKey]
506+
_, found := a.peerChanInfo[peerMapKey]
507507

508508
// Exit early if found.
509509
if found {
@@ -601,7 +601,7 @@ func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey,
601601
PendingOpenCount: 0,
602602
}
603603

604-
a.peerCounts[peerMapKey] = peerCount
604+
a.peerChanInfo[peerMapKey] = peerCount
605605
a.peerScores[peerMapKey] = peerSlotStatus{
606606
state: peerStatusTemporary,
607607
}

accessman_test.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,22 @@ func TestAccessManRestrictedSlots(t *testing.T) {
100100
a, err := newAccessMan(cfg)
101101
require.NoError(t, err)
102102

103-
// Check that the peerCounts map is correctly populated with three
103+
// Check that the peerChanInfo map is correctly populated with three
104104
// peers.
105105
require.Equal(t, 0, int(a.numRestricted))
106-
require.Equal(t, 3, len(a.peerCounts))
106+
require.Equal(t, 3, len(a.peerChanInfo))
107107

108-
peerCount1, ok := a.peerCounts[peerKeySer1]
108+
peerCount1, ok := a.peerChanInfo[peerKeySer1]
109109
require.True(t, ok)
110110
require.True(t, peerCount1.HasOpenOrClosedChan)
111111
require.Equal(t, peer1PendingCount, int(peerCount1.PendingOpenCount))
112112

113-
peerCount2, ok := a.peerCounts[peerKeySer2]
113+
peerCount2, ok := a.peerChanInfo[peerKeySer2]
114114
require.True(t, ok)
115115
require.True(t, peerCount2.HasOpenOrClosedChan)
116116
require.Equal(t, peer2PendingCount, int(peerCount2.PendingOpenCount))
117117

118-
peerCount3, ok := a.peerCounts[peerKeySer3]
118+
peerCount3, ok := a.peerChanInfo[peerKeySer3]
119119
require.True(t, ok)
120120
require.False(t, peerCount3.HasOpenOrClosedChan)
121121
require.Equal(t, peer3PendingCount, int(peerCount3.PendingOpenCount))
@@ -144,7 +144,7 @@ func TestAccessManRestrictedSlots(t *testing.T) {
144144

145145
// Assert that accessman's internal state is updated with peer4. We
146146
// expect this new peer to have 1 pending open count.
147-
peerCount4, ok := a.peerCounts[string(peerKey4.SerializeCompressed())]
147+
peerCount4, ok := a.peerChanInfo[string(peerKey4.SerializeCompressed())]
148148
require.True(t, ok)
149149
require.False(t, peerCount4.HasOpenOrClosedChan)
150150
require.Equal(t, 1, int(peerCount4.PendingOpenCount))
@@ -157,7 +157,7 @@ func TestAccessManRestrictedSlots(t *testing.T) {
157157
// Assert that accessman's internal state is updated with peer3. We
158158
// expect this existing peer to decrement its pending open count and the
159159
// flag `HasOpenOrClosedChan` should be true.
160-
peerCount3, ok = a.peerCounts[peerKeySer3]
160+
peerCount3, ok = a.peerChanInfo[peerKeySer3]
161161
require.True(t, ok)
162162
require.True(t, peerCount3.HasOpenOrClosedChan)
163163
require.Equal(t, peer3PendingCount-1, int(peerCount3.PendingOpenCount))
@@ -175,7 +175,7 @@ func TestAccessManRestrictedSlots(t *testing.T) {
175175
require.ErrorIs(t, err, ErrNoMoreRestrictedAccessSlots)
176176

177177
// Assert that peer4 is removed.
178-
_, ok = a.peerCounts[string(peerKey4.SerializeCompressed())]
178+
_, ok = a.peerChanInfo[string(peerKey4.SerializeCompressed())]
179179
require.False(t, ok)
180180
}
181181

@@ -434,7 +434,7 @@ func TestAssignPeerPermsBypassExisting(t *testing.T) {
434434
return peerPriv.PubKey()
435435
}
436436

437-
// peer1 exists in `peerCounts` map.
437+
// peer1 exists in `peerChanInfo` map.
438438
peer1 := genPeerPub()
439439
peer1Str := string(peer1.SerializeCompressed())
440440

@@ -494,27 +494,27 @@ func TestHasPeer(t *testing.T) {
494494

495495
// Create a testing accessMan.
496496
a := &accessMan{
497-
peerCounts: make(map[string]channeldb.ChanCount),
498-
peerScores: make(map[string]peerSlotStatus),
497+
peerChanInfo: make(map[string]channeldb.ChanCount),
498+
peerScores: make(map[string]peerSlotStatus),
499499
}
500500

501501
// peer1 exists with an open channel.
502502
peer1 := "peer1"
503-
a.peerCounts[peer1] = channeldb.ChanCount{
503+
a.peerChanInfo[peer1] = channeldb.ChanCount{
504504
HasOpenOrClosedChan: true,
505505
}
506506
peer1Access := peerStatusProtected
507507

508508
// peer2 exists with a pending channel.
509509
peer2 := "peer2"
510-
a.peerCounts[peer2] = channeldb.ChanCount{
510+
a.peerChanInfo[peer2] = channeldb.ChanCount{
511511
PendingOpenCount: 1,
512512
}
513513
peer2Access := peerStatusTemporary
514514

515515
// peer3 exists without any channels.
516516
peer3 := "peer3"
517-
a.peerCounts[peer3] = channeldb.ChanCount{}
517+
a.peerChanInfo[peer3] = channeldb.ChanCount{}
518518
peer3Access := peerStatusRestricted
519519

520520
// peer4 exists with a score.
@@ -560,8 +560,8 @@ func TestAddPeerAccessInbound(t *testing.T) {
560560

561561
// Create a testing accessMan.
562562
a := &accessMan{
563-
peerCounts: make(map[string]channeldb.ChanCount),
564-
peerScores: make(map[string]peerSlotStatus),
563+
peerChanInfo: make(map[string]channeldb.ChanCount),
564+
peerScores: make(map[string]peerSlotStatus),
565565
}
566566

567567
// Create a testing key.
@@ -579,7 +579,7 @@ func TestAddPeerAccessInbound(t *testing.T) {
579579
// taken, and this peer is not found in the counts map.
580580
require.Len(t, a.peerScores, 1)
581581
require.Equal(t, int64(1), a.numRestricted)
582-
require.NotContains(t, a.peerCounts, pubStr)
582+
require.NotContains(t, a.peerChanInfo, pubStr)
583583

584584
// The peer should be found in the score map.
585585
score, ok := a.peerScores[pubStr]
@@ -594,12 +594,12 @@ func TestAddPeerAccessInbound(t *testing.T) {
594594
// Assert the internal state is not changed.
595595
require.Len(t, a.peerScores, 1)
596596
require.Equal(t, int64(1), a.numRestricted)
597-
require.NotContains(t, a.peerCounts, pubStr)
597+
require.NotContains(t, a.peerChanInfo, pubStr)
598598

599599
// Reset the accessMan.
600600
a = &accessMan{
601-
peerCounts: make(map[string]channeldb.ChanCount),
602-
peerScores: make(map[string]peerSlotStatus),
601+
peerChanInfo: make(map[string]channeldb.ChanCount),
602+
peerScores: make(map[string]peerSlotStatus),
603603
}
604604

605605
// Add this peer as an inbound peer with peerStatusTemporary.
@@ -613,8 +613,8 @@ func TestAddPeerAccessInbound(t *testing.T) {
613613
require.Equal(t, int64(0), a.numRestricted)
614614

615615
// NOTE: in reality this is not possible as the peer must have been put
616-
// into the map `peerCounts` before its perm can be upgraded.
617-
require.NotContains(t, a.peerCounts, pubStr)
616+
// into the map `peerChanInfo` before its perm can be upgraded.
617+
require.NotContains(t, a.peerChanInfo, pubStr)
618618

619619
// The peer should be found in the score map.
620620
score, ok = a.peerScores[pubStr]
@@ -631,8 +631,8 @@ func TestAddPeerAccessOutbound(t *testing.T) {
631631

632632
// Create a testing accessMan.
633633
a := &accessMan{
634-
peerCounts: make(map[string]channeldb.ChanCount),
635-
peerScores: make(map[string]peerSlotStatus),
634+
peerChanInfo: make(map[string]channeldb.ChanCount),
635+
peerScores: make(map[string]peerSlotStatus),
636636
}
637637

638638
// Create a testing key.
@@ -650,7 +650,7 @@ func TestAddPeerAccessOutbound(t *testing.T) {
650650
// taken, and this peer is found in the counts map.
651651
require.Len(t, a.peerScores, 1)
652652
require.Equal(t, int64(0), a.numRestricted)
653-
require.Contains(t, a.peerCounts, pubStr)
653+
require.Contains(t, a.peerChanInfo, pubStr)
654654

655655
// The peer should be found in the score map.
656656
score, ok := a.peerScores[pubStr]
@@ -661,7 +661,7 @@ func TestAddPeerAccessOutbound(t *testing.T) {
661661
require.Equal(t, expecedScore, score)
662662

663663
// The peer should be found in the peer counts map.
664-
count, ok := a.peerCounts[pubStr]
664+
count, ok := a.peerChanInfo[pubStr]
665665
require.True(t, ok)
666666

667667
// The peer's count should be initialized correctly.
@@ -674,12 +674,12 @@ func TestAddPeerAccessOutbound(t *testing.T) {
674674
// Assert the internal state is not changed.
675675
require.Len(t, a.peerScores, 1)
676676
require.Equal(t, int64(0), a.numRestricted)
677-
require.Contains(t, a.peerCounts, pubStr)
677+
require.Contains(t, a.peerChanInfo, pubStr)
678678

679679
// Reset the accessMan.
680680
a = &accessMan{
681-
peerCounts: make(map[string]channeldb.ChanCount),
682-
peerScores: make(map[string]peerSlotStatus),
681+
peerChanInfo: make(map[string]channeldb.ChanCount),
682+
peerScores: make(map[string]peerSlotStatus),
683683
}
684684

685685
// Add this peer as an inbound peer with peerStatusTemporary.
@@ -693,8 +693,8 @@ func TestAddPeerAccessOutbound(t *testing.T) {
693693
require.Equal(t, int64(0), a.numRestricted)
694694

695695
// NOTE: in reality this is not possible as the peer must have been put
696-
// into the map `peerCounts` before its perm can be upgraded.
697-
require.NotContains(t, a.peerCounts, pubStr)
696+
// into the map `peerChanInfo` before its perm can be upgraded.
697+
require.NotContains(t, a.peerChanInfo, pubStr)
698698

699699
// The peer should be found in the score map.
700700
score, ok = a.peerScores[pubStr]

0 commit comments

Comments
 (0)