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.

40 lines
1.2 KiB

  1. package types
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tendermint/tendermint/crypto/tmhash"
  6. tmtime "github.com/tendermint/tendermint/types/time"
  7. )
  8. // ValidateTime does a basic time validation ensuring time does not drift too
  9. // much: +/- one year.
  10. // TODO: reduce this to eg 1 day
  11. // NOTE: DO NOT USE in ValidateBasic methods in this package. This function
  12. // can only be used for real time validation, like on proposals and votes
  13. // in the consensus. If consensus is stuck, and rounds increase for more than a day,
  14. // having only a 1-day band here could break things...
  15. // Can't use for validating blocks because we may be syncing years worth of history.
  16. func ValidateTime(t time.Time) error {
  17. var (
  18. now = tmtime.Now()
  19. oneYear = 8766 * time.Hour
  20. )
  21. if t.Before(now.Add(-oneYear)) || t.After(now.Add(oneYear)) {
  22. return fmt.Errorf("Time drifted too much. Expected: -1 < %v < 1 year", now)
  23. }
  24. return nil
  25. }
  26. // ValidateHash returns an error if the hash is not empty, but its
  27. // size != tmhash.Size.
  28. func ValidateHash(h []byte) error {
  29. if len(h) > 0 && len(h) != tmhash.Size {
  30. return fmt.Errorf("Expected size to be %d bytes, got %d bytes",
  31. tmhash.Size,
  32. len(h),
  33. )
  34. }
  35. return nil
  36. }