Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(consensus): verify vote if not exists
Browse files Browse the repository at this point in the history
themantre committed Nov 28, 2024
1 parent 1d95e2e commit 039890a
Showing 2 changed files with 26 additions and 18 deletions.
22 changes: 13 additions & 9 deletions consensus/voteset/binary_voteset.go
Original file line number Diff line number Diff line change
@@ -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 {
22 changes: 13 additions & 9 deletions consensus/voteset/block_voteset.go
Original file line number Diff line number Diff line change
@@ -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
}

@@ -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).

0 comments on commit 039890a

Please sign in to comment.