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.

101 lines
3.0 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 int, height int64, round, step int, blockID BlockID) *Vote {
  12. v := &Vote{
  13. ValidatorAddress: val.GetAddress(),
  14. ValidatorIndex: valIndex,
  15. Height: height,
  16. Round: round,
  17. Type: byte(step),
  18. BlockID: blockID,
  19. }
  20. err := val.SignVote(chainID, v)
  21. if err != nil {
  22. panic(err)
  23. }
  24. return v
  25. }
  26. func TestEvidence(t *testing.T) {
  27. val := NewMockPV()
  28. val2 := NewMockPV()
  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. const chainID = "mychain"
  34. vote1 := makeVote(val, chainID, 0, 10, 2, 1, blockID)
  35. badVote := makeVote(val, chainID, 0, 10, 2, 1, blockID)
  36. err := val2.SignVote(chainID, badVote)
  37. if err != nil {
  38. panic(err)
  39. }
  40. cases := []voteData{
  41. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID2), true}, // different block ids
  42. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID3), true},
  43. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID4), true},
  44. {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID), false}, // wrong block id
  45. {vote1, makeVote(val, "mychain2", 0, 10, 2, 1, blockID2), false}, // wrong chain id
  46. {vote1, makeVote(val, chainID, 1, 10, 2, 1, blockID2), false}, // wrong val index
  47. {vote1, makeVote(val, chainID, 0, 11, 2, 1, blockID2), false}, // wrong height
  48. {vote1, makeVote(val, chainID, 0, 10, 3, 1, blockID2), false}, // wrong round
  49. {vote1, makeVote(val, chainID, 0, 10, 2, 2, blockID2), false}, // wrong step
  50. {vote1, makeVote(val2, chainID, 0, 10, 2, 1, blockID), false}, // wrong validator
  51. {vote1, badVote, false}, // signed by wrong key
  52. }
  53. pubKey := val.GetPubKey()
  54. for _, c := range cases {
  55. ev := &DuplicateVoteEvidence{
  56. VoteA: c.vote1,
  57. VoteB: c.vote2,
  58. }
  59. if c.valid {
  60. assert.Nil(t, ev.Verify(chainID, pubKey), "evidence should be valid")
  61. } else {
  62. assert.NotNil(t, ev.Verify(chainID, pubKey), "evidence should be invalid")
  63. }
  64. }
  65. }
  66. func TestDuplicatedVoteEvidence(t *testing.T) {
  67. ev := randomDuplicatedVoteEvidence()
  68. assert.True(t, ev.Equal(ev))
  69. assert.False(t, ev.Equal(&DuplicateVoteEvidence{}))
  70. }
  71. func TestEvidenceList(t *testing.T) {
  72. ev := randomDuplicatedVoteEvidence()
  73. evl := EvidenceList([]Evidence{ev})
  74. assert.NotNil(t, evl.Hash())
  75. assert.True(t, evl.Has(ev))
  76. assert.False(t, evl.Has(&DuplicateVoteEvidence{}))
  77. }
  78. func randomDuplicatedVoteEvidence() *DuplicateVoteEvidence {
  79. val := NewMockPV()
  80. blockID := makeBlockID("blockhash", 1000, "partshash")
  81. blockID2 := makeBlockID("blockhash2", 1000, "partshash")
  82. const chainID = "mychain"
  83. return &DuplicateVoteEvidence{
  84. VoteA: makeVote(val, chainID, 0, 10, 2, 1, blockID),
  85. VoteB: makeVote(val, chainID, 0, 10, 2, 1, blockID2),
  86. }
  87. }