Skip to content

Commit 856ec46

Browse files
committed
accessman: skip incrementing num of slots for existing peer
When a peer is already existing, we should skip incrementing the `numRestricted` count. Also patched unit test for method `addPeerAccess`.
1 parent 0f9332b commit 856ec46

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

accessman.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,23 @@ func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey,
559559

560560
peerMapKey := string(remotePub.SerializeCompressed())
561561

562+
// Exit early if this is an existing peer, which means it won't take
563+
// another slot.
564+
_, found := a.peerScores[peerMapKey]
565+
if found {
566+
acsmLog.DebugS(ctx, "Skipped taking restricted slot for "+
567+
"existing peer")
568+
569+
return
570+
}
571+
562572
a.peerScores[peerMapKey] = peerSlotStatus{state: access}
563573

564574
// Exit early if this is not a restricted peer.
565575
if access != peerStatusRestricted {
576+
acsmLog.DebugS(ctx, "Skipped taking restricted slot as peer "+
577+
"already has access", "access", access)
578+
566579
return
567580
}
568581

accessman_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,154 @@ func TestHasPeer(t *testing.T) {
526526
require.False(t, found)
527527
require.Equal(t, peerStatusRestricted, access)
528528
}
529+
530+
// TestAddPeerAccessInbound asserts the num of slots is correctly incremented
531+
// only for a new inbound peer with restricted access.
532+
func TestAddPeerAccessInbound(t *testing.T) {
533+
t.Parallel()
534+
535+
// Create a testing accessMan.
536+
a := &accessMan{
537+
peerCounts: make(map[string]channeldb.ChanCount),
538+
peerScores: make(map[string]peerSlotStatus),
539+
}
540+
541+
// Create a testing key.
542+
priv, err := btcec.NewPrivateKey()
543+
require.NoError(t, err)
544+
pub := priv.PubKey()
545+
pubStr := string(pub.SerializeCompressed())
546+
547+
// Add this peer as an inbound peer with peerStatusRestricted.
548+
a.addPeerAccess(pub, peerStatusRestricted, true)
549+
550+
// Assert the accessMan's internal state.
551+
//
552+
// We expect to see one peer found in the score map, and one slot is
553+
// taken, and this peer is not found in the counts map.
554+
require.Len(t, a.peerScores, 1)
555+
require.Equal(t, int64(1), a.numRestricted)
556+
require.NotContains(t, a.peerCounts, pubStr)
557+
558+
// The peer should be found in the score map.
559+
score, ok := a.peerScores[pubStr]
560+
require.True(t, ok)
561+
562+
expecedScore := peerSlotStatus{state: peerStatusRestricted}
563+
require.Equal(t, expecedScore, score)
564+
565+
// Add this peer again, we expect the available slots to stay unchanged.
566+
a.addPeerAccess(pub, peerStatusRestricted, true)
567+
568+
// Assert the internal state is not changed.
569+
require.Len(t, a.peerScores, 1)
570+
require.Equal(t, int64(1), a.numRestricted)
571+
require.NotContains(t, a.peerCounts, pubStr)
572+
573+
// Reset the accessMan.
574+
a = &accessMan{
575+
peerCounts: make(map[string]channeldb.ChanCount),
576+
peerScores: make(map[string]peerSlotStatus),
577+
}
578+
579+
// Add this peer as an inbound peer with peerStatusTemporary.
580+
a.addPeerAccess(pub, peerStatusTemporary, true)
581+
582+
// Assert the accessMan's internal state.
583+
//
584+
// We expect to see one peer found in the score map, and no slot is
585+
// taken since this peer is not restricted.
586+
require.Len(t, a.peerScores, 1)
587+
require.Equal(t, int64(0), a.numRestricted)
588+
589+
// NOTE: in reality this is not possible as the peer must have been put
590+
// into the map `peerCounts` before its perm can be upgraded.
591+
require.NotContains(t, a.peerCounts, pubStr)
592+
593+
// The peer should be found in the score map.
594+
score, ok = a.peerScores[pubStr]
595+
require.True(t, ok)
596+
597+
expecedScore = peerSlotStatus{state: peerStatusTemporary}
598+
require.Equal(t, expecedScore, score)
599+
}
600+
601+
// TestAddPeerAccessOutbound asserts that outbound peer is not restricted and
602+
// its perm is upgraded when it has peerStatusRestricted.
603+
func TestAddPeerAccessOutbound(t *testing.T) {
604+
t.Parallel()
605+
606+
// Create a testing accessMan.
607+
a := &accessMan{
608+
peerCounts: make(map[string]channeldb.ChanCount),
609+
peerScores: make(map[string]peerSlotStatus),
610+
}
611+
612+
// Create a testing key.
613+
priv, err := btcec.NewPrivateKey()
614+
require.NoError(t, err)
615+
pub := priv.PubKey()
616+
pubStr := string(pub.SerializeCompressed())
617+
618+
// Add this peer as an outbound peer with peerStatusRestricted.
619+
a.addPeerAccess(pub, peerStatusRestricted, false)
620+
621+
// Assert the accessMan's internal state.
622+
//
623+
// We expect to see one peer found in the score map, and no slot is
624+
// taken, and this peer is found in the counts map.
625+
require.Len(t, a.peerScores, 1)
626+
require.Equal(t, int64(0), a.numRestricted)
627+
require.Contains(t, a.peerCounts, pubStr)
628+
629+
// The peer should be found in the score map.
630+
score, ok := a.peerScores[pubStr]
631+
require.True(t, ok)
632+
633+
// Its perm should be upgraded to temporary.
634+
expecedScore := peerSlotStatus{state: peerStatusTemporary}
635+
require.Equal(t, expecedScore, score)
636+
637+
// The peer should be found in the peer counts map.
638+
count, ok := a.peerCounts[pubStr]
639+
require.True(t, ok)
640+
641+
// The peer's count should be initialized correctly.
642+
require.Zero(t, count.PendingOpenCount)
643+
require.False(t, count.HasOpenOrClosedChan)
644+
645+
// Add this peer again, we expect the available slots to stay unchanged.
646+
a.addPeerAccess(pub, peerStatusRestricted, true)
647+
648+
// Assert the internal state is not changed.
649+
require.Len(t, a.peerScores, 1)
650+
require.Equal(t, int64(0), a.numRestricted)
651+
require.Contains(t, a.peerCounts, pubStr)
652+
653+
// Reset the accessMan.
654+
a = &accessMan{
655+
peerCounts: make(map[string]channeldb.ChanCount),
656+
peerScores: make(map[string]peerSlotStatus),
657+
}
658+
659+
// Add this peer as an inbound peer with peerStatusTemporary.
660+
a.addPeerAccess(pub, peerStatusTemporary, true)
661+
662+
// Assert the accessMan's internal state.
663+
//
664+
// We expect to see one peer found in the score map, and no slot is
665+
// taken since this peer is not restricted.
666+
require.Len(t, a.peerScores, 1)
667+
require.Equal(t, int64(0), a.numRestricted)
668+
669+
// NOTE: in reality this is not possible as the peer must have been put
670+
// into the map `peerCounts` before its perm can be upgraded.
671+
require.NotContains(t, a.peerCounts, pubStr)
672+
673+
// The peer should be found in the score map.
674+
score, ok = a.peerScores[pubStr]
675+
require.True(t, ok)
676+
677+
expecedScore = peerSlotStatus{state: peerStatusTemporary}
678+
require.Equal(t, expecedScore, score)
679+
}

0 commit comments

Comments
 (0)