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.

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