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.

49 lines
1.2 KiB

  1. package proxy
  2. import (
  3. "bytes"
  4. "github.com/pkg/errors"
  5. "github.com/tendermint/tendermint/lite"
  6. certerr "github.com/tendermint/tendermint/lite/errors"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. func ValidateBlockMeta(meta *types.BlockMeta, check lite.Commit) error {
  10. if meta == nil {
  11. return errors.New("expecting a non-nil BlockMeta")
  12. }
  13. // TODO: check the BlockID??
  14. return ValidateHeader(&meta.Header, check)
  15. }
  16. func ValidateBlock(meta *types.Block, check lite.Commit) error {
  17. if meta == nil {
  18. return errors.New("expecting a non-nil Block")
  19. }
  20. err := ValidateHeader(&meta.Header, check)
  21. if err != nil {
  22. return err
  23. }
  24. if !bytes.Equal(meta.Data.Hash(), meta.Header.DataHash) {
  25. return errors.New("Data hash doesn't match header")
  26. }
  27. return nil
  28. }
  29. func ValidateHeader(head *types.Header, check lite.Commit) error {
  30. if head == nil {
  31. return errors.New("expecting a non-nil Header")
  32. }
  33. // make sure they are for the same height (obvious fail)
  34. if head.Height != check.Height() {
  35. return certerr.ErrHeightMismatch(head.Height, check.Height())
  36. }
  37. // check if they are equal by using hashes
  38. chead := check.Header
  39. if !bytes.Equal(head.Hash(), chead.Hash()) {
  40. return errors.New("Headers don't match")
  41. }
  42. return nil
  43. }