From e0d318552ebf5c2787f1c7bc0cdeb4d5510133a7 Mon Sep 17 00:00:00 2001 From: kukugi Date: Tue, 6 Oct 2020 11:00:09 +0900 Subject: [PATCH] Change condition to sort voters --- .golangci.yml | 2 +- types/validator_set_test.go | 2 +- types/voter_set.go | 13 ++++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index f6b2dd23f..d0364c2c4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -72,4 +72,4 @@ linters-settings: # options for analysis running run: # timeout for analysis, e.g. 30s, 5m, default is 1m - timeout: 5m + timeout: 10m diff --git a/types/validator_set_test.go b/types/validator_set_test.go index 3190d065f..df8a288e1 100644 --- a/types/validator_set_test.go +++ b/types/validator_set_test.go @@ -1437,7 +1437,7 @@ func TestValidatorSet_VerifyCommitLightTrusting(t *testing.T) { require.NoError(t, err) testCases := []struct { - //valSet *ValidatorSet + // valSet *ValidatorSet voterSet *VoterSet err bool }{ diff --git a/types/voter_set.go b/types/voter_set.go index 79989320f..4cd090bc7 100644 --- a/types/voter_set.go +++ b/types/voter_set.go @@ -782,11 +782,14 @@ func sortVoters(candidates []voter) []voter { temp := make([]voter, len(candidates)) copy(temp, candidates) sort.Slice(temp, func(i, j int) bool { - a := new(big.Int).Mul(big.NewInt(temp[i].val.VotingPower), big.NewInt(precisionForSelection)) - a.Div(a, big.NewInt(temp[i].val.StakingPower)) - b := new(big.Int).Mul(big.NewInt(temp[j].val.VotingPower), big.NewInt(precisionForSelection)) - b.Div(b, big.NewInt(temp[j].val.StakingPower)) - return a.Cmp(b) == 1 + a, overflow1 := safeMul(temp[i].val.VotingPower, temp[j].val.StakingPower) + b, overflow2 := safeMul(temp[j].val.VotingPower, temp[i].val.StakingPower) + if !overflow1 && !overflow2 { + return a > b + } + bigA := new(big.Int).Mul(big.NewInt(temp[i].val.VotingPower), big.NewInt(temp[j].val.StakingPower)) + bigB := new(big.Int).Mul(big.NewInt(temp[j].val.VotingPower), big.NewInt(temp[i].val.StakingPower)) + return bigA.Cmp(bigB) == 1 }) return temp }