From 4326c14569c9226f89ff6e08fcd76daa4be8b42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Fri, 18 Feb 2022 11:57:33 +0100 Subject: [PATCH] refactor: ics29 OnChanOpenTry/Ack use mock module for testing instead of ics20 (#925) Co-authored-by: Sean King --- modules/apps/29-fee/ibc_module_test.go | 62 +++++++++++++++++--------- testing/simapp/app.go | 3 +- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/modules/apps/29-fee/ibc_module_test.go b/modules/apps/29-fee/ibc_module_test.go index 4e19badb808..1c5609665bc 100644 --- a/modules/apps/29-fee/ibc_module_test.go +++ b/modules/apps/29-fee/ibc_module_test.go @@ -71,7 +71,6 @@ func (suite *FeeTestSuite) TestOnChanOpenInit() { if version != ibcmock.Version { return fmt.Errorf("incorrect mock version") } - return nil } @@ -117,31 +116,31 @@ func (suite *FeeTestSuite) TestOnChanOpenTry() { }{ { "success - valid fee middleware version", - string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.Version})), + string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: ibcmock.Version})), false, true, }, { - "success - valid transfer version", - transfertypes.Version, + "success - valid mock version", + ibcmock.Version, false, true, }, { "success - crossing hellos: valid fee middleware", - string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.Version})), + string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: ibcmock.Version})), true, 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, 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, false, }, @@ -152,10 +151,21 @@ func (suite *FeeTestSuite) TestOnChanOpenTry() { suite.Run(tc.name, func() { // reset suite - suite.SetupTest() + suite.SetupMockTest() suite.coordinator.SetupConnections(suite.path) suite.path.EndpointB.ChanOpenInit() + // setup mock callback + suite.chainA.GetSimApp().FeeMockModule.IBCApp.OnChanOpenTry = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string, + portID, channelID string, chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, counterpartyVersion string, + ) (string, error) { + if counterpartyVersion != ibcmock.Version { + return "", fmt.Errorf("incorrect mock version") + } + return ibcmock.Version, nil + } + var ( chanCap *capabilitytypes.Capability ok bool @@ -163,10 +173,10 @@ func (suite *FeeTestSuite) TestOnChanOpenTry() { ) if tc.crossing { suite.path.EndpointA.ChanOpenInit() - chanCap, ok = suite.chainA.GetSimApp().ScopedTransferKeeper.GetCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(ibctesting.TransferPort, suite.path.EndpointA.ChannelID)) + chanCap, ok = suite.chainA.GetSimApp().ScopedFeeMockKeeper.GetCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID)) suite.Require().True(ok) } else { - 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) } @@ -181,7 +191,7 @@ func (suite *FeeTestSuite) TestOnChanOpenTry() { Version: tc.cpVersion, } - 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) cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) @@ -209,19 +219,19 @@ func (suite *FeeTestSuite) TestOnChanOpenAck() { }{ { "success", - string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.Version})), + string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: ibcmock.Version})), func(suite *FeeTestSuite) {}, true, }, { "invalid fee 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})), func(suite *FeeTestSuite) {}, 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"})), func(suite *FeeTestSuite) {}, false, }, @@ -233,11 +243,11 @@ func (suite *FeeTestSuite) TestOnChanOpenAck() { }, { "previous INIT set without fee, however counterparty set fee version", // note this can only happen with incompetent or malicious counterparty chain - string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: transfertypes.Version})), + string(types.ModuleCdc.MustMarshalJSON(&types.Metadata{FeeVersion: types.Version, AppVersion: ibcmock.Version})), func(suite *FeeTestSuite) { // do the first steps without fee version, then pass the fee version as counterparty version in ChanOpenACK - suite.path.EndpointA.ChannelConfig.Version = transfertypes.Version - suite.path.EndpointB.ChannelConfig.Version = transfertypes.Version + suite.path.EndpointA.ChannelConfig.Version = ibcmock.Version + suite.path.EndpointB.ChannelConfig.Version = ibcmock.Version }, false, }, @@ -246,16 +256,26 @@ func (suite *FeeTestSuite) TestOnChanOpenAck() { for _, tc := range testCases { tc := tc suite.Run(tc.name, func() { - suite.SetupTest() + suite.SetupMockTest() suite.coordinator.SetupConnections(suite.path) + // setup mock callback + suite.chainA.GetSimApp().FeeMockModule.IBCApp.OnChanOpenAck = func( + ctx sdk.Context, portID, channelID string, counterpartyVersion string, + ) error { + if counterpartyVersion != ibcmock.Version { + return fmt.Errorf("incorrect mock version") + } + return nil + } + // malleate test case tc.malleate(suite) suite.path.EndpointA.ChanOpenInit() suite.path.EndpointB.ChanOpenTry() - 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) cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) diff --git a/testing/simapp/app.go b/testing/simapp/app.go index 45f8cbf0b1c..fd7e191ff7b 100644 --- a/testing/simapp/app.go +++ b/testing/simapp/app.go @@ -414,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, feeWithMockModule) + AddRoute(MockFeePort, feeWithMockModule) app.IBCKeeper.SetRouter(ibcRouter) @@ -564,6 +564,7 @@ func NewSimApp( // note replicate if you do not need to test core IBC or light clients. app.ScopedIBCMockKeeper = scopedIBCMockKeeper app.ScopedICAMockKeeper = scopedICAMockKeeper + app.ScopedFeeMockKeeper = scopedFeeMockKeeper return app }