Skip to content

Commit

Permalink
chore: update and configure additional linters in golangci
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code committed Oct 19, 2024
1 parent 0fa0ddc commit 4115a84
Show file tree
Hide file tree
Showing 127 changed files with 691 additions and 631 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- run: |
curl -sLO https://github.com/BurntSushi/ripgrep/releases/download/14.1.0/ripgrep_14.1.0-1_amd64.deb
sudo dpkg -i ripgrep_14.1.0-1_amd64.deb
- run: ./scripts/linter.sh
- run: make custom-lint

lint:
runs-on: ubuntu-latest
Expand Down
89 changes: 81 additions & 8 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ run:
timeout: 5m
tests: true
go: '1.23'
sort-results: true
allow-parallel-runners: true

issues:
max-same-issues: 10
exclude-dirs:
- 'types/legacy'
exclude-files:
- '.*\\legacy\\_.*\\.go$'
- '.*\\legacy\\.go$'
- '.*\\.sol\\.go$'
- ".*\\.pb\\.go$"
- ".*\\.pb\\.gw\\.go$"
- ".*\\.pulsar\\.go$"
Expand All @@ -25,27 +30,43 @@ linters:
- dogsled
- gosec
- errcheck
# - goconst
- goconst
- gosimple
- govet
- ineffassign
- misspell
# - nakedret
- nakedret
- nolintlint
- staticcheck
- revive
- typecheck
- unused
- thelper
- copyloopvar
- wastedassign
- testifylint

linters-settings:
staticcheck:
checks: ["all", "-SA1019"]
checks: [ "all", "-SA1019" ]
stylecheck:
checks: ["all", "-ST1003"]
checks: [ "all", "-ST1003" ]
gocyclo:
min-complexity: 15
gocritic:
disabled-checks: ["assignOp", "ifElseChain", "appendAssign"]
disabled-checks: [ "assignOp", "ifElseChain", "appendAssign" ]
misspell:
locale: US
gofumpt:
extra-rules: true
dogsled:
max-blank-identifiers: 6
nolintlint:
allow-unused: false
require-explanation: true
require-specific: false
gosimple:
checks: [ "all" ]
gosec:
excludes: [ "G115" ]
gci:
Expand All @@ -59,11 +80,63 @@ linters-settings:
check-blank: false
revive:
# https://golangci-lint.run/usage/linters/#revive
enable-all-rules: false
enable-all-rules: true
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
rules:
- name: var-naming
- name: max-control-nesting
disabled: false
exclude:
- "**/doctor.go"
- name: blank-imports
disabled: false
exclude:
- "**/app.go"
- name: function-result-limit
disabled: false
arguments:
- 4
- name: unhandled-error
arguments:
- "fmt.Printf"
- "fmt.Println"
- "strings.Builder.WriteString"
- name: var-naming
arguments:
- [ "ID", "IDS", "URL", "JSON", "RPC" ] # AllowList
- [ "" ] # DenyList
- - upperCaseConst: true

- name: line-length-limit
disabled: true
- name: cyclomatic
disabled: true
- name: use-any
disabled: true
- name: add-constant
disabled: true
- name: import-shadowing
disabled: true
- name: function-length
disabled: true
- name: unused-parameter
disabled: true
- name: unused-receiver
disabled: true
- name: max-public-structs
disabled: true
- name: cognitive-complexity
disabled: true
- name: flag-parameter
disabled: true
- name: indent-error-flow
disabled: true
- name: argument-limit
disabled: true
- name: bare-return
disabled: true
- name: nested-structs
disabled: true
- name: unchecked-type-assertion
disabled: true
- name: comment-spacings
disabled: true
10 changes: 5 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ import (
_ "github.com/functionx/fx-core/v8/docs/statik"
fxcfg "github.com/functionx/fx-core/v8/server/config"
fxauth "github.com/functionx/fx-core/v8/server/grpc/auth"
gaspricev1 "github.com/functionx/fx-core/v8/server/grpc/gasprice/legacy/v1"
gaspricev2 "github.com/functionx/fx-core/v8/server/grpc/gasprice/legacy/v2"
gaspricev1 "github.com/functionx/fx-core/v8/server/grpc/gasprice/v1"
gaspricev2 "github.com/functionx/fx-core/v8/server/grpc/gasprice/v2"
fxtypes "github.com/functionx/fx-core/v8/types"
"github.com/functionx/fx-core/v8/x/crosschain"
"github.com/functionx/fx-core/v8/x/crosschain/keeper"
Expand Down Expand Up @@ -266,8 +266,8 @@ func New(

func (app *App) setAnteHandler(appOpts servertypes.AppOptions) {
maxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted))
BypassMinFeeMsgTypes := cast.ToStringSlice(appOpts.Get(fxcfg.BypassMinFeeMsgTypesKey))
MaxBypassMinFeeMsgGasUsage := cast.ToUint64(appOpts.Get(fxcfg.BypassMinFeeMsgMaxGasUsageKey))
bypassMinFeeMsgTypes := cast.ToStringSlice(appOpts.Get(fxcfg.BypassMinFeeMsgTypesKey))
maxBypassMinFeeMsgGasUsage := cast.ToUint64(appOpts.Get(fxcfg.BypassMinFeeMsgMaxGasUsageKey))
anteOptions := fxante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
Expand All @@ -278,7 +278,7 @@ func (app *App) setAnteHandler(appOpts servertypes.AppOptions) {
SignModeHandler: app.txConfig.SignModeHandler(),
SigGasConsumer: fxante.DefaultSigVerificationGasConsumer,
MaxTxGasWanted: maxGasWanted,
TxFeeChecker: fxante.NewCheckTxFeees(BypassMinFeeMsgTypes, MaxBypassMinFeeMsgGasUsage).Check,
TxFeeChecker: fxante.NewCheckTxFeees(bypassMinFeeMsgTypes, maxBypassMinFeeMsgGasUsage).Check,
DisabledAuthzMsgs: []string{
sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}),
sdk.MsgTypeURL(&vestingtypes.MsgCreateVestingAccount{}),
Expand Down
3 changes: 2 additions & 1 deletion app/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/testutil/helpers"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestRegisterInterfaces(t *testing.T) {
}
for _, implementation := range result.Msgs {
resolvedMsg, err := app.InterfaceRegistry().Resolve(implementation)
assert.NoError(t, err)
require.NoError(t, err)

if _, ok := resolvedMsg.(govProposalMsg); ok {
result.ProposalMsgs = append(result.ProposalMsgs, implementation)
Expand Down
4 changes: 2 additions & 2 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (app *App) ExportAppStateAndValidators(

// prepare for fresh start at zero height
// NOTE zero height genesis is a temporary feature which will be deprecated
// in favour of export at a block height
// in favor of export at a block height
//
//nolint:gocyclo
//nolint:gocyclo // copy from cosmos-sdk
func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
applyAllowedAddrs := false

Expand Down
3 changes: 3 additions & 0 deletions app/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func Test_ExportGenesisAndRunNode(t *testing.T) {
}

func exportGenesisDoc(t *testing.T, home string) *types.GenesisDoc {
t.Helper()
db, err := dbm.NewDB("application", dbm.GoLevelDBBackend, filepath.Join(home, "data"))
require.NoError(t, err)

Expand All @@ -126,6 +127,7 @@ func exportGenesisDoc(t *testing.T, home string) *types.GenesisDoc {
}

func updateGenesisState(t *testing.T, home string, cdc codec.Codec, genesisDoc *types.GenesisDoc) {
t.Helper()
appState := app.GenesisState{}
err := json.Unmarshal(genesisDoc.AppState, &appState)
require.NoError(t, err)
Expand Down Expand Up @@ -205,6 +207,7 @@ func updateSlashingGenesisState(cdc codec.Codec, appState app.GenesisState, newP
}

func newPrivValidatorKey(t *testing.T, home string) *codectypes.Any {
t.Helper()
privKeyFile := filepath.Join(home, "config/priv_validator_key.json")
privStateFile := filepath.Join(home, "data/priv_validator_state.json")
secret := tmrand.Bytes(32)
Expand Down
2 changes: 1 addition & 1 deletion app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type GenesisState map[string]json.RawMessage

// NewDefAppGenesisByDenom return new genesis state
//
//nolint:gocyclo
//nolint:gocyclo // a lot of modules need to be modified
func NewDefAppGenesisByDenom(cdc codec.JSONCodec, moduleBasics module.BasicManager) GenesisState {
denom := fxtypes.DefaultDenom
fxTotalSupply, ok := sdkmath.NewIntFromString(InitTotalSupply)
Expand Down
7 changes: 1 addition & 6 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keepers

import (
"os"

"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
evidencekeeper "cosmossdk.io/x/evidence/keeper"
Expand Down Expand Up @@ -166,8 +164,7 @@ func NewAppKeeper(
appKeepers.GenerateKeys()

if err := bApp.RegisterStreamingServices(appOpts, appKeepers.keys); err != nil {
logger.Error("failed to load state streaming", "err", err)
os.Exit(1)
panic(err)
}

appKeepers.ParamsKeeper = initParamsKeeper(
Expand Down Expand Up @@ -564,8 +561,6 @@ func NewAppKeeper(
appCodec,
appKeepers.keys[migratetypes.StoreKey],
appKeepers.AccountKeeper,
)
appKeepers.MigrateKeeper = appKeepers.MigrateKeeper.SetMigrateI(
migratekeeper.NewBankMigrate(appKeepers.BankKeeper),
migratekeeper.NewDistrStakingMigrate(appKeepers.keys[distrtypes.StoreKey], appKeepers.keys[stakingtypes.StoreKey], appKeepers.StakingKeeper),
migratekeeper.NewGovMigrate(appKeepers.GovKeeper, appKeepers.AccountKeeper),
Expand Down
3 changes: 2 additions & 1 deletion app/keepers/keepers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func TestNewAppKeeper(t *testing.T) {
checkStructField(t, valueOf, typeOf.Name())
}

//nolint:gocyclo
//nolint:gocyclo // for testing
func checkStructField(t *testing.T, valueOf reflect.Value, name string) {
t.Helper()
valueOf = reflect.Indirect(valueOf)
if valueOf.Kind() != reflect.Struct ||
valueOf.Type().String() == "baseapp.MsgServiceRouter" {
Expand Down
13 changes: 10 additions & 3 deletions app/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Test_UpgradeAndMigrate(t *testing.T) {
}

func buildApp(t *testing.T) *app.App {
t.Helper()
fxtypes.SetConfig(true)

home := filepath.Join(os.Getenv("HOME"), "tmp")
Expand All @@ -69,6 +70,7 @@ func buildApp(t *testing.T) *app.App {
}

func newContext(t *testing.T, myApp *app.App, chainId string, deliveState bool) sdk.Context {
t.Helper()
header := tmproto.Header{
ChainID: chainId,
Height: myApp.LastBlockHeight(),
Expand All @@ -89,14 +91,15 @@ func newContext(t *testing.T, myApp *app.App, chainId string, deliveState bool)
// set the first validator to proposer
validators, err := myApp.StakingKeeper.GetAllValidators(ctx)
require.NoError(t, err)
assert.True(t, len(validators) > 0)
assert.NotEmpty(t, validators)
var pubKey cryptotypes.PubKey
assert.NoError(t, myApp.AppCodec().UnpackAny(validators[0].ConsensusPubkey, &pubKey))
require.NoError(t, myApp.AppCodec().UnpackAny(validators[0].ConsensusPubkey, &pubKey))
ctx = ctx.WithProposer(pubKey.Address().Bytes())
return ctx
}

func checkAppUpgrade(t *testing.T, ctx sdk.Context, myApp *app.App) {
t.Helper()
checkStakingMigrationDelete(t, ctx, myApp)

checkGovCustomParams(t, ctx, myApp)
Expand All @@ -105,13 +108,15 @@ func checkAppUpgrade(t *testing.T, ctx sdk.Context, myApp *app.App) {
}

func checkErc20Keys(t *testing.T, ctx sdk.Context, myApp *app.App) {
t.Helper()
params, err := myApp.Erc20Keeper.Params.Get(ctx)
require.NoError(t, err)

require.True(t, params.EnableErc20)
}

func checkGovCustomParams(t *testing.T, ctx sdk.Context, myApp *app.App) {
t.Helper()
egfCustomParams, found := myApp.GovKeeper.GetCustomParams(ctx, sdk.MsgTypeURL(&distributiontypes.MsgCommunityPoolSpend{}))
require.True(t, found)
expectEGFParams := fxgovtypes.NewCustomParams(fxgovtypes.EGFCustomParamDepositRatio.String(), fxgovtypes.DefaultEGFCustomParamVotingPeriod, fxgovtypes.DefaultCustomParamQuorum40.String())
Expand All @@ -121,11 +126,13 @@ func checkGovCustomParams(t *testing.T, ctx sdk.Context, myApp *app.App) {
}

func checkStakingMigrationDelete(t *testing.T, ctx sdk.Context, myApp *app.App) {
t.Helper()
checkKeysIsDelete(t, ctx.KVStore(myApp.GetKey(stakingtypes.StoreKey)), fxstakingv8.GetRemovedStoreKeys())
}

func checkKeysIsDelete(t *testing.T, kvStore storetypes.KVStore, keys [][]byte) {
require.Greater(t, len(keys), 0)
t.Helper()
require.NotEmpty(t, keys)
checkFn := func(key []byte) {
iterator := storetypes.KVStorePrefixIterator(kvStore, key)
defer iterator.Close()
Expand Down
5 changes: 3 additions & 2 deletions client/cli/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/client/cli"
)
Expand Down Expand Up @@ -43,12 +44,12 @@ func TestToStringCmd(t *testing.T) {
cmd.SetOut(buf)
cmd.SetArgs(tt.args)
for k, v := range tt.flags {
assert.NoError(t, cmd.Flags().Set(k, v))
require.NoError(t, cmd.Flags().Set(k, v))
}
if err := cmd.Execute(); tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, tt.output, buf.String())
}
})
Expand Down
4 changes: 2 additions & 2 deletions client/cli/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ input
output
- armor encrypted private key (saved to file)
*/
//nolint:gocyclo
//nolint:gocyclo // copy from cosmos-sdk
func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *bufio.Reader) error {
name := args[0]
if strings.TrimSpace(name) == "" {
Expand Down Expand Up @@ -327,7 +327,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf
return printCreate(cmd, info, showMnemonic, showMnemonicIndiscreetly, mnemonic, outputFormat)
}

func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic, showMnemonicIndiscreetly bool, mnemonic string, outputFormat string) error {
func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic, showMnemonicIndiscreetly bool, mnemonic, outputFormat string) error {
switch outputFormat {
case OutputFormatText:
output, err := MkAccKeyOutput(k)
Expand Down
1 change: 1 addition & 0 deletions client/cli/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO
}

func cleanupKeys(t *testing.T, kb keyring.Keyring, keys ...string) func() {
t.Helper()
return func() {
for _, k := range keys {
if err := kb.Delete(k); err != nil {
Expand Down
Loading

0 comments on commit 4115a84

Please sign in to comment.