Skip to content
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

Add MaxTimeout condition #7521

Merged
merged 8 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions modules/core/04-channel/v2/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"slices"
"time"

errorsmod "cosmossdk.io/errors"

Expand Down Expand Up @@ -63,12 +64,25 @@ func (k *Keeper) RegisterCounterparty(goCtx context.Context, msg *channeltypesv2
// SendPacket defines a rpc handler method for MsgSendPacket.
func (k *Keeper) SendPacket(ctx context.Context, msg *channeltypesv2.MsgSendPacket) (*channeltypesv2.MsgSendPacketResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

sequence, destChannel, err := k.sendPacket(ctx, msg.SourceChannel, msg.TimeoutTimestamp, msg.Payloads)
if err != nil {
sdkCtx.Logger().Error("send packet failed", "source-channel", msg.SourceChannel, "error", errorsmod.Wrap(err, "send packet failed"))
return nil, errorsmod.Wrapf(err, "send packet failed for source id: %s", msg.SourceChannel)
}

// Note, the validate basic function in sendPacket does the timeoutTimestamp != 0 check and other stateless checks on the packet.
// timeoutTimestamp must be greater than current block time
timeout := time.Unix(int64(msg.TimeoutTimestamp), 0)
if timeout.Before(sdkCtx.BlockTime()) {
return nil, errorsmod.Wrap(channeltypesv2.ErrTimeoutTooLow, "timeout is less than the current block timestamp")
}

// timeoutTimestamp must be less than current block time + MaxTimeoutDelta
if timeout.After(sdkCtx.BlockTime().Add(channeltypesv2.MaxTimeoutDelta)) {
return nil, errorsmod.Wrap(channeltypesv2.ErrMaxTimeoutDeltaExceeded, "timeout exceeds the maximum expected value")
}

signer, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
sdkCtx.Logger().Error("send packet failed", "error", errorsmod.Wrap(err, "invalid address for msg Signer"))
Expand Down
26 changes: 26 additions & 0 deletions modules/core/04-channel/v2/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -116,6 +117,15 @@ func (suite *KeeperTestSuite) TestMsgSendPacket() {
malleate: func() {},
expError: nil,
},
{
name: "success: valid timeout timestamp",
malleate: func() {
// ensure a message timeout.
timeoutTimestamp = uint64(suite.chainA.GetContext().BlockTime().Add(channeltypesv2.MaxTimeoutDelta - 10*time.Second).Unix())
expectedPacket = channeltypesv2.NewPacket(1, path.EndpointA.ChannelID, path.EndpointB.ChannelID, timeoutTimestamp, payload)
},
expError: nil,
},
{
name: "failure: timeout elapsed",
malleate: func() {
Expand All @@ -124,6 +134,22 @@ func (suite *KeeperTestSuite) TestMsgSendPacket() {
},
expError: channeltypesv1.ErrTimeoutElapsed,
},
{
name: "failure: timeout timestamp exceeds max allowed input",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add an error case for when its too low

malleate: func() {
// ensure message timeout exceeds max allowed input.
timeoutTimestamp = uint64(suite.chainA.GetContext().BlockTime().Add(channeltypesv2.MaxTimeoutDelta + 10*time.Second).Unix())
},
expError: channeltypesv2.ErrMaxTimeoutDeltaExceeded,
},
{
name: "failure: timeout timestamp less than current block timestamp",
malleate: func() {
// ensure message timeout exceeds max allowed input.
timeoutTimestamp = uint64(suite.chainA.GetContext().BlockTime().Unix()) - 1
},
expError: channeltypesv2.ErrTimeoutTooLow,
},
{
name: "failure: inactive client",
malleate: func() {
Expand Down
2 changes: 2 additions & 0 deletions modules/core/04-channel/v2/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ var (
ErrInvalidAcknowledgement = errorsmod.Register(SubModuleName, 7, "invalid acknowledgement")
ErrPacketCommitmentNotFound = errorsmod.Register(SubModuleName, 8, "packet commitment not found")
ErrAcknowledgementNotFound = errorsmod.Register(SubModuleName, 9, "packet acknowledgement not found")
ErrMaxTimeoutDeltaExceeded = errorsmod.Register(SubModuleName, 10, "timeoutTimestamp exceeds max allowed value")
ErrTimeoutTooLow = errorsmod.Register(SubModuleName, 11, "timeoutTimestamp is less than current block timestamp")
)
4 changes: 4 additions & 0 deletions modules/core/04-channel/v2/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"time"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -13,6 +15,8 @@ import (
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
)

const MaxTimeoutDelta time.Duration = 24 * time.Hour

var (
_ sdk.Msg = (*MsgCreateChannel)(nil)
_ sdk.HasValidateBasic = (*MsgCreateChannel)(nil)
Expand Down
Loading