-
Notifications
You must be signed in to change notification settings - Fork 592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: restructure timeout type #5404
Changes from all commits
ac8840b
7ec14cc
01cf9b6
86dc54f
adcc031
b6b65c1
5c261e2
638f967
30ac3b4
6c7c9f7
e50e3a4
98f34ea
63373d1
714b405
3924754
74ebc67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
package types | ||
|
||
import ( | ||
"time" | ||
|
||
errorsmod "cosmossdk.io/errors" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" | ||
) | ||
|
||
|
@@ -18,27 +14,47 @@ func NewTimeout(height clienttypes.Height, timestamp uint64) Timeout { | |
} | ||
} | ||
|
||
// IsValid returns true if either the height or timestamp is non-zero | ||
// IsValid returns true if either the height or timestamp is non-zero. | ||
func (t Timeout) IsValid() bool { | ||
return !t.Height.IsZero() || t.Timestamp != 0 | ||
} | ||
|
||
// TODO: Update after https://github.com/cosmos/ibc-go/issues/3483 has been resolved | ||
// HasPassed returns true if the upgrade has passed the timeout height or timestamp | ||
func (t Timeout) HasPassed(ctx sdk.Context) (bool, error) { | ||
if !t.IsValid() { | ||
return true, errorsmod.Wrap(ErrInvalidUpgrade, "upgrade timeout cannot be empty") | ||
} | ||
// Elapsed returns true if either the provided height or timestamp is past the | ||
// respective absolute timeout values. | ||
func (t Timeout) Elapsed(height clienttypes.Height, timestamp uint64) bool { | ||
return t.heightElapsed(height) || t.timestampElapsed(timestamp) | ||
} | ||
|
||
selfHeight, timeoutHeight := clienttypes.GetSelfHeight(ctx), t.Height | ||
if selfHeight.GTE(timeoutHeight) && timeoutHeight.GT(clienttypes.ZeroHeight()) { | ||
return true, errorsmod.Wrapf(ErrInvalidUpgrade, "block height >= upgrade timeout height (%s >= %s)", selfHeight, timeoutHeight) | ||
// ErrTimeoutElapsed returns a timeout elapsed error indicating which timeout value | ||
// has elapsed. | ||
func (t Timeout) ErrTimeoutElapsed(height clienttypes.Height, timestamp uint64) error { | ||
if t.heightElapsed(height) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. happy to add an additional statement if folks want to indicate both height and timestamp elapsed. I just opted for this for now |
||
return errorsmod.Wrapf(ErrTimeoutElapsed, "current height: %s, timeout height %s", height, t.Height) | ||
} | ||
|
||
selfTime, timeoutTimestamp := uint64(ctx.BlockTime().UnixNano()), t.Timestamp | ||
if selfTime >= timeoutTimestamp && timeoutTimestamp > 0 { | ||
return true, errorsmod.Wrapf(ErrInvalidUpgrade, "block timestamp >= upgrade timeout timestamp (%s >= %s)", ctx.BlockTime(), time.Unix(0, int64(timeoutTimestamp))) | ||
return errorsmod.Wrapf(ErrTimeoutElapsed, "current timestamp: %d, timeout timestamp %d", timestamp, t.Timestamp) | ||
} | ||
|
||
// ErrTimeoutNotReached returns a timeout not reached error indicating which timeout value | ||
// has not been reached. | ||
func (t Timeout) ErrTimeoutNotReached(height clienttypes.Height, timestamp uint64) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be |
||
// only return height information if the height is set | ||
// t.heightElapsed() will return false when it is empty | ||
if !t.Height.IsZero() && !t.heightElapsed(height) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this just
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No it is: When t.Height.IsZero(), the second statement returns !false. This caught me while writing tests as I originally just had I could pull out the !IsZero() checks out of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aha! indeed. These are slightly tricky to read, thanks for adding a comment here! |
||
return errorsmod.Wrapf(ErrTimeoutNotReached, "current height: %s, timeout height %s", height, t.Height) | ||
} | ||
|
||
return false, nil | ||
return errorsmod.Wrapf(ErrTimeoutNotReached, "current timestamp: %d, timeout timestamp %d", timestamp, t.Timestamp) | ||
} | ||
|
||
// heightElapsed returns true if the timeout height is non empty | ||
// and the timeout height is greater than or equal to the relative height. | ||
func (t Timeout) heightElapsed(height clienttypes.Height) bool { | ||
return !t.Height.IsZero() && height.GTE(t.Height) | ||
} | ||
|
||
// timestampElapsed returns true if the timeout timestamp is non empty | ||
// and the timeout timestamp is greater than or equal to the relative timestamp. | ||
func (t Timeout) timestampElapsed(timestamp uint64) bool { | ||
return t.Timestamp != 0 && timestamp >= t.Timestamp | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what I like about this now is that we have a generic timeout type rather than a timeout specific to channel upgrades