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

chore(lint): fix golangci config and linting errors #98

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 19 additions & 44 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
run:
timeout: 5m

linters:
disable-all: true
enable:
- asasalint
Expand All @@ -8,96 +10,69 @@ run:
- 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
# - nlreturn # Style wise I personally like this one, todo(lazar): unlax at somepoint, good practice
- 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
- unused
- wastedassign
- whitespace
- wrapcheck
- wsl
# - wrapcheck # we really should be using this, lax for now todo(lazar): unlax at somepoint, good practice

issues:
max-same-issues: 0
# Default: https://golangci-lint.run/usage/false-positives/#default-exclusions
exclude-dirs:
- e2etest
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
- path-except: _test\.go
linters:
- forbidigo
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

* [#94](https://github.com/babylonlabs-io/vigilante/pull/94) adds gosec and fixes gosec issues
* [#96](https://github.com/babylonlabs-io/vigilante/pull/96) fixes potential stale data read
* [#98](https://github.com/babylonlabs-io/vigilante/pull/98) fixes golangci configuration

## v0.15.0

Expand Down
1 change: 0 additions & 1 deletion btcclient/client_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,4 @@ func (c *Client) TxDetails(txHash *chainhash.Hash, pkScript []byte) (*notifier.T
}

return c.getTxDetails(req, txNotFoundErrMsgBitcoind)

}
22 changes: 11 additions & 11 deletions btcclient/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ type Bitcoind struct {

func DefaultBitcoindConfig() Bitcoind {
return Bitcoind{
RPCHost: config.DefaultRpcBtcNodeHost,
RPCUser: config.DefaultBtcNodeRpcUser,
RPCPass: config.DefaultBtcNodeRpcPass,
RPCHost: config.DefaultRPCBtcNodeHost,
RPCUser: config.DefaultBtcNodeRPCUser,
RPCPass: config.DefaultBtcNodeRPCPass,
RPCPolling: true,
BlockPollingInterval: 30 * time.Second,
TxPollingInterval: 30 * time.Second,
EstimateMode: config.DefaultBtcNodeEstimateMode,
BlockCacheSize: config.DefaultBtcblockCacheSize,
BlockCacheSize: config.DefaultBtcBlockCacheSize,
ZMQPubRawBlock: config.DefaultZmqBlockEndpoint,
ZMQPubRawTx: config.DefaultZmqTxEndpoint,
ZMQReadDeadline: 30 * time.Second,
Expand Down Expand Up @@ -71,23 +71,23 @@ type EmptyHintCache struct{}

var _ HintCache = (*EmptyHintCache)(nil)

func (c *EmptyHintCache) CommitSpendHint(height uint32, spendRequests ...chainntnfs.SpendRequest) error {
func (c *EmptyHintCache) CommitSpendHint(_ uint32, _ ...chainntnfs.SpendRequest) error {
return nil
}
func (c *EmptyHintCache) QuerySpendHint(spendRequest chainntnfs.SpendRequest) (uint32, error) {
func (c *EmptyHintCache) QuerySpendHint(_ chainntnfs.SpendRequest) (uint32, error) {
return 0, nil
}
func (c *EmptyHintCache) PurgeSpendHint(spendRequests ...chainntnfs.SpendRequest) error {
func (c *EmptyHintCache) PurgeSpendHint(_ ...chainntnfs.SpendRequest) error {
return nil
}

func (c *EmptyHintCache) CommitConfirmHint(height uint32, confRequests ...chainntnfs.ConfRequest) error {
func (c *EmptyHintCache) CommitConfirmHint(_ uint32, _ ...chainntnfs.ConfRequest) error {
return nil
}
func (c *EmptyHintCache) QueryConfirmHint(confRequest chainntnfs.ConfRequest) (uint32, error) {
func (c *EmptyHintCache) QueryConfirmHint(_ chainntnfs.ConfRequest) (uint32, error) {
return 0, nil
}
func (c *EmptyHintCache) PurgeConfirmHint(confRequests ...chainntnfs.ConfRequest) error {
func (c *EmptyHintCache) PurgeConfirmHint(_ ...chainntnfs.ConfRequest) error {
return nil
}

Expand All @@ -96,7 +96,7 @@ func (c *EmptyHintCache) PurgeConfirmHint(confRequests ...chainntnfs.ConfRequest
// // According to chain.BitcoindConfig docs it should also support tor if node backend
// // works over tor.
func BuildDialer(rpcHost string) func(string) (net.Conn, error) {
return func(addr string) (net.Conn, error) {
return func(_ string) (net.Conn, error) {
return net.Dial("tcp", rpcHost)
}
}
Expand Down
9 changes: 7 additions & 2 deletions btcclient/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *Client) GetBestBlock() (uint32, error) {
}

if height < 0 || height > int64(math.MaxUint32) {
panic(fmt.Errorf("height (%d) is out of uint32 range", height)) //software bug, panic
panic(fmt.Errorf("height (%d) is out of uint32 range", height)) // software bug, panic
}

return uint32(height), nil
Expand All @@ -41,7 +41,7 @@ func (c *Client) GetBlockByHash(blockHash *chainhash.Hash) (*types.IndexedBlock,
btcTxs := types.GetWrappedTxs(mBlock)
height := blockInfo.Height
if height < 0 || height > int64(math.MaxUint32) {
panic(fmt.Errorf("height (%d) is out of uint32 range", height)) //software bug, panic
panic(fmt.Errorf("height (%d) is out of uint32 range", height)) // software bug, panic
}
return types.NewIndexedBlock(uint32(height), &mBlock.Header, btcTxs), mBlock, nil
}
Expand Down Expand Up @@ -82,6 +82,7 @@ func (c *Client) getBestBlockHashWithRetry() (*chainhash.Hash, error) {
); err != nil {
c.logger.Debug(
"failed to query the best block hash", zap.Error(err))
return nil, err
Copy link
Collaborator

Choose a reason for hiding this comment

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

woh those seems like critical fixes 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah some obvious misses on our end 😅

}

return blockHash, nil
Expand All @@ -106,6 +107,7 @@ func (c *Client) getBlockHashWithRetry(height uint32) (*chainhash.Hash, error) {
); err != nil {
c.logger.Debug(
"failed to query the block hash", zap.Uint32("height", height), zap.Error(err))
return nil, err
}

return blockHash, nil
Expand All @@ -130,6 +132,7 @@ func (c *Client) getBlockWithRetry(hash *chainhash.Hash) (*wire.MsgBlock, error)
); err != nil {
c.logger.Debug(
"failed to query the block", zap.String("hash", hash.String()), zap.Error(err))
return nil, err
}

return block, nil
Expand All @@ -154,6 +157,7 @@ func (c *Client) getBlockVerboseWithRetry(hash *chainhash.Hash) (*btcjson.GetBlo
); err != nil {
c.logger.Debug(
"failed to query the block verbose", zap.String("hash", hash.String()), zap.Error(err))
return nil, err
}

return blockVerbose, nil
Expand Down Expand Up @@ -235,6 +239,7 @@ func (c *Client) getBlockCountWithRetry() (int64, error) {
retry.Attempts(c.maxRetryTimes),
); err != nil {
c.logger.Debug("failed to query get block count", zap.Error(err))
return 0, err
}

return height, nil
Expand Down
2 changes: 1 addition & 1 deletion btcstaking-tracker/atomicslasher/babylon_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (ba *BabylonAdapter) HandleAllBTCDelegations(handleFunc func(btcDel *bstype
}

func (ba *BabylonAdapter) IsFPSlashed(
ctx context.Context,
_ context.Context,
fpBTCPK *bbn.BIP340PubKey,
) (bool, error) {
fpResp, err := ba.bbnClient.FinalityProvider(fpBTCPK.MarshalHex())
Expand Down
2 changes: 1 addition & 1 deletion btcstaking-tracker/atomicslasher/routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (as *AtomicSlasher) selectiveSlashingReporter() {
cancel()
if err != nil {
as.logger.Error(
"failed to get BTC staking paramter at version",
"failed to get BTC staking parameter at version",
zap.Uint32("version", paramsVersion),
zap.Error(err),
)
Expand Down
10 changes: 5 additions & 5 deletions btcstaking-tracker/atomicslasher/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (
"github.com/btcsuite/btcd/wire"
)

type slashingPath int
type SlashingPath int

const (
slashStakingTx slashingPath = iota
slashStakingTx SlashingPath = iota
slashUnbondingTx
)

type SlashingTxInfo struct {
path slashingPath
path SlashingPath
StakingTxHash chainhash.Hash
SlashingMsgTx *wire.MsgTx
}

func NewSlashingTxInfo(path slashingPath, stakingTxHash chainhash.Hash, slashingMsgTx *wire.MsgTx) *SlashingTxInfo {
func NewSlashingTxInfo(path SlashingPath, stakingTxHash chainhash.Hash, slashingMsgTx *wire.MsgTx) *SlashingTxInfo {
return &SlashingTxInfo{
path: path,
StakingTxHash: stakingTxHash,
Expand Down Expand Up @@ -139,7 +139,7 @@ func (bdi *BTCDelegationIndex) Remove(stakingTxHash chainhash.Hash) {
delete(bdi.unbondingSlashingTxMap, del.UnbondingSlashingTxHash)
}

func (bdi *BTCDelegationIndex) FindSlashedBTCDelegation(txHash chainhash.Hash) (*TrackedDelegation, slashingPath) {
func (bdi *BTCDelegationIndex) FindSlashedBTCDelegation(txHash chainhash.Hash) (*TrackedDelegation, SlashingPath) {
bdi.Lock()
defer bdi.Unlock()

Expand Down
4 changes: 2 additions & 2 deletions btcstaking-tracker/btcslasher/btc_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (bs *BTCSlasher) isTaprootOutputSpendable(txBytes []byte, outIdx uint32) (b
stakingMsgTx, err := bbn.NewBTCTxFromBytes(txBytes)
if err != nil {
return false, fmt.Errorf(
"failed to convert staking tx to MsgTx: %v",
"failed to convert staking tx to MsgTx: %w",
err,
)
}
Expand All @@ -33,7 +33,7 @@ func (bs *BTCSlasher) isTaprootOutputSpendable(txBytes []byte, outIdx uint32) (b
txOut, err := bs.BTCClient.GetTxOut(&stakingMsgTxHash, outIdx, true)
if err != nil {
return false, fmt.Errorf(
"failed to get the output of tx %s: %v",
"failed to get the output of tx %s: %w",
stakingMsgTxHash.String(),
err,
)
Expand Down
5 changes: 3 additions & 2 deletions btcstaking-tracker/btcslasher/slasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (bs *BTCSlasher) slashingEnforcer() {
)

// record the metrics of the slashed delegation
bs.metrics.RecordSlashedDelegation(slashRes.Del, slashRes.SlashingTxHash.String())
bs.metrics.RecordSlashedDelegation(slashRes.Del)
}
}
}
Expand Down Expand Up @@ -259,7 +259,8 @@ func (bs *BTCSlasher) SlashFinalityProvider(extractedFpBTCSK *btcec.PrivateKey)

// Initialize a semaphore to control the number of concurrent operations
sem := semaphore.NewWeighted(bs.maxSlashingConcurrency)
delegations := append(activeBTCDels, unbondedBTCDels...)
activeBTCDels = append(activeBTCDels, unbondedBTCDels...)
delegations := activeBTCDels

// try to slash both staking and unbonding txs for each BTC delegation
// sign and submit slashing tx for each active and unbonded delegation
Expand Down
1 change: 1 addition & 0 deletions btcstaking-tracker/btcslasher/slasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/babylonlabs-io/vigilante/testutil/mocks"
)

//nolint:maintidx // Ignoring high maintainability index for this fuzz test
func FuzzSlasher(f *testing.F) {
datagen.AddRandomSeedsToFuzzer(f, 10)

Expand Down
Loading
Loading