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.

471 lines
14 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
  1. package types
  2. import (
  3. "bytes"
  4. "github.com/tendermint/go-crypto"
  5. . "github.com/tendermint/tmlibs/common"
  6. . "github.com/tendermint/tmlibs/test"
  7. "testing"
  8. )
  9. // NOTE: privValidators are in order
  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 validator address/index
  15. func withValidator(vote *Vote, addr []byte, idx int) *Vote {
  16. vote = vote.Copy()
  17. vote.ValidatorAddress = addr
  18. vote.ValidatorIndex = idx
  19. return vote
  20. }
  21. // Convenience: Return new vote with different height
  22. func withHeight(vote *Vote, height int) *Vote {
  23. vote = vote.Copy()
  24. vote.Height = height
  25. return vote
  26. }
  27. // Convenience: Return new vote with different round
  28. func withRound(vote *Vote, round int) *Vote {
  29. vote = vote.Copy()
  30. vote.Round = round
  31. return vote
  32. }
  33. // Convenience: Return new vote with different type
  34. func withType(vote *Vote, type_ byte) *Vote {
  35. vote = vote.Copy()
  36. vote.Type = type_
  37. return vote
  38. }
  39. // Convenience: Return new vote with different blockHash
  40. func withBlockHash(vote *Vote, blockHash []byte) *Vote {
  41. vote = vote.Copy()
  42. vote.BlockID.Hash = blockHash
  43. return vote
  44. }
  45. // Convenience: Return new vote with different blockParts
  46. func withBlockPartsHeader(vote *Vote, blockPartsHeader PartSetHeader) *Vote {
  47. vote = vote.Copy()
  48. vote.BlockID.PartsHeader = blockPartsHeader
  49. return vote
  50. }
  51. func signAddVote(privVal *PrivValidator, vote *Vote, voteSet *VoteSet) (bool, error) {
  52. vote.Signature = privVal.Sign(SignBytes(voteSet.ChainID(), vote))
  53. added, err := voteSet.AddVote(vote)
  54. return added, err
  55. }
  56. func TestAddVote(t *testing.T) {
  57. height, round := 1, 0
  58. voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
  59. val0 := privValidators[0]
  60. // t.Logf(">> %v", voteSet)
  61. if voteSet.GetByAddress(val0.Address) != nil {
  62. t.Errorf("Expected GetByAddress(val0.Address) to be nil")
  63. }
  64. if voteSet.BitArray().GetIndex(0) {
  65. t.Errorf("Expected BitArray.GetIndex(0) to be false")
  66. }
  67. blockID, ok := voteSet.TwoThirdsMajority()
  68. if ok || !blockID.IsZero() {
  69. t.Errorf("There should be no 2/3 majority")
  70. }
  71. vote := &Vote{
  72. ValidatorAddress: val0.Address,
  73. ValidatorIndex: 0, // since privValidators are in order
  74. Height: height,
  75. Round: round,
  76. Type: VoteTypePrevote,
  77. BlockID: BlockID{nil, PartSetHeader{}},
  78. }
  79. _, err := signAddVote(val0, vote, voteSet)
  80. if err != nil {
  81. t.Error(err)
  82. }
  83. if voteSet.GetByAddress(val0.Address) == nil {
  84. t.Errorf("Expected GetByAddress(val0.Address) to be present")
  85. }
  86. if !voteSet.BitArray().GetIndex(0) {
  87. t.Errorf("Expected BitArray.GetIndex(0) to be true")
  88. }
  89. blockID, ok = voteSet.TwoThirdsMajority()
  90. if ok || !blockID.IsZero() {
  91. t.Errorf("There should be no 2/3 majority")
  92. }
  93. }
  94. func Test2_3Majority(t *testing.T) {
  95. height, round := 1, 0
  96. voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
  97. voteProto := &Vote{
  98. ValidatorAddress: nil, // NOTE: must fill in
  99. ValidatorIndex: -1, // NOTE: must fill in
  100. Height: height,
  101. Round: round,
  102. Type: VoteTypePrevote,
  103. BlockID: BlockID{nil, PartSetHeader{}},
  104. }
  105. // 6 out of 10 voted for nil.
  106. for i := 0; i < 6; i++ {
  107. vote := withValidator(voteProto, privValidators[i].Address, i)
  108. signAddVote(privValidators[i], vote, voteSet)
  109. }
  110. blockID, ok := voteSet.TwoThirdsMajority()
  111. if ok || !blockID.IsZero() {
  112. t.Errorf("There should be no 2/3 majority")
  113. }
  114. // 7th validator voted for some blockhash
  115. {
  116. vote := withValidator(voteProto, privValidators[6].Address, 6)
  117. signAddVote(privValidators[6], withBlockHash(vote, RandBytes(32)), voteSet)
  118. blockID, ok = voteSet.TwoThirdsMajority()
  119. if ok || !blockID.IsZero() {
  120. t.Errorf("There should be no 2/3 majority")
  121. }
  122. }
  123. // 8th validator voted for nil.
  124. {
  125. vote := withValidator(voteProto, privValidators[7].Address, 7)
  126. signAddVote(privValidators[7], vote, voteSet)
  127. blockID, ok = voteSet.TwoThirdsMajority()
  128. if !ok || !blockID.IsZero() {
  129. t.Errorf("There should be 2/3 majority for nil")
  130. }
  131. }
  132. }
  133. func Test2_3MajorityRedux(t *testing.T) {
  134. height, round := 1, 0
  135. voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 100, 1)
  136. blockHash := crypto.CRandBytes(32)
  137. blockPartsTotal := 123
  138. blockPartsHeader := PartSetHeader{blockPartsTotal, crypto.CRandBytes(32)}
  139. voteProto := &Vote{
  140. ValidatorAddress: nil, // NOTE: must fill in
  141. ValidatorIndex: -1, // NOTE: must fill in
  142. Height: height,
  143. Round: round,
  144. Type: VoteTypePrevote,
  145. BlockID: BlockID{blockHash, blockPartsHeader},
  146. }
  147. // 66 out of 100 voted for nil.
  148. for i := 0; i < 66; i++ {
  149. vote := withValidator(voteProto, privValidators[i].Address, i)
  150. signAddVote(privValidators[i], vote, voteSet)
  151. }
  152. blockID, ok := voteSet.TwoThirdsMajority()
  153. if ok || !blockID.IsZero() {
  154. t.Errorf("There should be no 2/3 majority")
  155. }
  156. // 67th validator voted for nil
  157. {
  158. vote := withValidator(voteProto, privValidators[66].Address, 66)
  159. signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
  160. blockID, ok = voteSet.TwoThirdsMajority()
  161. if ok || !blockID.IsZero() {
  162. t.Errorf("There should be no 2/3 majority: last vote added was nil")
  163. }
  164. }
  165. // 68th validator voted for a different BlockParts PartSetHeader
  166. {
  167. vote := withValidator(voteProto, privValidators[67].Address, 67)
  168. blockPartsHeader := PartSetHeader{blockPartsTotal, crypto.CRandBytes(32)}
  169. signAddVote(privValidators[67], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
  170. blockID, ok = voteSet.TwoThirdsMajority()
  171. if ok || !blockID.IsZero() {
  172. t.Errorf("There should be no 2/3 majority: last vote added had different PartSetHeader Hash")
  173. }
  174. }
  175. // 69th validator voted for different BlockParts Total
  176. {
  177. vote := withValidator(voteProto, privValidators[68].Address, 68)
  178. blockPartsHeader := PartSetHeader{blockPartsTotal + 1, blockPartsHeader.Hash}
  179. signAddVote(privValidators[68], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
  180. blockID, ok = voteSet.TwoThirdsMajority()
  181. if ok || !blockID.IsZero() {
  182. t.Errorf("There should be no 2/3 majority: last vote added had different PartSetHeader Total")
  183. }
  184. }
  185. // 70th validator voted for different BlockHash
  186. {
  187. vote := withValidator(voteProto, privValidators[69].Address, 69)
  188. signAddVote(privValidators[69], withBlockHash(vote, RandBytes(32)), voteSet)
  189. blockID, ok = voteSet.TwoThirdsMajority()
  190. if ok || !blockID.IsZero() {
  191. t.Errorf("There should be no 2/3 majority: last vote added had different BlockHash")
  192. }
  193. }
  194. // 71st validator voted for the right BlockHash & BlockPartsHeader
  195. {
  196. vote := withValidator(voteProto, privValidators[70].Address, 70)
  197. signAddVote(privValidators[70], vote, voteSet)
  198. blockID, ok = voteSet.TwoThirdsMajority()
  199. if !ok || !blockID.Equals(BlockID{blockHash, blockPartsHeader}) {
  200. t.Errorf("There should be 2/3 majority")
  201. }
  202. }
  203. }
  204. func TestBadVotes(t *testing.T) {
  205. height, round := 1, 0
  206. voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 10, 1)
  207. voteProto := &Vote{
  208. ValidatorAddress: nil,
  209. ValidatorIndex: -1,
  210. Height: height,
  211. Round: round,
  212. Type: VoteTypePrevote,
  213. BlockID: BlockID{nil, PartSetHeader{}},
  214. }
  215. // val0 votes for nil.
  216. {
  217. vote := withValidator(voteProto, privValidators[0].Address, 0)
  218. added, err := signAddVote(privValidators[0], vote, voteSet)
  219. if !added || err != nil {
  220. t.Errorf("Expected VoteSet.Add to succeed")
  221. }
  222. }
  223. // val0 votes again for some block.
  224. {
  225. vote := withValidator(voteProto, privValidators[0].Address, 0)
  226. added, err := signAddVote(privValidators[0], withBlockHash(vote, RandBytes(32)), voteSet)
  227. if added || err == nil {
  228. t.Errorf("Expected VoteSet.Add to fail, conflicting vote.")
  229. }
  230. }
  231. // val1 votes on another height
  232. {
  233. vote := withValidator(voteProto, privValidators[1].Address, 1)
  234. added, err := signAddVote(privValidators[1], withHeight(vote, height+1), voteSet)
  235. if added || err == nil {
  236. t.Errorf("Expected VoteSet.Add to fail, wrong height")
  237. }
  238. }
  239. // val2 votes on another round
  240. {
  241. vote := withValidator(voteProto, privValidators[2].Address, 2)
  242. added, err := signAddVote(privValidators[2], withRound(vote, round+1), voteSet)
  243. if added || err == nil {
  244. t.Errorf("Expected VoteSet.Add to fail, wrong round")
  245. }
  246. }
  247. // val3 votes of another type.
  248. {
  249. vote := withValidator(voteProto, privValidators[3].Address, 3)
  250. added, err := signAddVote(privValidators[3], withType(vote, VoteTypePrecommit), voteSet)
  251. if added || err == nil {
  252. t.Errorf("Expected VoteSet.Add to fail, wrong type")
  253. }
  254. }
  255. }
  256. func TestConflicts(t *testing.T) {
  257. height, round := 1, 0
  258. voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrevote, 4, 1)
  259. blockHash1 := RandBytes(32)
  260. blockHash2 := RandBytes(32)
  261. voteProto := &Vote{
  262. ValidatorAddress: nil,
  263. ValidatorIndex: -1,
  264. Height: height,
  265. Round: round,
  266. Type: VoteTypePrevote,
  267. BlockID: BlockID{nil, PartSetHeader{}},
  268. }
  269. // val0 votes for nil.
  270. {
  271. vote := withValidator(voteProto, privValidators[0].Address, 0)
  272. added, err := signAddVote(privValidators[0], vote, voteSet)
  273. if !added || err != nil {
  274. t.Errorf("Expected VoteSet.Add to succeed")
  275. }
  276. }
  277. // val0 votes again for blockHash1.
  278. {
  279. vote := withValidator(voteProto, privValidators[0].Address, 0)
  280. added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash1), voteSet)
  281. if added {
  282. t.Errorf("Expected VoteSet.Add to fail, conflicting vote.")
  283. }
  284. if err == nil {
  285. t.Errorf("Expected VoteSet.Add to return error, conflicting vote.")
  286. }
  287. }
  288. // start tracking blockHash1
  289. voteSet.SetPeerMaj23("peerA", BlockID{blockHash1, PartSetHeader{}})
  290. // val0 votes again for blockHash1.
  291. {
  292. vote := withValidator(voteProto, privValidators[0].Address, 0)
  293. added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash1), voteSet)
  294. if !added {
  295. t.Errorf("Expected VoteSet.Add to succeed, called SetPeerMaj23().")
  296. }
  297. if err == nil {
  298. t.Errorf("Expected VoteSet.Add to return error, conflicting vote.")
  299. }
  300. }
  301. // attempt tracking blockHash2, should fail because already set for peerA.
  302. voteSet.SetPeerMaj23("peerA", BlockID{blockHash2, PartSetHeader{}})
  303. // val0 votes again for blockHash1.
  304. {
  305. vote := withValidator(voteProto, privValidators[0].Address, 0)
  306. added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash2), voteSet)
  307. if added {
  308. t.Errorf("Expected VoteSet.Add to fail, duplicate SetPeerMaj23() from peerA")
  309. }
  310. if err == nil {
  311. t.Errorf("Expected VoteSet.Add to return error, conflicting vote.")
  312. }
  313. }
  314. // val1 votes for blockHash1.
  315. {
  316. vote := withValidator(voteProto, privValidators[1].Address, 1)
  317. added, err := signAddVote(privValidators[1], withBlockHash(vote, blockHash1), voteSet)
  318. if !added || err != nil {
  319. t.Errorf("Expected VoteSet.Add to succeed")
  320. }
  321. }
  322. // check
  323. if voteSet.HasTwoThirdsMajority() {
  324. t.Errorf("We shouldn't have 2/3 majority yet")
  325. }
  326. if voteSet.HasTwoThirdsAny() {
  327. t.Errorf("We shouldn't have 2/3 if any votes yet")
  328. }
  329. // val2 votes for blockHash2.
  330. {
  331. vote := withValidator(voteProto, privValidators[2].Address, 2)
  332. added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash2), voteSet)
  333. if !added || err != nil {
  334. t.Errorf("Expected VoteSet.Add to succeed")
  335. }
  336. }
  337. // check
  338. if voteSet.HasTwoThirdsMajority() {
  339. t.Errorf("We shouldn't have 2/3 majority yet")
  340. }
  341. if !voteSet.HasTwoThirdsAny() {
  342. t.Errorf("We should have 2/3 if any votes")
  343. }
  344. // now attempt tracking blockHash1
  345. voteSet.SetPeerMaj23("peerB", BlockID{blockHash1, PartSetHeader{}})
  346. // val2 votes for blockHash1.
  347. {
  348. vote := withValidator(voteProto, privValidators[2].Address, 2)
  349. added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash1), voteSet)
  350. if !added {
  351. t.Errorf("Expected VoteSet.Add to succeed")
  352. }
  353. if err == nil {
  354. t.Errorf("Expected VoteSet.Add to return error, conflicting vote")
  355. }
  356. }
  357. // check
  358. if !voteSet.HasTwoThirdsMajority() {
  359. t.Errorf("We should have 2/3 majority for blockHash1")
  360. }
  361. blockIDMaj23, _ := voteSet.TwoThirdsMajority()
  362. if !bytes.Equal(blockIDMaj23.Hash, blockHash1) {
  363. t.Errorf("Got the wrong 2/3 majority blockhash")
  364. }
  365. if !voteSet.HasTwoThirdsAny() {
  366. t.Errorf("We should have 2/3 if any votes")
  367. }
  368. }
  369. func TestMakeCommit(t *testing.T) {
  370. height, round := 1, 0
  371. voteSet, _, privValidators := randVoteSet(height, round, VoteTypePrecommit, 10, 1)
  372. blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)}
  373. voteProto := &Vote{
  374. ValidatorAddress: nil,
  375. ValidatorIndex: -1,
  376. Height: height,
  377. Round: round,
  378. Type: VoteTypePrecommit,
  379. BlockID: BlockID{blockHash, blockPartsHeader},
  380. }
  381. // 6 out of 10 voted for some block.
  382. for i := 0; i < 6; i++ {
  383. vote := withValidator(voteProto, privValidators[i].Address, i)
  384. signAddVote(privValidators[i], vote, voteSet)
  385. }
  386. // MakeCommit should fail.
  387. AssertPanics(t, "Doesn't have +2/3 majority", func() { voteSet.MakeCommit() })
  388. // 7th voted for some other block.
  389. {
  390. vote := withValidator(voteProto, privValidators[6].Address, 6)
  391. vote = withBlockHash(vote, RandBytes(32))
  392. vote = withBlockPartsHeader(vote, PartSetHeader{123, RandBytes(32)})
  393. signAddVote(privValidators[6], vote, voteSet)
  394. }
  395. // The 8th voted like everyone else.
  396. {
  397. vote := withValidator(voteProto, privValidators[7].Address, 7)
  398. signAddVote(privValidators[7], vote, voteSet)
  399. }
  400. commit := voteSet.MakeCommit()
  401. // Commit should have 10 elements
  402. if len(commit.Precommits) != 10 {
  403. t.Errorf("Commit Precommits should have the same number of precommits as validators")
  404. }
  405. // Ensure that Commit precommits are ordered.
  406. if err := commit.ValidateBasic(); err != nil {
  407. t.Errorf("Error in Commit.ValidateBasic(): %v", err)
  408. }
  409. }