Skip to content

Commit

Permalink
POS-2821: fix more lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marcello33 committed Feb 7, 2025
1 parent 324675e commit 46922fc
Show file tree
Hide file tree
Showing 46 changed files with 197 additions and 133 deletions.
6 changes: 3 additions & 3 deletions auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NewAnteHandler(
return newCtx, err.Result(), true
}

if res := ValidateMemo(stdTx, params); !res.IsOK() {
if res = ValidateMemo(stdTx, params); !res.IsOK() {
return newCtx, res, true
}

Expand Down Expand Up @@ -218,7 +218,7 @@ func ValidateMemo(stdTx authTypes.StdTx, params authTypes.Params) sdk.Result {
}

// verify the signature and increment the sequence. If the account doesn't have
// a pubkey, set it.
// a pubKey, set it.
func processSig(
ctx sdk.Context,
acc authTypes.Account,
Expand All @@ -228,7 +228,7 @@ func processSig(
params authTypes.Params,
sigGasConsumer SignatureVerificationGasConsumer,
) (updatedAcc authTypes.Account, res sdk.Result) {
if res := sigGasConsumer(ctx.GasMeter(), sig, params); !res.IsOK() {
if res = sigGasConsumer(ctx.GasMeter(), sig, params); !res.IsOK() {
return nil, res
}

Expand Down
4 changes: 2 additions & 2 deletions bor/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func GetPreparedProposeSpan(cdc *codec.Codec) *cobra.Command {
}

var spanDuration uint64
if err := jsoniter.Unmarshal(res, &spanDuration); err != nil {
if err = jsoniter.Unmarshal(res, &spanDuration); err != nil {
return err
}

Expand All @@ -346,7 +346,7 @@ func GetPreparedProposeSpan(cdc *codec.Codec) *cobra.Command {
}

var seed common.Hash
if err := jsoniter.Unmarshal(res, &seed); err != nil {
if err = jsoniter.Unmarshal(res, &seed); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion bor/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func PostSendProposeSpanTx(cdc *codec.Codec) *cobra.Command {
}

var spanDuration uint64
if err := jsoniter.ConfigFastest.Unmarshal(res, &spanDuration); err != nil {
if err = jsoniter.ConfigFastest.Unmarshal(res, &spanDuration); err != nil {
return err
}

Expand Down
16 changes: 10 additions & 6 deletions bor/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,11 @@ func (k *Keeper) GetNextSpanSeed(ctx sdk.Context, id uint64) (common.Hash, error
k.Logger(ctx).Debug("newEthBlock to generate seed", "newEthBlock", newEthBlock)

// fetch block header from mainchain
var err error
blockHeader, err = k.contractCaller.GetMainChainBlock(newEthBlock)
if err != nil {
k.Logger(ctx).Error("Error fetching block header from mainchain while calculating next span seed", "error", err)
return common.Hash{}, err
var e error
blockHeader, e = k.contractCaller.GetMainChainBlock(newEthBlock)
if e != nil {
k.Logger(ctx).Error("Error fetching block header from mainchain while calculating next span seed", "error", e)
return common.Hash{}, e
}
} else {
var seedSpanID uint64
Expand Down Expand Up @@ -519,7 +519,7 @@ func (k *Keeper) getBorBlockForSpanSeed(ctx sdk.Context, seedSpan *hmTypes.Span,
break
}

author, err := k.GetSeedProducer(ctx, spanID)
author, err = k.GetSeedProducer(ctx, spanID)
if err != nil {
logger.Error("Error fetching span seed producer", "error", err, "span id", spanID)
return 0, nil, err
Expand Down Expand Up @@ -566,6 +566,10 @@ func (k *Keeper) getBorBlockForSpanSeed(ctx sdk.Context, seedSpan *hmTypes.Span,
borBlock = seedSpan.EndBlock
}

if borBlock > math.MaxInt64 {
return 0, nil, fmt.Errorf("bor block value out of range for int64: %d", borBlock)
}

author, err = k.contractCaller.GetBorChainBlockAuthor(big.NewInt(int64(borBlock)))
if err != nil {
logger.Error("Error fetching end block author from bor chain while calculating next span seed", "error", err, "block", borBlock)
Expand Down
10 changes: 5 additions & 5 deletions bor/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ func binarySearch(array []uint64, search uint64) int {
}

// randomRangeInclusive produces unbiased pseudo random in the range [min, max]. Uses rand.Uint64() and can be seeded beforehand.
func randomRangeInclusive(min uint64, max uint64) uint64 {
if max <= min {
return max
func randomRangeInclusive(minV uint64, maxV uint64) uint64 {
if maxV <= minV {
return maxV
}

rangeLength := max - min + 1
rangeLength := maxV - minV + 1
maxAllowedValue := math.MaxUint64 - math.MaxUint64%rangeLength - 1
randomValue := rand.Uint64() //nolint

Expand All @@ -142,7 +142,7 @@ func randomRangeInclusive(min uint64, max uint64) uint64 {
randomValue = rand.Uint64() //nolint
}

return min + randomValue%rangeLength
return minV + randomValue%rangeLength
}

// createWeightedRanges converts array [1, 2, 3] into cumulative form [1, 3, 6]
Expand Down
2 changes: 2 additions & 0 deletions bor/shuffle.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func innerShuffleList(input []uint64, seed [32]byte, shuffle bool) ([]uint64, er
if pivot>>8 > math.MaxUint32 {
return nil, fmt.Errorf("pivot value out of range for uint32: %d", pivot>>8)
}
//nolint:gosec
binary.LittleEndian.PutUint32(buf[pivotViewSize:], uint32(pivot>>8))
source := sha256Hash(buf)

Expand All @@ -92,6 +93,7 @@ func innerShuffleList(input []uint64, seed [32]byte, shuffle bool) ([]uint64, er
if end>>8 > math.MaxUint32 {
return nil, fmt.Errorf("end value out of range for uint32: %d", end>>8)
}
//nolint:gosec
binary.LittleEndian.PutUint32(buf[pivotViewSize:], uint32(end>>8))
source = sha256Hash(buf)

Expand Down
4 changes: 2 additions & 2 deletions bor/shuffle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func GenRandomVal(count int, startBlock uint64, power int64, timeAlive uint64, r
return
}

func generateRandNumber(max int64) uint64 {
nBig, err := rand.Int(rand.Reader, big.NewInt(max))
func generateRandNumber(maxV int64) uint64 {
nBig, err := rand.Int(rand.Reader, big.NewInt(maxV))
if err != nil {
return 1
}
Expand Down
8 changes: 4 additions & 4 deletions bridge/setu/listener/heimdall.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ func (hl *HeimdallListener) fetchFromAndToBlock() (uint64, uint64, error) {
// fromBlock - get last block from storage
hasLastBlock, _ := hl.storageClient.Has([]byte(heimdallLastBlockKey), nil)
if hasLastBlock {
lastBlockBytes, err := hl.storageClient.Get([]byte(heimdallLastBlockKey), nil)
if err != nil {
hl.Logger.Info("Error while fetching last block bytes from storage", "error", err)
return fromBlock, toBlock, err
lastBlockBytes, e := hl.storageClient.Get([]byte(heimdallLastBlockKey), nil)
if e != nil {
hl.Logger.Info("Error while fetching last block bytes from storage", "error", e)
return fromBlock, toBlock, e
}

if result, err := strconv.ParseUint(string(lastBlockBytes), 10, 64); err == nil {

Check failure on line 177 in bridge/setu/listener/heimdall.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "err" shadows declaration at line 157 (govet)
Expand Down
6 changes: 3 additions & 3 deletions bridge/setu/listener/rootchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (rl *RootChainListener) ProcessHeader(newHeader *blockHeader) {
// get last block from storage
hasLastBlock, _ := rl.storageClient.Has([]byte(lastRootBlockKey), nil)
if hasLastBlock {
lastBlockBytes, err := rl.storageClient.Get([]byte(lastRootBlockKey), nil)
if err != nil {
rl.Logger.Info("Error while fetching last block bytes from storage", "error", err)
lastBlockBytes, e := rl.storageClient.Get([]byte(lastRootBlockKey), nil)
if e != nil {
rl.Logger.Info("Error while fetching last block bytes from storage", "error", e)
return
}

Expand Down
6 changes: 6 additions & 0 deletions bridge/setu/listener/rootchain_selfheal_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ func (rl *RootChainListener) getLatestNonce(ctx context.Context, validatorId uin

// getStakeUpdate returns StakeUpdate event based on the given validator ID and nonce
func (rl *RootChainListener) getStakeUpdate(ctx context.Context, validatorId, nonce uint64) (*types.Log, error) {
if validatorId > math.MaxInt {
return nil, fmt.Errorf("validator ID value out of range for int: %d", validatorId)
}
if nonce > math.MaxInt {
return nil, fmt.Errorf("nonce value out of range for int: %d", nonce)
}
query := map[string]string{
"query": `
{
Expand Down
12 changes: 8 additions & 4 deletions bridge/setu/processor/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (cp *CheckpointProcessor) startPollingForNoAck(ctx context.Context, interva
// 3. if so, propose checkpoint to heimdall.
func (cp *CheckpointProcessor) sendCheckpointToHeimdall(headerBlockStr string) (err error) {
var header = types.Header{}
if err := header.UnmarshalJSON([]byte(headerBlockStr)); err != nil {
if err = header.UnmarshalJSON([]byte(headerBlockStr)); err != nil {
cp.Logger.Error("Error while unmarshalling the header block", "error", err)
return err
}
Expand Down Expand Up @@ -150,9 +150,8 @@ func (cp *CheckpointProcessor) sendCheckpointToHeimdall(headerBlockStr string) (
start := expectedCheckpointState.newStart
end := expectedCheckpointState.newEnd

//
// Check checkpoint buffer
//
//nolint:gosec
timeStamp := uint64(time.Now().Unix())
checkpointBufferTime := uint64(checkpointContext.CheckpointParams.CheckpointBufferTime.Seconds())

Expand Down Expand Up @@ -420,6 +419,9 @@ func (cp *CheckpointProcessor) nextExpectedCheckpoint(checkpointContext *Checkpo
currentTime := time.Now().UTC().Unix()
defaultForcePushInterval := checkpointParams.MaxCheckpointLength * 2 // in seconds (1024 * 2 seconds)

if lastCheckpointTime < 0 || lastCheckpointTime > math.MaxInt64 {
return nil, fmt.Errorf("last checkpoint time is invalid")
}
if currentTime-int64(lastCheckpointTime) > int64(defaultForcePushInterval) {

Check failure on line 425 in bridge/setu/processor/checkpoint.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int64 (gosec)
end = latestChildBlock
cp.Logger.Info("Force push checkpoint",
Expand Down Expand Up @@ -611,7 +613,9 @@ func (cp *CheckpointProcessor) getLatestCheckpointTime(checkpointContext *Checkp
cp.Logger.Error("Error while fetching header block object", "error", err)
return 0, err
}

if createdAt < 0 || createdAt > math.MaxInt64 {
return 0, fmt.Errorf("createdAt is invalid")
}
return int64(createdAt), nil
}

Expand Down
4 changes: 2 additions & 2 deletions bridge/setu/processor/clerk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ func getTestServer() (*machinery.Server, error) {
})
}

func generateRandNumber(max int64) uint64 {
nBig, err := rand.Int(rand.Reader, big.NewInt(max))
func generateRandNumber(maxV int64) uint64 {
nBig, err := rand.Int(rand.Reader, big.NewInt(maxV))
if err != nil {
return 1
}
Expand Down
14 changes: 14 additions & 0 deletions bridge/setu/processor/staking.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package processor

import (
"errors"
"fmt"
"math"
"time"

"github.com/RichardKnop/machinery/v1/tasks"
Expand Down Expand Up @@ -180,6 +182,10 @@ func (sp *StakingProcessor) sendUnstakeInitToHeimdall(eventName string, logBytes
return err
}

if nonceDelay < 0 || nonceDelay > math.MaxInt64 {
return errors.New("nonceDelay is invalid")
}

if !validNonce {
sp.Logger.Info("Ignoring task to send unstake-init to heimdall as nonce is out of order")
return tasks.NewErrRetryTaskLater("Nonce out of order", defaultDelayDuration*time.Duration(nonceDelay))

Check failure on line 191 in bridge/setu/processor/staking.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int64 (gosec)
Expand Down Expand Up @@ -256,6 +262,10 @@ func (sp *StakingProcessor) sendStakeUpdateToHeimdall(eventName string, logBytes
return err
}

if nonceDelay < 0 || nonceDelay > math.MaxInt64 {
return errors.New("nonceDelay is invalid")
}

if !validNonce {
sp.Logger.Info("Ignoring task to send stake-update to heimdall as nonce is out of order")
return tasks.NewErrRetryTaskLater("Nonce out of order", defaultDelayDuration*time.Duration(nonceDelay))

Check failure on line 271 in bridge/setu/processor/staking.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int64 (gosec)
Expand Down Expand Up @@ -336,6 +346,10 @@ func (sp *StakingProcessor) sendSignerChangeToHeimdall(eventName string, logByte
return err
}

if nonceDelay < 0 || nonceDelay > math.MaxInt64 {
return errors.New("nonceDelay is invalid")
}

if !validNonce {
sp.Logger.Info("Ignoring task to send signer-change to heimdall as nonce is out of order")
return tasks.NewErrRetryTaskLater("Nonce out of order", defaultDelayDuration*time.Duration(nonceDelay))

Check failure on line 355 in bridge/setu/processor/staking.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int64 (gosec)
Expand Down
4 changes: 2 additions & 2 deletions checkpoint/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func GetCheckpointLatest(cdc *codec.Codec) *cobra.Command {
}

var ackCount uint64
if err := jsoniter.Unmarshal(ackcountBytes, &ackCount); err != nil {
if err = jsoniter.Unmarshal(ackcountBytes, &ackCount); err != nil {
return err
}

Expand Down Expand Up @@ -423,7 +423,7 @@ func GetOverview(cdc *codec.Codec) *cobra.Command {

validatorSetBytes, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", stakingTypes.QuerierRoute, stakingTypes.QueryCurrentValidatorSet), nil)
if err == nil {
if err := jsoniter.Unmarshal(validatorSetBytes, &validatorSet); err != nil {
if err = jsoniter.Unmarshal(validatorSetBytes, &validatorSet); err != nil {
// log and ignore
cliLogger.Error("Error while unmarshing validator set", "error", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion checkpoint/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func SendCheckpointTx(cdc *codec.Codec) *cobra.Command {
return err
}

if err := jsoniter.ConfigFastest.Unmarshal(proposerBytes, &checkpointProposer); err != nil {
if err = jsoniter.ConfigFastest.Unmarshal(proposerBytes, &checkpointProposer); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 46922fc

Please sign in to comment.