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.

84 lines
2.6 KiB

7 years ago
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, height, 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 makeBlockID(hash string, partSetSize int, partSetHash string) BlockID {
  26. return BlockID{
  27. Hash: []byte(hash),
  28. PartsHeader: PartSetHeader{
  29. Total: partSetSize,
  30. Hash: []byte(partSetHash),
  31. },
  32. }
  33. }
  34. func TestEvidence(t *testing.T) {
  35. _, tmpFilePath := cmn.Tempfile("priv_validator_")
  36. val := GenPrivValidatorFS(tmpFilePath)
  37. val2 := GenPrivValidatorFS(tmpFilePath)
  38. blockID := makeBlockID("blockhash", 1000, "partshash")
  39. blockID2 := makeBlockID("blockhash2", 1000, "partshash")
  40. blockID3 := makeBlockID("blockhash", 10000, "partshash")
  41. blockID4 := makeBlockID("blockhash", 10000, "partshash2")
  42. chainID := "mychain"
  43. vote1 := makeVote(val, chainID, 0, 10, 2, 1, blockID)
  44. badVote := makeVote(val, chainID, 0, 10, 2, 1, blockID)
  45. badVote.Signature = val2.PrivKey.Sign(SignBytes(chainID, badVote))
  46. cases := []voteData{
  47. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID2), true}, // different block ids
  48. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID3), true},
  49. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID4), true},
  50. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID), false}, // wrong block id
  51. {vote1, makeVote(val, "mychain2", 0, 10, 2, 1, blockID2), false}, // wrong chain id
  52. {vote1, makeVote(val, chainID, 1, 10, 2, 1, blockID2), false}, // wrong val index
  53. {vote1, makeVote(val, chainID, 0, 11, 2, 1, blockID2), false}, // wrong height
  54. {vote1, makeVote(val, chainID, 0, 10, 3, 1, blockID2), false}, // wrong round
  55. {vote1, makeVote(val, chainID, 0, 10, 2, 2, blockID2), false}, // wrong step
  56. {vote1, makeVote(val2, chainID, 0, 10, 2, 1, blockID), false}, // wrong validator
  57. {vote1, badVote, false}, // signed by wrong key
  58. }
  59. for _, c := range cases {
  60. ev := &DuplicateVoteEvidence{
  61. PubKey: val.PubKey,
  62. VoteA: c.vote1,
  63. VoteB: c.vote2,
  64. }
  65. if c.valid {
  66. assert.Nil(t, ev.Verify(chainID), "evidence should be valid")
  67. } else {
  68. assert.NotNil(t, ev.Verify(chainID), "evidence should be invalid")
  69. }
  70. }
  71. }