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.

325 lines
10 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
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. sm "github.com/tendermint/tendermint/state"
  7. _ "github.com/tendermint/tendermint/config/tendermint_test"
  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 uint) *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 uint) *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(vote)
  44. added, _, err := voteSet.Add(privVal.Address, vote)
  45. return added, err
  46. }
  47. func TestAddVote(t *testing.T) {
  48. height, round := uint(1), uint(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 := uint(1), uint(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 := uint(1), uint(0)
  106. voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 100, 1)
  107. blockHash := CRandBytes(32)
  108. blockPartsTotal := uint(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 := uint(1), uint(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 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 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 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 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 Add() to fail, wrong type")
  190. }
  191. }
  192. func TestAddCommitsToPrevoteVotes(t *testing.T) {
  193. height, round := uint(2), uint(5)
  194. voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  195. // val0, val1, val2, val3, val4, val5 vote for nil.
  196. vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: nil}
  197. for i := 0; i < 6; i++ {
  198. signAddVote(privValidators[i], vote, voteSet)
  199. }
  200. hash, header, ok := voteSet.TwoThirdsMajority()
  201. if hash != nil || !header.IsZero() || ok {
  202. t.Errorf("There should be no 2/3 majority")
  203. }
  204. // Attempt to add a commit from val6 at a previous height
  205. vote = &types.Vote{Height: height - 1, Round: round, Type: types.VoteTypeCommit, BlockHash: nil}
  206. added, _ := signAddVote(privValidators[6], vote, voteSet)
  207. if added {
  208. t.Errorf("Expected Add() to fail, wrong height.")
  209. }
  210. // Attempt to add a commit from val6 at a later round
  211. vote = &types.Vote{Height: height, Round: round + 1, Type: types.VoteTypeCommit, BlockHash: nil}
  212. added, _ = signAddVote(privValidators[6], vote, voteSet)
  213. if added {
  214. t.Errorf("Expected Add() to fail, cannot add future round vote.")
  215. }
  216. // Attempt to add a commit from val6 for currrent height/round.
  217. vote = &types.Vote{Height: height, Round: round, Type: types.VoteTypeCommit, BlockHash: nil}
  218. added, err := signAddVote(privValidators[6], vote, voteSet)
  219. if added || err == nil {
  220. t.Errorf("Expected Add() to fail, only prior round commits can be added.")
  221. }
  222. // Add commit from val6 at a previous round
  223. vote = &types.Vote{Height: height, Round: round - 1, Type: types.VoteTypeCommit, BlockHash: nil}
  224. added, err = signAddVote(privValidators[6], vote, voteSet)
  225. if !added || err != nil {
  226. t.Errorf("Expected Add() to succeed, commit for prior rounds are relevant.")
  227. }
  228. // Also add commit from val7 for previous round.
  229. vote = &types.Vote{Height: height, Round: round - 2, Type: types.VoteTypeCommit, BlockHash: nil}
  230. added, err = signAddVote(privValidators[7], vote, voteSet)
  231. if !added || err != nil {
  232. t.Errorf("Expected Add() to succeed. err: %v", err)
  233. }
  234. // We should have 2/3 majority
  235. hash, header, ok = voteSet.TwoThirdsMajority()
  236. if hash != nil || !header.IsZero() || !ok {
  237. t.Errorf("There should be 2/3 majority for nil")
  238. }
  239. }
  240. func TestMakeValidation(t *testing.T) {
  241. height, round := uint(1), uint(0)
  242. voteSet, _, privValidators := randVoteSet(height, round, types.VoteTypeCommit, 10, 1)
  243. blockHash, blockParts := CRandBytes(32), types.PartSetHeader{123, CRandBytes(32)}
  244. vote := &types.Vote{Height: height, Round: round, Type: types.VoteTypeCommit,
  245. BlockHash: blockHash, BlockParts: blockParts}
  246. // 6 out of 10 voted for some block.
  247. for i := 0; i < 6; i++ {
  248. signAddVote(privValidators[i], vote, voteSet)
  249. }
  250. // MakeValidation should fail.
  251. AssertPanics(t, "Doesn't have +2/3 majority", func() { voteSet.MakeValidation() })
  252. // 7th voted for some other block.
  253. {
  254. vote := withBlockHash(vote, RandBytes(32))
  255. vote = withBlockParts(vote, types.PartSetHeader{123, RandBytes(32)})
  256. signAddVote(privValidators[6], vote, voteSet)
  257. }
  258. // The 8th voted like everyone else.
  259. {
  260. signAddVote(privValidators[7], vote, voteSet)
  261. }
  262. validation := voteSet.MakeValidation()
  263. // Validation should have 10 elements
  264. if len(validation.Commits) != 10 {
  265. t.Errorf("Validation Commits should have the same number of commits as validators")
  266. }
  267. // Ensure that Validation commits are ordered.
  268. if err := validation.ValidateBasic(); err != nil {
  269. t.Errorf("Error in Validation.ValidateBasic(): %v", err)
  270. }
  271. }