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.

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