8058 fix zero time checks (#8282)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Robert Zaremba 2021-01-09 13:08:46 +01:00 committed by GitHub
parent 5113effd24
commit 2c611b5706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 17 deletions

View File

@ -91,7 +91,7 @@ func (e Equivocation) Hash() tmbytes.HexBytes {
// ValidateBasic performs basic stateless validation checks on an Equivocation object.
func (e Equivocation) ValidateBasic() error {
if e.Time.IsZero() {
if e.Time.Unix() <= 0 {
return fmt.Errorf("invalid equivocation time: %s", e.Time)
}
if e.Height < 1 {

View File

@ -43,7 +43,7 @@ func (e *Equivocation) Hash() tmbytes.HexBytes {
// ValidateBasic performs basic stateless validation checks on an Equivocation object.
func (e *Equivocation) ValidateBasic() error {
if e.Time.IsZero() {
if e.Time.Unix() <= 0 {
return fmt.Errorf("invalid equivocation time: %s", e.Time)
}
if e.Height < 1 {

View File

@ -48,11 +48,8 @@ func (cs ConsensusState) ValidateBasic() error {
if err := tmtypes.ValidateHash(cs.NextValidatorsHash); err != nil {
return sdkerrors.Wrap(err, "next validators hash is invalid")
}
if cs.Timestamp.IsZero() {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "timestamp cannot be zero Unix time")
}
if cs.Timestamp.UnixNano() < 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "timestamp cannot be negative Unix time")
if cs.Timestamp.Unix() <= 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "timestamp must be a positive Unix time")
}
return nil
}

View File

@ -60,7 +60,7 @@ func (k Keeper) ScheduleUpgrade(ctx sdk.Context, plan types.Plan) error {
return err
}
if !plan.Time.IsZero() {
if plan.Time.Unix() > 0 {
if !plan.Time.After(ctx.BlockHeader().Time) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "upgrade cannot be scheduled in the past")
}

View File

@ -64,10 +64,11 @@ func (p Plan) ValidateBasic() error {
if p.Height < 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "height cannot be negative")
}
if p.Time.IsZero() && p.Height == 0 {
isValidTime := p.Time.Unix() > 0
if !isValidTime && p.Height == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "must set either time or height")
}
if !p.Time.IsZero() && p.Height != 0 {
if isValidTime && p.Height != 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "cannot set both time and height")
}
@ -76,7 +77,7 @@ func (p Plan) ValidateBasic() error {
// ShouldExecute returns true if the Plan is ready to execute given the current context
func (p Plan) ShouldExecute(ctx sdk.Context) bool {
if !p.Time.IsZero() {
if p.Time.Unix() > 0 {
return !ctx.BlockTime().Before(p.Time)
}
if p.Height > 0 {
@ -87,7 +88,7 @@ func (p Plan) ShouldExecute(ctx sdk.Context) bool {
// DueAt is a string representation of when this plan is due to be executed
func (p Plan) DueAt() string {
if !p.Time.IsZero() {
if p.Time.Unix() > 0 {
return fmt.Sprintf("time: %s", p.Time.UTC().Format(time.RFC3339))
}
return fmt.Sprintf("height: %d", p.Height)

View File

@ -39,13 +39,13 @@ func (p Plan) ValidateBasic() error {
if p.Height < 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "height cannot be negative")
}
if p.Time.IsZero() && p.Height == 0 {
if p.Time.Unix() <= 0 && p.Height == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "must set either time or height")
}
if !p.Time.IsZero() && p.Height != 0 {
if p.Time.Unix() > 0 && p.Height != 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "cannot set both time and height")
}
if !p.Time.IsZero() && p.UpgradedClientState != nil {
if p.Time.Unix() > 0 && p.UpgradedClientState != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "IBC chain upgrades must only set height")
}
@ -54,7 +54,7 @@ func (p Plan) ValidateBasic() error {
// ShouldExecute returns true if the Plan is ready to execute given the current context
func (p Plan) ShouldExecute(ctx sdk.Context) bool {
if !p.Time.IsZero() {
if p.Time.Unix() > 0 {
return !ctx.BlockTime().Before(p.Time)
}
if p.Height > 0 {
@ -65,7 +65,7 @@ func (p Plan) ShouldExecute(ctx sdk.Context) bool {
// DueAt is a string representation of when this plan is due to be executed
func (p Plan) DueAt() string {
if !p.Time.IsZero() {
if p.Time.Unix() > 0 {
return fmt.Sprintf("time: %s", p.Time.UTC().Format(time.RFC3339))
}
return fmt.Sprintf("height: %d", p.Height)