Skip to content

Commit

Permalink
fix(consensus): verify vote if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
themantre committed Nov 28, 2024
1 parent ca0c7b5 commit 3d8b355
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions consensus/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func TestAddInvalidVoteType(t *testing.T) {
log := NewLog()
log.MoveToNewHeight(cmt.Validators())

data, _ := hex.DecodeString("A701050218320301045820BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" +
data, _ := hex.DecodeString("A7010F0218320301045820BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" +
"055501AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA06f607f6")
invVote := new(vote.Vote)
err := invVote.UnmarshalCBOR(data)
assert.NoError(t, err)

added, err := log.AddVote(invVote)
assert.Error(t, err)
assert.ErrorContains(t, err, "unexpected vote type: 15")
assert.False(t, added)
assert.False(t, log.HasVote(invVote.Hash()))
}
Expand Down
22 changes: 13 additions & 9 deletions consensus/voteset/binary_voteset.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,33 @@ func (vs *BinaryVoteSet) AllVotes() []*vote.Vote {

// AddVote attempts to add a vote to the VoteSet. Returns an error if the vote is invalid.
func (vs *BinaryVoteSet) AddVote(vote *vote.Vote) (bool, error) {
power, err := vs.voteSet.verifyVote(vote)
if err != nil {
return false, err
}
var dupErr error

roundVotes := vs.mustGetRoundVotes(vote.CPRound())
existingVote, ok := roundVotes.allVotes[vote.Signer()]
if ok {
existingVote, exists := roundVotes.allVotes[vote.Signer()]
if exists {
if existingVote.Hash() == vote.Hash() {
// The vote is already added
return false, nil
}

// It is a duplicated vote
err = ErrDuplicatedVote
} else {
dupErr = ErrDuplicatedVote
}

power, err := vs.voteSet.verifyVote(vote)
if err != nil {
return false, err
}

if !exists {
roundVotes.allVotes[vote.Signer()] = vote
roundVotes.votedPower += power
}

roundVotes.addVote(vote, power)

return true, err
return true, dupErr
}

func (vs *BinaryVoteSet) HasOneThirdOfTotalPower(cpRound int16) bool {
Expand Down
22 changes: 13 additions & 9 deletions consensus/voteset/block_voteset.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,25 @@ func (vs *BlockVoteSet) AllVotes() []*vote.Vote {

// AddVote attempts to add a vote to the VoteSet. Returns an error if the vote is invalid.
func (vs *BlockVoteSet) AddVote(vote *vote.Vote) (bool, error) {
power, err := vs.voteSet.verifyVote(vote)
if err != nil {
return false, err
}
var dupErr error

existingVote, ok := vs.allVotes[vote.Signer()]
if ok {
existingVote, exists := vs.allVotes[vote.Signer()]
if exists {
if existingVote.Hash() == vote.Hash() {
// The vote is already added
return false, nil
}

// It is a duplicated vote
err = ErrDuplicatedVote
} else {
dupErr = ErrDuplicatedVote
}

power, err := vs.voteSet.verifyVote(vote)
if err != nil {
return false, err
}

if !exists {
vs.allVotes[vote.Signer()] = vote
}

Expand All @@ -95,7 +99,7 @@ func (vs *BlockVoteSet) AddVote(vote *vote.Vote) (bool, error) {
vs.quorumHash = &h
}

return true, err
return true, dupErr
}

// HasQuorumHash checks if there is a block that has received quorum votes (2/3+ of total power).
Expand Down
6 changes: 5 additions & 1 deletion types/vote/vote_type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package vote

import (
"fmt"
)

type Type int

const (
Expand Down Expand Up @@ -33,6 +37,6 @@ func (t Type) String() string {
case VoteTypeCPDecided:
return "DECIDED"
default:
return ("invalid vote type")
return fmt.Sprintf("%d", t)
}
}

0 comments on commit 3d8b355

Please sign in to comment.