From 741492cc75306ea704537b28faffe37a71e20a59 Mon Sep 17 00:00:00 2001 From: Mantre Date: Thu, 26 Oct 2023 16:36:24 +0800 Subject: [PATCH] fix(consensus): not increase the vote-box power on duplicated votes (#785) --- consensus/voteset/vote_box.go | 6 ++++-- consensus/voteset/vote_box_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 consensus/voteset/vote_box_test.go diff --git a/consensus/voteset/vote_box.go b/consensus/voteset/vote_box.go index 89359ddf2..f789fad19 100644 --- a/consensus/voteset/vote_box.go +++ b/consensus/voteset/vote_box.go @@ -18,6 +18,8 @@ func newVoteBox() *voteBox { } func (vs *voteBox) addVote(vote *vote.Vote, power int64) { - vs.votes[vote.Signer()] = vote - vs.votedPower += power + if vs.votes[vote.Signer()] == nil { + vs.votes[vote.Signer()] = vote + vs.votedPower += power + } } diff --git a/consensus/voteset/vote_box_test.go b/consensus/voteset/vote_box_test.go new file mode 100644 index 000000000..33d7ad301 --- /dev/null +++ b/consensus/voteset/vote_box_test.go @@ -0,0 +1,28 @@ +package voteset + +import ( + "testing" + + "github.com/pactus-project/pactus/types/vote" + "github.com/pactus-project/pactus/util/testsuite" + "github.com/stretchr/testify/assert" +) + +func TestDuplicateVote(t *testing.T) { + ts := testsuite.NewTestSuite(t) + + hash := ts.RandHash() + height := ts.RandHeight() + round := ts.RandRound() + signer := ts.RandValAddress() + power := ts.RandInt64(1000) + + v := vote.NewPrepareVote(hash, height, round, signer) + + vb := newVoteBox() + + vb.addVote(v, power) + vb.addVote(v, power) + + assert.Equal(t, vb.votedPower, power) +}