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.

529 lines
14 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. cmn "github.com/tendermint/tmlibs/common"
  8. )
  9. /*
  10. VoteSet helps collect signatures from validators at each height+round for a
  11. predefined vote type.
  12. We need VoteSet to be able to keep track of conflicting votes when validators
  13. double-sign. Yet, we can't keep track of *all* the votes seen, as that could
  14. be a DoS attack vector.
  15. There are two storage areas for votes.
  16. 1. voteSet.votes
  17. 2. voteSet.votesByBlock
  18. `.votes` is the "canonical" list of votes. It always has at least one vote,
  19. if a vote from a validator had been seen at all. Usually it keeps track of
  20. the first vote seen, but when a 2/3 majority is found, votes for that get
  21. priority and are copied over from `.votesByBlock`.
  22. `.votesByBlock` keeps track of a list of votes for a particular block. There
  23. are two ways a &blockVotes{} gets created in `.votesByBlock`.
  24. 1. the first vote seen by a validator was for the particular block.
  25. 2. a peer claims to have seen 2/3 majority for the particular block.
  26. Since the first vote from a validator will always get added in `.votesByBlock`
  27. , all votes in `.votes` will have a corresponding entry in `.votesByBlock`.
  28. When a &blockVotes{} in `.votesByBlock` reaches a 2/3 majority quorum, its
  29. votes are copied into `.votes`.
  30. All this is memory bounded because conflicting votes only get added if a peer
  31. told us to track that block, each peer only gets to tell us 1 such block, and,
  32. there's only a limited number of peers.
  33. NOTE: Assumes that the sum total of voting power does not exceed MaxUInt64.
  34. */
  35. type VoteSet struct {
  36. chainID string
  37. height int
  38. round int
  39. type_ byte
  40. mtx sync.Mutex
  41. valSet *ValidatorSet
  42. votesBitArray *cmn.BitArray
  43. votes []*Vote // Primary votes to share
  44. sum int64 // Sum of voting power for seen votes, discounting conflicts
  45. maj23 *BlockID // First 2/3 majority seen
  46. votesByBlock map[string]*blockVotes // string(blockHash|blockParts) -> blockVotes
  47. peerMaj23s map[string]BlockID // Maj23 for each peer
  48. }
  49. // Constructs a new VoteSet struct used to accumulate votes for given height/round.
  50. func NewVoteSet(chainID string, height int, round int, type_ byte, valSet *ValidatorSet) *VoteSet {
  51. if height == 0 {
  52. cmn.PanicSanity("Cannot make VoteSet for height == 0, doesn't make sense.")
  53. }
  54. return &VoteSet{
  55. chainID: chainID,
  56. height: height,
  57. round: round,
  58. type_: type_,
  59. valSet: valSet,
  60. votesBitArray: cmn.NewBitArray(valSet.Size()),
  61. votes: make([]*Vote, valSet.Size()),
  62. sum: 0,
  63. maj23: nil,
  64. votesByBlock: make(map[string]*blockVotes, valSet.Size()),
  65. peerMaj23s: make(map[string]BlockID),
  66. }
  67. }
  68. func (voteSet *VoteSet) ChainID() string {
  69. return voteSet.chainID
  70. }
  71. func (voteSet *VoteSet) Height() int {
  72. if voteSet == nil {
  73. return 0
  74. } else {
  75. return voteSet.height
  76. }
  77. }
  78. func (voteSet *VoteSet) Round() int {
  79. if voteSet == nil {
  80. return -1
  81. } else {
  82. return voteSet.round
  83. }
  84. }
  85. func (voteSet *VoteSet) Type() byte {
  86. if voteSet == nil {
  87. return 0x00
  88. } else {
  89. return voteSet.type_
  90. }
  91. }
  92. func (voteSet *VoteSet) Size() int {
  93. if voteSet == nil {
  94. return 0
  95. } else {
  96. return voteSet.valSet.Size()
  97. }
  98. }
  99. // Returns added=true if vote is valid and new.
  100. // Otherwise returns err=ErrVote[
  101. // UnexpectedStep | InvalidIndex | InvalidAddress |
  102. // InvalidSignature | InvalidBlockHash | ConflictingVotes ]
  103. // Duplicate votes return added=false, err=nil.
  104. // Conflicting votes return added=*, err=ErrVoteConflictingVotes.
  105. // NOTE: vote should not be mutated after adding.
  106. // NOTE: VoteSet must not be nil
  107. func (voteSet *VoteSet) AddVote(vote *Vote) (added bool, err error) {
  108. if voteSet == nil {
  109. cmn.PanicSanity("AddVote() on nil VoteSet")
  110. }
  111. voteSet.mtx.Lock()
  112. defer voteSet.mtx.Unlock()
  113. return voteSet.addVote(vote)
  114. }
  115. // NOTE: Validates as much as possible before attempting to verify the signature.
  116. func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) {
  117. valIndex := vote.ValidatorIndex
  118. valAddr := vote.ValidatorAddress
  119. blockKey := vote.BlockID.Key()
  120. // Ensure that validator index was set
  121. if valIndex < 0 {
  122. return false, ErrVoteInvalidValidatorIndex
  123. } else if len(valAddr) == 0 {
  124. return false, ErrVoteInvalidValidatorAddress
  125. }
  126. // Make sure the step matches.
  127. if (vote.Height != voteSet.height) ||
  128. (vote.Round != voteSet.round) ||
  129. (vote.Type != voteSet.type_) {
  130. return false, ErrVoteUnexpectedStep
  131. }
  132. // Ensure that signer is a validator.
  133. lookupAddr, val := voteSet.valSet.GetByIndex(valIndex)
  134. if val == nil {
  135. return false, ErrVoteInvalidValidatorIndex
  136. }
  137. // Ensure that the signer has the right address
  138. if !bytes.Equal(valAddr, lookupAddr) {
  139. return false, ErrVoteInvalidValidatorAddress
  140. }
  141. // If we already know of this vote, return false.
  142. if existing, ok := voteSet.getVote(valIndex, blockKey); ok {
  143. if existing.Signature.Equals(vote.Signature) {
  144. return false, nil // duplicate
  145. } else {
  146. return false, ErrVoteInvalidSignature // NOTE: assumes deterministic signatures
  147. }
  148. }
  149. // Check signature.
  150. if !val.PubKey.VerifyBytes(SignBytes(voteSet.chainID, vote), vote.Signature) {
  151. // Bad signature.
  152. return false, ErrVoteInvalidSignature
  153. }
  154. // Add vote and get conflicting vote if any
  155. added, conflicting := voteSet.addVerifiedVote(vote, blockKey, val.VotingPower)
  156. if conflicting != nil {
  157. return added, &ErrVoteConflictingVotes{
  158. VoteA: conflicting,
  159. VoteB: vote,
  160. }
  161. } else {
  162. if !added {
  163. cmn.PanicSanity("Expected to add non-conflicting vote")
  164. }
  165. return added, nil
  166. }
  167. }
  168. // Returns (vote, true) if vote exists for valIndex and blockKey
  169. func (voteSet *VoteSet) getVote(valIndex int, blockKey string) (vote *Vote, ok bool) {
  170. if existing := voteSet.votes[valIndex]; existing != nil && existing.BlockID.Key() == blockKey {
  171. return existing, true
  172. }
  173. if existing := voteSet.votesByBlock[blockKey].getByIndex(valIndex); existing != nil {
  174. return existing, true
  175. }
  176. return nil, false
  177. }
  178. // Assumes signature is valid.
  179. // If conflicting vote exists, returns it.
  180. func (voteSet *VoteSet) addVerifiedVote(vote *Vote, blockKey string, votingPower int64) (added bool, conflicting *Vote) {
  181. valIndex := vote.ValidatorIndex
  182. // Already exists in voteSet.votes?
  183. if existing := voteSet.votes[valIndex]; existing != nil {
  184. if existing.BlockID.Equals(vote.BlockID) {
  185. cmn.PanicSanity("addVerifiedVote does not expect duplicate votes")
  186. } else {
  187. conflicting = existing
  188. }
  189. // Replace vote if blockKey matches voteSet.maj23.
  190. if voteSet.maj23 != nil && voteSet.maj23.Key() == blockKey {
  191. voteSet.votes[valIndex] = vote
  192. voteSet.votesBitArray.SetIndex(valIndex, true)
  193. }
  194. // Otherwise don't add it to voteSet.votes
  195. } else {
  196. // Add to voteSet.votes and incr .sum
  197. voteSet.votes[valIndex] = vote
  198. voteSet.votesBitArray.SetIndex(valIndex, true)
  199. voteSet.sum += votingPower
  200. }
  201. votesByBlock, ok := voteSet.votesByBlock[blockKey]
  202. if ok {
  203. if conflicting != nil && !votesByBlock.peerMaj23 {
  204. // There's a conflict and no peer claims that this block is special.
  205. return false, conflicting
  206. }
  207. // We'll add the vote in a bit.
  208. } else {
  209. // .votesByBlock doesn't exist...
  210. if conflicting != nil {
  211. // ... and there's a conflicting vote.
  212. // We're not even tracking this blockKey, so just forget it.
  213. return false, conflicting
  214. } else {
  215. // ... and there's no conflicting vote.
  216. // Start tracking this blockKey
  217. votesByBlock = newBlockVotes(false, voteSet.valSet.Size())
  218. voteSet.votesByBlock[blockKey] = votesByBlock
  219. // We'll add the vote in a bit.
  220. }
  221. }
  222. // Before adding to votesByBlock, see if we'll exceed quorum
  223. origSum := votesByBlock.sum
  224. quorum := voteSet.valSet.TotalVotingPower()*2/3 + 1
  225. // Add vote to votesByBlock
  226. votesByBlock.addVerifiedVote(vote, votingPower)
  227. // If we just crossed the quorum threshold and have 2/3 majority...
  228. if origSum < quorum && quorum <= votesByBlock.sum {
  229. // Only consider the first quorum reached
  230. if voteSet.maj23 == nil {
  231. maj23BlockID := vote.BlockID
  232. voteSet.maj23 = &maj23BlockID
  233. // And also copy votes over to voteSet.votes
  234. for i, vote := range votesByBlock.votes {
  235. if vote != nil {
  236. voteSet.votes[i] = vote
  237. }
  238. }
  239. }
  240. }
  241. return true, conflicting
  242. }
  243. // If a peer claims that it has 2/3 majority for given blockKey, call this.
  244. // NOTE: if there are too many peers, or too much peer churn,
  245. // this can cause memory issues.
  246. // TODO: implement ability to remove peers too
  247. // NOTE: VoteSet must not be nil
  248. func (voteSet *VoteSet) SetPeerMaj23(peerID string, blockID BlockID) {
  249. if voteSet == nil {
  250. cmn.PanicSanity("SetPeerMaj23() on nil VoteSet")
  251. }
  252. voteSet.mtx.Lock()
  253. defer voteSet.mtx.Unlock()
  254. blockKey := blockID.Key()
  255. // Make sure peer hasn't already told us something.
  256. if existing, ok := voteSet.peerMaj23s[peerID]; ok {
  257. if existing.Equals(blockID) {
  258. return // Nothing to do
  259. } else {
  260. return // TODO bad peer!
  261. }
  262. }
  263. voteSet.peerMaj23s[peerID] = blockID
  264. // Create .votesByBlock entry if needed.
  265. votesByBlock, ok := voteSet.votesByBlock[blockKey]
  266. if ok {
  267. if votesByBlock.peerMaj23 {
  268. return // Nothing to do
  269. } else {
  270. votesByBlock.peerMaj23 = true
  271. // No need to copy votes, already there.
  272. }
  273. } else {
  274. votesByBlock = newBlockVotes(true, voteSet.valSet.Size())
  275. voteSet.votesByBlock[blockKey] = votesByBlock
  276. // No need to copy votes, no votes to copy over.
  277. }
  278. }
  279. func (voteSet *VoteSet) BitArray() *cmn.BitArray {
  280. if voteSet == nil {
  281. return nil
  282. }
  283. voteSet.mtx.Lock()
  284. defer voteSet.mtx.Unlock()
  285. return voteSet.votesBitArray.Copy()
  286. }
  287. func (voteSet *VoteSet) BitArrayByBlockID(blockID BlockID) *cmn.BitArray {
  288. if voteSet == nil {
  289. return nil
  290. }
  291. voteSet.mtx.Lock()
  292. defer voteSet.mtx.Unlock()
  293. votesByBlock, ok := voteSet.votesByBlock[blockID.Key()]
  294. if ok {
  295. return votesByBlock.bitArray.Copy()
  296. }
  297. return nil
  298. }
  299. // NOTE: if validator has conflicting votes, returns "canonical" vote
  300. func (voteSet *VoteSet) GetByIndex(valIndex int) *Vote {
  301. if voteSet == nil {
  302. return nil
  303. }
  304. voteSet.mtx.Lock()
  305. defer voteSet.mtx.Unlock()
  306. return voteSet.votes[valIndex]
  307. }
  308. func (voteSet *VoteSet) GetByAddress(address []byte) *Vote {
  309. if voteSet == nil {
  310. return nil
  311. }
  312. voteSet.mtx.Lock()
  313. defer voteSet.mtx.Unlock()
  314. valIndex, val := voteSet.valSet.GetByAddress(address)
  315. if val == nil {
  316. cmn.PanicSanity("GetByAddress(address) returned nil")
  317. }
  318. return voteSet.votes[valIndex]
  319. }
  320. func (voteSet *VoteSet) HasTwoThirdsMajority() bool {
  321. if voteSet == nil {
  322. return false
  323. }
  324. voteSet.mtx.Lock()
  325. defer voteSet.mtx.Unlock()
  326. return voteSet.maj23 != nil
  327. }
  328. func (voteSet *VoteSet) IsCommit() bool {
  329. if voteSet == nil {
  330. return false
  331. }
  332. if voteSet.type_ != VoteTypePrecommit {
  333. return false
  334. }
  335. voteSet.mtx.Lock()
  336. defer voteSet.mtx.Unlock()
  337. return voteSet.maj23 != nil
  338. }
  339. func (voteSet *VoteSet) HasTwoThirdsAny() bool {
  340. if voteSet == nil {
  341. return false
  342. }
  343. voteSet.mtx.Lock()
  344. defer voteSet.mtx.Unlock()
  345. return voteSet.sum > voteSet.valSet.TotalVotingPower()*2/3
  346. }
  347. func (voteSet *VoteSet) HasAll() bool {
  348. return voteSet.sum == voteSet.valSet.TotalVotingPower()
  349. }
  350. // Returns either a blockhash (or nil) that received +2/3 majority.
  351. // If there exists no such majority, returns (nil, PartSetHeader{}, false).
  352. func (voteSet *VoteSet) TwoThirdsMajority() (blockID BlockID, ok bool) {
  353. if voteSet == nil {
  354. return BlockID{}, false
  355. }
  356. voteSet.mtx.Lock()
  357. defer voteSet.mtx.Unlock()
  358. if voteSet.maj23 != nil {
  359. return *voteSet.maj23, true
  360. } else {
  361. return BlockID{}, false
  362. }
  363. }
  364. func (voteSet *VoteSet) String() string {
  365. if voteSet == nil {
  366. return "nil-VoteSet"
  367. }
  368. return voteSet.StringIndented("")
  369. }
  370. func (voteSet *VoteSet) StringIndented(indent string) string {
  371. voteStrings := make([]string, len(voteSet.votes))
  372. for i, vote := range voteSet.votes {
  373. if vote == nil {
  374. voteStrings[i] = "nil-Vote"
  375. } else {
  376. voteStrings[i] = vote.String()
  377. }
  378. }
  379. return fmt.Sprintf(`VoteSet{
  380. %s H:%v R:%v T:%v
  381. %s %v
  382. %s %v
  383. %s %v
  384. %s}`,
  385. indent, voteSet.height, voteSet.round, voteSet.type_,
  386. indent, strings.Join(voteStrings, "\n"+indent+" "),
  387. indent, voteSet.votesBitArray,
  388. indent, voteSet.peerMaj23s,
  389. indent)
  390. }
  391. func (voteSet *VoteSet) StringShort() string {
  392. if voteSet == nil {
  393. return "nil-VoteSet"
  394. }
  395. voteSet.mtx.Lock()
  396. defer voteSet.mtx.Unlock()
  397. return fmt.Sprintf(`VoteSet{H:%v R:%v T:%v +2/3:%v %v %v}`,
  398. voteSet.height, voteSet.round, voteSet.type_, voteSet.maj23, voteSet.votesBitArray, voteSet.peerMaj23s)
  399. }
  400. //--------------------------------------------------------------------------------
  401. // Commit
  402. func (voteSet *VoteSet) MakeCommit() *Commit {
  403. if voteSet.type_ != VoteTypePrecommit {
  404. cmn.PanicSanity("Cannot MakeCommit() unless VoteSet.Type is VoteTypePrecommit")
  405. }
  406. voteSet.mtx.Lock()
  407. defer voteSet.mtx.Unlock()
  408. // Make sure we have a 2/3 majority
  409. if voteSet.maj23 == nil {
  410. cmn.PanicSanity("Cannot MakeCommit() unless a blockhash has +2/3")
  411. }
  412. // For every validator, get the precommit
  413. votesCopy := make([]*Vote, len(voteSet.votes))
  414. copy(votesCopy, voteSet.votes)
  415. return &Commit{
  416. BlockID: *voteSet.maj23,
  417. Precommits: votesCopy,
  418. }
  419. }
  420. //--------------------------------------------------------------------------------
  421. /*
  422. Votes for a particular block
  423. There are two ways a *blockVotes gets created for a blockKey.
  424. 1. first (non-conflicting) vote of a validator w/ blockKey (peerMaj23=false)
  425. 2. A peer claims to have a 2/3 majority w/ blockKey (peerMaj23=true)
  426. */
  427. type blockVotes struct {
  428. peerMaj23 bool // peer claims to have maj23
  429. bitArray *cmn.BitArray // valIndex -> hasVote?
  430. votes []*Vote // valIndex -> *Vote
  431. sum int64 // vote sum
  432. }
  433. func newBlockVotes(peerMaj23 bool, numValidators int) *blockVotes {
  434. return &blockVotes{
  435. peerMaj23: peerMaj23,
  436. bitArray: cmn.NewBitArray(numValidators),
  437. votes: make([]*Vote, numValidators),
  438. sum: 0,
  439. }
  440. }
  441. func (vs *blockVotes) addVerifiedVote(vote *Vote, votingPower int64) {
  442. valIndex := vote.ValidatorIndex
  443. if existing := vs.votes[valIndex]; existing == nil {
  444. vs.bitArray.SetIndex(valIndex, true)
  445. vs.votes[valIndex] = vote
  446. vs.sum += votingPower
  447. }
  448. }
  449. func (vs *blockVotes) getByIndex(index int) *Vote {
  450. if vs == nil {
  451. return nil
  452. }
  453. return vs.votes[index]
  454. }
  455. //--------------------------------------------------------------------------------
  456. // Common interface between *consensus.VoteSet and types.Commit
  457. type VoteSetReader interface {
  458. Height() int
  459. Round() int
  460. Type() byte
  461. Size() int
  462. BitArray() *cmn.BitArray
  463. GetByIndex(int) *Vote
  464. IsCommit() bool
  465. }