Skip to content

Commit 67f5906

Browse files
Merge pull request #379 from jinjingroad/master
refactor: use slices.Contains to simplify code
2 parents 5af8b59 + 898d9d7 commit 67f5906

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

policy/policy.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"regexp"
12+
"slices"
1213
"sync"
1314

1415
"github.com/jessevdk/go-flags"
@@ -148,24 +149,14 @@ func (p *Policy) IsPeerAllowed(peer string) bool {
148149
if p.AcceptAllPeers {
149150
return true
150151
}
151-
for _, allowedPeer := range p.PeerAllowlist {
152-
if peer == allowedPeer {
153-
return true
154-
}
155-
}
156-
return false
152+
return slices.Contains(p.PeerAllowlist, peer)
157153
}
158154

159155
// IsPeerSuspicious returns true if the peer is on the list of suspicious peers.
160156
func (p *Policy) IsPeerSuspicious(peer string) bool {
161157
mu.Lock()
162158
defer mu.Unlock()
163-
for _, suspiciousPeer := range p.SuspiciousPeerList {
164-
if peer == suspiciousPeer {
165-
return true
166-
}
167-
}
168-
return false
159+
return slices.Contains(p.SuspiciousPeerList, peer)
169160
}
170161

171162
// ReloadFile reloads and sets the policy
@@ -247,10 +238,8 @@ func (p *Policy) AddToAllowlist(pubkey string) error {
247238
mu.Lock()
248239
defer mu.Unlock()
249240

250-
for _, v := range p.PeerAllowlist {
251-
if v == pubkey {
252-
return errors.New("peer is already whitelisted")
253-
}
241+
if slices.Contains(p.PeerAllowlist, pubkey) {
242+
return errors.New("peer is already whitelisted")
254243
}
255244
if p.path == "" {
256245
return ErrNoPolicyFile
@@ -274,10 +263,8 @@ func (p *Policy) AddToSuspiciousPeerList(pubkey string) error {
274263
mu.Lock()
275264
defer mu.Unlock()
276265

277-
for _, v := range p.SuspiciousPeerList {
278-
if v == pubkey {
279-
return errors.New("peer is already marked as suspicious")
280-
}
266+
if slices.Contains(p.SuspiciousPeerList, pubkey) {
267+
return errors.New("peer is already marked as suspicious")
281268
}
282269
if p.path == "" {
283270
return ErrNoPolicyFile

0 commit comments

Comments
 (0)