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 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/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 84e57b8f25d..fb46864709b 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,13 @@ 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()))) - if err != nil { + capName := GetMockRecvCanaryCapabilityName(packet) + if _, err := im.scopedKeeper.NewCapability(ctx, capName); 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()) { @@ -126,8 +128,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) @@ -142,8 +144,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) @@ -151,3 +153,18 @@ func (im IBCModule) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, return nil } + +// 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 new file mode 100644 index 00000000000..d3efe9f142c --- /dev/null +++ b/testing/mock/ibc_module_test.go @@ -0,0 +1,33 @@ +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" +) + +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.GetMockRecvCanaryCapabilityName(packet) + 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 6f12f8fac66..fd454aa80d9 100644 --- a/testing/mock/mock.go +++ b/testing/mock/mock.go @@ -23,6 +23,7 @@ import ( const ( ModuleName = "mock" + PortID = ModuleName Version = "mock-version" ) diff --git a/testing/values.go b/testing/values.go index 8fb6fa30407..6bdf782c65a 100644 --- a/testing/values.go +++ b/testing/values.go @@ -5,14 +5,12 @@ package ibctesting import ( - "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" @@ -61,7 +59,3 @@ var ( prefix = commitmenttypes.NewMerklePrefix([]byte("ibc")) ) - -func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string { - return MockRecvCanaryCapabilityName + strconv.Itoa(int(packet.GetSequence())) -}