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

lint: enable staticcheck #1956

Merged
merged 7 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ linters:
# - revive <- temporarily disabled while jacob figures out how to make poolId not trigger it.
# - rowserrcheck <- needs go 1.18 support
# - sqlclosecheck <- needs go 1.18 support
# - staticcheck <- later
- staticcheck # set of rules from staticcheck, subset of staticcheck binary.
# - structcheck <- later
- stylecheck # replacement of golint, subset of staticcheck binary.
# - tagliatelle <- disabled for defying cosmos idiom
Expand Down Expand Up @@ -102,6 +102,9 @@ issues:
# across its files, once stableswap is fully complete.
- unused
- deadcode
- linters:
- staticcheck
text: "SA1024: cutset contains duplicate characters" # proved to not provide much value, only false positives.
- linters:
- stylecheck
text: "ST1003:" # requires identifiers with "id" to be "ID".
Expand Down
10 changes: 0 additions & 10 deletions cmd/osmosisd/cmd/balances_from_state_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -141,7 +140,6 @@ func getGenStateFromPath(genesisFilePath string) (map[string]json.RawMessage, er
}

// ExportAirdropSnapshotCmd generates a snapshot.json from a provided exported genesis.json.
//nolint:ineffassign // because of accounts = authtypes.SanitizeGenesisAccounts(accounts)
func ExportDeriveBalancesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export-derive-balances [input-genesis-file] [output-snapshot-json]",
Expand Down Expand Up @@ -177,14 +175,6 @@ Example:
}
}

authGenesis := authtypes.GenesisState{}
clientCtx.Codec.MustUnmarshalJSON(genState["auth"], &authGenesis)
accounts, err := authtypes.UnpackAccounts(authGenesis.Accounts)
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}
accounts = authtypes.SanitizeGenesisAccounts(accounts)

// Produce the map of address to total atom balance, both staked and UnbondingStake
snapshotAccs := make(map[string]DerivedAccount)

Expand Down
19 changes: 19 additions & 0 deletions cmd/osmosisd/cmd/forceprune.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func pruneBlockStoreAndGetHeights(dbPath string, fullHeight int64) (
if err != nil {
return 0, 0, err
}

// nolint: staticcheck
defer db_bs.Close()

bs := tmstore.NewBlockStore(db_bs)
Expand All @@ -117,6 +119,15 @@ func pruneBlockStoreAndGetHeights(dbPath string, fullHeight int64) (
return 0, 0, err
}
fmt.Println("Pruned Block Store ...", prunedBlocks)

// N.B: We duplicate the call to db_bs.Close() on top of
// the call in defer statement above to make sure that the resources
// are properly released and any potential error from Close()
// is handled. Close() should be idempotent so this is acceptable.
if err := db_bs.Close(); err != nil {
return 0, 0, err
}

return startHeight, currentHeight, nil
}

Expand All @@ -128,13 +139,21 @@ func compactBlockStore(dbPath string) error {
fmt.Println("Compacting Block Store ...")

db, err := leveldb.OpenFile(dbPath+"/blockstore.db", &compactOpts)
// nolint: staticcheck
defer db.Close()
if err != nil {
return err
}
if err = db.CompactRange(*util.BytesPrefix([]byte{})); err != nil {
return err
}
// N.B: We duplicate the call to db.Close() on top of
// the call in defer statement above to make sure that the resources
// are properly released and any potential error from Close()
// is handled. Close() should be idempotent so this is acceptable.
if err := db.Close(); err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't get why are we doing this? The defer handles this more cleanly?

Copy link
Member Author

Choose a reason for hiding this comment

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

If we do defer db.Close(), it does not do error handling.

The new change validates the error while keeping defer db.Close() as well. The code in defer runs even if the code panics, making sure that we always close the resources. Calling this twice should not be a problem because Close() is idempotent

Copy link
Member

Choose a reason for hiding this comment

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

Can we instead do a named return error, (e.g. func compactBlockStore(dbPath string) (rerr error), and then do defer rerr = db.Close(). This is the general pattern for handling errors in a panic.

Encouraging db closing being isolated scope / not handled multiple times is ideal imo.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sweet, yeah followed your advice

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/osmosisd/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ Example:

// run Prepare Genesis
appState, genDoc, err = PrepareGenesis(clientCtx, appState, genDoc, genesisParams, chainID)
if err != nil {
return err
}

// validate genesis state
if err = mbm.ValidateGenesis(cdc, clientCtx.TxConfig, appState); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/osmosisd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -40,6 +41,8 @@ var (
flagOutputDir = "output-dir"
flagNodeDaemonHome = "node-daemon-home"
flagStartingIPAddress = "starting-ip-address"

emptyMnemonic = ""
)

// get cmd to initialize all files for tendermint testnet and application.
Expand Down Expand Up @@ -180,7 +183,7 @@ func InitTestnet(
return err
}

addr, secret, err := server.GenerateSaveCoinKey(kb, nodeDirName, true, algo)
addr, secret, err := testutil.GenerateSaveCoinKey(kb, nodeDirName, emptyMnemonic, true, algo)
if err != nil {
_ = os.RemoveAll(outputDir)
return err
Expand Down
2 changes: 0 additions & 2 deletions x/epochs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
// NewHandler returns a handler for epochs module messages.
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO we shouldn't even have these handler.go files -- it's all legacy code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm guessing this: cosmos/cosmos-sdk#9650 would be the right way to do it?


switch msg := msg.(type) {
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
Expand Down
3 changes: 0 additions & 3 deletions x/incentives/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,5 @@ func (gs GenesisState) Validate() error {
if gs.Params.DistrEpochIdentifier == "" {
return errors.New("epoch identifier should NOT be empty")
}
if gs.LastGaugeId < 0 {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("lock gauge lock id should be non-negative")
}
return nil
}
5 changes: 0 additions & 5 deletions x/lockup/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package types

import "errors"

// DefaultIndex is the default capability global index.
const DefaultIndex uint64 = 1

Expand All @@ -13,8 +11,5 @@ func DefaultGenesis() *GenesisState {
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
if gs.LastLockId < 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a uint so it cannot be less than 0

return errors.New("lock last lock id should be non-negative")
}
return nil
}