Skip to content

Commit

Permalink
chore: some linter issues fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Sep 18, 2024
1 parent c9d1ec1 commit 9e865f2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 15 deletions.
6 changes: 0 additions & 6 deletions internal/consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,6 @@ func incrementRound(vss ...*validatorStub) {
}
}

func resetRound(vss ...*validatorStub) {
for _, vs := range vss {
vs.Round = 0
}
}

func sortVValidatorStubsByPower(ctx context.Context, t *testing.T, vss []*validatorStub) []*validatorStub {
t.Helper()
sort.Slice(vss, func(i, j int) bool {
Expand Down
7 changes: 6 additions & 1 deletion internal/consensus/peer_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
cstypes "github.com/dashpay/tenderdash/internal/consensus/types"
"github.com/dashpay/tenderdash/libs/bits"
"github.com/dashpay/tenderdash/libs/log"
"github.com/dashpay/tenderdash/libs/math"
tmproto "github.com/dashpay/tenderdash/proto/tendermint/types"
"github.com/dashpay/tenderdash/types"
)
Expand Down Expand Up @@ -214,7 +215,11 @@ func (ps *PeerState) PickVoteToSend(votes types.VoteSetReader) (*types.Vote, boo
}

if index, ok := votes.BitArray().Sub(psVotes).PickRandom(); ok {
vote := votes.GetByIndex(int32(index))
idx, err := math.SafeConvertInt32(int64(index))
if err != nil {
panic(fmt.Errorf("failed to convert index to int32: %w", err))
}
vote := votes.GetByIndex(idx)
if vote != nil {
return vote, true
}
Expand Down
1 change: 0 additions & 1 deletion internal/consensus/state_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type StateDataStore struct {
emitter *eventemitter.EventEmitter
replayMode bool
version int64
selectproposer selectproposer.ProposerSelector
}

// NewStateDataStore creates and returns a new state-data store
Expand Down
2 changes: 0 additions & 2 deletions internal/consensus/state_enter_propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ func (c *EnterProposeAction) Execute(ctx context.Context, stateEvent StateEvent)
logger.Info("propose step; our turn to propose",
"node_proTxHash", proTxHash.ShortString(),
"proposer_proTxHash", proTxHash.ShortString(),
"height", stateData.Height,
"round", stateData.Round,
"step", stateData.Step,
)
// Flush the WAL. Otherwise, we may not recompute the same proposal to sign,
Expand Down
3 changes: 2 additions & 1 deletion internal/consensus/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/dashpay/tenderdash/internal/jsontypes"
auto "github.com/dashpay/tenderdash/internal/libs/autofile"
"github.com/dashpay/tenderdash/libs/log"
tmmath "github.com/dashpay/tenderdash/libs/math"
tmos "github.com/dashpay/tenderdash/libs/os"
"github.com/dashpay/tenderdash/libs/service"
tmtime "github.com/dashpay/tenderdash/libs/time"
Expand Down Expand Up @@ -331,7 +332,7 @@ func (enc *WALEncoder) Encode(v *TimedWALMessage) error {
}

crc := crc32.Checksum(data, crc32c)
length := uint32(len(data))
length := tmmath.MustConvertUint32(len(data))
if length > maxMsgSizeBytes {
return fmt.Errorf("msg is too big: %d bytes, max: %d bytes", length, maxMsgSizeBytes)
}
Expand Down
42 changes: 39 additions & 3 deletions libs/math/safemath.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package math

import (
"errors"
"fmt"
"math"
)

var ErrOverflowInt64 = errors.New("int64 overflow")
var ErrOverflowInt32 = errors.New("int32 overflow")
var ErrOverflowUint32 = errors.New("uint32 overflow")
var ErrOverflowUint8 = errors.New("uint8 overflow")
var ErrOverflowInt8 = errors.New("int8 overflow")

Expand Down Expand Up @@ -73,15 +75,49 @@ func SafeSubInt32(a, b int32) (int32, error) {
}

// SafeConvertInt32 takes a int and checks if it overflows.
func SafeConvertInt32(a int64) (int32, error) {
if a > math.MaxInt32 {
func SafeConvertInt32[T Integer](a T) (int32, error) {
if int64(a) > math.MaxInt32 {
return 0, ErrOverflowInt32
} else if a < math.MinInt32 {
} else if int64(a) < math.MinInt32 {
return 0, ErrOverflowInt32
}
return int32(a), nil
}

// SafeConvertInt32 takes a int and checks if it overflows.
func SafeConvertUint32[T Integer](a T) (uint32, error) {
if uint64(a) > math.MaxUint32 {
return 0, ErrOverflowUint32
} else if a < 0 {
return 0, ErrOverflowUint32
}
return uint32(a), nil
}

type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

// MustConvertInt32 takes an Integer and converts it to int32.
// Panics if the conversion overflows.
func MustConvertInt32[T Integer](a T) int32 {
i, err := SafeConvertInt32(a)
if err != nil {
panic(fmt.Errorf("cannot convert %d to int32: %w", a, err))
}
return i
}

// MustConvertInt32 takes an Integer and converts it to int32.
// Panics if the conversion overflows.
func MustConvertUint32[T Integer](a T) uint32 {
i, err := SafeConvertUint32(a)
if err != nil {
panic(fmt.Errorf("cannot convert %d to int32: %w", a, err))
}
return i
}

// SafeConvertUint8 takes an int64 and checks if it overflows.
func SafeConvertUint8(a int64) (uint8, error) {
if a > math.MaxUint8 {
Expand Down
30 changes: 29 additions & 1 deletion libs/math/safemath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestSafeAdd(t *testing.T) {
f := func(a, b int64) bool {
c, overflow := SafeAddInt64(a, b)
return overflow != nil || (overflow == nil && c == a+b)
return overflow != nil || c == a+b
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
Expand All @@ -31,6 +31,34 @@ func TestSafeSubClip(t *testing.T) {
assert.EqualValues(t, math.MaxInt64, SafeSubClipInt64(math.MaxInt64, -10))
}

func TestSafeConvertUint32(t *testing.T) {
testCases := []struct {
a int64
overflow bool
}{
{-1, true},
{0, false},
{1, false},
{math.MaxInt64, true},
{math.MaxInt32, false},
{math.MaxUint32, false},
{math.MaxUint32 + 1, true},
{math.MaxInt32, false},
}

for i, tc := range testCases {
b, err := SafeConvertUint32(tc.a)
if tc.overflow {
assert.Error(t, err, "#%d", i)
assert.Panics(t, func() { MustConvertUint32(tc.a) }, "#%d", i)
} else {
assert.EqualValues(t, tc.a, b, "#%d", i)
assert.NotPanics(t, func() { MustConvertUint32(tc.a) }, "#%d", i)
}

}
}

func TestSafeMul(t *testing.T) {
testCases := []struct {
a int64
Expand Down

0 comments on commit 9e865f2

Please sign in to comment.