From 9e865f2e7bc589dfd9993fa5a8c8cf597fd780c0 Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Wed, 18 Sep 2024 21:10:28 +0200 Subject: [PATCH] chore: some linter issues fixed --- internal/consensus/common_test.go | 6 ---- internal/consensus/peer_state.go | 7 +++- internal/consensus/state_data.go | 1 - internal/consensus/state_enter_propose.go | 2 -- internal/consensus/wal.go | 3 +- libs/math/safemath.go | 42 +++++++++++++++++++++-- libs/math/safemath_test.go | 30 +++++++++++++++- 7 files changed, 76 insertions(+), 15 deletions(-) diff --git a/internal/consensus/common_test.go b/internal/consensus/common_test.go index 36acab5101..a3de1f11cd 100644 --- a/internal/consensus/common_test.go +++ b/internal/consensus/common_test.go @@ -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 { diff --git a/internal/consensus/peer_state.go b/internal/consensus/peer_state.go index 8ab8f90be6..77844c395d 100644 --- a/internal/consensus/peer_state.go +++ b/internal/consensus/peer_state.go @@ -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" ) @@ -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 } diff --git a/internal/consensus/state_data.go b/internal/consensus/state_data.go index 21b5cdc1c4..0686b0f32d 100644 --- a/internal/consensus/state_data.go +++ b/internal/consensus/state_data.go @@ -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 diff --git a/internal/consensus/state_enter_propose.go b/internal/consensus/state_enter_propose.go index 5837f7b066..db23ef2248 100644 --- a/internal/consensus/state_enter_propose.go +++ b/internal/consensus/state_enter_propose.go @@ -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, diff --git a/internal/consensus/wal.go b/internal/consensus/wal.go index 6f5fe608ad..1c3c249801 100644 --- a/internal/consensus/wal.go +++ b/internal/consensus/wal.go @@ -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" @@ -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) } diff --git a/libs/math/safemath.go b/libs/math/safemath.go index c4347d07ee..9afb409b21 100644 --- a/libs/math/safemath.go +++ b/libs/math/safemath.go @@ -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") @@ -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 { diff --git a/libs/math/safemath_test.go b/libs/math/safemath_test.go index 33fc914d00..92a8f32110 100644 --- a/libs/math/safemath_test.go +++ b/libs/math/safemath_test.go @@ -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) @@ -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