From ceeb1239251cfc033f188b456eecd7a259641d50 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 19 Jun 2025 16:48:13 +0800 Subject: [PATCH] channeldb+lnd: rename `peerCounts` to `peerChanInfo` for clarity --- accessman.go | 60 +++++++++++++++++++++--------------------- accessman_test.go | 62 ++++++++++++++++++++++---------------------- channeldb/db.go | 14 +++++----- channeldb/db_test.go | 16 ++++++------ 4 files changed, 76 insertions(+), 76 deletions(-) diff --git a/accessman.go b/accessman.go index aabeaf9f2..80a629569 100644 --- a/accessman.go +++ b/accessman.go @@ -21,24 +21,24 @@ type accessMan struct { // the server mutex. banScoreMtx sync.RWMutex - // peerCounts is a mapping from remote public key to {bool, uint64} - // where the bool indicates that we have an open/closed channel with - // the peer and where the uint64 indicates the number of pending-open + // peerChanInfo is a mapping from remote public key to {bool, uint64} + // where the bool indicates that we have an open/closed channel with the + // peer and where the uint64 indicates the number of pending-open // channels we currently have with them. This mapping will be used to // determine access permissions for the peer. The map key is the // string-version of the serialized public key. // // NOTE: This MUST be accessed with the banScoreMtx held. - peerCounts map[string]channeldb.ChanCount + peerChanInfo map[string]channeldb.ChanCount // peerScores stores each connected peer's access status. The map key // is the string-version of the serialized public key. // // NOTE: This MUST be accessed with the banScoreMtx held. // - // TODO(yy): unify `peerScores` and `peerCounts` - there's no need to + // TODO(yy): unify `peerScores` and `peerChanInfo` - there's no need to // create two maps tracking essentially the same info. `numRestricted` - // can also be derived from `peerCounts`. + // can also be derived from `peerChanInfo`. peerScores map[string]peerSlotStatus // numRestricted tracks the number of peers with restricted access in @@ -48,7 +48,7 @@ type accessMan struct { type accessManConfig struct { // initAccessPerms checks the channeldb for initial access permissions - // and then populates the peerCounts and peerScores maps. + // and then populates the peerChanInfo and peerScores maps. initAccessPerms func() (map[string]channeldb.ChanCount, error) // shouldDisconnect determines whether we should disconnect a peer or @@ -61,9 +61,9 @@ type accessManConfig struct { func newAccessMan(cfg *accessManConfig) (*accessMan, error) { a := &accessMan{ - cfg: cfg, - peerCounts: make(map[string]channeldb.ChanCount), - peerScores: make(map[string]peerSlotStatus), + cfg: cfg, + peerChanInfo: make(map[string]channeldb.ChanCount), + peerScores: make(map[string]peerSlotStatus), } counts, err := a.cfg.initAccessPerms() @@ -71,10 +71,10 @@ func newAccessMan(cfg *accessManConfig) (*accessMan, error) { return nil, err } - // We'll populate the server's peerCounts map with the counts fetched + // We'll populate the server's peerChanInfo map with the counts fetched // via initAccessPerms. Also note that we haven't yet connected to the // peers. - maps.Copy(a.peerCounts, counts) + maps.Copy(a.peerChanInfo, counts) acsmLog.Info("Access Manager initialized") @@ -90,7 +90,7 @@ func (a *accessMan) hasPeer(ctx context.Context, a.banScoreMtx.RLock() defer a.banScoreMtx.RUnlock() - count, found := a.peerCounts[pub] + count, found := a.peerChanInfo[pub] if found { if count.HasOpenOrClosedChan { acsmLog.DebugS(ctx, "Peer has open/closed channel, "+ @@ -223,11 +223,11 @@ func (a *accessMan) newPendingOpenChan(remotePub *btcec.PublicKey) error { case peerStatusTemporary: // If this peer's access status is temporary, we'll need to - // update the peerCounts map. The peer's access status will stay - // temporary. - peerCount, found := a.peerCounts[peerMapKey] + // update the peerChanInfo map. The peer's access status will + // stay temporary. + peerCount, found := a.peerChanInfo[peerMapKey] if !found { - // Error if we did not find any info in peerCounts. + // Error if we did not find any info in peerChanInfo. acsmLog.ErrorS(ctx, "Pending peer info not found", ErrNoPendingPeerInfo) @@ -236,7 +236,7 @@ func (a *accessMan) newPendingOpenChan(remotePub *btcec.PublicKey) error { // Increment the pending channel amount. peerCount.PendingOpenCount += 1 - a.peerCounts[peerMapKey] = peerCount + a.peerChanInfo[peerMapKey] = peerCount acsmLog.DebugS(ctx, "Peer is temporary, incremented "+ "pending count", @@ -246,13 +246,13 @@ func (a *accessMan) newPendingOpenChan(remotePub *btcec.PublicKey) error { // If the peer's access status is restricted, then we can // transition it to a temporary-access peer. We'll need to // update numRestricted and also peerScores. We'll also need to - // update peerCounts. + // update peerChanInfo. peerCount := channeldb.ChanCount{ HasOpenOrClosedChan: false, PendingOpenCount: 1, } - a.peerCounts[peerMapKey] = peerCount + a.peerChanInfo[peerMapKey] = peerCount // A restricted-access slot has opened up. oldRestricted := a.numRestricted @@ -313,12 +313,12 @@ func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error { case peerStatusTemporary: // If this peer is temporary, we need to check if it will // revert to a restricted-access peer. - peerCount, found := a.peerCounts[peerMapKey] + peerCount, found := a.peerChanInfo[peerMapKey] if !found { acsmLog.ErrorS(ctx, "Pending peer info not found", ErrNoPendingPeerInfo) - // Error if we did not find any info in peerCounts. + // Error if we did not find any info in peerChanInfo. return ErrNoPendingPeerInfo } @@ -329,8 +329,8 @@ func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error { "pending_count", currentNumPending) if currentNumPending == 0 { - // Remove the entry from peerCounts. - delete(a.peerCounts, peerMapKey) + // Remove the entry from peerChanInfo. + delete(a.peerChanInfo, peerMapKey) // If this is the only pending-open channel for this // peer and it's getting removed, attempt to demote @@ -370,7 +370,7 @@ func (a *accessMan) newPendingCloseChan(remotePub *btcec.PublicKey) error { // Else, we don't need to demote this peer since it has other // pending-open channels with us. peerCount.PendingOpenCount = currentNumPending - a.peerCounts[peerMapKey] = peerCount + a.peerChanInfo[peerMapKey] = peerCount acsmLog.DebugS(ctx, "Peer still has other pending channels", "pending_count", currentNumPending) @@ -430,9 +430,9 @@ func (a *accessMan) newOpenChan(remotePub *btcec.PublicKey) error { case peerStatusTemporary: // If the peer's state is temporary, we'll upgrade the peer to // a protected peer. - peerCount, found := a.peerCounts[peerMapKey] + peerCount, found := a.peerChanInfo[peerMapKey] if !found { - // Error if we did not find any info in peerCounts. + // Error if we did not find any info in peerChanInfo. acsmLog.ErrorS(ctx, "Pending peer info not found", ErrNoPendingPeerInfo) @@ -442,7 +442,7 @@ func (a *accessMan) newOpenChan(remotePub *btcec.PublicKey) error { peerCount.HasOpenOrClosedChan = true peerCount.PendingOpenCount -= 1 - a.peerCounts[peerMapKey] = peerCount + a.peerChanInfo[peerMapKey] = peerCount newStatus := peerSlotStatus{ state: peerStatusProtected, @@ -503,7 +503,7 @@ func (a *accessMan) checkAcceptIncomingConn(remotePub *btcec.PublicKey) ( a.banScoreMtx.RLock() defer a.banScoreMtx.RUnlock() - _, found := a.peerCounts[peerMapKey] + _, found := a.peerChanInfo[peerMapKey] // Exit early if found. if found { @@ -601,7 +601,7 @@ func (a *accessMan) addPeerAccess(remotePub *btcec.PublicKey, PendingOpenCount: 0, } - a.peerCounts[peerMapKey] = peerCount + a.peerChanInfo[peerMapKey] = peerCount a.peerScores[peerMapKey] = peerSlotStatus{ state: peerStatusTemporary, } diff --git a/accessman_test.go b/accessman_test.go index b2ab79e78..6409d0806 100644 --- a/accessman_test.go +++ b/accessman_test.go @@ -100,22 +100,22 @@ func TestAccessManRestrictedSlots(t *testing.T) { a, err := newAccessMan(cfg) require.NoError(t, err) - // Check that the peerCounts map is correctly populated with three + // Check that the peerChanInfo map is correctly populated with three // peers. require.Equal(t, 0, int(a.numRestricted)) - require.Equal(t, 3, len(a.peerCounts)) + require.Equal(t, 3, len(a.peerChanInfo)) - peerCount1, ok := a.peerCounts[peerKeySer1] + peerCount1, ok := a.peerChanInfo[peerKeySer1] require.True(t, ok) require.True(t, peerCount1.HasOpenOrClosedChan) require.Equal(t, peer1PendingCount, int(peerCount1.PendingOpenCount)) - peerCount2, ok := a.peerCounts[peerKeySer2] + peerCount2, ok := a.peerChanInfo[peerKeySer2] require.True(t, ok) require.True(t, peerCount2.HasOpenOrClosedChan) require.Equal(t, peer2PendingCount, int(peerCount2.PendingOpenCount)) - peerCount3, ok := a.peerCounts[peerKeySer3] + peerCount3, ok := a.peerChanInfo[peerKeySer3] require.True(t, ok) require.False(t, peerCount3.HasOpenOrClosedChan) require.Equal(t, peer3PendingCount, int(peerCount3.PendingOpenCount)) @@ -144,7 +144,7 @@ func TestAccessManRestrictedSlots(t *testing.T) { // Assert that accessman's internal state is updated with peer4. We // expect this new peer to have 1 pending open count. - peerCount4, ok := a.peerCounts[string(peerKey4.SerializeCompressed())] + peerCount4, ok := a.peerChanInfo[string(peerKey4.SerializeCompressed())] require.True(t, ok) require.False(t, peerCount4.HasOpenOrClosedChan) require.Equal(t, 1, int(peerCount4.PendingOpenCount)) @@ -157,7 +157,7 @@ func TestAccessManRestrictedSlots(t *testing.T) { // Assert that accessman's internal state is updated with peer3. We // expect this existing peer to decrement its pending open count and the // flag `HasOpenOrClosedChan` should be true. - peerCount3, ok = a.peerCounts[peerKeySer3] + peerCount3, ok = a.peerChanInfo[peerKeySer3] require.True(t, ok) require.True(t, peerCount3.HasOpenOrClosedChan) require.Equal(t, peer3PendingCount-1, int(peerCount3.PendingOpenCount)) @@ -175,7 +175,7 @@ func TestAccessManRestrictedSlots(t *testing.T) { require.ErrorIs(t, err, ErrNoMoreRestrictedAccessSlots) // Assert that peer4 is removed. - _, ok = a.peerCounts[string(peerKey4.SerializeCompressed())] + _, ok = a.peerChanInfo[string(peerKey4.SerializeCompressed())] require.False(t, ok) } @@ -434,7 +434,7 @@ func TestAssignPeerPermsBypassExisting(t *testing.T) { return peerPriv.PubKey() } - // peer1 exists in `peerCounts` map. + // peer1 exists in `peerChanInfo` map. peer1 := genPeerPub() peer1Str := string(peer1.SerializeCompressed()) @@ -494,27 +494,27 @@ func TestHasPeer(t *testing.T) { // Create a testing accessMan. a := &accessMan{ - peerCounts: make(map[string]channeldb.ChanCount), - peerScores: make(map[string]peerSlotStatus), + peerChanInfo: make(map[string]channeldb.ChanCount), + peerScores: make(map[string]peerSlotStatus), } // peer1 exists with an open channel. peer1 := "peer1" - a.peerCounts[peer1] = channeldb.ChanCount{ + a.peerChanInfo[peer1] = channeldb.ChanCount{ HasOpenOrClosedChan: true, } peer1Access := peerStatusProtected // peer2 exists with a pending channel. peer2 := "peer2" - a.peerCounts[peer2] = channeldb.ChanCount{ + a.peerChanInfo[peer2] = channeldb.ChanCount{ PendingOpenCount: 1, } peer2Access := peerStatusTemporary // peer3 exists without any channels. peer3 := "peer3" - a.peerCounts[peer3] = channeldb.ChanCount{} + a.peerChanInfo[peer3] = channeldb.ChanCount{} peer3Access := peerStatusRestricted // peer4 exists with a score. @@ -560,8 +560,8 @@ func TestAddPeerAccessInbound(t *testing.T) { // Create a testing accessMan. a := &accessMan{ - peerCounts: make(map[string]channeldb.ChanCount), - peerScores: make(map[string]peerSlotStatus), + peerChanInfo: make(map[string]channeldb.ChanCount), + peerScores: make(map[string]peerSlotStatus), } // Create a testing key. @@ -579,7 +579,7 @@ func TestAddPeerAccessInbound(t *testing.T) { // taken, and this peer is not found in the counts map. require.Len(t, a.peerScores, 1) require.Equal(t, int64(1), a.numRestricted) - require.NotContains(t, a.peerCounts, pubStr) + require.NotContains(t, a.peerChanInfo, pubStr) // The peer should be found in the score map. score, ok := a.peerScores[pubStr] @@ -594,12 +594,12 @@ func TestAddPeerAccessInbound(t *testing.T) { // Assert the internal state is not changed. require.Len(t, a.peerScores, 1) require.Equal(t, int64(1), a.numRestricted) - require.NotContains(t, a.peerCounts, pubStr) + require.NotContains(t, a.peerChanInfo, pubStr) // Reset the accessMan. a = &accessMan{ - peerCounts: make(map[string]channeldb.ChanCount), - peerScores: make(map[string]peerSlotStatus), + peerChanInfo: make(map[string]channeldb.ChanCount), + peerScores: make(map[string]peerSlotStatus), } // Add this peer as an inbound peer with peerStatusTemporary. @@ -613,8 +613,8 @@ func TestAddPeerAccessInbound(t *testing.T) { require.Equal(t, int64(0), a.numRestricted) // NOTE: in reality this is not possible as the peer must have been put - // into the map `peerCounts` before its perm can be upgraded. - require.NotContains(t, a.peerCounts, pubStr) + // into the map `peerChanInfo` before its perm can be upgraded. + require.NotContains(t, a.peerChanInfo, pubStr) // The peer should be found in the score map. score, ok = a.peerScores[pubStr] @@ -631,8 +631,8 @@ func TestAddPeerAccessOutbound(t *testing.T) { // Create a testing accessMan. a := &accessMan{ - peerCounts: make(map[string]channeldb.ChanCount), - peerScores: make(map[string]peerSlotStatus), + peerChanInfo: make(map[string]channeldb.ChanCount), + peerScores: make(map[string]peerSlotStatus), } // Create a testing key. @@ -650,7 +650,7 @@ func TestAddPeerAccessOutbound(t *testing.T) { // taken, and this peer is found in the counts map. require.Len(t, a.peerScores, 1) require.Equal(t, int64(0), a.numRestricted) - require.Contains(t, a.peerCounts, pubStr) + require.Contains(t, a.peerChanInfo, pubStr) // The peer should be found in the score map. score, ok := a.peerScores[pubStr] @@ -661,7 +661,7 @@ func TestAddPeerAccessOutbound(t *testing.T) { require.Equal(t, expecedScore, score) // The peer should be found in the peer counts map. - count, ok := a.peerCounts[pubStr] + count, ok := a.peerChanInfo[pubStr] require.True(t, ok) // The peer's count should be initialized correctly. @@ -674,12 +674,12 @@ func TestAddPeerAccessOutbound(t *testing.T) { // Assert the internal state is not changed. require.Len(t, a.peerScores, 1) require.Equal(t, int64(0), a.numRestricted) - require.Contains(t, a.peerCounts, pubStr) + require.Contains(t, a.peerChanInfo, pubStr) // Reset the accessMan. a = &accessMan{ - peerCounts: make(map[string]channeldb.ChanCount), - peerScores: make(map[string]peerSlotStatus), + peerChanInfo: make(map[string]channeldb.ChanCount), + peerScores: make(map[string]peerSlotStatus), } // Add this peer as an inbound peer with peerStatusTemporary. @@ -693,8 +693,8 @@ func TestAddPeerAccessOutbound(t *testing.T) { require.Equal(t, int64(0), a.numRestricted) // NOTE: in reality this is not possible as the peer must have been put - // into the map `peerCounts` before its perm can be upgraded. - require.NotContains(t, a.peerCounts, pubStr) + // into the map `peerChanInfo` before its perm can be upgraded. + require.NotContains(t, a.peerChanInfo, pubStr) // The peer should be found in the score map. score, ok = a.peerScores[pubStr] diff --git a/channeldb/db.go b/channeldb/db.go index b617d1008..42116a535 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -742,7 +742,7 @@ type ChanCount struct { func (c *ChannelStateDB) FetchPermAndTempPeers( chainHash []byte) (map[string]ChanCount, error) { - peerCounts := make(map[string]ChanCount) + peerChanInfo := make(map[string]ChanCount) err := kvdb.View(c.backend, func(tx kvdb.RTx) error { openChanBucket := tx.ReadBucket(openChannelBucket) @@ -828,7 +828,7 @@ func (c *ChannelStateDB) FetchPermAndTempPeers( HasOpenOrClosedChan: isPermPeer, PendingOpenCount: pendingOpenCount, } - peerCounts[string(nodePub)] = peerCount + peerChanInfo[string(nodePub)] = peerCount return nil }) @@ -892,15 +892,15 @@ func (c *ChannelStateDB) FetchPermAndTempPeers( remoteSer := remotePub.SerializeCompressed() remoteKey := string(remoteSer) - count, exists := peerCounts[remoteKey] + count, exists := peerChanInfo[remoteKey] if exists { count.HasOpenOrClosedChan = true - peerCounts[remoteKey] = count + peerChanInfo[remoteKey] = count } else { peerCount := ChanCount{ HasOpenOrClosedChan: true, } - peerCounts[remoteKey] = peerCount + peerChanInfo[remoteKey] = peerCount } } @@ -912,10 +912,10 @@ func (c *ChannelStateDB) FetchPermAndTempPeers( return nil }, func() { - clear(peerCounts) + clear(peerChanInfo) }) - return peerCounts, err + return peerChanInfo, err } // channelSelector describes a function that takes a chain-hash bucket from diff --git a/channeldb/db_test.go b/channeldb/db_test.go index e175ea1fb..98a530457 100644 --- a/channeldb/db_test.go +++ b/channeldb/db_test.go @@ -768,16 +768,16 @@ func TestFetchPermTempPeer(t *testing.T) { ) // Fetch the ChanCount for our peers. - peerCounts, err := cdb.FetchPermAndTempPeers(key[:]) + peerChanInfo, err := cdb.FetchPermAndTempPeers(key[:]) require.NoError(t, err, "unable to fetch perm and temp peers") // There should only be three entries. - require.Len(t, peerCounts, 3) + require.Len(t, peerChanInfo, 3) // The first entry should have OpenClosed set to true and Pending set // to 0. - count1, found := peerCounts[string(pubKey1.SerializeCompressed())] - require.True(t, found, "unable to find peer 1 in peerCounts") + count1, found := peerChanInfo[string(pubKey1.SerializeCompressed())] + require.True(t, found, "unable to find peer 1 in peerChanInfo") require.True( t, count1.HasOpenOrClosedChan, "couldn't find peer 1's channels", @@ -787,15 +787,15 @@ func TestFetchPermTempPeer(t *testing.T) { "peer 1 doesn't have 0 pending-open", ) - count2, found := peerCounts[string(pubKey2.SerializeCompressed())] - require.True(t, found, "unable to find peer 2 in peerCounts") + count2, found := peerChanInfo[string(pubKey2.SerializeCompressed())] + require.True(t, found, "unable to find peer 2 in peerChanInfo") require.False( t, count2.HasOpenOrClosedChan, "found erroneous channels", ) require.Equal(t, uint64(1), count2.PendingOpenCount) - count3, found := peerCounts[string(pubKey3.SerializeCompressed())] - require.True(t, found, "unable to find peer 3 in peerCounts") + count3, found := peerChanInfo[string(pubKey3.SerializeCompressed())] + require.True(t, found, "unable to find peer 3 in peerChanInfo") require.True( t, count3.HasOpenOrClosedChan, "couldn't find peer 3's channels",