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

Add some linting to gaia #188

Merged
merged 6 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
55 changes: 45 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
linters:
disable-all: true
enable:
- errcheck
- bodyclose
- deadcode
- depguard
- dogsled
- goconst
- gocritic
- gofmt
- goimports
- golint
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- maligned
- misspell
- nakedret
- prealloc
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unused
- misspell
disable:
- errcheck

issues:
exclude-rules:
- text: "Use of weak random number generator"
linters:
- gosec
- text: "comment on exported var"
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
linters:
- golint
- text: "don't use an underscore in package name"
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
linters:
- golint
- text: "ST1003:"
linters:
- stylecheck

linters-settings:
gocyclo:
min-complexity: 11
errcheck:
ignore: fmt:.*,io/ioutil:^Read.*,github.com/spf13/cobra:MarkFlagRequired,github.com/spf13/viper:BindPFlag
golint:
min-confidence: 1.1
run:
tests: false
dogsled:
max-blank-identifiers: 3
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
2 changes: 0 additions & 2 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []st
iter := sdk.KVStoreReversePrefixIterator(store, staking.ValidatorsKey)
counter := int16(0)

var valConsAddrs []sdk.ConsAddress
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[1:])
validator, found := app.stakingKeeper.GetValidator(ctx, addr)
Expand All @@ -137,7 +136,6 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []st
}

validator.UnbondingHeight = 0
valConsAddrs = append(valConsAddrs, validator.ConsAddress())
if applyWhiteList && !whiteListMap[addr.String()] {
validator.Jailed = true
}
Expand Down
1 change: 1 addition & 0 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ func BenchmarkInvariants(b *testing.B) {
// NOTE: We use the crisis keeper as it has all the invariants registered with
// their respective metadata which makes it useful for testing/benchmarking.
for _, cr := range gapp.crisisKeeper.Routes() {
cr := cr
b.Run(fmt.Sprintf("%s/%s", cr.ModuleName, cr.Route), func(b *testing.B) {
if res, stop := cr.Invar(ctx); stop {
fmt.Printf("broken invariant at block %d of %d\n%s", ctx.BlockHeight()-1, config.NumBlocks, res)
Expand Down
5 changes: 4 additions & 1 deletion cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
)

var (
// nolint:varcheck,deadcode,unused
totalCoins = sdk.NewCoins(
sdk.NewCoin(fee2Denom, sdk.TokensFromConsensusPower(2000000)),
sdk.NewCoin(feeDenom, sdk.TokensFromConsensusPower(2000000)),
Expand Down Expand Up @@ -171,7 +172,7 @@ func InitFixtures(t *testing.T) (f *Fixtures) {
f.GenTx(keyFoo)
f.CollectGenTxs()

return
return f
}

// Cleanup is meant to be run at the end of a test to clean up an remaining test state
Expand Down Expand Up @@ -757,13 +758,15 @@ func WriteToNewTempFile(t *testing.T, s string) *os.File {
return fp
}

tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
//nolint:deadcode,unused
func marshalStdTx(t *testing.T, stdTx auth.StdTx) []byte {
cdc := app.MakeCodec()
bz, err := cdc.MarshalBinaryBare(stdTx)
require.NoError(t, err)
return bz
}

//nolint:deadcode,unused
func unmarshalStdTx(t *testing.T, s string) (stdTx auth.StdTx) {
cdc := app.MakeCodec()
require.Nil(t, cdc.UnmarshalJSON([]byte(s), &stdTx))
Expand Down
1 change: 1 addition & 0 deletions cmd/contract_tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/snikch/goodman/hooks"
"github.com/snikch/goodman/transaction"
)
Expand Down
12 changes: 1 addition & 11 deletions cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/base64"
"encoding/hex"
"fmt"
"os"
Expand All @@ -14,7 +13,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"

"github.com/tendermint/tendermint/libs/log"

Expand All @@ -26,7 +24,7 @@ import (
func runHackCmd(cmd *cobra.Command, args []string) error {

if len(args) != 1 {
return fmt.Errorf("Expected 1 arg")
return fmt.Errorf("expected 1 arg")
}

// ".gaiad"
Expand Down Expand Up @@ -88,14 +86,6 @@ func runHackCmd(cmd *cobra.Command, args []string) error {
}
}

func base64ToPub(b64 string) ed25519.PubKeyEd25519 {
data, _ := base64.StdEncoding.DecodeString(b64)
var pubKey ed25519.PubKeyEd25519
copy(pubKey[:], data)
return pubKey

}

func hexToBytes(h string) []byte {
trouble, _ := hex.DecodeString(h)
return trouble
Expand Down
14 changes: 7 additions & 7 deletions cmd/gaiadebug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var rawBytesCmd = &cobra.Command{

func runRawBytesCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("Expected single arg")
return fmt.Errorf("expected single arg")
}
stringBytes := args[0]
stringBytes = strings.Trim(stringBytes, "[")
Expand All @@ -94,7 +94,7 @@ func runRawBytesCmd(cmd *cobra.Command, args []string) error {

func runPubKeyCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("Expected single arg")
return fmt.Errorf("expected single arg")
}

pubkeyString := args[0]
Expand All @@ -116,7 +116,7 @@ func runPubKeyCmd(cmd *cobra.Command, args []string) error {
var err5 error
pubKeyI, err5 = sdk.GetConsPubKeyBech32(pubkeyString)
if err5 != nil {
return fmt.Errorf(`Expected hex, base64, or bech32. Got errors:
return fmt.Errorf(`expected hex, base64, or bech32. Got errors:
hex: %v,
base64: %v
bech32 Acc: %v
Expand Down Expand Up @@ -168,7 +168,7 @@ func runPubKeyCmd(cmd *cobra.Command, args []string) error {

func runAddrCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("Expected single arg")
return fmt.Errorf("expected single arg")
}

addrString := args[0]
Expand All @@ -185,7 +185,7 @@ func runAddrCmd(cmd *cobra.Command, args []string) error {
addr, err3 = sdk.ValAddressFromBech32(addrString)

if err3 != nil {
return fmt.Errorf(`Expected hex or bech32. Got errors:
return fmt.Errorf(`expected hex or bech32. Got errors:
hex: %v,
bech32 acc: %v
bech32 val: %v
Expand All @@ -207,7 +207,7 @@ func runAddrCmd(cmd *cobra.Command, args []string) error {

func runTxCmd(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("Expected single arg")
return fmt.Errorf("expected single arg")
}

txString := args[0]
Expand All @@ -218,7 +218,7 @@ func runTxCmd(cmd *cobra.Command, args []string) error {
var err2 error
txBytes, err2 = base64.StdEncoding.DecodeString(txString)
if err2 != nil {
return fmt.Errorf(`Expected hex or base64. Got errors:
return fmt.Errorf(`expected hex or base64. Got errors:
hex: %v,
base64: %v
`, err, err2)
Expand Down
7 changes: 3 additions & 4 deletions lcd_test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ func defaultGenesis(config *tmcfg.Config, nValidators int, initAddrs []sdk.AccAd

// append any additional (non-proposing) validators
var genTxs []auth.StdTx
var genAccounts []authexported.GenesisAccount

genAccounts := make([]authexported.GenesisAccount, 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

don't preallocate to 0, just add nolint

Copy link
Contributor

Choose a reason for hiding this comment

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

even better, you already know the size to allocate: len(initAddrs) + nValidators

Copy link
Member Author

Choose a reason for hiding this comment

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

len(initAddrs) + nValidators this seems to be causing most the tests in lcd_test to fail

Copy link
Contributor

Choose a reason for hiding this comment

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

are you setting values in the array correctly? i.e. genAccounts[genAccsIdx] = genAccount

Copy link
Collaborator

Choose a reason for hiding this comment

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

totalSupply := sdk.ZeroInt()

for i := 0; i < nValidators; i++ {
Expand Down Expand Up @@ -298,7 +297,7 @@ func defaultGenesis(config *tmcfg.Config, nValidators int, initAddrs []sdk.AccAd
}

genDoc.AppState = appState
return
return genDoc, valConsPubKeys, valOperAddrs, privVal, err
}

// startTM creates and starts an in-process Tendermint node with memDB and
Expand Down Expand Up @@ -412,7 +411,7 @@ func CreateAddrs(kb crkeys.Keybase, numAddrs int) (addrs []sdk.AccAddress, seeds
passwords = append(passwords, addrSeeds[i].Password)
}

return
return addrs, seeds, names, passwords, errs
}

// AddrSeed combines an Address with the mnemonic of the private key to that address
Expand Down
25 changes: 10 additions & 15 deletions lcd_test/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:unused,deadcode,bodyclose
package lcdtest

import (
Expand Down Expand Up @@ -115,7 +116,7 @@ func getBlock(t *testing.T, port string, height int, expectFail bool) ctypes.Res

func extractResultFromResponse(t *testing.T, body []byte) []byte {
var resp rest.ResponseWithHeight
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &resp))
require.NoError(t, cdc.UnmarshalJSON(body, &resp))

return resp.Result
}
Expand Down Expand Up @@ -155,6 +156,7 @@ func getTransaction(t *testing.T, port string, hash string) sdk.TxResponse {

err := cdc.UnmarshalJSON([]byte(body), &tx)
require.NoError(t, err)

return tx
}

Expand All @@ -176,6 +178,7 @@ func getTransactions(t *testing.T, port string, tags ...string) *sdk.SearchTxsRe
require.Equal(t, http.StatusOK, res.StatusCode, body)

err := cdc.UnmarshalJSON([]byte(body), &result)

require.NoError(t, err)
return &result
}
Expand All @@ -190,6 +193,7 @@ func getKeys(t *testing.T, port string) []keys.KeyOutput {
var m []keys.KeyOutput
err := cdc.UnmarshalJSON([]byte(body), &m)
require.Nil(t, err)

return m
}

Expand All @@ -205,17 +209,18 @@ func doKeysPost(t *testing.T, port, name, password, mnemonic string, account int
var resp keys.KeyOutput
err = cdc.UnmarshalJSON([]byte(body), &resp)
require.Nil(t, err, body)

return resp
}

// GET /keys/seed Create a new seed to create a new account defaultValidFor
func getKeysSeed(t *testing.T, port string) string {
res, body := Request(t, port, "GET", "/keys/seed", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
reg, err := regexp.Compile(`([a-z]+ ){12}`)
require.Nil(t, err)
reg := regexp.MustCompile(`([a-z]+ ){12}`)
match := reg.MatchString(body)
require.True(t, match, "Returned seed has wrong format", body)

return body
}

Expand Down Expand Up @@ -244,6 +249,7 @@ func getKey(t *testing.T, port, name string) keys.KeyOutput {
var resp keys.KeyOutput
err := cdc.UnmarshalJSON([]byte(body), &resp)
require.Nil(t, err)

return resp
}

Expand All @@ -259,6 +265,7 @@ func updateKey(t *testing.T, port, name, oldPassword, newPassword string, fail b
return
}
require.Equal(t, http.StatusOK, res.StatusCode, body)

}

// GET /auth/accounts/{address} Get the account information on blockchain
Expand Down Expand Up @@ -1129,10 +1136,6 @@ func doUnjail(
return txResp
}

type unjailReq struct {
BaseReq rest.BaseReq `json:"base_req"`
}

// ICS24 - fee distribution

// POST /distribution/delegators/{delgatorAddr}/rewards Withdraw delegator rewards
Expand Down Expand Up @@ -1165,11 +1168,3 @@ func doWithdrawDelegatorAllRewards(

return txResp
}

func mustParseDecCoins(dcstring string) sdk.DecCoins {
dcoins, err := sdk.ParseDecCoins(dcstring)
if err != nil {
panic(err)
}
return dcoins
}
6 changes: 3 additions & 3 deletions lcd_test/lcd_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:bodyclose
package lcdtest

import (
Expand Down Expand Up @@ -269,12 +270,12 @@ func TestEncodeTx(t *testing.T) {
require.NoError(t, err)
defer cleanup()

res, body, _ := doTransferWithGas(t, port, seed, name1, memo, "", addr, "2", 1, false, false, fees)
_, body, _ := doTransferWithGas(t, port, seed, name1, memo, "", addr, "2", 1, false, false, fees)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
var tx auth.StdTx
require.Nil(t, cdc.UnmarshalJSON([]byte(body), &tx))

encodedJSON, _ := cdc.MarshalJSON(tx)
res, body = Request(t, port, "POST", "/txs/encode", encodedJSON)
res, body := Request(t, port, "POST", "/txs/encode", encodedJSON)
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

// Make sure it came back ok, and that we can decode it back to the transaction
// 200 response.
Expand Down Expand Up @@ -491,7 +492,6 @@ func TestBonding(t *testing.T) {
//require.Equal(t, rdTokens.ToDec(), delTokensAfterRedelegation)

// verify balance after paying fees
acc = getAccount(t, port, addr)
expectedBalance = expectedBalance.Sub(fees[0])
require.True(t,
expectedBalance.Amount.LT(coins.AmountOf(sdk.DefaultBondDenom)) ||
Expand Down