Skip to content

Commit

Permalink
8058 fix zero time checks (#8282)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
robert-zaremba and mergify[bot] committed Jan 18, 2021
1 parent f999126 commit d90c498
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion x/evidence/legacy/v038/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion x/evidence/types/evidence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions x/ibc/light-clients/07-tendermint/types/consensus_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion x/upgrade/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
9 changes: 5 additions & 4 deletions x/upgrade/legacy/v038/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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 {
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions x/upgrade/types/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit d90c498

Please sign in to comment.