You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.9 KiB

9 years ago
9 years ago
  1. package consensus
  2. import (
  3. "strings"
  4. "sync"
  5. . "github.com/tendermint/tendermint/common"
  6. sm "github.com/tendermint/tendermint/state"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. type RoundVoteSet struct {
  10. Prevotes *VoteSet
  11. Precommits *VoteSet
  12. }
  13. /*
  14. Keeps track of all VoteSets from round 0 to round 'round'.
  15. Also keeps track of up to one RoundVoteSet greater than
  16. 'round' from each peer, to facilitate catchup syncing of commits.
  17. A commit is +2/3 precommits for a block at a round,
  18. but which round is not known in advance, so when a peer
  19. provides a precommit for a round greater than mtx.round,
  20. we create a new entry in roundVoteSets but also remember the
  21. peer to prevent abuse.
  22. */
  23. type HeightVoteSet struct {
  24. height int
  25. valSet *sm.ValidatorSet
  26. mtx sync.Mutex
  27. round int // max tracked round
  28. roundVoteSets map[int]RoundVoteSet // keys: [0...round]
  29. peerCatchupRounds map[string]int // keys: peer.Key; values: round
  30. }
  31. func NewHeightVoteSet(height int, valSet *sm.ValidatorSet) *HeightVoteSet {
  32. hvs := &HeightVoteSet{
  33. height: height,
  34. valSet: valSet,
  35. roundVoteSets: make(map[int]RoundVoteSet),
  36. peerCatchupRounds: make(map[string]int),
  37. }
  38. hvs.addRound(0)
  39. hvs.round = 0
  40. return hvs
  41. }
  42. func (hvs *HeightVoteSet) Height() int {
  43. return hvs.height
  44. }
  45. func (hvs *HeightVoteSet) Round() int {
  46. hvs.mtx.Lock()
  47. defer hvs.mtx.Unlock()
  48. return hvs.round
  49. }
  50. // Create more RoundVoteSets up to round.
  51. func (hvs *HeightVoteSet) SetRound(round int) {
  52. hvs.mtx.Lock()
  53. defer hvs.mtx.Unlock()
  54. if hvs.round != 0 && (round < hvs.round+1) {
  55. panic("SetRound() must increment hvs.round")
  56. }
  57. for r := hvs.round + 1; r <= round; r++ {
  58. if _, ok := hvs.roundVoteSets[r]; ok {
  59. continue // Already exists because peerCatchupRounds.
  60. }
  61. hvs.addRound(round)
  62. }
  63. hvs.round = round
  64. }
  65. func (hvs *HeightVoteSet) addRound(round int) {
  66. if _, ok := hvs.roundVoteSets[round]; ok {
  67. panic("addRound() for an existing round")
  68. }
  69. prevotes := NewVoteSet(hvs.height, round, types.VoteTypePrevote, hvs.valSet)
  70. precommits := NewVoteSet(hvs.height, round, types.VoteTypePrecommit, hvs.valSet)
  71. hvs.roundVoteSets[round] = RoundVoteSet{
  72. Prevotes: prevotes,
  73. Precommits: precommits,
  74. }
  75. }
  76. // Duplicate votes return added=false, err=nil.
  77. // By convention, peerKey is "" if origin is self.
  78. func (hvs *HeightVoteSet) AddByAddress(address []byte, vote *types.Vote, peerKey string) (added bool, index int, err error) {
  79. hvs.mtx.Lock()
  80. defer hvs.mtx.Unlock()
  81. voteSet := hvs.getVoteSet(vote.Round, vote.Type)
  82. if voteSet == nil {
  83. if _, ok := hvs.peerCatchupRounds[peerKey]; !ok {
  84. hvs.addRound(vote.Round)
  85. hvs.peerCatchupRounds[peerKey] = vote.Round
  86. } else {
  87. // Peer has sent a vote that does not match our round,
  88. // for more than one round. Bad peer!
  89. // TODO punish peer.
  90. log.Warn("Deal with peer giving votes from unwanted rounds")
  91. }
  92. return
  93. }
  94. added, index, err = voteSet.AddByAddress(address, vote)
  95. return
  96. }
  97. func (hvs *HeightVoteSet) Prevotes(round int) *VoteSet {
  98. hvs.mtx.Lock()
  99. defer hvs.mtx.Unlock()
  100. return hvs.getVoteSet(round, types.VoteTypePrevote)
  101. }
  102. func (hvs *HeightVoteSet) Precommits(round int) *VoteSet {
  103. hvs.mtx.Lock()
  104. defer hvs.mtx.Unlock()
  105. return hvs.getVoteSet(round, types.VoteTypePrecommit)
  106. }
  107. // Last round that has +2/3 prevotes for a particular block or nik.
  108. // Returns -1 if no such round exists.
  109. func (hvs *HeightVoteSet) POLRound() int {
  110. hvs.mtx.Lock()
  111. defer hvs.mtx.Unlock()
  112. for r := hvs.round; r >= 0; r-- {
  113. if hvs.getVoteSet(r, types.VoteTypePrevote).HasTwoThirdsMajority() {
  114. return r
  115. }
  116. }
  117. return -1
  118. }
  119. func (hvs *HeightVoteSet) getVoteSet(round int, type_ byte) *VoteSet {
  120. rvs, ok := hvs.roundVoteSets[round]
  121. if !ok {
  122. return nil
  123. }
  124. switch type_ {
  125. case types.VoteTypePrevote:
  126. return rvs.Prevotes
  127. case types.VoteTypePrecommit:
  128. return rvs.Precommits
  129. default:
  130. panic(Fmt("Unexpected vote type %X", type_))
  131. }
  132. }
  133. func (hvs *HeightVoteSet) String() string {
  134. return hvs.StringIndented("")
  135. }
  136. func (hvs *HeightVoteSet) StringIndented(indent string) string {
  137. vsStrings := make([]string, 0, (len(hvs.roundVoteSets)+1)*2)
  138. // rounds 0 ~ hvs.round inclusive
  139. for round := 0; round <= hvs.round; round++ {
  140. voteSetString := hvs.roundVoteSets[round].Prevotes.StringShort()
  141. vsStrings = append(vsStrings, voteSetString)
  142. voteSetString = hvs.roundVoteSets[round].Precommits.StringShort()
  143. vsStrings = append(vsStrings, voteSetString)
  144. }
  145. // all other peer catchup rounds
  146. for round, roundVoteSet := range hvs.roundVoteSets {
  147. if round <= hvs.round {
  148. continue
  149. }
  150. voteSetString := roundVoteSet.Prevotes.StringShort()
  151. vsStrings = append(vsStrings, voteSetString)
  152. voteSetString = roundVoteSet.Precommits.StringShort()
  153. vsStrings = append(vsStrings, voteSetString)
  154. }
  155. return Fmt(`HeightVoteSet{H:%v R:0~%v
  156. %s %v
  157. %s}`,
  158. hvs.height, hvs.round,
  159. indent, strings.Join(vsStrings, "\n"+indent+" "),
  160. indent)
  161. }