From 5c9cd9a24839af06180bc0ae8b9e18d50a880db0 Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:51:05 +0100 Subject: [PATCH 1/3] compare seconds instead of nanoseconds --- modules/core/04-channel/v2/keeper/packet.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/modules/core/04-channel/v2/keeper/packet.go b/modules/core/04-channel/v2/keeper/packet.go index ce00443cfe9..654bc678a74 100644 --- a/modules/core/04-channel/v2/keeper/packet.go +++ b/modules/core/04-channel/v2/keeper/packet.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "strconv" + "time" errorsmod "cosmossdk.io/errors" @@ -53,15 +54,17 @@ func (k *Keeper) sendPacket( return 0, "", errorsmod.Wrapf(clienttypes.ErrInvalidHeight, "cannot send packet using client (%s) with zero height", sourceClient) } + // client timestamps are in nanoseconds while packet timeouts are in seconds + // thus to compare them, we convert the client timestamp to seconds in uint64 + // to be consistent with IBC V2 specified timeout behaviour latestTimestamp, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, sourceClient, latestHeight) if err != nil { return 0, "", err } + latestTimestampSecs := uint64(time.Unix(0, int64(latestTimestamp)).Unix()) - // client timestamps are in nanoseconds while packet timeouts are in seconds - // thus to compare them, we convert the packet timeout to nanoseconds timeoutTimestamp = types.TimeoutTimestampToNanos(packet.TimeoutTimestamp) - if latestTimestamp >= timeoutTimestamp { + if latestTimestampSecs >= packet.TimeoutTimestamp { return 0, "", errorsmod.Wrapf(types.ErrTimeoutElapsed, "latest timestamp: %d, timeout timestamp: %d", latestTimestamp, timeoutTimestamp) } @@ -264,15 +267,18 @@ func (k *Keeper) timeoutPacket( return errorsmod.Wrapf(clienttypes.ErrInvalidCounterparty, "counterparty id (%s) does not match packet destination id (%s)", counterparty.ClientId, packet.DestinationClient) } - // check that timeout height or timeout timestamp has passed on the other end + // check that timeout timestamp has passed on the other end + // client timestamps are in nanoseconds while packet timeouts are in seconds + // so we convert client timestamp to seconds in uint64 to be consistent + // with IBC V2 timeout behaviour proofTimestamp, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, packet.SourceClient, proofHeight) if err != nil { return err } + proofTimestampSecs := uint64(time.Unix(0, int64(proofTimestamp)).Unix()) - timeoutTimestamp := types.TimeoutTimestampToNanos(packet.TimeoutTimestamp) - if proofTimestamp < timeoutTimestamp { - return errorsmod.Wrapf(types.ErrTimeoutNotReached, "proof timestamp: %d, timeout timestamp: %d", proofTimestamp, timeoutTimestamp) + if proofTimestampSecs < packet.TimeoutTimestamp { + return errorsmod.Wrapf(types.ErrTimeoutNotReached, "proof timestamp: %d, timeout timestamp: %d", proofTimestamp, packet.TimeoutTimestamp) } // check that the commitment has not been cleared and that it matches the packet sent by relayer From 0fa9597fd627c868749fe4f0683058d3c7f48d31 Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Mon, 20 Jan 2025 13:52:46 +0100 Subject: [PATCH 2/3] remove unnecessary helper func --- modules/core/04-channel/v2/types/packet.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/modules/core/04-channel/v2/types/packet.go b/modules/core/04-channel/v2/types/packet.go index e78a598fa6e..21525033f2a 100644 --- a/modules/core/04-channel/v2/types/packet.go +++ b/modules/core/04-channel/v2/types/packet.go @@ -2,7 +2,6 @@ package types import ( "strings" - "time" errorsmod "cosmossdk.io/errors" @@ -79,8 +78,3 @@ func (p Payload) ValidateBasic() error { } return nil } - -// TimeoutTimestampToNanos takes seconds as a uint64 and returns Unix nanoseconds as uint64. -func TimeoutTimestampToNanos(seconds uint64) uint64 { - return uint64(time.Unix(int64(seconds), 0).UnixNano()) -} From de31736bfd2ca9994a1ac9986456e53bcd586f4b Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Mon, 20 Jan 2025 14:33:41 +0100 Subject: [PATCH 3/3] improve naming --- modules/core/04-channel/v2/keeper/packet.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/core/04-channel/v2/keeper/packet.go b/modules/core/04-channel/v2/keeper/packet.go index 654bc678a74..a0254d9ca64 100644 --- a/modules/core/04-channel/v2/keeper/packet.go +++ b/modules/core/04-channel/v2/keeper/packet.go @@ -57,15 +57,14 @@ func (k *Keeper) sendPacket( // client timestamps are in nanoseconds while packet timeouts are in seconds // thus to compare them, we convert the client timestamp to seconds in uint64 // to be consistent with IBC V2 specified timeout behaviour - latestTimestamp, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, sourceClient, latestHeight) + latestTimestampNano, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, sourceClient, latestHeight) if err != nil { return 0, "", err } - latestTimestampSecs := uint64(time.Unix(0, int64(latestTimestamp)).Unix()) + latestTimestamp := uint64(time.Unix(0, int64(latestTimestampNano)).Unix()) - timeoutTimestamp = types.TimeoutTimestampToNanos(packet.TimeoutTimestamp) - if latestTimestampSecs >= packet.TimeoutTimestamp { - return 0, "", errorsmod.Wrapf(types.ErrTimeoutElapsed, "latest timestamp: %d, timeout timestamp: %d", latestTimestamp, timeoutTimestamp) + if latestTimestamp >= packet.TimeoutTimestamp { + return 0, "", errorsmod.Wrapf(types.ErrTimeoutElapsed, "latest timestamp: %d, timeout timestamp: %d", latestTimestamp, packet.TimeoutTimestamp) } commitment := types.CommitPacket(packet) @@ -271,13 +270,13 @@ func (k *Keeper) timeoutPacket( // client timestamps are in nanoseconds while packet timeouts are in seconds // so we convert client timestamp to seconds in uint64 to be consistent // with IBC V2 timeout behaviour - proofTimestamp, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, packet.SourceClient, proofHeight) + proofTimestampNano, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, packet.SourceClient, proofHeight) if err != nil { return err } - proofTimestampSecs := uint64(time.Unix(0, int64(proofTimestamp)).Unix()) + proofTimestamp := uint64(time.Unix(0, int64(proofTimestampNano)).Unix()) - if proofTimestampSecs < packet.TimeoutTimestamp { + if proofTimestamp < packet.TimeoutTimestamp { return errorsmod.Wrapf(types.ErrTimeoutNotReached, "proof timestamp: %d, timeout timestamp: %d", proofTimestamp, packet.TimeoutTimestamp) }