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

refactor: ics29 OnChanOpenInit callback tests now use mock module #924

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the base application, it needs to be the mock module since that's the type we can change the callbacks for as done in the OnChanOpenInit test. This prevents us from hard coding certain logic within the mock module itself and rather specifying the expected behaviour from the base application within the test itself


// 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