From 9b6258a7a57521689a08dcd48203bc2c86f38d6d Mon Sep 17 00:00:00 2001 From: Sean King Date: Thu, 20 Jan 2022 13:39:42 +0100 Subject: [PATCH 1/9] feat: adding helper fn to generate cap name for testing --- testing/mock/ibc_module.go | 11 +++++++++-- testing/mock/ibc_module_test.go | 26 ++++++++++++++++++++++++++ testing/mock/mock.go | 1 + 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 testing/mock/ibc_module_test.go diff --git a/testing/mock/ibc_module.go b/testing/mock/ibc_module.go index 84e57b8f25d..b5b69f123ad 100644 --- a/testing/mock/ibc_module.go +++ b/testing/mock/ibc_module.go @@ -2,6 +2,7 @@ package mock import ( "bytes" + "fmt" "strconv" sdk "github.com/cosmos/cosmos-sdk/types" @@ -52,8 +53,8 @@ func (im IBCModule) OnChanOpenTry( ) (version string, err error) { if im.IBCApp.OnChanOpenTry != nil { return im.IBCApp.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, counterpartyVersion) - } + // Claim channel capability passed back by IBC module if err := im.scopedKeeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { return "", err @@ -105,12 +106,14 @@ func (im IBCModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, re } // set state by claiming capability to check if revert happens return - _, err := im.scopedKeeper.NewCapability(ctx, MockRecvCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence()))) + capName := CreateOnRecvMockCapabilityName(packet) + _, err := im.scopedKeeper.NewCapability(ctx, capName) if err != nil { // application callback called twice on same packet sequence // must never occur panic(err) } + if bytes.Equal(MockPacketData, packet.GetData()) { return MockAcknowledgement } else if bytes.Equal(MockAsyncPacketData, packet.GetData()) { @@ -151,3 +154,7 @@ func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, return nil } + +func CreateOnRecvMockCapabilityName(packet channeltypes.Packet) string { + return fmt.Sprintf("%s%s%s%s", MockRecvCanaryCapabilityName, packet.GetDestPort(), packet.GetDestChannel(), strconv.Itoa(int(packet.GetSequence()))) +} diff --git a/testing/mock/ibc_module_test.go b/testing/mock/ibc_module_test.go new file mode 100644 index 00000000000..45b88c5e342 --- /dev/null +++ b/testing/mock/ibc_module_test.go @@ -0,0 +1,26 @@ +package mock_test + +import ( + "testing" + + clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" + "github.com/cosmos/ibc-go/v3/testing/mock" + "github.com/stretchr/testify/require" +) + +func TestCreateCapabilityName(t *testing.T) { + packet := channeltypes.NewPacket( + []byte{}, + 1, + mock.PortID, + "channel-0", + mock.PortID, + "channel-0", + clienttypes.NewHeight(0, 100), + 0, + ) + + name := mock.CreateOnRecvMockCapabilityName(packet) + require.Equal(t, "mock receive canary capability namemock-portIDchannel-01", name) +} diff --git a/testing/mock/mock.go b/testing/mock/mock.go index 6f12f8fac66..31b1795db36 100644 --- a/testing/mock/mock.go +++ b/testing/mock/mock.go @@ -23,6 +23,7 @@ import ( const ( ModuleName = "mock" + PortID = "mock-portID" Version = "mock-version" ) From 7e80dca719c1563a7d5b2faaf572742c57dc25d7 Mon Sep 17 00:00:00 2001 From: Sean King Date: Thu, 20 Jan 2022 14:16:57 +0100 Subject: [PATCH 2/9] fix: refactoring tests to use new helper in Mock package --- modules/core/keeper/msg_server_test.go | 2 +- testing/mock/ibc_module.go | 5 +++-- testing/mock/ibc_module_test.go | 2 +- testing/values.go | 3 ++- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/core/keeper/msg_server_test.go b/modules/core/keeper/msg_server_test.go index 7b437d420b5..e3779768dc9 100644 --- a/modules/core/keeper/msg_server_test.go +++ b/modules/core/keeper/msg_server_test.go @@ -199,7 +199,7 @@ func (suite *KeeperTestSuite) TestHandleRecvPacket() { suite.Require().NoError(err) // check that callback state was handled correctly - _, exists := suite.chainB.GetSimApp().ScopedIBCMockKeeper.GetCapability(suite.chainB.GetContext(), ibctesting.GetMockRecvCanaryCapabilityName(packet)) + _, exists := suite.chainB.GetSimApp().ScopedIBCMockKeeper.GetCapability(suite.chainB.GetContext(), ibcmock.GetMockRecvCanaryCapabilityName(packet)) if tc.expRevert { suite.Require().False(exists, "capability exists in store even after callback reverted") } else { diff --git a/testing/mock/ibc_module.go b/testing/mock/ibc_module.go index b5b69f123ad..ac56bc35ab5 100644 --- a/testing/mock/ibc_module.go +++ b/testing/mock/ibc_module.go @@ -106,7 +106,7 @@ func (im IBCModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, re } // set state by claiming capability to check if revert happens return - capName := CreateOnRecvMockCapabilityName(packet) + capName := GetMockRecvCanaryCapabilityName(packet) _, err := im.scopedKeeper.NewCapability(ctx, capName) if err != nil { // application callback called twice on same packet sequence @@ -155,6 +155,7 @@ func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, return nil } -func CreateOnRecvMockCapabilityName(packet channeltypes.Packet) string { +// Generates a capability name based on a Packet +func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string { return fmt.Sprintf("%s%s%s%s", MockRecvCanaryCapabilityName, packet.GetDestPort(), packet.GetDestChannel(), strconv.Itoa(int(packet.GetSequence()))) } diff --git a/testing/mock/ibc_module_test.go b/testing/mock/ibc_module_test.go index 45b88c5e342..8e49e74ba5a 100644 --- a/testing/mock/ibc_module_test.go +++ b/testing/mock/ibc_module_test.go @@ -21,6 +21,6 @@ func TestCreateCapabilityName(t *testing.T) { 0, ) - name := mock.CreateOnRecvMockCapabilityName(packet) + name := mock.GetMockRecvCanaryCapabilityName(packet) require.Equal(t, "mock receive canary capability namemock-portIDchannel-01", name) } diff --git a/testing/values.go b/testing/values.go index 8fb6fa30407..c9d7833d6c3 100644 --- a/testing/values.go +++ b/testing/values.go @@ -5,6 +5,7 @@ package ibctesting import ( + "fmt" "strconv" "time" @@ -63,5 +64,5 @@ var ( ) func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string { - return MockRecvCanaryCapabilityName + strconv.Itoa(int(packet.GetSequence())) + return fmt.Sprintf("%s%s%s%s", MockRecvCanaryCapabilityName, packet.GetDestPort(), packet.GetDestChannel(), strconv.Itoa(int(packet.GetSequence()))) } From b3fcc38c49ecc96ecfbfa571c3e2b84bd09abe9e Mon Sep 17 00:00:00 2001 From: Sean King Date: Thu, 20 Jan 2022 14:36:10 +0100 Subject: [PATCH 3/9] fix: comment --- testing/mock/ibc_module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/mock/ibc_module.go b/testing/mock/ibc_module.go index ac56bc35ab5..a4f8c5219eb 100644 --- a/testing/mock/ibc_module.go +++ b/testing/mock/ibc_module.go @@ -155,7 +155,7 @@ func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, return nil } -// Generates a capability name based on a Packet +// GetMockRecvCanaryCapabilityName generates a capability name based on a Packet func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string { return fmt.Sprintf("%s%s%s%s", MockRecvCanaryCapabilityName, packet.GetDestPort(), packet.GetDestChannel(), strconv.Itoa(int(packet.GetSequence()))) } From 7f5e704d88b8ed4e5eba7c7cabfff29fc4cbad16 Mon Sep 17 00:00:00 2001 From: Sean King Date: Thu, 20 Jan 2022 14:39:36 +0100 Subject: [PATCH 4/9] fix: remove unused fn --- testing/values.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/testing/values.go b/testing/values.go index c9d7833d6c3..6bdf782c65a 100644 --- a/testing/values.go +++ b/testing/values.go @@ -5,15 +5,12 @@ package ibctesting import ( - "fmt" - "strconv" "time" sdk "github.com/cosmos/cosmos-sdk/types" ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types" connectiontypes "github.com/cosmos/ibc-go/v3/modules/core/03-connection/types" - channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types" ibctmtypes "github.com/cosmos/ibc-go/v3/modules/light-clients/07-tendermint/types" "github.com/cosmos/ibc-go/v3/testing/mock" @@ -62,7 +59,3 @@ var ( prefix = commitmenttypes.NewMerklePrefix([]byte("ibc")) ) - -func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string { - return fmt.Sprintf("%s%s%s%s", MockRecvCanaryCapabilityName, packet.GetDestPort(), packet.GetDestChannel(), strconv.Itoa(int(packet.GetSequence()))) -} From 22648932c3f3c8fff1770b8f2b4a109b5713555d Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 21 Jan 2022 11:45:39 +0100 Subject: [PATCH 5/9] Update testing/mock/ibc_module.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- testing/mock/ibc_module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/mock/ibc_module.go b/testing/mock/ibc_module.go index a4f8c5219eb..d1ee6a4d6e7 100644 --- a/testing/mock/ibc_module.go +++ b/testing/mock/ibc_module.go @@ -155,7 +155,7 @@ func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, return nil } -// GetMockRecvCanaryCapabilityName generates a capability name based on a Packet +// GetMockRecvCanaryCapabilityName generates a capability name for testing OnRecvPacket functionality. func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string { return fmt.Sprintf("%s%s%s%s", MockRecvCanaryCapabilityName, packet.GetDestPort(), packet.GetDestChannel(), strconv.Itoa(int(packet.GetSequence()))) } From 9b0359b55c1eed7266257a23ceffa93a9e15f818 Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 21 Jan 2022 12:00:40 +0100 Subject: [PATCH 6/9] Update testing/mock/ibc_module_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> --- testing/mock/ibc_module_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testing/mock/ibc_module_test.go b/testing/mock/ibc_module_test.go index 8e49e74ba5a..0e7a09a1cc2 100644 --- a/testing/mock/ibc_module_test.go +++ b/testing/mock/ibc_module_test.go @@ -2,11 +2,12 @@ package mock_test import ( "testing" + + "github.com/stretchr/testify/require" clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" "github.com/cosmos/ibc-go/v3/testing/mock" - "github.com/stretchr/testify/require" ) func TestCreateCapabilityName(t *testing.T) { From ffe1249918d6d69d076c85194f433298a0cde7ec Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 21 Jan 2022 11:56:25 +0100 Subject: [PATCH 7/9] adding additional helpers for ack + timeout --- testing/config.go | 2 +- testing/mock/ibc_module.go | 20 +++++++++++++++----- testing/mock/ibc_module_test.go | 8 +++++++- testing/mock/mock.go | 2 +- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/testing/config.go b/testing/config.go index 720820498e8..a47dea1701b 100644 --- a/testing/config.go +++ b/testing/config.go @@ -58,7 +58,7 @@ type ChannelConfig struct { func NewChannelConfig() *ChannelConfig { return &ChannelConfig{ - PortID: mock.ModuleName, + PortID: mock.PortID, Version: DefaultChannelVersion, Order: channeltypes.UNORDERED, } diff --git a/testing/mock/ibc_module.go b/testing/mock/ibc_module.go index d1ee6a4d6e7..d23d90f8ed5 100644 --- a/testing/mock/ibc_module.go +++ b/testing/mock/ibc_module.go @@ -129,8 +129,8 @@ func (im IBCModule) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes return im.IBCApp.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer) } - _, err := im.scopedKeeper.NewCapability(ctx, MockAckCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence()))) - if err != nil { + capName := GetMockAckCanaryCapabilityName(packet) + if _, err := im.scopedKeeper.NewCapability(ctx, capName); err != nil { // application callback called twice on same packet sequence // must never occur panic(err) @@ -145,8 +145,8 @@ func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, return im.IBCApp.OnTimeoutPacket(ctx, packet, relayer) } - _, err := im.scopedKeeper.NewCapability(ctx, MockTimeoutCanaryCapabilityName+strconv.Itoa(int(packet.GetSequence()))) - if err != nil { + capName := GetMockTimeoutCanaryCapabilityName(packet) + if _, err := im.scopedKeeper.NewCapability(ctx, capName); err != nil { // application callback called twice on same packet sequence // must never occur panic(err) @@ -155,7 +155,17 @@ func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, return nil } -// GetMockRecvCanaryCapabilityName generates a capability name for testing OnRecvPacket functionality. +// GetMockRecvCanaryCapabilityName generates a capability name for testing OnRecvPacket functionality. func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string { return fmt.Sprintf("%s%s%s%s", MockRecvCanaryCapabilityName, packet.GetDestPort(), packet.GetDestChannel(), strconv.Itoa(int(packet.GetSequence()))) } + +// GetMockAckCanaryCapabilityName generates a capability name for OnAcknowledgementPacket functionality. +func GetMockAckCanaryCapabilityName(packet channeltypes.Packet) string { + return fmt.Sprintf("%s%s%s%s", MockAckCanaryCapabilityName, packet.GetSourcePort(), packet.GetSourceChannel(), strconv.Itoa(int(packet.GetSequence()))) +} + +// GetMockTimeoutCanaryCapabilityName generates a capability name for OnTimeoutacket functionality. +func GetMockTimeoutCanaryCapabilityName(packet channeltypes.Packet) string { + return fmt.Sprintf("%s%s%s%s", MockTimeoutCanaryCapabilityName, packet.GetSourcePort(), packet.GetSourceChannel(), strconv.Itoa(int(packet.GetSequence()))) +} diff --git a/testing/mock/ibc_module_test.go b/testing/mock/ibc_module_test.go index 0e7a09a1cc2..d3efe9f142c 100644 --- a/testing/mock/ibc_module_test.go +++ b/testing/mock/ibc_module_test.go @@ -23,5 +23,11 @@ func TestCreateCapabilityName(t *testing.T) { ) name := mock.GetMockRecvCanaryCapabilityName(packet) - require.Equal(t, "mock receive canary capability namemock-portIDchannel-01", name) + require.Equal(t, "mock receive canary capability namemockchannel-01", name) + + name = mock.GetMockAckCanaryCapabilityName(packet) + require.Equal(t, "mock acknowledgement canary capability namemockchannel-01", name) + + name = mock.GetMockTimeoutCanaryCapabilityName(packet) + require.Equal(t, "mock timeout canary capability namemockchannel-01", name) } diff --git a/testing/mock/mock.go b/testing/mock/mock.go index 31b1795db36..fd454aa80d9 100644 --- a/testing/mock/mock.go +++ b/testing/mock/mock.go @@ -23,7 +23,7 @@ import ( const ( ModuleName = "mock" - PortID = "mock-portID" + PortID = ModuleName Version = "mock-version" ) From 4ce0a1b742031905fb0d4afe0ca8e000e92c81a9 Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 21 Jan 2022 12:02:25 +0100 Subject: [PATCH 8/9] chore: changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39efde8e78c..8fe33b958b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (transfer) [\#517](https://github.com/cosmos/ibc-go/pull/517) Separates the ICS 26 callback functions from `AppModule` into a new type `IBCModule` for ICS 20 transfer. * (modules/core/02-client) [\#536](https://github.com/cosmos/ibc-go/pull/536) `GetSelfConsensusState` return type changed from bool to error. * (channel) [\#644](https://github.com/cosmos/ibc-go/pull/644) Removes `CounterpartyHops` function from the ChannelKeeper. +* (testing) [\#776](https://github.com/cosmos/ibc-go/pull/776) Adding helper fn to generate capability name for testing callbacks + ### State Machine Breaking From 3147bbf0839f8d3559fecb6a731b7380472c414b Mon Sep 17 00:00:00 2001 From: Sean King Date: Fri, 21 Jan 2022 12:07:46 +0100 Subject: [PATCH 9/9] nit: err syntax --- testing/mock/ibc_module.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/testing/mock/ibc_module.go b/testing/mock/ibc_module.go index d23d90f8ed5..fb46864709b 100644 --- a/testing/mock/ibc_module.go +++ b/testing/mock/ibc_module.go @@ -107,8 +107,7 @@ func (im IBCModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, re // set state by claiming capability to check if revert happens return capName := GetMockRecvCanaryCapabilityName(packet) - _, err := im.scopedKeeper.NewCapability(ctx, capName) - if err != nil { + if _, err := im.scopedKeeper.NewCapability(ctx, capName); err != nil { // application callback called twice on same packet sequence // must never occur panic(err)