Skip to content

Commit

Permalink
refactor: ics29 OnChanOpenInit callback tests now use mock module (#924)
Browse files Browse the repository at this point in the history
* refactor: OnChanOpenInit callback tests now use mock module

* Update modules/apps/29-fee/fee_test.go
  • Loading branch information
colin-axner committed Feb 16, 2022
1 parent fb243c6 commit 8d226de
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
18 changes: 18 additions & 0 deletions modules/apps/29-fee/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
ibcmock "github.com/cosmos/ibc-go/v3/testing/mock"
)

type FeeTestSuite struct {
Expand All @@ -24,6 +25,7 @@ type FeeTestSuite struct {
path *ibctesting.Path
}

// TODO: remove and rename 'SetupMockTest' to 'SetupTest'
func (suite *FeeTestSuite) SetupTest() {
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2)
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
Expand All @@ -38,10 +40,26 @@ func (suite *FeeTestSuite) SetupTest() {
suite.path = path
}

// TODO: rename to 'SetupTest' when the above function is removed
func (suite *FeeTestSuite) SetupMockTest() {
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2)
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1))
suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2))

path := ibctesting.NewPath(suite.chainA, suite.chainB)
mockFeeVersion := string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: ibcmock.Version}))
path.EndpointA.ChannelConfig.Version = mockFeeVersion
path.EndpointB.ChannelConfig.Version = mockFeeVersion
path.EndpointA.ChannelConfig.PortID = ibctesting.MockFeePort
path.EndpointB.ChannelConfig.PortID = ibctesting.MockFeePort
suite.path = path
}

func TestIBCFeeTestSuite(t *testing.T) {
suite.Run(t, new(FeeTestSuite))
}

// TODO: remove
func (suite *FeeTestSuite) CreateICS20Packet(coin sdk.Coin) channeltypes.Packet {

fungibleTokenPacket := transfertypes.NewFungibleTokenPacketData(
Expand Down
38 changes: 27 additions & 11 deletions modules/apps/29-fee/ibc_module_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fee_test

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"

Expand All @@ -9,6 +11,7 @@ import (
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
ibcmock "github.com/cosmos/ibc-go/v3/testing/mock"
"github.com/cosmos/ibc-go/v3/testing/simapp"
)

Expand All @@ -26,27 +29,27 @@ func (suite *FeeTestSuite) TestOnChanOpenInit() {
expPass bool
}{
{
"success - valid fee middleware and transfer version",
string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.Version})),
"success - valid fee middleware and mock version",
string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: ibcmock.Version})),
true,
},
{
"success - fee version not included, only perform transfer logic",
transfertypes.Version,
"success - fee version not included, only perform mock logic",
ibcmock.Version,
true,
},
{
"invalid fee middleware version",
string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: "invalid-ics29-1", AppVersion: transfertypes.Version})),
string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: "invalid-ics29-1", AppVersion: ibcmock.Version})),
false,
},
{
"invalid transfer version",
string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: "invalid-ics20-1"})),
"invalid mock version",
string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: "invalid-mock-version"})),
false,
},
{
"transfer version not wrapped",
"mock version not wrapped",
types.Version,
false,
},
Expand All @@ -57,8 +60,21 @@ func (suite *FeeTestSuite) TestOnChanOpenInit() {

suite.Run(tc.name, func() {
// reset suite
suite.SetupTest()
suite.SetupMockTest()
suite.coordinator.SetupConnections(suite.path)

// setup mock callback
suite.chainA.GetSimApp().FeeMockModule.IBCApp.OnChanOpenInit = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string,
portID, channelID string, chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty, version string,
) error {
if version != ibcmock.Version {
return fmt.Errorf("incorrect mock version")
}

return nil
}

suite.path.EndpointA.ChannelID = ibctesting.FirstChannelID

counterparty := channeltypes.NewCounterparty(suite.path.EndpointB.ChannelConfig.PortID, suite.path.EndpointB.ChannelID)
Expand All @@ -70,10 +86,10 @@ func (suite *FeeTestSuite) TestOnChanOpenInit() {
Version: tc.version,
}

module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort)
module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.MockFeePort)
suite.Require().NoError(err)

chanCap, err := suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(ibctesting.TransferPort, suite.path.EndpointA.ChannelID))
chanCap, err := suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID))
suite.Require().NoError(err)

cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module)
Expand Down
19 changes: 12 additions & 7 deletions testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ import (

const appName = "SimApp"

// IBC application testing ports
const (
MockFeePort string = ibcmock.ModuleName + ibcfeetypes.ModuleName
)

var (
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string
Expand Down Expand Up @@ -217,6 +222,7 @@ type SimApp struct {
// make IBC modules public for test purposes
// these modules are never directly routed to by the IBC Router
ICAAuthModule ibcmock.IBCModule
FeeMockModule ibcmock.IBCModule

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -288,7 +294,7 @@ func NewSimApp(
// NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do
// not replicate if you do not need to test core IBC or light clients.
scopedIBCMockKeeper := app.CapabilityKeeper.ScopeToModule(ibcmock.ModuleName)
scopedFeeMockKeeper := app.CapabilityKeeper.ScopeToModule(ibcmock.ModuleName + ibcfeetypes.ModuleName)
scopedFeeMockKeeper := app.CapabilityKeeper.ScopeToModule(MockFeePort)
scopedICAMockKeeper := app.CapabilityKeeper.ScopeToModule(ibcmock.ModuleName + icacontrollertypes.SubModuleName)

// seal capability keeper after scoping modules
Expand Down Expand Up @@ -371,11 +377,10 @@ func NewSimApp(
mockModule := ibcmock.NewAppModule(&app.IBCKeeper.PortKeeper)

// create fee wrapped mock module
feeMockModule := ibcfee.NewIBCModule(
app.IBCFeeKeeper, ibcmock.NewIBCModule(
&mockModule, ibcmock.NewMockIBCApp(ibcmock.ModuleName+ibcfeetypes.ModuleName, scopedFeeMockKeeper),
),
)
feeMockModule := ibcmock.NewIBCModule(&mockModule, ibcmock.NewMockIBCApp(MockFeePort, scopedFeeMockKeeper))
app.FeeMockModule = feeMockModule

feeWithMockModule := ibcfee.NewIBCModule(app.IBCFeeKeeper, feeMockModule)

mockIBCModule := ibcmock.NewIBCModule(&mockModule, ibcmock.NewMockIBCApp(ibcmock.ModuleName, scopedIBCMockKeeper))

Expand Down Expand Up @@ -409,7 +414,7 @@ func NewSimApp(
AddRoute(ibcmock.ModuleName+icacontrollertypes.SubModuleName, icaControllerIBCModule). // ica with mock auth module stack route to ica (top level of middleware stack)
AddRoute(ibctransfertypes.ModuleName, feeTransferModule).
AddRoute(ibcmock.ModuleName, mockIBCModule).
AddRoute(ibcmock.ModuleName+ibcfeetypes.ModuleName, feeMockModule)
AddRoute(ibcmock.ModuleName+ibcfeetypes.ModuleName, feeWithMockModule)

app.IBCKeeper.SetRouter(ibcRouter)

Expand Down
2 changes: 2 additions & 0 deletions testing/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
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"
"github.com/cosmos/ibc-go/v3/testing/simapp"
)

const (
Expand All @@ -33,6 +34,7 @@ const (
// Application Ports
TransferPort = ibctransfertypes.ModuleName
MockPort = mock.ModuleName
MockFeePort = simapp.MockFeePort

// used for testing proposals
Title = "title"
Expand Down

0 comments on commit 8d226de

Please sign in to comment.