From 04855f451658bb4e1eea9385662ca57afc9d9c38 Mon Sep 17 00:00:00 2001 From: Marko Baricevic Date: Fri, 21 Jun 2019 17:35:20 +0200 Subject: [PATCH 01/10] More linters - Gosec, staticcheck - Added gosec, staticcheck Signed-off-by: Marko Baricevic --- .golangci.yml | 2 ++ client/input/input.go | 4 ++-- client/rpc/block.go | 5 +++++ client/rpc/validators.go | 5 +++++ server/test_helpers.go | 2 +- server/util.go | 3 +++ simapp/export.go | 2 -- tests/util.go | 5 +++-- x/genutil/client/cli/gentx.go | 2 +- x/gov/simulation/msgs.go | 2 +- x/staking/client/rest/utils.go | 5 +++++ 11 files changed, 28 insertions(+), 9 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 70dede1be99e..56d69839e204 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,6 +8,8 @@ linters: - misspell - govet - goconst + - gosec + - staticcheck linters-settings: gocyclo: min-complexity: 11 diff --git a/client/input/input.go b/client/input/input.go index 2ff691432c1b..0c5bf6b3b6c5 100644 --- a/client/input/input.go +++ b/client/input/input.go @@ -100,9 +100,9 @@ func GetConfirmation(prompt string, buf *bufio.Reader) (bool, error) { response = strings.ToLower(strings.TrimSpace(response)) if response[0] == 'y' { return true, nil + } else { + return false, nil } - - return false, nil } } diff --git a/client/rpc/block.go b/client/rpc/block.go index 53177ec3ba6c..893c845763be 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -121,6 +121,11 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } chainHeight, err := GetChainHeight(cliCtx) + if err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, + "ERROR: Could not recieve chain height. ") + return + } if height > chainHeight { rest.WriteErrorResponse(w, http.StatusNotFound, "ERROR: Requested block height is bigger then the chain length.") diff --git a/client/rpc/validators.go b/client/rpc/validators.go index e98db0a0dd52..2832c8ee5f81 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -166,6 +166,11 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } chainHeight, err := GetChainHeight(cliCtx) + if err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, + "ERROR: Could not recieve chain height.") + return + } if height > chainHeight { rest.WriteErrorResponse(w, http.StatusNotFound, "ERROR: Requested block height is bigger then the chain length.") return diff --git a/server/test_helpers.go b/server/test_helpers.go index 120d7b17b592..5f98ab07b60e 100644 --- a/server/test_helpers.go +++ b/server/test_helpers.go @@ -16,7 +16,7 @@ import ( // Get a free address for a test tendermint server // protocol is either tcp, http, etc func FreeTCPAddr() (addr, port string, err error) { - l, err := net.Listen("tcp", "0.0.0.0:0") + l, err := net.Listen("tcp", "localhost:0") if err != nil { return "", "", err } diff --git a/server/util.go b/server/util.go index 9ee927d2cfa4..150c1045fef3 100644 --- a/server/util.go +++ b/server/util.go @@ -103,6 +103,9 @@ func interceptLoadConfig() (conf *cfg.Config, err error) { if conf == nil { conf, err = tcmd.ParseConfig() // NOTE: ParseConfig() creates dir/files as necessary. + if err != nil { + panic("err") + } } appConfigFilePath := filepath.Join(rootDir, "config/app.toml") diff --git a/simapp/export.go b/simapp/export.go index de2b2bac3480..6979349c0eff 100644 --- a/simapp/export.go +++ b/simapp/export.go @@ -133,7 +133,6 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []str iter := sdk.KVStoreReversePrefixIterator(store, staking.ValidatorsKey) counter := int16(0) - var valConsAddrs []sdk.ConsAddress for ; iter.Valid(); iter.Next() { addr := sdk.ValAddress(iter.Key()[1:]) validator, found := app.stakingKeeper.GetValidator(ctx, addr) @@ -142,7 +141,6 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []str } validator.UnbondingHeight = 0 - valConsAddrs = append(valConsAddrs, validator.ConsAddress()) if applyWhiteList && !whiteListMap[addr.String()] { validator.Jailed = true } diff --git a/tests/util.go b/tests/util.go index 070073c2b3ed..73d63c513d6c 100644 --- a/tests/util.go +++ b/tests/util.go @@ -97,7 +97,8 @@ func waitForHeight(height int64, url string) { var res *http.Response var err error for { - res, err = http.Get(url) + // Since this is in a testing file we are accepting nolint to be passed + res, err = http.Get(url) //nolint:gosec if err != nil { panic(err) } @@ -150,7 +151,7 @@ func WaitForStart(url string) { time.Sleep(time.Millisecond * 100) var res *http.Response - res, err = http.Get(url) + res, err = http.Get(url) //nolint:gosec Error is arising in testing files, accepting nolint if err != nil || res == nil { continue } diff --git a/x/genutil/client/cli/gentx.go b/x/genutil/client/cli/gentx.go index 545b0ceccfad..2ca9bd6294e5 100644 --- a/x/genutil/client/cli/gentx.go +++ b/x/genutil/client/cli/gentx.go @@ -214,10 +214,10 @@ func readUnsignedGenTxFile(cdc *codec.Codec, r io.Reader) (auth.StdTx, error) { func writeSignedGenTx(cdc *codec.Codec, outputDocument string, tx auth.StdTx) error { outputFile, err := os.OpenFile(outputDocument, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644) - defer outputFile.Close() if err != nil { return err } + defer outputFile.Close() json, err := cdc.MarshalJSON(tx) if err != nil { return err diff --git a/x/gov/simulation/msgs.go b/x/gov/simulation/msgs.go index 7053ec655262..8b65fa3d611a 100644 --- a/x/gov/simulation/msgs.go +++ b/x/gov/simulation/msgs.go @@ -163,7 +163,7 @@ func operationSimulateMsgVote(k gov.Keeper, acc simulation.Account, proposalID u acc = simulation.RandomAcc(r, accs) } - if proposalID < 0 { + if proposalID < uint64(0) { var ok bool proposalID, ok = randomProposalID(r, k, ctx) if !ok { diff --git a/x/staking/client/rest/utils.go b/x/staking/client/rest/utils.go index 1d07c04a2e4c..38eb4a4c780f 100644 --- a/x/staking/client/rest/utils.go +++ b/x/staking/client/rest/utils.go @@ -43,6 +43,11 @@ func queryBonds(cliCtx context.CLIContext, endpoint string) http.HandlerFunc { bech32validator := vars["validatorAddr"] delegatorAddr, err := sdk.AccAddressFromBech32(bech32delegator) + if err != nil { + rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) + return + } + validatorAddr, err := sdk.ValAddressFromBech32(bech32validator) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) From 055d730d655f363a3c2404fc2444bbe64e450931 Mon Sep 17 00:00:00 2001 From: Marko Baricevic Date: Fri, 21 Jun 2019 18:10:46 +0200 Subject: [PATCH 02/10] panic err correction --- server/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/util.go b/server/util.go index 150c1045fef3..624acce94620 100644 --- a/server/util.go +++ b/server/util.go @@ -104,7 +104,7 @@ func interceptLoadConfig() (conf *cfg.Config, err error) { if conf == nil { conf, err = tcmd.ParseConfig() // NOTE: ParseConfig() creates dir/files as necessary. if err != nil { - panic("err") + panic("ERROR: Could not parse config") } } From 174f87e4367ebabc0b76ad2d14a16cff1a039ba0 Mon Sep 17 00:00:00 2001 From: Marko Baricevic Date: Fri, 21 Jun 2019 18:12:08 +0200 Subject: [PATCH 03/10] misspelling --- client/rpc/block.go | 2 +- client/rpc/validators.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/rpc/block.go b/client/rpc/block.go index 893c845763be..677aea721b07 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -123,7 +123,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { chainHeight, err := GetChainHeight(cliCtx) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, - "ERROR: Could not recieve chain height. ") + "ERROR: Could not receive chain height. ") return } if height > chainHeight { diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 2832c8ee5f81..4ea9333a6500 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -168,7 +168,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { chainHeight, err := GetChainHeight(cliCtx) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, - "ERROR: Could not recieve chain height.") + "ERROR: Could not receive chain height.") return } if height > chainHeight { From 920e218f0a10a92d38ec5d0b2f7e601520ffb443 Mon Sep 17 00:00:00 2001 From: Marko Baricevic Date: Fri, 21 Jun 2019 18:42:25 +0200 Subject: [PATCH 04/10] remove for --- client/input/input.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/client/input/input.go b/client/input/input.go index 0c5bf6b3b6c5..04be2e83ccca 100644 --- a/client/input/input.go +++ b/client/input/input.go @@ -87,23 +87,21 @@ func GetCheckPassword(prompt, prompt2 string, buf *bufio.Reader) (string, error) // "y", "Y", "yes", "YES", and "Yes" all count as confirmations. // If the input is not recognized, it returns false and a nil error. func GetConfirmation(prompt string, buf *bufio.Reader) (bool, error) { - for { - if inputIsTty() { - fmt.Print(fmt.Sprintf("%s [y/N]: ", prompt)) - } - - response, err := readLineFromBuf(buf) - if err != nil { - return false, err - } - - response = strings.ToLower(strings.TrimSpace(response)) - if response[0] == 'y' { - return true, nil - } else { - return false, nil - } + if inputIsTty() { + fmt.Print(fmt.Sprintf("%s [y/N]: ", prompt)) + } + + response, err := readLineFromBuf(buf) + if err != nil { + return false, err } + + response = strings.ToLower(strings.TrimSpace(response)) + if response[0] == 'y' { + return true, nil + } + + return false, nil } // GetString simply returns the trimmed string output of a given reader. From 0ab12f522fcd553d30993bf59c472435c33aecc7 Mon Sep 17 00:00:00 2001 From: Marko Baricevic Date: Fri, 21 Jun 2019 19:01:17 +0200 Subject: [PATCH 05/10] remove lint in circle-ci, status --- .circleci/config.yml | 19 ------------------- client/rpc/block.go | 8 +++----- client/rpc/validators.go | 7 +++---- server/util.go | 2 +- 4 files changed, 7 insertions(+), 29 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index aab3834603aa..b4cdb51199d0 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -50,22 +50,6 @@ jobs: - bin - profiles - lint: - <<: *linux_defaults - parallelism: 1 - steps: - - attach_workspace: - at: /tmp/workspace - - checkout - - restore_cache: - keys: - - go-mod-v1-{{ checksum "go.sum" }} - - run: - name: Lint source - command: | - export PATH=/tmp/workspace/bin:$PATH - make ci-lint - test_sim_app_nondeterminism: <<: *linux_defaults parallelism: 1 @@ -284,9 +268,6 @@ workflows: tags: only: - /^v.*/ - - lint: - requires: - - setup_dependencies - test_sim_app_nondeterminism: requires: - setup_dependencies diff --git a/client/rpc/block.go b/client/rpc/block.go index 677aea721b07..65ae8ac07aa7 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -117,18 +117,16 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { rest.WriteErrorResponse(w, http.StatusBadRequest, - "ERROR: Couldn't parse block height. Assumed format is '/block/{height}'.") + "couldn't parse block height. Assumed format is '/block/{height}'.") return } chainHeight, err := GetChainHeight(cliCtx) if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, - "ERROR: Could not receive chain height. ") + rest.WriteErrorResponse(w, http.StatusInternalServerError, "could not receive chain height. ") return } if height > chainHeight { - rest.WriteErrorResponse(w, http.StatusNotFound, - "ERROR: Requested block height is bigger then the chain length.") + rest.WriteErrorResponse(w, http.StatusNotFound, "requested block height is bigger then the chain length.") return } output, err := getBlock(cliCtx, &height) diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 4ea9333a6500..b6d5e6ed49b0 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -161,18 +161,17 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, "ERROR: Couldn't parse block height. Assumed format is '/validatorsets/{height}'.") + rest.WriteErrorResponse(w, http.StatusBadRequest, "couldn't parse block height. Assumed format is '/validatorsets/{height}'.") return } chainHeight, err := GetChainHeight(cliCtx) if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, - "ERROR: Could not receive chain height.") + rest.WriteErrorResponse(w, http.StatusInternalServerError, "could not receive chain height.") return } if height > chainHeight { - rest.WriteErrorResponse(w, http.StatusNotFound, "ERROR: Requested block height is bigger then the chain length.") + rest.WriteErrorResponse(w, http.StatusNotFound, "requested block height is bigger then the chain length.") return } diff --git a/server/util.go b/server/util.go index 624acce94620..d74b25bfde35 100644 --- a/server/util.go +++ b/server/util.go @@ -104,7 +104,7 @@ func interceptLoadConfig() (conf *cfg.Config, err error) { if conf == nil { conf, err = tcmd.ParseConfig() // NOTE: ParseConfig() creates dir/files as necessary. if err != nil { - panic("ERROR: Could not parse config") + panic(err) } } From ace0e87d0b89a66e9c74f1652b8c4d2b06530df6 Mon Sep 17 00:00:00 2001 From: Marko Baricevic Date: Fri, 21 Jun 2019 19:06:32 +0200 Subject: [PATCH 06/10] added dontcover --- client/rpc/block.go | 2 +- tests/util.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/client/rpc/block.go b/client/rpc/block.go index 65ae8ac07aa7..c34b9ef52a1c 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -122,7 +122,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } chainHeight, err := GetChainHeight(cliCtx) if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, "could not receive chain height. ") + rest.WriteErrorResponse(w, http.StatusInternalServerError, "could not receive chain height.") return } if height > chainHeight { diff --git a/tests/util.go b/tests/util.go index 73d63c513d6c..4ca2192859e0 100644 --- a/tests/util.go +++ b/tests/util.go @@ -215,3 +215,5 @@ var cdc = codec.New() func init() { ctypes.RegisterAmino(cdc) } + +//DONTCOVER From 5ae13f7ae6e055a4c9bee927a98ae585c4dafad8 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Fri, 21 Jun 2019 13:50:53 -0400 Subject: [PATCH 07/10] Update client/rpc/validators.go --- client/rpc/validators.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rpc/validators.go b/client/rpc/validators.go index b6d5e6ed49b0..4e61a57c8979 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -161,7 +161,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { height, err := strconv.ParseInt(vars["height"], 10, 64) if err != nil { - rest.WriteErrorResponse(w, http.StatusBadRequest, "couldn't parse block height. Assumed format is '/validatorsets/{height}'.") + rest.WriteErrorResponse(w, http.StatusBadRequest, "failed to parse block height; assumed format is '/validatorsets/{height}'") return } From faa1c19aca63d7c0234758c032887f6d80f71ec6 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Fri, 21 Jun 2019 13:51:17 -0400 Subject: [PATCH 08/10] Update client/rpc/validators.go --- client/rpc/validators.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 4e61a57c8979..9a5a35915d8a 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -171,7 +171,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } if height > chainHeight { - rest.WriteErrorResponse(w, http.StatusNotFound, "requested block height is bigger then the chain length.") + rest.WriteErrorResponse(w, http.StatusNotFound, "requested block height is bigger then the chain length") return } From d645f177709491ac578f8c798b4668ccad75f1ae Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Fri, 21 Jun 2019 13:51:37 -0400 Subject: [PATCH 09/10] Update client/rpc/block.go --- client/rpc/block.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/rpc/block.go b/client/rpc/block.go index c34b9ef52a1c..10863226c282 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -126,7 +126,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { return } if height > chainHeight { - rest.WriteErrorResponse(w, http.StatusNotFound, "requested block height is bigger then the chain length.") + rest.WriteErrorResponse(w, http.StatusNotFound, "requested block height is bigger then the chain length") return } output, err := getBlock(cliCtx, &height) From 521b0cf3cd5695a6a43935226b2078c5a3e05205 Mon Sep 17 00:00:00 2001 From: Marko Baricevic Date: Sat, 22 Jun 2019 09:40:51 +0200 Subject: [PATCH 10/10] remove periods --- client/rpc/block.go | 2 +- client/rpc/validators.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/rpc/block.go b/client/rpc/block.go index 10863226c282..b8581a32be63 100644 --- a/client/rpc/block.go +++ b/client/rpc/block.go @@ -122,7 +122,7 @@ func BlockRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } chainHeight, err := GetChainHeight(cliCtx) if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, "could not receive chain height.") + rest.WriteErrorResponse(w, http.StatusInternalServerError, "failed to parse chain height") return } if height > chainHeight { diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 9a5a35915d8a..5aaa6985c506 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -167,7 +167,7 @@ func ValidatorSetRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { chainHeight, err := GetChainHeight(cliCtx) if err != nil { - rest.WriteErrorResponse(w, http.StatusInternalServerError, "could not receive chain height.") + rest.WriteErrorResponse(w, http.StatusInternalServerError, "failed to parse chain height") return } if height > chainHeight {