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.

48 lines
1.1 KiB

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