github.com/soomindae/tendermint@v0.0.5-0.20210528140126-84a0c70c8162/types/validation.go (about)

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