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.

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