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(consensus): not increase the voting power on duplicated binary votes #762

Merged
merged 1 commit into from
Oct 20, 2023
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
6 changes: 3 additions & 3 deletions consensus/voteset/binary_voteset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ func newRoundVotes() *roundVotes {
}
}

func (bs *roundVotes) addVote(v *vote.Vote, power int64) {
vb := bs.voteBoxes[v.CPValue()]
func (rv *roundVotes) addVote(v *vote.Vote, power int64) {
vb := rv.voteBoxes[v.CPValue()]
vb.addVote(v, power)
bs.votedPower += power
}

type BinaryVoteSet struct {
Expand Down Expand Up @@ -97,6 +96,7 @@ func (vs *BinaryVoteSet) AddVote(v *vote.Vote) (bool, error) {
}
} else {
roundVotes.allVotes[v.Signer()] = v
roundVotes.votedPower += power
}

roundVotes.addVote(v, power)
Expand Down
59 changes: 50 additions & 9 deletions consensus/voteset/voteset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,39 +118,80 @@ func TestAddBinaryVote(t *testing.T) {
assert.True(t, added)
}

func TestDuplicateVote(t *testing.T) {
func TestDuplicateBlockVote(t *testing.T) {
ts := testsuite.NewTestSuite(t)

valsMap, valKeys, totalPower := setupCommittee(ts, 1000, 1500, 2500, 2000)
valsMap, valKeys, totalPower := setupCommittee(ts, 1, 1, 1, 1)

h1 := ts.RandHash()
h2 := ts.RandHash()
h3 := ts.RandHash()
addr := valKeys[0].Address()
vs := NewPrepareVoteSet(0, totalPower, valsMap)

correctVote := vote.NewPrepareVote(h1, 1, 0, addr)
duplicatedVote := vote.NewPrepareVote(h2, 1, 0, addr)
duplicatedVote1 := vote.NewPrepareVote(h2, 1, 0, addr)
duplicatedVote2 := vote.NewPrepareVote(h3, 1, 0, addr)

// sign the votes
ts.HelperSignVote(valKeys[0], correctVote)
ts.HelperSignVote(valKeys[0], duplicatedVote)
ts.HelperSignVote(valKeys[0], duplicatedVote1)
ts.HelperSignVote(valKeys[0], duplicatedVote2)

added, err := vs.AddVote(correctVote)
assert.NoError(t, err)
assert.True(t, added)

added, err = vs.AddVote(duplicatedVote)
added, err = vs.AddVote(duplicatedVote1)
assert.Equal(t, errors.Code(err), errors.ErrDuplicateVote)
assert.True(t, added)

added, err = vs.AddVote(correctVote)
assert.NoError(t, err) // added before
assert.False(t, added)
added, err = vs.AddVote(duplicatedVote2)
assert.Equal(t, errors.Code(err), errors.ErrDuplicateVote)
assert.True(t, added)

bv1 := vs.BlockVotes(h1)
bv2 := vs.BlockVotes(h2)
bv3 := vs.BlockVotes(h3)
assert.Equal(t, bv1[addr], correctVote)
assert.Equal(t, bv2[addr], duplicatedVote)
assert.Equal(t, bv2[addr], duplicatedVote1)
assert.Equal(t, bv3[addr], duplicatedVote2)
assert.False(t, vs.HasQuorumHash())
}

func TestDuplicateBinaryVote(t *testing.T) {
ts := testsuite.NewTestSuite(t)

valsMap, valKeys, totalPower := setupCommittee(ts, 1, 1, 1, 1)

h1 := ts.RandHash()
h2 := ts.RandHash()
h3 := ts.RandHash()
addr := valKeys[0].Address()
vs := NewCPPreVoteVoteSet(0, totalPower, valsMap)

correctVote := vote.NewCPPreVote(h1, 1, 0, 0, vote.CPValueOne, &vote.JustInitOne{}, addr)
duplicatedVote1 := vote.NewCPPreVote(h2, 1, 0, 0, vote.CPValueOne, &vote.JustInitOne{}, addr)
duplicatedVote2 := vote.NewCPPreVote(h3, 1, 0, 0, vote.CPValueOne, &vote.JustInitOne{}, addr)

// sign the votes
ts.HelperSignVote(valKeys[0], correctVote)
ts.HelperSignVote(valKeys[0], duplicatedVote1)
ts.HelperSignVote(valKeys[0], duplicatedVote2)

added, err := vs.AddVote(correctVote)
assert.NoError(t, err)
assert.True(t, added)

added, err = vs.AddVote(duplicatedVote1)
assert.Equal(t, errors.Code(err), errors.ErrDuplicateVote)
assert.True(t, added)

added, err = vs.AddVote(duplicatedVote2)
assert.Equal(t, errors.Code(err), errors.ErrDuplicateVote)
assert.True(t, added)

assert.False(t, vs.HasOneThirdOfTotalPower(0))
}

func TestQuorum(t *testing.T) {
Expand Down
Loading