Skip to content

Commit

Permalink
chore(x/vote,x/tss,x/snapshot): replace fmt.Errorf with `errors.New…
Browse files Browse the repository at this point in the history
…` when there is no parameters (#2159)

Co-authored-by: Christian Gorenflo <[email protected]>
  • Loading branch information
yukionfire and cgorenflo authored Jul 30, 2024
1 parent 25482b4 commit 375cc31
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 49 deletions.
5 changes: 3 additions & 2 deletions x/snapshot/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -95,12 +96,12 @@ func GetCmdSendTokens() *cobra.Command {
}

if decCoins.Len() != 1 {
return fmt.Errorf("only a single amount is permitted")
return errors.New("only a single amount is permitted")
}

coins, decimals := decCoins.TruncateDecimal()
if !decimals.IsZero() {
return fmt.Errorf("amount must be an integer value")
return errors.New("amount must be an integer value")
}

inputs := make([]banktypes.Input, 0)
Expand Down
14 changes: 7 additions & 7 deletions x/snapshot/exported/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package exported

import (
"bytes"
"fmt"
"errors"
"sort"
"time"

Expand Down Expand Up @@ -44,19 +44,19 @@ func NewSnapshot(timestamp time.Time, height int64, participants []Participant,
// ValidateBasic returns an error if the given snapshot is invalid; nil otherwise
func (m Snapshot) ValidateBasic() error {
if len(m.Participants) == 0 {
return fmt.Errorf("snapshot cannot have no participant")
return errors.New("snapshot cannot have no participant")
}

if m.BondedWeight.IsZero() {
return fmt.Errorf("snapshot must have bonded weight >0")
return errors.New("snapshot must have bonded weight >0")
}

if m.Height <= 0 {
return fmt.Errorf("snapshot must have height >0")
return errors.New("snapshot must have height >0")
}

if m.Timestamp.IsZero() {
return fmt.Errorf("snapshot must have timestamp >0")
return errors.New("snapshot must have timestamp >0")
}

for addr, p := range m.Participants {
Expand All @@ -65,12 +65,12 @@ func (m Snapshot) ValidateBasic() error {
}

if addr != p.Address.String() {
return fmt.Errorf("invalid snapshot")
return errors.New("invalid snapshot")
}
}

if m.GetParticipantsWeight().GT(m.BondedWeight) {
return fmt.Errorf("snapshot cannot have sum of participants weight greater than bonded weight")
return errors.New("snapshot cannot have sum of participants weight greater than bonded weight")
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion x/snapshot/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"bytes"
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (k Keeper) GetMinProxyBalance(ctx sdk.Context) sdk.Int {
// The proxy will be marked as active and to be included in the next snapshot by default
func (k Keeper) ActivateProxy(ctx sdk.Context, operator sdk.ValAddress, proxy sdk.AccAddress) error {
if bytes.Equal(operator, proxy) {
return fmt.Errorf("proxy address cannot be the same as the operator address")
return errors.New("proxy address cannot be the same as the operator address")
}

if existing, ok := k.getProxiedValidator(ctx, operator); ok && !existing.Proxy.Equals(proxy) {
Expand Down
4 changes: 2 additions & 2 deletions x/snapshot/types/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
"fmt"
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -27,7 +27,7 @@ func (m ProxiedValidator) Validate() error {
}

if m.Validator.Equals(m.Proxy) {
return fmt.Errorf("validator cannot be the same as proxy")
return errors.New("validator cannot be the same as proxy")
}

return nil
Expand Down
15 changes: 8 additions & 7 deletions x/tss/exported/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exported

import (
"crypto/ecdsa"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -118,35 +119,35 @@ func (m KeyRequirement) Validate() error {
}

if m.MinKeygenThreshold.Validate() != nil {
return fmt.Errorf("MinKeygenThreshold must be <=1 and >0")
return errors.New("MinKeygenThreshold must be <=1 and >0")
}

if m.SafetyThreshold.Validate() != nil {
return fmt.Errorf("SafetyThreshold must be <=1 and >0")
return errors.New("SafetyThreshold must be <=1 and >0")
}

if err := m.KeyShareDistributionPolicy.Validate(); err != nil {
return err
}

if m.KeygenVotingThreshold.Validate() != nil {
return fmt.Errorf("KeygenVotingThreshold must be <=1 and >0")
return errors.New("KeygenVotingThreshold must be <=1 and >0")
}

if m.SignVotingThreshold.Validate() != nil {
return fmt.Errorf("SignVotingThreshold must be <=1 and >0")
return errors.New("SignVotingThreshold must be <=1 and >0")
}

if m.KeygenTimeout <= 0 {
return fmt.Errorf("KeygenTimeout must be >0")
return errors.New("KeygenTimeout must be >0")
}

if m.SignTimeout <= 0 {
return fmt.Errorf("SignTimeout must be >0")
return errors.New("SignTimeout must be >0")
}

if m.MinTotalShareCount <= 0 || m.MinTotalShareCount > m.MaxTotalShareCount {
return fmt.Errorf("must satisfy 0 < MinTotalShareCount <= MaxTotalShareCount")
return errors.New("must satisfy 0 < MinTotalShareCount <= MaxTotalShareCount")
}

for totalShareCount := m.MinTotalShareCount; totalShareCount <= m.MaxTotalShareCount; totalShareCount++ {
Expand Down
15 changes: 8 additions & 7 deletions x/tss/tofnd/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tofnd

import (
"errors"
"fmt"

"github.com/btcsuite/btcd/btcec/v2"
Expand All @@ -11,7 +12,7 @@ import (
// Validate checks if the criminal list is valid
func (m *MessageOut_CriminalList) Validate() error {
if len(m.Criminals) == 0 {
return fmt.Errorf("missing criminals")
return errors.New("missing criminals")
}

criminalSeen := make(map[string]bool)
Expand All @@ -31,7 +32,7 @@ func (m *MessageOut_CriminalList) Validate() error {
}

if i < len(m.Criminals)-1 && !m.Less(i, i+1) {
return fmt.Errorf("criminals have to be sorted in ascending order")
return errors.New("criminals have to be sorted in ascending order")
}

criminalSeen[criminal.String()] = true
Expand All @@ -58,23 +59,23 @@ func (m *MessageOut_SignResult) Validate() error {
return nil
}

return fmt.Errorf("missing signature or criminals")
return errors.New("missing signature or criminals")
}

// Validate checks if the keygen result is valid
func (m *MessageOut_KeygenResult) Validate() error {
if keygenData := m.GetData(); keygenData != nil {
pubKeyBytes := keygenData.GetPubKey()
if pubKeyBytes == nil {
return fmt.Errorf("pubkey is nil")
return errors.New("pubkey is nil")
}
groupRecoverInfo := keygenData.GetGroupRecoverInfo()
if groupRecoverInfo == nil {
return fmt.Errorf("group recovery info is nil")
return errors.New("group recovery info is nil")
}
privateRecoverInfo := keygenData.GetPrivateRecoverInfo()
if privateRecoverInfo == nil {
return fmt.Errorf("private recovery info is nil")
return errors.New("private recovery info is nil")
}
_, err := btcec.ParsePubKey(pubKeyBytes)
if err != nil {
Expand All @@ -92,7 +93,7 @@ func (m *MessageOut_KeygenResult) Validate() error {
return nil
}

return fmt.Errorf("missing pubkey or criminals")
return errors.New("missing pubkey or criminals")
}

// Len returns the number of criminals in the criminal list
Expand Down
4 changes: 2 additions & 2 deletions x/tss/types/msg_submit_multisig_pubkey.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
"fmt"
"errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -42,7 +42,7 @@ func (m SubmitMultisigPubKeysRequest) ValidateBasic() error {
return nil
}
if seen[string(info.PubKey)] {
return fmt.Errorf("duplicate key")
return errors.New("duplicate key")
}
seen[string(info.PubKey)] = true
}
Expand Down
9 changes: 5 additions & 4 deletions x/tss/types/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"errors"
"fmt"

params "github.com/cosmos/cosmos-sdk/x/params/types"
Expand Down Expand Up @@ -173,7 +174,7 @@ func validateKeyRequirements(keyRequirements interface{}) error {
for _, keyRequirement := range val {
key := fmt.Sprintf("%s_%s", keyRequirement.KeyRole.SimpleString(), keyRequirement.KeyType.SimpleString())
if keyRoleSeen[key] {
return fmt.Errorf("duplicate key role and key type found in KeyRequirements")
return errors.New("duplicate key role and key type found in KeyRequirements")
}

if err := keyRequirement.Validate(); err != nil {
Expand All @@ -193,7 +194,7 @@ func validateSuspendDurationInBlocks(suspendDurationInBlocks interface{}) error
}

if val <= 0 {
return fmt.Errorf("SuspendDurationInBlocks must be a positive integer")
return errors.New("SuspendDurationInBlocks must be a positive integer")
}

return nil
Expand Down Expand Up @@ -221,7 +222,7 @@ func validateMaxMissedBlocksPerWindow(maxMissedBlocksPerWindow interface{}) erro
}

if val.Validate() != nil {
return fmt.Errorf("MaxMissedBlocksPerWindow threshold must be >0 and <=1")
return errors.New("MaxMissedBlocksPerWindow threshold must be >0 and <=1")
}

return nil
Expand All @@ -234,7 +235,7 @@ func validateExternalMultisigThreshold(externalMultisigThreshold interface{}) er
}

if t.Validate() != nil {
return fmt.Errorf("ExternalMultisigThreshold must be >0 and <=1")
return errors.New("ExternalMultisigThreshold must be >0 and <=1")
}

return nil
Expand Down
19 changes: 10 additions & 9 deletions x/vote/exported/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exported

import (
"errors"
"fmt"
"strconv"

Expand Down Expand Up @@ -137,43 +138,43 @@ func (builder PollBuilder) Build(blockHeight int64) (PollMetadata, error) {
// ValidateBasic returns an error if the poll metadata is not valid; nil otherwise
func (m PollMetadata) ValidateBasic() error {
if len(m.Module) == 0 {
return fmt.Errorf("module must be set")
return errors.New("module must be set")
}

if m.ExpiresAt <= 0 {
return fmt.Errorf("expires at must be >0")
return errors.New("expires at must be >0")
}

if m.CompletedAt < 0 {
return fmt.Errorf("completed at must be >=0")
return errors.New("completed at must be >=0")
}

if m.VotingThreshold.LTE(utils.ZeroThreshold) || m.VotingThreshold.GT(utils.OneThreshold) {
return fmt.Errorf("voting threshold must be >0 and <=1")
return errors.New("voting threshold must be >0 and <=1")
}

if m.Is(Completed) == (m.Result == nil) {
return fmt.Errorf("completed poll must have result set")
return errors.New("completed poll must have result set")
}

if m.Is(Completed) == (m.CompletedAt <= 0) {
return fmt.Errorf("completed poll must have CompletedAt set and non-completed poll must not")
return errors.New("completed poll must have CompletedAt set and non-completed poll must not")
}

if m.Is(NonExistent) {
return fmt.Errorf("state cannot be non-existent")
return errors.New("state cannot be non-existent")
}

if m.MinVoterCount < 0 || m.MinVoterCount > int64(len(m.Snapshot.Participants)) {
return fmt.Errorf("invalid min voter count")
return errors.New("invalid min voter count")
}

if err := m.Snapshot.ValidateBasic(); err != nil {
return err
}

if m.Snapshot.GetParticipantsWeight().LT(m.Snapshot.CalculateMinPassingWeight(m.VotingThreshold)) {
return fmt.Errorf("invalid voting threshold")
return errors.New("invalid voting threshold")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions x/vote/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package keeper

import (
"fmt"
"errors"
"testing"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -62,7 +62,7 @@ func initializeRandomPoll(ctx sdk.Context, keeper Keeper) exported.PollMetadata

metadata, ok := keeper.getPollMetadata(ctx, pollID)
if !ok {
panic(fmt.Errorf("poll metadata not found"))
panic(errors.New("poll metadata not found"))
}

metadata.State = rand.Of(exported.Completed, exported.Failed)
Expand Down
3 changes: 2 additions & 1 deletion x/vote/keeper/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/hex"
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -100,7 +101,7 @@ func (p poll) GetState() exported.PollState {
// Vote records the given vote
func (p *poll) Vote(voter sdk.ValAddress, blockHeight int64, data codec.ProtoMarshaler) (exported.VoteResult, error) {
if p.Is(exported.NonExistent) {
return exported.NoVote, fmt.Errorf("poll does not exist")
return exported.NoVote, errors.New("poll does not exist")
}

if p.HasVoted(voter) {
Expand Down
Loading

0 comments on commit 375cc31

Please sign in to comment.