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.

268 lines
8.2 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
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
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
  1. package consensus
  2. import (
  3. "bytes"
  4. . "github.com/tendermint/tendermint/common"
  5. . "github.com/tendermint/tendermint/common/test"
  6. _ "github.com/tendermint/tendermint/config/tendermint_test"
  7. sm "github.com/tendermint/tendermint/state"
  8. "github.com/tendermint/tendermint/types"
  9. "testing"
  10. )
  11. // NOTE: see consensus/test.go for common test methods.
  12. // Convenience: Return new vote with different height
  13. func withHeight(vote *types.Vote, height int) *types.Vote {
  14. vote = vote.Copy()
  15. vote.Height = height
  16. return vote
  17. }
  18. // Convenience: Return new vote with different round
  19. func withRound(vote *types.Vote, round int) *types.Vote {
  20. vote = vote.Copy()
  21. vote.Round = round
  22. return vote
  23. }
  24. // Convenience: Return new vote with different type
  25. func withType(vote *types.Vote, type_ byte) *types.Vote {
  26. vote = vote.Copy()
  27. vote.Type = type_
  28. return vote
  29. }
  30. // Convenience: Return new vote with different blockHash
  31. func withBlockHash(vote *types.Vote, blockHash []byte) *types.Vote {
  32. vote = vote.Copy()
  33. vote.BlockHash = blockHash
  34. return vote
  35. }
  36. // Convenience: Return new vote with different blockParts
  37. func withBlockParts(vote *types.Vote, blockParts types.PartSetHeader) *types.Vote {
  38. vote = vote.Copy()
  39. vote.BlockParts = blockParts
  40. return vote
  41. }
  42. func signAddVote(privVal *sm.PrivValidator, vote *types.Vote, voteSet *VoteSet) (bool, error) {
  43. privVal.SignVoteUnsafe(config.GetString("chain_id"), vote)
  44. added, _, err := voteSet.AddByAddress(privVal.Address, vote)
  45. return added, err
  46. }
  47. func TestAddVote(t *testing.T) {
  48. height, round := 1, 0
  49. voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  50. val0 := privValidators[0]
  51. // t.Logf(">> %v", voteSet)
  52. if voteSet.GetByAddress(val0.Address) != nil {
  53. t.Errorf("Expected GetByAddress(val0.Address) to be nil")
  54. }
  55. if voteSet.BitArray().GetIndex(0) {
  56. t.Errorf("Expected BitArray.GetIndex(0) to be false")
  57. }
  58. hash, header, ok := voteSet.TwoThirdsMajority()
  59. if hash != nil || !header.IsZero() || ok {
  60. t.Errorf("There should be no 2/3 majority")
  61. }
  62. vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: nil}
  63. signAddVote(val0, vote, voteSet)
  64. if voteSet.GetByAddress(val0.Address) == nil {
  65. t.Errorf("Expected GetByAddress(val0.Address) to be present")
  66. }
  67. if !voteSet.BitArray().GetIndex(0) {
  68. t.Errorf("Expected BitArray.GetIndex(0) to be true")
  69. }
  70. hash, header, ok = voteSet.TwoThirdsMajority()
  71. if hash != nil || !header.IsZero() || ok {
  72. t.Errorf("There should be no 2/3 majority")
  73. }
  74. }
  75. func Test2_3Majority(t *testing.T) {
  76. height, round := 1, 0
  77. voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  78. vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: nil}
  79. // 6 out of 10 voted for nil.
  80. for i := 0; i < 6; i++ {
  81. signAddVote(privValidators[i], vote, voteSet)
  82. }
  83. hash, header, ok := voteSet.TwoThirdsMajority()
  84. if hash != nil || !header.IsZero() || ok {
  85. t.Errorf("There should be no 2/3 majority")
  86. }
  87. // 7th validator voted for some blockhash
  88. {
  89. signAddVote(privValidators[6], withBlockHash(vote, RandBytes(32)), voteSet)
  90. hash, header, ok = voteSet.TwoThirdsMajority()
  91. if hash != nil || !header.IsZero() || ok {
  92. t.Errorf("There should be no 2/3 majority")
  93. }
  94. }
  95. // 8th validator voted for nil.
  96. {
  97. signAddVote(privValidators[7], vote, voteSet)
  98. hash, header, ok = voteSet.TwoThirdsMajority()
  99. if hash != nil || !header.IsZero() || !ok {
  100. t.Errorf("There should be 2/3 majority for nil")
  101. }
  102. }
  103. }
  104. func Test2_3MajorityRedux(t *testing.T) {
  105. height, round := 1, 0
  106. voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 100, 1)
  107. blockHash := CRandBytes(32)
  108. blockPartsTotal := 123
  109. blockParts := types.PartSetHeader{blockPartsTotal, CRandBytes(32)}
  110. vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash, BlockParts: blockParts}
  111. // 66 out of 100 voted for nil.
  112. for i := 0; i < 66; i++ {
  113. signAddVote(privValidators[i], vote, voteSet)
  114. }
  115. hash, header, ok := voteSet.TwoThirdsMajority()
  116. if hash != nil || !header.IsZero() || ok {
  117. t.Errorf("There should be no 2/3 majority")
  118. }
  119. // 67th validator voted for nil
  120. {
  121. signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
  122. hash, header, ok = voteSet.TwoThirdsMajority()
  123. if hash != nil || !header.IsZero() || ok {
  124. t.Errorf("There should be no 2/3 majority: last vote added was nil")
  125. }
  126. }
  127. // 68th validator voted for a different BlockParts PartSetHeader
  128. {
  129. blockParts := types.PartSetHeader{blockPartsTotal, CRandBytes(32)}
  130. signAddVote(privValidators[67], withBlockParts(vote, blockParts), voteSet)
  131. hash, header, ok = voteSet.TwoThirdsMajority()
  132. if hash != nil || !header.IsZero() || ok {
  133. t.Errorf("There should be no 2/3 majority: last vote added had different PartSetHeader Hash")
  134. }
  135. }
  136. // 69th validator voted for different BlockParts Total
  137. {
  138. blockParts := types.PartSetHeader{blockPartsTotal + 1, blockParts.Hash}
  139. signAddVote(privValidators[68], withBlockParts(vote, blockParts), voteSet)
  140. hash, header, ok = voteSet.TwoThirdsMajority()
  141. if hash != nil || !header.IsZero() || ok {
  142. t.Errorf("There should be no 2/3 majority: last vote added had different PartSetHeader Total")
  143. }
  144. }
  145. // 70th validator voted for different BlockHash
  146. {
  147. signAddVote(privValidators[69], withBlockHash(vote, RandBytes(32)), voteSet)
  148. hash, header, ok = voteSet.TwoThirdsMajority()
  149. if hash != nil || !header.IsZero() || ok {
  150. t.Errorf("There should be no 2/3 majority: last vote added had different BlockHash")
  151. }
  152. }
  153. // 71st validator voted for the right BlockHash & BlockParts
  154. {
  155. signAddVote(privValidators[70], vote, voteSet)
  156. hash, header, ok = voteSet.TwoThirdsMajority()
  157. if !bytes.Equal(hash, blockHash) || !header.Equals(blockParts) || !ok {
  158. t.Errorf("There should be 2/3 majority")
  159. }
  160. }
  161. }
  162. func TestBadVotes(t *testing.T) {
  163. height, round := 1, 0
  164. voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  165. // val0 votes for nil.
  166. vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: nil}
  167. added, err := signAddVote(privValidators[0], vote, voteSet)
  168. if !added || err != nil {
  169. t.Errorf("Expected VoteSet.Add to succeed")
  170. }
  171. // val0 votes again for some block.
  172. added, err = signAddVote(privValidators[0], withBlockHash(vote, RandBytes(32)), voteSet)
  173. if added || err == nil {
  174. t.Errorf("Expected VoteSet.Add to fail, dupeout.")
  175. }
  176. // val1 votes on another height
  177. added, err = signAddVote(privValidators[1], withHeight(vote, height+1), voteSet)
  178. if added {
  179. t.Errorf("Expected VoteSet.Add to fail, wrong height")
  180. }
  181. // val2 votes on another round
  182. added, err = signAddVote(privValidators[2], withRound(vote, round+1), voteSet)
  183. if added {
  184. t.Errorf("Expected VoteSet.Add to fail, wrong round")
  185. }
  186. // val3 votes of another type.
  187. added, err = signAddVote(privValidators[3], withType(vote, types.VoteTypePrecommit), voteSet)
  188. if added {
  189. t.Errorf("Expected VoteSet.Add to fail, wrong type")
  190. }
  191. }
  192. func TestMakeValidation(t *testing.T) {
  193. height, round := 1, 0
  194. voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrecommit, 10, 1)
  195. blockHash, blockParts := CRandBytes(32), types.PartSetHeader{123, CRandBytes(32)}
  196. vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrecommit,
  197. BlockHash: blockHash, BlockParts: blockParts}
  198. // 6 out of 10 voted for some block.
  199. for i := 0; i < 6; i++ {
  200. signAddVote(privValidators[i], vote, voteSet)
  201. }
  202. // MakeValidation should fail.
  203. AssertPanics(t, "Doesn't have +2/3 majority", func() { voteSet.MakeValidation() })
  204. // 7th voted for some other block.
  205. {
  206. vote := withBlockHash(vote, RandBytes(32))
  207. vote = withBlockParts(vote, types.PartSetHeader{123, RandBytes(32)})
  208. signAddVote(privValidators[6], vote, voteSet)
  209. }
  210. // The 8th voted like everyone else.
  211. {
  212. signAddVote(privValidators[7], vote, voteSet)
  213. }
  214. validation := voteSet.MakeValidation()
  215. // Validation should have 10 elements
  216. if len(validation.Precommits) != 10 {
  217. t.Errorf("Validation Precommits should have the same number of precommits as validators")
  218. }
  219. // Ensure that Validation precommits are ordered.
  220. if err := validation.ValidateBasic(); err != nil {
  221. t.Errorf("Error in Validation.ValidateBasic(): %v", err)
  222. }
  223. }