Skip to content

Commit

Permalink
feat(core, apps): 'PacketData' interface added and implemented (backp…
Browse files Browse the repository at this point in the history
…ort #4200) (#4219)

* feat(core, apps): 'PacketData' interface added and implemented (#4200)

* refactor(core/exported): moved packet interfaces to packet.go

* feat(core/exported): created PacketData interface

* feat(transfer): implemented PacketData for transfer

* feat(ica): implemented PacketData

* docs(transfer.adr8): updated godocs of GetPacketSender

* style(adr8): renamed parameter srcPortID -> sourcePortID

* docs(core.adr8): updated godocs for PacketData interface

* docs(core.adr8): improved godocs for PacketData interface

* docs(core.adr8): updated godocs

* docs(transfer.adr8): updated godocs

* style(ica_test): removed unneeded comment

* docs(ica.adr8): updated godocs

* style(ica): fixed revive linter complaint

* docs(transfer.adr8): updated GetPacketSender's godocs

* style(transfer, ica): ran golangci-lin

(cherry picked from commit ec68438)

# Conflicts:
#	modules/apps/27-interchain-accounts/types/packet.go
#	modules/apps/27-interchain-accounts/types/packet_test.go
#	modules/apps/transfer/types/packet.go
#	modules/apps/transfer/types/packet_test.go
#	modules/core/exported/packet.go

* fix: fixed backporting conflicts

* fix: used TrimPrefix because CutPrefix was added in go1.20

* style(ica_test): typo fixed

---------

Co-authored-by: srdtrk <59252793+srdtrk@users.noreply.github.com>
Co-authored-by: srdtrk <srdtrk@hotmail.com>
  • Loading branch information
3 people authored Aug 4, 2023
1 parent 0ba9f75 commit 69f6cd3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
22 changes: 22 additions & 0 deletions modules/apps/27-interchain-accounts/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/json"
"strings"
"time"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -11,6 +12,11 @@ import (
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)

var (
_ ibcexported.PacketData = (*InterchainAccountPacketData)(nil)
_ ibcexported.PacketDataProvider = (*InterchainAccountPacketData)(nil)
)

var _ ibcexported.PacketDataProvider = (*InterchainAccountPacketData)(nil)

// MaxMemoCharLength defines the maximum length for the InterchainAccountPacketData memo field
Expand Down Expand Up @@ -69,6 +75,22 @@ func (ct CosmosTx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return nil
}

// GetPacketSender returns the sender address of the interchain accounts packet data.
// It is obtained from the source port ID by cutting off the ControllerPortPrefix.
// If the source port ID does not have the ControllerPortPrefix, then an empty string is returned.
//
// NOTE:
// - The sender address is set by the packet sender and may not have been validated a signature
// check if the packet sender isn't the interchain accounts module.
// - The sender address must only be used by modules on the sending chain.
func (InterchainAccountPacketData) GetPacketSender(sourcePortID string) string {
found := strings.HasPrefix(sourcePortID, ControllerPortPrefix)
if !found {
return ""
}
return strings.TrimPrefix(sourcePortID, ControllerPortPrefix)
}

// GetCustomPacketData interprets the memo field of the packet data as a JSON object
// and returns the value associated with the given key.
// If the key is missing or the memo is not properly formatted, then nil is returned.
Expand Down
29 changes: 29 additions & 0 deletions modules/apps/27-interchain-accounts/types/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ func (suite *TypesTestSuite) TestValidateBasic() {
}
}

func (suite *TypesTestSuite) TestGetPacketSender() {
testCases := []struct {
name string
srcPortID string
expSender string
}{
{
"success: port id has prefix",
types.ControllerPortPrefix + ibctesting.TestAccAddress,
ibctesting.TestAccAddress,
},
{
"failure: missing prefix",
ibctesting.TestAccAddress,
"",
},
{
"failure: empty port id",
"",
"",
},
}

for _, tc := range testCases {
packetData := types.InterchainAccountPacketData{}
suite.Require().Equal(tc.expSender, packetData.GetPacketSender(tc.srcPortID))
}
}

func (suite *TypesTestSuite) TestPacketDataProvider() {
expCallbackAddr := ibctesting.TestAccAddress

Expand Down
16 changes: 16 additions & 0 deletions modules/apps/transfer/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)

var (
_ ibcexported.PacketData = (*FungibleTokenPacketData)(nil)
_ ibcexported.PacketDataProvider = (*FungibleTokenPacketData)(nil)
)

var _ ibcexported.PacketDataProvider = (*FungibleTokenPacketData)(nil)

var (
Expand Down Expand Up @@ -66,6 +71,17 @@ func (ftpd FungibleTokenPacketData) GetBytes() []byte {
return sdk.MustSortJSON(mustProtoMarshalJSON(&ftpd))
}

// GetPacketSender returns the sender address embedded in the packet data.
//
// NOTE:
// - The sender address is set by the module which requested the packet to be sent,
// and this module may not have validated the sender address by a signature check.
// - The sender address must only be used by modules on the sending chain.
// - sourcePortID is not used in this implementation.
func (ftpd FungibleTokenPacketData) GetPacketSender(sourcePortID string) string {
return ftpd.Sender
}

// GetCustomPacketData interprets the memo field of the packet data as a JSON object
// and returns the value associated with the given key.
// If the key is missing or the memo is not properly formatted, then nil is returned.
Expand Down
12 changes: 12 additions & 0 deletions modules/apps/transfer/types/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ func TestFungibleTokenPacketDataValidateBasic(t *testing.T) {
}
}

func (suite *TypesTestSuite) TestGetPacketSender() {
packetData := types.FungibleTokenPacketData{
Denom: denom,
Amount: amount,
Sender: sender,
Receiver: receiver,
Memo: "",
}

suite.Require().Equal(sender, packetData.GetPacketSender(types.PortID))
}

func (suite *TypesTestSuite) TestPacketDataProvider() {
testCases := []struct {
name string
Expand Down
7 changes: 7 additions & 0 deletions modules/core/exported/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ type Acknowledgement interface {
Acknowledgement() []byte
}

// PacketData defines an optional interface which an application's packet data structure may implement.
type PacketData interface {
// GetPacketSender returns the sender address of the packet data.
// If the packet sender is unknown or undefined, an empty string should be returned.
GetPacketSender(sourcePortID string) string
}

// PacketDataProvider defines an optional interfaces for retrieving custom packet data stored on behalf of another application.
// An existing problem in the IBC middleware design is the inability for a middleware to define its own packet data type and insert packet sender provided information.
// A short term solution was introduced into several application's packet data to utilize a memo field to carry this information on behalf of another application.
Expand Down

0 comments on commit 69f6cd3

Please sign in to comment.