Skip to content
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
6 changes: 6 additions & 0 deletions modules/core/04-channel/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)

const MaximumPayloadsSize = 262144 // 256 KiB. This is the maximum size of all payloads combined

// CommitPacket returns the packet commitment bytes. The commitment consists of:
// sha256_hash(timeout_timestamp + timeout_height.RevisionNumber + timeout_height.RevisionHeight + sha256_hash(data))
// from a given packet. This results in a fixed length preimage.
Expand Down Expand Up @@ -110,6 +112,10 @@ func (p Packet) ValidateBasic() error {
return errorsmod.Wrap(ErrInvalidPacket, "packet data bytes cannot be empty")
}

if len(p.Data) > MaximumPayloadsSize {
return errorsmod.Wrapf(ErrInvalidPacket, "packet data bytes cannot exceed %d bytes", MaximumPayloadsSize)
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions modules/core/04-channel/types/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestPacketValidateBasic(t *testing.T) {
{types.NewPacket(validPacketData, 1, portid, chanid, invalidPort, cpchanid, timeoutHeight, timeoutTimestamp), errors.New("invalid destination port")},
{types.NewPacket(validPacketData, 1, portid, chanid, cpportid, invalidChannel, timeoutHeight, timeoutTimestamp), errors.New("invalid destination channel")},
{types.NewPacket(validPacketData, 1, portid, chanid, cpportid, cpchanid, disabledTimeout, 0), errors.New("packet timeout height and packet timeout timestamp cannot both be 0: invalid packet")},
{types.NewPacket(make([]byte, types.MaximumPayloadsSize+1), 1, portid, chanid, cpportid, cpchanid, timeoutHeight, timeoutTimestamp), errors.New("packet data bytes cannot exceed 262144 bytes: invalid packet")},
}

for _, tc := range testCases {
Expand Down
7 changes: 7 additions & 0 deletions modules/core/04-channel/v2/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

errorsmod "cosmossdk.io/errors"

channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
)

Expand Down Expand Up @@ -36,10 +37,16 @@ func (p Packet) ValidateBasic() error {
return errorsmod.Wrap(ErrInvalidPacket, "payloads must contain exactly one payload")
}

totalPayloadsSize := 0
for _, pd := range p.Payloads {
if err := pd.ValidateBasic(); err != nil {
return errorsmod.Wrap(err, "invalid Payload")
}
totalPayloadsSize += len(pd.Value)
}

if totalPayloadsSize > channeltypesv1.MaximumPayloadsSize {
return errorsmod.Wrapf(ErrInvalidPacket, "packet data bytes cannot exceed %d bytes", channeltypesv1.MaximumPayloadsSize)
}

if err := host.ChannelIdentifierValidator(p.SourceClient); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions modules/core/04-channel/v2/types/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"

transfertypes "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
channeltypesv1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
Expand All @@ -26,6 +27,22 @@ func TestValidateBasic(t *testing.T) {
func() {},
nil,
},
{
"success, single payload just below MaxPayloadsSize",
func() {
packet.Payloads[0].Value = make([]byte, channeltypesv1.MaximumPayloadsSize-1)
},
nil,
},
{
"failure: invalid single payloads size",
func() {
// bytes that are larger than MaxPayloadsSize
packet.Payloads[0].Value = make([]byte, channeltypesv1.MaximumPayloadsSize+1)
},
types.ErrInvalidPacket,
},
// TODO: add test cases for multiple payloads when enabled (#7008)
{
"failure: payloads is nil",
func() {
Expand Down
Loading