Skip to content

Commit

Permalink
x/ibc/light-clients: simplify max time comparison (cosmos#8089)
Browse files Browse the repository at this point in the history
The prior code that tried to get the newest/larger time
between the too performed:
* 2 time.Time.UnixNano() retrievals: 2 multiplications, 2 additions
* 1 math.Max comparison
* 1 time.Unix(0, minTime) creation

but really this code could just use native operations on:
time.Time.After() simply.

This now becomes a single operation, single comparison for the same effect.
  • Loading branch information
odeke-em authored Dec 8, 2020
1 parent b219c54 commit e38a6df
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions x/ibc/light-clients/07-tendermint/types/misbehaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"bytes"
"math"
"time"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down Expand Up @@ -48,8 +47,11 @@ func (misbehaviour Misbehaviour) GetHeight() exported.Height {
// maximum value from both headers to prevent producing an invalid header outside
// of the misbehaviour age range.
func (misbehaviour Misbehaviour) GetTime() time.Time {
minTime := int64(math.Max(float64(misbehaviour.Header1.GetTime().UnixNano()), float64(misbehaviour.Header2.GetTime().UnixNano())))
return time.Unix(0, minTime)
t1, t2 := misbehaviour.Header1.GetTime(), misbehaviour.Header2.GetTime()
if t1.After(t2) {
return t1
}
return t2
}

// ValidateBasic implements Misbehaviour interface
Expand Down

0 comments on commit e38a6df

Please sign in to comment.