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.

160 lines
4.7 KiB

7 years ago
7 years ago
  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. Height() int // height of the equivocation
  24. Address() []byte // address of the equivocating validator
  25. Index() int // index of the validator in the validator set
  26. Hash() []byte // hash of the evidence
  27. Verify(chainID string) error // verify the evidence
  28. Equal(Evidence) bool // check equality of evidence
  29. String() string
  30. }
  31. //-------------------------------------------
  32. // EvidenceList is a list of Evidence. Evidences is not a word.
  33. type EvidenceList []Evidence
  34. // Hash returns the simple merkle root hash of the EvidenceList.
  35. func (evl EvidenceList) Hash() []byte {
  36. // Recursive impl.
  37. // Copied from tmlibs/merkle to avoid allocations
  38. switch len(evl) {
  39. case 0:
  40. return nil
  41. case 1:
  42. return evl[0].Hash()
  43. default:
  44. left := EvidenceList(evl[:(len(evl)+1)/2]).Hash()
  45. right := EvidenceList(evl[(len(evl)+1)/2:]).Hash()
  46. return merkle.SimpleHashFromTwoHashes(left, right)
  47. }
  48. }
  49. func (evl EvidenceList) String() string {
  50. s := ""
  51. for _, e := range evl {
  52. s += fmt.Sprintf("%s\t\t", e)
  53. }
  54. return s
  55. }
  56. // Has returns true if the evidence is in the EvidenceList.
  57. func (evl EvidenceList) Has(evidence Evidence) bool {
  58. for _, ev := range evl {
  59. if ev.Equal(evidence) {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. //-------------------------------------------
  66. // DuplicateVoteEvidence contains evidence a validator signed two conflicting votes.
  67. type DuplicateVoteEvidence struct {
  68. PubKey crypto.PubKey
  69. VoteA *Vote
  70. VoteB *Vote
  71. }
  72. // String returns a string representation of the evidence.
  73. func (dve *DuplicateVoteEvidence) String() string {
  74. return fmt.Sprintf("VoteA: %v; VoteB: %v", dve.VoteA, dve.VoteB)
  75. }
  76. // Height returns the height this evidence refers to.
  77. func (dve *DuplicateVoteEvidence) Height() int {
  78. return dve.VoteA.Height
  79. }
  80. // Address returns the address of the validator.
  81. func (dve *DuplicateVoteEvidence) Address() []byte {
  82. return dve.PubKey.Address()
  83. }
  84. // Index returns the index of the validator.
  85. func (dve *DuplicateVoteEvidence) Index() int {
  86. return dve.VoteA.ValidatorIndex
  87. }
  88. // Hash returns the hash of the evidence.
  89. func (dve *DuplicateVoteEvidence) Hash() []byte {
  90. return merkle.SimpleHashFromBinary(dve)
  91. }
  92. // Verify returns an error if the two votes aren't conflicting.
  93. // To be conflicting, they must be from the same validator, for the same H/R/S, but for different blocks.
  94. func (dve *DuplicateVoteEvidence) Verify(chainID string) error {
  95. // TODO: verify (cs.Height - dve.Height) < MaxHeightDiff
  96. // H/R/S must be the same
  97. if dve.VoteA.Height != dve.VoteB.Height ||
  98. dve.VoteA.Round != dve.VoteB.Round ||
  99. dve.VoteA.Type != dve.VoteB.Type {
  100. return fmt.Errorf("DuplicateVoteEvidence Error: H/R/S does not match. Got %v and %v", dve.VoteA, dve.VoteB)
  101. }
  102. // Address must be the same
  103. if !bytes.Equal(dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress) {
  104. return fmt.Errorf("DuplicateVoteEvidence Error: Validator addresses do not match. Got %X and %X", dve.VoteA.ValidatorAddress, dve.VoteB.ValidatorAddress)
  105. }
  106. // XXX: Should we enforce index is the same ?
  107. if dve.VoteA.ValidatorIndex != dve.VoteB.ValidatorIndex {
  108. return fmt.Errorf("DuplicateVoteEvidence Error: Validator indices do not match. Got %d and %d", dve.VoteA.ValidatorIndex, dve.VoteB.ValidatorIndex)
  109. }
  110. // BlockIDs must be different
  111. if dve.VoteA.BlockID.Equals(dve.VoteB.BlockID) {
  112. return fmt.Errorf("DuplicateVoteEvidence Error: BlockIDs are the same (%v) - not a real duplicate vote!", dve.VoteA.BlockID)
  113. }
  114. // Signatures must be valid
  115. if !dve.PubKey.VerifyBytes(SignBytes(chainID, dve.VoteA), dve.VoteA.Signature) {
  116. return ErrVoteInvalidSignature
  117. }
  118. if !dve.PubKey.VerifyBytes(SignBytes(chainID, dve.VoteB), dve.VoteB.Signature) {
  119. return ErrVoteInvalidSignature
  120. }
  121. return nil
  122. }
  123. // Equal checks if two pieces of evidence are equal.
  124. func (dve *DuplicateVoteEvidence) Equal(ev Evidence) bool {
  125. if _, ok := ev.(*DuplicateVoteEvidence); !ok {
  126. return false
  127. }
  128. // just check their hashes
  129. return bytes.Equal(merkle.SimpleHashFromBinary(dve), merkle.SimpleHashFromBinary(ev))
  130. }