From 7ca1dad6d723389ad5fdab31504ad4166baed7ed Mon Sep 17 00:00:00 2001 From: srdtrk Date: Wed, 6 Sep 2023 11:30:35 +0300 Subject: [PATCH 1/2] imp: added PacketDataUnmarshaler to ica/host --- .../27-interchain-accounts/host/ibc_module.go | 18 ++++++++++++++ .../host/ibc_module_test.go | 24 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/modules/apps/27-interchain-accounts/host/ibc_module.go b/modules/apps/27-interchain-accounts/host/ibc_module.go index 6efd8c47cb8..7870e91b0ed 100644 --- a/modules/apps/27-interchain-accounts/host/ibc_module.go +++ b/modules/apps/27-interchain-accounts/host/ibc_module.go @@ -12,10 +12,16 @@ import ( "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types" icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" + porttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types" ibcerrors "github.com/cosmos/ibc-go/v7/modules/core/errors" ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" ) +var ( + _ porttypes.IBCModule = (*IBCModule)(nil) + _ porttypes.PacketDataUnmarshaler = (*IBCModule)(nil) +) + // IBCModule implements the ICS26 interface for interchain accounts host chains type IBCModule struct { keeper keeper.Keeper @@ -149,3 +155,15 @@ func (IBCModule) OnTimeoutPacket( ) error { return errorsmod.Wrap(icatypes.ErrInvalidChannelFlow, "cannot cause a packet timeout on a host channel end, a host chain does not send a packet over the channel") } + +// UnmarshalPacketData attempts to unmarshal the provided packet data bytes +// into an InterchainAccountPacketData. This function implements the optional +// PacketDataUnmarshaler interface required for ADR 008 support. +func (IBCModule) UnmarshalPacketData(bz []byte) (interface{}, error) { + var data icatypes.InterchainAccountPacketData + err := data.UnmarshalJSON(bz) + if err != nil { + return nil, err + } + return data, nil +} diff --git a/modules/apps/27-interchain-accounts/host/ibc_module_test.go b/modules/apps/27-interchain-accounts/host/ibc_module_test.go index 05f9c7d520f..b9bc6928e47 100644 --- a/modules/apps/27-interchain-accounts/host/ibc_module_test.go +++ b/modules/apps/27-interchain-accounts/host/ibc_module_test.go @@ -15,6 +15,7 @@ import ( capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types" + icahost "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host" icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types" feetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types" clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" @@ -714,3 +715,26 @@ func (suite *InterchainAccountsTestSuite) assertBalance(addr sdk.AccAddress, exp balance := suite.chainB.GetSimApp().BankKeeper.GetBalance(suite.chainB.GetContext(), addr, sdk.DefaultBondDenom) suite.Require().Equal(expBalance[0], balance) } + +func (suite *InterchainAccountsTestSuite) TestPacketDataUnmarshalerInterface() { + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + expPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: []byte("data"), + Memo: "", + } + + packetData, err := icahost.IBCModule{}.UnmarshalPacketData(expPacketData.GetBytes()) + suite.Require().NoError(err) + suite.Require().Equal(expPacketData, packetData) + + // test invalid packet data + invalidPacketData := []byte("invalid packet data") + packetData, err = icahost.IBCModule{}.UnmarshalPacketData(invalidPacketData) + suite.Require().Error(err) + suite.Require().Nil(packetData) +} From b0964bce007e30a0553544e3f0420db470ee24bc Mon Sep 17 00:00:00 2001 From: srdtrk Date: Wed, 6 Sep 2023 13:04:40 +0300 Subject: [PATCH 2/2] style: ran gofumpt --- modules/apps/27-interchain-accounts/host/ibc_module_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/27-interchain-accounts/host/ibc_module_test.go b/modules/apps/27-interchain-accounts/host/ibc_module_test.go index b9bc6928e47..a48e4b9a6bd 100644 --- a/modules/apps/27-interchain-accounts/host/ibc_module_test.go +++ b/modules/apps/27-interchain-accounts/host/ibc_module_test.go @@ -14,8 +14,8 @@ import ( banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types" - "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types" icahost "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host" + "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types" icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types" feetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types" clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"