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.

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