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.

194 lines
5.2 KiB

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