Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: using slices.Contains to simplify the code #6234

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions cmd/algons/dnsCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"os"
"regexp"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -439,13 +440,7 @@ func checkedDelete(toDelete []cloudflare.DNSRecordResponseEntry, cloudflareDNS *

func getEntries(getNetwork string, recordType string) ([]cloudflare.DNSRecordResponseEntry, error) {
recordTypes := []string{"A", "CNAME", "SRV", "TXT"}
isKnown := false
for _, known := range append(recordTypes, "") {
if recordType == known {
isKnown = true
break
}
}
isKnown := slices.Contains(recordTypes, recordType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful. The loop was iterating over append(recordTypes, ""). I guess blank matters here.

if !isKnown {
return nil, fmt.Errorf("invalid recordType specified %s", recordType)
}
Expand Down
10 changes: 2 additions & 8 deletions network/requestTracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
"net"
"net/http"
"net/textproto"
"slices"
"sort"
"strings"
"sync/atomic"
"time"

"github.com/algorand/go-deadlock"

"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/logging/telemetryspec"
Expand Down Expand Up @@ -218,11 +217,11 @@

hostRequests hostsIncomingMap // maps a request host to a request data (i.e. "1.2.3.4" -> *hostIncomingRequests )
acceptedConnections map[net.Addr]*TrackerRequest // maps a local address interface to a tracked request data (i.e. "1.2.3.4:1560" -> *TrackerRequest ); used to associate connection between the Accept and the ServeHTTP
hostRequestsMu deadlock.Mutex // used to syncronize access to the hostRequests and acceptedConnections variables

Check failure on line 220 in network/requestTracker.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 undefined: deadlock Raw Output: network/requestTracker.go:220:22: undefined: deadlock

Check failure on line 220 in network/requestTracker.go

View workflow job for this annotation

GitHub Actions / Performance regression check

undefined: deadlock

Check failure on line 220 in network/requestTracker.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: deadlock

httpHostRequests hostsIncomingMap // maps a request host to a request data (i.e. "1.2.3.4" -> *hostIncomingRequests )
httpConnections map[net.Addr]*TrackerRequest // maps a local address interface to a tracked request data (i.e. "1.2.3.4:1560" -> *TrackerRequest ); used to associate connection between the Accept and the ServeHTTP
httpConnectionsMu deadlock.Mutex // used to syncronize access to the httpHostRequests and httpConnections variables

Check failure on line 224 in network/requestTracker.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 undefined: deadlock) (typecheck) Raw Output: network/requestTracker.go:224:20: undefined: deadlock) (typecheck) "github.com/algorand/go-algorand/network" ^

Check failure on line 224 in network/requestTracker.go

View workflow job for this annotation

GitHub Actions / Performance regression check

undefined: deadlock

Check failure on line 224 in network/requestTracker.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: deadlock
}

// makeRequestsTracker creates a request tracker object.
Expand Down Expand Up @@ -519,10 +518,5 @@

// isLocalhost returns true if the given host is a localhost address.
func isLocalhost(host string) bool {
for _, v := range []string{"localhost", "127.0.0.1", "[::1]", "::1", "[::]"} {
if host == v {
return true
}
}
return false
return slices.Contains([]string{"localhost", "127.0.0.1", "[::1]", "::1", "[::]"}, host)
}
10 changes: 2 additions & 8 deletions network/wsPeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
"net"
"net/http"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/algorand/go-deadlock"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that dropping this import is why you're getting the deadlock complaints.

"github.com/algorand/websocket"

"github.com/algorand/go-algorand/config"
Expand All @@ -52,13 +52,7 @@
const msgsInReadBufferPerPeer = 10

func init() {
matched := false
for _, version := range SupportedProtocolVersions {
if version == versionPeerFeatures {
matched = true
}
}
if !matched {
if !slices.Contains(SupportedProtocolVersions, versionPeerFeatures) {
panic(fmt.Sprintf("peer features version %s is not supported %v", versionPeerFeatures, SupportedProtocolVersions))
}

Expand Down Expand Up @@ -252,7 +246,7 @@
responseChannels map[uint64]chan *Response

// responseChannelsMutex guards the operations of responseChannels
responseChannelsMutex deadlock.RWMutex

Check failure on line 249 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 undefined: deadlock Raw Output: network/wsPeer.go:249:24: undefined: deadlock

Check failure on line 249 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 undefined: deadlock Raw Output: network/wsPeer.go:249:24: undefined: deadlock

Check failure on line 249 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / Performance regression check

undefined: deadlock

Check failure on line 249 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: deadlock

// sendMessageTag is a map of allowed message to send to a peer. We don't use any synchronization on this map, and the
// only guarantee is that it's being accessed only during startup and/or by the sending loop go routine.
Expand All @@ -278,12 +272,12 @@
clientDataStore map[string]interface{}

// clientDataStoreMu synchronizes access to clientDataStore
clientDataStoreMu deadlock.Mutex

Check failure on line 275 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 undefined: deadlock Raw Output: network/wsPeer.go:275:20: undefined: deadlock

Check failure on line 275 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 undefined: deadlock Raw Output: network/wsPeer.go:275:20: undefined: deadlock

Check failure on line 275 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / Performance regression check

undefined: deadlock

Check failure on line 275 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: deadlock

// closers is a slice of functions to run when the peer is closed
closers []func()
// closersMu synchronizes access to closers
closersMu deadlock.RWMutex

Check failure on line 280 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 undefined: deadlock Raw Output: network/wsPeer.go:280:12: undefined: deadlock

Check failure on line 280 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / Performance regression check

undefined: deadlock

Check failure on line 280 in network/wsPeer.go

View workflow job for this annotation

GitHub Actions / build-windows

undefined: deadlock

// peerType defines the peer's underlying connection type
// used for separate p2p vs ws metrics
Expand Down
9 changes: 4 additions & 5 deletions shared/pingpong/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/rand"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -1003,10 +1004,8 @@ func (pps *WorkerState) generateAccounts() {
}

func uniqueAppend(they []string, x string) []string {
for _, v := range they {
if v == x {
return they
}
if !slices.Contains(they, x) {
return append(they, x)
}
return append(they, x)
return they
Comment on lines +1007 to +1010
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer swapping the return lines and removing the ! in the predicate. All else being equal, I prefer to avoid negation in predicates.

}
10 changes: 2 additions & 8 deletions shared/pingpong/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"os"
"slices"
"time"

"github.com/algorand/go-algorand/util/codecs"
Expand Down Expand Up @@ -192,14 +193,7 @@ var accountSampleMethods = []string{

// Check returns an error if config is invalid.
func (cfg *PpConfig) Check() error {
sampleOk := false
for _, v := range accountSampleMethods {
if v == cfg.GeneratedAccountSampleMethod {
sampleOk = true
break
}
}
if !sampleOk {
if !slices.Contains(accountSampleMethods, cfg.GeneratedAccountSampleMethod) {
return fmt.Errorf("unknown GeneratedAccountSampleMethod: %s", cfg.GeneratedAccountSampleMethod)
}
if cfg.DeterministicKeys && (cfg.GeneratedAccountsOffset+uint64(cfg.NumPartAccounts) > cfg.GeneratedAccountsCount) {
Expand Down
10 changes: 2 additions & 8 deletions tools/block-generator/generator/generator_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/binary"
"fmt"
"os"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -333,14 +334,7 @@ func (g *generator) introspectLedgerVsGenerator(roundNumber, intra uint64) (errs
generatorExpectedOptinsNotFound := map[uint64][]uint64{}
for appId, appOptins := range expectedOptins[appKindBoxes] {
for optin := range appOptins {
missing := true
for _, boxOptin := range ledgerBoxEvidence[appId] {
if boxOptin == optin {
missing = false
break
}
}
if missing {
if !slices.Contains(ledgerBoxEvidence[appId], optin) {
generatorExpectedOptinsNotFound[appId] = append(generatorExpectedOptinsNotFound[appId], optin)
}
}
Expand Down
12 changes: 2 additions & 10 deletions util/codecs/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"os"
"reflect"
"slices"
"strings"
)

Expand Down Expand Up @@ -123,7 +124,7 @@ func WriteNonDefaultValues(writer io.Writer, object, defaultObject interface{},
return fmt.Errorf("error processing serialized object - should be at EOF: %s", line)
}

if inStringArray(valName, ignore) {
if slices.Contains(ignore, valName) {
newFile[newIndex] = line
newIndex++
continue
Expand Down Expand Up @@ -183,15 +184,6 @@ func extractValueName(line string) (name string) {
return line[start+1 : end]
}

func inStringArray(item string, set []string) bool {
for _, s := range set {
if item == s {
return true
}
}
return false
}

func createValueMap(object interface{}) map[string]interface{} {
valueMap := make(map[string]interface{})

Expand Down
Loading