Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding helper fn to generate capability name for testing #776

Merged
merged 10 commits into from
Jan 21, 2022
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too late now, but I think this changelog entry could have been a little more specific, at least indicating this is to be only used for the mock testing module



### State Machine Breaking

Expand Down
2 changes: 1 addition & 1 deletion modules/core/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion testing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type ChannelConfig struct {

func NewChannelConfig() *ChannelConfig {
return &ChannelConfig{
PortID: mock.ModuleName,
PortID: mock.PortID,
Version: DefaultChannelVersion,
Order: channeltypes.UNORDERED,
}
Expand Down
31 changes: 24 additions & 7 deletions testing/mock/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mock

import (
"bytes"
"fmt"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand All @@ -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)
Expand All @@ -142,12 +144,27 @@ 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)
}

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())))
}
33 changes: 33 additions & 0 deletions testing/mock/ibc_module_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions testing/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
const (
ModuleName = "mock"

PortID = ModuleName
Version = "mock-version"
Comment on lines +26 to 27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth adding a line break here

Suggested change
PortID = ModuleName
Version = "mock-version"
PortID = ModuleName
Version = "mock-version"

)

Expand Down
6 changes: 0 additions & 6 deletions testing/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -61,7 +59,3 @@ var (

prefix = commitmenttypes.NewMerklePrefix([]byte("ibc"))
)

func GetMockRecvCanaryCapabilityName(packet channeltypes.Packet) string {
return MockRecvCanaryCapabilityName + strconv.Itoa(int(packet.GetSequence()))
}