tendermint/consensus/vote_set.go

281 lines
7.6 KiB
Go
Raw Normal View History

2014-09-14 15:37:32 -07:00
package consensus
import (
"bytes"
2014-10-15 20:15:38 -07:00
"fmt"
"strings"
2014-09-14 15:37:32 -07:00
"sync"
. "github.com/tendermint/tendermint/account"
2014-10-30 03:32:09 -07:00
. "github.com/tendermint/tendermint/binary"
2014-12-17 01:37:13 -08:00
. "github.com/tendermint/tendermint/block"
"github.com/tendermint/tendermint/state"
2014-09-14 15:37:32 -07:00
)
// VoteSet helps collect signatures from validators at each height+round
// for a predefined vote type.
// Note that there three kinds of votes: prevotes, precommits, and commits.
2014-09-14 15:37:32 -07:00
// A commit of prior rounds can be added added in lieu of votes/precommits.
// NOTE: Assumes that the sum total of voting power does not exceed MaxUInt64.
2014-09-14 15:37:32 -07:00
type VoteSet struct {
height uint
round uint
2014-09-14 15:37:32 -07:00
type_ byte
2014-10-30 03:32:09 -07:00
mtx sync.Mutex
valSet *state.ValidatorSet
votes []*Vote // validator index -> vote
votesBitArray BitArray // validator index -> has vote?
2014-10-30 03:32:09 -07:00
votesByBlock map[string]uint64 // string(blockHash)+string(blockParts) -> vote sum.
totalVotes uint64
maj23Hash []byte
maj23Parts PartSetHeader
maj23Exists bool
2014-09-14 15:37:32 -07:00
}
// Constructs a new VoteSet struct used to accumulate votes for each round.
func NewVoteSet(height uint, round uint, type_ byte, valSet *state.ValidatorSet) *VoteSet {
2014-10-31 18:35:38 -07:00
if height == 0 {
panic("Cannot make VoteSet for height == 0, doesn't make sense.")
}
2014-09-14 15:37:32 -07:00
if type_ == VoteTypeCommit && round != 0 {
panic("Expected round 0 for commit vote set")
}
return &VoteSet{
2014-10-30 03:32:09 -07:00
height: height,
round: round,
type_: type_,
valSet: valSet,
votes: make([]*Vote, valSet.Size()),
votesBitArray: NewBitArray(valSet.Size()),
2014-10-30 03:32:09 -07:00
votesByBlock: make(map[string]uint64),
totalVotes: 0,
2014-09-14 15:37:32 -07:00
}
}
func (voteSet *VoteSet) Size() uint {
if voteSet == nil {
2014-11-03 15:50:23 -08:00
return 0
} else {
return voteSet.valSet.Size()
2014-11-03 15:50:23 -08:00
}
}
2014-09-14 15:37:32 -07:00
// True if added, false if not.
// Returns ErrVote[UnexpectedStep|InvalidAccount|InvalidSignature|InvalidBlockHash|ConflictingSignature]
// NOTE: vote should not be mutated after adding.
func (voteSet *VoteSet) Add(address []byte, vote *Vote) (bool, uint, error) {
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
2014-09-14 15:37:32 -07:00
// Make sure the step matches. (or that vote is commit && round < voteSet.round)
if vote.Height != voteSet.height ||
(vote.Type != VoteTypeCommit && vote.Round != voteSet.round) ||
(vote.Type != VoteTypeCommit && vote.Type != voteSet.type_) ||
(vote.Type == VoteTypeCommit && voteSet.type_ != VoteTypeCommit && vote.Round >= voteSet.round) {
return false, 0, ErrVoteUnexpectedStep
2014-09-14 15:37:32 -07:00
}
// Ensure that signer is a validator.
valIndex, val := voteSet.valSet.GetByAddress(address)
2014-09-14 15:37:32 -07:00
if val == nil {
return false, 0, ErrVoteInvalidAccount
2014-09-14 15:37:32 -07:00
}
// Check signature.
if !val.PubKey.VerifyBytes(SignBytes(vote), vote.Signature) {
2014-09-14 15:37:32 -07:00
// Bad signature.
return false, 0, ErrVoteInvalidSignature
2014-09-14 15:37:32 -07:00
}
return voteSet.addVote(valIndex, vote)
2014-09-14 15:37:32 -07:00
}
func (voteSet *VoteSet) addVote(valIndex uint, vote *Vote) (bool, uint, error) {
2014-09-14 15:37:32 -07:00
// If vote already exists, return false.
if existingVote := voteSet.votes[valIndex]; existingVote != nil {
2014-09-14 15:37:32 -07:00
if bytes.Equal(existingVote.BlockHash, vote.BlockHash) {
return false, 0, nil
2014-09-14 15:37:32 -07:00
} else {
return false, 0, ErrVoteConflictingSignature
2014-09-14 15:37:32 -07:00
}
}
// Add vote.
_, val := voteSet.valSet.GetByIndex(valIndex)
2014-10-11 21:27:58 -07:00
if val == nil {
panic(fmt.Sprintf("Missing validator for index %v", valIndex))
2014-09-14 15:37:32 -07:00
}
voteSet.votes[valIndex] = vote
voteSet.votesBitArray.SetIndex(valIndex, true)
2014-10-30 03:32:09 -07:00
blockKey := string(vote.BlockHash) + string(BinaryBytes(vote.BlockParts))
totalBlockHashVotes := voteSet.votesByBlock[blockKey] + val.VotingPower
voteSet.votesByBlock[blockKey] = totalBlockHashVotes
voteSet.totalVotes += val.VotingPower
2014-09-14 15:37:32 -07:00
// If we just nudged it up to two thirds majority, add it.
if totalBlockHashVotes > voteSet.valSet.TotalVotingPower()*2/3 &&
(totalBlockHashVotes-val.VotingPower) <= voteSet.valSet.TotalVotingPower()*2/3 {
voteSet.maj23Hash = vote.BlockHash
voteSet.maj23Parts = vote.BlockParts
voteSet.maj23Exists = true
2014-09-14 15:37:32 -07:00
}
return true, valIndex, nil
2014-09-14 15:37:32 -07:00
}
// Assumes that commits VoteSet is valid.
func (voteSet *VoteSet) AddFromCommits(commits *VoteSet) {
for valIndex, commit := range commits.votes {
2014-12-17 01:37:13 -08:00
if commit == nil {
continue
}
if commit.Round < voteSet.round {
voteSet.addVote(uint(valIndex), commit)
2014-09-14 15:37:32 -07:00
}
}
}
func (voteSet *VoteSet) BitArray() BitArray {
if voteSet == nil {
2014-11-03 15:50:23 -08:00
return BitArray{}
}
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
return voteSet.votesBitArray.Copy()
2014-10-30 03:32:09 -07:00
}
func (voteSet *VoteSet) GetByIndex(valIndex uint) *Vote {
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
return voteSet.votes[valIndex]
2014-09-14 15:37:32 -07:00
}
func (voteSet *VoteSet) GetByAddress(address []byte) *Vote {
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
valIndex, val := voteSet.valSet.GetByAddress(address)
if val == nil {
panic("GetByAddress(address) returned nil")
2014-09-14 15:37:32 -07:00
}
return voteSet.votes[valIndex]
2014-09-14 15:37:32 -07:00
}
func (voteSet *VoteSet) HasTwoThirdsMajority() bool {
if voteSet == nil {
return false
}
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
return voteSet.maj23Exists
}
2014-09-14 15:37:32 -07:00
// Returns either a blockhash (or nil) that received +2/3 majority.
// If there exists no such majority, returns (nil, false).
func (voteSet *VoteSet) TwoThirdsMajority() (hash []byte, parts PartSetHeader, ok bool) {
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
if voteSet.maj23Exists {
return voteSet.maj23Hash, voteSet.maj23Parts, true
2014-10-21 23:30:18 -07:00
} else {
2014-10-30 03:32:09 -07:00
return nil, PartSetHeader{}, false
2014-09-14 15:37:32 -07:00
}
}
func (voteSet *VoteSet) MakePOL() *POL {
if voteSet.type_ != VoteTypePrevote {
panic("Cannot MakePOL() unless VoteSet.Type is VoteTypePrevote")
}
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
if !voteSet.maj23Exists {
2014-09-14 15:37:32 -07:00
return nil
}
pol := &POL{
Height: voteSet.height,
Round: voteSet.round,
BlockHash: voteSet.maj23Hash,
BlockParts: voteSet.maj23Parts,
Votes: make([]POLVoteSignature, voteSet.valSet.Size()),
2014-09-14 15:37:32 -07:00
}
for valIndex, vote := range voteSet.votes {
2014-12-17 01:37:13 -08:00
if vote == nil {
continue
}
if !bytes.Equal(vote.BlockHash, voteSet.maj23Hash) {
2014-10-30 03:32:09 -07:00
continue
}
if !vote.BlockParts.Equals(voteSet.maj23Parts) {
2014-10-30 03:32:09 -07:00
continue
}
pol.Votes[valIndex] = POLVoteSignature{
Round: vote.Round,
Signature: vote.Signature,
2014-09-14 15:37:32 -07:00
}
}
return pol
}
2014-10-15 20:15:38 -07:00
func (voteSet *VoteSet) MakeValidation() *Validation {
if voteSet.type_ != VoteTypeCommit {
2014-10-31 18:35:38 -07:00
panic("Cannot MakeValidation() unless VoteSet.Type is VoteTypeCommit")
}
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
if len(voteSet.maj23Hash) == 0 {
panic("Cannot MakeValidation() unless a blockhash has +2/3")
}
commits := make([]Commit, voteSet.valSet.Size())
voteSet.valSet.Iterate(func(valIndex uint, val *state.Validator) bool {
vote := voteSet.votes[valIndex]
2014-10-30 03:32:09 -07:00
if vote == nil {
return false
}
if !bytes.Equal(vote.BlockHash, voteSet.maj23Hash) {
2014-10-30 03:32:09 -07:00
return false
}
if !vote.BlockParts.Equals(voteSet.maj23Parts) {
2014-10-30 03:32:09 -07:00
return false
}
commits[valIndex] = Commit{val.Address, vote.Round, vote.Signature}
2014-10-30 03:32:09 -07:00
return false
})
return &Validation{
Commits: commits,
}
}
func (voteSet *VoteSet) String() string {
2014-12-23 01:35:54 -08:00
return voteSet.StringIndented("")
2014-10-15 20:15:38 -07:00
}
2014-12-23 01:35:54 -08:00
func (voteSet *VoteSet) StringIndented(indent string) string {
voteStrings := make([]string, len(voteSet.votes))
2014-12-17 01:37:13 -08:00
for i, vote := range voteSet.votes {
if vote == nil {
2014-12-23 01:35:54 -08:00
voteStrings[i] = "nil-Vote"
2014-12-17 01:37:13 -08:00
} else {
voteStrings[i] = vote.String()
}
2014-10-15 20:15:38 -07:00
}
return fmt.Sprintf(`VoteSet{
%s H:%v R:%v T:%v
%s %v
%s %v
%s}`,
indent, voteSet.height, voteSet.round, voteSet.type_,
2014-10-15 20:15:38 -07:00
indent, strings.Join(voteStrings, "\n"+indent+" "),
indent, voteSet.votesBitArray,
2014-10-15 20:15:38 -07:00
indent)
}
2014-12-23 01:35:54 -08:00
func (voteSet *VoteSet) StringShort() string {
if voteSet == nil {
return "nil-VoteSet"
}
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
return fmt.Sprintf(`VoteSet{H:%v R:%v T:%v %v}`,
voteSet.height, voteSet.round, voteSet.type_, voteSet.votesBitArray)
}