Skip to content

Commit

Permalink
chore: require app version be non-empty when protocol version is vers…
Browse files Browse the repository at this point in the history
…ion 2.
  • Loading branch information
DimitrisJim committed Aug 26, 2024
1 parent f4783d1 commit 6d7d618
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 8 additions & 5 deletions modules/core/04-channel/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"crypto/sha256"
"slices"
"strings"

errorsmod "cosmossdk.io/errors"

Expand Down Expand Up @@ -87,11 +88,9 @@ func commitV2Packet(packet Packet) []byte {
destinationHash := sha256.Sum256([]byte(packet.GetDestChannel()))
buf = append(buf, destinationHash[:]...)

// hash the version only if it is nonempty
if packet.AppVersion != "" {
versionHash := sha256.Sum256([]byte(packet.AppVersion))
buf = append(buf, versionHash[:]...)
}
// hash the app version.
versionHash := sha256.Sum256([]byte(packet.AppVersion))
buf = append(buf, versionHash[:]...)

// hash the data
dataHash := sha256.Sum256(packet.GetData())
Expand Down Expand Up @@ -199,6 +198,10 @@ func (p Packet) ValidateBasic() error {
if p.AppVersion != "" && slices.Contains([]IBCVersion{IBC_VERSION_UNSPECIFIED, IBC_VERSION_1}, p.ProtocolVersion) {
return errorsmod.Wrapf(ErrInvalidPacket, "app version cannot be specified when packet does not use protocol %s", IBC_VERSION_2)
}
if strings.TrimSpace(p.AppVersion) == "" && p.ProtocolVersion == IBC_VERSION_2 {
return errorsmod.Wrapf(ErrInvalidPacket, "app version must be specified when packet uses protocol %s", IBC_VERSION_2)
}

return nil
}

Expand Down
4 changes: 3 additions & 1 deletion modules/core/04-channel/types/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestPacketValidateBasic(t *testing.T) {
errMsg string
}{
{types.NewPacket(validPacketData, 1, portid, chanid, cpportid, cpchanid, timeoutHeight, timeoutTimestamp), true, ""},
{types.NewPacket(unknownPacketData, 1, portid, chanid, cpportid, cpchanid, timeoutHeight, timeoutTimestamp), true, ""},
{types.NewPacket(validPacketData, 0, portid, chanid, cpportid, cpchanid, timeoutHeight, timeoutTimestamp), false, "invalid sequence"},
{types.NewPacket(validPacketData, 1, invalidPort, chanid, cpportid, cpchanid, timeoutHeight, timeoutTimestamp), false, "invalid source port"},
{types.NewPacket(validPacketData, 1, portid, invalidChannel, cpportid, cpchanid, timeoutHeight, timeoutTimestamp), false, "invalid source channel"},
Expand All @@ -60,7 +61,8 @@ func TestPacketValidateBasic(t *testing.T) {
{types.NewPacketWithVersion(validPacketData, 1, portid, chanid, cpportid, cpchanid, timeoutHeight, timeoutTimestamp, "version"), true, "valid v2 packet"},
{types.Packet{1, portid, chanid, cpportid, cpchanid, validPacketData, timeoutHeight, timeoutTimestamp, types.IBC_VERSION_1, "version"}, false, "invalid specifying of app version with protocol version 1"},
{types.Packet{1, portid, chanid, cpportid, cpchanid, validPacketData, timeoutHeight, timeoutTimestamp, types.IBC_VERSION_UNSPECIFIED, "version"}, false, "invalid specifying of app version with unspecified protocol version"},
{types.NewPacket(unknownPacketData, 1, portid, chanid, cpportid, cpchanid, timeoutHeight, timeoutTimestamp), true, ""},
{types.Packet{1, portid, chanid, cpportid, cpchanid, validPacketData, timeoutHeight, timeoutTimestamp, types.IBC_VERSION_2, ""}, false, "app version must be specified when packet uses protocol version 2"},
{types.Packet{1, portid, chanid, cpportid, cpchanid, validPacketData, timeoutHeight, timeoutTimestamp, types.IBC_VERSION_2, " "}, false, "app version must be specified when packet uses protocol version 2"},
}

for i, tc := range testCases {
Expand Down

0 comments on commit 6d7d618

Please sign in to comment.