Skip to content

Commit

Permalink
various linter fixes (cosmos#6106)
Browse files Browse the repository at this point in the history
x/staking: Fix all linter warnings.

Fixed warnings across base packages.

New linters:
- unparam
- nolintlint

Co-authored-by: Alessio Treglia <[email protected]>
  • Loading branch information
tac0turtle and Alessio Treglia authored May 2, 2020
1 parent 9251812 commit 218ec99
Show file tree
Hide file tree
Showing 103 changed files with 636 additions and 278 deletions.
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ linters:
- typecheck
- unconvert
- unused
- unparam
- misspell
- wsl
- nolintlint

issues:
exclude-rules:
Expand All @@ -54,10 +56,17 @@ issues:
- text: "ST1016:"
linters:
- stylecheck
max-issues-per-linter: 10000
max-same-issues: 10000

linters-settings:
dogsled:
max-blank-identifiers: 3
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
nolintlint:
allow-unused: false
allow-leading-space: true
require-explanation: false
require-specific: false
3 changes: 2 additions & 1 deletion baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (app *BaseApp) FilterPeerByAddrPort(info string) abci.ResponseQuery {
if app.addrPeerFilter != nil {
return app.addrPeerFilter(info)
}

return abci.ResponseQuery{}
}

Expand All @@ -95,6 +96,7 @@ func (app *BaseApp) FilterPeerByID(info string) abci.ResponseQuery {
if app.idPeerFilter != nil {
return app.idPeerFilter(info)
}

return abci.ResponseQuery{}
}

Expand Down Expand Up @@ -136,7 +138,6 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
if app.beginBlocker != nil {
res = app.beginBlocker(app.deliverState.ctx, req)
}

// set the signed validators for addition to context in deliverTx
app.voteInfos = req.LastCommitInfo.GetVotes()
return res
Expand Down
13 changes: 13 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (app *BaseApp) SetName(name string) {
if app.sealed {
panic("SetName() on sealed BaseApp")
}

app.name = name
}

Expand All @@ -56,6 +57,7 @@ func (app *BaseApp) SetParamStore(ps ParamStore) {
if app.sealed {
panic("SetParamStore() on sealed BaseApp")
}

app.paramStore = ps
}

Expand All @@ -64,69 +66,79 @@ func (app *BaseApp) SetAppVersion(v string) {
if app.sealed {
panic("SetAppVersion() on sealed BaseApp")
}

app.appVersion = v
}

func (app *BaseApp) SetDB(db dbm.DB) {
if app.sealed {
panic("SetDB() on sealed BaseApp")
}

app.db = db
}

func (app *BaseApp) SetCMS(cms store.CommitMultiStore) {
if app.sealed {
panic("SetEndBlocker() on sealed BaseApp")
}

app.cms = cms
}

func (app *BaseApp) SetInitChainer(initChainer sdk.InitChainer) {
if app.sealed {
panic("SetInitChainer() on sealed BaseApp")
}

app.initChainer = initChainer
}

func (app *BaseApp) SetBeginBlocker(beginBlocker sdk.BeginBlocker) {
if app.sealed {
panic("SetBeginBlocker() on sealed BaseApp")
}

app.beginBlocker = beginBlocker
}

func (app *BaseApp) SetEndBlocker(endBlocker sdk.EndBlocker) {
if app.sealed {
panic("SetEndBlocker() on sealed BaseApp")
}

app.endBlocker = endBlocker
}

func (app *BaseApp) SetAnteHandler(ah sdk.AnteHandler) {
if app.sealed {
panic("SetAnteHandler() on sealed BaseApp")
}

app.anteHandler = ah
}

func (app *BaseApp) SetAddrPeerFilter(pf sdk.PeerFilter) {
if app.sealed {
panic("SetAddrPeerFilter() on sealed BaseApp")
}

app.addrPeerFilter = pf
}

func (app *BaseApp) SetIDPeerFilter(pf sdk.PeerFilter) {
if app.sealed {
panic("SetIDPeerFilter() on sealed BaseApp")
}

app.idPeerFilter = pf
}

func (app *BaseApp) SetFauxMerkleMode() {
if app.sealed {
panic("SetFauxMerkleMode() on sealed BaseApp")
}

app.fauxMerkleMode = true
}

Expand All @@ -141,6 +153,7 @@ func (app *BaseApp) SetStoreLoader(loader StoreLoader) {
if app.sealed {
panic("SetStoreLoader() on sealed BaseApp")
}

app.storeLoader = loader
}

Expand Down
2 changes: 2 additions & 0 deletions baseapp/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func ValidateBlockParams(i interface{}) error {
if v.MaxBytes <= 0 {
return fmt.Errorf("block maximum bytes must be positive: %d", v.MaxBytes)
}

if v.MaxGas < -1 {
return fmt.Errorf("block maximum gas must be greater than or equal to -1: %d", v.MaxGas)
}
Expand All @@ -56,6 +57,7 @@ func ValidateEvidenceParams(i interface{}) error {
if v.MaxAgeNumBlocks <= 0 {
return fmt.Errorf("evidence maximum age in blocks must be positive: %d", v.MaxAgeNumBlocks)
}

if v.MaxAgeDuration <= 0 {
return fmt.Errorf("evidence maximum age time duration must be positive: %v", v.MaxAgeDuration)
}
Expand Down
2 changes: 2 additions & 0 deletions baseapp/queryrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ func (qrt *QueryRouter) AddRoute(path string, q sdk.Querier) sdk.QueryRouter {
if !isAlphaNumeric(path) {
panic("route expressions can only contain alphanumeric characters")
}

if qrt.routes[path] != nil {
panic(fmt.Sprintf("route %s has already been initialized", path))
}

qrt.routes[path] = q

return qrt
}

Expand Down
8 changes: 6 additions & 2 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ func (ctx CLIContext) Verify(height int64) (tmtypes.SignedHeader, error) {
if ctx.Verifier == nil {
return tmtypes.SignedHeader{}, fmt.Errorf("missing valid certifier to verify data from distrusted node")
}

check, err := tmliteProxy.GetCertifiedCommit(height, ctx.Client, ctx.Verifier)

switch {
case tmliteErr.IsErrCommitNotFound(err):
return tmtypes.SignedHeader{}, ErrVerifyCommit(height)
Expand Down Expand Up @@ -175,8 +177,8 @@ func (ctx CLIContext) verifyProof(queryPath string, resp abci.ResponseQuery) err
}
return nil
}
err = prt.VerifyValue(resp.Proof, commit.Header.AppHash, kp.String(), resp.Value)
if err != nil {

if err := prt.VerifyValue(resp.Proof, commit.Header.AppHash, kp.String(), resp.Value); err != nil {
return errors.Wrap(err, "failed to prove merkle proof")
}

Expand All @@ -199,6 +201,7 @@ func isQueryStoreWithProof(path string) bool {
}

paths := strings.SplitN(path[1:], "/", 3)

switch {
case len(paths) != 3:
return false
Expand All @@ -218,6 +221,7 @@ func parseQueryStorePath(path string) (storeName string, err error) {
}

paths := strings.SplitN(path[1:], "/", 3)

switch {
case len(paths) != 3:
return "", errors.New("expected format like /store/<storeName>/key")
Expand Down
2 changes: 2 additions & 0 deletions client/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func GetString(prompt string, buf *bufio.Reader) (string, error) {
if err != nil {
return "", err
}

return strings.TrimSpace(out), nil
}

Expand All @@ -90,5 +91,6 @@ func readLineFromBuf(buf *bufio.Reader) (string, error) {
if err != nil {
return "", err
}

return strings.TrimSpace(pass), nil
}
3 changes: 3 additions & 0 deletions client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTi
if err != nil {
return
}

rs.log.Info(
fmt.Sprintf(
"Starting application REST service (chain-id: %q)...",
Expand All @@ -70,6 +71,7 @@ func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTi
)

var h http.Handler = rs.Mux

if cors {
return rpcserver.StartHTTPServer(rs.listener, handlers.CORS()(h), rs.log, cfg)
}
Expand Down Expand Up @@ -111,6 +113,7 @@ func (rs *RestServer) registerSwaggerUI() {
if err != nil {
panic(err)
}

staticServer := http.FileServer(statikFS)
rs.Mux.PathPrefix("/").Handler(staticServer)
}
29 changes: 18 additions & 11 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ func NewInMemory(opts ...Option) Keyring {
func New(
appName, backend, rootDir string, userInput io.Reader, opts ...Option,
) (Keyring, error) {

var db keyring.Keyring
var err error
var (
db keyring.Keyring
err error
)

switch backend {
case BackendMemory:
Expand Down Expand Up @@ -353,8 +354,7 @@ func (ks keystore) SaveLedgerKey(uid string, algo SignatureAlgo, hrp string, coi

func (ks keystore) writeLedgerKey(name string, pub tmcrypto.PubKey, path hd.BIP44Params, algo hd.PubKeyType) (Info, error) {
info := newLedgerInfo(name, pub, path, algo)
err := ks.writeInfo(info)
if err != nil {
if err := ks.writeInfo(info); err != nil {
return nil, err
}

Expand Down Expand Up @@ -422,8 +422,9 @@ func (ks keystore) KeyByAddress(address sdk.Address) (Info, error) {

func (ks keystore) List() ([]Info, error) {
var res []Info

keys, err := ks.db.Keys()
if err != nil {
if err != nil { //nolint:unparam
return nil, err
}

Expand Down Expand Up @@ -525,7 +526,8 @@ func SignWithLedger(info Info, msg []byte) (sig []byte, pub tmcrypto.PubKey, err
default:
return nil, nil, errors.New("not a ledger object")
}
path, err := info.GetPath()

path, err := info.GetPath() //nolint:unparam
if err != nil {
return
}
Expand Down Expand Up @@ -573,6 +575,7 @@ func newKWalletBackendKeyringConfig(appName, _ string, _ io.Reader) keyring.Conf

func newPassBackendKeyringConfig(appName, _ string, _ io.Reader) keyring.Config {
prefix := fmt.Sprintf(passKeyringPrefix, appName)

return keyring.Config{
AllowedBackends: []keyring.BackendType{keyring.PassBackend},
ServiceName: appName,
Expand All @@ -582,6 +585,7 @@ func newPassBackendKeyringConfig(appName, _ string, _ io.Reader) keyring.Config

func newFileBackendKeyringConfig(name, dir string, buf io.Reader) keyring.Config {
fileDir := filepath.Join(dir, keyringFileDirName)

return keyring.Config{
AllowedBackends: []keyring.BackendType{keyring.FileBackend},
ServiceName: name,
Expand All @@ -598,6 +602,7 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
var keyhash []byte

_, err := os.Stat(keyhashFilePath)

switch {
case err == nil:
keyhash, err = ioutil.ReadFile(keyhashFilePath)
Expand All @@ -615,6 +620,7 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
}

failureCounter := 0

for {
failureCounter++
if failureCounter > maxPassphraseEntryAttempts {
Expand All @@ -623,7 +629,7 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {

buf := bufio.NewReader(buf)
pass, err := input.GetPassword("Enter keyring passphrase:", buf)
if err != nil {
if err != nil { //nolint:unparam
fmt.Fprintln(os.Stderr, err)
continue
}
Expand All @@ -633,6 +639,7 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
fmt.Fprintln(os.Stderr, "incorrect passphrase")
continue
}

return pass, nil
}

Expand All @@ -649,7 +656,7 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {

saltBytes := tmcrypto.CRandBytes(16)
passwordHash, err := bcrypt.GenerateFromPassword(saltBytes, []byte(pass), 2)
if err != nil {
if err != nil { //nolint:unparam
fmt.Fprintln(os.Stderr, err)
continue
}
Expand All @@ -668,8 +675,7 @@ func (ks keystore) writeLocalKey(name string, priv tmcrypto.PrivKey, algo hd.Pub
pub := priv.PubKey()

info := newLocalInfo(name, pub, string(priv.Bytes()), algo)
err := ks.writeInfo(info)
if err != nil {
if err := ks.writeInfo(info); err != nil {
return nil, err
}

Expand All @@ -685,6 +691,7 @@ func (ks keystore) writeInfo(info Info) error {
if exists {
return fmt.Errorf("public key already exist in keybase")
}

if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 218ec99

Please sign in to comment.