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.

82 lines
2.4 KiB

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