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.

79 lines
2.4 KiB

  1. package evidence
  2. import (
  3. "fmt"
  4. sm "github.com/tendermint/tendermint/state"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. // VerifyEvidence verifies the evidence fully by checking:
  8. // - it is sufficiently recent (MaxAge)
  9. // - it is from a key who was a validator at the given height
  10. // - it is internally consistent
  11. // - it was properly signed by the alleged equivocator
  12. func VerifyEvidence(evidence types.Evidence, state sm.State, stateDB sm.Store, blockStore BlockStore) error {
  13. var (
  14. height = state.LastBlockHeight
  15. evidenceParams = state.ConsensusParams.Evidence
  16. ageDuration = state.LastBlockTime.Sub(evidence.Time())
  17. ageNumBlocks = height - evidence.Height()
  18. header *types.Header
  19. )
  20. // if the evidence is from the current height - this means the evidence is fresh from the consensus
  21. // and we won't have it in the block store. We thus check that the time isn't before the previous block
  22. if evidence.Height() == height+1 {
  23. if evidence.Time().Before(state.LastBlockTime) {
  24. return fmt.Errorf("evidence is from an earlier time than the previous block: %v < %v",
  25. evidence.Time(),
  26. state.LastBlockTime)
  27. }
  28. } else {
  29. // try to retrieve header from blockstore
  30. blockMeta := blockStore.LoadBlockMeta(evidence.Height())
  31. header = &blockMeta.Header
  32. if header == nil {
  33. return fmt.Errorf("don't have header at height #%d", evidence.Height())
  34. }
  35. if header.Time != evidence.Time() {
  36. return fmt.Errorf("evidence time (%v) is different to the time of the header we have for the same height (%v)",
  37. evidence.Time(),
  38. header.Time,
  39. )
  40. }
  41. }
  42. if ageDuration > evidenceParams.MaxAgeDuration && ageNumBlocks > evidenceParams.MaxAgeNumBlocks {
  43. return fmt.Errorf(
  44. "evidence from height %d (created at: %v) is too old; min height is %d and evidence can not be older than %v",
  45. evidence.Height(),
  46. evidence.Time(),
  47. height-evidenceParams.MaxAgeNumBlocks,
  48. state.LastBlockTime.Add(evidenceParams.MaxAgeDuration),
  49. )
  50. }
  51. valset, err := stateDB.LoadValidators(evidence.Height())
  52. if err != nil {
  53. return err
  54. }
  55. addr := evidence.Address()
  56. var val *types.Validator
  57. // For all other types, expect evidence.Address to be a validator at height
  58. // evidence.Height.
  59. _, val = valset.GetByAddress(addr)
  60. if val == nil {
  61. return fmt.Errorf("address %X was not a validator at height %d", addr, evidence.Height())
  62. }
  63. if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
  64. return err
  65. }
  66. return nil
  67. }