Skip to content

Commit 704bb73

Browse files
committed
accessman: only check ban score for restricted peers
If a peer has, or used to have a channel with us there's no need to check for the ban score.
1 parent 7e44422 commit 704bb73

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

accessman.go

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,9 @@ func (a *accessMan) assignPeerPerms(remotePub *btcec.PublicKey) (
9393
// Default is restricted unless the below filters say otherwise.
9494
access := peerStatusRestricted
9595

96-
shouldDisconnect, err := a.cfg.shouldDisconnect(remotePub)
97-
if err != nil {
98-
acsmLog.ErrorS(ctx, "Error checking disconnect status", err)
99-
100-
// Access is restricted here.
101-
return access, err
102-
}
103-
104-
if shouldDisconnect {
105-
acsmLog.WarnS(ctx, "Peer is banned, assigning restricted access",
106-
ErrGossiperBan)
107-
108-
// Access is restricted here.
109-
return access, ErrGossiperBan
110-
}
111-
11296
// Lock banScoreMtx for reading so that we can update the banning maps
11397
// below.
11498
a.banScoreMtx.RLock()
115-
defer a.banScoreMtx.RUnlock()
116-
11799
if count, found := a.peerCounts[peerMapKey]; found {
118100
if count.HasOpenOrClosedChan {
119101
acsmLog.DebugS(ctx, "Peer has open/closed channel, "+
@@ -127,23 +109,45 @@ func (a *accessMan) assignPeerPerms(remotePub *btcec.PublicKey) (
127109
access = peerStatusTemporary
128110
}
129111
}
112+
a.banScoreMtx.RUnlock()
113+
114+
// Exit early if the peer status is no longer restricted.
115+
if access != peerStatusRestricted {
116+
return access, nil
117+
}
118+
119+
// Check whether this peer is banned.
120+
shouldDisconnect, err := a.cfg.shouldDisconnect(remotePub)
121+
if err != nil {
122+
acsmLog.ErrorS(ctx, "Error checking disconnect status", err)
123+
124+
// Access is restricted here.
125+
return access, err
126+
}
127+
128+
if shouldDisconnect {
129+
acsmLog.WarnS(ctx, "Peer is banned, assigning restricted access",
130+
ErrGossiperBan)
131+
132+
// Access is restricted here.
133+
return access, ErrGossiperBan
134+
}
130135

131136
// If we've reached this point and access hasn't changed from
132137
// restricted, then we need to check if we even have a slot for this
133138
// peer.
134-
if access == peerStatusRestricted {
135-
acsmLog.DebugS(ctx, "Peer has no channels, assigning "+
136-
"restricted access")
139+
acsmLog.DebugS(ctx, "Peer has no channels, assigning restricted access")
137140

138-
if a.numRestricted >= a.cfg.maxRestrictedSlots {
139-
acsmLog.WarnS(ctx, "No more restricted slots "+
140-
"available, denying peer",
141-
ErrNoMoreRestrictedAccessSlots,
142-
"num_restricted", a.numRestricted,
143-
"max_restricted", a.cfg.maxRestrictedSlots)
141+
a.banScoreMtx.RLock()
142+
defer a.banScoreMtx.RUnlock()
144143

145-
return access, ErrNoMoreRestrictedAccessSlots
146-
}
144+
if a.numRestricted >= a.cfg.maxRestrictedSlots {
145+
acsmLog.WarnS(ctx, "No more restricted slots available, "+
146+
"denying peer", ErrNoMoreRestrictedAccessSlots,
147+
"num_restricted", a.numRestricted, "max_restricted",
148+
a.cfg.maxRestrictedSlots)
149+
150+
return access, ErrNoMoreRestrictedAccessSlots
147151
}
148152

149153
return access, nil

0 commit comments

Comments
 (0)