Skip to content

Commit

Permalink
Merge PR cosmos#1438: Tools: Add errcheck linter
Browse files Browse the repository at this point in the history
This linter ensures that all errors are checked.
This is disabled in the client directories, since its not needed on
those writes
  • Loading branch information
ValarDragon authored and cwgoes committed Jun 28, 2018
1 parent 7f59aa2 commit ac3adff
Show file tree
Hide file tree
Showing 30 changed files with 193 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ FEATURES
* go vet -composites=false
* unconvert
* ineffassign
* errcheck
* [server] Default config now creates a profiler at port 6060, and increase p2p send/recv rates
* [tests] Add WaitForNextNBlocksTM helper method
* [types] Switches internal representation of Int/Uint/Rat to use pointers
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ test_cover:

test_lint:
gometalinter.v2 --disable-all --enable='golint' --enable='misspell' --enable='unconvert' --enable='ineffassign' --linter='vet:go vet -composites=false:PATH:LINE:MESSAGE' --enable='vet' --deadline=500s --vendor ./...
!(gometalinter.v2 --disable-all --enable='errcheck' --vendor ./... | grep -v "client/")
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s

benchmark:
Expand Down
10 changes: 8 additions & 2 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,19 @@ func (app *BaseApp) Router() Router { return app.router }

// load latest application version
func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error {
app.cms.LoadLatestVersion()
err := app.cms.LoadLatestVersion()
if err != nil {
return err
}
return app.initFromStore(mainKey)
}

// load application version
func (app *BaseApp) LoadVersion(version int64, mainKey sdk.StoreKey) error {
app.cms.LoadVersion(version)
err := app.cms.LoadVersion(version)
if err != nil {
return err
}
return app.initFromStore(mainKey)
}

Expand Down
12 changes: 10 additions & 2 deletions baseapp/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ func RunForever(app abci.Application) {
srv, err := server.NewServer("0.0.0.0:26658", "socket", app)
if err != nil {
cmn.Exit(err.Error())
return
}
err = srv.Start()
if err != nil {
cmn.Exit(err.Error())
return
}
srv.Start()

// Wait forever
cmn.TrapSignal(func() {
// Cleanup
srv.Stop()
err := srv.Stop()
if err != nil {
cmn.Exit(err.Error())
}
})
}
6 changes: 5 additions & 1 deletion cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,9 @@ func main() {

// prepare and add flags
executor := cli.PrepareMainCmd(rootCmd, "GA", app.DefaultCLIHome)
executor.Execute()
err := executor.Execute()
if err != nil {
// handle with #870
panic(err)
}
}
6 changes: 5 additions & 1 deletion cmd/gaia/cmd/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func main() {

// prepare and add flags
executor := cli.PrepareBaseCmd(rootCmd, "GA", app.DefaultNodeHome)
executor.Execute()
err := executor.Execute()
if err != nil {
// handle with #870
panic(err)
}
}

func newApp(logger log.Logger, db dbm.DB) abci.Application {
Expand Down
6 changes: 5 additions & 1 deletion examples/basecoin/cmd/basecli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,9 @@ func main() {

// prepare and add flags
executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/.basecli"))
executor.Execute()
err := executor.Execute()
if err != nil {
// handle with #870
panic(err)
}
}
6 changes: 5 additions & 1 deletion examples/basecoin/cmd/basecoind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ func main() {
// prepare and add flags
rootDir := os.ExpandEnv("$HOME/.basecoind")
executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir)
executor.Execute()
err := executor.Execute()
if err != nil {
// handle with #870
panic(err)
}
}

func newApp(logger log.Logger, db dbm.DB) abci.Application {
Expand Down
6 changes: 5 additions & 1 deletion examples/democoin/cmd/democli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,9 @@ func main() {

// prepare and add flags
executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/.democli"))
executor.Execute()
err := executor.Execute()
if err != nil {
// handle with #870
panic(err)
}
}
6 changes: 5 additions & 1 deletion examples/democoin/cmd/democoind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,9 @@ func main() {
// prepare and add flags
rootDir := os.ExpandEnv("$HOME/.democoind")
executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir)
executor.Execute()
err := executor.Execute()
if err != nil {
// handle with #870
panic(err)
}
}
10 changes: 8 additions & 2 deletions examples/kvstore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ func main() {
fmt.Println(err)
os.Exit(1)
}
srv.Start()
err = srv.Start()
if err != nil {
cmn.Exit(err.Error())
}

// Wait forever
cmn.TrapSignal(func() {
// Cleanup
srv.Stop()
err = srv.Stop()
if err != nil {
cmn.Exit(err.Error())
}
})
return
}
Expand Down
8 changes: 4 additions & 4 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func GenTxCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
viper.GetBool(FlagOWK),
ip,
}
cliPrint, genTxFile, err := gentxWithConfig(ctx, cdc, appInit, config, genTxConfig)
cliPrint, genTxFile, err := gentxWithConfig(cdc, appInit, config, genTxConfig)
if err != nil {
return err
}
Expand All @@ -112,7 +112,7 @@ func GenTxCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
return cmd
}

func gentxWithConfig(ctx *Context, cdc *wire.Codec, appInit AppInit, config *cfg.Config, genTxConfig serverconfig.GenTx) (
func gentxWithConfig(cdc *wire.Codec, appInit AppInit, config *cfg.Config, genTxConfig serverconfig.GenTx) (
cliPrint json.RawMessage, genTxFile json.RawMessage, err error) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
Expand Down Expand Up @@ -169,7 +169,7 @@ func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
viper.GetBool(FlagOverwrite),
}

chainID, nodeID, appMessage, err := initWithConfig(ctx, cdc, appInit, config, initConfig)
chainID, nodeID, appMessage, err := initWithConfig(cdc, appInit, config, initConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
return cmd
}

func initWithConfig(ctx *Context, cdc *wire.Codec, appInit AppInit, config *cfg.Config, initConfig InitConfig) (
func initWithConfig(cdc *wire.Codec, appInit AppInit, config *cfg.Config, initConfig InitConfig) (
chainID string, nodeID string, appMessage json.RawMessage, err error) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion server/mock/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mock

import (
"fmt"
"io/ioutil"
"os"

Expand All @@ -19,7 +20,10 @@ func SetupApp() (abci.Application, func(), error) {
}

cleanup := func() {
os.RemoveAll(rootDir)
err := os.RemoveAll(rootDir)
if err != nil {
fmt.Printf("could not delete %s, had error %s\n", rootDir, err.Error())
}
}

app, err := NewApp(rootDir, logger)
Expand Down
10 changes: 8 additions & 2 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@ func startStandAlone(ctx *Context, appCreator AppCreator) error {
return errors.Errorf("error creating listener: %v\n", err)
}
svr.SetLogger(ctx.Logger.With("module", "abci-server"))
svr.Start()
err = svr.Start()
if err != nil {
cmn.Exit(err.Error())
}

// Wait forever
cmn.TrapSignal(func() {
// Cleanup
svr.Stop()
err = svr.Stop()
if err != nil {
cmn.Exit(err.Error())
}
})
return nil
}
Expand Down
17 changes: 15 additions & 2 deletions server/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ func FreeTCPAddr() (addr, port string, err error) {
if err != nil {
return "", "", err
}
defer l.Close()

closer := func() {
err := l.Close()
if err != nil {
// TODO: Handle with #870
panic(err)
}
}

defer closer()

portI := l.Addr().(*net.TCPAddr).Port
port = fmt.Sprintf("%d", portI)
Expand All @@ -36,7 +45,11 @@ func setupViper(t *testing.T) func() {
require.Nil(t, err)
viper.Set(cli.HomeFlag, rootDir)
return func() {
os.RemoveAll(rootDir)
err := os.RemoveAll(rootDir)
if err != nil {
// TODO: Handle with #870
panic(err)
}
}
}

Expand Down
11 changes: 6 additions & 5 deletions server/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (

gc "github.com/cosmos/cosmos-sdk/server/config"

"os"

"github.com/cosmos/cosmos-sdk/wire"
"github.com/spf13/viper"
cfg "github.com/tendermint/tendermint/config"
cmn "github.com/tendermint/tmlibs/common"
"os"
)

var (
Expand Down Expand Up @@ -42,7 +43,7 @@ Example:
`,
RunE: func(_ *cobra.Command, _ []string) error {
config := ctx.Config
err := testnetWithConfig(config, ctx, cdc, appInit)
err := testnetWithConfig(config, cdc, appInit)
return err
},
}
Expand All @@ -58,7 +59,7 @@ Example:
return cmd
}

func testnetWithConfig(config *cfg.Config, ctx *Context, cdc *wire.Codec, appInit AppInit) error {
func testnetWithConfig(config *cfg.Config, cdc *wire.Codec, appInit AppInit) error {

outDir := viper.GetString(outputDir)
// Generate private key, node ID, initial transaction
Expand Down Expand Up @@ -104,7 +105,7 @@ func testnetWithConfig(config *cfg.Config, ctx *Context, cdc *wire.Codec, appIni
}

// Run `init gen-tx` and generate initial transactions
cliPrint, genTxFile, err := gentxWithConfig(ctx, cdc, appInit, config, genTxConfig)
cliPrint, genTxFile, err := gentxWithConfig(cdc, appInit, config, genTxConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -154,7 +155,7 @@ func testnetWithConfig(config *cfg.Config, ctx *Context, cdc *wire.Codec, appIni
config.SetRoot(nodeDir)

// Run `init` and generate genesis.json and config.toml
_, _, _, err := initWithConfig(ctx, cdc, appInit, config, initConfig)
_, _, _, err := initWithConfig(cdc, appInit, config, initConfig)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func PersistentPreRunEFn(context *Context) func(*cobra.Command, []string) error
// If a new config is created, change some of the default tendermint settings
func interceptLoadConfig() (conf *cfg.Config, err error) {
tmpConf := cfg.DefaultConfig()
viper.Unmarshal(tmpConf)
err = viper.Unmarshal(tmpConf)
if err != nil {
// TODO: Handle with #870
panic(err)
}
rootDir := tmpConf.RootDir
configFilePath := filepath.Join(rootDir, "config/config.toml")
// Intercept only if the file doesn't already exist
Expand Down
6 changes: 5 additions & 1 deletion store/iavlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func (st *iavlStore) Commit() CommitID {
// Release an old version of history
if st.numHistory > 0 && (st.numHistory < st.tree.Version64()) {
toRelease := version - st.numHistory
st.tree.DeleteVersion(toRelease)
err := st.tree.DeleteVersion(toRelease)
if err != nil {
// TODO: Handle with #870
panic(err)
}
}

return CommitID{
Expand Down
6 changes: 5 additions & 1 deletion store/rootmultistore.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ func (si storeInfo) Hash() []byte {
// include them via the keys.
bz, _ := cdc.MarshalBinary(si.Core) // Does not error
hasher := ripemd160.New()
hasher.Write(bz)
_, err := hasher.Write(bz)
if err != nil {
// TODO: Handle with #870
panic(err)
}
return hasher.Sum(nil)
}

Expand Down
10 changes: 8 additions & 2 deletions tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ func waitForHeight(height int64, url string) {
if err != nil {
panic(err)
}
res.Body.Close()
err = res.Body.Close()
if err != nil {
panic(err)
}

var resultBlock ctypes.ResultBlock
err = cdc.UnmarshalJSON(body, &resultBlock)
Expand Down Expand Up @@ -136,7 +139,10 @@ func WaitForStart(port string) {

// waiting for server to start ...
if res.StatusCode != http.StatusOK {
res.Body.Close()
err = res.Body.Close()
if err != nil {
panic(err)
}
return
}
}
Expand Down
Loading

0 comments on commit ac3adff

Please sign in to comment.