Skip to content

Commit

Permalink
chore: add seconds to nanos conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Oct 17, 2024
1 parent 378ced3 commit 990fa7e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
29 changes: 13 additions & 16 deletions modules/core/04-channel/v2/keeper/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func (k *Keeper) sendPacket(
return 0, "", err
}

if timeoutTimestamp < latestTimestamp {
return 0, "", errorsmod.Wrapf(channeltypes.ErrTimeoutElapsed, "latest timestamp: %d, timeout timestamp: %d", latestTimestamp, timeoutTimestamp)
timeout := channeltypesv2.TimeoutTimestampToNanos(timeoutTimestamp)
if timeout < latestTimestamp {
return 0, "", errorsmod.Wrapf(channeltypes.ErrTimeoutElapsed, "latest timestamp: %d, timeout timestamp: %d", latestTimestamp, packet.TimeoutTimestamp)
}

commitment := channeltypesv2.CommitPacket(packet)
Expand Down Expand Up @@ -98,9 +99,6 @@ func (k *Keeper) recvPacket(
proof []byte,
proofHeight exported.Height,
) error {
// Lookup channel associated with destination channel ID and ensure
// that the packet was indeed sent by our counterparty by verifying
// packet sender is our channel's counterparty channel id.
channel, ok := k.GetChannel(ctx, packet.DestinationChannel)
if !ok {
// TODO: figure out how aliasing will work when more than one packet data is sent.
Expand All @@ -109,15 +107,18 @@ func (k *Keeper) recvPacket(
return errorsmod.Wrap(types.ErrChannelNotFound, packet.DestinationChannel)
}
}

if channel.CounterpartyChannelId != packet.SourceChannel {
return channeltypes.ErrInvalidChannelIdentifier
}

clientID := channel.ClientId

// check if packet timed out by comparing it with the latest height of the chain
sdkCtx := sdk.UnwrapSDKContext(ctx)
currentTimestamp := uint64(sdkCtx.BlockTime().Unix())
if packet.GetTimeoutTimestamp() >= currentTimestamp {
timeout := channeltypesv2.TimeoutTimestampToNanos(packet.TimeoutTimestamp)
if timeout >= currentTimestamp {
return errorsmod.Wrapf(channeltypes.ErrTimeoutElapsed, "current timestamp: %d, timeout timestamp: %d", currentTimestamp, packet.GetTimeoutTimestamp())
}

Expand Down Expand Up @@ -160,8 +161,6 @@ func (k *Keeper) recvPacket(
}

func (k *Keeper) acknowledgePacket(ctx context.Context, packet channeltypesv2.Packet, acknowledgement channeltypesv2.Acknowledgement, proof []byte, proofHeight exported.Height) error {
// Lookup counterparty associated with our channel and ensure
// that the packet was indeed sent by our counterparty.
channel, ok := k.GetChannel(ctx, packet.SourceChannel)
if !ok {
return errorsmod.Wrap(types.ErrChannelNotFound, packet.SourceChannel)
Expand All @@ -170,6 +169,7 @@ func (k *Keeper) acknowledgePacket(ctx context.Context, packet channeltypesv2.Pa
if channel.CounterpartyChannelId != packet.DestinationChannel {
return channeltypes.ErrInvalidChannelIdentifier
}

clientID := channel.ClientId

commitment := k.GetPacketCommitment(ctx, packet.SourceChannel, packet.Sequence)
Expand Down Expand Up @@ -228,19 +228,15 @@ func (k *Keeper) timeoutPacket(
proof []byte,
proofHeight exported.Height,
) error {
// Lookup counterparty associated with our channel and ensure
// that the packet was indeed sent by our counterparty.
channel, ok := k.GetChannel(ctx, packet.SourceChannel)
if !ok {
// TODO: figure out how aliasing will work when more than one packet data is sent.
channel, ok = k.convertV1Channel(ctx, packet.Data[0].SourcePort, packet.SourceChannel)
if !ok {
return errorsmod.Wrap(types.ErrChannelNotFound, packet.DestinationChannel)
}
return errorsmod.Wrap(types.ErrChannelNotFound, packet.DestinationChannel)
}

if channel.CounterpartyChannelId != packet.DestinationChannel {
return channeltypes.ErrInvalidChannelIdentifier
}

clientID := channel.ClientId

// check that timeout height or timeout timestamp has passed on the other end
Expand All @@ -249,7 +245,8 @@ func (k *Keeper) timeoutPacket(
return err
}

if packet.GetTimeoutTimestamp() < proofTimestamp {
timeout := channeltypesv2.TimeoutTimestampToNanos(packet.TimeoutTimestamp)
if timeout < proofTimestamp {
return errorsmod.Wrapf(channeltypes.ErrTimeoutNotReached, "proof timestamp: %d, timeout timestamp: %d", proofTimestamp, packet.GetTimeoutTimestamp())
}

Expand Down
6 changes: 6 additions & 0 deletions modules/core/04-channel/v2/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"strings"
"time"

errorsmod "cosmossdk.io/errors"

Expand Down Expand Up @@ -84,3 +85,8 @@ func (p Payload) Validate() 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).Unix())
}

0 comments on commit 990fa7e

Please sign in to comment.