2015-06-04 13:36:57 -07:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
. "github.com/tendermint/tendermint/common"
|
|
|
|
sm "github.com/tendermint/tendermint/state"
|
|
|
|
"github.com/tendermint/tendermint/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RoundVoteSet struct {
|
|
|
|
Prevotes *VoteSet
|
|
|
|
Precommits *VoteSet
|
|
|
|
}
|
|
|
|
|
2015-06-14 09:01:20 -07:00
|
|
|
/*
|
|
|
|
Keeps track of all VoteSets from round 0 to round 'round'.
|
|
|
|
|
|
|
|
Also keeps track of up to one RoundVoteSet greater than
|
2015-06-24 17:05:52 -07:00
|
|
|
'round' from each peer, to facilitate catchup syncing of commits.
|
|
|
|
|
2015-06-14 09:01:20 -07:00
|
|
|
A commit is +2/3 precommits for a block at a round,
|
|
|
|
but which round is not known in advance, so when a peer
|
|
|
|
provides a precommit for a round greater than mtx.round,
|
|
|
|
we create a new entry in roundVoteSets but also remember the
|
|
|
|
peer to prevent abuse.
|
|
|
|
*/
|
2015-06-04 13:36:57 -07:00
|
|
|
type HeightVoteSet struct {
|
2015-06-25 20:28:34 -07:00
|
|
|
height int
|
2015-06-04 13:36:57 -07:00
|
|
|
valSet *sm.ValidatorSet
|
|
|
|
|
2015-06-24 17:05:52 -07:00
|
|
|
mtx sync.Mutex
|
2015-06-25 20:28:34 -07:00
|
|
|
round int // max tracked round
|
|
|
|
roundVoteSets map[int]RoundVoteSet // keys: [0...round]
|
|
|
|
peerCatchupRounds map[string]int // keys: peer.Key; values: round
|
2015-06-04 13:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func NewHeightVoteSet(height int, valSet *sm.ValidatorSet) *HeightVoteSet {
|
2015-06-04 13:36:57 -07:00
|
|
|
hvs := &HeightVoteSet{
|
2015-06-24 17:05:52 -07:00
|
|
|
height: height,
|
|
|
|
valSet: valSet,
|
2015-06-25 20:28:34 -07:00
|
|
|
roundVoteSets: make(map[int]RoundVoteSet),
|
|
|
|
peerCatchupRounds: make(map[string]int),
|
2015-06-04 13:36:57 -07:00
|
|
|
}
|
2015-06-24 14:04:40 -07:00
|
|
|
hvs.addRound(0)
|
|
|
|
hvs.round = 0
|
2015-06-04 13:36:57 -07:00
|
|
|
return hvs
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (hvs *HeightVoteSet) Height() int {
|
2015-06-04 13:36:57 -07:00
|
|
|
return hvs.height
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (hvs *HeightVoteSet) Round() int {
|
2015-06-04 13:36:57 -07:00
|
|
|
hvs.mtx.Lock()
|
|
|
|
defer hvs.mtx.Unlock()
|
|
|
|
return hvs.round
|
|
|
|
}
|
|
|
|
|
2015-06-05 14:15:40 -07:00
|
|
|
// Create more RoundVoteSets up to round.
|
2015-06-25 20:28:34 -07:00
|
|
|
func (hvs *HeightVoteSet) SetRound(round int) {
|
2015-06-04 13:36:57 -07:00
|
|
|
hvs.mtx.Lock()
|
|
|
|
defer hvs.mtx.Unlock()
|
|
|
|
if hvs.round != 0 && (round < hvs.round+1) {
|
|
|
|
panic("SetRound() must increment hvs.round")
|
|
|
|
}
|
|
|
|
for r := hvs.round + 1; r <= round; r++ {
|
2015-06-22 19:04:31 -07:00
|
|
|
if _, ok := hvs.roundVoteSets[r]; ok {
|
2015-06-24 17:05:52 -07:00
|
|
|
continue // Already exists because peerCatchupRounds.
|
2015-06-04 13:36:57 -07:00
|
|
|
}
|
2015-07-05 21:01:59 -07:00
|
|
|
hvs.addRound(r)
|
2015-06-04 13:36:57 -07:00
|
|
|
}
|
|
|
|
hvs.round = round
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (hvs *HeightVoteSet) addRound(round int) {
|
2015-06-22 19:04:31 -07:00
|
|
|
if _, ok := hvs.roundVoteSets[round]; ok {
|
2015-06-14 09:01:20 -07:00
|
|
|
panic("addRound() for an existing round")
|
|
|
|
}
|
2015-07-19 14:49:13 -07:00
|
|
|
log.Info("addRound(round)", "round", round)
|
2015-06-22 19:04:31 -07:00
|
|
|
prevotes := NewVoteSet(hvs.height, round, types.VoteTypePrevote, hvs.valSet)
|
|
|
|
precommits := NewVoteSet(hvs.height, round, types.VoteTypePrecommit, hvs.valSet)
|
|
|
|
hvs.roundVoteSets[round] = RoundVoteSet{
|
2015-06-14 09:01:20 -07:00
|
|
|
Prevotes: prevotes,
|
|
|
|
Precommits: precommits,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-22 19:04:31 -07:00
|
|
|
// Duplicate votes return added=false, err=nil.
|
2015-06-24 14:04:40 -07:00
|
|
|
// By convention, peerKey is "" if origin is self.
|
2015-06-25 20:28:34 -07:00
|
|
|
func (hvs *HeightVoteSet) AddByAddress(address []byte, vote *types.Vote, peerKey string) (added bool, index int, err error) {
|
2015-06-04 13:36:57 -07:00
|
|
|
hvs.mtx.Lock()
|
|
|
|
defer hvs.mtx.Unlock()
|
|
|
|
voteSet := hvs.getVoteSet(vote.Round, vote.Type)
|
|
|
|
if voteSet == nil {
|
2015-06-24 17:05:52 -07:00
|
|
|
if _, ok := hvs.peerCatchupRounds[peerKey]; !ok {
|
2015-06-14 09:01:20 -07:00
|
|
|
hvs.addRound(vote.Round)
|
2015-06-24 17:05:52 -07:00
|
|
|
hvs.peerCatchupRounds[peerKey] = vote.Round
|
2015-06-14 09:01:20 -07:00
|
|
|
} else {
|
|
|
|
// Peer has sent a vote that does not match our round,
|
|
|
|
// for more than one round. Bad peer!
|
|
|
|
// TODO punish peer.
|
|
|
|
log.Warn("Deal with peer giving votes from unwanted rounds")
|
|
|
|
}
|
2015-06-04 13:36:57 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
added, index, err = voteSet.AddByAddress(address, vote)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (hvs *HeightVoteSet) Prevotes(round int) *VoteSet {
|
2015-06-04 13:36:57 -07:00
|
|
|
hvs.mtx.Lock()
|
|
|
|
defer hvs.mtx.Unlock()
|
2015-06-05 14:15:40 -07:00
|
|
|
return hvs.getVoteSet(round, types.VoteTypePrevote)
|
2015-06-04 13:36:57 -07:00
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (hvs *HeightVoteSet) Precommits(round int) *VoteSet {
|
2015-06-05 14:15:40 -07:00
|
|
|
hvs.mtx.Lock()
|
|
|
|
defer hvs.mtx.Unlock()
|
|
|
|
return hvs.getVoteSet(round, types.VoteTypePrecommit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last round that has +2/3 prevotes for a particular block or nik.
|
|
|
|
// Returns -1 if no such round exists.
|
|
|
|
func (hvs *HeightVoteSet) POLRound() int {
|
|
|
|
hvs.mtx.Lock()
|
|
|
|
defer hvs.mtx.Unlock()
|
2015-06-25 20:28:34 -07:00
|
|
|
for r := hvs.round; r >= 0; r-- {
|
|
|
|
if hvs.getVoteSet(r, types.VoteTypePrevote).HasTwoThirdsMajority() {
|
|
|
|
return r
|
2015-06-05 14:15:40 -07:00
|
|
|
}
|
2015-06-04 13:36:57 -07:00
|
|
|
}
|
2015-06-05 14:15:40 -07:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2015-06-25 20:28:34 -07:00
|
|
|
func (hvs *HeightVoteSet) getVoteSet(round int, type_ byte) *VoteSet {
|
2015-07-19 14:49:13 -07:00
|
|
|
log.Info("getVoteSet(round)", "round", round, "type", type_)
|
2015-06-04 13:36:57 -07:00
|
|
|
rvs, ok := hvs.roundVoteSets[round]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch type_ {
|
|
|
|
case types.VoteTypePrevote:
|
|
|
|
return rvs.Prevotes
|
|
|
|
case types.VoteTypePrecommit:
|
|
|
|
return rvs.Precommits
|
|
|
|
default:
|
|
|
|
panic(Fmt("Unexpected vote type %X", type_))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hvs *HeightVoteSet) String() string {
|
|
|
|
return hvs.StringIndented("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hvs *HeightVoteSet) StringIndented(indent string) string {
|
2015-06-14 09:01:20 -07:00
|
|
|
vsStrings := make([]string, 0, (len(hvs.roundVoteSets)+1)*2)
|
|
|
|
// rounds 0 ~ hvs.round inclusive
|
2015-06-25 20:28:34 -07:00
|
|
|
for round := 0; round <= hvs.round; round++ {
|
2015-06-04 13:36:57 -07:00
|
|
|
voteSetString := hvs.roundVoteSets[round].Prevotes.StringShort()
|
|
|
|
vsStrings = append(vsStrings, voteSetString)
|
|
|
|
voteSetString = hvs.roundVoteSets[round].Precommits.StringShort()
|
|
|
|
vsStrings = append(vsStrings, voteSetString)
|
|
|
|
}
|
2015-06-24 17:05:52 -07:00
|
|
|
// all other peer catchup rounds
|
2015-06-14 09:01:20 -07:00
|
|
|
for round, roundVoteSet := range hvs.roundVoteSets {
|
|
|
|
if round <= hvs.round {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
voteSetString := roundVoteSet.Prevotes.StringShort()
|
|
|
|
vsStrings = append(vsStrings, voteSetString)
|
|
|
|
voteSetString = roundVoteSet.Precommits.StringShort()
|
|
|
|
vsStrings = append(vsStrings, voteSetString)
|
|
|
|
}
|
2015-06-04 13:36:57 -07:00
|
|
|
return Fmt(`HeightVoteSet{H:%v R:0~%v
|
|
|
|
%s %v
|
|
|
|
%s}`,
|
2015-06-14 09:01:20 -07:00
|
|
|
hvs.height, hvs.round,
|
2015-06-04 13:36:57 -07:00
|
|
|
indent, strings.Join(vsStrings, "\n"+indent+" "),
|
|
|
|
indent)
|
|
|
|
}
|