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.

212 lines
6.4 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
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. "github.com/tendermint/tendermint/binary"
  4. . "github.com/tendermint/tendermint/common"
  5. sm "github.com/tendermint/tendermint/state"
  6. "github.com/tendermint/tendermint/types"
  7. "bytes"
  8. "testing"
  9. )
  10. // NOTE: see consensus/test.go for common test methods.
  11. // Convenience method.
  12. // Signs the vote and sets the POL's vote at the desired index
  13. // Returns the POLVoteSignature pointer, so you can modify it afterwards.
  14. func signAddPOLVoteSignature(val *sm.PrivValidator, valSet *sm.ValidatorSet, vote *types.Vote, pol *POL) *POLVoteSignature {
  15. vote = vote.Copy()
  16. err := val.SignVote(vote)
  17. if err != nil {
  18. panic(err)
  19. }
  20. idx, _ := valSet.GetByAddress(val.Address) // now we have the index
  21. pol.Votes[idx] = POLVoteSignature{vote.Round, vote.Signature}
  22. return &pol.Votes[idx]
  23. }
  24. func TestVerifyVotes(t *testing.T) {
  25. height, round := uint(1), uint(0)
  26. _, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  27. // Make a POL with -2/3 votes.
  28. blockHash := RandBytes(32)
  29. pol := &POL{
  30. Height: height, Round: round, BlockHash: blockHash,
  31. Votes: make([]POLVoteSignature, valSet.Size()),
  32. }
  33. voteProto := &types.Vote{
  34. Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash,
  35. }
  36. for i := 0; i < 6; i++ {
  37. signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
  38. }
  39. // Check that validation fails.
  40. if err := pol.Verify(valSet); err == nil {
  41. t.Errorf("Expected POL.Verify() to fail, not enough votes.")
  42. }
  43. // Insert another vote to make +2/3
  44. signAddPOLVoteSignature(privValidators[7], valSet, voteProto, pol)
  45. // Check that validation succeeds.
  46. if err := pol.Verify(valSet); err != nil {
  47. t.Errorf("POL.Verify() failed: %v", err)
  48. }
  49. }
  50. func TestVerifyInvalidVote(t *testing.T) {
  51. height, round := uint(1), uint(0)
  52. _, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  53. // Make a POL with +2/3 votes with the wrong signature.
  54. blockHash := RandBytes(32)
  55. pol := &POL{
  56. Height: height, Round: round, BlockHash: blockHash,
  57. Votes: make([]POLVoteSignature, valSet.Size()),
  58. }
  59. voteProto := &types.Vote{
  60. Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash,
  61. }
  62. for i := 0; i < 7; i++ {
  63. polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
  64. polVoteSig.Signature[0] += byte(0x01) // mutated!
  65. }
  66. // Check that validation fails.
  67. if err := pol.Verify(valSet); err == nil {
  68. t.Errorf("Expected POL.Verify() to fail, wrong signatures.")
  69. }
  70. }
  71. func TestVerifyCommits(t *testing.T) {
  72. height, round := uint(1), uint(2)
  73. _, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  74. // Make a POL with +2/3 votes.
  75. blockHash := RandBytes(32)
  76. pol := &POL{
  77. Height: height, Round: round, BlockHash: blockHash,
  78. Votes: make([]POLVoteSignature, valSet.Size()),
  79. }
  80. voteProto := &types.Vote{
  81. Height: height, Round: round - 1, Type: types.VoteTypeCommit, BlockHash: blockHash,
  82. }
  83. for i := 0; i < 7; i++ {
  84. signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
  85. }
  86. // Check that validation succeeds.
  87. if err := pol.Verify(valSet); err != nil {
  88. t.Errorf("POL.Verify() failed: %v", err)
  89. }
  90. }
  91. func TestVerifyInvalidCommits(t *testing.T) {
  92. height, round := uint(1), uint(2)
  93. _, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  94. // Make a POL with +2/3 votes with the wrong signature.
  95. blockHash := RandBytes(32)
  96. pol := &POL{
  97. Height: height, Round: round, BlockHash: blockHash,
  98. Votes: make([]POLVoteSignature, valSet.Size()),
  99. }
  100. voteProto := &types.Vote{
  101. Height: height, Round: round - 1, Type: types.VoteTypeCommit, BlockHash: blockHash,
  102. }
  103. for i := 0; i < 7; i++ {
  104. polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
  105. polVoteSig.Signature[0] += byte(0x01)
  106. }
  107. // Check that validation fails.
  108. if err := pol.Verify(valSet); err == nil {
  109. t.Errorf("Expected POL.Verify() to fail, wrong signatures.")
  110. }
  111. }
  112. func TestVerifyInvalidCommitRounds(t *testing.T) {
  113. height, round := uint(1), uint(2)
  114. _, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  115. // Make a POL with +2/3 commits for the current round.
  116. blockHash := RandBytes(32)
  117. pol := &POL{
  118. Height: height, Round: round, BlockHash: blockHash,
  119. Votes: make([]POLVoteSignature, valSet.Size()),
  120. }
  121. voteProto := &types.Vote{
  122. Height: height, Round: round, Type: types.VoteTypeCommit, BlockHash: blockHash,
  123. }
  124. for i := 0; i < 7; i++ {
  125. signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
  126. }
  127. // Check that validation fails.
  128. if err := pol.Verify(valSet); err == nil {
  129. t.Errorf("Expected POL.Verify() to fail, same round.")
  130. }
  131. }
  132. func TestVerifyInvalidCommitRounds2(t *testing.T) {
  133. height, round := uint(1), uint(2)
  134. _, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  135. // Make a POL with +2/3 commits for future round.
  136. blockHash := RandBytes(32)
  137. pol := &POL{
  138. Height: height, Round: round, BlockHash: blockHash,
  139. Votes: make([]POLVoteSignature, valSet.Size()),
  140. }
  141. voteProto := &types.Vote{
  142. Height: height, Round: round + 1, Type: types.VoteTypeCommit, BlockHash: blockHash,
  143. }
  144. for i := 0; i < 7; i++ {
  145. polVoteSig := signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
  146. polVoteSig.Round += 1 // mutate round
  147. }
  148. // Check that validation fails.
  149. if err := pol.Verify(valSet); err == nil {
  150. t.Errorf("Expected POL.Verify() to fail, future round.")
  151. }
  152. }
  153. func TestReadWrite(t *testing.T) {
  154. height, round := uint(1), uint(2)
  155. _, valSet, privValidators := randVoteSet(height, round, types.VoteTypePrevote, 10, 1)
  156. // Make a POL with +2/3 votes.
  157. blockHash := RandBytes(32)
  158. pol := &POL{
  159. Height: height, Round: round, BlockHash: blockHash,
  160. Votes: make([]POLVoteSignature, valSet.Size()),
  161. }
  162. voteProto := &types.Vote{
  163. Height: height, Round: round, Type: types.VoteTypePrevote, BlockHash: blockHash,
  164. }
  165. for i := 0; i < 7; i++ {
  166. signAddPOLVoteSignature(privValidators[i], valSet, voteProto, pol)
  167. }
  168. // Write it to a buffer.
  169. buf, n, err := new(bytes.Buffer), new(int64), new(error)
  170. binary.WriteBinary(pol, buf, n, err)
  171. if *err != nil {
  172. t.Fatalf("Failed to write POL: %v", *err)
  173. }
  174. // Read from buffer.
  175. pol2 := binary.ReadBinary(&POL{}, buf, n, err).(*POL)
  176. if *err != nil {
  177. t.Fatalf("Failed to read POL: %v", *err)
  178. }
  179. // Check that validation succeeds.
  180. if err := pol2.Verify(valSet); err != nil {
  181. t.Errorf("POL.Verify() failed: %v", err)
  182. }
  183. }