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

style: update golangci config and fix lint errors #1384

Merged
merged 19 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 6 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
run:
tests: false
# # timeout for analysis, e.g. 30s, 5m, default is 1m
# timeout: 5m
timeout: 5m

linters:
disable-all: true
Expand All @@ -11,22 +9,22 @@ linters:
- depguard
- dogsled
- errcheck
- exportloopref
- goconst
- gocritic
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- maligned
# - maligned
- misspell
- nakedret
- prealloc
- scopelint
- revive
- staticcheck
- structcheck
# - structcheck
- stylecheck
- typecheck
- unconvert
Expand Down Expand Up @@ -68,4 +66,4 @@ linters-settings:
allow-unused: false
allow-leading-space: true
require-explanation: false
require-specific: false
require-specific: false
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ generate:
###############################################################################

lint:
golangci-lint run --out-format=tab
# TODO: use find and run after fixing lint errors and warnings per module
# @find . -name 'go.mod' -type f -execdir golangci-lint run --out-format=tab \;
@golangci-lint run --out-format=tab
@cd ./types && golangci-lint run --out-format=tab
@cd ./x/data && golangci-lint run --out-format=tab
@cd ./x/ecocredit && golangci-lint run --out-format=tab
protolint .

lint-fix:
Expand Down
5 changes: 2 additions & 3 deletions app/client/cli/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,9 @@ func calculateIP(ip string, i int) (string, error) {
}

func writeFile(name string, dir string, contents []byte) error {
writePath := filepath.Join(dir)
file := filepath.Join(writePath, name)
file := filepath.Join(dir, name)

err := tmos.EnsureDir(writePath, 0755)
err := tmos.EnsureDir(dir, 0755)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions types/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ func TestCheckDuplicateKey(t *testing.T) {
}

for _, tc := range testCases {
input, expErr, expErrMsg := tc.input, tc.expErr, tc.expErrMsg
t.Run(tc.name, func(t *testing.T) {
err := CheckDuplicateKey(json.NewDecoder(strings.NewReader(tc.input)), nil)
if tc.expErr {
err := CheckDuplicateKey(json.NewDecoder(strings.NewReader(input)), nil)
if expErr {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expErrMsg)
require.Contains(t, err.Error(), expErrMsg)
} else {
require.NoError(t, err)
}
Expand Down
9 changes: 5 additions & 4 deletions types/math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"math/big"

sdkmath "cosmossdk.io/math"

"github.com/cockroachdb/apd/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
)

Expand Down Expand Up @@ -206,9 +207,9 @@ func (x Dec) BigInt() (*big.Int, error) {
return z, nil
}

// SdkIntTrim rounds decimal number to the integer towards zero and converts it to `sdk.Int`.
// SdkIntTrim rounds decimal number to the integer towards zero and converts it to `sdkmath.Int`.
// Panics if x is bigger the SDK Int max value
func (x Dec) SdkIntTrim() sdk.Int {
func (x Dec) SdkIntTrim() sdkmath.Int {
y, _ := x.Reduce()
var r = y.dec.Coeff
if y.dec.Exponent != 0 {
Expand All @@ -224,7 +225,7 @@ func (x Dec) SdkIntTrim() sdk.Int {
if x.dec.Negative {
r.Neg(&r)
}
return sdk.NewIntFromBigInt(&r)
return sdkmath.NewIntFromBigInt(&r)
}

func (x Dec) String() string {
Expand Down
10 changes: 5 additions & 5 deletions types/math/dec_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package math
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkmath "cosmossdk.io/math"
)

func BenchmarkSdkIntTrim(b *testing.B) {
Expand Down Expand Up @@ -33,7 +33,7 @@ func BenchmarkSdkIntTrim(b *testing.B) {

}

func sdkIntTrimQuo(d Dec) sdk.Int {
func sdkIntTrimQuo(d Dec) sdkmath.Int {
d, err := d.QuoInteger(NewDecFromInt64(1))
if err != nil {
panic(err)
Expand All @@ -43,17 +43,17 @@ func sdkIntTrimQuo(d Dec) sdk.Int {
if err != nil {
panic(err)
}
return sdk.NewIntFromBigInt(i)
return sdkmath.NewIntFromBigInt(i)
}

func sdkIntTrimNaive(d Dec) sdk.Int {
func sdkIntTrimNaive(d Dec) sdkmath.Int {
d, err := d.QuoInteger(NewDecFromInt64(1))
if err != nil {
panic(err)
}

s := d.String()
i, ok := sdk.NewIntFromString(s)
i, ok := sdkmath.NewIntFromString(s)
if !ok {
panic("can't convert from string")
}
Expand Down
13 changes: 7 additions & 6 deletions types/math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestDec(t *testing.T) {
t.Run("TestMulQuoExact", rapid.MakeCheck(testMulQuoExact))
t.Run("TestQuoMulExact", rapid.MakeCheck(testQuoMulExact))

// Properties about comparision and equality
// Properties about comparison and equality
t.Run("TestCmpInverse", rapid.MakeCheck(testCmpInverse))
t.Run("TestEqualCommutative", rapid.MakeCheck(testEqualCommutative))

Expand Down Expand Up @@ -97,14 +97,14 @@ func TestDec(t *testing.T) {
require.NoError(t, err)
require.True(t, res.Equal(three))

res, err = SafeSubBalance(two, five)
_, err = SafeSubBalance(two, five)
require.Error(t, err, "Expected insufficient funds error")

res, err = SafeAddBalance(three, two)
require.NoError(t, err)
require.True(t, res.Equal(five))

res, err = SafeAddBalance(minusFivePointZero, five)
_, err = SafeAddBalance(minusFivePointZero, five)
require.Error(t, err, "Expected ErrInvalidRequest")

res, err = four.Quo(two)
Expand Down Expand Up @@ -591,11 +591,12 @@ func floatDecimalPlaces(t *rapid.T, f float64) uint32 {
}

// Subtract exponent from base and check if negative
if res := basePlaces - exp; res <= 0 {
res := basePlaces - exp
if res <= 0 {
return 0
} else {
return uint32(res)
}

return uint32(res)
}

func TestIsFinite(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions types/math/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func (d *decEq) Matches(x interface{}) bool {
if y.Cmp(d.dec) != 0 {
d.msg = fmt.Sprintf("%s != %s", d.dec, y)
return false
} else {
return true
}

return true
}

func (d *decEq) String() string {
Expand Down
2 changes: 1 addition & 1 deletion types/module/client/grpc_gateway/module.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package grpc_gateway
package grpcgateway
Copy link
Member Author

Choose a reason for hiding this comment

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

breaking change


import (
"github.com/cosmos/cosmos-sdk/client"
Expand Down
2 changes: 1 addition & 1 deletion types/module/server/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func GetMsgs(anys []*types.Any) ([]sdk.Msg, error) {
for i, any := range anys {
cached := any.GetCachedValue()
if cached == nil {
return nil, fmt.Errorf("any cached value is nil, proposal messages must be correctly packed Any values.")
return nil, fmt.Errorf("any cached value is nil, proposal messages must be correctly packed Any values")
}
msgs[i] = cached.(sdk.Msg)
}
Expand Down
6 changes: 4 additions & 2 deletions types/ormstore/store.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ormstore

import storetypes "github.com/cosmos/cosmos-sdk/types"
import "github.com/cosmos/cosmos-sdk/orm/types/kv"
import (
"github.com/cosmos/cosmos-sdk/orm/types/kv"
storetypes "github.com/cosmos/cosmos-sdk/types"
)

type storeWrapper struct {
store storetypes.KVStore
Expand Down
11 changes: 6 additions & 5 deletions types/testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"
"time"

sdkmath "cosmossdk.io/math"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/node"
Expand Down Expand Up @@ -63,9 +64,9 @@ type Config struct {
Mnemonics []string // custom user-provided validator operator mnemonics
BondDenom string // the staking bond denomination
MinGasPrices string // the minimum gas prices each validator will accept
AccountTokens sdk.Int // the amount of unique validator tokens (e.g. 1000node0)
StakingTokens sdk.Int // the amount of tokens each validator has available to stake
BondedTokens sdk.Int // the amount of tokens each validator stakes
AccountTokens sdkmath.Int // the amount of unique validator tokens (e.g. 1000node0)
StakingTokens sdkmath.Int // the amount of tokens each validator has available to stake
BondedTokens sdkmath.Int // the amount of tokens each validator stakes
PruningStrategy string // the pruning strategy each validator will have
EnableTMLogging bool // enable Tendermint logging to STDOUT
CleanupDir bool // remove base temporary directory during cleanup
Expand Down Expand Up @@ -356,7 +357,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
sdk.NewCoin(cfg.BondDenom, cfg.BondedTokens),
stakingtypes.NewDescription(nodeDirName, "", "", "", ""),
stakingtypes.NewCommissionRates(commission, sdk.OneDec(), sdk.OneDec()),
sdk.OneInt(),
sdkmath.OneInt(),
)
if err != nil {
return nil, err
Expand All @@ -368,7 +369,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
}

memo := fmt.Sprintf("%s@%s:%s", nodeIDs[i], p2pURL.Hostname(), p2pURL.Port())
fee := sdk.NewCoins(sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), sdk.NewInt(0)))
fee := sdk.NewCoins(sdk.NewCoin(fmt.Sprintf("%stoken", nodeDirName), sdkmath.NewInt(0)))
txBuilder := cfg.TxConfig.NewTxBuilder()
err = txBuilder.SetMsgs(createValMsg)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions types/testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,14 @@ func initGenFiles(cfg Config, genAccounts []authtypes.GenesisAccount, genBalance
}

func writeFile(name string, dir string, contents []byte) error {
writePath := filepath.Join(dir)
file := filepath.Join(writePath, name)
file := filepath.Join(dir, name)

err := tmos.EnsureDir(writePath, 0o755)
err := tmos.EnsureDir(dir, 0o755)
if err != nil {
return err
}

err = ioutil.WriteFile(file, contents, 0o644) // nolint: gosec
err = ioutil.WriteFile(file, contents, 0o644) //nolint: gosec
if err != nil {
return err
}
Expand Down
20 changes: 12 additions & 8 deletions types/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func TestGogoToProtobufDuration(t *testing.T) {
},
}
for _, tt := range tests {
args, want := tt.args, tt.want
t.Run(tt.name, func(t *testing.T) {
if got := types.GogoToProtobufDuration(tt.args.d); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GogoToProtobufDuration() = %v, want %v", got, tt.want)
if got := types.GogoToProtobufDuration(args.d); !reflect.DeepEqual(got, want) {
t.Errorf("GogoToProtobufDuration() = %v, want %v", got, want)
}
})
}
Expand All @@ -63,9 +64,10 @@ func TestGogoToProtobufTimestamp(t *testing.T) {
},
}
for _, tt := range tests {
args, want := tt.args, tt.want
t.Run(tt.name, func(t *testing.T) {
if got := types.GogoToProtobufTimestamp(tt.args.ts); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GogoToProtobufTimestamp() = %v, want %v", got, tt.want)
if got := types.GogoToProtobufTimestamp(args.ts); !reflect.DeepEqual(got, want) {
t.Errorf("GogoToProtobufTimestamp() = %v, want %v", got, want)
}
})
}
Expand All @@ -92,9 +94,10 @@ func TestProtobufToGogoTimestamp(t *testing.T) {
},
}
for _, tt := range tests {
args, want := tt.args, tt.want
t.Run(tt.name, func(t *testing.T) {
if got := types.ProtobufToGogoTimestamp(tt.args.ts); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ProtobufToGogoTimestamp() = %v, want %v", got, tt.want)
if got := types.ProtobufToGogoTimestamp(args.ts); !reflect.DeepEqual(got, want) {
t.Errorf("ProtobufToGogoTimestamp() = %v, want %v", got, want)
}
})
}
Expand All @@ -110,10 +113,11 @@ func TestParseDate(t *testing.T) {
{"bad", "01-2021-20", true},
}
for _, tc := range tcs {
date, hasErr := tc.date, tc.hasErr
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)
tm, err := types.ParseDate(tc.date, tc.date)
if tc.hasErr {
tm, err := types.ParseDate(date, date)
if hasErr {
require.Error(err)
require.Equal(time.Time{}, tm)
} else {
Expand Down
Loading