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.

73 lines
2.4 KiB

7 years ago
7 years ago
  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. cmn "github.com/tendermint/tmlibs/common"
  6. )
  7. type voteData struct {
  8. vote1 *Vote
  9. vote2 *Vote
  10. valid bool
  11. }
  12. func makeVote(val *PrivValidatorFS, chainID string, valIndex int, height int64, round, step int, blockID BlockID) *Vote {
  13. v := &Vote{
  14. ValidatorAddress: val.PubKey.Address(),
  15. ValidatorIndex: valIndex,
  16. Height: height,
  17. Round: round,
  18. Type: byte(step),
  19. BlockID: blockID,
  20. }
  21. sig := val.PrivKey.Sign(SignBytes(chainID, v))
  22. v.Signature = sig
  23. return v
  24. }
  25. func TestEvidence(t *testing.T) {
  26. _, tmpFilePath := cmn.Tempfile("priv_validator_")
  27. val := GenPrivValidatorFS(tmpFilePath)
  28. val2 := GenPrivValidatorFS(tmpFilePath)
  29. blockID := makeBlockID("blockhash", 1000, "partshash")
  30. blockID2 := makeBlockID("blockhash2", 1000, "partshash")
  31. blockID3 := makeBlockID("blockhash", 10000, "partshash")
  32. blockID4 := makeBlockID("blockhash", 10000, "partshash2")
  33. chainID := "mychain"
  34. vote1 := makeVote(val, chainID, 0, 10, 2, 1, blockID)
  35. badVote := makeVote(val, chainID, 0, 10, 2, 1, blockID)
  36. badVote.Signature = val2.PrivKey.Sign(SignBytes(chainID, badVote))
  37. cases := []voteData{
  38. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID2), true}, // different block ids
  39. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID3), true},
  40. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID4), true},
  41. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID), false}, // wrong block id
  42. {vote1, makeVote(val, "mychain2", 0, 10, 2, 1, blockID2), false}, // wrong chain id
  43. {vote1, makeVote(val, chainID, 1, 10, 2, 1, blockID2), false}, // wrong val index
  44. {vote1, makeVote(val, chainID, 0, 11, 2, 1, blockID2), false}, // wrong height
  45. {vote1, makeVote(val, chainID, 0, 10, 3, 1, blockID2), false}, // wrong round
  46. {vote1, makeVote(val, chainID, 0, 10, 2, 2, blockID2), false}, // wrong step
  47. {vote1, makeVote(val2, chainID, 0, 10, 2, 1, blockID), false}, // wrong validator
  48. {vote1, badVote, false}, // signed by wrong key
  49. }
  50. for _, c := range cases {
  51. ev := &DuplicateVoteEvidence{
  52. PubKey: val.PubKey,
  53. VoteA: c.vote1,
  54. VoteB: c.vote2,
  55. }
  56. if c.valid {
  57. assert.Nil(t, ev.Verify(chainID), "evidence should be valid")
  58. } else {
  59. assert.NotNil(t, ev.Verify(chainID), "evidence should be invalid")
  60. }
  61. }
  62. }