diff --git a/cmd/cli/editor.go b/cmd/cli/editor.go index 90bb1e4c8..48543b8bd 100644 --- a/cmd/cli/editor.go +++ b/cmd/cli/editor.go @@ -1,6 +1,7 @@ package main import ( + "cmp" "os" "os/exec" "strings" @@ -16,13 +17,7 @@ type PreferredEditorResolver func() string // GetPreferredEditorFromEnvironment returns the user's editor as defined by the `$EDITOR` environment variable, or // the `DefaultEditor` if it is not set. func GetPreferredEditorFromEnvironment() string { - editor := os.Getenv("EDITOR") - - if editor == "" { - return DefaultEditor - } - - return editor + return cmp.Or(os.Getenv("EDITOR"), DefaultEditor) } func resolveEditorArguments(executable, filename string) []string { diff --git a/db/metadb/metadb.go b/db/metadb/metadb.go index 19e96003e..3ab760eab 100644 --- a/db/metadb/metadb.go +++ b/db/metadb/metadb.go @@ -1,6 +1,7 @@ package metadb import ( + "cmp" "fmt" "os" "testing" @@ -39,11 +40,7 @@ func New(typ, dir string) (db.Database, error) { } func ForTest() (typ string) { - dbType := os.Getenv("DVOTE_DB_TYPE") - if dbType == "" { - dbType = "pebble" // default to Pebble, just like vocdoninode - } - return dbType + return cmp.Or(os.Getenv("DVOTE_DB_TYPE"), "pebble") // default to Pebble, just like vocdoninode } func NewTest(tb testing.TB) db.Database { diff --git a/log/log.go b/log/log.go index a4c5fc020..48d6e8f8c 100644 --- a/log/log.go +++ b/log/log.go @@ -2,6 +2,7 @@ package log import ( "bytes" + "cmp" "fmt" "io" "os" @@ -33,11 +34,7 @@ func init() { // environment variable can be set globally even when running tests. // Always initializing the logger is also useful to avoid panics when // logging if the logger is nil. - level := "error" - if s := os.Getenv("LOG_LEVEL"); s != "" { - level = s - } - Init(level, "stderr", nil) + Init(cmp.Or(os.Getenv("LOG_LEVEL"), "error"), "stderr", nil) } // Logger provides access to the global logger (zerolog). diff --git a/vochain/cometbft.go b/vochain/cometbft.go index 9b56249fa..d78aa585f 100644 --- a/vochain/cometbft.go +++ b/vochain/cometbft.go @@ -2,12 +2,13 @@ package vochain import ( "bytes" + "cmp" "context" "encoding/hex" "encoding/json" "errors" "fmt" - "sort" + "slices" "time" cometabcitypes "github.com/cometbft/cometbft/abci/types" @@ -382,20 +383,17 @@ func (app *BaseApplication) PrepareProposal(ctx context.Context, } // Sort the transactions based on the sender's address and nonce - sort.Slice(validTxInfos, func(i, j int) bool { - if validTxInfos[i].Addr == nil && validTxInfos[j].Addr != nil { - return true + slices.SortFunc(validTxInfos, func(a, b txInfo) int { + // If both addresses are equal, compare by nonce. + if a.Addr == nil && b.Addr == nil { + } else if a.Addr == nil { + return -1 // a < b when only a is nil + } else if b.Addr == nil { + return 1 // a > b when only b is nil + } else if c := a.Addr.Cmp(*b.Addr); c != 0 { + return c } - if validTxInfos[i].Addr != nil && validTxInfos[j].Addr == nil { - return false - } - if validTxInfos[i].Addr != nil && validTxInfos[j].Addr != nil { - if bytes.Equal(validTxInfos[i].Addr.Bytes(), validTxInfos[j].Addr.Bytes()) { - return validTxInfos[i].Nonce < validTxInfos[j].Nonce - } - return bytes.Compare(validTxInfos[i].Addr.Bytes(), validTxInfos[j].Addr.Bytes()) == -1 - } - return false + return cmp.Compare(a.Nonce, b.Nonce) }) // Check the validity of the transactions diff --git a/vochain/indexer/indexer.go b/vochain/indexer/indexer.go index aa336fdea..85d462407 100644 --- a/vochain/indexer/indexer.go +++ b/vochain/indexer/indexer.go @@ -10,7 +10,7 @@ import ( "math/big" "os" "path/filepath" - "sort" + "slices" "strings" "sync" "time" @@ -384,7 +384,7 @@ func (idx *Indexer) Commit(height uint32) error { // Update existing processes updateProcs := maps.Keys(idx.blockUpdateProcs) - sort.Strings(updateProcs) + slices.Sort(updateProcs) queries := idx.blockTxQueries() ctx := context.TODO() diff --git a/vochain/state/sik.go b/vochain/state/sik.go index ac6d70723..9e1b0e3e3 100644 --- a/vochain/state/sik.go +++ b/vochain/state/sik.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "math/big" + "slices" "github.com/ethereum/go-ethereum/common" "go.vocdoni.io/dvote/db" @@ -139,7 +140,7 @@ func (v *State) InvalidateSIK(address common.Address) error { func (v *State) ValidSIKRoots() [][]byte { v.mtxValidSIKRoots.Lock() defer v.mtxValidSIKRoots.Unlock() - return append([][]byte{}, v.validSIKRoots...) + return slices.Clone(v.validSIKRoots) } // FetchValidSIKRoots updates the list of current valid SIK roots in the current diff --git a/vochain/state/votes.go b/vochain/state/votes.go index 26c012f49..e5c7fc088 100644 --- a/vochain/state/votes.go +++ b/vochain/state/votes.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "math/big" + "slices" "go.vocdoni.io/dvote/crypto/ethereum" "go.vocdoni.io/dvote/db" @@ -82,11 +83,11 @@ func (v *Vote) Hash() []byte { // DeepCopy returns a deep copy of the Vote struct. func (v *Vote) DeepCopy() *Vote { voteCopy := &Vote{ - ProcessID: append([]byte{}, v.ProcessID...), - Nullifier: append([]byte{}, v.Nullifier...), + ProcessID: slices.Clone(v.ProcessID), + Nullifier: slices.Clone(v.Nullifier), Height: v.Height, - VotePackage: append([]byte{}, v.VotePackage...), - EncryptionKeyIndexes: append([]uint32{}, v.EncryptionKeyIndexes...), + VotePackage: slices.Clone(v.VotePackage), + EncryptionKeyIndexes: slices.Clone(v.EncryptionKeyIndexes), Weight: new(big.Int).Set(v.Weight), VoterID: v.VoterID, Overwrites: v.Overwrites,