github.com/Finschia/ostracon@v1.1.5/types/validation.go (about)

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