From cecc48037c8326280f32805de1f39d81f8a8c4cd Mon Sep 17 00:00:00 2001 From: DongLieu Date: Mon, 23 Oct 2023 17:47:59 +0700 Subject: [PATCH] Test for GovReopenChannel --- x/interchainstaking/keeper/msg_server_test.go | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/x/interchainstaking/keeper/msg_server_test.go b/x/interchainstaking/keeper/msg_server_test.go index 7e3d8c8a0..f27cfd96e 100644 --- a/x/interchainstaking/keeper/msg_server_test.go +++ b/x/interchainstaking/keeper/msg_server_test.go @@ -1,12 +1,19 @@ package keeper_test import ( + "errors" "fmt" "time" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + + clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types" + connectiontypes "github.com/cosmos/ibc-go/v5/modules/core/03-connection/types" + channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types" + tmclienttypes "github.com/cosmos/ibc-go/v5/modules/light-clients/07-tendermint/types" "github.com/quicksilver-zone/quicksilver/utils/addressutils" icskeeper "github.com/quicksilver-zone/quicksilver/x/interchainstaking/keeper" @@ -493,3 +500,101 @@ func (suite *KeeperTestSuite) TestSignalIntent() { }) } } + +func (suite *KeeperTestSuite) TestGovReopenChannel() { + testCase := []struct { + name string + malleate func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel + expecErr error + }{ + { + name: "invalid connection id", + malleate: func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel { + return &icstypes.MsgGovReopenChannel{ + ConnectionId: "", + PortId: "", + Authority: "", + } + }, + expecErr: fmt.Errorf("unable to obtain chain id: invalid connection id, \"%s\" not found", ""), + }, + { + name: "chainID / connectsionID mismatch", + malleate: func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel { + return &icstypes.MsgGovReopenChannel{ + ConnectionId: suite.path.EndpointA.ConnectionID, + PortId: "", + Authority: "", + } + }, + expecErr: fmt.Errorf("chainID / connectionID mismatch. Connection: %s, Port: %s", "testchain2", ""), + }, + { + name: "existing active channel", + malleate: func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel { + k := suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper + ctx := suite.chainA.GetContext() + channels := suite.GetQuicksilverApp(suite.chainA).IBCKeeper.ChannelKeeper.GetAllChannels(ctx) + return &icstypes.MsgGovReopenChannel{ + ConnectionId: suite.path.EndpointA.ConnectionID, + PortId: channels[0].PortId, + Authority: sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), k.AccountKeeper.GetModuleAddress(govtypes.ModuleName)), + } + }, + expecErr: errors.New("existing active channel channel-7 for portID icacontroller-testchain2.delegate on connection connection-0 for owner testchain2.delegate: active channel already set for this owner"), + }, + { + name: "pass", + malleate: func(suite *KeeperTestSuite) *icstypes.MsgGovReopenChannel { + quicksilver := suite.GetQuicksilverApp(suite.chainA) + ctx := suite.chainA.GetContext() + connectionID := "connection-1" + portID := "icacontroller-testchain2.delegate" + channelID := "channel-9" + + version := []*connectiontypes.Version{ + {Identifier: "1", Features: []string{"ORDER_ORDERED", "ORDER_UNORDERED"}}, + } + connectionEnd := connectiontypes.ConnectionEnd{ClientId: "09-tendermint-1", State: connectiontypes.OPEN, Versions: version} + quicksilver.IBCKeeper.ConnectionKeeper.SetConnection(ctx, connectionID, connectionEnd) + + _, f := quicksilver.IBCKeeper.ConnectionKeeper.GetConnection(ctx, connectionID) + suite.True(f) + + channelSet := channeltypes.Channel{ + State: channeltypes.TRYOPEN, + Ordering: channeltypes.NONE, + Counterparty: channeltypes.NewCounterparty(portID, channelID), + ConnectionHops: []string{connectionID}, + } + quicksilver.IBCKeeper.ChannelKeeper.SetChannel(ctx, portID, channelID, channelSet) + + quicksilver.IBCKeeper.ClientKeeper.SetClientState(ctx, connectionEnd.ClientId, &tmclienttypes.ClientState{ChainId: suite.chainB.ChainID, TrustingPeriod: time.Hour, LatestHeight: clienttypes.Height{RevisionNumber: 1, RevisionHeight: 100}}) + + return &icstypes.MsgGovReopenChannel{ + ConnectionId: connectionID, + PortId: portID, + Authority: sdk.MustBech32ifyAddressBytes(sdk.GetConfig().GetBech32AccountAddrPrefix(), quicksilver.InterchainstakingKeeper.AccountKeeper.GetModuleAddress(govtypes.ModuleName)), + } + }, + expecErr: nil, + }, + } + for _, tc := range testCase { + suite.Run(tc.name, func() { + suite.SetupTest() + suite.setupTestZones() + + msg := tc.malleate(suite) + msgSrv := icskeeper.NewMsgServerImpl(*suite.GetQuicksilverApp(suite.chainA).InterchainstakingKeeper) + ctx := suite.chainA.GetContext() + + _, err := msgSrv.GovReopenChannel(ctx, msg) + if tc.expecErr != nil { + suite.Equal(tc.expecErr.Error(), err.Error()) + return + } + suite.NoError(err) + }) + } +}