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.

202 lines
6.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package consensus
  2. import (
  3. . "github.com/tendermint/tendermint/blocks"
  4. . "github.com/tendermint/tendermint/common"
  5. . "github.com/tendermint/tendermint/state"
  6. "testing"
  7. )
  8. func makeValidator(id uint64, votingPower uint64) (*Validator, *PrivAccount) {
  9. privAccount := GenPrivAccount()
  10. privAccount.Id = id
  11. return &Validator{
  12. Account: privAccount.Account,
  13. VotingPower: votingPower,
  14. }, privAccount
  15. }
  16. func makeVoteSet(height uint32, round uint16, numValidators int, votingPower uint64) (*VoteSet, *ValidatorSet, []*PrivAccount) {
  17. vals := make([]*Validator, numValidators)
  18. privAccounts := make([]*PrivAccount, numValidators)
  19. for i := 0; i < numValidators; i++ {
  20. val, privAccount := makeValidator(uint64(i), votingPower)
  21. vals[i] = val
  22. privAccounts[i] = privAccount
  23. }
  24. valSet := NewValidatorSet(vals)
  25. return NewVoteSet(height, round, VoteTypeBare, valSet), valSet, privAccounts
  26. }
  27. func TestAddVote(t *testing.T) {
  28. voteSet, _, privAccounts := makeVoteSet(0, 0, 10, 1)
  29. // t.Logf(">> %v", voteSet)
  30. if voteSet.GetVote(0) != nil {
  31. t.Errorf("Expected GetVote(0) to be nil")
  32. }
  33. if voteSet.BitArray().GetIndex(0) {
  34. t.Errorf("Expected BitArray.GetIndex(0) to be false")
  35. }
  36. hash, commitTime, ok := voteSet.TwoThirdsMajority()
  37. if hash != nil || !commitTime.IsZero() || ok {
  38. t.Errorf("There should be no 2/3 majority")
  39. }
  40. vote := &Vote{Height: 0, Round: 0, Type: VoteTypeBare, BlockHash: nil}
  41. privAccounts[0].Sign(vote)
  42. voteSet.Add(vote)
  43. if voteSet.GetVote(0) == nil {
  44. t.Errorf("Expected GetVote(0) to be present")
  45. }
  46. if !voteSet.BitArray().GetIndex(0) {
  47. t.Errorf("Expected BitArray.GetIndex(0) to be true")
  48. }
  49. hash, commitTime, ok = voteSet.TwoThirdsMajority()
  50. if hash != nil || !commitTime.IsZero() || ok {
  51. t.Errorf("There should be no 2/3 majority")
  52. }
  53. }
  54. func Test2_3Majority(t *testing.T) {
  55. voteSet, _, privAccounts := makeVoteSet(0, 0, 10, 1)
  56. // 6 out of 10 voted for nil.
  57. vote := &Vote{Height: 0, Round: 0, Type: VoteTypeBare, BlockHash: nil}
  58. for i := 0; i < 6; i++ {
  59. privAccounts[i].Sign(vote)
  60. voteSet.Add(vote)
  61. }
  62. hash, commitTime, ok := voteSet.TwoThirdsMajority()
  63. if hash != nil || !commitTime.IsZero() || ok {
  64. t.Errorf("There should be no 2/3 majority")
  65. }
  66. // 7th validator voted for some blockhash
  67. vote.BlockHash = CRandBytes(32)
  68. privAccounts[6].Sign(vote)
  69. voteSet.Add(vote)
  70. hash, commitTime, ok = voteSet.TwoThirdsMajority()
  71. if hash != nil || !commitTime.IsZero() || ok {
  72. t.Errorf("There should be no 2/3 majority")
  73. }
  74. // 8th validator voted for nil.
  75. vote.BlockHash = nil
  76. privAccounts[7].Sign(vote)
  77. voteSet.Add(vote)
  78. hash, commitTime, ok = voteSet.TwoThirdsMajority()
  79. if hash != nil || commitTime.IsZero() || !ok {
  80. t.Errorf("There should be 2/3 majority for nil")
  81. }
  82. }
  83. func TestBadVotes(t *testing.T) {
  84. voteSet, _, privAccounts := makeVoteSet(1, 0, 10, 1)
  85. // val0 votes for nil.
  86. vote := &Vote{Height: 1, Round: 0, Type: VoteTypeBare, BlockHash: nil}
  87. privAccounts[0].Sign(vote)
  88. added, err := voteSet.Add(vote)
  89. if !added || err != nil {
  90. t.Errorf("Expected Add(vote) to succeed")
  91. }
  92. // val0 votes again for some block.
  93. vote = &Vote{Height: 1, Round: 0, Type: VoteTypeBare, BlockHash: CRandBytes(32)}
  94. privAccounts[0].Sign(vote)
  95. added, err = voteSet.Add(vote)
  96. if added || err == nil {
  97. t.Errorf("Expected Add(vote) to fail, dupeout.")
  98. }
  99. // val1 votes on another height
  100. vote = &Vote{Height: 0, Round: 0, Type: VoteTypeBare, BlockHash: nil}
  101. privAccounts[1].Sign(vote)
  102. added, err = voteSet.Add(vote)
  103. if added {
  104. t.Errorf("Expected Add(vote) to fail, wrong height")
  105. }
  106. // val2 votes on another round
  107. vote = &Vote{Height: 1, Round: 1, Type: VoteTypeBare, BlockHash: nil}
  108. privAccounts[2].Sign(vote)
  109. added, err = voteSet.Add(vote)
  110. if added {
  111. t.Errorf("Expected Add(vote) to fail, wrong round")
  112. }
  113. // val3 votes of another type.
  114. vote = &Vote{Height: 1, Round: 0, Type: VoteTypePrecommit, BlockHash: nil}
  115. privAccounts[3].Sign(vote)
  116. added, err = voteSet.Add(vote)
  117. if added {
  118. t.Errorf("Expected Add(vote) to fail, wrong type")
  119. }
  120. }
  121. func TestAddCommitsToBareVotes(t *testing.T) {
  122. voteSet, _, privAccounts := makeVoteSet(1, 5, 10, 1)
  123. // val0, val1, val2, val3, val4, val5 vote for nil.
  124. vote := &Vote{Height: 1, Round: 5, Type: VoteTypeBare, BlockHash: nil}
  125. for i := 0; i < 6; i++ {
  126. privAccounts[i].Sign(vote)
  127. voteSet.Add(vote)
  128. }
  129. hash, commitTime, ok := voteSet.TwoThirdsMajority()
  130. if hash != nil || !commitTime.IsZero() || ok {
  131. t.Errorf("There should be no 2/3 majority")
  132. }
  133. // Attempt to add a commit from val6 at a previous height
  134. vote = &Vote{Height: 0, Round: 5, Type: VoteTypeCommit, BlockHash: nil}
  135. privAccounts[6].Sign(vote)
  136. added, _ := voteSet.Add(vote)
  137. if added {
  138. t.Errorf("Expected Add(vote) to fail, wrong height.")
  139. }
  140. // Attempt to add a commit from val6 at a later round
  141. vote = &Vote{Height: 1, Round: 6, Type: VoteTypeCommit, BlockHash: nil}
  142. privAccounts[6].Sign(vote)
  143. added, _ = voteSet.Add(vote)
  144. if added {
  145. t.Errorf("Expected Add(vote) to fail, cannot add future round vote.")
  146. }
  147. // Attempt to add a commit from val6 for currrent height/round.
  148. vote = &Vote{Height: 1, Round: 5, Type: VoteTypeCommit, BlockHash: nil}
  149. privAccounts[6].Sign(vote)
  150. added, err := voteSet.Add(vote)
  151. if added || err == nil {
  152. t.Errorf("Expected Add(vote) to fail, only prior round commits can be added.")
  153. }
  154. // Add commit from val6 at a previous round
  155. vote = &Vote{Height: 1, Round: 4, Type: VoteTypeCommit, BlockHash: nil}
  156. privAccounts[6].Sign(vote)
  157. added, err = voteSet.Add(vote)
  158. if !added || err != nil {
  159. t.Errorf("Expected Add(vote) to succeed, commit for prior rounds are relevant.")
  160. }
  161. // Also add commit from val7 for previous round.
  162. vote = &Vote{Height: 1, Round: 3, Type: VoteTypeCommit, BlockHash: nil}
  163. privAccounts[7].Sign(vote)
  164. added, err = voteSet.Add(vote)
  165. if !added || err != nil {
  166. t.Errorf("Expected Add(vote) to succeed. err: %v", err)
  167. }
  168. // We should have 2/3 majority
  169. hash, commitTime, ok = voteSet.TwoThirdsMajority()
  170. if hash != nil || commitTime.IsZero() || !ok {
  171. t.Errorf("There should be 2/3 majority for nil")
  172. }
  173. }