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.

114 lines
3.4 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/tendermint/go-crypto"
  6. "github.com/tendermint/tmlibs/merkle"
  7. )
  8. // ErrEvidenceInvalid wraps a piece of evidence and the error denoting how or why it is invalid.
  9. type ErrEvidenceInvalid struct {
  10. Evidence Evidence
  11. ErrorValue error
  12. }
  13. func NewEvidenceInvalidErr(ev Evidence, err error) *ErrEvidenceInvalid {
  14. return &ErrEvidenceInvalid{ev, err}
  15. }
  16. // Error returns a string representation of the error.
  17. func (err *ErrEvidenceInvalid) Error() string {
  18. return fmt.Sprintf("Invalid evidence: %v. Evidence: %v", err.ErrorValue, err.Evidence)
  19. }
  20. //-------------------------------------------
  21. // Evidence represents any provable malicious activity by a validator
  22. type Evidence interface {
  23. Address() []byte
  24. Hash() []byte
  25. Verify(chainID string) error
  26. String() string
  27. }
  28. //-------------------------------------------
  29. type Evidences []Evidence
  30. func (evs Evidences) Hash() []byte {
  31. // Recursive impl.
  32. // Copied from tmlibs/merkle to avoid allocations
  33. switch len(evs) {
  34. case 0:
  35. return nil
  36. case 1:
  37. return evs[0].Hash()
  38. default:
  39. left := Evidences(evs[:(len(evs)+1)/2]).Hash()
  40. right := Evidences(evs[(len(evs)+1)/2:]).Hash()
  41. return merkle.SimpleHashFromTwoHashes(left, right)
  42. }
  43. }
  44. //-------------------------------------------
  45. // DuplicateVoteEvidence contains evidence a validator signed two conflicting votes.
  46. type DuplicateVoteEvidence struct {
  47. PubKey crypto.PubKey
  48. VoteA *Vote
  49. VoteB *Vote
  50. }
  51. // String returns a string representation of the evidence.
  52. func (dve *DuplicateVoteEvidence) String() string {
  53. return fmt.Sprintf("VoteA: %v; VoteB: %v", dve.VoteA, dve.VoteB)
  54. }
  55. // Address returns the address of the validator.
  56. func (dve *DuplicateVoteEvidence) Address() []byte {
  57. return dve.PubKey.Address()
  58. }
  59. // Hash returns the hash of the evidence.
  60. func (dve *DuplicateVoteEvidence) Hash() []byte {
  61. return merkle.SimpleHashFromBinary(dve)
  62. }
  63. // Verify returns an error if the two votes aren't conflicting.
  64. // To be conflicting, they must be from the same validator, for the same H/R/S, but for different blocks.
  65. func (dve *DuplicateVoteEvidence) Verify(chainID string) error {
  66. // H/R/S must be the same
  67. if dve.VoteA.Height != dve.VoteB.Height ||
  68. dve.VoteA.Round != dve.VoteB.Round ||
  69. dve.VoteA.Type != dve.VoteB.Type {
  70. return fmt.Errorf("DuplicateVoteEvidence Error: H/R/S does not match. Got %v and %v", dve.VoteA, dve.VoteB)
  71. }
  72. // Address must be the same
  73. if !bytes.Equal(dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress) {
  74. return fmt.Errorf("DuplicateVoteEvidence Error: Validator addresses do not match. Got %X and %X", dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress)
  75. }
  76. // XXX: Should we enforce index is the same ?
  77. if dve.VoteA.ValidatorIndex != dve.VoteB.ValidatorIndex {
  78. return fmt.Errorf("DuplicateVoteEvidence Error: Validator indices do not match. Got %d and %d", dve.VoteA.ValidatorIndex, dve.VoteB.ValidatorIndex)
  79. }
  80. // BlockIDs must be different
  81. if dve.VoteA.BlockID.Equals(dve.VoteB.BlockID) {
  82. return fmt.Errorf("DuplicateVoteEvidence Error: BlockIDs are the same (%v) - not a real duplicate vote!", dve.VoteA.BlockID)
  83. }
  84. // Signatures must be valid
  85. if !dve.PubKey.VerifyBytes(SignBytes(chainID, dve.VoteA), dve.VoteA.Signature) {
  86. return ErrVoteInvalidSignature
  87. }
  88. if !dve.PubKey.VerifyBytes(SignBytes(chainID, dve.VoteB), dve.VoteB.Signature) {
  89. return ErrVoteInvalidSignature
  90. }
  91. return nil
  92. }