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.

548 lines
15 KiB

lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tendermint/tendermint/crypto"
  7. tmrand "github.com/tendermint/tendermint/libs/rand"
  8. tmtime "github.com/tendermint/tendermint/types/time"
  9. )
  10. // NOTE: privValidators are in order
  11. func randVoteSet(
  12. height int64,
  13. round int,
  14. signedMsgType SignedMsgType,
  15. numValidators int,
  16. votingPower int64,
  17. ) (*VoteSet, *ValidatorSet, []PrivValidator) {
  18. valSet, privValidators := RandValidatorSet(numValidators, votingPower)
  19. return NewVoteSet("test_chain_id", height, round, signedMsgType, valSet), valSet, privValidators
  20. }
  21. // Convenience: Return new vote with different validator address/index
  22. func withValidator(vote *Vote, addr []byte, idx int) *Vote {
  23. vote = vote.Copy()
  24. vote.ValidatorAddress = addr
  25. vote.ValidatorIndex = idx
  26. return vote
  27. }
  28. // Convenience: Return new vote with different height
  29. func withHeight(vote *Vote, height int64) *Vote {
  30. vote = vote.Copy()
  31. vote.Height = height
  32. return vote
  33. }
  34. // Convenience: Return new vote with different round
  35. func withRound(vote *Vote, round int) *Vote {
  36. vote = vote.Copy()
  37. vote.Round = round
  38. return vote
  39. }
  40. // Convenience: Return new vote with different type
  41. func withType(vote *Vote, signedMsgType byte) *Vote {
  42. vote = vote.Copy()
  43. vote.Type = SignedMsgType(signedMsgType)
  44. return vote
  45. }
  46. // Convenience: Return new vote with different blockHash
  47. func withBlockHash(vote *Vote, blockHash []byte) *Vote {
  48. vote = vote.Copy()
  49. vote.BlockID.Hash = blockHash
  50. return vote
  51. }
  52. // Convenience: Return new vote with different blockParts
  53. func withBlockPartsHeader(vote *Vote, blockPartsHeader PartSetHeader) *Vote {
  54. vote = vote.Copy()
  55. vote.BlockID.PartsHeader = blockPartsHeader
  56. return vote
  57. }
  58. func TestAddVote(t *testing.T) {
  59. height, round := int64(1), 0
  60. voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 10, 1)
  61. val0 := privValidators[0]
  62. // t.Logf(">> %v", voteSet)
  63. val0Addr := val0.GetPubKey().Address()
  64. if voteSet.GetByAddress(val0Addr) != nil {
  65. t.Errorf("expected GetByAddress(val0.Address) to be nil")
  66. }
  67. if voteSet.BitArray().GetIndex(0) {
  68. t.Errorf("expected BitArray.GetIndex(0) to be false")
  69. }
  70. blockID, ok := voteSet.TwoThirdsMajority()
  71. if ok || !blockID.IsZero() {
  72. t.Errorf("there should be no 2/3 majority")
  73. }
  74. vote := &Vote{
  75. ValidatorAddress: val0Addr,
  76. ValidatorIndex: 0, // since privValidators are in order
  77. Height: height,
  78. Round: round,
  79. Type: PrevoteType,
  80. Timestamp: tmtime.Now(),
  81. BlockID: BlockID{nil, PartSetHeader{}},
  82. }
  83. _, err := signAddVote(val0, vote, voteSet)
  84. if err != nil {
  85. t.Error(err)
  86. }
  87. if voteSet.GetByAddress(val0Addr) == nil {
  88. t.Errorf("expected GetByAddress(val0.Address) to be present")
  89. }
  90. if !voteSet.BitArray().GetIndex(0) {
  91. t.Errorf("expected BitArray.GetIndex(0) to be true")
  92. }
  93. blockID, ok = voteSet.TwoThirdsMajority()
  94. if ok || !blockID.IsZero() {
  95. t.Errorf("there should be no 2/3 majority")
  96. }
  97. }
  98. func Test2_3Majority(t *testing.T) {
  99. height, round := int64(1), 0
  100. voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 10, 1)
  101. voteProto := &Vote{
  102. ValidatorAddress: nil, // NOTE: must fill in
  103. ValidatorIndex: -1, // NOTE: must fill in
  104. Height: height,
  105. Round: round,
  106. Type: PrevoteType,
  107. Timestamp: tmtime.Now(),
  108. BlockID: BlockID{nil, PartSetHeader{}},
  109. }
  110. // 6 out of 10 voted for nil.
  111. for i := 0; i < 6; i++ {
  112. addr := privValidators[i].GetPubKey().Address()
  113. vote := withValidator(voteProto, addr, i)
  114. _, err := signAddVote(privValidators[i], vote, voteSet)
  115. if err != nil {
  116. t.Error(err)
  117. }
  118. }
  119. blockID, ok := voteSet.TwoThirdsMajority()
  120. if ok || !blockID.IsZero() {
  121. t.Errorf("there should be no 2/3 majority")
  122. }
  123. // 7th validator voted for some blockhash
  124. {
  125. addr := privValidators[6].GetPubKey().Address()
  126. vote := withValidator(voteProto, addr, 6)
  127. _, err := signAddVote(privValidators[6], withBlockHash(vote, tmrand.Bytes(32)), voteSet)
  128. if err != nil {
  129. t.Error(err)
  130. }
  131. blockID, ok = voteSet.TwoThirdsMajority()
  132. if ok || !blockID.IsZero() {
  133. t.Errorf("there should be no 2/3 majority")
  134. }
  135. }
  136. // 8th validator voted for nil.
  137. {
  138. addr := privValidators[7].GetPubKey().Address()
  139. vote := withValidator(voteProto, addr, 7)
  140. _, err := signAddVote(privValidators[7], vote, voteSet)
  141. if err != nil {
  142. t.Error(err)
  143. }
  144. blockID, ok = voteSet.TwoThirdsMajority()
  145. if !ok || !blockID.IsZero() {
  146. t.Errorf("there should be 2/3 majority for nil")
  147. }
  148. }
  149. }
  150. func Test2_3MajorityRedux(t *testing.T) {
  151. height, round := int64(1), 0
  152. voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 100, 1)
  153. blockHash := crypto.CRandBytes(32)
  154. blockPartsTotal := 123
  155. blockPartsHeader := PartSetHeader{blockPartsTotal, crypto.CRandBytes(32)}
  156. voteProto := &Vote{
  157. ValidatorAddress: nil, // NOTE: must fill in
  158. ValidatorIndex: -1, // NOTE: must fill in
  159. Height: height,
  160. Round: round,
  161. Timestamp: tmtime.Now(),
  162. Type: PrevoteType,
  163. BlockID: BlockID{blockHash, blockPartsHeader},
  164. }
  165. // 66 out of 100 voted for nil.
  166. for i := 0; i < 66; i++ {
  167. addr := privValidators[i].GetPubKey().Address()
  168. vote := withValidator(voteProto, addr, i)
  169. _, err := signAddVote(privValidators[i], vote, voteSet)
  170. if err != nil {
  171. t.Error(err)
  172. }
  173. }
  174. blockID, ok := voteSet.TwoThirdsMajority()
  175. if ok || !blockID.IsZero() {
  176. t.Errorf("there should be no 2/3 majority")
  177. }
  178. // 67th validator voted for nil
  179. {
  180. adrr := privValidators[66].GetPubKey().Address()
  181. vote := withValidator(voteProto, adrr, 66)
  182. _, err := signAddVote(privValidators[66], withBlockHash(vote, nil), voteSet)
  183. if err != nil {
  184. t.Error(err)
  185. }
  186. blockID, ok = voteSet.TwoThirdsMajority()
  187. if ok || !blockID.IsZero() {
  188. t.Errorf("there should be no 2/3 majority: last vote added was nil")
  189. }
  190. }
  191. // 68th validator voted for a different BlockParts PartSetHeader
  192. {
  193. addr := privValidators[67].GetPubKey().Address()
  194. vote := withValidator(voteProto, addr, 67)
  195. blockPartsHeader := PartSetHeader{blockPartsTotal, crypto.CRandBytes(32)}
  196. _, err := signAddVote(privValidators[67], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
  197. if err != nil {
  198. t.Error(err)
  199. }
  200. blockID, ok = voteSet.TwoThirdsMajority()
  201. if ok || !blockID.IsZero() {
  202. t.Errorf("there should be no 2/3 majority: last vote added had different PartSetHeader Hash")
  203. }
  204. }
  205. // 69th validator voted for different BlockParts Total
  206. {
  207. addr := privValidators[68].GetPubKey().Address()
  208. vote := withValidator(voteProto, addr, 68)
  209. blockPartsHeader := PartSetHeader{blockPartsTotal + 1, blockPartsHeader.Hash}
  210. _, err := signAddVote(privValidators[68], withBlockPartsHeader(vote, blockPartsHeader), voteSet)
  211. if err != nil {
  212. t.Error(err)
  213. }
  214. blockID, ok = voteSet.TwoThirdsMajority()
  215. if ok || !blockID.IsZero() {
  216. t.Errorf("there should be no 2/3 majority: last vote added had different PartSetHeader Total")
  217. }
  218. }
  219. // 70th validator voted for different BlockHash
  220. {
  221. addr := privValidators[69].GetPubKey().Address()
  222. vote := withValidator(voteProto, addr, 69)
  223. _, err := signAddVote(privValidators[69], withBlockHash(vote, tmrand.Bytes(32)), voteSet)
  224. if err != nil {
  225. t.Error(err)
  226. }
  227. blockID, ok = voteSet.TwoThirdsMajority()
  228. if ok || !blockID.IsZero() {
  229. t.Errorf("there should be no 2/3 majority: last vote added had different BlockHash")
  230. }
  231. }
  232. // 71st validator voted for the right BlockHash & BlockPartsHeader
  233. {
  234. addr := privValidators[70].GetPubKey().Address()
  235. vote := withValidator(voteProto, addr, 70)
  236. _, err := signAddVote(privValidators[70], vote, voteSet)
  237. if err != nil {
  238. t.Error(err)
  239. }
  240. blockID, ok = voteSet.TwoThirdsMajority()
  241. if !ok || !blockID.Equals(BlockID{blockHash, blockPartsHeader}) {
  242. t.Errorf("there should be 2/3 majority")
  243. }
  244. }
  245. }
  246. func TestBadVotes(t *testing.T) {
  247. height, round := int64(1), 0
  248. voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 10, 1)
  249. voteProto := &Vote{
  250. ValidatorAddress: nil,
  251. ValidatorIndex: -1,
  252. Height: height,
  253. Round: round,
  254. Timestamp: tmtime.Now(),
  255. Type: PrevoteType,
  256. BlockID: BlockID{nil, PartSetHeader{}},
  257. }
  258. // val0 votes for nil.
  259. {
  260. addr := privValidators[0].GetPubKey().Address()
  261. vote := withValidator(voteProto, addr, 0)
  262. added, err := signAddVote(privValidators[0], vote, voteSet)
  263. if !added || err != nil {
  264. t.Errorf("expected VoteSet.Add to succeed")
  265. }
  266. }
  267. // val0 votes again for some block.
  268. {
  269. addr := privValidators[0].GetPubKey().Address()
  270. vote := withValidator(voteProto, addr, 0)
  271. added, err := signAddVote(privValidators[0], withBlockHash(vote, tmrand.Bytes(32)), voteSet)
  272. if added || err == nil {
  273. t.Errorf("expected VoteSet.Add to fail, conflicting vote.")
  274. }
  275. }
  276. // val1 votes on another height
  277. {
  278. addr := privValidators[1].GetPubKey().Address()
  279. vote := withValidator(voteProto, addr, 1)
  280. added, err := signAddVote(privValidators[1], withHeight(vote, height+1), voteSet)
  281. if added || err == nil {
  282. t.Errorf("expected VoteSet.Add to fail, wrong height")
  283. }
  284. }
  285. // val2 votes on another round
  286. {
  287. addr := privValidators[2].GetPubKey().Address()
  288. vote := withValidator(voteProto, addr, 2)
  289. added, err := signAddVote(privValidators[2], withRound(vote, round+1), voteSet)
  290. if added || err == nil {
  291. t.Errorf("expected VoteSet.Add to fail, wrong round")
  292. }
  293. }
  294. // val3 votes of another type.
  295. {
  296. addr := privValidators[3].GetPubKey().Address()
  297. vote := withValidator(voteProto, addr, 3)
  298. added, err := signAddVote(privValidators[3], withType(vote, byte(PrecommitType)), voteSet)
  299. if added || err == nil {
  300. t.Errorf("expected VoteSet.Add to fail, wrong type")
  301. }
  302. }
  303. }
  304. func TestConflicts(t *testing.T) {
  305. height, round := int64(1), 0
  306. voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 4, 1)
  307. blockHash1 := tmrand.Bytes(32)
  308. blockHash2 := tmrand.Bytes(32)
  309. voteProto := &Vote{
  310. ValidatorAddress: nil,
  311. ValidatorIndex: -1,
  312. Height: height,
  313. Round: round,
  314. Timestamp: tmtime.Now(),
  315. Type: PrevoteType,
  316. BlockID: BlockID{nil, PartSetHeader{}},
  317. }
  318. val0Addr := privValidators[0].GetPubKey().Address()
  319. // val0 votes for nil.
  320. {
  321. vote := withValidator(voteProto, val0Addr, 0)
  322. added, err := signAddVote(privValidators[0], vote, voteSet)
  323. if !added || err != nil {
  324. t.Errorf("expected VoteSet.Add to succeed")
  325. }
  326. }
  327. // val0 votes again for blockHash1.
  328. {
  329. vote := withValidator(voteProto, val0Addr, 0)
  330. added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash1), voteSet)
  331. if added {
  332. t.Errorf("expected VoteSet.Add to fail, conflicting vote.")
  333. }
  334. if err == nil {
  335. t.Errorf("expected VoteSet.Add to return error, conflicting vote.")
  336. }
  337. }
  338. // start tracking blockHash1
  339. voteSet.SetPeerMaj23("peerA", BlockID{blockHash1, PartSetHeader{}})
  340. // val0 votes again for blockHash1.
  341. {
  342. vote := withValidator(voteProto, val0Addr, 0)
  343. added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash1), voteSet)
  344. if !added {
  345. t.Errorf("expected VoteSet.Add to succeed, called SetPeerMaj23().")
  346. }
  347. if err == nil {
  348. t.Errorf("expected VoteSet.Add to return error, conflicting vote.")
  349. }
  350. }
  351. // attempt tracking blockHash2, should fail because already set for peerA.
  352. voteSet.SetPeerMaj23("peerA", BlockID{blockHash2, PartSetHeader{}})
  353. // val0 votes again for blockHash1.
  354. {
  355. vote := withValidator(voteProto, val0Addr, 0)
  356. added, err := signAddVote(privValidators[0], withBlockHash(vote, blockHash2), voteSet)
  357. if added {
  358. t.Errorf("expected VoteSet.Add to fail, duplicate SetPeerMaj23() from peerA")
  359. }
  360. if err == nil {
  361. t.Errorf("expected VoteSet.Add to return error, conflicting vote.")
  362. }
  363. }
  364. // val1 votes for blockHash1.
  365. {
  366. addr := privValidators[1].GetPubKey().Address()
  367. vote := withValidator(voteProto, addr, 1)
  368. added, err := signAddVote(privValidators[1], withBlockHash(vote, blockHash1), voteSet)
  369. if !added || err != nil {
  370. t.Errorf("expected VoteSet.Add to succeed")
  371. }
  372. }
  373. // check
  374. if voteSet.HasTwoThirdsMajority() {
  375. t.Errorf("we shouldn't have 2/3 majority yet")
  376. }
  377. if voteSet.HasTwoThirdsAny() {
  378. t.Errorf("we shouldn't have 2/3 if any votes yet")
  379. }
  380. // val2 votes for blockHash2.
  381. {
  382. addr := privValidators[2].GetPubKey().Address()
  383. vote := withValidator(voteProto, addr, 2)
  384. added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash2), voteSet)
  385. if !added || err != nil {
  386. t.Errorf("expected VoteSet.Add to succeed")
  387. }
  388. }
  389. // check
  390. if voteSet.HasTwoThirdsMajority() {
  391. t.Errorf("we shouldn't have 2/3 majority yet")
  392. }
  393. if !voteSet.HasTwoThirdsAny() {
  394. t.Errorf("we should have 2/3 if any votes")
  395. }
  396. // now attempt tracking blockHash1
  397. voteSet.SetPeerMaj23("peerB", BlockID{blockHash1, PartSetHeader{}})
  398. // val2 votes for blockHash1.
  399. {
  400. addr := privValidators[2].GetPubKey().Address()
  401. vote := withValidator(voteProto, addr, 2)
  402. added, err := signAddVote(privValidators[2], withBlockHash(vote, blockHash1), voteSet)
  403. if !added {
  404. t.Errorf("expected VoteSet.Add to succeed")
  405. }
  406. if err == nil {
  407. t.Errorf("expected VoteSet.Add to return error, conflicting vote")
  408. }
  409. }
  410. // check
  411. if !voteSet.HasTwoThirdsMajority() {
  412. t.Errorf("we should have 2/3 majority for blockHash1")
  413. }
  414. blockIDMaj23, _ := voteSet.TwoThirdsMajority()
  415. if !bytes.Equal(blockIDMaj23.Hash, blockHash1) {
  416. t.Errorf("got the wrong 2/3 majority blockhash")
  417. }
  418. if !voteSet.HasTwoThirdsAny() {
  419. t.Errorf("we should have 2/3 if any votes")
  420. }
  421. }
  422. func TestMakeCommit(t *testing.T) {
  423. height, round := int64(1), 0
  424. voteSet, _, privValidators := randVoteSet(height, round, PrecommitType, 10, 1)
  425. blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)}
  426. voteProto := &Vote{
  427. ValidatorAddress: nil,
  428. ValidatorIndex: -1,
  429. Height: height,
  430. Round: round,
  431. Timestamp: tmtime.Now(),
  432. Type: PrecommitType,
  433. BlockID: BlockID{blockHash, blockPartsHeader},
  434. }
  435. // 6 out of 10 voted for some block.
  436. for i := 0; i < 6; i++ {
  437. addr := privValidators[i].GetPubKey().Address()
  438. vote := withValidator(voteProto, addr, i)
  439. _, err := signAddVote(privValidators[i], vote, voteSet)
  440. if err != nil {
  441. t.Error(err)
  442. }
  443. }
  444. // MakeCommit should fail.
  445. assert.Panics(t, func() { voteSet.MakeCommit() }, "Doesn't have +2/3 majority")
  446. // 7th voted for some other block.
  447. {
  448. addr := privValidators[6].GetPubKey().Address()
  449. vote := withValidator(voteProto, addr, 6)
  450. vote = withBlockHash(vote, tmrand.Bytes(32))
  451. vote = withBlockPartsHeader(vote, PartSetHeader{123, tmrand.Bytes(32)})
  452. _, err := signAddVote(privValidators[6], vote, voteSet)
  453. if err != nil {
  454. t.Error(err)
  455. }
  456. }
  457. // The 8th voted like everyone else.
  458. {
  459. addr := privValidators[7].GetPubKey().Address()
  460. vote := withValidator(voteProto, addr, 7)
  461. _, err := signAddVote(privValidators[7], vote, voteSet)
  462. if err != nil {
  463. t.Error(err)
  464. }
  465. }
  466. // The 9th voted for nil.
  467. {
  468. addr := privValidators[8].GetPubKey().Address()
  469. vote := withValidator(voteProto, addr, 8)
  470. vote.BlockID = BlockID{}
  471. _, err := signAddVote(privValidators[8], vote, voteSet)
  472. if err != nil {
  473. t.Error(err)
  474. }
  475. }
  476. commit := voteSet.MakeCommit()
  477. // Commit should have 10 elements
  478. if len(commit.Signatures) != 10 {
  479. t.Errorf("expected commit to include %d elems, got %d", 10, len(commit.Signatures))
  480. }
  481. // Ensure that Commit is good.
  482. if err := commit.ValidateBasic(); err != nil {
  483. t.Errorf("error in Commit.ValidateBasic(): %v", err)
  484. }
  485. }