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.

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