Skip to content

Commit

Permalink
Merge PR cosmos#4881: Linting Galore
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored and alexanderbez committed Aug 19, 2019
1 parent 89b1220 commit 3a4f1fc
Show file tree
Hide file tree
Showing 75 changed files with 207 additions and 282 deletions.
42 changes: 19 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
run:
deadline: 1m

linters:
disable-all: true
enable:
- errcheck
enable-all: true
disable:
- gosimple
- gocyclo
- gochecknoinits
- golint
- ineffassign
- unconvert
- misspell
- govet
- unused
- deadcode
- goconst
- gochecknoglobals
- dupl
- interfacer
- gosec
- staticcheck
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
issues:
exclude:
- composite
run:
tests: false
- unparam
- lll
- maligned
- nakedret
- errcheck
- scopelint
- prealloc
- varcheck
6 changes: 3 additions & 3 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,18 @@ func UpgradeableStoreLoader(upgradeInfoPath string) StoreLoader {
// there is a migration file, let's execute
data, err := ioutil.ReadFile(upgradeInfoPath)
if err != nil {
return fmt.Errorf("Cannot read upgrade file %s: %v", upgradeInfoPath, err)
return fmt.Errorf("cannot read upgrade file %s: %v", upgradeInfoPath, err)
}

var upgrades storetypes.StoreUpgrades
err = json.Unmarshal(data, &upgrades)
if err != nil {
return fmt.Errorf("Cannot parse upgrade file: %v", err)
return fmt.Errorf("cannot parse upgrade file: %v", err)
}

err = ms.LoadLatestVersionAndUpgrade(&upgrades)
if err != nil {
return fmt.Errorf("Load and upgrade database: %v", err)
return fmt.Errorf("load and upgrade database: %v", err)
}

// if we have a successful load, we delete the file
Expand Down
4 changes: 0 additions & 4 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,6 @@ func handlerMsgCounter(t *testing.T, capKey *sdk.KVStoreKey, deliverKey []byte)
}
}

func i2b(i int64) []byte {
return []byte{byte(i)}
}

func getIntFromStore(store sdk.KVStore, key []byte) int64 {
bz := store.Get(key)
if len(bz) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions client/context/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
// ErrInvalidAccount returns a standardized error reflecting that a given
// account address does not exist.
func ErrInvalidAccount(addr sdk.AccAddress) error {
return fmt.Errorf(`No account with address %s was found in the state.
return fmt.Errorf(`no account with address %s was found in the state.
Are you sure there has been a transaction involving it?`, addr)
}

// ErrVerifyCommit returns a common error reflecting that the blockchain commit at a given
// height can't be verified. The reason is that the base checkpoint of the certifier is
// newer than the given height
func ErrVerifyCommit(height int64) error {
return fmt.Errorf(`The height of base truststore in the light client is higher than height %d.
return fmt.Errorf(`the height of base truststore in the light client is higher than height %d.
Can't verify blockchain proof at this height. Please set --trust-node to true and try again`, height)
}
4 changes: 2 additions & 2 deletions client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error {
return err
}

mnemonic, err = bip39.NewMnemonic(entropySeed[:])
mnemonic, err = bip39.NewMnemonic(entropySeed)
if err != nil {
return err
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func printCreate(cmd *cobra.Command, info keys.Info, showMnemonic bool, mnemonic
}
cmd.PrintErrln(string(jsonString))
default:
return fmt.Errorf("I can't speak: %s", output)
return fmt.Errorf("invalid output format %s", output)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions client/keys/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ func TestMarshalJSON(t *testing.T) {
want []byte
wantErr bool
}{
{"basic", args{data.Keys[0]}, []byte(data.JSON[0]), false},
{"mnemonic is optional", args{data.Keys[1]}, []byte(data.JSON[1]), false},
{"basic", args{data.Keys[0]}, data.JSON[0], false},
{"mnemonic is optional", args{data.Keys[1]}, data.JSON[1], false},

// REVIEW: Are the next results expected??
{"empty name", args{data.Keys[2]}, []byte(data.JSON[2]), false},
{"empty object", args{data.Keys[3]}, []byte(data.JSON[3]), false},
{"empty name", args{data.Keys[2]}, data.JSON[2], false},
{"empty object", args{data.Keys[3]}, data.JSON[3], false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestUnmarshalJSON(t *testing.T) {
for idx, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UnmarshalJSON(tt.args.bz, tt.args.ptr); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("unmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}

// Confirm deserialized objects are the same
Expand Down
2 changes: 1 addition & 1 deletion client/keys/mnemonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func runMnemonicCmd(cmd *cobra.Command, args []string) error {
}
}

mnemonic, err := bip39.NewMnemonic(entropySeed[:])
mnemonic, err := bip39.NewMnemonic(entropySeed)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion client/keys/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func ReadPassphraseFromStdin(name string) (string, error) {

passphrase, err := input.GetPassword(prompt, buf)
if err != nil {
return passphrase, fmt.Errorf("Error reading passphrase: %v", err)
return passphrase, fmt.Errorf("error reading passphrase: %v", err)
}

return passphrase, nil
Expand Down
2 changes: 1 addition & 1 deletion codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"

"github.com/tendermint/go-amino"
amino "github.com/tendermint/go-amino"
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
tmtypes "github.com/tendermint/tendermint/types"
)
Expand Down
8 changes: 4 additions & 4 deletions crypto/keys/hd/hdpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func DerivePrivateKeyForPath(privKeyBytes [32]byte, chainCode [32]byte, path str
func derivePrivateKey(privKeyBytes [32]byte, chainCode [32]byte, index uint32, harden bool) ([32]byte, [32]byte) {
var data []byte
if harden {
index = index | 0x80000000
index |= 0x80000000
data = append([]byte{byte(0)}, privKeyBytes[:]...)
} else {
// this can't return an error:
Expand Down Expand Up @@ -245,14 +245,14 @@ func uint32ToBytes(i uint32) []byte {
}

// i64 returns the two halfs of the SHA512 HMAC of key and data.
func i64(key []byte, data []byte) (IL [32]byte, IR [32]byte) {
func i64(key []byte, data []byte) (il [32]byte, ir [32]byte) {
mac := hmac.New(sha512.New, key)
// sha512 does not err
_, _ = mac.Write(data)

I := mac.Sum(nil)
copy(IL[:], I[:32])
copy(IR[:], I[32:])
copy(il[:], I[:32])
copy(ir[:], I[32:])

return
}
18 changes: 8 additions & 10 deletions crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,22 +235,20 @@ func (kb dbKeybase) Sign(name, passphrase string, msg []byte) (sig []byte, pub t

var priv tmcrypto.PrivKey

switch info.(type) {
switch i := info.(type) {
case localInfo:
linfo := info.(localInfo)
if linfo.PrivKeyArmor == "" {
if i.PrivKeyArmor == "" {
err = fmt.Errorf("private key not available")
return
}

priv, err = mintkey.UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase)
priv, err = mintkey.UnarmorDecryptPrivKey(i.PrivKeyArmor, passphrase)
if err != nil {
return nil, nil, err
}

case ledgerInfo:
linfo := info.(ledgerInfo)
priv, err = crypto.NewPrivKeyLedgerSecp256k1Unsafe(linfo.Path)
priv, err = crypto.NewPrivKeyLedgerSecp256k1Unsafe(i.Path)
if err != nil {
return
}
Expand Down Expand Up @@ -297,9 +295,9 @@ func (kb dbKeybase) ExportPrivateKeyObject(name string, passphrase string) (tmcr

var priv tmcrypto.PrivKey

switch info.(type) {
switch i := info.(type) {
case localInfo:
linfo := info.(localInfo)
linfo := i
if linfo.PrivKeyArmor == "" {
err = fmt.Errorf("private key not available")
return nil, err
Expand Down Expand Up @@ -434,9 +432,9 @@ func (kb dbKeybase) Update(name, oldpass string, getNewpass func() (string, erro
if err != nil {
return err
}
switch info.(type) {
switch i := info.(type) {
case localInfo:
linfo := info.(localInfo)
linfo := i
key, err := mintkey.UnarmorDecryptPrivKey(linfo.PrivKeyArmor, oldpass)
if err != nil {
return err
Expand Down
15 changes: 11 additions & 4 deletions crypto/keys/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func init() {
mintkey.BcryptSecurityParameter = 1
}

const (
nums = "1234"
foobar = "foobar"
)

func TestLanguage(t *testing.T) {
kb := NewInMemory()
_, _, err := kb.CreateMnemonic("something", Japanese, "no_pass", Secp256k1)
Expand Down Expand Up @@ -68,11 +73,13 @@ func TestCreateLedger(t *testing.T) {

// Check that restoring the key gets the same results
restoredKey, err := kb.Get("some_account")
assert.NoError(t, err)
assert.NotNil(t, restoredKey)
assert.Equal(t, "some_account", restoredKey.GetName())
assert.Equal(t, TypeLedger, restoredKey.GetType())
pubKey = restoredKey.GetPubKey()
pk, err = sdk.Bech32ifyAccPub(pubKey)
assert.NoError(t, err)
assert.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk)

path, err := restoredKey.GetPath()
Expand All @@ -87,7 +94,7 @@ func TestKeyManagement(t *testing.T) {

algo := Secp256k1
n1, n2, n3 := "personal", "business", "other"
p1, p2 := "1234", "really-secure!@#$"
p1, p2 := nums, "really-secure!@#$"

// Check empty state
l, err := cstore.List()
Expand Down Expand Up @@ -170,7 +177,7 @@ func TestSignVerify(t *testing.T) {
algo := Secp256k1

n1, n2, n3 := "some dude", "a dudette", "dude-ish"
p1, p2, p3 := "1234", "foobar", "foobar"
p1, p2, p3 := nums, foobar, foobar

// create two users and get their info
i1, _, err := cstore.CreateMnemonic(n1, English, p1, algo)
Expand Down Expand Up @@ -320,7 +327,7 @@ func TestAdvancedKeyManagement(t *testing.T) {

algo := Secp256k1
n1, n2 := "old-name", "new name"
p1, p2 := "1234", "foobar"
p1, p2 := nums, foobar

// make sure key works with initial password
_, _, err := cstore.CreateMnemonic(n1, English, p1, algo)
Expand Down Expand Up @@ -368,7 +375,7 @@ func TestSeedPhrase(t *testing.T) {

algo := Secp256k1
n1, n2 := "lost-key", "found-again"
p1, p2 := "1234", "foobar"
p1, p2 := nums, foobar

// make sure key works with initial password
info, mnemonic, err := cstore.CreateMnemonic(n1, English, p1, algo)
Expand Down
8 changes: 4 additions & 4 deletions crypto/keys/lazy_keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestLazyKeyManagement(t *testing.T) {

algo := Secp256k1
n1, n2, n3 := "personal", "business", "other"
p1, p2 := "1234", "really-secure!@#$"
p1, p2 := nums, "really-secure!@#$"

// Check empty state
l, err := kb.List()
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestLazySignVerify(t *testing.T) {
algo := Secp256k1

n1, n2, n3 := "some dude", "a dudette", "dude-ish"
p1, p2, p3 := "1234", "foobar", "foobar"
p1, p2, p3 := nums, foobar, foobar

// create two users and get their info
i1, _, err := kb.CreateMnemonic(n1, English, p1, algo)
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestLazyAdvancedKeyManagement(t *testing.T) {

algo := Secp256k1
n1, n2 := "old-name", "new name"
p1, p2 := "1234", "foobar"
p1, p2 := nums, foobar

// make sure key works with initial password
_, _, err := kb.CreateMnemonic(n1, English, p1, algo)
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestLazySeedPhrase(t *testing.T) {

algo := Secp256k1
n1, n2 := "lost-key", "found-again"
p1, p2 := "1234", "foobar"
p1, p2 := nums, foobar

// make sure key works with initial password
info, mnemonic, err := kb.CreateMnemonic(n1, English, p1, algo)
Expand Down
14 changes: 7 additions & 7 deletions crypto/keys/mintkey/mintkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ func unarmorBytes(armorStr, blockType string) (bz []byte, err error) {
return
}
if bType != blockType {
err = fmt.Errorf("Unrecognized armor type %q, expected: %q", bType, blockType)
err = fmt.Errorf("unrecognized armor type %q, expected: %q", bType, blockType)
return
}
if header["version"] != "0.0.0" {
err = fmt.Errorf("Unrecognized version: %v", header["version"])
err = fmt.Errorf("unrecognized version: %v", header["version"])
return
}
return
Expand Down Expand Up @@ -123,17 +123,17 @@ func UnarmorDecryptPrivKey(armorStr string, passphrase string) (crypto.PrivKey,
return privKey, err
}
if blockType != blockTypePrivKey {
return privKey, fmt.Errorf("Unrecognized armor type: %v", blockType)
return privKey, fmt.Errorf("unrecognized armor type: %v", blockType)
}
if header["kdf"] != "bcrypt" {
return privKey, fmt.Errorf("Unrecognized KDF type: %v", header["KDF"])
return privKey, fmt.Errorf("unrecognized KDF type: %v", header["KDF"])
}
if header["salt"] == "" {
return privKey, fmt.Errorf("Missing salt bytes")
return privKey, fmt.Errorf("missing salt bytes")
}
saltBytes, err := hex.DecodeString(header["salt"])
if err != nil {
return privKey, fmt.Errorf("Error decoding salt: %v", err.Error())
return privKey, fmt.Errorf("error decoding salt: %v", err.Error())
}
privKey, err = decryptPrivKey(saltBytes, encBytes, passphrase)
return privKey, err
Expand All @@ -142,7 +142,7 @@ func UnarmorDecryptPrivKey(armorStr string, passphrase string) (crypto.PrivKey,
func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privKey crypto.PrivKey, err error) {
key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), BcryptSecurityParameter)
if err != nil {
cmn.Exit("Error generating bcrypt key from passphrase: " + err.Error())
cmn.Exit("error generating bcrypt key from passphrase: " + err.Error())
}
key = crypto.Sha256(key) // Get 32 bytes
privKeyBytes, err := xsalsa20symmetric.DecryptSymmetric(encBytes, key)
Expand Down
Loading

0 comments on commit 3a4f1fc

Please sign in to comment.