Skip to content

Commit

Permalink
Introduce golangci-lint (#222)
Browse files Browse the repository at this point in the history

Co-authored-by: Vitalis Salis <[email protected]>
  • Loading branch information
faddat and vitsalis authored Dec 13, 2022
1 parent 00924d4 commit d01783e
Show file tree
Hide file tree
Showing 56 changed files with 290 additions and 141 deletions.
7 changes: 6 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ jobs:
key: go-mod-v6-{{ checksum "go.sum" }}
paths:
- "/home/circleci/.go_workspace/pkg/mod"
- run:
name: Lint
command: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.50.1
./bin/golangci-lint run
- run:
name: Run tests
command: |
Expand All @@ -43,6 +48,6 @@ jobs:
# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
build-and-test:
build-lint-test:
jobs:
- build
103 changes: 103 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
run:
timeout: 5m
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- cyclop
- decorder
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exhaustivestruct
- exhaustruct
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- gci
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofumpt
- goheader
- golint
- gomnd
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- grouper
- ifshort
- importas
- ineffassign
- interfacebloat
- interfacer
- ireturn
- lll
- loggercheck
- maintidx
- makezero
- maligned
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosnakecase
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- scopelint
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
- tagliatelle
- tenv
- testableexamples
- testpackage
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- varcheck
- varnamelen
- wastedassign
- whitespace
- wrapcheck
- wsl

issues:
max-same-issues: 0
8 changes: 4 additions & 4 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (app *BabylonApp) 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 favour of export at a block height
func (app *BabylonApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []string) {
applyAllowedAddrs := false

Expand Down Expand Up @@ -109,7 +109,7 @@ func (app *BabylonApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddr
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
app.DistrKeeper.SetFeePool(ctx, feePool)

app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()) //nolint:errcheck // either we ignore the error here or propogate up the stack
return false
})

Expand All @@ -123,8 +123,8 @@ func (app *BabylonApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddr
if err != nil {
panic(err)
}
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr)
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr)
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr) //nolint:errcheck // either we ignore the error here or propogate up the stack
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr) //nolint:errcheck // either we ignore the error here or propogate up the stack
}

// reset context height
Expand Down
12 changes: 7 additions & 5 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
tmconfig "github.com/tendermint/tendermint/config"
"math/rand"
"strconv"
"testing"
"time"

"cosmossdk.io/math"
tmconfig "github.com/tendermint/tendermint/config"

"github.com/babylonchain/babylon/app/params"
appparams "github.com/babylonchain/babylon/app/params"
"github.com/babylonchain/babylon/testutil/datagen"
Expand Down Expand Up @@ -394,7 +396,7 @@ func createIncrementalAccounts(accNum int) []sdk.AccAddress {
}

// AddTestAddrsFromPubKeys adds the addresses into the BabylonApp providing only the public keys.
func AddTestAddrsFromPubKeys(app *BabylonApp, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt sdk.Int) {
func AddTestAddrsFromPubKeys(app *BabylonApp, ctx sdk.Context, pubKeys []cryptotypes.PubKey, accAmt math.Int) {
initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))

for _, pk := range pubKeys {
Expand All @@ -404,17 +406,17 @@ func AddTestAddrsFromPubKeys(app *BabylonApp, ctx sdk.Context, pubKeys []cryptot

// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrs(app *BabylonApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
func AddTestAddrs(app *BabylonApp, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
return addTestAddrs(app, ctx, accNum, accAmt, createRandomAccounts)
}

// AddTestAddrs constructs and returns accNum amount of accounts with an
// initial balance of accAmt in random order
func AddTestAddrsIncremental(app *BabylonApp, ctx sdk.Context, accNum int, accAmt sdk.Int) []sdk.AccAddress {
func AddTestAddrsIncremental(app *BabylonApp, ctx sdk.Context, accNum int, accAmt math.Int) []sdk.AccAddress {
return addTestAddrs(app, ctx, accNum, accAmt, createIncrementalAccounts)
}

func addTestAddrs(app *BabylonApp, ctx sdk.Context, accNum int, accAmt sdk.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
func addTestAddrs(app *BabylonApp, ctx sdk.Context, accNum int, accAmt math.Int, strategy GenerateAccountStrategy) []sdk.AccAddress {
testAddrs := strategy(accNum)

initCoins := sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.BondDenom(ctx), accAmt))
Expand Down
3 changes: 1 addition & 2 deletions app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"
Expand Down Expand Up @@ -128,5 +127,5 @@ func writeConfigToFile(configFilePath string, config *config.ClientConfig) error
return err
}

return ioutil.WriteFile(configFilePath, buffer.Bytes(), 0600)
return os.WriteFile(configFilePath, buffer.Bytes(), 0600)
}
2 changes: 1 addition & 1 deletion btctxformatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (header *formatHeader) validateHeader(
expectedPart uint8,
) error {
if !bytes.Equal(header.tag, expectedTag) {
return errors.New(fmt.Sprintf("data does not have expected tag, expected tag: %v, got tag: %v", expectedTag, header.tag))
return fmt.Errorf("data does not have expected tag, expected tag: %v, got tag: %v", expectedTag, header.tag)
}

if header.version > CurrentVersion {
Expand Down
10 changes: 4 additions & 6 deletions cmd/babylond/cmd/genbls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ package cmd

import (
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/babylonchain/babylon/app"
"github.com/babylonchain/babylon/privval"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
tmconfig "github.com/tendermint/tendermint/config"
tmos "github.com/tendermint/tendermint/libs/os"
"path/filepath"
"strings"
)

func GenBlsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create-genesis-bls",
Short: "Create genesis BLS key file for the validator",
Long: strings.TrimSpace(
fmt.Sprintf(`genbls will create a BLS key file that consists of
Long: strings.TrimSpace(`genbls will create a BLS key file that consists of
{address, bls_pub_key, pop, pub_key} where pop is the proof-of-possession that proves
the ownership of bls_pub_key which is bonded with pub_key.
Expand All @@ -29,7 +28,6 @@ and the existence of priv_validator_key.json which contains the validator privat
Example:
$ babylond genbls --home ./
`),
),

RunE: func(cmd *cobra.Command, args []string) error {
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
Expand Down
3 changes: 3 additions & 0 deletions cmd/babylond/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ Example:
}

appState, genDoc, err = PrepareGenesis(clientCtx, appState, genDoc, genesisParams, chainID)
if err != nil {
return fmt.Errorf("failed to prepare genesis: %w", err)
}

if err = mbm.ValidateGenesis(clientCtx.Codec, clientCtx.TxConfig, appState); err != nil {
return fmt.Errorf("error validating genesis file: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/babylond/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a

homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
snapshotDir := filepath.Join(homeDir, "data", "snapshots")
snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir)
snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir) //nolint:staticcheck // Prefer the old-fashioned way.
if err != nil {
panic(err)
}
Expand Down
5 changes: 4 additions & 1 deletion crypto/bls12381/bls_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package bls12381

import (
"github.com/stretchr/testify/require"
"testing"

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

// Tests single BLS sig verification
Expand Down Expand Up @@ -86,7 +87,9 @@ func TestAccumulativeAggregation(t *testing.T) {
}
sig := Sign(sks[n-1], msgb)
aggSig, err = AggrSig(aggSig, sig)
require.Nil(t, err)
aggPK, err = AggrPK(aggPK, pks[n-1])
require.Nil(t, err)
res, err = Verify(aggSig, aggPK, msga)
require.False(t, res)
require.Nil(t, err)
Expand Down
10 changes: 6 additions & 4 deletions privval/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package privval
import (
"errors"
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/codec"
"io/ioutil"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/crypto/codec"

sdk "github.com/cosmos/cosmos-sdk/types"
tmcrypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
Expand All @@ -31,6 +31,8 @@ const (
)

// copied from github.com/tendermint/tendermint/privval/file.go"
//
//nolint:unused
func voteToStep(vote *tmproto.Vote) int8 {
switch vote.Type {
case tmproto.PrevoteType:
Expand Down Expand Up @@ -116,7 +118,7 @@ func LoadWrappedFilePVEmptyState(keyFilePath, stateFilePath string) *WrappedFile

// If loadState is true, we load from the stateFilePath. Otherwise, we use an empty LastSignState.
func loadWrappedFilePV(keyFilePath, stateFilePath string, loadState bool) *WrappedFilePV {
keyJSONBytes, err := ioutil.ReadFile(keyFilePath)
keyJSONBytes, err := os.ReadFile(keyFilePath)
if err != nil {
tmos.Exit(err.Error())
}
Expand All @@ -135,7 +137,7 @@ func loadWrappedFilePV(keyFilePath, stateFilePath string, loadState bool) *Wrapp
pvState := privval.FilePVLastSignState{}

if loadState {
stateJSONBytes, err := ioutil.ReadFile(stateFilePath)
stateJSONBytes, err := os.ReadFile(stateFilePath)
if err != nil {
tmos.Exit(err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions simapp/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"time"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -59,7 +59,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty

case config.ParamsFile != "":
appParams := make(simtypes.AppParams)
bz, err := ioutil.ReadFile(config.ParamsFile)
bz, err := os.ReadFile(config.ParamsFile)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func AppStateRandomizedFn(
// AppStateFromGenesisFileFn util function to generate the genesis AppState
// from a genesis.json file.
func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile string) (tmtypes.GenesisDoc, []simtypes.Account) {
bytes, err := ioutil.ReadFile(genesisFile)
bytes, err := os.ReadFile(genesisFile)
if err != nil {
panic(err)
}
Expand Down
Loading

0 comments on commit d01783e

Please sign in to comment.