Skip to content

Commit

Permalink
Compare Timeout timestamps in seconds instead of nanoseconds (#7857)
Browse files Browse the repository at this point in the history
* compare seconds instead of nanoseconds

* remove unnecessary helper func

* improve naming
  • Loading branch information
AdityaSripal authored Jan 20, 2025
1 parent fb275ab commit 3fefb28
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
27 changes: 16 additions & 11 deletions modules/core/04-channel/v2/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"strconv"
"time"

errorsmod "cosmossdk.io/errors"

Expand Down Expand Up @@ -53,16 +54,17 @@ func (k *Keeper) sendPacket(
return 0, "", errorsmod.Wrapf(clienttypes.ErrInvalidHeight, "cannot send packet using client (%s) with zero height", sourceClient)
}

latestTimestamp, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, sourceClient, latestHeight)
// 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
latestTimestampNano, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, sourceClient, latestHeight)
if err != nil {
return 0, "", err
}
latestTimestamp := uint64(time.Unix(0, int64(latestTimestampNano)).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 {
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)
Expand Down Expand Up @@ -264,15 +266,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
proofTimestamp, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, packet.SourceClient, proofHeight)
// 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
proofTimestampNano, err := k.ClientKeeper.GetClientTimestampAtHeight(ctx, packet.SourceClient, proofHeight)
if err != nil {
return err
}
proofTimestamp := uint64(time.Unix(0, int64(proofTimestampNano)).Unix())

timeoutTimestamp := types.TimeoutTimestampToNanos(packet.TimeoutTimestamp)
if proofTimestamp < timeoutTimestamp {
return errorsmod.Wrapf(types.ErrTimeoutNotReached, "proof timestamp: %d, timeout timestamp: %d", proofTimestamp, timeoutTimestamp)
if proofTimestamp < 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
Expand Down
6 changes: 0 additions & 6 deletions modules/core/04-channel/v2/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"strings"
"time"

errorsmod "cosmossdk.io/errors"

Expand Down Expand Up @@ -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())
}

0 comments on commit 3fefb28

Please sign in to comment.