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

fix minus voting power panic #108

Merged
merged 7 commits into from
Jul 20, 2020
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
18 changes: 12 additions & 6 deletions libs/rand/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,20 @@ func RandomSamplingWithoutReplacement(
compensationProportions[i].Add(&compensationProportions[i+1], additionalCompensation)
}
winners = candidates[len(candidates)-winnerNum:]
winPoints := make([]*big.Int, len(winners))
totalWinPoint := new(big.Int)
for i, winner := range winners {
winPoints[i] = new(big.Int).SetUint64(winner.Priority())
winPoints[i].Mul(winPoints[i], &compensationProportions[i])
winPoints[i].Add(winPoints[i], correction)
totalWinPoint.Add(totalWinPoint, winPoints[i])
}
recalibration := new(big.Int).Div(correction, new(big.Int).SetUint64(precisionCorrectionForSelection))
for i, winner := range winners {
// winPoint = correction + winner.Priority() * compensationProportions[i]
winPoint := new(big.Int).SetUint64(winner.Priority())
winPoint.Mul(winPoint, &compensationProportions[i])
winPoint.Add(winPoint, correction)

winner.SetWinPoint(winPoint.Div(winPoint, recalibration).Int64())
winPoint := new(big.Int)
winPoint.Mul(recalibration, winPoints[i])
winPoint.Div(winPoint, totalWinPoint)
winner.SetWinPoint(winPoint.Int64())
}
return winners
}
Expand Down
1 change: 0 additions & 1 deletion libs/rand/sampling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func TestRandomSamplingWithoutReplacementOverflow(t *testing.T) {
assert.True(t, element.winPoint <= lastWinPoint)
lastWinPoint = element.winPoint
}
assert.Equal(t, lastWinPoint, int64(precisionForSelection))
}

func accumulateAndResetReward(candidate []Candidate, acc []uint64) uint64 {
Expand Down
45 changes: 45 additions & 0 deletions types/voter_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math"
"testing"

"github.com/tendermint/tendermint/libs/rand"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -393,3 +395,46 @@ func TestVoterSetProtoBuf(t *testing.T) {
}
}
}

func testVotingPower(t *testing.T, valSet *ValidatorSet) {
voterParams := &VoterParams{
VoterElectionThreshold: 100,
MaxTolerableByzantinePercentage: 20,
ElectionPrecision: 2,
}

voterSetNoSampling := SelectVoter(valSet, []byte{0}, voterParams)
for _, v := range voterSetNoSampling.Voters {
assert.True(t, v.StakingPower == v.VotingPower)
}

for i := 90; i > 50; i-- {
voterParams.VoterElectionThreshold = int32(i)
voterSetSampling := SelectVoter(valSet, []byte{0}, voterParams)
allSame := true
for _, v := range voterSetSampling.Voters {
if v.StakingPower != v.VotingPower {
allSame = false
break
}
}
assert.False(t, allSame)
assert.True(t, valSet.TotalStakingPower() > voterSetSampling.TotalVotingPower())
// total voting power can not be less than total staking power - precisionForSelection(1000)
assert.True(t, valSet.TotalStakingPower()-voterSetSampling.TotalVotingPower() <= 1000)
}
}

func TestVotingPower(t *testing.T) {
testVotingPower(t, randValidatorSet(100))
vals := make([]*Validator, 100)
for i := 0; i < len(vals); i++ {
vals[i] = newValidator(rand.Bytes(32), 100)
}
testVotingPower(t, NewValidatorSet(vals))
Copy link
Member

Choose a reason for hiding this comment

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

Could you add test which the total staking power is close the Max Int64?

	vals2 := make([]*Validator, 100)
	total := int64(0)
	for i:= 0; i < len(vals2); i++ {
		vals2[i] = newValidator(rand.Bytes(32), MaxTotalStakingPower / 100)
		total += vals2[i].StakingPower
	}
	testVotingPower(t, NewValidatorSet(vals2))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. Is it right total is useless?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's right total is not used. This value was used when I check the sum of all staking power is close with max int64. And it didn't removed. 😅

vals2 := make([]*Validator, 100)
for i := 0; i < len(vals2); i++ {
vals2[i] = newValidator(rand.Bytes(32), MaxTotalStakingPower/100)
}
testVotingPower(t, NewValidatorSet(vals2))
}