Skip to content

Commit

Permalink
Merge branch 'test/voter_selection' of https://github.com/line/tender…
Browse files Browse the repository at this point in the history
…mint into fix/votingpower_panic
  • Loading branch information
egonspace committed Jul 20, 2020
2 parents 117ed63 + 171bbf9 commit 9f38532
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2009,12 +2009,45 @@ func ensureVotingPowerOfVoteSet(t *testing.T, voteSet *types.VoteSet, votingPowe
}

func TestStateBadVoterWithSelectedVoter(t *testing.T) {
// if validators are 9, then selected voters are 4
// if one of 4 voters does not vote, the consensus state does not progress to next step
// if validators are 9, then selected voters are 4+
// if one of 4+ voters does not vote, the consensus state does not progress to next step
// making him having 1/3 + 1 voting power of total
cs, vss := randStateWithVoterParams(9, &types.VoterParams{
VoterElectionThreshold: 5,
MaxTolerableByzantinePercentage: 20,
ElectionPrecision: 2})
ElectionPrecision: 5})

assert.True(t, cs.Voters.Size() >= 4)

// a validator being not a voter votes instead of a voter
notVoter := getValidatorBeingNotVoter(cs)
if notVoter == nil {
panic("invalid test case: cannot find a validator being not a voter")
}

nonMyIndex := 0
myPub, _ := cs.privValidator.GetPubKey()
for i, v := range cs.Voters.Voters {
if !myPub.Equals(v.PubKey) {
nonMyIndex = i
break
}
}

// make the invalid voter having voting power of 1/3+1 of total
cs.Voters.Voters[nonMyIndex].VotingPower =
(cs.Voters.TotalVotingPower()-cs.Voters.Voters[nonMyIndex].VotingPower)/2 + 1

voters := cs.Voters.Copy()

// make a voter having invalid pub key
voters.Voters[nonMyIndex] = &types.Validator{
PubKey: notVoter.PubKey,
Address: notVoter.Address,
StakingPower: cs.Voters.Voters[nonMyIndex].StakingPower,
VotingPower: cs.Voters.Voters[nonMyIndex].VotingPower,
}

vss[0].Height = 1 // this is needed because of `incrementHeight(vss[1:]...)` of randStateWithVoterParams()
vssMap := makeVssMap(vss)
height, round := cs.Height, cs.Round
Expand All @@ -2037,28 +2070,6 @@ func TestStateBadVoterWithSelectedVoter(t *testing.T) {
}

propBlock := cs.GetRoundState().ProposalBlock
voters := cs.Voters.Copy()
// a validator being not a voter votes instead of a voter
notVoter := getValidatorBeingNotVoter(cs)
if notVoter == nil {
panic("invalid test case: cannot find a validator being not a voter")
}

nonMyIndex := 0
myPub, _ := cs.privValidator.GetPubKey()
for i, v := range voters.Voters {
if !myPub.Equals(v.PubKey) {
nonMyIndex = i
break
}
}

voters.Voters[nonMyIndex] = &types.Validator{
PubKey: notVoter.PubKey,
Address: notVoter.Address,
StakingPower: voters.Voters[nonMyIndex].StakingPower,
VotingPower: voters.Voters[nonMyIndex].VotingPower,
}

voterPrivVals := votersPrivVals(voters, vssMap)
signAddVotes(cs, types.PrevoteType, propBlock.Hash(), propBlock.MakePartSet(types.BlockPartSizeBytes).Header(),
Expand Down Expand Up @@ -2108,8 +2119,7 @@ func TestStateBadVoterWithSelectedVoter(t *testing.T) {
}

propBlock = cs.GetRoundState().ProposalBlock
voters = cs.Voters
voterPrivVals = votersPrivVals(voters, vssMap)
voterPrivVals = votersPrivVals(cs.Voters, vssMap)

signAddVotes(cs, types.PrevoteType, propBlock.Hash(), propBlock.MakePartSet(types.BlockPartSizeBytes).Header(),
voterPrivVals...)
Expand Down

0 comments on commit 9f38532

Please sign in to comment.