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 8 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
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

- P2P Protocol
- [abci] [\#100](https://github.com/line/tendermint/pull/100) Add `voters_hash` field, which is needed for verification of a block header
- [abci] [\#102](https://github.com/line/tendermint/pull/102) Add voting power in `VoterInfo` of abci

- Go API

Expand Down
349 changes: 196 additions & 153 deletions abci/types/types.pb.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion abci/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ message ValidatorUpdate {
// VoteInfo
message VoteInfo {
Validator validator = 1 [(gogoproto.nullable) = false];
bool signed_last_block = 2;
int64 voting_power = 2;
bool signed_last_block = 3;
}

message PubKey {
Expand Down
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
4 changes: 3 additions & 1 deletion state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ func getBeginBlockValidatorInfo(block *types.Block, stateDB dbm.DB, voterParams
for i, val := range lastVoterSet.Voters {
commitSig := block.LastCommit.Signatures[i]
voteInfos[i] = abci.VoteInfo{
Validator: types.TM2PB.Validator(val),
Validator: types.TM2PB.Validator(val),
// TODO We need to change distribution of cosmos-sdk in order to reference this power for reward later
VotingPower: val.VotingPower,
SignedLastBlock: !commitSig.Absent(),
}
}
Expand Down
39 changes: 39 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,40 @@ 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())
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. 😅

}