diff --git a/.codecov.yml b/.codecov.yml index 7935dfdbd4a..88f51441f92 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -53,4 +53,5 @@ ignore: - "modules/**/**/**/*.pb.go" - "modules/**/**/**/*.pb.gw.go" - "modules/**/**/**/test_common.go" + - "testing/" - "scripts/" diff --git a/modules/apps/transfer/handler_test.go b/modules/apps/transfer/handler_test.go index 976ecb2e898..030fb1bd14c 100644 --- a/modules/apps/transfer/handler_test.go +++ b/modules/apps/transfer/handler_test.go @@ -9,7 +9,6 @@ import ( "github.com/cosmos/ibc-go/modules/apps/transfer/types" clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" - "github.com/cosmos/ibc-go/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/testing" ) @@ -31,90 +30,102 @@ func (suite *TransferTestSuite) SetupTest() { suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(2)) } +func NewTransferPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path { + path := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig.PortID = ibctesting.TransferPort + path.EndpointB.ChannelConfig.PortID = ibctesting.TransferPort + + return path +} + // constructs a send from chainA to chainB on the established channel/connection // and sends the same coin back from chainB to chainA. func (suite *TransferTestSuite) TestHandleMsgTransfer() { // setup between chainA and chainB - clientA, clientB, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB := suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) - // originalBalance := suite.chainA.App.BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), sdk.DefaultBondDenom) + path := NewTransferPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + + // originalBalance := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), sdk.DefaultBondDenom) timeoutHeight := clienttypes.NewHeight(0, 110) coinToSendToB := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) // send from chainA to chainB - msg := types.NewMsgTransfer(channelA.PortID, channelA.ID, coinToSendToB, suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), timeoutHeight, 0) + msg := types.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, coinToSendToB, suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), timeoutHeight, 0) - err := suite.coordinator.SendMsg(suite.chainA, suite.chainB, clientB, msg) + _, err := suite.chainA.SendMsgs(msg) suite.Require().NoError(err) // message committed // relay send fungibleTokenPacket := types.NewFungibleTokenPacketData(coinToSendToB.Denom, coinToSendToB.Amount.Uint64(), suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String()) - packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)}) - err = suite.coordinator.RelayPacket(suite.chainA, suite.chainB, clientA, clientB, packet, ack.Acknowledgement()) + err = path.RelayPacket(packet, ack.Acknowledgement()) suite.Require().NoError(err) // relay committed // check that voucher exists on chain B voucherDenomTrace := types.ParseDenomTrace(types.GetPrefixedDenom(packet.GetDestPort(), packet.GetDestChannel(), sdk.DefaultBondDenom)) - balance := suite.chainB.App.BankKeeper.GetBalance(suite.chainB.GetContext(), suite.chainB.SenderAccount.GetAddress(), voucherDenomTrace.IBCDenom()) + balance := suite.chainB.GetSimApp().BankKeeper.GetBalance(suite.chainB.GetContext(), suite.chainB.SenderAccount.GetAddress(), voucherDenomTrace.IBCDenom()) - coinSentFromAToB := types.GetTransferCoin(channelB.PortID, channelB.ID, sdk.DefaultBondDenom, 100) + coinSentFromAToB := types.GetTransferCoin(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, sdk.DefaultBondDenom, 100) suite.Require().Equal(coinSentFromAToB, balance) // setup between chainB to chainC - clientOnBForC, clientOnCForB, connOnBForC, connOnCForB := suite.coordinator.SetupClientConnections(suite.chainB, suite.chainC, exported.Tendermint) - channelOnBForC, channelOnCForB := suite.coordinator.CreateTransferChannels(suite.chainB, suite.chainC, connOnBForC, connOnCForB, channeltypes.UNORDERED) + // NOTE: + // pathBtoC.EndpointA = endpoint on chainB + // pathBtoC.EndpointB = endpoint on chainC + pathBtoC := NewTransferPath(suite.chainB, suite.chainC) + suite.coordinator.Setup(pathBtoC) // send from chainB to chainC - msg = types.NewMsgTransfer(channelOnBForC.PortID, channelOnBForC.ID, coinSentFromAToB, suite.chainB.SenderAccount.GetAddress().String(), suite.chainC.SenderAccount.GetAddress().String(), timeoutHeight, 0) + msg = types.NewMsgTransfer(pathBtoC.EndpointA.ChannelConfig.PortID, pathBtoC.EndpointA.ChannelID, coinSentFromAToB, suite.chainB.SenderAccount.GetAddress().String(), suite.chainC.SenderAccount.GetAddress().String(), timeoutHeight, 0) - err = suite.coordinator.SendMsg(suite.chainB, suite.chainC, clientOnCForB, msg) + _, err = suite.chainB.SendMsgs(msg) suite.Require().NoError(err) // message committed // relay send // NOTE: fungible token is prefixed with the full trace in order to verify the packet commitment - fullDenomPath := types.GetPrefixedDenom(channelOnCForB.PortID, channelOnCForB.ID, voucherDenomTrace.GetFullDenomPath()) + fullDenomPath := types.GetPrefixedDenom(pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, voucherDenomTrace.GetFullDenomPath()) fungibleTokenPacket = types.NewFungibleTokenPacketData(voucherDenomTrace.GetFullDenomPath(), coinSentFromAToB.Amount.Uint64(), suite.chainB.SenderAccount.GetAddress().String(), suite.chainC.SenderAccount.GetAddress().String()) - packet = channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, channelOnBForC.PortID, channelOnBForC.ID, channelOnCForB.PortID, channelOnCForB.ID, timeoutHeight, 0) - err = suite.coordinator.RelayPacket(suite.chainB, suite.chainC, clientOnBForC, clientOnCForB, packet, ack.Acknowledgement()) + packet = channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, pathBtoC.EndpointA.ChannelConfig.PortID, pathBtoC.EndpointA.ChannelID, pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, timeoutHeight, 0) + err = pathBtoC.RelayPacket(packet, ack.Acknowledgement()) suite.Require().NoError(err) // relay committed coinSentFromBToC := sdk.NewInt64Coin(types.ParseDenomTrace(fullDenomPath).IBCDenom(), 100) - balance = suite.chainC.App.BankKeeper.GetBalance(suite.chainC.GetContext(), suite.chainC.SenderAccount.GetAddress(), coinSentFromBToC.Denom) + balance = suite.chainC.GetSimApp().BankKeeper.GetBalance(suite.chainC.GetContext(), suite.chainC.SenderAccount.GetAddress(), coinSentFromBToC.Denom) // check that the balance is updated on chainC suite.Require().Equal(coinSentFromBToC, balance) // check that balance on chain B is empty - balance = suite.chainB.App.BankKeeper.GetBalance(suite.chainB.GetContext(), suite.chainB.SenderAccount.GetAddress(), coinSentFromBToC.Denom) + balance = suite.chainB.GetSimApp().BankKeeper.GetBalance(suite.chainB.GetContext(), suite.chainB.SenderAccount.GetAddress(), coinSentFromBToC.Denom) suite.Require().Zero(balance.Amount.Int64()) // send from chainC back to chainB - msg = types.NewMsgTransfer(channelOnCForB.PortID, channelOnCForB.ID, coinSentFromBToC, suite.chainC.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), timeoutHeight, 0) + msg = types.NewMsgTransfer(pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, coinSentFromBToC, suite.chainC.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String(), timeoutHeight, 0) - err = suite.coordinator.SendMsg(suite.chainC, suite.chainB, clientOnBForC, msg) + _, err = suite.chainC.SendMsgs(msg) suite.Require().NoError(err) // message committed // relay send // NOTE: fungible token is prefixed with the full trace in order to verify the packet commitment fungibleTokenPacket = types.NewFungibleTokenPacketData(fullDenomPath, coinSentFromBToC.Amount.Uint64(), suite.chainC.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String()) - packet = channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, channelOnCForB.PortID, channelOnCForB.ID, channelOnBForC.PortID, channelOnBForC.ID, timeoutHeight, 0) - err = suite.coordinator.RelayPacket(suite.chainC, suite.chainB, clientOnCForB, clientOnBForC, packet, ack.Acknowledgement()) + packet = channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, pathBtoC.EndpointB.ChannelConfig.PortID, pathBtoC.EndpointB.ChannelID, pathBtoC.EndpointA.ChannelConfig.PortID, pathBtoC.EndpointA.ChannelID, timeoutHeight, 0) + err = pathBtoC.RelayPacket(packet, ack.Acknowledgement()) suite.Require().NoError(err) // relay committed - balance = suite.chainB.App.BankKeeper.GetBalance(suite.chainB.GetContext(), suite.chainB.SenderAccount.GetAddress(), coinSentFromAToB.Denom) + balance = suite.chainB.GetSimApp().BankKeeper.GetBalance(suite.chainB.GetContext(), suite.chainB.SenderAccount.GetAddress(), coinSentFromAToB.Denom) // check that the balance on chainA returned back to the original state suite.Require().Equal(coinSentFromAToB, balance) // check that module account escrow address is empty escrowAddress := types.GetEscrowAddress(packet.GetDestPort(), packet.GetDestChannel()) - balance = suite.chainB.App.BankKeeper.GetBalance(suite.chainB.GetContext(), escrowAddress, sdk.DefaultBondDenom) + balance = suite.chainB.GetSimApp().BankKeeper.GetBalance(suite.chainB.GetContext(), escrowAddress, sdk.DefaultBondDenom) suite.Require().Equal(sdk.NewCoin(sdk.DefaultBondDenom, sdk.ZeroInt()), balance) // check that balance on chain B is empty - balance = suite.chainC.App.BankKeeper.GetBalance(suite.chainC.GetContext(), suite.chainC.SenderAccount.GetAddress(), voucherDenomTrace.IBCDenom()) + balance = suite.chainC.GetSimApp().BankKeeper.GetBalance(suite.chainC.GetContext(), suite.chainC.SenderAccount.GetAddress(), voucherDenomTrace.IBCDenom()) suite.Require().Zero(balance.Amount.Int64()) } diff --git a/modules/apps/transfer/keeper/genesis_test.go b/modules/apps/transfer/keeper/genesis_test.go index 7dfffbdb7ca..19e5dfe4a18 100644 --- a/modules/apps/transfer/keeper/genesis_test.go +++ b/modules/apps/transfer/keeper/genesis_test.go @@ -25,15 +25,15 @@ func (suite *KeeperTestSuite) TestGenesis() { Path: path, } traces = append(types.Traces{denomTrace}, traces...) - suite.chainA.App.TransferKeeper.SetDenomTrace(suite.chainA.GetContext(), denomTrace) + suite.chainA.GetSimApp().TransferKeeper.SetDenomTrace(suite.chainA.GetContext(), denomTrace) } - genesis := suite.chainA.App.TransferKeeper.ExportGenesis(suite.chainA.GetContext()) + genesis := suite.chainA.GetSimApp().TransferKeeper.ExportGenesis(suite.chainA.GetContext()) suite.Require().Equal(types.PortID, genesis.PortId) suite.Require().Equal(traces.Sort(), genesis.DenomTraces) suite.Require().NotPanics(func() { - suite.chainA.App.TransferKeeper.InitGenesis(suite.chainA.GetContext(), *genesis) + suite.chainA.GetSimApp().TransferKeeper.InitGenesis(suite.chainA.GetContext(), *genesis) }) } diff --git a/modules/apps/transfer/keeper/grpc_query_test.go b/modules/apps/transfer/keeper/grpc_query_test.go index ca98bd771c0..d5ffe6920ee 100644 --- a/modules/apps/transfer/keeper/grpc_query_test.go +++ b/modules/apps/transfer/keeper/grpc_query_test.go @@ -44,7 +44,7 @@ func (suite *KeeperTestSuite) TestQueryDenomTrace() { func() { expTrace.Path = "transfer/channelToA/transfer/channelToB" expTrace.BaseDenom = "uatom" - suite.chainA.App.TransferKeeper.SetDenomTrace(suite.chainA.GetContext(), expTrace) + suite.chainA.GetSimApp().TransferKeeper.SetDenomTrace(suite.chainA.GetContext(), expTrace) req = &types.QueryDenomTraceRequest{ Hash: expTrace.Hash().String(), @@ -100,7 +100,7 @@ func (suite *KeeperTestSuite) TestQueryDenomTraces() { expTraces = append(expTraces, types.DenomTrace{Path: "transfer/channelToA/transfer/channelToB", BaseDenom: "uatom"}) for _, trace := range expTraces { - suite.chainA.App.TransferKeeper.SetDenomTrace(suite.chainA.GetContext(), trace) + suite.chainA.GetSimApp().TransferKeeper.SetDenomTrace(suite.chainA.GetContext(), trace) } req = &types.QueryDenomTracesRequest{ diff --git a/modules/apps/transfer/keeper/keeper_test.go b/modules/apps/transfer/keeper/keeper_test.go index 8c90f186ea3..de3902df669 100644 --- a/modules/apps/transfer/keeper/keeper_test.go +++ b/modules/apps/transfer/keeper/keeper_test.go @@ -31,15 +31,23 @@ func (suite *KeeperTestSuite) SetupTest() { suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(2)) - queryHelper := baseapp.NewQueryServerTestHelper(suite.chainA.GetContext(), suite.chainA.App.InterfaceRegistry()) - types.RegisterQueryServer(queryHelper, suite.chainA.App.TransferKeeper) + queryHelper := baseapp.NewQueryServerTestHelper(suite.chainA.GetContext(), suite.chainA.GetSimApp().InterfaceRegistry()) + types.RegisterQueryServer(queryHelper, suite.chainA.GetSimApp().TransferKeeper) suite.queryClient = types.NewQueryClient(queryHelper) } +func NewTransferPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path { + path := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig.PortID = ibctesting.TransferPort + path.EndpointB.ChannelConfig.PortID = ibctesting.TransferPort + + return path +} + func (suite *KeeperTestSuite) TestGetTransferAccount() { expectedMaccAddr := sdk.AccAddress(crypto.AddressHash([]byte(types.ModuleName))) - macc := suite.chainA.App.TransferKeeper.GetTransferAccount(suite.chainA.GetContext()) + macc := suite.chainA.GetSimApp().TransferKeeper.GetTransferAccount(suite.chainA.GetContext()) suite.Require().NotNil(macc) suite.Require().Equal(types.ModuleName, macc.GetName()) diff --git a/modules/apps/transfer/keeper/mbt_relay_test.go b/modules/apps/transfer/keeper/mbt_relay_test.go index 4130845c8a7..db425f24507 100644 --- a/modules/apps/transfer/keeper/mbt_relay_test.go +++ b/modules/apps/transfer/keeper/mbt_relay_test.go @@ -18,7 +18,6 @@ import ( "github.com/cosmos/ibc-go/modules/apps/transfer/types" clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" - "github.com/cosmos/ibc-go/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/testing" ) @@ -251,10 +250,10 @@ func (bank *Bank) NonZeroString() string { // Construct a bank out of the chain bank func BankOfChain(chain *ibctesting.TestChain) Bank { bank := MakeBank() - chain.App.BankKeeper.IterateAllBalances(chain.GetContext(), func(address sdk.AccAddress, coin sdk.Coin) (stop bool) { + chain.GetSimApp().BankKeeper.IterateAllBalances(chain.GetContext(), func(address sdk.AccAddress, coin sdk.Coin) (stop bool) { fullDenom := coin.Denom if strings.HasPrefix(coin.Denom, "ibc/") { - fullDenom, _ = chain.App.TransferKeeper.DenomPathFromHash(chain.GetContext(), coin.Denom) + fullDenom, _ = chain.GetSimApp().TransferKeeper.DenomPathFromHash(chain.GetContext(), coin.Denom) } bank.SetBalance(address.String(), fullDenom, coin.Amount) return false @@ -295,18 +294,18 @@ func (suite *KeeperTestSuite) TestModelBasedRelay() { } suite.SetupTest() - _, _, connAB, connBA := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - _, _, connBC, connCB := suite.coordinator.SetupClientConnections(suite.chainB, suite.chainC, exported.Tendermint) - suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connAB, connBA, channeltypes.UNORDERED) - suite.coordinator.CreateTransferChannels(suite.chainB, suite.chainC, connBC, connCB, channeltypes.UNORDERED) + pathAtoB := NewTransferPath(suite.chainA, suite.chainB) + pathBtoC := NewTransferPath(suite.chainB, suite.chainC) + suite.coordinator.Setup(pathAtoB) + suite.coordinator.Setup(pathBtoC) for i, tlaTc := range tlaTestCases { tc := OnRecvPacketTestCaseFromTla(tlaTc) registerDenom := func() { denomTrace := types.ParseDenomTrace(tc.packet.Data.Denom) traceHash := denomTrace.Hash() - if !suite.chainB.App.TransferKeeper.HasDenomTrace(suite.chainB.GetContext(), traceHash) { - suite.chainB.App.TransferKeeper.SetDenomTrace(suite.chainB.GetContext(), denomTrace) + if !suite.chainB.GetSimApp().TransferKeeper.HasDenomTrace(suite.chainB.GetContext(), traceHash) { + suite.chainB.GetSimApp().TransferKeeper.SetDenomTrace(suite.chainB.GetContext(), denomTrace) } } @@ -334,7 +333,7 @@ func (suite *KeeperTestSuite) TestModelBasedRelay() { denom := denomTrace.IBCDenom() err = sdk.ValidateDenom(denom) if err == nil { - err = suite.chainB.App.TransferKeeper.SendTransfer( + err = suite.chainB.GetSimApp().TransferKeeper.SendTransfer( suite.chainB.GetContext(), tc.packet.SourcePort, tc.packet.SourceChannel, @@ -345,17 +344,17 @@ func (suite *KeeperTestSuite) TestModelBasedRelay() { 0) } case "OnRecvPacket": - err = suite.chainB.App.TransferKeeper.OnRecvPacket(suite.chainB.GetContext(), packet, tc.packet.Data) + err = suite.chainB.GetSimApp().TransferKeeper.OnRecvPacket(suite.chainB.GetContext(), packet, tc.packet.Data) case "OnTimeoutPacket": registerDenom() - err = suite.chainB.App.TransferKeeper.OnTimeoutPacket(suite.chainB.GetContext(), packet, tc.packet.Data) + err = suite.chainB.GetSimApp().TransferKeeper.OnTimeoutPacket(suite.chainB.GetContext(), packet, tc.packet.Data) case "OnRecvAcknowledgementResult": - err = suite.chainB.App.TransferKeeper.OnAcknowledgementPacket( + err = suite.chainB.GetSimApp().TransferKeeper.OnAcknowledgementPacket( suite.chainB.GetContext(), packet, tc.packet.Data, channeltypes.NewResultAcknowledgement(nil)) case "OnRecvAcknowledgementError": registerDenom() - err = suite.chainB.App.TransferKeeper.OnAcknowledgementPacket( + err = suite.chainB.GetSimApp().TransferKeeper.OnAcknowledgementPacket( suite.chainB.GetContext(), packet, tc.packet.Data, channeltypes.NewErrorAcknowledgement("MBT Error Acknowledgement")) default: diff --git a/modules/apps/transfer/keeper/params_test.go b/modules/apps/transfer/keeper/params_test.go index ac680a41ef3..a5d78005836 100644 --- a/modules/apps/transfer/keeper/params_test.go +++ b/modules/apps/transfer/keeper/params_test.go @@ -5,11 +5,11 @@ import "github.com/cosmos/ibc-go/modules/apps/transfer/types" func (suite *KeeperTestSuite) TestParams() { expParams := types.DefaultParams() - params := suite.chainA.App.TransferKeeper.GetParams(suite.chainA.GetContext()) + params := suite.chainA.GetSimApp().TransferKeeper.GetParams(suite.chainA.GetContext()) suite.Require().Equal(expParams, params) expParams.SendEnabled = false - suite.chainA.App.TransferKeeper.SetParams(suite.chainA.GetContext(), expParams) - params = suite.chainA.App.TransferKeeper.GetParams(suite.chainA.GetContext()) + suite.chainA.GetSimApp().TransferKeeper.SetParams(suite.chainA.GetContext(), expParams) + params = suite.chainA.GetSimApp().TransferKeeper.GetParams(suite.chainA.GetContext()) suite.Require().Equal(expParams, params) } diff --git a/modules/apps/transfer/keeper/relay_test.go b/modules/apps/transfer/keeper/relay_test.go index 020b132cfda..4c383f0948f 100644 --- a/modules/apps/transfer/keeper/relay_test.go +++ b/modules/apps/transfer/keeper/relay_test.go @@ -10,7 +10,6 @@ import ( clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" host "github.com/cosmos/ibc-go/modules/core/24-host" - "github.com/cosmos/ibc-go/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/testing" ) @@ -18,9 +17,9 @@ import ( // chainA and coin that orignate on chainB func (suite *KeeperTestSuite) TestSendTransfer() { var ( - amount sdk.Coin - channelA, channelB ibctesting.TestChannel - err error + amount sdk.Coin + path *ibctesting.Path + err error ) testCases := []struct { @@ -31,37 +30,33 @@ func (suite *KeeperTestSuite) TestSendTransfer() { }{ {"successful transfer from source chain", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB = suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) + suite.coordinator.CreateTransferChannels(path) amount = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) }, true, true}, {"successful transfer with coin from counterparty chain", func() { // send coin from chainA back to chainB - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB = suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) - amount = types.GetTransferCoin(channelA.PortID, channelA.ID, sdk.DefaultBondDenom, 100) + suite.coordinator.CreateTransferChannels(path) + amount = types.GetTransferCoin(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, sdk.DefaultBondDenom, 100) }, false, true}, {"source channel not found", func() { // channel references wrong ID - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB = suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) - channelA.ID = ibctesting.InvalidID + suite.coordinator.CreateTransferChannels(path) + path.EndpointA.ChannelID = ibctesting.InvalidID amount = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) }, true, false}, {"next seq send not found", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA = suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) - channelB = suite.chainB.NextTestChannel(connB, ibctesting.TransferPort) + path.EndpointA.ChannelID = "channel-0" + path.EndpointB.ChannelID = "channel-0" // manually create channel so next seq send is never set - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainA.GetContext(), - channelA.PortID, channelA.ID, - channeltypes.NewChannel(channeltypes.OPEN, channeltypes.ORDERED, channeltypes.NewCounterparty(channelB.PortID, channelB.ID), []string{connA.ID}, ibctesting.DefaultChannelVersion), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + channeltypes.NewChannel(channeltypes.OPEN, channeltypes.ORDERED, channeltypes.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID), []string{path.EndpointA.ConnectionID}, ibctesting.DefaultChannelVersion), ) - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) amount = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) }, true, false}, @@ -69,25 +64,22 @@ func (suite *KeeperTestSuite) TestSendTransfer() { // - source chain {"send coin failed", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB = suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) + suite.coordinator.CreateTransferChannels(path) amount = sdk.NewCoin("randomdenom", sdk.NewInt(100)) }, true, false}, // - receiving chain {"send from module account failed", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB = suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) - amount = types.GetTransferCoin(channelA.PortID, channelA.ID, " randomdenom", 100) + suite.coordinator.CreateTransferChannels(path) + amount = types.GetTransferCoin(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, " randomdenom", 100) }, false, false}, {"channel capability not found", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB = suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) - cap := suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.CreateTransferChannels(path) + cap := suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) // Release channel capability - suite.chainA.App.ScopedTransferKeeper.ReleaseCapability(suite.chainA.GetContext(), cap) + suite.chainA.GetSimApp().ScopedTransferKeeper.ReleaseCapability(suite.chainA.GetContext(), cap) amount = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) }, true, false}, } @@ -97,31 +89,35 @@ func (suite *KeeperTestSuite) TestSendTransfer() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset + path = NewTransferPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) tc.malleate() if !tc.sendFromSource { // send coin from chainB to chainA coinFromBToA := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) - transferMsg := types.NewMsgTransfer(channelB.PortID, channelB.ID, coinFromBToA, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String(), clienttypes.NewHeight(0, 110), 0) - err = suite.coordinator.SendMsg(suite.chainB, suite.chainA, channelA.ClientID, transferMsg) + transferMsg := types.NewMsgTransfer(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, coinFromBToA, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String(), clienttypes.NewHeight(0, 110), 0) + _, err = suite.chainB.SendMsgs(transferMsg) suite.Require().NoError(err) // message committed // receive coin on chainA from chainB fungibleTokenPacket := types.NewFungibleTokenPacketData(coinFromBToA.Denom, coinFromBToA.Amount.Uint64(), suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String()) - packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, channelB.PortID, channelB.ID, channelA.PortID, channelA.ID, clienttypes.NewHeight(0, 110), 0) + packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, clienttypes.NewHeight(0, 110), 0) // get proof of packet commitment from chainB + err = path.EndpointA.UpdateClient() + suite.Require().NoError(err) packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - proof, proofHeight := suite.chainB.QueryProof(packetKey) + proof, proofHeight := path.EndpointB.QueryProof(packetKey) recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, suite.chainA.SenderAccount.GetAddress().String()) - err = suite.coordinator.SendMsg(suite.chainA, suite.chainB, channelB.ClientID, recvMsg) + _, err = suite.chainA.SendMsgs(recvMsg) suite.Require().NoError(err) // message committed } - err = suite.chainA.App.TransferKeeper.SendTransfer( - suite.chainA.GetContext(), channelA.PortID, channelA.ID, amount, + err = suite.chainA.GetSimApp().TransferKeeper.SendTransfer( + suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, amount, suite.chainA.SenderAccount.GetAddress(), suite.chainB.SenderAccount.GetAddress().String(), clienttypes.NewHeight(0, 110), 0, ) @@ -140,10 +136,9 @@ func (suite *KeeperTestSuite) TestSendTransfer() { // malleate function allows for testing invalid cases. func (suite *KeeperTestSuite) TestOnRecvPacket() { var ( - channelA, channelB ibctesting.TestChannel - trace types.DenomTrace - amount sdk.Int - receiver string + trace types.DenomTrace + amount sdk.Int + receiver string ) testCases := []struct { @@ -180,8 +175,8 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset - clientA, clientB, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB = suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) + path := NewTransferPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) receiver = suite.chainB.SenderAccount.GetAddress().String() // must be explicitly changed in malleate amount = sdk.NewInt(100) // must be explicitly changed in malleate @@ -190,36 +185,36 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() { if tc.recvIsSource { // send coin from chainB to chainA, receive them, acknowledge them, and send back to chainB coinFromBToA := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) - transferMsg := types.NewMsgTransfer(channelB.PortID, channelB.ID, coinFromBToA, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String(), clienttypes.NewHeight(0, 110), 0) - err := suite.coordinator.SendMsg(suite.chainB, suite.chainA, channelA.ClientID, transferMsg) + transferMsg := types.NewMsgTransfer(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, coinFromBToA, suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String(), clienttypes.NewHeight(0, 110), 0) + _, err := suite.chainB.SendMsgs(transferMsg) suite.Require().NoError(err) // message committed // relay send packet fungibleTokenPacket := types.NewFungibleTokenPacketData(coinFromBToA.Denom, coinFromBToA.Amount.Uint64(), suite.chainB.SenderAccount.GetAddress().String(), suite.chainA.SenderAccount.GetAddress().String()) - packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, channelB.PortID, channelB.ID, channelA.PortID, channelA.ID, clienttypes.NewHeight(0, 110), 0) + packet := channeltypes.NewPacket(fungibleTokenPacket.GetBytes(), 1, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, clienttypes.NewHeight(0, 110), 0) ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)}) - err = suite.coordinator.RelayPacket(suite.chainB, suite.chainA, clientB, clientA, packet, ack.Acknowledgement()) + err = path.RelayPacket(packet, ack.Acknowledgement()) suite.Require().NoError(err) // relay committed seq++ // NOTE: trace must be explicitly changed in malleate to test invalid cases - trace = types.ParseDenomTrace(types.GetPrefixedDenom(channelA.PortID, channelA.ID, sdk.DefaultBondDenom)) + trace = types.ParseDenomTrace(types.GetPrefixedDenom(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, sdk.DefaultBondDenom)) } else { trace = types.ParseDenomTrace(sdk.DefaultBondDenom) } // send coin from chainA to chainB - transferMsg := types.NewMsgTransfer(channelA.PortID, channelA.ID, sdk.NewCoin(trace.IBCDenom(), amount), suite.chainA.SenderAccount.GetAddress().String(), receiver, clienttypes.NewHeight(0, 110), 0) - err := suite.coordinator.SendMsg(suite.chainA, suite.chainB, channelB.ClientID, transferMsg) + transferMsg := types.NewMsgTransfer(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, sdk.NewCoin(trace.IBCDenom(), amount), suite.chainA.SenderAccount.GetAddress().String(), receiver, clienttypes.NewHeight(0, 110), 0) + _, err := suite.chainA.SendMsgs(transferMsg) suite.Require().NoError(err) // message committed tc.malleate() data := types.NewFungibleTokenPacketData(trace.GetFullDenomPath(), amount.Uint64(), suite.chainA.SenderAccount.GetAddress().String(), receiver) - packet := channeltypes.NewPacket(data.GetBytes(), seq, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.NewHeight(0, 100), 0) + packet := channeltypes.NewPacket(data.GetBytes(), seq, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0) - err = suite.chainB.App.TransferKeeper.OnRecvPacket(suite.chainB.GetContext(), packet, data) + err = suite.chainB.GetSimApp().TransferKeeper.OnRecvPacket(suite.chainB.GetContext(), packet, data) if tc.expPass { suite.Require().NoError(err) @@ -238,10 +233,9 @@ func (suite *KeeperTestSuite) TestOnAcknowledgementPacket() { var ( successAck = channeltypes.NewResultAcknowledgement([]byte{byte(1)}) failedAck = channeltypes.NewErrorAcknowledgement("failed packet transfer") - - channelA, channelB ibctesting.TestChannel - trace types.DenomTrace - amount sdk.Int + trace types.DenomTrace + amount sdk.Int + path *ibctesting.Path ) testCases := []struct { @@ -252,14 +246,14 @@ func (suite *KeeperTestSuite) TestOnAcknowledgementPacket() { expPass bool }{ {"success ack causes no-op", successAck, func() { - trace = types.ParseDenomTrace(types.GetPrefixedDenom(channelB.PortID, channelB.ID, sdk.DefaultBondDenom)) + trace = types.ParseDenomTrace(types.GetPrefixedDenom(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, sdk.DefaultBondDenom)) }, true, true}, {"successful refund from source chain", failedAck, func() { - escrow := types.GetEscrowAddress(channelA.PortID, channelA.ID) + escrow := types.GetEscrowAddress(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) trace = types.ParseDenomTrace(sdk.DefaultBondDenom) coin := sdk.NewCoin(sdk.DefaultBondDenom, amount) - suite.Require().NoError(simapp.FundAccount(suite.chainA.App, suite.chainA.GetContext(), escrow, sdk.NewCoins(coin))) + suite.Require().NoError(simapp.FundAccount(suite.chainA.GetSimApp(), suite.chainA.GetContext(), escrow, sdk.NewCoins(coin))) }, false, true}, {"unsuccessful refund from source", failedAck, func() { @@ -267,11 +261,11 @@ func (suite *KeeperTestSuite) TestOnAcknowledgementPacket() { }, false, false}, {"successful refund from with coin from external chain", failedAck, func() { - escrow := types.GetEscrowAddress(channelA.PortID, channelA.ID) - trace = types.ParseDenomTrace(types.GetPrefixedDenom(channelA.PortID, channelA.ID, sdk.DefaultBondDenom)) + escrow := types.GetEscrowAddress(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + trace = types.ParseDenomTrace(types.GetPrefixedDenom(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, sdk.DefaultBondDenom)) coin := sdk.NewCoin(trace.IBCDenom(), amount) - suite.Require().NoError(simapp.FundAccount(suite.chainA.App, suite.chainA.GetContext(), escrow, sdk.NewCoins(coin))) + suite.Require().NoError(simapp.FundAccount(suite.chainA.GetSimApp(), suite.chainA.GetContext(), escrow, sdk.NewCoins(coin))) }, false, true}, } @@ -280,20 +274,21 @@ func (suite *KeeperTestSuite) TestOnAcknowledgementPacket() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset - _, _, _, _, channelA, channelB = suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + path = NewTransferPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) amount = sdk.NewInt(100) // must be explicitly changed tc.malleate() data := types.NewFungibleTokenPacketData(trace.GetFullDenomPath(), amount.Uint64(), suite.chainA.SenderAccount.GetAddress().String(), suite.chainB.SenderAccount.GetAddress().String()) - packet := channeltypes.NewPacket(data.GetBytes(), 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.NewHeight(0, 100), 0) + packet := channeltypes.NewPacket(data.GetBytes(), 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0) - preCoin := suite.chainA.App.BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), trace.IBCDenom()) + preCoin := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), trace.IBCDenom()) - err := suite.chainA.App.TransferKeeper.OnAcknowledgementPacket(suite.chainA.GetContext(), packet, data, tc.ack) + err := suite.chainA.GetSimApp().TransferKeeper.OnAcknowledgementPacket(suite.chainA.GetContext(), packet, data, tc.ack) if tc.expPass { suite.Require().NoError(err) - postCoin := suite.chainA.App.BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), trace.IBCDenom()) + postCoin := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), trace.IBCDenom()) deltaAmount := postCoin.Amount.Sub(preCoin.Amount) if tc.success { @@ -315,10 +310,10 @@ func (suite *KeeperTestSuite) TestOnAcknowledgementPacket() { // so the refunds are occurring on chainA. func (suite *KeeperTestSuite) TestOnTimeoutPacket() { var ( - channelA, channelB ibctesting.TestChannel - trace types.DenomTrace - amount sdk.Int - sender string + trace types.DenomTrace + path *ibctesting.Path + amount sdk.Int + sender string ) testCases := []struct { @@ -328,19 +323,19 @@ func (suite *KeeperTestSuite) TestOnTimeoutPacket() { }{ {"successful timeout from sender as source chain", func() { - escrow := types.GetEscrowAddress(channelA.PortID, channelA.ID) + escrow := types.GetEscrowAddress(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) trace = types.ParseDenomTrace(sdk.DefaultBondDenom) coin := sdk.NewCoin(trace.IBCDenom(), amount) - suite.Require().NoError(simapp.FundAccount(suite.chainA.App, suite.chainA.GetContext(), escrow, sdk.NewCoins(coin))) + suite.Require().NoError(simapp.FundAccount(suite.chainA.GetSimApp(), suite.chainA.GetContext(), escrow, sdk.NewCoins(coin))) }, true}, {"successful timeout from external chain", func() { - escrow := types.GetEscrowAddress(channelA.PortID, channelA.ID) - trace = types.ParseDenomTrace(types.GetPrefixedDenom(channelA.PortID, channelA.ID, sdk.DefaultBondDenom)) + escrow := types.GetEscrowAddress(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + trace = types.ParseDenomTrace(types.GetPrefixedDenom(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, sdk.DefaultBondDenom)) coin := sdk.NewCoin(trace.IBCDenom(), amount) - suite.Require().NoError(simapp.FundAccount(suite.chainA.App, suite.chainA.GetContext(), escrow, sdk.NewCoins(coin))) + suite.Require().NoError(simapp.FundAccount(suite.chainA.GetSimApp(), suite.chainA.GetContext(), escrow, sdk.NewCoins(coin))) }, true}, {"no balance for coin denom", func() { @@ -352,7 +347,7 @@ func (suite *KeeperTestSuite) TestOnTimeoutPacket() { }, false}, {"mint failed", func() { - trace = types.ParseDenomTrace(types.GetPrefixedDenom(channelA.PortID, channelA.ID, sdk.DefaultBondDenom)) + trace = types.ParseDenomTrace(types.GetPrefixedDenom(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, sdk.DefaultBondDenom)) amount = sdk.OneInt() sender = "invalid address" }, false}, @@ -364,21 +359,21 @@ func (suite *KeeperTestSuite) TestOnTimeoutPacket() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB = suite.coordinator.CreateTransferChannels(suite.chainA, suite.chainB, connA, connB, channeltypes.UNORDERED) + path = NewTransferPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) amount = sdk.NewInt(100) // must be explicitly changed sender = suite.chainA.SenderAccount.GetAddress().String() tc.malleate() data := types.NewFungibleTokenPacketData(trace.GetFullDenomPath(), amount.Uint64(), sender, suite.chainB.SenderAccount.GetAddress().String()) - packet := channeltypes.NewPacket(data.GetBytes(), 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.NewHeight(0, 100), 0) + packet := channeltypes.NewPacket(data.GetBytes(), 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0) - preCoin := suite.chainA.App.BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), trace.IBCDenom()) + preCoin := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), trace.IBCDenom()) - err := suite.chainA.App.TransferKeeper.OnTimeoutPacket(suite.chainA.GetContext(), packet, data) + err := suite.chainA.GetSimApp().TransferKeeper.OnTimeoutPacket(suite.chainA.GetContext(), packet, data) - postCoin := suite.chainA.App.BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), trace.IBCDenom()) + postCoin := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), trace.IBCDenom()) deltaAmount := postCoin.Amount.Sub(preCoin.Amount) if tc.expPass { diff --git a/modules/apps/transfer/module_test.go b/modules/apps/transfer/module_test.go index 5387621330c..63d610de84a 100644 --- a/modules/apps/transfer/module_test.go +++ b/modules/apps/transfer/module_test.go @@ -7,16 +7,14 @@ import ( "github.com/cosmos/ibc-go/modules/apps/transfer/types" channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" host "github.com/cosmos/ibc-go/modules/core/24-host" - "github.com/cosmos/ibc-go/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/testing" ) func (suite *TransferTestSuite) TestOnChanOpenInit() { var ( - channel *channeltypes.Channel - testChannel ibctesting.TestChannel - connA *ibctesting.TestConnection - chanCap *capabilitytypes.Capability + channel *channeltypes.Channel + path *ibctesting.Path + chanCap *capabilitytypes.Capability ) testCases := []struct { @@ -30,7 +28,7 @@ func (suite *TransferTestSuite) TestOnChanOpenInit() { }, { "max channels reached", func() { - testChannel.ID = channeltypes.FormatChannelIdentifier(math.MaxUint32 + 1) + path.EndpointA.ChannelID = channeltypes.FormatChannelIdentifier(math.MaxUint32 + 1) }, false, }, { @@ -40,7 +38,7 @@ func (suite *TransferTestSuite) TestOnChanOpenInit() { }, { "invalid port ID", func() { - testChannel = suite.chainA.NextTestChannel(connA, ibctesting.MockPort) + path.EndpointA.ChannelConfig.PortID = ibctesting.MockPort }, false, }, { @@ -50,7 +48,7 @@ func (suite *TransferTestSuite) TestOnChanOpenInit() { }, { "capability already claimed", func() { - err := suite.chainA.App.ScopedTransferKeeper.ClaimCapability(suite.chainA.GetContext(), chanCap, host.ChannelCapabilityPath(testChannel.PortID, testChannel.ID)) + err := suite.chainA.GetSimApp().ScopedTransferKeeper.ClaimCapability(suite.chainA.GetContext(), chanCap, host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) suite.Require().NoError(err) }, false, }, @@ -61,31 +59,32 @@ func (suite *TransferTestSuite) TestOnChanOpenInit() { suite.Run(tc.name, func() { suite.SetupTest() // reset + path = NewTransferPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + path.EndpointA.ChannelID = ibctesting.FirstChannelID - _, _, connA, _ = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - testChannel = suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) - counterparty := channeltypes.NewCounterparty(testChannel.PortID, testChannel.ID) + counterparty := channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) channel = &channeltypes.Channel{ State: channeltypes.INIT, Ordering: channeltypes.UNORDERED, Counterparty: counterparty, - ConnectionHops: []string{connA.ID}, + ConnectionHops: []string{path.EndpointA.ConnectionID}, Version: types.Version, } - module, _, err := suite.chainA.App.IBCKeeper.PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort) + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort) suite.Require().NoError(err) - chanCap, err = suite.chainA.App.ScopedIBCKeeper.NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(ibctesting.TransferPort, testChannel.ID)) + chanCap, err = suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(ibctesting.TransferPort, path.EndpointA.ChannelID)) suite.Require().NoError(err) - cbs, ok := suite.chainA.App.IBCKeeper.Router.GetRoute(module) + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) suite.Require().True(ok) tc.malleate() // explicitly change fields in channel and testChannel err = cbs.OnChanOpenInit(suite.chainA.GetContext(), channel.Ordering, channel.GetConnectionHops(), - testChannel.PortID, testChannel.ID, chanCap, channel.Counterparty, channel.GetVersion(), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, chanCap, channel.Counterparty, channel.GetVersion(), ) if tc.expPass { @@ -101,9 +100,8 @@ func (suite *TransferTestSuite) TestOnChanOpenInit() { func (suite *TransferTestSuite) TestOnChanOpenTry() { var ( channel *channeltypes.Channel - testChannel ibctesting.TestChannel - connA *ibctesting.TestConnection chanCap *capabilitytypes.Capability + path *ibctesting.Path counterpartyVersion string ) @@ -118,12 +116,12 @@ func (suite *TransferTestSuite) TestOnChanOpenTry() { }, { "max channels reached", func() { - testChannel.ID = channeltypes.FormatChannelIdentifier(math.MaxUint32 + 1) + path.EndpointA.ChannelID = channeltypes.FormatChannelIdentifier(math.MaxUint32 + 1) }, false, }, { "capability already claimed in INIT should pass", func() { - err := suite.chainA.App.ScopedTransferKeeper.ClaimCapability(suite.chainA.GetContext(), chanCap, host.ChannelCapabilityPath(testChannel.PortID, testChannel.ID)) + err := suite.chainA.GetSimApp().ScopedTransferKeeper.ClaimCapability(suite.chainA.GetContext(), chanCap, host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) suite.Require().NoError(err) }, true, }, @@ -134,7 +132,7 @@ func (suite *TransferTestSuite) TestOnChanOpenTry() { }, { "invalid port ID", func() { - testChannel = suite.chainA.NextTestChannel(connA, ibctesting.MockPort) + path.EndpointA.ChannelConfig.PortID = ibctesting.MockPort }, false, }, { @@ -155,31 +153,33 @@ func (suite *TransferTestSuite) TestOnChanOpenTry() { suite.Run(tc.name, func() { suite.SetupTest() // reset - _, _, connA, _ = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - testChannel = suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) - counterparty := channeltypes.NewCounterparty(testChannel.PortID, testChannel.ID) + path = NewTransferPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + path.EndpointA.ChannelID = ibctesting.FirstChannelID + + counterparty := channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) channel = &channeltypes.Channel{ State: channeltypes.TRYOPEN, Ordering: channeltypes.UNORDERED, Counterparty: counterparty, - ConnectionHops: []string{connA.ID}, + ConnectionHops: []string{path.EndpointA.ConnectionID}, Version: types.Version, } counterpartyVersion = types.Version - module, _, err := suite.chainA.App.IBCKeeper.PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort) + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort) suite.Require().NoError(err) - chanCap, err = suite.chainA.App.ScopedIBCKeeper.NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(ibctesting.TransferPort, testChannel.ID)) + chanCap, err = suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(ibctesting.TransferPort, path.EndpointA.ChannelID)) suite.Require().NoError(err) - cbs, ok := suite.chainA.App.IBCKeeper.Router.GetRoute(module) + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) suite.Require().True(ok) tc.malleate() // explicitly change fields in channel and testChannel err = cbs.OnChanOpenTry(suite.chainA.GetContext(), channel.Ordering, channel.GetConnectionHops(), - testChannel.PortID, testChannel.ID, chanCap, channel.Counterparty, channel.GetVersion(), counterpartyVersion, + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, chanCap, channel.Counterparty, channel.GetVersion(), counterpartyVersion, ) if tc.expPass { @@ -193,11 +193,7 @@ func (suite *TransferTestSuite) TestOnChanOpenTry() { } func (suite *TransferTestSuite) TestOnChanOpenAck() { - var ( - testChannel ibctesting.TestChannel - connA *ibctesting.TestConnection - counterpartyVersion string - ) + var counterpartyVersion string testCases := []struct { name string @@ -221,19 +217,20 @@ func (suite *TransferTestSuite) TestOnChanOpenAck() { suite.Run(tc.name, func() { suite.SetupTest() // reset - _, _, connA, _ = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - testChannel = suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) + path := NewTransferPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + path.EndpointA.ChannelID = ibctesting.FirstChannelID counterpartyVersion = types.Version - module, _, err := suite.chainA.App.IBCKeeper.PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort) + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort) suite.Require().NoError(err) - cbs, ok := suite.chainA.App.IBCKeeper.Router.GetRoute(module) + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) suite.Require().True(ok) tc.malleate() // explicitly change fields in channel and testChannel - err = cbs.OnChanOpenAck(suite.chainA.GetContext(), testChannel.PortID, testChannel.ID, counterpartyVersion) + err = cbs.OnChanOpenAck(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, counterpartyVersion) if tc.expPass { suite.Require().NoError(err) diff --git a/modules/core/02-client/abci_test.go b/modules/core/02-client/abci_test.go index cbf63d85b8c..0bbdf489574 100644 --- a/modules/core/02-client/abci_test.go +++ b/modules/core/02-client/abci_test.go @@ -7,13 +7,13 @@ import ( abci "github.com/tendermint/tendermint/abci/types" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" client "github.com/cosmos/ibc-go/modules/core/02-client" "github.com/cosmos/ibc-go/modules/core/02-client/types" "github.com/cosmos/ibc-go/modules/core/exported" ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" localhosttypes "github.com/cosmos/ibc-go/modules/light-clients/09-localhost/types" ibctesting "github.com/cosmos/ibc-go/testing" - upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" ) type ClientTestSuite struct { @@ -36,7 +36,7 @@ func (suite *ClientTestSuite) SetupTest() { localHostClient := localhosttypes.NewClientState( suite.chainA.GetContext().ChainID(), types.NewHeight(revision, uint64(suite.chainA.GetContext().BlockHeight())), ) - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), exported.Localhost, localHostClient) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), exported.Localhost, localHostClient) } func TestClientTestSuite(t *testing.T) { @@ -54,7 +54,7 @@ func (suite *ClientTestSuite) TestBeginBlocker() { suite.coordinator.CommitBlock(suite.chainA, suite.chainB) suite.Require().NotPanics(func() { - client.BeginBlocker(suite.chainA.GetContext(), suite.chainA.App.IBCKeeper.ClientKeeper) + client.BeginBlocker(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) }, "BeginBlocker shouldn't panic") localHostClient = suite.chainA.GetClientState(exported.Localhost) @@ -69,7 +69,7 @@ func (suite *ClientTestSuite) TestBeginBlockerConsensusState() { Height: suite.chainA.GetContext().BlockHeight() + 1, } // set upgrade plan in the upgrade store - store := suite.chainA.GetContext().KVStore(suite.chainA.App.GetKey(upgradetypes.StoreKey)) + store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(upgradetypes.StoreKey)) bz := suite.chainA.App.AppCodec().MustMarshalBinaryBare(plan) store.Set(upgradetypes.PlanKey(), bz) @@ -79,14 +79,14 @@ func (suite *ClientTestSuite) TestBeginBlockerConsensusState() { NextValidatorsHash: nextValsHash, }) - err := suite.chainA.App.UpgradeKeeper.SetUpgradedClient(newCtx, plan.Height, []byte("client state")) + err := suite.chainA.GetSimApp().UpgradeKeeper.SetUpgradedClient(newCtx, plan.Height, []byte("client state")) suite.Require().NoError(err) req := abci.RequestBeginBlock{Header: newCtx.BlockHeader()} suite.chainA.App.BeginBlock(req) // plan Height is at ctx.BlockHeight+1 - consState, found := suite.chainA.App.UpgradeKeeper.GetUpgradedConsensusState(newCtx, plan.Height) + consState, found := suite.chainA.GetSimApp().UpgradeKeeper.GetUpgradedConsensusState(newCtx, plan.Height) suite.Require().True(found) bz, err = types.MarshalConsensusState(suite.chainA.App.AppCodec(), &ibctmtypes.ConsensusState{Timestamp: newCtx.BlockTime(), NextValidatorsHash: nextValsHash}) suite.Require().NoError(err) diff --git a/modules/core/02-client/keeper/client_test.go b/modules/core/02-client/keeper/client_test.go index 69e0953be74..b42322d992d 100644 --- a/modules/core/02-client/keeper/client_test.go +++ b/modules/core/02-client/keeper/client_test.go @@ -5,9 +5,9 @@ import ( "fmt" "time" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" tmtypes "github.com/tendermint/tendermint/types" - upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" "github.com/cosmos/ibc-go/modules/core/02-client/types" clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types" @@ -216,20 +216,20 @@ func (suite *KeeperTestSuite) TestUpdateClientLocalhost() { var localhostClient exported.ClientState = localhosttypes.NewClientState(suite.chainA.ChainID, types.NewHeight(revision, uint64(suite.chainA.GetContext().BlockHeight()))) ctx := suite.chainA.GetContext().WithBlockHeight(suite.chainA.GetContext().BlockHeight() + 1) - err := suite.chainA.App.IBCKeeper.ClientKeeper.UpdateClient(ctx, exported.Localhost, nil) + err := suite.chainA.App.GetIBCKeeper().ClientKeeper.UpdateClient(ctx, exported.Localhost, nil) suite.Require().NoError(err) - clientState, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(ctx, exported.Localhost) + clientState, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(ctx, exported.Localhost) suite.Require().True(found) suite.Require().Equal(localhostClient.GetLatestHeight().(types.Height).Increment(), clientState.GetLatestHeight()) } func (suite *KeeperTestSuite) TestUpgradeClient() { var ( + path *ibctesting.Path upgradedClient exported.ClientState upgradedConsState exported.ConsensusState lastHeight exported.Height - clientA string proofUpgradedClient, proofUpgradedConsState []byte upgradedClientBz, upgradedConsStateBz []byte err error @@ -247,16 +247,16 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -271,22 +271,22 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) proofUpgradedConsState, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedConsStateKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) - clientA = "wrongclientid" + path.EndpointA.ClientID = "wrongclientid" }, expPass: false, }, @@ -297,16 +297,16 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -316,7 +316,7 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { tmClient, ok := cs.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClient.FrozenHeight = types.NewHeight(0, 1) - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), clientA, tmClient) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID, tmClient) }, expPass: false, }, @@ -327,17 +327,17 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // change upgradedClient client-specified parameters upgradedClient = ibctmtypes.NewClientState("wrongchainID", ibctmtypes.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, newClientHeight, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath, true, true) suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -349,7 +349,8 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { for _, tc := range testCases { tc := tc - clientA, _ = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + path = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) upgradedClient = ibctmtypes.NewClientState("newChainId", ibctmtypes.DefaultTrustLevel, trustingPeriod, ubdPeriod+trustingPeriod, maxClockDrift, newClientHeight, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath, false, false) upgradedClient = upgradedClient.ZeroCustomFields() upgradedClientBz, err = types.MarshalClientState(suite.chainA.App.AppCodec(), upgradedClient) @@ -366,7 +367,7 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { // Call ZeroCustomFields on upgraded clients to clear any client-chosen parameters in test-case upgradedClient upgradedClient = upgradedClient.ZeroCustomFields() - err = suite.chainA.App.IBCKeeper.ClientKeeper.UpgradeClient(suite.chainA.GetContext(), clientA, upgradedClient, upgradedConsState, proofUpgradedClient, proofUpgradedConsState) + err = suite.chainA.App.GetIBCKeeper().ClientKeeper.UpgradeClient(suite.chainA.GetContext(), path.EndpointA.ClientID, upgradedClient, upgradedConsState, proofUpgradedClient, proofUpgradedConsState) if tc.expPass { suite.Require().NoError(err, "verify upgrade failed on valid case: %s", tc.name) @@ -592,12 +593,13 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() { } func (suite *KeeperTestSuite) TestUpdateClientEventEmission() { - clientID, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - header, err := suite.chainA.ConstructUpdateTMClientHeader(suite.chainB, clientID) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) + header, err := suite.chainA.ConstructUpdateTMClientHeader(suite.chainB, path.EndpointA.ClientID) suite.Require().NoError(err) msg, err := clienttypes.NewMsgUpdateClient( - clientID, header, + path.EndpointA.ClientID, header, suite.chainA.SenderAccount.GetAddress().String(), ) diff --git a/modules/core/02-client/keeper/grpc_query_test.go b/modules/core/02-client/keeper/grpc_query_test.go index 41c128d9e52..b80fd57ff87 100644 --- a/modules/core/02-client/keeper/grpc_query_test.go +++ b/modules/core/02-client/keeper/grpc_query_test.go @@ -116,14 +116,17 @@ func (suite *KeeperTestSuite) TestQueryClientStates() { { "success", func() { - clientA1, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - clientA2, _ := suite.coordinator.CreateClient(suite.chainA, suite.chainB, exported.Tendermint) + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path1) - clientStateA1 := suite.chainA.GetClientState(clientA1) - clientStateA2 := suite.chainA.GetClientState(clientA2) + path2 := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path2) - idcs := types.NewIdentifiedClientState(clientA1, clientStateA1) - idcs2 := types.NewIdentifiedClientState(clientA2, clientStateA2) + clientStateA1 := path1.EndpointA.GetClientState() + clientStateA2 := path2.EndpointA.GetClientState() + + idcs := types.NewIdentifiedClientState(path1.EndpointA.ClientID, clientStateA1) + idcs2 := types.NewIdentifiedClientState(path2.EndpointA.ClientID, clientStateA2) // order is sorted by client id, localhost is last expClientStates = types.IdentifiedClientStates{idcs, idcs2}.Sort() diff --git a/modules/core/02-client/keeper/keeper_test.go b/modules/core/02-client/keeper/keeper_test.go index b31972b325c..d3e1cef5bcc 100644 --- a/modules/core/02-client/keeper/keeper_test.go +++ b/modules/core/02-client/keeper/keeper_test.go @@ -120,7 +120,7 @@ func (suite *KeeperTestSuite) SetupTest() { localHostClient := localhosttypes.NewClientState( suite.chainA.ChainID, types.NewHeight(revision, uint64(suite.chainA.GetContext().BlockHeight())), ) - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), exported.Localhost, localHostClient) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), exported.Localhost, localHostClient) queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, app.InterfaceRegistry()) types.RegisterQueryServer(queryHelper, app.IBCKeeper.ClientKeeper) @@ -222,7 +222,7 @@ func (suite *KeeperTestSuite) TestValidateSelfClient() { } for _, tc := range testCases { - err := suite.chainA.App.IBCKeeper.ClientKeeper.ValidateSelfClient(suite.chainA.GetContext(), tc.clientState) + err := suite.chainA.App.GetIBCKeeper().ClientKeeper.ValidateSelfClient(suite.chainA.GetContext(), tc.clientState) if tc.expPass { suite.Require().NoError(err, "expected valid client for case: %s", tc.name) } else { @@ -244,16 +244,16 @@ func (suite KeeperTestSuite) TestGetAllGenesisClients() { expGenClients := make(types.IdentifiedClientStates, len(expClients)) for i := range expClients { - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), clientIDs[i], expClients[i]) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), clientIDs[i], expClients[i]) expGenClients[i] = types.NewIdentifiedClientState(clientIDs[i], expClients[i]) } // add localhost client - localHostClient, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), exported.Localhost) + localHostClient, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), exported.Localhost) suite.Require().True(found) expGenClients = append(expGenClients, types.NewIdentifiedClientState(exported.Localhost, localHostClient)) - genClients := suite.chainA.App.IBCKeeper.ClientKeeper.GetAllGenesisClients(suite.chainA.GetContext()) + genClients := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetAllGenesisClients(suite.chainA.GetContext()) suite.Require().Equal(expGenClients.Sort(), genClients) } @@ -261,7 +261,7 @@ func (suite KeeperTestSuite) TestGetAllGenesisClients() { func (suite KeeperTestSuite) TestGetAllGenesisMetadata() { expectedGenMetadata := []types.IdentifiedGenesisMetadata{ types.NewIdentifiedGenesisMetadata( - "clientA", + "07-tendermint-1", []types.GenesisMetadata{ types.NewGenesisMetadata(ibctmtypes.ProcessedTimeKey(types.NewHeight(0, 1)), []byte("foo")), types.NewGenesisMetadata(ibctmtypes.ProcessedTimeKey(types.NewHeight(0, 2)), []byte("bar")), @@ -278,13 +278,13 @@ func (suite KeeperTestSuite) TestGetAllGenesisMetadata() { } genClients := []types.IdentifiedClientState{ - types.NewIdentifiedClientState("clientA", &ibctmtypes.ClientState{}), types.NewIdentifiedClientState("clientB", &ibctmtypes.ClientState{}), + types.NewIdentifiedClientState("07-tendermint-1", &ibctmtypes.ClientState{}), types.NewIdentifiedClientState("clientB", &ibctmtypes.ClientState{}), types.NewIdentifiedClientState("clientC", &ibctmtypes.ClientState{}), types.NewIdentifiedClientState("clientD", &localhosttypes.ClientState{}), } - suite.chainA.App.IBCKeeper.ClientKeeper.SetAllClientMetadata(suite.chainA.GetContext(), expectedGenMetadata) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetAllClientMetadata(suite.chainA.GetContext(), expectedGenMetadata) - actualGenMetadata, err := suite.chainA.App.IBCKeeper.ClientKeeper.GetAllClientMetadata(suite.chainA.GetContext(), genClients) + actualGenMetadata, err := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetAllClientMetadata(suite.chainA.GetContext(), genClients) suite.Require().NoError(err, "get client metadata returned error unexpectedly") suite.Require().Equal(expectedGenMetadata, actualGenMetadata, "retrieved metadata is unexpected") } @@ -342,21 +342,22 @@ func (suite KeeperTestSuite) TestConsensusStateHelpers() { // 2 clients in total are created on chainA. The first client is updated so it contains an initial consensus state // and a consensus state at the update height. func (suite KeeperTestSuite) TestGetAllConsensusStates() { - clientA, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) - clientState := suite.chainA.GetClientState(clientA) + clientState := path.EndpointA.GetClientState() expConsensusHeight0 := clientState.GetLatestHeight() - consensusState0, ok := suite.chainA.GetConsensusState(clientA, expConsensusHeight0) + consensusState0, ok := suite.chainA.GetConsensusState(path.EndpointA.ClientID, expConsensusHeight0) suite.Require().True(ok) // update client to create a second consensus state - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - clientState = suite.chainA.GetClientState(clientA) + clientState = path.EndpointA.GetClientState() expConsensusHeight1 := clientState.GetLatestHeight() suite.Require().True(expConsensusHeight1.GT(expConsensusHeight0)) - consensusState1, ok := suite.chainA.GetConsensusState(clientA, expConsensusHeight1) + consensusState1, ok := suite.chainA.GetConsensusState(path.EndpointA.ClientID, expConsensusHeight1) suite.Require().True(ok) expConsensus := []exported.ConsensusState{ @@ -365,25 +366,26 @@ func (suite KeeperTestSuite) TestGetAllConsensusStates() { } // create second client on chainA - clientA2, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - clientState = suite.chainA.GetClientState(clientA2) + path2 := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path2) + clientState = path2.EndpointA.GetClientState() expConsensusHeight2 := clientState.GetLatestHeight() - consensusState2, ok := suite.chainA.GetConsensusState(clientA2, expConsensusHeight2) + consensusState2, ok := suite.chainA.GetConsensusState(path2.EndpointA.ClientID, expConsensusHeight2) suite.Require().True(ok) expConsensus2 := []exported.ConsensusState{consensusState2} expConsensusStates := types.ClientsConsensusStates{ - types.NewClientConsensusStates(clientA, []types.ConsensusStateWithHeight{ + types.NewClientConsensusStates(path.EndpointA.ClientID, []types.ConsensusStateWithHeight{ types.NewConsensusStateWithHeight(expConsensusHeight0.(types.Height), expConsensus[0]), types.NewConsensusStateWithHeight(expConsensusHeight1.(types.Height), expConsensus[1]), }), - types.NewClientConsensusStates(clientA2, []types.ConsensusStateWithHeight{ + types.NewClientConsensusStates(path2.EndpointA.ClientID, []types.ConsensusStateWithHeight{ types.NewConsensusStateWithHeight(expConsensusHeight2.(types.Height), expConsensus2[0]), }), }.Sort() - consStates := suite.chainA.App.IBCKeeper.ClientKeeper.GetAllConsensusStates(suite.chainA.GetContext()) + consStates := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetAllConsensusStates(suite.chainA.GetContext()) suite.Require().Equal(expConsensusStates, consStates, "%s \n\n%s", expConsensusStates, consStates) } diff --git a/modules/core/02-client/keeper/params_test.go b/modules/core/02-client/keeper/params_test.go index fdcaad5f831..cbcc2fea161 100644 --- a/modules/core/02-client/keeper/params_test.go +++ b/modules/core/02-client/keeper/params_test.go @@ -7,11 +7,11 @@ import ( func (suite *KeeperTestSuite) TestParams() { expParams := types.DefaultParams() - params := suite.chainA.App.IBCKeeper.ClientKeeper.GetParams(suite.chainA.GetContext()) + params := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetParams(suite.chainA.GetContext()) suite.Require().Equal(expParams, params) expParams.AllowedClients = []string{} - suite.chainA.App.IBCKeeper.ClientKeeper.SetParams(suite.chainA.GetContext(), expParams) - params = suite.chainA.App.IBCKeeper.ClientKeeper.GetParams(suite.chainA.GetContext()) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetParams(suite.chainA.GetContext(), expParams) + params = suite.chainA.App.GetIBCKeeper().ClientKeeper.GetParams(suite.chainA.GetContext()) suite.Require().Empty(expParams.AllowedClients) } diff --git a/modules/core/02-client/keeper/proposal_test.go b/modules/core/02-client/keeper/proposal_test.go index bd381052b7f..5e4eca405d4 100644 --- a/modules/core/02-client/keeper/proposal_test.go +++ b/modules/core/02-client/keeper/proposal_test.go @@ -2,11 +2,11 @@ package keeper_test import ( govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" "github.com/cosmos/ibc-go/modules/core/02-client/types" "github.com/cosmos/ibc-go/modules/core/exported" ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" ibctesting "github.com/cosmos/ibc-go/testing" - upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" ) func (suite *KeeperTestSuite) TestClientUpdateProposal() { @@ -32,14 +32,14 @@ func (suite *KeeperTestSuite) TestClientUpdateProposal() { "subject and substitute use different revision numbers", func() { tmClientState, ok := substituteClientState.(*ibctmtypes.ClientState) suite.Require().True(ok) - consState, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientConsensusState(suite.chainA.GetContext(), substitute, tmClientState.LatestHeight) + consState, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientConsensusState(suite.chainA.GetContext(), substitute, tmClientState.LatestHeight) suite.Require().True(found) newRevisionNumber := tmClientState.GetLatestHeight().GetRevisionNumber() + 1 tmClientState.LatestHeight = types.NewHeight(newRevisionNumber, tmClientState.GetLatestHeight().GetRevisionHeight()) initialHeight = types.NewHeight(newRevisionNumber, initialHeight.GetRevisionHeight()) - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientConsensusState(suite.chainA.GetContext(), substitute, tmClientState.LatestHeight, consState) - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), substitute, tmClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(suite.chainA.GetContext(), substitute, tmClientState.LatestHeight, consState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), substitute, tmClientState) content = types.NewClientUpdateProposal(ibctesting.Title, ibctesting.Description, subject, substitute, initialHeight) }, true, @@ -69,7 +69,7 @@ func (suite *KeeperTestSuite) TestClientUpdateProposal() { tmClientState, ok := subjectClientState.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClientState.LatestHeight = substituteClientState.GetLatestHeight().(types.Height) - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState) content = types.NewClientUpdateProposal(ibctesting.Title, ibctesting.Description, subject, substitute, initialHeight) }, false, @@ -79,7 +79,7 @@ func (suite *KeeperTestSuite) TestClientUpdateProposal() { tmClientState, ok := subjectClientState.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClientState.FrozenHeight = types.ZeroHeight() - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState) content = types.NewClientUpdateProposal(ibctesting.Title, ibctesting.Description, subject, substitute, initialHeight) }, false, @@ -92,14 +92,19 @@ func (suite *KeeperTestSuite) TestClientUpdateProposal() { suite.Run(tc.name, func() { suite.SetupTest() // reset - subject, _ = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + subjectPath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(subjectPath) + subject = subjectPath.EndpointA.ClientID subjectClientState = suite.chainA.GetClientState(subject) - substitute, _ = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + + substitutePath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(substitutePath) + substitute = substitutePath.EndpointA.ClientID initialHeight = types.NewHeight(subjectClientState.GetLatestHeight().GetRevisionNumber(), subjectClientState.GetLatestHeight().GetRevisionHeight()+1) // update substitute twice - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, substitute, exported.Tendermint) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, substitute, exported.Tendermint) + substitutePath.EndpointA.UpdateClient() + substitutePath.EndpointA.UpdateClient() substituteClientState = suite.chainA.GetClientState(substitute) tmClientState, ok := subjectClientState.(*ibctmtypes.ClientState) @@ -107,20 +112,20 @@ func (suite *KeeperTestSuite) TestClientUpdateProposal() { tmClientState.AllowUpdateAfterMisbehaviour = true tmClientState.AllowUpdateAfterExpiry = true tmClientState.FrozenHeight = tmClientState.LatestHeight - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState) tmClientState, ok = substituteClientState.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClientState.AllowUpdateAfterMisbehaviour = true tmClientState.AllowUpdateAfterExpiry = true tmClientState.FrozenHeight = tmClientState.LatestHeight - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), substitute, tmClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), substitute, tmClientState) tc.malleate() updateProp, ok := content.(*types.ClientUpdateProposal) suite.Require().True(ok) - err = suite.chainA.App.IBCKeeper.ClientKeeper.ClientUpdateProposal(suite.chainA.GetContext(), updateProp) + err = suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientUpdateProposal(suite.chainA.GetContext(), updateProp) if tc.expPass { suite.Require().NoError(err) @@ -194,8 +199,9 @@ func (suite *KeeperTestSuite) TestHandleUpgradeProposal() { suite.SetupTest() // reset oldPlan.Height = 0 //reset - clientID, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - upgradedClientState = suite.chainA.GetClientState(clientID).ZeroCustomFields().(*ibctmtypes.ClientState) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) + upgradedClientState = suite.chainA.GetClientState(path.EndpointA.ClientID).ZeroCustomFields().(*ibctmtypes.ClientState) // use height 1000 to distinguish from old plan plan = upgradetypes.Plan{ @@ -208,33 +214,33 @@ func (suite *KeeperTestSuite) TestHandleUpgradeProposal() { // set the old plan if it is not empty if oldPlan.Height != 0 { // set upgrade plan in the upgrade store - store := suite.chainA.GetContext().KVStore(suite.chainA.App.GetKey(upgradetypes.StoreKey)) + store := suite.chainA.GetContext().KVStore(suite.chainA.GetSimApp().GetKey(upgradetypes.StoreKey)) bz := suite.chainA.App.AppCodec().MustMarshalBinaryBare(&oldPlan) store.Set(upgradetypes.PlanKey(), bz) bz, err := types.MarshalClientState(suite.chainA.App.AppCodec(), upgradedClientState) suite.Require().NoError(err) - suite.chainA.App.UpgradeKeeper.SetUpgradedClient(suite.chainA.GetContext(), oldPlan.Height, bz) + suite.chainA.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainA.GetContext(), oldPlan.Height, bz) } upgradeProp, ok := content.(*types.UpgradeProposal) suite.Require().True(ok) - err = suite.chainA.App.IBCKeeper.ClientKeeper.HandleUpgradeProposal(suite.chainA.GetContext(), upgradeProp) + err = suite.chainA.App.GetIBCKeeper().ClientKeeper.HandleUpgradeProposal(suite.chainA.GetContext(), upgradeProp) if tc.expPass { suite.Require().NoError(err) // check that the correct plan is returned - storedPlan, found := suite.chainA.App.UpgradeKeeper.GetUpgradePlan(suite.chainA.GetContext()) + storedPlan, found := suite.chainA.GetSimApp().UpgradeKeeper.GetUpgradePlan(suite.chainA.GetContext()) suite.Require().True(found) suite.Require().Equal(plan, storedPlan) // check that old upgraded client state is cleared - _, found = suite.chainA.App.UpgradeKeeper.GetUpgradedClient(suite.chainA.GetContext(), oldPlan.Height) + _, found = suite.chainA.GetSimApp().UpgradeKeeper.GetUpgradedClient(suite.chainA.GetContext(), oldPlan.Height) suite.Require().False(found) // check that client state was set - storedClientState, found := suite.chainA.App.UpgradeKeeper.GetUpgradedClient(suite.chainA.GetContext(), plan.Height) + storedClientState, found := suite.chainA.GetSimApp().UpgradeKeeper.GetUpgradedClient(suite.chainA.GetContext(), plan.Height) suite.Require().True(found) clientState, err := types.UnmarshalClientState(suite.chainA.App.AppCodec(), storedClientState) suite.Require().NoError(err) @@ -243,7 +249,7 @@ func (suite *KeeperTestSuite) TestHandleUpgradeProposal() { suite.Require().Error(err) // check that the new plan wasn't stored - storedPlan, found := suite.chainA.App.UpgradeKeeper.GetUpgradePlan(suite.chainA.GetContext()) + storedPlan, found := suite.chainA.GetSimApp().UpgradeKeeper.GetUpgradePlan(suite.chainA.GetContext()) if oldPlan.Height != 0 { // NOTE: this is only true if the ScheduleUpgrade function // returns an error before clearing the old plan @@ -255,7 +261,7 @@ func (suite *KeeperTestSuite) TestHandleUpgradeProposal() { } // check that client state was not set - _, found = suite.chainA.App.UpgradeKeeper.GetUpgradedClient(suite.chainA.GetContext(), plan.Height) + _, found = suite.chainA.GetSimApp().UpgradeKeeper.GetUpgradedClient(suite.chainA.GetContext(), plan.Height) suite.Require().False(found) } diff --git a/modules/core/02-client/proposal_handler_test.go b/modules/core/02-client/proposal_handler_test.go index ad7873f5d77..2c83b95b8cf 100644 --- a/modules/core/02-client/proposal_handler_test.go +++ b/modules/core/02-client/proposal_handler_test.go @@ -6,7 +6,6 @@ import ( govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" client "github.com/cosmos/ibc-go/modules/core/02-client" clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" - "github.com/cosmos/ibc-go/modules/core/exported" ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" ibctesting "github.com/cosmos/ibc-go/testing" ) @@ -24,29 +23,34 @@ func (suite *ClientTestSuite) TestNewClientUpdateProposalHandler() { }{ { "valid update client proposal", func() { - subject, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - subjectClientState := suite.chainA.GetClientState(subject) - substitute, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + subjectPath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(subjectPath) + subjectClientState := suite.chainA.GetClientState(subjectPath.EndpointA.ClientID) + + substitutePath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(substitutePath) initialHeight := clienttypes.NewHeight(subjectClientState.GetLatestHeight().GetRevisionNumber(), subjectClientState.GetLatestHeight().GetRevisionHeight()+1) // update substitute twice - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, substitute, exported.Tendermint) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, substitute, exported.Tendermint) - substituteClientState := suite.chainA.GetClientState(substitute) + err = substitutePath.EndpointA.UpdateClient() + suite.Require().NoError(err) + err = substitutePath.EndpointA.UpdateClient() + suite.Require().NoError(err) + substituteClientState := suite.chainA.GetClientState(substitutePath.EndpointA.ClientID) tmClientState, ok := subjectClientState.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClientState.AllowUpdateAfterMisbehaviour = true tmClientState.FrozenHeight = tmClientState.LatestHeight - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), subject, tmClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), subjectPath.EndpointA.ClientID, tmClientState) // replicate changes to substitute (they must match) tmClientState, ok = substituteClientState.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClientState.AllowUpdateAfterMisbehaviour = true - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), substitute, tmClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), substitutePath.EndpointA.ClientID, tmClientState) - content = clienttypes.NewClientUpdateProposal(ibctesting.Title, ibctesting.Description, subject, substitute, initialHeight) + content = clienttypes.NewClientUpdateProposal(ibctesting.Title, ibctesting.Description, subjectPath.EndpointA.ClientID, substitutePath.EndpointA.ClientID, initialHeight) }, true, }, { @@ -69,7 +73,7 @@ func (suite *ClientTestSuite) TestNewClientUpdateProposalHandler() { tc.malleate() - proposalHandler := client.NewClientProposalHandler(suite.chainA.App.IBCKeeper.ClientKeeper) + proposalHandler := client.NewClientProposalHandler(suite.chainA.App.GetIBCKeeper().ClientKeeper) err = proposalHandler(suite.chainA.GetContext(), content) diff --git a/modules/core/02-client/types/client_test.go b/modules/core/02-client/types/client_test.go index e51d7ead6ae..b5b9b73ca0e 100644 --- a/modules/core/02-client/types/client_test.go +++ b/modules/core/02-client/types/client_test.go @@ -6,7 +6,6 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/ibc-go/modules/core/02-client/types" - "github.com/cosmos/ibc-go/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/testing" ) @@ -27,9 +26,10 @@ func (suite *TypesTestSuite) TestMarshalConsensusStateWithHeight() { }, { "tendermint client", func() { - clientA, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - clientState := suite.chainA.GetClientState(clientA) - consensusState, ok := suite.chainA.GetConsensusState(clientA, clientState.GetLatestHeight()) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) + clientState := suite.chainA.GetClientState(path.EndpointA.ClientID) + consensusState, ok := suite.chainA.GetConsensusState(path.EndpointA.ClientID, clientState.GetLatestHeight()) suite.Require().True(ok) cswh = types.NewConsensusStateWithHeight(clientState.GetLatestHeight().(types.Height), consensusState) diff --git a/modules/core/02-client/types/genesis_test.go b/modules/core/02-client/types/genesis_test.go index c3d207c1001..ab2c479c6f1 100644 --- a/modules/core/02-client/types/genesis_test.go +++ b/modules/core/02-client/types/genesis_test.go @@ -7,7 +7,6 @@ import ( client "github.com/cosmos/ibc-go/modules/core/02-client" "github.com/cosmos/ibc-go/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types" "github.com/cosmos/ibc-go/modules/core/exported" ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" @@ -30,10 +29,12 @@ var clientHeight = types.NewHeight(0, 10) func (suite *TypesTestSuite) TestMarshalGenesisState() { cdc := suite.chainA.App.AppCodec() - clientA, _, _, _, _, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + err := path.EndpointA.UpdateClient() + suite.Require().NoError(err) - genesis := client.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.IBCKeeper.ClientKeeper) + genesis := client.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper) bz, err := cdc.MarshalJSON(&genesis) suite.Require().NoError(err) diff --git a/modules/core/02-client/types/proposal_test.go b/modules/core/02-client/types/proposal_test.go index ab3fd362519..76aee1172ad 100644 --- a/modules/core/02-client/types/proposal_test.go +++ b/modules/core/02-client/types/proposal_test.go @@ -7,17 +7,23 @@ import ( "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + "github.com/cosmos/ibc-go/modules/core/02-client/types" - "github.com/cosmos/ibc-go/modules/core/exported" ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" ibctesting "github.com/cosmos/ibc-go/testing" - upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" ) func (suite *TypesTestSuite) TestValidateBasic() { - subject, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + subjectPath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(subjectPath) + subject := subjectPath.EndpointA.ClientID subjectClientState := suite.chainA.GetClientState(subject) - substitute, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + + substitutePath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(substitutePath) + substitute := substitutePath.EndpointA.ClientID + initialHeight := types.NewHeight(subjectClientState.GetLatestHeight().GetRevisionNumber(), subjectClientState.GetLatestHeight().GetRevisionHeight()+1) testCases := []struct { @@ -97,8 +103,9 @@ func (suite *TypesTestSuite) TestUpgradeProposalValidateBasic() { err error ) - client, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - cs := suite.chainA.GetClientState(client) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) + cs := suite.chainA.GetClientState(path.EndpointA.ClientID) plan := upgradetypes.Plan{ Name: "ibc upgrade", Height: 1000, diff --git a/modules/core/03-connection/keeper/grpc_query_test.go b/modules/core/03-connection/keeper/grpc_query_test.go index 42892dfdd15..a8480dadda8 100644 --- a/modules/core/03-connection/keeper/grpc_query_test.go +++ b/modules/core/03-connection/keeper/grpc_query_test.go @@ -7,7 +7,6 @@ import ( "github.com/cosmos/cosmos-sdk/types/query" clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" "github.com/cosmos/ibc-go/modules/core/03-connection/types" - channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" "github.com/cosmos/ibc-go/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/testing" ) @@ -47,16 +46,17 @@ func (suite *KeeperTestSuite) TestQueryConnection() { { "success", func() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA := suite.chainA.GetFirstTestConnection(clientA, clientB) - connB := suite.chainB.GetFirstTestConnection(clientB, clientA) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) + err := path.EndpointA.ConnOpenInit() + suite.Require().NoError(err) - counterparty := types.NewCounterparty(clientB, connB.ID, suite.chainB.GetPrefix()) - expConnection = types.NewConnectionEnd(types.INIT, clientA, counterparty, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 500) - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainA.GetContext(), connA.ID, expConnection) + counterparty := types.NewCounterparty(path.EndpointB.ClientID, "", suite.chainB.GetPrefix()) + expConnection = types.NewConnectionEnd(types.INIT, path.EndpointA.ClientID, counterparty, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 500) + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, expConnection) req = &types.QueryConnectionRequest{ - ConnectionId: connA.ID, + ConnectionId: path.EndpointA.ConnectionID, } }, true, @@ -111,23 +111,28 @@ func (suite *KeeperTestSuite) TestQueryConnections() { { "success", func() { - clientA, clientB, connA0, connB0 := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - clientA1, clientB1, connA1, connB1 := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - connA2, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + path2 := ibctesting.NewPath(suite.chainA, suite.chainB) + path3 := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path1) + suite.coordinator.SetupConnections(path2) + suite.coordinator.SetupClients(path3) + + err := path3.EndpointA.ConnOpenInit() suite.Require().NoError(err) - counterparty1 := types.NewCounterparty(clientB, connB0.ID, suite.chainB.GetPrefix()) - counterparty2 := types.NewCounterparty(clientB1, connB1.ID, suite.chainB.GetPrefix()) + counterparty1 := types.NewCounterparty(path1.EndpointB.ClientID, path1.EndpointB.ConnectionID, suite.chainB.GetPrefix()) + counterparty2 := types.NewCounterparty(path2.EndpointB.ClientID, path2.EndpointB.ConnectionID, suite.chainB.GetPrefix()) // counterparty connection id is blank after open init - counterparty3 := types.NewCounterparty(clientB, "", suite.chainB.GetPrefix()) + counterparty3 := types.NewCounterparty(path3.EndpointB.ClientID, "", suite.chainB.GetPrefix()) - conn1 := types.NewConnectionEnd(types.OPEN, clientA, counterparty1, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) - conn2 := types.NewConnectionEnd(types.OPEN, clientA1, counterparty2, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) - conn3 := types.NewConnectionEnd(types.INIT, clientA, counterparty3, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) + conn1 := types.NewConnectionEnd(types.OPEN, path1.EndpointA.ClientID, counterparty1, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) + conn2 := types.NewConnectionEnd(types.OPEN, path2.EndpointA.ClientID, counterparty2, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) + conn3 := types.NewConnectionEnd(types.INIT, path3.EndpointA.ClientID, counterparty3, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) - iconn1 := types.NewIdentifiedConnection(connA0.ID, conn1) - iconn2 := types.NewIdentifiedConnection(connA1.ID, conn2) - iconn3 := types.NewIdentifiedConnection(connA2.ID, conn3) + iconn1 := types.NewIdentifiedConnection(path1.EndpointA.ConnectionID, conn1) + iconn2 := types.NewIdentifiedConnection(path2.EndpointA.ConnectionID, conn2) + iconn3 := types.NewIdentifiedConnection(path3.EndpointA.ConnectionID, conn3) expConnections = []*types.IdentifiedConnection{&iconn1, &iconn2, &iconn3} @@ -197,13 +202,21 @@ func (suite *KeeperTestSuite) TestQueryClientConnections() { { "success", func() { - clientA, clientB, connA0, _ := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - connA1, _ := suite.coordinator.CreateConnection(suite.chainA, suite.chainB, clientA, clientB) - expPaths = []string{connA0.ID, connA1.ID} - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetClientConnectionPaths(suite.chainA.GetContext(), clientA, expPaths) + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path1) + + // create another connection using same underlying clients + path2 := ibctesting.NewPath(suite.chainA, suite.chainB) + path2.EndpointA.ClientID = path1.EndpointA.ClientID + path2.EndpointB.ClientID = path1.EndpointB.ClientID + + suite.coordinator.CreateConnections(path2) + + expPaths = []string{path1.EndpointA.ConnectionID, path2.EndpointA.ConnectionID} + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetClientConnectionPaths(suite.chainA.GetContext(), path1.EndpointA.ClientID, expPaths) req = &types.QueryClientConnectionsRequest{ - ClientId: clientA, + ClientId: path1.EndpointA.ClientID, } }, true, @@ -269,26 +282,28 @@ func (suite *KeeperTestSuite) TestQueryConnectionClientState() { { "client state not found", func() { - _, _, connA, _, _, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) // set connection to empty so clientID is empty - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainA.GetContext(), connA.ID, types.ConnectionEnd{}) + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, types.ConnectionEnd{}) req = &types.QueryConnectionClientStateRequest{ - ConnectionId: connA.ID, + ConnectionId: path.EndpointA.ConnectionID, } }, false, }, { "success", func() { - clientA, _, connA, _ := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) - expClientState := suite.chainA.GetClientState(clientA) - expIdentifiedClientState = clienttypes.NewIdentifiedClientState(clientA, expClientState) + expClientState := suite.chainA.GetClientState(path.EndpointA.ClientID) + expIdentifiedClientState = clienttypes.NewIdentifiedClientState(path.EndpointA.ClientID, expClientState) req = &types.QueryConnectionClientStateRequest{ - ConnectionId: connA.ID, + ConnectionId: path.EndpointA.ConnectionID, } }, true, @@ -363,10 +378,11 @@ func (suite *KeeperTestSuite) TestQueryConnectionConsensusState() { { "consensus state not found", func() { - _, _, connA, _, _, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) req = &types.QueryConnectionConsensusStateRequest{ - ConnectionId: connA.ID, + ConnectionId: path.EndpointA.ConnectionID, RevisionNumber: 0, RevisionHeight: uint64(suite.chainA.GetContext().BlockHeight()), // use current height } @@ -375,15 +391,16 @@ func (suite *KeeperTestSuite) TestQueryConnectionConsensusState() { { "success", func() { - clientA, _, connA, _ := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) - clientState := suite.chainA.GetClientState(clientA) - expConsensusState, _ = suite.chainA.GetConsensusState(clientA, clientState.GetLatestHeight()) + clientState := suite.chainA.GetClientState(path.EndpointA.ClientID) + expConsensusState, _ = suite.chainA.GetConsensusState(path.EndpointA.ClientID, clientState.GetLatestHeight()) suite.Require().NotNil(expConsensusState) - expClientID = clientA + expClientID = path.EndpointA.ClientID req = &types.QueryConnectionConsensusStateRequest{ - ConnectionId: connA.ID, + ConnectionId: path.EndpointA.ConnectionID, RevisionNumber: clientState.GetLatestHeight().GetRevisionNumber(), RevisionHeight: clientState.GetLatestHeight().GetRevisionHeight(), } diff --git a/modules/core/03-connection/keeper/handshake_test.go b/modules/core/03-connection/keeper/handshake_test.go index 1a94fb3944a..0fc12f3ebd7 100644 --- a/modules/core/03-connection/keeper/handshake_test.go +++ b/modules/core/03-connection/keeper/handshake_test.go @@ -8,14 +8,14 @@ import ( host "github.com/cosmos/ibc-go/modules/core/24-host" "github.com/cosmos/ibc-go/modules/core/exported" ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" + ibctesting "github.com/cosmos/ibc-go/testing" ) // TestConnOpenInit - chainA initializes (INIT state) a connection with // chainB which is yet UNINITIALIZED func (suite *KeeperTestSuite) TestConnOpenInit() { var ( - clientA string - clientB string + path *ibctesting.Path version *types.Version delayPeriod uint64 emptyConnBID bool @@ -27,29 +27,23 @@ func (suite *KeeperTestSuite) TestConnOpenInit() { expPass bool }{ {"success", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) }, true}, {"success with empty counterparty identifier", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) emptyConnBID = true }, true}, {"success with non empty version", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) version = types.ExportedVersionsToProto(types.GetCompatibleVersions())[0] }, true}, {"success with non zero delayPeriod", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) delayPeriod = uint64(time.Hour.Nanoseconds()) }, true}, {"invalid version", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) version = &types.Version{} }, false}, {"couldn't add connection to client", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - // set clientA to invalid client identifier - clientA = "clientidentifier" + // set path.EndpointA.ClientID to invalid client identifier + path.EndpointA.ClientID = "clientidentifier" }, false}, } @@ -59,16 +53,17 @@ func (suite *KeeperTestSuite) TestConnOpenInit() { suite.SetupTest() // reset emptyConnBID = false // must be explicitly changed version = nil // must be explicitly changed + path = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) tc.malleate() - connB := suite.chainB.GetFirstTestConnection(clientB, clientA) if emptyConnBID { - connB.ID = "" + path.EndpointB.ConnectionID = "" } - counterparty := types.NewCounterparty(clientB, connB.ID, suite.chainB.GetPrefix()) + counterparty := types.NewCounterparty(path.EndpointB.ClientID, path.EndpointB.ConnectionID, suite.chainB.GetPrefix()) - connectionID, err := suite.chainA.App.IBCKeeper.ConnectionKeeper.ConnOpenInit(suite.chainA.GetContext(), clientA, counterparty, version, delayPeriod) + connectionID, err := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.ConnOpenInit(suite.chainA.GetContext(), path.EndpointA.ClientID, counterparty, version, delayPeriod) if tc.expPass { suite.Require().NoError(err) @@ -85,8 +80,7 @@ func (suite *KeeperTestSuite) TestConnOpenInit() { // connection on chainA is INIT func (suite *KeeperTestSuite) TestConnOpenTry() { var ( - clientA string - clientB string + path *ibctesting.Path delayPeriod uint64 previousConnectionID string versions []exported.Version @@ -100,112 +94,102 @@ func (suite *KeeperTestSuite) TestConnOpenTry() { expPass bool }{ {"success", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) }, true}, {"success with crossing hellos", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, connB, err := suite.coordinator.ConnOpenInitOnBothChains(suite.chainA, suite.chainB, clientA, clientB) + err := suite.coordinator.ConnOpenInitOnBothChains(path) suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) - previousConnectionID = connB.ID + previousConnectionID = path.EndpointB.ConnectionID }, true}, {"success with delay period", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) delayPeriod = uint64(time.Hour.Nanoseconds()) // set delay period on counterparty to non-zero value - conn := suite.chainA.GetConnection(connA) + conn := path.EndpointA.GetConnection() conn.DelayPeriod = delayPeriod - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainA.GetContext(), connA.ID, conn) + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, conn) // commit in order for proof to return correct value suite.coordinator.CommitBlock(suite.chainA) - suite.coordinator.UpdateClient(suite.chainB, suite.chainA, clientB, exported.Tendermint) + path.EndpointB.UpdateClient() // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) }, true}, {"invalid counterparty client", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) // Set an invalid client of chainA on chainB tmClient, ok := counterpartyClient.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClient.ChainId = "wrongchainid" - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), clientA, tmClient) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID, tmClient) }, false}, {"consensus height >= latest height", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) consensusHeight = clienttypes.GetSelfHeight(suite.chainB.GetContext()) }, false}, {"self consensus state not found", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) consensusHeight = clienttypes.NewHeight(0, 1) }, false}, {"counterparty versions is empty", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) versions = nil }, false}, {"counterparty versions don't have a match", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) version := types.NewVersion("0.0", nil) versions = []exported.Version{version} }, false}, {"connection state verification failed", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) // chainA connection not created // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) }, false}, {"client state verification failed", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) // modify counterparty client without setting in store so it still passes validate but fails proof verification tmClient, ok := counterpartyClient.(*ibctmtypes.ClientState) @@ -213,70 +197,64 @@ func (suite *KeeperTestSuite) TestConnOpenTry() { tmClient.LatestHeight = tmClient.LatestHeight.Increment().(clienttypes.Height) }, false}, {"consensus state verification failed", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) // give chainA wrong consensus state for chainB - consState, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainA.GetContext(), clientA) + consState, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetLatestClientConsensusState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) tmConsState, ok := consState.(*ibctmtypes.ConsensusState) suite.Require().True(ok) tmConsState.Timestamp = time.Now() - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientConsensusState(suite.chainA.GetContext(), clientA, counterpartyClient.GetLatestHeight(), tmConsState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(suite.chainA.GetContext(), path.EndpointA.ClientID, counterpartyClient.GetLatestHeight(), tmConsState) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) }, false}, {"invalid previous connection is in TRYOPEN", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - // open init chainA - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // open try chainB - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) - err = suite.coordinator.UpdateClient(suite.chainB, suite.chainA, clientB, exported.Tendermint) + err = path.EndpointB.UpdateClient() suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) - previousConnectionID = connB.ID + previousConnectionID = path.EndpointB.ConnectionID }, false}, {"invalid previous connection has invalid versions", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - // open init chainA - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // open try chainB - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) // modify connB to be in INIT with incorrect versions - connection, found := suite.chainB.App.IBCKeeper.ConnectionKeeper.GetConnection(suite.chainB.GetContext(), connB.ID) + connection, found := suite.chainB.App.GetIBCKeeper().ConnectionKeeper.GetConnection(suite.chainB.GetContext(), path.EndpointB.ConnectionID) suite.Require().True(found) connection.State = types.INIT connection.Versions = []*types.Version{{}} - suite.chainB.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainB.GetContext(), connB.ID, connection) + suite.chainB.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainB.GetContext(), path.EndpointB.ConnectionID, connection) - err = suite.coordinator.UpdateClient(suite.chainB, suite.chainA, clientB, exported.Tendermint) + err = path.EndpointB.UpdateClient() suite.Require().NoError(err) // retrieve client state of chainA to pass as counterpartyClient - counterpartyClient = suite.chainA.GetClientState(clientA) + counterpartyClient = suite.chainA.GetClientState(path.EndpointA.ClientID) - previousConnectionID = connB.ID + previousConnectionID = path.EndpointB.ConnectionID }, false}, } @@ -288,28 +266,33 @@ func (suite *KeeperTestSuite) TestConnOpenTry() { consensusHeight = clienttypes.ZeroHeight() // must be explicitly changed in malleate versions = types.GetCompatibleVersions() // must be explicitly changed in malleate previousConnectionID = "" + path = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) tc.malleate() - connA := suite.chainA.GetFirstTestConnection(clientA, clientB) - counterparty := types.NewCounterparty(clientA, connA.ID, suite.chainA.GetPrefix()) + counterparty := types.NewCounterparty(path.EndpointA.ClientID, path.EndpointA.ConnectionID, suite.chainA.GetPrefix()) - connectionKey := host.ConnectionKey(connA.ID) + // ensure client is up to date to receive proof + err := path.EndpointB.UpdateClient() + suite.Require().NoError(err) + + connectionKey := host.ConnectionKey(path.EndpointA.ConnectionID) proofInit, proofHeight := suite.chainA.QueryProof(connectionKey) if consensusHeight.IsZero() { // retrieve consensus state height to provide proof for consensusHeight = counterpartyClient.GetLatestHeight() } - consensusKey := host.FullConsensusStateKey(clientA, consensusHeight) + consensusKey := host.FullConsensusStateKey(path.EndpointA.ClientID, consensusHeight) proofConsensus, _ := suite.chainA.QueryProof(consensusKey) // retrieve proof of counterparty clientstate on chainA - clientKey := host.FullClientStateKey(clientA) + clientKey := host.FullClientStateKey(path.EndpointA.ClientID) proofClient, _ := suite.chainA.QueryProof(clientKey) - connectionID, err := suite.chainB.App.IBCKeeper.ConnectionKeeper.ConnOpenTry( - suite.chainB.GetContext(), previousConnectionID, counterparty, delayPeriod, clientB, counterpartyClient, + connectionID, err := suite.chainB.App.GetIBCKeeper().ConnectionKeeper.ConnOpenTry( + suite.chainB.GetContext(), previousConnectionID, counterparty, delayPeriod, path.EndpointB.ClientID, counterpartyClient, versions, proofInit, proofClient, proofConsensus, proofHeight, consensusHeight, ) @@ -329,8 +312,7 @@ func (suite *KeeperTestSuite) TestConnOpenTry() { // the initialization (TRYINIT) of the connection on Chain B (ID #2). func (suite *KeeperTestSuite) TestConnOpenAck() { var ( - clientA string - clientB string + path *ibctesting.Path consensusHeight exported.Height version *types.Version counterpartyClient exported.ClientState @@ -342,251 +324,237 @@ func (suite *KeeperTestSuite) TestConnOpenAck() { expPass bool }{ {"success", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) }, true}, {"success from tryopen", func() { // chainA is in TRYOPEN, chainB is in TRYOPEN - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connB, connA, err := suite.coordinator.ConnOpenInit(suite.chainB, suite.chainA, clientB, clientA) + err := path.EndpointB.ConnOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ConnOpenTry(suite.chainA, suite.chainB, connA, connB) + err = path.EndpointA.ConnOpenTry() suite.Require().NoError(err) // set chainB to TRYOPEN - connection := suite.chainB.GetConnection(connB) + connection := path.EndpointB.GetConnection() connection.State = types.TRYOPEN - connection.Counterparty.ConnectionId = connA.ID - suite.chainB.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainB.GetContext(), connB.ID, connection) - // update clientB so state change is committed - suite.coordinator.UpdateClient(suite.chainB, suite.chainA, clientB, exported.Tendermint) + connection.Counterparty.ConnectionId = path.EndpointA.ConnectionID + suite.chainB.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainB.GetContext(), path.EndpointB.ConnectionID, connection) + // update path.EndpointB.ClientID so state change is committed + path.EndpointB.UpdateClient() - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) }, true}, {"invalid counterparty client", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() + suite.Require().NoError(err) + + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) // Set an invalid client of chainA on chainB tmClient, ok := counterpartyClient.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClient.ChainId = "wrongchainid" - suite.chainB.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainB.GetContext(), clientB, tmClient) + suite.chainB.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainB.GetContext(), path.EndpointB.ClientID, tmClient) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) - suite.Require().NoError(err) }, false}, {"consensus height >= latest height", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) consensusHeight = clienttypes.GetSelfHeight(suite.chainA.GetContext()) }, false}, {"connection not found", func() { // connections are never created - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) }, false}, {"invalid counterparty connection ID", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) // modify connB to set counterparty connection identifier to wrong identifier - connection, found := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetConnection(suite.chainA.GetContext(), connA.ID) + connection, found := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID) suite.Require().True(found) connection.Counterparty.ConnectionId = "badconnectionid" - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainA.GetContext(), connA.ID, connection) + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, connection) - err = suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err = path.EndpointA.UpdateClient() suite.Require().NoError(err) - err = suite.coordinator.UpdateClient(suite.chainB, suite.chainA, clientB, exported.Tendermint) + err = path.EndpointB.UpdateClient() suite.Require().NoError(err) }, false}, {"connection state is not INIT", func() { // connection state is already OPEN on chainA - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) - err = suite.coordinator.ConnOpenAck(suite.chainA, suite.chainB, connA, connB) + err = path.EndpointA.ConnOpenAck() suite.Require().NoError(err) }, false}, {"connection is in INIT but the proposed version is invalid", func() { // chainA is in INIT, chainB is in TRYOPEN - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) version = types.NewVersion("2.0", nil) }, false}, {"connection is in TRYOPEN but the set version in the connection is invalid", func() { // chainA is in TRYOPEN, chainB is in TRYOPEN - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connB, connA, err := suite.coordinator.ConnOpenInit(suite.chainB, suite.chainA, clientB, clientA) + err := path.EndpointB.ConnOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ConnOpenTry(suite.chainA, suite.chainB, connA, connB) + err = path.EndpointA.ConnOpenTry() suite.Require().NoError(err) // set chainB to TRYOPEN - connection := suite.chainB.GetConnection(connB) + connection := path.EndpointB.GetConnection() connection.State = types.TRYOPEN - suite.chainB.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainB.GetContext(), connB.ID, connection) + suite.chainB.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainB.GetContext(), path.EndpointB.ConnectionID, connection) - // update clientB so state change is committed - suite.coordinator.UpdateClient(suite.chainB, suite.chainA, clientB, exported.Tendermint) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + // update path.EndpointB.ClientID so state change is committed + path.EndpointB.UpdateClient() + path.EndpointA.UpdateClient() // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) version = types.NewVersion("2.0", nil) }, false}, {"incompatible IBC versions", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) // set version to a non-compatible version version = types.NewVersion("2.0", nil) }, false}, {"empty version", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) version = &types.Version{} }, false}, {"feature set verification failed - unsupported feature", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) version = types.NewVersion(types.DefaultIBCVersionIdentifier, []string{"ORDER_ORDERED", "ORDER_UNORDERED", "ORDER_DAG"}) }, false}, {"self consensus state not found", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) consensusHeight = clienttypes.NewHeight(0, 1) }, false}, {"connection state verification failed", func() { // chainB connection is not in INIT - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) }, false}, {"client state verification failed", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) // modify counterparty client without setting in store so it still passes validate but fails proof verification tmClient, ok := counterpartyClient.(*ibctmtypes.ClientState) suite.Require().True(ok) tmClient.LatestHeight = tmClient.LatestHeight.Increment().(clienttypes.Height) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) }, false}, {"consensus state verification failed", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // retrieve client state of chainB to pass as counterpartyClient - counterpartyClient = suite.chainB.GetClientState(clientB) + counterpartyClient = suite.chainB.GetClientState(path.EndpointB.ClientID) // give chainB wrong consensus state for chainA - consState, found := suite.chainB.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainB.GetContext(), clientB) + consState, found := suite.chainB.App.GetIBCKeeper().ClientKeeper.GetLatestClientConsensusState(suite.chainB.GetContext(), path.EndpointB.ClientID) suite.Require().True(found) tmConsState, ok := consState.(*ibctmtypes.ConsensusState) suite.Require().True(ok) - tmConsState.Timestamp = time.Now() - suite.chainB.App.IBCKeeper.ClientKeeper.SetClientConsensusState(suite.chainB.GetContext(), clientB, counterpartyClient.GetLatestHeight(), tmConsState) + tmConsState.Timestamp = tmConsState.Timestamp.Add(time.Second) + suite.chainB.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(suite.chainB.GetContext(), path.EndpointB.ClientID, counterpartyClient.GetLatestHeight(), tmConsState) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) + }, false}, } @@ -596,29 +564,32 @@ func (suite *KeeperTestSuite) TestConnOpenAck() { suite.SetupTest() // reset version = types.ExportedVersionsToProto(types.GetCompatibleVersions())[0] // must be explicitly changed in malleate consensusHeight = clienttypes.ZeroHeight() // must be explicitly changed in malleate + path = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) tc.malleate() - connA := suite.chainA.GetFirstTestConnection(clientA, clientB) - connB := suite.chainB.GetFirstTestConnection(clientB, clientA) + // ensure client is up to date to receive proof + err := path.EndpointA.UpdateClient() + suite.Require().NoError(err) - connectionKey := host.ConnectionKey(connB.ID) + connectionKey := host.ConnectionKey(path.EndpointB.ConnectionID) proofTry, proofHeight := suite.chainB.QueryProof(connectionKey) if consensusHeight.IsZero() { // retrieve consensus state height to provide proof for - clientState := suite.chainB.GetClientState(clientB) + clientState := suite.chainB.GetClientState(path.EndpointB.ClientID) consensusHeight = clientState.GetLatestHeight() } - consensusKey := host.FullConsensusStateKey(clientB, consensusHeight) + consensusKey := host.FullConsensusStateKey(path.EndpointB.ClientID, consensusHeight) proofConsensus, _ := suite.chainB.QueryProof(consensusKey) // retrieve proof of counterparty clientstate on chainA - clientKey := host.FullClientStateKey(clientB) + clientKey := host.FullClientStateKey(path.EndpointB.ClientID) proofClient, _ := suite.chainB.QueryProof(clientKey) - err := suite.chainA.App.IBCKeeper.ConnectionKeeper.ConnOpenAck( - suite.chainA.GetContext(), connA.ID, counterpartyClient, version, connB.ID, + err = suite.chainA.App.GetIBCKeeper().ConnectionKeeper.ConnOpenAck( + suite.chainA.GetContext(), path.EndpointA.ConnectionID, counterpartyClient, version, path.EndpointB.ConnectionID, proofTry, proofClient, proofConsensus, proofHeight, consensusHeight, ) @@ -634,41 +605,35 @@ func (suite *KeeperTestSuite) TestConnOpenAck() { // TestConnOpenConfirm - chainB calls ConnOpenConfirm to confirm that // chainA state is now OPEN. func (suite *KeeperTestSuite) TestConnOpenConfirm() { - var ( - clientA string - clientB string - ) + var path *ibctesting.Path testCases := []struct { msg string malleate func() expPass bool }{ {"success", func() { - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) - err = suite.coordinator.ConnOpenAck(suite.chainA, suite.chainB, connA, connB) + err = path.EndpointA.ConnOpenAck() suite.Require().NoError(err) }, true}, {"connection not found", func() { // connections are never created - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) }, false}, {"chain B's connection state is not TRYOPEN", func() { // connections are OPEN - clientA, clientB, _, _ = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.CreateConnections(path) }, false}, {"connection state verification failed", func() { // chainA is in INIT - clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() suite.Require().NoError(err) }, false}, } @@ -678,17 +643,20 @@ func (suite *KeeperTestSuite) TestConnOpenConfirm() { suite.Run(tc.msg, func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) tc.malleate() - connA := suite.chainA.GetFirstTestConnection(clientA, clientB) - connB := suite.chainB.GetFirstTestConnection(clientB, clientA) + // ensure client is up to date to receive proof + err := path.EndpointB.UpdateClient() + suite.Require().NoError(err) - connectionKey := host.ConnectionKey(connA.ID) + connectionKey := host.ConnectionKey(path.EndpointA.ConnectionID) proofAck, proofHeight := suite.chainA.QueryProof(connectionKey) - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.ConnOpenConfirm( - suite.chainB.GetContext(), connB.ID, proofAck, proofHeight, + err = suite.chainB.App.GetIBCKeeper().ConnectionKeeper.ConnOpenConfirm( + suite.chainB.GetContext(), path.EndpointB.ConnectionID, proofAck, proofHeight, ) if tc.expPass { diff --git a/modules/core/03-connection/keeper/keeper_test.go b/modules/core/03-connection/keeper/keeper_test.go index a6a6807efe9..d2eb86f6bee 100644 --- a/modules/core/03-connection/keeper/keeper_test.go +++ b/modules/core/03-connection/keeper/keeper_test.go @@ -7,7 +7,6 @@ import ( "github.com/stretchr/testify/suite" "github.com/cosmos/ibc-go/modules/core/03-connection/types" - "github.com/cosmos/ibc-go/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/testing" ) @@ -32,62 +31,80 @@ func TestKeeperTestSuite(t *testing.T) { } func (suite *KeeperTestSuite) TestSetAndGetConnection() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - connA := suite.chainA.GetFirstTestConnection(clientA, clientB) - _, existed := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetConnection(suite.chainA.GetContext(), connA.ID) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) + firstConnection := "connection-0" + + // check first connection does not exist + _, existed := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetConnection(suite.chainA.GetContext(), firstConnection) suite.Require().False(existed) - suite.coordinator.CreateConnection(suite.chainA, suite.chainB, clientA, clientB) - _, existed = suite.chainA.App.IBCKeeper.ConnectionKeeper.GetConnection(suite.chainA.GetContext(), connA.ID) + suite.coordinator.CreateConnections(path) + _, existed = suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetConnection(suite.chainA.GetContext(), firstConnection) suite.Require().True(existed) } func (suite *KeeperTestSuite) TestSetAndGetClientConnectionPaths() { - clientA, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - _, existed := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetClientConnectionPaths(suite.chainA.GetContext(), clientA) + + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) + + _, existed := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetClientConnectionPaths(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.False(existed) connections := []string{"connectionA", "connectionB"} - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetClientConnectionPaths(suite.chainA.GetContext(), clientA, connections) - paths, existed := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetClientConnectionPaths(suite.chainA.GetContext(), clientA) + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetClientConnectionPaths(suite.chainA.GetContext(), path.EndpointA.ClientID, connections) + paths, existed := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetClientConnectionPaths(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.True(existed) suite.EqualValues(connections, paths) } // create 2 connections: A0 - B0, A1 - B1 func (suite KeeperTestSuite) TestGetAllConnections() { - clientA, clientB, connA0, connB0 := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - connA1, connB1 := suite.coordinator.CreateConnection(suite.chainA, suite.chainB, clientA, clientB) + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path1) - counterpartyB0 := types.NewCounterparty(clientB, connB0.ID, suite.chainB.GetPrefix()) // connection B0 - counterpartyB1 := types.NewCounterparty(clientB, connB1.ID, suite.chainB.GetPrefix()) // connection B1 + path2 := ibctesting.NewPath(suite.chainA, suite.chainB) + path2.EndpointA.ClientID = path1.EndpointA.ClientID + path2.EndpointB.ClientID = path1.EndpointB.ClientID - conn1 := types.NewConnectionEnd(types.OPEN, clientA, counterpartyB0, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) // A0 - B0 - conn2 := types.NewConnectionEnd(types.OPEN, clientA, counterpartyB1, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) // A1 - B1 + suite.coordinator.CreateConnections(path2) - iconn1 := types.NewIdentifiedConnection(connA0.ID, conn1) - iconn2 := types.NewIdentifiedConnection(connA1.ID, conn2) + counterpartyB0 := types.NewCounterparty(path1.EndpointB.ClientID, path1.EndpointB.ConnectionID, suite.chainB.GetPrefix()) // connection B0 + counterpartyB1 := types.NewCounterparty(path2.EndpointB.ClientID, path2.EndpointB.ConnectionID, suite.chainB.GetPrefix()) // connection B1 + + conn1 := types.NewConnectionEnd(types.OPEN, path1.EndpointA.ClientID, counterpartyB0, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) // A0 - B0 + conn2 := types.NewConnectionEnd(types.OPEN, path2.EndpointA.ClientID, counterpartyB1, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) // A1 - B1 + + iconn1 := types.NewIdentifiedConnection(path1.EndpointA.ConnectionID, conn1) + iconn2 := types.NewIdentifiedConnection(path2.EndpointA.ConnectionID, conn2) expConnections := []types.IdentifiedConnection{iconn1, iconn2} - connections := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetAllConnections(suite.chainA.GetContext()) + connections := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetAllConnections(suite.chainA.GetContext()) suite.Require().Len(connections, len(expConnections)) suite.Require().Equal(expConnections, connections) } -// the test creates 2 clients clientA0 and clientA1. clientA0 has a single -// connection and clientA1 has 2 connections. +// the test creates 2 clients path.EndpointA.ClientID0 and path.EndpointA.ClientID1. path.EndpointA.ClientID0 has a single +// connection and path.EndpointA.ClientID1 has 2 connections. func (suite KeeperTestSuite) TestGetAllClientConnectionPaths() { - clientA0, _, connA0, _ := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - clientA1, clientB1, connA1, _ := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - connA2, _ := suite.coordinator.CreateConnection(suite.chainA, suite.chainB, clientA1, clientB1) + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + path2 := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path1) + suite.coordinator.SetupConnections(path2) + + path3 := ibctesting.NewPath(suite.chainA, suite.chainB) + path3.EndpointA.ClientID = path2.EndpointA.ClientID + path3.EndpointB.ClientID = path2.EndpointB.ClientID + suite.coordinator.CreateConnections(path3) expPaths := []types.ConnectionPaths{ - types.NewConnectionPaths(clientA0, []string{connA0.ID}), - types.NewConnectionPaths(clientA1, []string{connA1.ID, connA2.ID}), + types.NewConnectionPaths(path1.EndpointA.ClientID, []string{path1.EndpointA.ConnectionID}), + types.NewConnectionPaths(path2.EndpointA.ClientID, []string{path2.EndpointA.ConnectionID, path3.EndpointA.ConnectionID}), } - connPaths := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetAllClientConnectionPaths(suite.chainA.GetContext()) + connPaths := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetAllClientConnectionPaths(suite.chainA.GetContext()) suite.Require().Len(connPaths, 2) suite.Require().Equal(expPaths, connPaths) } @@ -103,8 +120,9 @@ func (suite *KeeperTestSuite) TestGetTimestampAtHeight() { expPass bool }{ {"verification success", func() { - _, _, connA, _ := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - connection = suite.chainA.GetConnection(connA) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + connection = path.EndpointA.GetConnection() }, true}, {"consensus state not found", func() { // any non-nil value of connection is valid @@ -118,7 +136,7 @@ func (suite *KeeperTestSuite) TestGetTimestampAtHeight() { tc.malleate() - actualTimestamp, err := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetTimestampAtHeight( + actualTimestamp, err := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetTimestampAtHeight( suite.chainA.GetContext(), connection, suite.chainB.LastHeader.GetHeight(), ) diff --git a/modules/core/03-connection/keeper/verify_test.go b/modules/core/03-connection/keeper/verify_test.go index 9afd4816943..2c63ea97a03 100644 --- a/modules/core/03-connection/keeper/verify_test.go +++ b/modules/core/03-connection/keeper/verify_test.go @@ -17,7 +17,7 @@ import ( var defaultTimeoutHeight = clienttypes.NewHeight(0, 100000) // TestVerifyClientState verifies a client state of chainA -// stored on clientB (which is on chainB) +// stored on path.EndpointB (which is on chainB) func (suite *KeeperTestSuite) TestVerifyClientState() { cases := []struct { msg string @@ -38,9 +38,10 @@ func (suite *KeeperTestSuite) TestVerifyClientState() { suite.Run(tc.msg, func() { suite.SetupTest() // reset - _, clientB, connA, _ := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) - counterpartyClient, clientProof := suite.chainB.QueryClientStateProof(clientB) + counterpartyClient, clientProof := path.EndpointB.QueryClientStateProof() proofHeight := clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()-1)) if tc.malleateCounterparty { @@ -48,12 +49,12 @@ func (suite *KeeperTestSuite) TestVerifyClientState() { tmClient.ChainId = "wrongChainID" } - connection := suite.chainA.GetConnection(connA) + connection := path.EndpointA.GetConnection() if tc.changeClientID { connection.ClientId = ibctesting.InvalidID } - err := suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyClientState( + err := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.VerifyClientState( suite.chainA.GetContext(), connection, malleateHeight(proofHeight, tc.heightDiff), clientProof, counterpartyClient, ) @@ -68,12 +69,11 @@ func (suite *KeeperTestSuite) TestVerifyClientState() { } // TestVerifyClientConsensusState verifies that the consensus state of -// chainA stored on clientB (which is on chainB) matches the consensus +// chainA stored on path.EndpointB.ClientID (which is on chainB) matches the consensus // state for chainA at that height. func (suite *KeeperTestSuite) TestVerifyClientConsensusState() { var ( - connA *ibctesting.TestConnection - connB *ibctesting.TestConnection + path *ibctesting.Path changeClientID bool heightDiff uint64 ) @@ -83,32 +83,25 @@ func (suite *KeeperTestSuite) TestVerifyClientConsensusState() { expPass bool }{ {"verification success", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) }, true}, {"client state not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - changeClientID = true }, false}, {"consensus state not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - heightDiff = 5 }, false}, {"verification failed", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - clientB := connB.ClientID - clientState := suite.chainB.GetClientState(clientB) + clientState := suite.chainB.GetClientState(path.EndpointB.ClientID) // give chainB wrong consensus state for chainA - consState, found := suite.chainB.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainB.GetContext(), clientB) + consState, found := suite.chainB.App.GetIBCKeeper().ClientKeeper.GetLatestClientConsensusState(suite.chainB.GetContext(), path.EndpointB.ClientID) suite.Require().True(found) tmConsState, ok := consState.(*ibctmtypes.ConsensusState) suite.Require().True(ok) tmConsState.Timestamp = time.Now() - suite.chainB.App.IBCKeeper.ClientKeeper.SetClientConsensusState(suite.chainB.GetContext(), clientB, clientState.GetLatestHeight(), tmConsState) + suite.chainB.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(suite.chainB.GetContext(), path.EndpointB.ClientID, clientState.GetLatestHeight(), tmConsState) suite.coordinator.CommitBlock(suite.chainB) }, false}, @@ -121,20 +114,22 @@ func (suite *KeeperTestSuite) TestVerifyClientConsensusState() { suite.SetupTest() // reset heightDiff = 0 // must be explicitly changed in malleate changeClientID = false // must be explicitly changed in malleate + path = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) tc.malleate() - connection := suite.chainA.GetConnection(connA) + connection := path.EndpointA.GetConnection() if changeClientID { connection.ClientId = ibctesting.InvalidID } - proof, consensusHeight := suite.chainB.QueryConsensusStateProof(connB.ClientID) + proof, consensusHeight := suite.chainB.QueryConsensusStateProof(path.EndpointB.ClientID) proofHeight := clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()-1)) - consensusState, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetSelfConsensusState(suite.chainA.GetContext(), consensusHeight) + consensusState, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetSelfConsensusState(suite.chainA.GetContext(), consensusHeight) suite.Require().True(found) - err := suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyClientConsensusState( + err := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.VerifyClientConsensusState( suite.chainA.GetContext(), connection, malleateHeight(proofHeight, heightDiff), consensusHeight, proof, consensusState, ) @@ -170,24 +165,25 @@ func (suite *KeeperTestSuite) TestVerifyConnectionState() { suite.Run(tc.msg, func() { suite.SetupTest() // reset - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) - connection := suite.chainA.GetConnection(connA) + connection := path.EndpointA.GetConnection() if tc.changeClientID { connection.ClientId = ibctesting.InvalidID } - expectedConnection := suite.chainB.GetConnection(connB) + expectedConnection := path.EndpointB.GetConnection() - connectionKey := host.ConnectionKey(connB.ID) + connectionKey := host.ConnectionKey(path.EndpointB.ConnectionID) proof, proofHeight := suite.chainB.QueryProof(connectionKey) if tc.changeConnectionState { expectedConnection.State = types.TRYOPEN } - err := suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyConnectionState( + err := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.VerifyConnectionState( suite.chainA.GetContext(), connection, - malleateHeight(proofHeight, tc.heightDiff), proof, connB.ID, expectedConnection, + malleateHeight(proofHeight, tc.heightDiff), proof, path.EndpointB.ConnectionID, expectedConnection, ) if tc.expPass { @@ -221,23 +217,24 @@ func (suite *KeeperTestSuite) TestVerifyChannelState() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset - _, _, connA, _, _, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - connection := suite.chainA.GetConnection(connA) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + connection := path.EndpointA.GetConnection() if tc.changeClientID { connection.ClientId = ibctesting.InvalidID } - channelKey := host.ChannelKey(channelB.PortID, channelB.ID) + channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) proof, proofHeight := suite.chainB.QueryProof(channelKey) - channel := suite.chainB.GetChannel(channelB) + channel := path.EndpointB.GetChannel() if tc.changeChannelState { channel.State = channeltypes.TRYOPEN } - err := suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyChannelState( + err := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.VerifyChannelState( suite.chainA.GetContext(), connection, malleateHeight(proofHeight, tc.heightDiff), proof, - channelB.PortID, channelB.ID, channel, + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, channel, ) if tc.expPass { @@ -275,16 +272,17 @@ func (suite *KeeperTestSuite) TestVerifyPacketCommitment() { suite.Run(tc.msg, func() { suite.SetupTest() // reset - _, clientB, _, connB, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) - connection := suite.chainB.GetConnection(connB) + connection := path.EndpointB.GetConnection() connection.DelayPeriod = tc.delayPeriod if tc.changeClientID { connection.ClientId = ibctesting.InvalidID } - packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, defaultTimeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, 0) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) commitmentKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) @@ -294,8 +292,8 @@ func (suite *KeeperTestSuite) TestVerifyPacketCommitment() { packet.Data = []byte(ibctesting.InvalidID) } - commitment := channeltypes.CommitPacket(suite.chainB.App.IBCKeeper.Codec(), packet) - err = suite.chainB.App.IBCKeeper.ConnectionKeeper.VerifyPacketCommitment( + commitment := channeltypes.CommitPacket(suite.chainB.App.GetIBCKeeper().Codec(), packet) + err = suite.chainB.App.GetIBCKeeper().ConnectionKeeper.VerifyPacketCommitment( suite.chainB.GetContext(), connection, malleateHeight(proofHeight, tc.heightDiff), proof, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence(), commitment, ) @@ -335,24 +333,25 @@ func (suite *KeeperTestSuite) TestVerifyPacketAcknowledgement() { suite.Run(tc.msg, func() { suite.SetupTest() // reset - clientA, clientB, connA, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) - connection := suite.chainA.GetConnection(connA) + connection := path.EndpointA.GetConnection() connection.DelayPeriod = tc.delayPeriod if tc.changeClientID { connection.ClientId = ibctesting.InvalidID } // send and receive packet - packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, defaultTimeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, 0) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // increment receiving chain's (chainB) time by 2 hour to always pass receive suite.coordinator.IncrementTimeBy(time.Hour * 2) suite.coordinator.CommitBlock(suite.chainB) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) packetAckKey := host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) @@ -363,7 +362,7 @@ func (suite *KeeperTestSuite) TestVerifyPacketAcknowledgement() { ack = ibcmock.MockFailAcknowledgement } - err = suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyPacketAcknowledgement( + err = suite.chainA.App.GetIBCKeeper().ConnectionKeeper.VerifyPacketAcknowledgement( suite.chainA.GetContext(), connection, malleateHeight(proofHeight, tc.heightDiff), proof, packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence(), ack.Acknowledgement(), ) @@ -403,17 +402,18 @@ func (suite *KeeperTestSuite) TestVerifyPacketReceiptAbsence() { suite.Run(tc.msg, func() { suite.SetupTest() // reset - clientA, clientB, connA, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) - connection := suite.chainA.GetConnection(connA) + connection := path.EndpointA.GetConnection() connection.DelayPeriod = tc.delayPeriod if tc.changeClientID { connection.ClientId = ibctesting.InvalidID } // send, only receive if specified - packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, defaultTimeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, 0) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) if tc.recvAck { @@ -421,18 +421,18 @@ func (suite *KeeperTestSuite) TestVerifyPacketReceiptAbsence() { suite.coordinator.IncrementTimeBy(time.Hour * 2) suite.coordinator.CommitBlock(suite.chainB) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) } else { // need to update height to prove absence suite.coordinator.CommitBlock(suite.chainA, suite.chainB) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() } packetReceiptKey := host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) proof, proofHeight := suite.chainB.QueryProof(packetReceiptKey) - err = suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyPacketReceiptAbsence( + err = suite.chainA.App.GetIBCKeeper().ConnectionKeeper.VerifyPacketReceiptAbsence( suite.chainA.GetContext(), connection, malleateHeight(proofHeight, tc.heightDiff), proof, packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence(), ) @@ -472,30 +472,31 @@ func (suite *KeeperTestSuite) TestVerifyNextSequenceRecv() { suite.Run(tc.msg, func() { suite.SetupTest() // reset - clientA, clientB, connA, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) - connection := suite.chainA.GetConnection(connA) + connection := path.EndpointA.GetConnection() connection.DelayPeriod = tc.delayPeriod if tc.changeClientID { connection.ClientId = ibctesting.InvalidID } // send and receive packet - packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, defaultTimeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, defaultTimeoutHeight, 0) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // increment receiving chain's (chainB) time by 2 hour to always pass receive suite.coordinator.IncrementTimeBy(time.Hour * 2) suite.coordinator.CommitBlock(suite.chainB) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) nextSeqRecvKey := host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) proof, proofHeight := suite.chainB.QueryProof(nextSeqRecvKey) - err = suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyNextSequenceRecv( + err = suite.chainA.App.GetIBCKeeper().ConnectionKeeper.VerifyNextSequenceRecv( suite.chainA.GetContext(), connection, malleateHeight(proofHeight, tc.heightDiff), proof, packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()+tc.offsetSeq, ) diff --git a/modules/core/04-channel/keeper/grpc_query_test.go b/modules/core/04-channel/keeper/grpc_query_test.go index c8af41fed85..88e3a717005 100644 --- a/modules/core/04-channel/keeper/grpc_query_test.go +++ b/modules/core/04-channel/keeper/grpc_query_test.go @@ -62,16 +62,19 @@ func (suite *KeeperTestSuite) TestQueryChannel() { { "success", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + // init channel - channelA, _, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - expChannel = suite.chainA.GetChannel(channelA) + expChannel = path.EndpointA.GetChannel() req = &types.QueryChannelRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, } }, true, @@ -126,31 +129,39 @@ func (suite *KeeperTestSuite) TestQueryChannels() { { "success", func() { - _, _, connA0, connB0, testchannel0, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) // channel0 on first connection on chainA counterparty0 := types.Counterparty{ - PortId: connB0.Channels[0].PortID, - ChannelId: connB0.Channels[0].ID, + PortId: path.EndpointB.ChannelConfig.PortID, + ChannelId: path.EndpointB.ChannelID, } - // channel1 is second channel on first connection on chainA - testchannel1, _ := suite.coordinator.CreateMockChannels(suite.chainA, suite.chainB, connA0, connB0, types.ORDERED) + // path1 creates a second channel on first connection on chainA + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + path1.SetChannelOrdered() + path1.EndpointA.ClientID = path.EndpointA.ClientID + path1.EndpointB.ClientID = path.EndpointB.ClientID + path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID + path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID + + suite.coordinator.CreateMockChannels(path1) counterparty1 := types.Counterparty{ - PortId: connB0.Channels[1].PortID, - ChannelId: connB0.Channels[1].ID, + PortId: path1.EndpointB.ChannelConfig.PortID, + ChannelId: path1.EndpointB.ChannelID, } channel0 := types.NewChannel( types.OPEN, types.UNORDERED, - counterparty0, []string{connA0.ID}, testchannel0.Version, + counterparty0, []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.Version, ) channel1 := types.NewChannel( types.OPEN, types.ORDERED, - counterparty1, []string{connA0.ID}, testchannel1.Version, + counterparty1, []string{path.EndpointA.ConnectionID}, path1.EndpointA.ChannelConfig.Version, ) - idCh0 := types.NewIdentifiedChannel(testchannel0.PortID, testchannel0.ID, channel0) - idCh1 := types.NewIdentifiedChannel(testchannel1.PortID, testchannel1.ID, channel1) + idCh0 := types.NewIdentifiedChannel(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel0) + idCh1 := types.NewIdentifiedChannel(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, channel1) expChannels = []*types.IdentifiedChannel{&idCh0, &idCh1} @@ -217,36 +228,44 @@ func (suite *KeeperTestSuite) TestQueryConnectionChannels() { { "success", func() { - _, _, connA0, connB0, testchannel0, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) // channel0 on first connection on chainA counterparty0 := types.Counterparty{ - PortId: connB0.Channels[0].PortID, - ChannelId: connB0.Channels[0].ID, + PortId: path.EndpointB.ChannelConfig.PortID, + ChannelId: path.EndpointB.ChannelID, } - // channel1 is second channel on first connection on chainA - testchannel1, _ := suite.coordinator.CreateMockChannels(suite.chainA, suite.chainB, connA0, connB0, types.ORDERED) + // path1 creates a second channel on first connection on chainA + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + path1.SetChannelOrdered() + path1.EndpointA.ClientID = path.EndpointA.ClientID + path1.EndpointB.ClientID = path.EndpointB.ClientID + path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID + path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID + + suite.coordinator.CreateMockChannels(path1) counterparty1 := types.Counterparty{ - PortId: connB0.Channels[1].PortID, - ChannelId: connB0.Channels[1].ID, + PortId: path1.EndpointB.ChannelConfig.PortID, + ChannelId: path1.EndpointB.ChannelID, } channel0 := types.NewChannel( types.OPEN, types.UNORDERED, - counterparty0, []string{connA0.ID}, testchannel0.Version, + counterparty0, []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.Version, ) channel1 := types.NewChannel( types.OPEN, types.ORDERED, - counterparty1, []string{connA0.ID}, testchannel1.Version, + counterparty1, []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.Version, ) - idCh0 := types.NewIdentifiedChannel(testchannel0.PortID, testchannel0.ID, channel0) - idCh1 := types.NewIdentifiedChannel(testchannel1.PortID, testchannel1.ID, channel1) + idCh0 := types.NewIdentifiedChannel(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel0) + idCh1 := types.NewIdentifiedChannel(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, channel1) expChannels = []*types.IdentifiedChannel{&idCh0, &idCh1} req = &types.QueryConnectionChannelsRequest{ - Connection: connA0.ID, + Connection: path.EndpointA.ConnectionID, Pagination: &query.PageRequest{ Key: nil, Limit: 2, @@ -259,7 +278,8 @@ func (suite *KeeperTestSuite) TestQueryConnectionChannels() { { "success, empty response", func() { - suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expChannels = []*types.IdentifiedChannel{} req = &types.QueryConnectionChannelsRequest{ Connection: "externalConnID", @@ -345,49 +365,54 @@ func (suite *KeeperTestSuite) TestQueryChannelClientState() { { "connection not found", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) - channel := suite.chainA.GetChannel(channelA) + channel := path.EndpointA.GetChannel() // update channel to reference a connection that does not exist channel.ConnectionHops[0] = "doesnotexist" // set connection hops to wrong connection ID - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), channelA.PortID, channelA.ID, channel) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel) req = &types.QueryChannelClientStateRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, } }, false, }, { "client state for channel's connection not found", func() { - _, _, connA, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) // set connection to empty so clientID is empty - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainA.GetContext(), connA.ID, connectiontypes.ConnectionEnd{}) + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, connectiontypes.ConnectionEnd{}) req = &types.QueryChannelClientStateRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, } }, false, }, { "success", func() { - clientA, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + // init channel - channelA, _, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - expClientState := suite.chainA.GetClientState(clientA) - expIdentifiedClientState = clienttypes.NewIdentifiedClientState(clientA, expClientState) + expClientState := suite.chainA.GetClientState(path.EndpointA.ClientID) + expIdentifiedClientState = clienttypes.NewIdentifiedClientState(path.EndpointA.ClientID, expClientState) req = &types.QueryChannelClientStateRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, } }, true, @@ -476,18 +501,19 @@ func (suite *KeeperTestSuite) TestQueryChannelConsensusState() { { "connection not found", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) - channel := suite.chainA.GetChannel(channelA) + channel := path.EndpointA.GetChannel() // update channel to reference a connection that does not exist channel.ConnectionHops[0] = "doesnotexist" // set connection hops to wrong connection ID - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), channelA.PortID, channelA.ID, channel) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel) req = &types.QueryChannelConsensusStateRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, RevisionNumber: 0, RevisionHeight: 1, } @@ -496,11 +522,12 @@ func (suite *KeeperTestSuite) TestQueryChannelConsensusState() { { "consensus state for channel's connection not found", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) req = &types.QueryChannelConsensusStateRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, RevisionNumber: 0, RevisionHeight: uint64(suite.chainA.GetContext().BlockHeight()), // use current height } @@ -509,19 +536,22 @@ func (suite *KeeperTestSuite) TestQueryChannelConsensusState() { { "success", func() { - clientA, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + // init channel - channelA, _, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - clientState := suite.chainA.GetClientState(clientA) - expConsensusState, _ = suite.chainA.GetConsensusState(clientA, clientState.GetLatestHeight()) + clientState := suite.chainA.GetClientState(path.EndpointA.ClientID) + expConsensusState, _ = suite.chainA.GetConsensusState(path.EndpointA.ClientID, clientState.GetLatestHeight()) suite.Require().NotNil(expConsensusState) - expClientID = clientA + expClientID = path.EndpointA.ClientID req = &types.QueryChannelConsensusStateRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, RevisionNumber: clientState.GetLatestHeight().GetRevisionNumber(), RevisionHeight: clientState.GetLatestHeight().GetRevisionHeight(), } @@ -620,13 +650,14 @@ func (suite *KeeperTestSuite) TestQueryPacketCommitment() { { "success", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expCommitment = []byte("hash") - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), channelA.PortID, channelA.ID, 1, expCommitment) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, expCommitment) req = &types.QueryPacketCommitmentRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, Sequence: 1, } }, @@ -702,19 +733,20 @@ func (suite *KeeperTestSuite) TestQueryPacketCommitments() { { "success", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expCommitments = make([]*types.PacketState, 9) for i := uint64(0); i < 9; i++ { - commitment := types.NewPacketState(channelA.PortID, channelA.ID, i, []byte(fmt.Sprintf("hash_%d", i))) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), commitment.PortId, commitment.ChannelId, commitment.Sequence, commitment.Data) + commitment := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, i, []byte(fmt.Sprintf("hash_%d", i))) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), commitment.PortId, commitment.ChannelId, commitment.Sequence, commitment.Data) expCommitments[i] = &commitment } req = &types.QueryPacketCommitmentsRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, Pagination: &query.PageRequest{ Key: nil, Limit: 11, @@ -799,12 +831,13 @@ func (suite *KeeperTestSuite) TestQueryPacketReceipt() { { "success: receipt not found", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketReceipt(suite.chainA.GetContext(), channelA.PortID, channelA.ID, 1) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1) req = &types.QueryPacketReceiptRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, Sequence: 3, } expReceived = false @@ -814,12 +847,13 @@ func (suite *KeeperTestSuite) TestQueryPacketReceipt() { { "success: receipt found", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketReceipt(suite.chainA.GetContext(), channelA.PortID, channelA.ID, 1) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1) req = &types.QueryPacketReceiptRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, Sequence: 1, } expReceived = true @@ -911,13 +945,14 @@ func (suite *KeeperTestSuite) TestQueryPacketAcknowledgement() { { "success", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expAck = []byte("hash") - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(suite.chainA.GetContext(), channelA.PortID, channelA.ID, 1, expAck) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketAcknowledgement(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, expAck) req = &types.QueryPacketAcknowledgementRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, Sequence: 1, } }, @@ -993,19 +1028,20 @@ func (suite *KeeperTestSuite) TestQueryPacketAcknowledgements() { { "success", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expAcknowledgements = make([]*types.PacketState, 9) for i := uint64(0); i < 9; i++ { - ack := types.NewPacketState(channelA.PortID, channelA.ID, i, []byte(fmt.Sprintf("hash_%d", i))) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(suite.chainA.GetContext(), ack.PortId, ack.ChannelId, ack.Sequence, ack.Data) + ack := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, i, []byte(fmt.Sprintf("hash_%d", i))) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketAcknowledgement(suite.chainA.GetContext(), ack.PortId, ack.ChannelId, ack.Sequence, ack.Data) expAcknowledgements[i] = &ack } req = &types.QueryPacketAcknowledgementsRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, Pagination: &query.PageRequest{ Key: nil, Limit: 11, @@ -1089,14 +1125,15 @@ func (suite *KeeperTestSuite) TestQueryUnreceivedPackets() { { "basic success unreceived packet commitments", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) // no ack exists expSeq = []uint64{1} req = &types.QueryUnreceivedPacketsRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, PacketCommitmentSequences: []uint64{1}, } }, @@ -1105,14 +1142,15 @@ func (suite *KeeperTestSuite) TestQueryUnreceivedPackets() { { "basic success unreceived packet commitments, nothing to relay", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketReceipt(suite.chainA.GetContext(), channelA.PortID, channelA.ID, 1) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1) expSeq = []uint64{} req = &types.QueryUnreceivedPacketsRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, PacketCommitmentSequences: []uint64{1}, } }, @@ -1121,7 +1159,8 @@ func (suite *KeeperTestSuite) TestQueryUnreceivedPackets() { { "success multiple unreceived packet commitments", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expSeq = []uint64{} // reset packetCommitments := []uint64{} @@ -1130,15 +1169,15 @@ func (suite *KeeperTestSuite) TestQueryUnreceivedPackets() { packetCommitments = append(packetCommitments, seq) if seq%2 == 0 { - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketReceipt(suite.chainA.GetContext(), channelA.PortID, channelA.ID, seq) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketReceipt(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq) } else { expSeq = append(expSeq, seq) } } req = &types.QueryUnreceivedPacketsRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, PacketCommitmentSequences: packetCommitments, } }, @@ -1218,14 +1257,15 @@ func (suite *KeeperTestSuite) TestQueryUnreceivedAcks() { { "basic success unreceived packet acks", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), channelA.PortID, channelA.ID, 1, []byte("commitment")) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, []byte("commitment")) expSeq = []uint64{1} req = &types.QueryUnreceivedAcksRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, PacketAckSequences: []uint64{1}, } }, @@ -1234,12 +1274,13 @@ func (suite *KeeperTestSuite) TestQueryUnreceivedAcks() { { "basic success unreceived packet acknowledgements, nothing to relay", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expSeq = []uint64{} req = &types.QueryUnreceivedAcksRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, PacketAckSequences: []uint64{1}, } }, @@ -1248,7 +1289,8 @@ func (suite *KeeperTestSuite) TestQueryUnreceivedAcks() { { "success multiple unreceived packet acknowledgements", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expSeq = []uint64{} // reset packetAcks := []uint64{} @@ -1257,14 +1299,14 @@ func (suite *KeeperTestSuite) TestQueryUnreceivedAcks() { packetAcks = append(packetAcks, seq) if seq%2 == 0 { - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), channelA.PortID, channelA.ID, seq, []byte("commitement")) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq, []byte("commitement")) expSeq = append(expSeq, seq) } } req = &types.QueryUnreceivedAcksRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, PacketAckSequences: packetAcks, } }, @@ -1342,13 +1384,14 @@ func (suite *KeeperTestSuite) TestQueryNextSequenceReceive() { { "success", func() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) expSeq = 1 - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(suite.chainA.GetContext(), channelA.PortID, channelA.ID, expSeq) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceRecv(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, expSeq) req = &types.QueryNextSequenceReceiveRequest{ - PortId: channelA.PortID, - ChannelId: channelA.ID, + PortId: path.EndpointA.ChannelConfig.PortID, + ChannelId: path.EndpointA.ChannelID, } }, true, diff --git a/modules/core/04-channel/keeper/handshake_test.go b/modules/core/04-channel/keeper/handshake_test.go index a79de391fe1..19535792833 100644 --- a/modules/core/04-channel/keeper/handshake_test.go +++ b/modules/core/04-channel/keeper/handshake_test.go @@ -24,66 +24,65 @@ type testCase = struct { // can succeed. func (suite *KeeperTestSuite) TestChanOpenInit() { var ( - connA *ibctesting.TestConnection - connB *ibctesting.TestConnection + path *ibctesting.Path features []string portCap *capabilitytypes.Capability ) testCases := []testCase{ {"success", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupConnections(path) features = []string{"ORDER_ORDERED", "ORDER_UNORDERED"} - suite.chainA.CreatePortCapability(suite.chainA.NextTestChannel(connA, ibctesting.MockPort).PortID) - portCap = suite.chainA.GetPortCapability(suite.chainA.NextTestChannel(connA, ibctesting.MockPort).PortID) + suite.chainA.CreatePortCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainA.GetPortCapability(ibctesting.MockPort) }, true}, {"channel already exists", func() { - _, _, connA, connB, _, _ = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) }, false}, {"connection doesn't exist", func() { - // any non-nil values of connA and connB are acceptable - suite.Require().NotNil(connA) - suite.Require().NotNil(connB) + // any non-empty values + path.EndpointA.ConnectionID = "connection-0" + path.EndpointB.ConnectionID = "connection-0" }, false}, {"capability is incorrect", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupConnections(path) features = []string{"ORDER_ORDERED", "ORDER_UNORDERED"} portCap = capabilitytypes.NewCapability(3) }, false}, {"connection version not negotiated", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupConnections(path) // modify connA versions - conn := suite.chainA.GetConnection(connA) + conn := path.EndpointA.GetConnection() version := connectiontypes.NewVersion("2", []string{"ORDER_ORDERED", "ORDER_UNORDERED"}) conn.Versions = append(conn.Versions, version) - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection( + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection( suite.chainA.GetContext(), - connA.ID, conn, + path.EndpointA.ConnectionID, conn, ) features = []string{"ORDER_ORDERED", "ORDER_UNORDERED"} - suite.chainA.CreatePortCapability(suite.chainA.NextTestChannel(connA, ibctesting.MockPort).PortID) - portCap = suite.chainA.GetPortCapability(suite.chainA.NextTestChannel(connA, ibctesting.MockPort).PortID) + suite.chainA.CreatePortCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainA.GetPortCapability(ibctesting.MockPort) }, false}, {"connection does not support ORDERED channels", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupConnections(path) // modify connA versions to only support UNORDERED channels - conn := suite.chainA.GetConnection(connA) + conn := path.EndpointA.GetConnection() version := connectiontypes.NewVersion("1", []string{"ORDER_UNORDERED"}) conn.Versions = []*connectiontypes.Version{version} - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection( + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection( suite.chainA.GetContext(), - connA.ID, conn, + path.EndpointA.ConnectionID, conn, ) // NOTE: Opening UNORDERED channels is still expected to pass but ORDERED channels should fail features = []string{"ORDER_UNORDERED"} - suite.chainA.CreatePortCapability(suite.chainA.NextTestChannel(connA, ibctesting.MockPort).PortID) - portCap = suite.chainA.GetPortCapability(suite.chainA.NextTestChannel(connA, ibctesting.MockPort).PortID) + suite.chainA.CreatePortCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainA.GetPortCapability(ibctesting.MockPort) }, true}, } @@ -93,14 +92,17 @@ func (suite *KeeperTestSuite) TestChanOpenInit() { // run test for all types of ordering for _, order := range []types.Order{types.UNORDERED, types.ORDERED} { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) + path.EndpointA.ChannelConfig.Order = order + path.EndpointB.ChannelConfig.Order = order + tc.malleate() - counterparty := types.NewCounterparty(connB.FirstOrNextTestChannel(ibctesting.MockPort).PortID, connB.FirstOrNextTestChannel(ibctesting.MockPort).ID) - channelA := connA.FirstOrNextTestChannel(ibctesting.MockPort) + counterparty := types.NewCounterparty(ibctesting.MockPort, ibctesting.FirstChannelID) - channelID, cap, err := suite.chainA.App.IBCKeeper.ChannelKeeper.ChanOpenInit( - suite.chainA.GetContext(), order, []string{connA.ID}, - channelA.PortID, portCap, counterparty, channelA.Version, + channelID, cap, err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanOpenInit( + suite.chainA.GetContext(), path.EndpointA.ChannelConfig.Order, []string{path.EndpointA.ConnectionID}, + path.EndpointA.ChannelConfig.PortID, portCap, counterparty, path.EndpointA.ChannelConfig.Version, ) // check if order is supported by channel to determine expected behaviour @@ -118,9 +120,9 @@ func (suite *KeeperTestSuite) TestChanOpenInit() { suite.Require().NotNil(cap) suite.Require().Equal(types.FormatChannelIdentifier(0), channelID) - chanCap, ok := suite.chainA.App.ScopedIBCKeeper.GetCapability( + chanCap, ok := suite.chainA.App.GetScopedIBCKeeper().GetCapability( suite.chainA.GetContext(), - host.ChannelCapabilityPath(channelA.PortID, channelA.ID), + host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, channelID), ) suite.Require().True(ok, "could not retrieve channel capability after successful ChanOpenInit") suite.Require().Equal(chanCap.String(), cap.String(), "channel capability is not correct") @@ -140,8 +142,7 @@ func (suite *KeeperTestSuite) TestChanOpenInit() { // ChanOpenTry can succeed. func (suite *KeeperTestSuite) TestChanOpenTry() { var ( - connA *ibctesting.TestConnection - connB *ibctesting.TestConnection + path *ibctesting.Path previousChannelID string portCap *capabilitytypes.Capability heightDiff uint64 @@ -149,98 +150,102 @@ func (suite *KeeperTestSuite) TestChanOpenTry() { testCases := []testCase{ {"success", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + path.EndpointA.ChanOpenInit() - suite.chainB.CreatePortCapability(suite.chainB.NextTestChannel(connB, ibctesting.MockPort).PortID) - portCap = suite.chainB.GetPortCapability(suite.chainB.NextTestChannel(connB, ibctesting.MockPort).PortID) + suite.chainB.CreatePortCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainB.GetPortCapability(ibctesting.MockPort) }, true}, {"success with crossing hello", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - _, channelB, err := suite.coordinator.ChanOpenInitOnBothChains(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + err := suite.coordinator.ChanOpenInitOnBothChains(path) suite.Require().NoError(err) - previousChannelID = channelB.ID - portCap = suite.chainB.GetPortCapability(suite.chainB.NextTestChannel(connB, ibctesting.MockPort).PortID) + previousChannelID = path.EndpointB.ChannelID + portCap = suite.chainB.GetPortCapability(ibctesting.MockPort) }, true}, {"previous channel with invalid state", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupConnections(path) // make previous channel have wrong ordering - suite.coordinator.ChanOpenInit(suite.chainB, suite.chainA, connB, connA, ibctesting.MockPort, ibctesting.MockPort, types.UNORDERED) + path.EndpointA.ChanOpenInit() }, false}, {"connection doesn't exist", func() { - // any non-nil values of connA and connB are acceptable - suite.Require().NotNil(connA) - suite.Require().NotNil(connB) + path.EndpointA.ConnectionID = ibctesting.FirstConnectionID + path.EndpointB.ConnectionID = ibctesting.FirstConnectionID // pass capability check - suite.chainB.CreatePortCapability(connB.FirstOrNextTestChannel(ibctesting.MockPort).PortID) - portCap = suite.chainB.GetPortCapability(connB.FirstOrNextTestChannel(ibctesting.MockPort).PortID) + suite.chainB.CreatePortCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainB.GetPortCapability(ibctesting.MockPort) }, false}, {"connection is not OPEN", func() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(path) // pass capability check - suite.chainB.CreatePortCapability(connB.FirstOrNextTestChannel(ibctesting.MockPort).PortID) - portCap = suite.chainB.GetPortCapability(connB.FirstOrNextTestChannel(ibctesting.MockPort).PortID) + suite.chainB.CreatePortCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainB.GetPortCapability(ibctesting.MockPort) - var err error - connB, connA, err = suite.coordinator.ConnOpenInit(suite.chainB, suite.chainA, clientB, clientA) + err := path.EndpointB.ConnOpenInit() suite.Require().NoError(err) }, false}, {"consensus state not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + path.EndpointA.ChanOpenInit() - suite.chainB.CreatePortCapability(suite.chainB.NextTestChannel(connB, ibctesting.MockPort).PortID) - portCap = suite.chainB.GetPortCapability(suite.chainB.NextTestChannel(connB, ibctesting.MockPort).PortID) + suite.chainB.CreatePortCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainB.GetPortCapability(ibctesting.MockPort) heightDiff = 3 // consensus state doesn't exist at this height }, false}, {"channel verification failed", func() { // not creating a channel on chainA will result in an invalid proof of existence - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - portCap = suite.chainB.GetPortCapability(suite.chainB.NextTestChannel(connB, ibctesting.MockPort).PortID) + suite.coordinator.SetupConnections(path) + portCap = suite.chainB.GetPortCapability(ibctesting.MockPort) }, false}, {"port capability not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + path.EndpointA.ChanOpenInit() portCap = capabilitytypes.NewCapability(3) }, false}, {"connection version not negotiated", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + path.EndpointA.ChanOpenInit() // modify connB versions - conn := suite.chainB.GetConnection(connB) + conn := path.EndpointB.GetConnection() version := connectiontypes.NewVersion("2", []string{"ORDER_ORDERED", "ORDER_UNORDERED"}) conn.Versions = append(conn.Versions, version) - suite.chainB.App.IBCKeeper.ConnectionKeeper.SetConnection( + suite.chainB.App.GetIBCKeeper().ConnectionKeeper.SetConnection( suite.chainB.GetContext(), - connB.ID, conn, + path.EndpointB.ConnectionID, conn, ) - suite.chainB.CreatePortCapability(suite.chainB.NextTestChannel(connB, ibctesting.MockPort).PortID) - portCap = suite.chainB.GetPortCapability(suite.chainB.NextTestChannel(connB, ibctesting.MockPort).PortID) + suite.chainB.CreatePortCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainB.GetPortCapability(ibctesting.MockPort) }, false}, {"connection does not support ORDERED channels", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + path.EndpointA.ChanOpenInit() // modify connA versions to only support UNORDERED channels - conn := suite.chainA.GetConnection(connA) + conn := path.EndpointA.GetConnection() version := connectiontypes.NewVersion("1", []string{"ORDER_UNORDERED"}) conn.Versions = []*connectiontypes.Version{version} - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection( + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection( suite.chainA.GetContext(), - connA.ID, conn, + path.EndpointA.ConnectionID, conn, ) - suite.chainA.CreatePortCapability(suite.chainA.NextTestChannel(connA, ibctesting.MockPort).PortID) - portCap = suite.chainA.GetPortCapability(suite.chainA.NextTestChannel(connA, ibctesting.MockPort).PortID) + suite.chainA.CreatePortCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, ibctesting.MockPort) + portCap = suite.chainA.GetPortCapability(ibctesting.MockPort) }, false}, } @@ -250,18 +255,24 @@ func (suite *KeeperTestSuite) TestChanOpenTry() { suite.SetupTest() // reset heightDiff = 0 // must be explicitly changed in malleate previousChannelID = "" + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - channelA := connA.FirstOrNextTestChannel(ibctesting.MockPort) - channelB := connB.FirstOrNextTestChannel(ibctesting.MockPort) - counterparty := types.NewCounterparty(channelA.PortID, channelA.ID) + + if path.EndpointB.ClientID != "" { + // ensure client is up to date + err := path.EndpointB.UpdateClient() + suite.Require().NoError(err) + } + + counterparty := types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID) channelKey := host.ChannelKey(counterparty.PortId, counterparty.ChannelId) proof, proofHeight := suite.chainA.QueryProof(channelKey) - channelID, cap, err := suite.chainB.App.IBCKeeper.ChannelKeeper.ChanOpenTry( - suite.chainB.GetContext(), types.ORDERED, []string{connB.ID}, - channelB.PortID, previousChannelID, portCap, counterparty, channelB.Version, connA.FirstOrNextTestChannel(ibctesting.MockPort).Version, + channelID, cap, err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanOpenTry( + suite.chainB.GetContext(), types.ORDERED, []string{path.EndpointB.ConnectionID}, + path.EndpointB.ChannelConfig.PortID, previousChannelID, portCap, counterparty, path.EndpointB.ChannelConfig.Version, path.EndpointA.ChannelConfig.Version, proof, malleateHeight(proofHeight, heightDiff), ) @@ -269,9 +280,9 @@ func (suite *KeeperTestSuite) TestChanOpenTry() { suite.Require().NoError(err) suite.Require().NotNil(cap) - chanCap, ok := suite.chainB.App.ScopedIBCKeeper.GetCapability( + chanCap, ok := suite.chainB.App.GetScopedIBCKeeper().GetCapability( suite.chainB.GetContext(), - host.ChannelCapabilityPath(channelB.PortID, channelID), + host.ChannelCapabilityPath(path.EndpointB.ChannelConfig.PortID, channelID), ) suite.Require().True(ok, "could not retrieve channel capapbility after successful ChanOpenTry") suite.Require().Equal(chanCap.String(), cap.String(), "channel capability is not correct") @@ -287,8 +298,7 @@ func (suite *KeeperTestSuite) TestChanOpenTry() { // call is occurring on chainA. func (suite *KeeperTestSuite) TestChanOpenAck() { var ( - connA *ibctesting.TestConnection - connB *ibctesting.TestConnection + path *ibctesting.Path counterpartyChannelID string channelCap *capabilitytypes.Capability heightDiff uint64 @@ -296,111 +306,122 @@ func (suite *KeeperTestSuite) TestChanOpenAck() { testCases := []testCase{ {"success", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"success with empty stored counterparty channel ID", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) // set the channel's counterparty channel identifier to empty string - channel := suite.chainA.GetChannel(channelA) + channel := path.EndpointA.GetChannel() channel.Counterparty.ChannelId = "" // use a different channel identifier - counterpartyChannelID = channelB.ID + counterpartyChannelID = path.EndpointB.ChannelID - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), channelA.PortID, channelA.ID, channel) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"channel doesn't exist", func() {}, false}, {"channel state is not INIT or TRYOPEN", func() { // create fully open channels on both chains - _, _, connA, connB, _, _ = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelA := connA.Channels[0] - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"connection not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) // set the channel's connection hops to wrong connection ID - channel := suite.chainA.GetChannel(channelA) + channel := path.EndpointA.GetChannel() channel.ConnectionHops[0] = "doesnotexist" - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), channelA.PortID, channelA.ID, channel) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel) }, false}, {"connection is not OPEN", func() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(path) - var err error - connA, connB, err = suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // create channel in init - channelA, _, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + path.SetChannelOrdered() + + err = path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"consensus state not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) heightDiff = 3 // consensus state doesn't exist at this height }, false}, {"invalid counterparty channel identifier", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) counterpartyChannelID = "otheridentifier" - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"channel verification failed", func() { // chainB is INIT, chainA in TRYOPEN - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelB, channelA, err := suite.coordinator.ChanOpenInit(suite.chainB, suite.chainA, connB, connA, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointB.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainA, suite.chainB, channelA, channelB, connA, types.ORDERED) + err = path.EndpointA.ChanOpenTry() suite.Require().NoError(err) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"channel capability not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + path.EndpointB.ChanOpenTry() channelCap = capabilitytypes.NewCapability(6) }, false}, @@ -412,21 +433,25 @@ func (suite *KeeperTestSuite) TestChanOpenAck() { suite.SetupTest() // reset counterpartyChannelID = "" // must be explicitly changed in malleate heightDiff = 0 // must be explicitly changed + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - channelA := connA.FirstOrNextTestChannel(ibctesting.MockPort) - channelB := connB.FirstOrNextTestChannel(ibctesting.MockPort) - if counterpartyChannelID == "" { - counterpartyChannelID = channelB.ID + counterpartyChannelID = ibctesting.FirstChannelID + } + + if path.EndpointA.ClientID != "" { + // ensure client is up to date + err := path.EndpointA.UpdateClient() + suite.Require().NoError(err) } - channelKey := host.ChannelKey(channelB.PortID, channelB.ID) + channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID) proof, proofHeight := suite.chainB.QueryProof(channelKey) - err := suite.chainA.App.IBCKeeper.ChannelKeeper.ChanOpenAck( - suite.chainA.GetContext(), channelA.PortID, channelA.ID, channelCap, channelB.Version, counterpartyChannelID, + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanOpenAck( + suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channelCap, path.EndpointB.ChannelConfig.Version, counterpartyChannelID, proof, malleateHeight(proofHeight, heightDiff), ) @@ -444,95 +469,102 @@ func (suite *KeeperTestSuite) TestChanOpenAck() { // call is occurring on chainB. func (suite *KeeperTestSuite) TestChanOpenConfirm() { var ( - connA *ibctesting.TestConnection - connB *ibctesting.TestConnection + path *ibctesting.Path channelCap *capabilitytypes.Capability heightDiff uint64 ) testCases := []testCase{ {"success", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenAck(suite.chainA, suite.chainB, channelA, channelB) + err = path.EndpointA.ChanOpenAck() suite.Require().NoError(err) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, true}, {"channel doesn't exist", func() {}, false}, {"channel state is not TRYOPEN", func() { // create fully open channels on both cahins - _, _, connA, connB, _, _ = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelB := connB.Channels[0] - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"connection not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenAck(suite.chainA, suite.chainB, channelA, channelB) + err = path.EndpointA.ChanOpenAck() suite.Require().NoError(err) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) // set the channel's connection hops to wrong connection ID - channel := suite.chainB.GetChannel(channelB) + channel := path.EndpointB.GetChannel() channel.ConnectionHops[0] = "doesnotexist" - suite.chainB.App.IBCKeeper.ChannelKeeper.SetChannel(suite.chainB.GetContext(), channelB.PortID, channelB.ID, channel) + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, channel) }, false}, {"connection is not OPEN", func() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(path) - var err error - connA, connB, err = suite.coordinator.ConnOpenInit(suite.chainB, suite.chainA, clientB, clientA) + err := path.EndpointB.ConnOpenInit() suite.Require().NoError(err) - channelB := connB.FirstOrNextTestChannel(ibctesting.MockPort) - suite.chainB.CreateChannelCapability(channelB.PortID, channelB.ID) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + + suite.chainB.CreateChannelCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID) }, false}, {"consensus state not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenAck(suite.chainA, suite.chainB, channelA, channelB) + err = path.EndpointA.ChanOpenAck() suite.Require().NoError(err) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) heightDiff = 3 }, false}, {"channel verification failed", func() { // chainA is INIT, chainB in TRYOPEN - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"channel capability not found", func() { - _, _, connA, connB = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + suite.coordinator.SetupConnections(path) + path.SetChannelOrdered() + + err := path.EndpointA.ChanOpenInit() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenTry(suite.chainB, suite.chainA, channelB, channelA, connB, types.ORDERED) + err = path.EndpointB.ChanOpenTry() suite.Require().NoError(err) - err = suite.coordinator.ChanOpenAck(suite.chainA, suite.chainB, channelA, channelB) + err = path.EndpointA.ChanOpenAck() suite.Require().NoError(err) channelCap = capabilitytypes.NewCapability(6) @@ -544,17 +576,22 @@ func (suite *KeeperTestSuite) TestChanOpenConfirm() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset heightDiff = 0 // must be explicitly changed + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - channelA := connA.FirstOrNextTestChannel(ibctesting.MockPort) - channelB := connB.FirstOrNextTestChannel(ibctesting.MockPort) + if path.EndpointB.ClientID != "" { + // ensure client is up to date + err := path.EndpointB.UpdateClient() + suite.Require().NoError(err) + + } - channelKey := host.ChannelKey(channelA.PortID, channelA.ID) + channelKey := host.ChannelKey(path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID) proof, proofHeight := suite.chainA.QueryProof(channelKey) - err := suite.chainB.App.IBCKeeper.ChannelKeeper.ChanOpenConfirm( - suite.chainB.GetContext(), channelB.PortID, channelB.ID, + err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanOpenConfirm( + suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap, proof, malleateHeight(proofHeight, heightDiff), ) @@ -571,62 +608,60 @@ func (suite *KeeperTestSuite) TestChanOpenConfirm() { // ChanCloseInit. Both chains will use message passing to setup OPEN channels. func (suite *KeeperTestSuite) TestChanCloseInit() { var ( - connA *ibctesting.TestConnection - connB *ibctesting.TestConnection + path *ibctesting.Path channelCap *capabilitytypes.Capability ) testCases := []testCase{ {"success", func() { - _, _, connA, connB, _, _ = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelA := connA.Channels[0] - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"channel doesn't exist", func() { // any non-nil values work for connections - suite.Require().NotNil(connA) - suite.Require().NotNil(connB) - channelA := connA.FirstOrNextTestChannel(ibctesting.MockPort) + path.EndpointA.ConnectionID = ibctesting.FirstConnectionID + path.EndpointB.ConnectionID = ibctesting.FirstConnectionID + + path.EndpointA.ChannelID = ibctesting.FirstChannelID + path.EndpointB.ChannelID = ibctesting.FirstChannelID // ensure channel capability check passes - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"channel state is CLOSED", func() { - _, _, connA, connB, _, _ = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelA := connA.Channels[0] - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) // close channel - err := suite.coordinator.SetChannelClosed(suite.chainA, suite.chainB, channelA) + err := path.EndpointA.SetChannelClosed() suite.Require().NoError(err) }, false}, {"connection not found", func() { - _, _, connA, connB, _, _ = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelA := connA.Channels[0] - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) // set the channel's connection hops to wrong connection ID - channel := suite.chainA.GetChannel(channelA) + channel := path.EndpointA.GetChannel() channel.ConnectionHops[0] = "doesnotexist" - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), channelA.PortID, channelA.ID, channel) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel) }, false}, {"connection is not OPEN", func() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(path) - var err error - connA, connB, err = suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) // create channel in init - channelA, _, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + path.SetChannelOrdered() + err = path.EndpointA.ChanOpenInit() // ensure channel capability check passes - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"channel capability not found", func() { - _, _, connA, connB, _, _ = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) channelCap = capabilitytypes.NewCapability(3) }, false}, } @@ -635,13 +670,12 @@ func (suite *KeeperTestSuite) TestChanCloseInit() { tc := tc suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - channelA := connA.FirstOrNextTestChannel(ibctesting.MockPort) - - err := suite.chainA.App.IBCKeeper.ChannelKeeper.ChanCloseInit( - suite.chainA.GetContext(), channelA.PortID, channelA.ID, channelCap, + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.ChanCloseInit( + suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap, ) if tc.expPass { @@ -658,82 +692,78 @@ func (suite *KeeperTestSuite) TestChanCloseInit() { // bypassed on chainA by setting the channel state in the ChannelKeeper. func (suite *KeeperTestSuite) TestChanCloseConfirm() { var ( - connA *ibctesting.TestConnection - connB *ibctesting.TestConnection - channelA ibctesting.TestChannel - channelB ibctesting.TestChannel + path *ibctesting.Path channelCap *capabilitytypes.Capability heightDiff uint64 ) testCases := []testCase{ {"success", func() { - _, _, connA, connB, channelA, channelB = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) - err := suite.coordinator.SetChannelClosed(suite.chainA, suite.chainB, channelA) + err := path.EndpointA.SetChannelClosed() suite.Require().NoError(err) }, true}, {"channel doesn't exist", func() { // any non-nil values work for connections - suite.Require().NotNil(connA) - suite.Require().NotNil(connB) - channelB = connB.FirstOrNextTestChannel(ibctesting.MockPort) + path.EndpointA.ChannelID = ibctesting.FirstChannelID + path.EndpointB.ChannelID = ibctesting.FirstChannelID // ensure channel capability check passes - suite.chainB.CreateChannelCapability(channelB.PortID, channelB.ID) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.chainB.CreateChannelCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID) }, false}, {"channel state is CLOSED", func() { - _, _, connA, connB, _, channelB = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) - err := suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) + err := path.EndpointB.SetChannelClosed() suite.Require().NoError(err) }, false}, {"connection not found", func() { - _, _, connA, connB, _, channelB = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) // set the channel's connection hops to wrong connection ID - channel := suite.chainB.GetChannel(channelB) + channel := path.EndpointB.GetChannel() channel.ConnectionHops[0] = "doesnotexist" - suite.chainB.App.IBCKeeper.ChannelKeeper.SetChannel(suite.chainB.GetContext(), channelB.PortID, channelB.ID, channel) + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, channel) }, false}, {"connection is not OPEN", func() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(path) - var err error - connB, connA, err = suite.coordinator.ConnOpenInit(suite.chainB, suite.chainA, clientB, clientA) + err := path.EndpointB.ConnOpenInit() suite.Require().NoError(err) // create channel in init - channelB, _, err := suite.coordinator.ChanOpenInit(suite.chainB, suite.chainA, connB, connA, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + path.SetChannelOrdered() + err = path.EndpointB.ChanOpenInit() suite.Require().NoError(err) // ensure channel capability check passes - suite.chainB.CreateChannelCapability(channelB.PortID, channelB.ID) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.chainB.CreateChannelCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"consensus state not found", func() { - _, _, connA, connB, channelA, channelB = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) - err := suite.coordinator.SetChannelClosed(suite.chainA, suite.chainB, channelA) + err := path.EndpointA.SetChannelClosed() suite.Require().NoError(err) heightDiff = 3 }, false}, {"channel verification failed", func() { // channel not closed - _, _, connA, connB, _, channelB = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"channel capability not found", func() { - _, _, connA, connB, channelA, channelB = suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) - err := suite.coordinator.SetChannelClosed(suite.chainA, suite.chainB, channelA) + err := path.EndpointA.SetChannelClosed() suite.Require().NoError(err) channelCap = capabilitytypes.NewCapability(3) @@ -745,17 +775,15 @@ func (suite *KeeperTestSuite) TestChanCloseConfirm() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset heightDiff = 0 // must explicitly be changed + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - channelA = connA.FirstOrNextTestChannel(ibctesting.MockPort) - channelB = connB.FirstOrNextTestChannel(ibctesting.MockPort) - - channelKey := host.ChannelKey(channelA.PortID, channelA.ID) + channelKey := host.ChannelKey(path.EndpointA.ChannelConfig.PortID, ibctesting.FirstChannelID) proof, proofHeight := suite.chainA.QueryProof(channelKey) - err := suite.chainB.App.IBCKeeper.ChannelKeeper.ChanCloseConfirm( - suite.chainB.GetContext(), channelB.PortID, channelB.ID, channelCap, + err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.ChanCloseConfirm( + suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, ibctesting.FirstChannelID, channelCap, proof, malleateHeight(proofHeight, heightDiff), ) diff --git a/modules/core/04-channel/keeper/keeper_test.go b/modules/core/04-channel/keeper/keeper_test.go index 531cbb50c17..74899f4abe9 100644 --- a/modules/core/04-channel/keeper/keeper_test.go +++ b/modules/core/04-channel/keeper/keeper_test.go @@ -6,7 +6,6 @@ import ( "github.com/stretchr/testify/suite" "github.com/cosmos/ibc-go/modules/core/04-channel/types" - "github.com/cosmos/ibc-go/modules/core/exported" ibctesting "github.com/cosmos/ibc-go/testing" ) @@ -40,20 +39,22 @@ func (suite *KeeperTestSuite) SetupTest() { // and existence of a channel in INIT on chainA. func (suite *KeeperTestSuite) TestSetChannel() { // create client and connections on both chains - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) // check for channel to be created on chainA - channelA := suite.chainA.NextTestChannel(connA, ibctesting.MockPort) - _, found := suite.chainA.App.IBCKeeper.ChannelKeeper.GetChannel(suite.chainA.GetContext(), channelA.PortID, channelA.ID) + _, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.False(found) + path.SetChannelOrdered() + // init channel - channelA, channelB, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA, connB, ibctesting.MockPort, ibctesting.MockPort, types.ORDERED) + err := path.EndpointA.ChanOpenInit() suite.NoError(err) - storedChannel, found := suite.chainA.App.IBCKeeper.ChannelKeeper.GetChannel(suite.chainA.GetContext(), channelA.PortID, channelA.ID) + storedChannel, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) // counterparty channel id is empty after open init - expectedCounterparty := types.NewCounterparty(channelB.PortID, "") + expectedCounterparty := types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, "") suite.True(found) suite.Equal(types.INIT, storedChannel.State) @@ -64,54 +65,63 @@ func (suite *KeeperTestSuite) TestSetChannel() { // TestGetAllChannels creates multiple channels on chain A through various connections // and tests their retrieval. 2 channels are on connA0 and 1 channel is on connA1 func (suite KeeperTestSuite) TestGetAllChannels() { - clientA, clientB, connA0, connB0, testchannel0, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) // channel0 on first connection on chainA counterparty0 := types.Counterparty{ - PortId: connB0.Channels[0].PortID, - ChannelId: connB0.Channels[0].ID, + PortId: path.EndpointB.ChannelConfig.PortID, + ChannelId: path.EndpointB.ChannelID, } - // channel1 is second channel on first connection on chainA - testchannel1, _ := suite.coordinator.CreateMockChannels(suite.chainA, suite.chainB, connA0, connB0, types.ORDERED) + // path1 creates a second channel on first connection on chainA + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + path1.SetChannelOrdered() + path1.EndpointA.ClientID = path.EndpointA.ClientID + path1.EndpointB.ClientID = path.EndpointB.ClientID + path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID + path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID + + suite.coordinator.CreateMockChannels(path1) counterparty1 := types.Counterparty{ - PortId: connB0.Channels[1].PortID, - ChannelId: connB0.Channels[1].ID, + PortId: path1.EndpointB.ChannelConfig.PortID, + ChannelId: path1.EndpointB.ChannelID, } - connA1, connB1 := suite.coordinator.CreateConnection(suite.chainA, suite.chainB, clientA, clientB) + path2 := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path2) - // channel2 is on a second connection on chainA - testchannel2, _, err := suite.coordinator.ChanOpenInit(suite.chainA, suite.chainB, connA1, connB1, ibctesting.MockPort, ibctesting.MockPort, types.UNORDERED) + // path2 creates a second channel on chainA + err := path2.EndpointA.ChanOpenInit() suite.Require().NoError(err) // counterparty channel id is empty after open init counterparty2 := types.Counterparty{ - PortId: connB1.Channels[0].PortID, + PortId: path2.EndpointB.ChannelConfig.PortID, ChannelId: "", } channel0 := types.NewChannel( types.OPEN, types.UNORDERED, - counterparty0, []string{connA0.ID}, testchannel0.Version, + counterparty0, []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.Version, ) channel1 := types.NewChannel( types.OPEN, types.ORDERED, - counterparty1, []string{connA0.ID}, testchannel1.Version, + counterparty1, []string{path1.EndpointA.ConnectionID}, path1.EndpointA.ChannelConfig.Version, ) channel2 := types.NewChannel( types.INIT, types.UNORDERED, - counterparty2, []string{connA1.ID}, testchannel2.Version, + counterparty2, []string{path2.EndpointA.ConnectionID}, path2.EndpointA.ChannelConfig.Version, ) expChannels := []types.IdentifiedChannel{ - types.NewIdentifiedChannel(testchannel0.PortID, testchannel0.ID, channel0), - types.NewIdentifiedChannel(testchannel1.PortID, testchannel1.ID, channel1), - types.NewIdentifiedChannel(testchannel2.PortID, testchannel2.ID, channel2), + types.NewIdentifiedChannel(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel0), + types.NewIdentifiedChannel(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, channel1), + types.NewIdentifiedChannel(path2.EndpointA.ChannelConfig.PortID, path2.EndpointA.ChannelID, channel2), } ctxA := suite.chainA.GetContext() - channels := suite.chainA.App.IBCKeeper.ChannelKeeper.GetAllChannels(ctxA) + channels := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetAllChannels(ctxA) suite.Require().Len(channels, len(expChannels)) suite.Require().Equal(expChannels, channels) } @@ -119,12 +129,21 @@ func (suite KeeperTestSuite) TestGetAllChannels() { // TestGetAllSequences sets all packet sequences for two different channels on chain A and // tests their retrieval. func (suite KeeperTestSuite) TestGetAllSequences() { - _, _, connA, connB, channelA0, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelA1, _ := suite.coordinator.CreateMockChannels(suite.chainA, suite.chainB, connA, connB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + path1.SetChannelOrdered() + path1.EndpointA.ClientID = path.EndpointA.ClientID + path1.EndpointB.ClientID = path.EndpointB.ClientID + path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID + path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID + + suite.coordinator.CreateMockChannels(path1) - seq1 := types.NewPacketSequence(channelA0.PortID, channelA0.ID, 1) - seq2 := types.NewPacketSequence(channelA0.PortID, channelA0.ID, 2) - seq3 := types.NewPacketSequence(channelA1.PortID, channelA1.ID, 3) + seq1 := types.NewPacketSequence(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1) + seq2 := types.NewPacketSequence(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2) + seq3 := types.NewPacketSequence(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 3) // seq1 should be overwritten by seq2 expSeqs := []types.PacketSequence{seq2, seq3} @@ -132,14 +151,14 @@ func (suite KeeperTestSuite) TestGetAllSequences() { ctxA := suite.chainA.GetContext() for _, seq := range []types.PacketSequence{seq1, seq2, seq3} { - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceSend(ctxA, seq.PortId, seq.ChannelId, seq.Sequence) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(ctxA, seq.PortId, seq.ChannelId, seq.Sequence) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceAck(ctxA, seq.PortId, seq.ChannelId, seq.Sequence) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceSend(ctxA, seq.PortId, seq.ChannelId, seq.Sequence) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceRecv(ctxA, seq.PortId, seq.ChannelId, seq.Sequence) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceAck(ctxA, seq.PortId, seq.ChannelId, seq.Sequence) } - sendSeqs := suite.chainA.App.IBCKeeper.ChannelKeeper.GetAllPacketSendSeqs(ctxA) - recvSeqs := suite.chainA.App.IBCKeeper.ChannelKeeper.GetAllPacketRecvSeqs(ctxA) - ackSeqs := suite.chainA.App.IBCKeeper.ChannelKeeper.GetAllPacketAckSeqs(ctxA) + sendSeqs := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetAllPacketSendSeqs(ctxA) + recvSeqs := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetAllPacketRecvSeqs(ctxA) + ackSeqs := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetAllPacketAckSeqs(ctxA) suite.Len(sendSeqs, 2) suite.Len(recvSeqs, 2) suite.Len(ackSeqs, 2) @@ -152,35 +171,43 @@ func (suite KeeperTestSuite) TestGetAllSequences() { // TestGetAllPacketState creates a set of acks, packet commitments, and receipts on two different // channels on chain A and tests their retrieval. func (suite KeeperTestSuite) TestGetAllPacketState() { - _, _, connA, connB, channelA0, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - channelA1, _ := suite.coordinator.CreateMockChannels(suite.chainA, suite.chainB, connA, connB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + path1.EndpointA.ClientID = path.EndpointA.ClientID + path1.EndpointB.ClientID = path.EndpointB.ClientID + path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID + path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID + + suite.coordinator.CreateMockChannels(path1) // channel 0 acks - ack1 := types.NewPacketState(channelA0.PortID, channelA0.ID, 1, []byte("ack")) - ack2 := types.NewPacketState(channelA0.PortID, channelA0.ID, 2, []byte("ack")) + ack1 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, []byte("ack")) + ack2 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2, []byte("ack")) // duplicate ack - ack2dup := types.NewPacketState(channelA0.PortID, channelA0.ID, 2, []byte("ack")) + ack2dup := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2, []byte("ack")) // channel 1 acks - ack3 := types.NewPacketState(channelA1.PortID, channelA1.ID, 1, []byte("ack")) + ack3 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 1, []byte("ack")) // create channel 0 receipts receipt := string([]byte{byte(1)}) - rec1 := types.NewPacketState(channelA0.PortID, channelA0.ID, 1, []byte(receipt)) - rec2 := types.NewPacketState(channelA0.PortID, channelA0.ID, 2, []byte(receipt)) + rec1 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, []byte(receipt)) + rec2 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2, []byte(receipt)) // channel 1 receipts - rec3 := types.NewPacketState(channelA1.PortID, channelA1.ID, 1, []byte(receipt)) - rec4 := types.NewPacketState(channelA1.PortID, channelA1.ID, 2, []byte(receipt)) + rec3 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 1, []byte(receipt)) + rec4 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 2, []byte(receipt)) // channel 0 packet commitments - comm1 := types.NewPacketState(channelA0.PortID, channelA0.ID, 1, []byte("hash")) - comm2 := types.NewPacketState(channelA0.PortID, channelA0.ID, 2, []byte("hash")) + comm1 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 1, []byte("hash")) + comm2 := types.NewPacketState(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 2, []byte("hash")) // channel 1 packet commitments - comm3 := types.NewPacketState(channelA1.PortID, channelA1.ID, 1, []byte("hash")) - comm4 := types.NewPacketState(channelA1.PortID, channelA1.ID, 2, []byte("hash")) + comm3 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 1, []byte("hash")) + comm4 := types.NewPacketState(path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, 2, []byte("hash")) expAcks := []types.PacketState{ack1, ack2, ack3} expReceipts := []types.PacketState{rec1, rec2, rec3, rec4} @@ -190,22 +217,22 @@ func (suite KeeperTestSuite) TestGetAllPacketState() { // set acknowledgements for _, ack := range []types.PacketState{ack1, ack2, ack2dup, ack3} { - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(ctxA, ack.PortId, ack.ChannelId, ack.Sequence, ack.Data) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketAcknowledgement(ctxA, ack.PortId, ack.ChannelId, ack.Sequence, ack.Data) } // set packet receipts for _, rec := range expReceipts { - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketReceipt(ctxA, rec.PortId, rec.ChannelId, rec.Sequence) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketReceipt(ctxA, rec.PortId, rec.ChannelId, rec.Sequence) } // set packet commitments for _, comm := range expCommitments { - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(ctxA, comm.PortId, comm.ChannelId, comm.Sequence, comm.Data) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(ctxA, comm.PortId, comm.ChannelId, comm.Sequence, comm.Data) } - acks := suite.chainA.App.IBCKeeper.ChannelKeeper.GetAllPacketAcks(ctxA) - receipts := suite.chainA.App.IBCKeeper.ChannelKeeper.GetAllPacketReceipts(ctxA) - commitments := suite.chainA.App.IBCKeeper.ChannelKeeper.GetAllPacketCommitments(ctxA) + acks := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetAllPacketAcks(ctxA) + receipts := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetAllPacketReceipts(ctxA) + commitments := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetAllPacketCommitments(ctxA) suite.Require().Len(acks, len(expAcks)) suite.Require().Len(commitments, len(expCommitments)) @@ -218,40 +245,41 @@ func (suite KeeperTestSuite) TestGetAllPacketState() { // TestSetSequence verifies that the keeper correctly sets the sequence counters. func (suite *KeeperTestSuite) TestSetSequence() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) ctxA := suite.chainA.GetContext() one := uint64(1) // initialized channel has next send seq of 1 - seq, found := suite.chainA.App.IBCKeeper.ChannelKeeper.GetNextSequenceSend(ctxA, channelA.PortID, channelA.ID) + seq, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceSend(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.True(found) suite.Equal(one, seq) // initialized channel has next seq recv of 1 - seq, found = suite.chainA.App.IBCKeeper.ChannelKeeper.GetNextSequenceRecv(ctxA, channelA.PortID, channelA.ID) + seq, found = suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceRecv(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.True(found) suite.Equal(one, seq) // initialized channel has next seq ack of - seq, found = suite.chainA.App.IBCKeeper.ChannelKeeper.GetNextSequenceAck(ctxA, channelA.PortID, channelA.ID) + seq, found = suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceAck(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.True(found) suite.Equal(one, seq) nextSeqSend, nextSeqRecv, nextSeqAck := uint64(10), uint64(10), uint64(10) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceSend(ctxA, channelA.PortID, channelA.ID, nextSeqSend) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(ctxA, channelA.PortID, channelA.ID, nextSeqRecv) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceAck(ctxA, channelA.PortID, channelA.ID, nextSeqAck) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceSend(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, nextSeqSend) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceRecv(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, nextSeqRecv) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceAck(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, nextSeqAck) - storedNextSeqSend, found := suite.chainA.App.IBCKeeper.ChannelKeeper.GetNextSequenceSend(ctxA, channelA.PortID, channelA.ID) + storedNextSeqSend, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceSend(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.True(found) suite.Equal(nextSeqSend, storedNextSeqSend) - storedNextSeqRecv, found := suite.chainA.App.IBCKeeper.ChannelKeeper.GetNextSequenceSend(ctxA, channelA.PortID, channelA.ID) + storedNextSeqRecv, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceSend(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.True(found) suite.Equal(nextSeqRecv, storedNextSeqRecv) - storedNextSeqAck, found := suite.chainA.App.IBCKeeper.ChannelKeeper.GetNextSequenceAck(ctxA, channelA.PortID, channelA.ID) + storedNextSeqAck, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceAck(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.True(found) suite.Equal(nextSeqAck, storedNextSeqAck) } @@ -261,10 +289,18 @@ func (suite *KeeperTestSuite) TestSetSequence() { // value of "seq" and then add non-consecutive up to the value of "maxSeq". A final commitment // with the value maxSeq + 1 is set on a different channel. func (suite *KeeperTestSuite) TestGetAllPacketCommitmentsAtChannel() { - _, _, connA, connB, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) // create second channel - channelA1, _ := suite.coordinator.CreateMockChannels(suite.chainA, suite.chainB, connA, connB, types.UNORDERED) + path1 := ibctesting.NewPath(suite.chainA, suite.chainB) + path1.SetChannelOrdered() + path1.EndpointA.ClientID = path.EndpointA.ClientID + path1.EndpointB.ClientID = path.EndpointB.ClientID + path1.EndpointA.ConnectionID = path.EndpointA.ConnectionID + path1.EndpointB.ConnectionID = path.EndpointB.ConnectionID + + suite.coordinator.CreateMockChannels(path1) ctxA := suite.chainA.GetContext() expectedSeqs := make(map[uint64]bool) @@ -276,20 +312,20 @@ func (suite *KeeperTestSuite) TestGetAllPacketCommitmentsAtChannel() { // create consecutive commitments for i := uint64(1); i < seq; i++ { - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(ctxA, channelA.PortID, channelA.ID, i, hash) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, i, hash) expectedSeqs[i] = true } // add non-consecutive commitments for i := seq; i < maxSeq; i += 2 { - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(ctxA, channelA.PortID, channelA.ID, i, hash) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, i, hash) expectedSeqs[i] = true } // add sequence on different channel/port - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(ctxA, channelA1.PortID, channelA1.ID, maxSeq+1, hash) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(ctxA, path1.EndpointA.ChannelConfig.PortID, path1.EndpointA.ChannelID, maxSeq+1, hash) - commitments := suite.chainA.App.IBCKeeper.ChannelKeeper.GetAllPacketCommitmentsAtChannel(ctxA, channelA.PortID, channelA.ID) + commitments := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetAllPacketCommitmentsAtChannel(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) suite.Equal(len(expectedSeqs), len(commitments)) // ensure above for loops occurred @@ -298,8 +334,8 @@ func (suite *KeeperTestSuite) TestGetAllPacketCommitmentsAtChannel() { // verify that all the packet commitments were stored for _, packet := range commitments { suite.True(expectedSeqs[packet.Sequence]) - suite.Equal(channelA.PortID, packet.PortId) - suite.Equal(channelA.ID, packet.ChannelId) + suite.Equal(path.EndpointA.ChannelConfig.PortID, packet.PortId) + suite.Equal(path.EndpointA.ChannelID, packet.ChannelId) suite.Equal(hash, packet.Data) // prevent duplicates from passing checks @@ -310,20 +346,21 @@ func (suite *KeeperTestSuite) TestGetAllPacketCommitmentsAtChannel() { // TestSetPacketAcknowledgement verifies that packet acknowledgements are correctly // set in the keeper. func (suite *KeeperTestSuite) TestSetPacketAcknowledgement() { - _, _, _, _, channelA, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) ctxA := suite.chainA.GetContext() seq := uint64(10) - storedAckHash, found := suite.chainA.App.IBCKeeper.ChannelKeeper.GetPacketAcknowledgement(ctxA, channelA.PortID, channelA.ID, seq) + storedAckHash, found := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq) suite.Require().False(found) suite.Require().Nil(storedAckHash) ackHash := []byte("ackhash") - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(ctxA, channelA.PortID, channelA.ID, seq, ackHash) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketAcknowledgement(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq, ackHash) - storedAckHash, found = suite.chainA.App.IBCKeeper.ChannelKeeper.GetPacketAcknowledgement(ctxA, channelA.PortID, channelA.ID, seq) + storedAckHash, found = suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq) suite.Require().True(found) suite.Require().Equal(ackHash, storedAckHash) - suite.Require().True(suite.chainA.App.IBCKeeper.ChannelKeeper.HasPacketAcknowledgement(ctxA, channelA.PortID, channelA.ID, seq)) + suite.Require().True(suite.chainA.App.GetIBCKeeper().ChannelKeeper.HasPacketAcknowledgement(ctxA, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, seq)) } diff --git a/modules/core/04-channel/keeper/packet_test.go b/modules/core/04-channel/keeper/packet_test.go index 9a503e34b4d..4916ae9d995 100644 --- a/modules/core/04-channel/keeper/packet_test.go +++ b/modules/core/04-channel/keeper/packet_test.go @@ -31,143 +31,143 @@ var ( // TestSendPacket tests SendPacket from chainA to chainB func (suite *KeeperTestSuite) TestSendPacket() { var ( + path *ibctesting.Path packet exported.PacketI channelCap *capabilitytypes.Capability ) testCases := []testCase{ {"success: UNORDERED channel", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"success: ORDERED channel", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"sending packet out of order on UNORDERED channel", func() { // setup creates an unordered channel - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 5, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 5, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"sending packet out of order on ORDERED channel", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 5, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 5, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"packet basic validation failed, empty packet data", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket([]byte{}, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket([]byte{}, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"channel not found", func() { // use wrong channel naming - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"channel closed", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) - err := suite.coordinator.SetChannelClosed(suite.chainA, suite.chainB, channelA) + err := path.EndpointA.SetChannelClosed() suite.Require().NoError(err) }, false}, {"packet dest port ≠ channel counterparty port", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong port for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, ibctesting.InvalidID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, ibctesting.InvalidID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"packet dest channel ID ≠ channel counterparty channel ID", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong channel for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"connection not found", func() { - channelA := ibctesting.TestChannel{PortID: portID, ID: channelIDA} - channelB := ibctesting.TestChannel{PortID: portID, ID: channelIDB} // pass channel check - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainA.GetContext(), - channelA.PortID, channelA.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelB.PortID, channelB.ID), []string{connIDA}, channelA.Version), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID), []string{connIDA}, path.EndpointA.ChannelConfig.Version), ) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"client state not found", func() { - _, _, connA, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // change connection client ID - connection := suite.chainA.GetConnection(connA) + connection := path.EndpointA.GetConnection() connection.ClientId = ibctesting.InvalidID - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainA.GetContext(), connA.ID, connection) + suite.chainA.App.GetIBCKeeper().ConnectionKeeper.SetConnection(suite.chainA.GetContext(), path.EndpointA.ConnectionID, connection) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"client state is frozen", func() { - _, _, connA, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) - connection := suite.chainA.GetConnection(connA) - clientState := suite.chainA.GetClientState(connection.ClientId) + connection := path.EndpointA.GetConnection() + clientState := path.EndpointA.GetClientState() cs, ok := clientState.(*ibctmtypes.ClientState) suite.Require().True(ok) // freeze client cs.FrozenHeight = clienttypes.NewHeight(0, 1) - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), connection.ClientId, cs) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), connection.ClientId, cs) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"timeout height passed", func() { - clientA, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use client state latest height for timeout - clientState := suite.chainA.GetClientState(clientA) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clientState.GetLatestHeight().(clienttypes.Height), disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + clientState := path.EndpointA.GetClientState() + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clientState.GetLatestHeight().(clienttypes.Height), disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"timeout timestamp passed", func() { - clientA, _, connA, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use latest time on client state - clientState := suite.chainA.GetClientState(clientA) - connection := suite.chainA.GetConnection(connA) - timestamp, err := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetTimestampAtHeight(suite.chainA.GetContext(), connection, clientState.GetLatestHeight()) + clientState := path.EndpointA.GetClientState() + connection := path.EndpointA.GetConnection() + timestamp, err := suite.chainA.App.GetIBCKeeper().ConnectionKeeper.GetTimestampAtHeight(suite.chainA.GetContext(), connection, clientState.GetLatestHeight()) suite.Require().NoError(err) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, disabledTimeoutHeight, timestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, disabledTimeoutHeight, timestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"next sequence send not found", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA := suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) - channelB := suite.chainB.NextTestChannel(connB, ibctesting.TransferPort) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // manually creating channel prevents next sequence from being set - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainA.GetContext(), - channelA.PortID, channelA.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelB.PortID, channelB.ID), []string{connA.ID}, channelA.Version), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID), []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.Version), ) - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"next sequence wrong", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceSend(suite.chainA.GetContext(), channelA.PortID, channelA.ID, 5) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceSend(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 5) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"channel capability not found", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) channelCap = capabilitytypes.NewCapability(5) }, false}, } @@ -176,10 +176,11 @@ func (suite *KeeperTestSuite) TestSendPacket() { tc := tc suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.msg, i, len(testCases)), func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - err := suite.chainA.App.IBCKeeper.ChannelKeeper.SendPacket(suite.chainA.GetContext(), channelCap, packet) + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.SendPacket(suite.chainA.GetContext(), channelCap, packet) if tc.expPass { suite.Require().NoError(err) @@ -196,161 +197,162 @@ func (suite *KeeperTestSuite) TestSendPacket() { // verification tests need to simulate sending a packet from chainA to chainB. func (suite *KeeperTestSuite) TestRecvPacket() { var ( + path *ibctesting.Path packet exported.PacketI channelCap *capabilitytypes.Capability ) testCases := []testCase{ {"success: ORDERED channel", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, true}, {"success UNORDERED channel", func() { // setup uses an UNORDERED channel - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, true}, {"success with out of order packet: UNORDERED channel", func() { // setup uses an UNORDERED channel - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // send 2 packets - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // set sequence to 2 - packet = types.NewPacket(ibctesting.MockPacketData, 2, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - err = suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + packet = types.NewPacket(ibctesting.MockPacketData, 2, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + err = path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // attempts to receive packet 2 without receiving packet 1 - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, true}, {"out of order packet failure with ORDERED channel", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // send 2 packets - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // set sequence to 2 - packet = types.NewPacket(ibctesting.MockPacketData, 2, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - err = suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + packet = types.NewPacket(ibctesting.MockPacketData, 2, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + err = path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // attempts to receive packet 2 without receiving packet 1 - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"channel not found", func() { // use wrong channel naming - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, ibctesting.InvalidID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, ibctesting.InvalidID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"channel not open", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) - err := suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) + err := path.EndpointB.SetChannelClosed() suite.Require().NoError(err) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, - {"capability cannot authenticate", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + {"capability cannot authenticate ORDERED", func() { + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) channelCap = capabilitytypes.NewCapability(3) }, false}, {"packet source port ≠ channel counterparty port", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong port for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"packet source channel ID ≠ channel counterparty channel ID", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong port for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, ibctesting.InvalidID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, ibctesting.InvalidID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"connection not found", func() { - channelA := ibctesting.TestChannel{PortID: portID, ID: channelIDA} - channelB := ibctesting.TestChannel{PortID: portID, ID: channelIDB} // pass channel check - suite.chainB.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainB.GetContext(), - channelB.PortID, channelB.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelA.PortID, channelA.ID), []string{connIDB}, channelB.Version), + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID), []string{connIDB}, path.EndpointB.ChannelConfig.Version), ) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - suite.chainB.CreateChannelCapability(channelB.PortID, channelB.ID) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + suite.chainB.CreateChannelCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"connection not OPEN", func() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(path) + // connection on chainB is in INIT - connB, connA, err := suite.coordinator.ConnOpenInit(suite.chainB, suite.chainA, clientB, clientA) + err := path.EndpointB.ConnOpenInit() suite.Require().NoError(err) - channelA := suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) - channelB := suite.chainB.NextTestChannel(connB, ibctesting.TransferPort) // pass channel check - suite.chainB.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainB.GetContext(), - channelB.PortID, channelB.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelA.PortID, channelA.ID), []string{connB.ID}, channelB.Version), + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID), []string{path.EndpointB.ConnectionID}, path.EndpointB.ChannelConfig.Version), ) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - suite.chainB.CreateChannelCapability(channelB.PortID, channelB.ID) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + suite.chainB.CreateChannelCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"timeout height passed", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"timeout timestamp passed", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, disabledTimeoutHeight, uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, disabledTimeoutHeight, uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"next receive sequence is not found", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA := suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) - channelB := suite.chainB.NextTestChannel(connB, ibctesting.TransferPort) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) // manually creating channel prevents next recv sequence from being set - suite.chainB.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainB.GetContext(), - channelB.PortID, channelB.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelA.PortID, channelA.ID), []string{connB.ID}, channelB.Version), + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID), []string{path.EndpointB.ConnectionID}, path.EndpointB.ChannelConfig.Version), ) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // manually set packet commitment - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), channelA.PortID, channelA.ID, packet.GetSequence(), ibctesting.MockPacketData) - suite.chainB.CreateChannelCapability(channelB.PortID, channelB.ID) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, packet.GetSequence(), ibctesting.MockPacketData) + suite.chainB.CreateChannelCapability(suite.chainB.GetSimApp().ScopedIBCMockKeeper, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"receipt already stored", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketReceipt(suite.chainB.GetContext(), channelB.PortID, channelB.ID, 1) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + path.EndpointA.SendPacket(packet) + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetPacketReceipt(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, 1) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"validation failed", func() { // packet commitment not set resulting in invalid proof - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, } @@ -358,21 +360,23 @@ func (suite *KeeperTestSuite) TestRecvPacket() { tc := tc suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.msg, i, len(testCases)), func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) + tc.malleate() // get proof of packet commitment from chainA packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) proof, proofHeight := suite.chainA.QueryProof(packetKey) - err := suite.chainB.App.IBCKeeper.ChannelKeeper.RecvPacket(suite.chainB.GetContext(), channelCap, packet, proof, proofHeight) + err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.RecvPacket(suite.chainB.GetContext(), channelCap, packet, proof, proofHeight) if tc.expPass { suite.Require().NoError(err) - channelB, _ := suite.chainB.App.IBCKeeper.ChannelKeeper.GetChannel(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel()) - nextSeqRecv, found := suite.chainB.App.IBCKeeper.ChannelKeeper.GetNextSequenceRecv(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel()) + channelB, _ := suite.chainB.App.GetIBCKeeper().ChannelKeeper.GetChannel(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel()) + nextSeqRecv, found := suite.chainB.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceRecv(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel()) suite.Require().True(found) - receipt, receiptStored := suite.chainB.App.IBCKeeper.ChannelKeeper.GetPacketReceipt(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) + receipt, receiptStored := suite.chainB.App.GetIBCKeeper().ChannelKeeper.GetPacketReceipt(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) if channelB.Ordering == types.ORDERED { suite.Require().Equal(packet.GetSequence()+1, nextSeqRecv, "sequence not incremented in ordered channel") @@ -392,6 +396,7 @@ func (suite *KeeperTestSuite) TestRecvPacket() { func (suite *KeeperTestSuite) TestWriteAcknowledgement() { var ( + path *ibctesting.Path ack []byte packet exported.PacketI channelCap *capabilitytypes.Capability @@ -401,34 +406,34 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgement() { { "success", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) ack = ibctesting.MockAcknowledgement - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, true, }, {"channel not found", func() { // use wrong channel naming - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, ibctesting.InvalidID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, ibctesting.InvalidID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) ack = ibctesting.MockAcknowledgement - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, {"channel not open", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) ack = ibctesting.MockAcknowledgement - err := suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) + err := path.EndpointB.SetChannelClosed() suite.Require().NoError(err) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false}, { "capability authentication failed", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) ack = ibctesting.MockAcknowledgement channelCap = capabilitytypes.NewCapability(3) }, @@ -437,21 +442,21 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgement() { { "no-op, already acked", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) ack = ibctesting.MockAcknowledgement - suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence(), ack) - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetPacketAcknowledgement(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence(), ack) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false, }, { "empty acknowledgement", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) ack = nil - channelCap = suite.chainB.GetChannelCapability(channelB.PortID, channelB.ID) + channelCap = suite.chainB.GetChannelCapability(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) }, false, }, @@ -460,10 +465,11 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgement() { tc := tc suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.msg, i, len(testCases)), func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - err := suite.chainB.App.IBCKeeper.ChannelKeeper.WriteAcknowledgement(suite.chainB.GetContext(), channelCap, packet, ack) + err := suite.chainB.App.GetIBCKeeper().ChannelKeeper.WriteAcknowledgement(suite.chainB.GetContext(), channelCap, packet, ack) if tc.expPass { suite.Require().NoError(err) @@ -477,6 +483,7 @@ func (suite *KeeperTestSuite) TestWriteAcknowledgement() { // TestAcknowledgePacket tests the call AcknowledgePacket on chainA. func (suite *KeeperTestSuite) TestAcknowledgePacket() { var ( + path *ibctesting.Path packet types.Packet ack = ibcmock.MockAcknowledgement @@ -485,150 +492,148 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() { testCases := []testCase{ {"success on ordered channel", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // create packet receipt and acknowledgement - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"success on unordered channel", func() { // setup uses an UNORDERED channel - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // create packet receipt and acknowledgement - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"channel not found", func() { // use wrong channel naming - _, _, _, _, _, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) }, false}, {"channel not open", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) - err := suite.coordinator.SetChannelClosed(suite.chainA, suite.chainB, channelA) + err := path.EndpointA.SetChannelClosed() suite.Require().NoError(err) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, - {"capability authentication failed", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + {"capability authentication failed ORDERED", func() { + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // create packet receipt and acknowledgement - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) channelCap = capabilitytypes.NewCapability(3) }, false}, {"packet destination port ≠ channel counterparty port", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong port for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, ibctesting.InvalidID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, ibctesting.InvalidID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"packet destination channel ID ≠ channel counterparty channel ID", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong channel for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"connection not found", func() { - channelA := ibctesting.TestChannel{PortID: portID, ID: channelIDA} - channelB := ibctesting.TestChannel{PortID: portID, ID: channelIDB} // pass channel check - suite.chainB.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainB.GetContext(), - channelB.PortID, channelB.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelA.PortID, channelA.ID), []string{connIDB}, channelB.Version), + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID), []string{connIDB}, path.EndpointB.ChannelConfig.Version), ) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"connection not OPEN", func() { - clientA, clientB := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(path) // connection on chainA is in INIT - connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() suite.Require().NoError(err) - channelA := suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) - channelB := suite.chainB.NextTestChannel(connB, ibctesting.TransferPort) // pass channel check - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainA.GetContext(), - channelA.PortID, channelA.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelB.PortID, channelB.ID), []string{connA.ID}, channelA.Version), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID), []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.Version), ) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"packet hasn't been sent", func() { // packet commitment never written - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"packet ack verification failed", func() { // ack never written - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // create packet commitment - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + path.EndpointA.SendPacket(packet) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"next ack sequence not found", func() { - _, _, connA, connB := suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, exported.Tendermint) - channelA := suite.chainA.NextTestChannel(connA, ibctesting.TransferPort) - channelB := suite.chainB.NextTestChannel(connB, ibctesting.TransferPort) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // manually creating channel prevents next sequence acknowledgement from being set - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainA.GetContext(), - channelA.PortID, channelA.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelB.PortID, channelB.ID), []string{connA.ID}, channelA.Version), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID), []string{path.EndpointA.ConnectionID}, path.EndpointA.ChannelConfig.Version), ) // manually set packet commitment - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), channelA.PortID, channelA.ID, packet.GetSequence(), ibctesting.MockPacketData) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, packet.GetSequence(), ibctesting.MockPacketData) // manually set packet acknowledgement and capability - suite.chainB.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(suite.chainB.GetContext(), channelB.PortID, channelB.ID, packet.GetSequence(), ibctesting.MockAcknowledgement) - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) - }, false}, - {"next ack sequence mismatch", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.chainB.App.GetIBCKeeper().ChannelKeeper.SetPacketAcknowledgement(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, packet.GetSequence(), ibctesting.MockAcknowledgement) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + }, false}, + {"next ack sequence mismatch ORDERED", func() { + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // create packet acknowledgement - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) // set next sequence ack wrong - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceAck(suite.chainA.GetContext(), channelA.PortID, channelA.ID, 10) - channelCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetNextSequenceAck(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, 10) + channelCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, } @@ -636,16 +641,18 @@ func (suite *KeeperTestSuite) TestAcknowledgePacket() { tc := tc suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.msg, i, len(testCases)), func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) + tc.malleate() packetKey := host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) proof, proofHeight := suite.chainB.QueryProof(packetKey) - err := suite.chainA.App.IBCKeeper.ChannelKeeper.AcknowledgePacket(suite.chainA.GetContext(), channelCap, packet, ack.Acknowledgement(), proof, proofHeight) - pc := suite.chainA.App.IBCKeeper.ChannelKeeper.GetPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.AcknowledgePacket(suite.chainA.GetContext(), channelCap, packet, ack.Acknowledgement(), proof, proofHeight) + pc := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - channelA, _ := suite.chainA.App.IBCKeeper.ChannelKeeper.GetChannel(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel()) - sequenceAck, _ := suite.chainA.App.IBCKeeper.ChannelKeeper.GetNextSequenceAck(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel()) + channelA, _ := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetChannel(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel()) + sequenceAck, _ := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceAck(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel()) if tc.expPass { suite.NoError(err) diff --git a/modules/core/04-channel/keeper/timeout_test.go b/modules/core/04-channel/keeper/timeout_test.go index 94c4b6a008e..ab6c4e49d2c 100644 --- a/modules/core/04-channel/keeper/timeout_test.go +++ b/modules/core/04-channel/keeper/timeout_test.go @@ -16,6 +16,7 @@ import ( // verification must specify which proof to use using the ordered bool. func (suite *KeeperTestSuite) TestTimeoutPacket() { var ( + path *ibctesting.Path packet types.Packet nextSeqRecv uint64 ordered bool @@ -24,92 +25,98 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { testCases := []testCase{ {"success: ORDERED", func() { ordered = true + path.SetChannelOrdered() - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) // need to update chainA's client representing chainB to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() }, true}, {"success: UNORDERED", func() { ordered = false - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) + path.EndpointA.SendPacket(packet) // need to update chainA's client representing chainB to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() }, true}, {"channel not found", func() { // use wrong channel naming - _, _, _, _, _, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) }, false}, {"channel not open", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) - err := suite.coordinator.SetChannelClosed(suite.chainA, suite.chainB, channelA) + err := path.EndpointA.SetChannelClosed() suite.Require().NoError(err) }, false}, {"packet destination port ≠ channel counterparty port", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong port for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, ibctesting.InvalidID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, ibctesting.InvalidID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) }, false}, {"packet destination channel ID ≠ channel counterparty channel ID", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong channel for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) }, false}, {"connection not found", func() { - channelA := ibctesting.TestChannel{PortID: portID, ID: channelIDA} - channelB := ibctesting.TestChannel{PortID: portID, ID: channelIDB} // pass channel check - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainA.GetContext(), - channelA.PortID, channelA.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelB.PortID, channelB.ID), []string{connIDA}, channelA.Version), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID), []string{connIDA}, path.EndpointA.ChannelConfig.Version), ) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) }, false}, {"timeout", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + path.EndpointA.SendPacket(packet) + path.EndpointA.UpdateClient() }, false}, {"packet already received ", func() { ordered = true + path.SetChannelOrdered() + nextSeqRecv = 2 - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) + path.EndpointA.UpdateClient() }, false}, {"packet hasn't been sent", func() { - clientA, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + ordered = true + path.SetChannelOrdered() + + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.UpdateClient() }, false}, {"next seq receive verification failed", func() { // set ordered to false resulting in wrong proof provided ordered = false - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.SetChannelOrdered() + + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) + path.EndpointA.SendPacket(packet) + path.EndpointA.UpdateClient() }, false}, {"packet ack verification failed", func() { // set ordered to true resulting in wrong proof provided ordered = true - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) + path.EndpointA.SendPacket(packet) + path.EndpointA.UpdateClient() }, false}, } @@ -123,6 +130,8 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { suite.SetupTest() // reset nextSeqRecv = 1 // must be explicitly changed + path = ibctesting.NewPath(suite.chainA, suite.chainB) + tc.malleate() orderedPacketKey := host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) @@ -134,7 +143,7 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { proof, proofHeight = suite.chainB.QueryProof(unorderedPacketKey) } - err := suite.chainA.App.IBCKeeper.ChannelKeeper.TimeoutPacket(suite.chainA.GetContext(), packet, proof, proofHeight, nextSeqRecv) + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutPacket(suite.chainA.GetContext(), packet, proof, proofHeight, nextSeqRecv) if tc.expPass { suite.Require().NoError(err) @@ -149,27 +158,32 @@ func (suite *KeeperTestSuite) TestTimeoutPacket() { // channel capabilities are verified. func (suite *KeeperTestSuite) TestTimeoutExecuted() { var ( + path *ibctesting.Path packet types.Packet chanCap *capabilitytypes.Capability ) testCases := []testCase{ {"success ORDERED", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"channel not found", func() { // use wrong channel naming - _, _, _, _, _, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) }, false}, - {"incorrect capability", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + {"incorrect capability ORDERED", func() { + path.SetChannelOrdered() + suite.coordinator.Setup(path) + + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) chanCap = capabilitytypes.NewCapability(100) }, false}, @@ -179,11 +193,12 @@ func (suite *KeeperTestSuite) TestTimeoutExecuted() { tc := tc suite.Run(fmt.Sprintf("Case %s, %d/%d tests", tc.msg, i, len(testCases)), func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() - err := suite.chainA.App.IBCKeeper.ChannelKeeper.TimeoutExecuted(suite.chainA.GetContext(), chanCap, packet) - pc := suite.chainA.App.IBCKeeper.ChannelKeeper.GetPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutExecuted(suite.chainA.GetContext(), chanCap, packet) + pc := suite.chainA.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) if tc.expPass { suite.NoError(err) @@ -199,6 +214,7 @@ func (suite *KeeperTestSuite) TestTimeoutExecuted() { // channel on chainB after the packet commitment has been created. func (suite *KeeperTestSuite) TestTimeoutOnClose() { var ( + path *ibctesting.Path packet types.Packet chanCap *capabilitytypes.Capability nextSeqRecv uint64 @@ -208,110 +224,119 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { testCases := []testCase{ {"success: ORDERED", func() { ordered = true - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) + path.EndpointB.SetChannelClosed() // need to update chainA's client representing chainB to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"success: UNORDERED", func() { ordered = false - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) + path.EndpointA.SendPacket(packet) + path.EndpointB.SetChannelClosed() // need to update chainA's client representing chainB to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, true}, {"channel not found", func() { // use wrong channel naming - _, _, _, _, _, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, ibctesting.InvalidID, ibctesting.InvalidID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) }, false}, {"packet dest port ≠ channel counterparty port", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong port for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, ibctesting.InvalidID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, ibctesting.InvalidID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"packet dest channel ID ≠ channel counterparty channel ID", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) + suite.coordinator.Setup(path) // use wrong channel for dest - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, ibctesting.InvalidID, timeoutHeight, disabledTimeoutTimestamp) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"connection not found", func() { - channelA := ibctesting.TestChannel{PortID: portID, ID: channelIDA} - channelB := ibctesting.TestChannel{PortID: portID, ID: channelIDB} // pass channel check - suite.chainA.App.IBCKeeper.ChannelKeeper.SetChannel( + suite.chainA.App.GetIBCKeeper().ChannelKeeper.SetChannel( suite.chainA.GetContext(), - channelA.PortID, channelA.ID, - types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(channelB.PortID, channelB.ID), []string{connIDA}, channelA.Version), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + types.NewChannel(types.OPEN, types.ORDERED, types.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID), []string{connIDA}, path.EndpointA.ChannelConfig.Version), ) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, disabledTimeoutTimestamp) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, disabledTimeoutTimestamp) // create chancap - suite.chainA.CreateChannelCapability(channelA.PortID, channelA.ID) - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.chainA.CreateChannelCapability(suite.chainA.GetSimApp().ScopedIBCMockKeeper, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, - {"packet hasn't been sent", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + {"packet hasn't been sent ORDERED", func() { + path.SetChannelOrdered() + suite.coordinator.Setup(path) + + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, - {"packet already received", func() { + {"packet already received ORDERED", func() { + path.SetChannelOrdered() nextSeqRecv = 2 ordered = true - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) + suite.coordinator.Setup(path) + + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) + path.EndpointB.SetChannelClosed() // need to update chainA's client representing chainB to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, - {"channel verification failed", func() { + {"channel verification failed ORDERED", func() { ordered = true - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, - {"next seq receive verification failed", func() { + {"next seq receive verification failed ORDERED", func() { // set ordered to false providing the wrong proof for ORDERED case ordered = false - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) + path.EndpointB.SetChannelClosed() + path.EndpointA.UpdateClient() + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, {"packet ack verification failed", func() { // set ordered to true providing the wrong proof for UNORDERED case ordered = true - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.UNORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) - chanCap = suite.chainA.GetChannelCapability(channelA.PortID, channelA.ID) + suite.coordinator.Setup(path) + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), disabledTimeoutTimestamp) + path.EndpointA.SendPacket(packet) + path.EndpointB.SetChannelClosed() + path.EndpointA.UpdateClient() + chanCap = suite.chainA.GetChannelCapability(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) }, false}, - {"channel capability not found", func() { + {"channel capability not found ORDERED", func() { ordered = true - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, types.ORDERED) - packet = types.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) - suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, channelB) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + + packet = types.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.EndpointA.SendPacket(packet) + path.EndpointB.SetChannelClosed() // need to update chainA's client representing chainB to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() chanCap = capabilitytypes.NewCapability(100) }, false}, @@ -324,6 +349,8 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { suite.SetupTest() // reset nextSeqRecv = 1 // must be explicitly changed + path = ibctesting.NewPath(suite.chainA, suite.chainB) + tc.malleate() channelKey := host.ChannelKey(packet.GetDestPort(), packet.GetDestChannel()) @@ -338,7 +365,7 @@ func (suite *KeeperTestSuite) TestTimeoutOnClose() { proof, _ = suite.chainB.QueryProof(unorderedPacketKey) } - err := suite.chainA.App.IBCKeeper.ChannelKeeper.TimeoutOnClose(suite.chainA.GetContext(), chanCap, packet, proof, proofClosed, proofHeight, nextSeqRecv) + err := suite.chainA.App.GetIBCKeeper().ChannelKeeper.TimeoutOnClose(suite.chainA.GetContext(), chanCap, packet, proof, proofClosed, proofHeight, nextSeqRecv) if tc.expPass { suite.Require().NoError(err) diff --git a/modules/core/genesis_test.go b/modules/core/genesis_test.go index 0e17f6cd066..924fb42e2d3 100644 --- a/modules/core/genesis_test.go +++ b/modules/core/genesis_test.go @@ -8,7 +8,6 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/ibc-go/testing/simapp" ibc "github.com/cosmos/ibc-go/modules/core" clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types" @@ -19,6 +18,7 @@ import ( ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" localhosttypes "github.com/cosmos/ibc-go/modules/light-clients/09-localhost/types" ibctesting "github.com/cosmos/ibc-go/testing" + "github.com/cosmos/ibc-go/testing/simapp" ) const ( @@ -331,10 +331,10 @@ func (suite *IBCTestSuite) TestExportGenesis() { "success", func() { // creates clients - suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + suite.coordinator.Setup(ibctesting.NewPath(suite.chainA, suite.chainB)) // create extra clients - suite.coordinator.CreateClient(suite.chainA, suite.chainB, exported.Tendermint) - suite.coordinator.CreateClient(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(ibctesting.NewPath(suite.chainA, suite.chainB)) + suite.coordinator.SetupClients(ibctesting.NewPath(suite.chainA, suite.chainB)) }, }, } @@ -347,23 +347,23 @@ func (suite *IBCTestSuite) TestExportGenesis() { var gs *types.GenesisState suite.NotPanics(func() { - gs = ibc.ExportGenesis(suite.chainA.GetContext(), *suite.chainA.App.IBCKeeper) + gs = ibc.ExportGenesis(suite.chainA.GetContext(), *suite.chainA.App.GetIBCKeeper()) }) // init genesis based on export suite.NotPanics(func() { - ibc.InitGenesis(suite.chainA.GetContext(), *suite.chainA.App.IBCKeeper, true, gs) + ibc.InitGenesis(suite.chainA.GetContext(), *suite.chainA.App.GetIBCKeeper(), true, gs) }) suite.NotPanics(func() { - cdc := codec.NewProtoCodec(suite.chainA.App.InterfaceRegistry()) + cdc := codec.NewProtoCodec(suite.chainA.GetSimApp().InterfaceRegistry()) genState := cdc.MustMarshalJSON(gs) cdc.MustUnmarshalJSON(genState, gs) }) // init genesis based on marshal and unmarshal suite.NotPanics(func() { - ibc.InitGenesis(suite.chainA.GetContext(), *suite.chainA.App.IBCKeeper, true, gs) + ibc.InitGenesis(suite.chainA.GetContext(), *suite.chainA.App.GetIBCKeeper(), true, gs) }) }) } diff --git a/modules/core/keeper/msg_server_test.go b/modules/core/keeper/msg_server_test.go index 97d2c4abf61..5660c32d158 100644 --- a/modules/core/keeper/msg_server_test.go +++ b/modules/core/keeper/msg_server_test.go @@ -40,9 +40,12 @@ func (suite *KeeperTestSuite) SetupTest() { suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) + + // TODO: remove // commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1) suite.coordinator.CommitNBlocks(suite.chainA, 2) suite.coordinator.CommitNBlocks(suite.chainB, 2) + } func TestIBCTestSuite(t *testing.T) { @@ -57,6 +60,7 @@ func TestIBCTestSuite(t *testing.T) { func (suite *KeeperTestSuite) TestHandleRecvPacket() { var ( packet channeltypes.Packet + path *ibctesting.Path async bool // indicate no ack written ) @@ -67,62 +71,65 @@ func (suite *KeeperTestSuite) TestHandleRecvPacket() { expRevert bool }{ {"success: ORDERED", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) }, true, false}, {"success: UNORDERED", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) }, true, false}, {"success: UNORDERED out of order packet", func() { // setup uses an UNORDERED channel - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + suite.coordinator.Setup(path) // attempts to receive packet with sequence 10 without receiving packet with sequence 1 for i := uint64(1); i < 10; i++ { - packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) } }, true, false}, {"success: OnRecvPacket callback returns revert=true", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockFailPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockFailPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) }, true, true}, {"success: ORDERED - async acknowledgement", func() { + path.SetChannelOrdered() + suite.coordinator.Setup(path) async = true - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet = channeltypes.NewPacket(ibcmock.MockAsyncPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibcmock.MockAsyncPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) }, true, false}, {"success: UNORDERED - async acknowledgement", func() { + suite.coordinator.Setup(path) async = true - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibcmock.MockAsyncPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibcmock.MockAsyncPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) }, true, false}, {"failure: ORDERED out of order packet", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) + path.SetChannelOrdered() + suite.coordinator.Setup(path) // attempts to receive packet with sequence 10 without receiving packet with sequence 1 for i := uint64(1); i < 10; i++ { - packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) } }, false, false}, @@ -131,28 +138,28 @@ func (suite *KeeperTestSuite) TestHandleRecvPacket() { suite.Require().NotNil(packet) }, false, false}, {"packet not sent", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) }, false, false}, {"ORDERED: packet already received (replay)", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) }, false, false}, {"UNORDERED: packet already received (replay)", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) }, false, false}, } @@ -163,6 +170,7 @@ func (suite *KeeperTestSuite) TestHandleRecvPacket() { suite.Run(tc.name, func() { suite.SetupTest() // reset async = false // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() @@ -172,17 +180,17 @@ func (suite *KeeperTestSuite) TestHandleRecvPacket() { msg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, suite.chainB.SenderAccount.GetAddress().String()) - _, err := keeper.Keeper.RecvPacket(*suite.chainB.App.IBCKeeper, sdk.WrapSDKContext(suite.chainB.GetContext()), msg) + _, err := keeper.Keeper.RecvPacket(*suite.chainB.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainB.GetContext()), msg) if tc.expPass { suite.Require().NoError(err) // replay should fail since state changes occur - _, err := keeper.Keeper.RecvPacket(*suite.chainB.App.IBCKeeper, sdk.WrapSDKContext(suite.chainB.GetContext()), msg) + _, err := keeper.Keeper.RecvPacket(*suite.chainB.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainB.GetContext()), msg) suite.Require().Error(err) // check that callback state was handled correctly - _, exists := suite.chainB.App.ScopedIBCMockKeeper.GetCapability(suite.chainB.GetContext(), ibctesting.MockCanaryCapabilityName) + _, exists := suite.chainB.GetSimApp().ScopedIBCMockKeeper.GetCapability(suite.chainB.GetContext(), ibctesting.MockCanaryCapabilityName) if tc.expRevert { suite.Require().False(exists, "capability exists in store even after callback reverted") } else { @@ -190,7 +198,7 @@ func (suite *KeeperTestSuite) TestHandleRecvPacket() { } // verify if ack was written - ack, found := suite.chainB.App.IBCKeeper.ChannelKeeper.GetPacketAcknowledgement(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) + ack, found := suite.chainB.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(suite.chainB.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) if async { suite.Require().Nil(ack) @@ -215,6 +223,7 @@ func (suite *KeeperTestSuite) TestHandleRecvPacket() { func (suite *KeeperTestSuite) TestHandleAcknowledgePacket() { var ( packet channeltypes.Packet + path *ibctesting.Path ) testCases := []struct { @@ -223,51 +232,53 @@ func (suite *KeeperTestSuite) TestHandleAcknowledgePacket() { expPass bool }{ {"success: ORDERED", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) }, true}, {"success: UNORDERED", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) }, true}, {"success: UNORDERED acknowledge out of order packet", func() { // setup uses an UNORDERED channel - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + suite.coordinator.Setup(path) // attempts to acknowledge ack with sequence 10 without acknowledging ack with sequence 1 (removing packet commitment) for i := uint64(1); i < 10; i++ { - packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) } }, true}, {"failure: ORDERED acknowledge out of order packet", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) + path.SetChannelOrdered() + suite.coordinator.Setup(path) // attempts to acknowledge ack with sequence 10 without acknowledging ack with sequence 1 (removing packet commitment for i := uint64(1); i < 10; i++ { - packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) } }, false}, @@ -276,37 +287,37 @@ func (suite *KeeperTestSuite) TestHandleAcknowledgePacket() { suite.Require().NotNil(packet) }, false}, {"packet not received", func() { - _, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) }, false}, {"ORDERED: packet already acknowledged (replay)", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.AcknowledgePacket(suite.chainA, suite.chainB, clientB, packet, ibctesting.MockAcknowledgement) + err = path.EndpointA.AcknowledgePacket(packet, ibctesting.MockAcknowledgement) suite.Require().NoError(err) }, false}, {"UNORDERED: packet already acknowledged (replay)", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + suite.coordinator.Setup(path) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) - err = suite.coordinator.AcknowledgePacket(suite.chainA, suite.chainB, clientB, packet, ibctesting.MockAcknowledgement) + err = path.EndpointA.AcknowledgePacket(packet, ibctesting.MockAcknowledgement) suite.Require().NoError(err) }, false}, } @@ -316,6 +327,7 @@ func (suite *KeeperTestSuite) TestHandleAcknowledgePacket() { suite.Run(tc.name, func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() @@ -324,17 +336,17 @@ func (suite *KeeperTestSuite) TestHandleAcknowledgePacket() { msg := channeltypes.NewMsgAcknowledgement(packet, ibcmock.MockAcknowledgement.Acknowledgement(), proof, proofHeight, suite.chainA.SenderAccount.GetAddress().String()) - _, err := keeper.Keeper.Acknowledgement(*suite.chainA.App.IBCKeeper, sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + _, err := keeper.Keeper.Acknowledgement(*suite.chainA.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainA.GetContext()), msg) if tc.expPass { suite.Require().NoError(err) // replay should an error - _, err := keeper.Keeper.Acknowledgement(*suite.chainA.App.IBCKeeper, sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + _, err := keeper.Keeper.Acknowledgement(*suite.chainA.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainA.GetContext()), msg) suite.Require().Error(err) // verify packet commitment was deleted on source chain - has := suite.chainA.App.IBCKeeper.ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + has := suite.chainA.App.GetIBCKeeper().ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) suite.Require().False(has) } else { @@ -353,6 +365,7 @@ func (suite *KeeperTestSuite) TestHandleTimeoutPacket() { var ( packet channeltypes.Packet packetKey []byte + path *ibctesting.Path ) testCases := []struct { @@ -361,62 +374,64 @@ func (suite *KeeperTestSuite) TestHandleTimeoutPacket() { expPass bool }{ {"success: ORDERED", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // need to update chainA client to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) }, true}, {"success: UNORDERED", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), uint64(suite.chainB.GetContext().BlockTime().UnixNano())) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // need to update chainA client to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) }, true}, {"success: UNORDERED timeout out of order packet", func() { // setup uses an UNORDERED channel - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) + suite.coordinator.Setup(path) // attempts to timeout the last packet sent without timing out the first packet // packet sequences begin at 1 for i := uint64(1); i < maxSequence; i++ { - packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), 0) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) } - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) }, true}, {"success: ORDERED timeout out of order packet", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) + path.SetChannelOrdered() + suite.coordinator.Setup(path) // attempts to timeout the last packet sent without timing out the first packet // packet sequences begin at 1 for i := uint64(1); i < maxSequence; i++ { - packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.GetSelfHeight(suite.chainB.GetContext()), 0) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) } - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) }, true}, @@ -427,8 +442,8 @@ func (suite *KeeperTestSuite) TestHandleTimeoutPacket() { packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) }, false}, {"UNORDERED: packet not sent", func() { - _, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) }, false}, } @@ -438,6 +453,7 @@ func (suite *KeeperTestSuite) TestHandleTimeoutPacket() { suite.Run(tc.name, func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() @@ -445,17 +461,17 @@ func (suite *KeeperTestSuite) TestHandleTimeoutPacket() { msg := channeltypes.NewMsgTimeout(packet, 1, proof, proofHeight, suite.chainA.SenderAccount.GetAddress().String()) - _, err := keeper.Keeper.Timeout(*suite.chainA.App.IBCKeeper, sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + _, err := keeper.Keeper.Timeout(*suite.chainA.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainA.GetContext()), msg) if tc.expPass { suite.Require().NoError(err) // replay should return an error - _, err := keeper.Keeper.Timeout(*suite.chainA.App.IBCKeeper, sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + _, err := keeper.Keeper.Timeout(*suite.chainA.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainA.GetContext()), msg) suite.Require().Error(err) // verify packet commitment was deleted on source chain - has := suite.chainA.App.IBCKeeper.ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + has := suite.chainA.App.GetIBCKeeper().ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) suite.Require().False(has) } else { @@ -472,9 +488,9 @@ func (suite *KeeperTestSuite) TestHandleTimeoutPacket() { //'TimeoutExecuted' can be found in the 04-channel/keeper/timeout_test.go. func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() { var ( - packet channeltypes.Packet - packetKey []byte - counterpartyChannel ibctesting.TestChannel + packet channeltypes.Packet + packetKey []byte + path *ibctesting.Path ) testCases := []struct { @@ -483,95 +499,77 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() { expPass bool }{ {"success: ORDERED", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) - counterpartyChannel = ibctesting.TestChannel{ - PortID: channelB.PortID, - ID: channelB.ID, - CounterpartyClientID: clientA, - } + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // need to update chainA client to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) // close counterparty channel - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, counterpartyChannel) + path.EndpointB.SetChannelClosed() }, true}, {"success: UNORDERED", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) - counterpartyChannel = ibctesting.TestChannel{ - PortID: channelB.PortID, - ID: channelB.ID, - CounterpartyClientID: clientA, - } + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // need to update chainA client to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) // close counterparty channel - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, counterpartyChannel) + path.EndpointB.SetChannelClosed() }, true}, {"success: UNORDERED timeout out of order packet", func() { // setup uses an UNORDERED channel - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - counterpartyChannel = ibctesting.TestChannel{ - PortID: channelB.PortID, - ID: channelB.ID, - CounterpartyClientID: clientA, - } + suite.coordinator.Setup(path) // attempts to timeout the last packet sent without timing out the first packet // packet sequences begin at 1 for i := uint64(1); i < maxSequence; i++ { - packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) } - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) // close counterparty channel - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, counterpartyChannel) + path.EndpointB.SetChannelClosed() }, true}, {"success: ORDERED timeout out of order packet", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - counterpartyChannel = ibctesting.TestChannel{ - PortID: channelB.PortID, - ID: channelB.ID, - CounterpartyClientID: clientA, - } + path.SetChannelOrdered() + suite.coordinator.Setup(path) // attempts to timeout the last packet sent without timing out the first packet // packet sequences begin at 1 for i := uint64(1); i < maxSequence; i++ { - packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, i, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) } - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) // close counterparty channel - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, counterpartyChannel) + path.EndpointB.SetChannelClosed() }, true}, {"channel does not exist", func() { // any non-nil value of packet is valid @@ -580,33 +578,24 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() { packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) }, false}, {"UNORDERED: packet not sent", func() { - clientA, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) packetKey = host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) - counterpartyChannel = ibctesting.TestChannel{ - PortID: channelB.PortID, - ID: channelB.ID, - CounterpartyClientID: clientA, - } // close counterparty channel - suite.coordinator.SetChannelClosed(suite.chainB, suite.chainA, counterpartyChannel) + path.EndpointB.SetChannelClosed() }, false}, {"ORDERED: channel not closed", func() { - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, timeoutHeight, 0) - counterpartyChannel = ibctesting.TestChannel{ - PortID: channelB.PortID, - ID: channelB.ID, - CounterpartyClientID: clientA, - } + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet = channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, timeoutHeight, 0) // create packet commitment - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // need to update chainA client to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + path.EndpointA.UpdateClient() packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) }, false}, @@ -617,27 +606,28 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() { suite.Run(tc.name, func() { suite.SetupTest() // reset + path = ibctesting.NewPath(suite.chainA, suite.chainB) tc.malleate() proof, proofHeight := suite.chainB.QueryProof(packetKey) - channelKey := host.ChannelKey(counterpartyChannel.PortID, counterpartyChannel.ID) + channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) proofClosed, _ := suite.chainB.QueryProof(channelKey) msg := channeltypes.NewMsgTimeoutOnClose(packet, 1, proof, proofClosed, proofHeight, suite.chainA.SenderAccount.GetAddress().String()) - _, err := keeper.Keeper.TimeoutOnClose(*suite.chainA.App.IBCKeeper, sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + _, err := keeper.Keeper.TimeoutOnClose(*suite.chainA.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainA.GetContext()), msg) if tc.expPass { suite.Require().NoError(err) // replay should return an error - _, err := keeper.Keeper.TimeoutOnClose(*suite.chainA.App.IBCKeeper, sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + _, err := keeper.Keeper.TimeoutOnClose(*suite.chainA.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainA.GetContext()), msg) suite.Require().Error(err) // verify packet commitment was deleted on source chain - has := suite.chainA.App.IBCKeeper.ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + has := suite.chainA.App.GetIBCKeeper().ChannelKeeper.HasPacketCommitment(suite.chainA.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) suite.Require().False(has) } else { @@ -649,7 +639,7 @@ func (suite *KeeperTestSuite) TestHandleTimeoutOnClosePacket() { func (suite *KeeperTestSuite) TestUpgradeClient() { var ( - clientA string + path *ibctesting.Path upgradedClient exported.ClientState upgradedConsState exported.ConsensusState lastHeight exported.Height @@ -684,21 +674,21 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { suite.Require().NoError(err) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err = suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err = path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradeClient, _ := suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) proofUpgradedConsState, _ := suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedConsStateKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) - msg, err = clienttypes.NewMsgUpgradeClient(clientA, upgradedClient, upgradedConsState, + msg, err = clienttypes.NewMsgUpgradeClient(path.EndpointA.ClientID, upgradedClient, upgradedConsState, proofUpgradeClient, proofUpgradedConsState, suite.chainA.SenderAccount.GetAddress().String()) suite.Require().NoError(err) }, @@ -725,15 +715,15 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { suite.Require().NoError(err) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err = suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err = path.EndpointA.UpdateClient() suite.Require().NoError(err) - msg, err = clienttypes.NewMsgUpgradeClient(clientA, upgradedClient, upgradedConsState, nil, nil, suite.chainA.SenderAccount.GetAddress().String()) + msg, err = clienttypes.NewMsgUpgradeClient(path.EndpointA.ClientID, upgradedClient, upgradedConsState, nil, nil, suite.chainA.SenderAccount.GetAddress().String()) suite.Require().NoError(err) }, expPass: false, @@ -742,15 +732,16 @@ func (suite *KeeperTestSuite) TestUpgradeClient() { for _, tc := range cases { tc := tc - clientA, _ = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + path = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(path) tc.setup() - _, err := keeper.Keeper.UpgradeClient(*suite.chainA.App.IBCKeeper, sdk.WrapSDKContext(suite.chainA.GetContext()), msg) + _, err := keeper.Keeper.UpgradeClient(*suite.chainA.App.GetIBCKeeper(), sdk.WrapSDKContext(suite.chainA.GetContext()), msg) if tc.expPass { suite.Require().NoError(err, "upgrade handler failed on valid case: %s", tc.name) - newClient, ok := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + newClient, ok := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(ok) newChainSpecifiedClient := newClient.ZeroCustomFields() suite.Require().Equal(upgradedClient, newChainSpecifiedClient) diff --git a/modules/light-clients/06-solomachine/types/client_state_test.go b/modules/light-clients/06-solomachine/types/client_state_test.go index 654ab1baf19..6bab40ff1bc 100644 --- a/modules/light-clients/06-solomachine/types/client_state_test.go +++ b/modules/light-clients/06-solomachine/types/client_state_test.go @@ -119,7 +119,7 @@ func (suite *SoloMachineTestSuite) TestInitialize() { for _, tc := range testCases { err := solomachine.ClientState().Initialize( suite.chainA.GetContext(), suite.chainA.Codec, - suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), "solomachine"), + suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), "solomachine"), tc.consState, ) @@ -134,8 +134,9 @@ func (suite *SoloMachineTestSuite) TestInitialize() { func (suite *SoloMachineTestSuite) TestVerifyClientState() { // create client for tendermint so we can use client state for verification - clientA, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - clientState := suite.chainA.GetClientState(clientA) + tmPath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(tmPath) + clientState := suite.chainA.GetClientState(tmPath.EndpointA.ClientID) path := suite.solomachine.GetClientStatePath(counterpartyClientIdentifier) // test singlesig and multisig public keys @@ -261,9 +262,10 @@ func (suite *SoloMachineTestSuite) TestVerifyClientState() { func (suite *SoloMachineTestSuite) TestVerifyClientConsensusState() { // create client for tendermint so we can use consensus state for verification - clientA, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - clientState := suite.chainA.GetClientState(clientA) - consensusState, found := suite.chainA.GetConsensusState(clientA, clientState.GetLatestHeight()) + tmPath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(tmPath) + clientState := suite.chainA.GetClientState(tmPath.EndpointA.ClientID) + consensusState, found := suite.chainA.GetConsensusState(tmPath.EndpointA.ClientID, clientState.GetLatestHeight()) suite.Require().True(found) path := suite.solomachine.GetConsensusStatePath(counterpartyClientIdentifier, consensusHeight) diff --git a/modules/light-clients/06-solomachine/types/proposal_handle_test.go b/modules/light-clients/06-solomachine/types/proposal_handle_test.go index 4b797e6d0dc..db99bbe2538 100644 --- a/modules/light-clients/06-solomachine/types/proposal_handle_test.go +++ b/modules/light-clients/06-solomachine/types/proposal_handle_test.go @@ -67,8 +67,8 @@ func (suite *SoloMachineTestSuite) TestCheckSubstituteAndUpdateState() { tc.malleate() - subjectClientStore := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), solomachine.ClientID) - substituteClientStore := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), substitute.ClientID) + subjectClientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), solomachine.ClientID) + substituteClientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), substitute.ClientID) updatedClient, err := subjectClientState.CheckSubstituteAndUpdateState(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), subjectClientStore, substituteClientStore, substituteClientState, nil) diff --git a/modules/light-clients/06-solomachine/types/solomachine_test.go b/modules/light-clients/06-solomachine/types/solomachine_test.go index 42d1a384f6c..2f8e559a6c5 100644 --- a/modules/light-clients/06-solomachine/types/solomachine_test.go +++ b/modules/light-clients/06-solomachine/types/solomachine_test.go @@ -40,7 +40,7 @@ func (suite *SoloMachineTestSuite) SetupTest() { suite.solomachine = ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "solomachinesingle", "testing", 1) suite.solomachineMulti = ibctesting.NewSolomachine(suite.T(), suite.chainA.Codec, "solomachinemulti", "testing", 4) - suite.store = suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), exported.Solomachine) + suite.store = suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), exported.Solomachine) } func TestSoloMachineTestSuite(t *testing.T) { diff --git a/modules/light-clients/07-tendermint/types/client_state_test.go b/modules/light-clients/07-tendermint/types/client_state_test.go index 2838ebb49aa..94de0199da3 100644 --- a/modules/light-clients/07-tendermint/types/client_state_test.go +++ b/modules/light-clients/07-tendermint/types/client_state_test.go @@ -125,11 +125,12 @@ func (suite *TendermintTestSuite) TestInitialize() { }, } - clientA, err := suite.coordinator.CreateClient(suite.chainA, suite.chainB, exported.Tendermint) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + err := path.EndpointA.CreateClient() suite.Require().NoError(err) - clientState := suite.chainA.GetClientState(clientA) - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + clientState := suite.chainA.GetClientState(path.EndpointA.ClientID) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) for _, tc := range testCases { err := clientState.Initialize(suite.chainA.GetContext(), suite.chainA.Codec, store, tc.consensusState) @@ -262,26 +263,27 @@ func (suite *TendermintTestSuite) TestVerifyConnectionState() { suite.SetupTest() // reset // setup testing conditions - clientA, _, _, connB, _, _ := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - connection := suite.chainB.GetConnection(connB) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + connection := path.EndpointB.GetConnection() var ok bool - clientStateI := suite.chainA.GetClientState(clientA) + clientStateI := suite.chainA.GetClientState(path.EndpointA.ClientID) clientState, ok = clientStateI.(*types.ClientState) suite.Require().True(ok) prefix = suite.chainB.GetPrefix() // make connection proof - connectionKey := host.ConnectionKey(connB.ID) + connectionKey := host.ConnectionKey(path.EndpointB.ConnectionID) proof, proofHeight = suite.chainB.QueryProof(connectionKey) tc.malleate() // make changes as necessary - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) err := clientState.VerifyConnectionState( - store, suite.chainA.Codec, proofHeight, &prefix, proof, connB.ID, connection, + store, suite.chainA.Codec, proofHeight, &prefix, proof, path.EndpointB.ConnectionID, connection, ) if tc.expPass { @@ -340,27 +342,28 @@ func (suite *TendermintTestSuite) TestVerifyChannelState() { suite.SetupTest() // reset // setup testing conditions - clientA, _, _, _, _, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - channel := suite.chainB.GetChannel(channelB) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + channel := path.EndpointB.GetChannel() var ok bool - clientStateI := suite.chainA.GetClientState(clientA) + clientStateI := suite.chainA.GetClientState(path.EndpointA.ClientID) clientState, ok = clientStateI.(*types.ClientState) suite.Require().True(ok) prefix = suite.chainB.GetPrefix() // make channel proof - channelKey := host.ChannelKey(channelB.PortID, channelB.ID) + channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) proof, proofHeight = suite.chainB.QueryProof(channelKey) tc.malleate() // make changes as necessary - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) err := clientState.VerifyChannelState( store, suite.chainA.Codec, proofHeight, &prefix, proof, - channelB.PortID, channelB.ID, channel, + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, channel, ) if tc.expPass { @@ -434,13 +437,14 @@ func (suite *TendermintTestSuite) TestVerifyPacketCommitment() { suite.SetupTest() // reset // setup testing conditions - clientA, _, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelB.PortID, channelB.ID, channelA.PortID, channelA.ID, clienttypes.NewHeight(0, 100), 0) - err := suite.coordinator.SendPacket(suite.chainB, suite.chainA, packet, clientA) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, clienttypes.NewHeight(0, 100), 0) + err := path.EndpointB.SendPacket(packet) suite.Require().NoError(err) var ok bool - clientStateI := suite.chainA.GetClientState(clientA) + clientStateI := suite.chainA.GetClientState(path.EndpointA.ClientID) clientState, ok = clientStateI.(*types.ClientState) suite.Require().True(ok) @@ -448,14 +452,14 @@ func (suite *TendermintTestSuite) TestVerifyPacketCommitment() { // make packet commitment proof packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - proof, proofHeight = suite.chainB.QueryProof(packetKey) + proof, proofHeight = path.EndpointB.QueryProof(packetKey) tc.malleate() // make changes as necessary - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) currentTime := uint64(suite.chainA.GetContext().BlockTime().UnixNano()) - commitment := channeltypes.CommitPacket(suite.chainA.App.IBCKeeper.Codec(), packet) + commitment := channeltypes.CommitPacket(suite.chainA.App.GetIBCKeeper().Codec(), packet) err = clientState.VerifyPacketCommitment( store, suite.chainA.Codec, proofHeight, currentTime, delayPeriod, &prefix, proof, packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence(), commitment, @@ -533,19 +537,20 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() { suite.SetupTest() // reset // setup testing conditions - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.NewHeight(0, 100), 0) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0) // send packet - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // write receipt and ack - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) var ok bool - clientStateI := suite.chainA.GetClientState(clientA) + clientStateI := suite.chainA.GetClientState(path.EndpointA.ClientID) clientState, ok = clientStateI.(*types.ClientState) suite.Require().True(ok) @@ -557,7 +562,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() { tc.malleate() // make changes as necessary - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) currentTime := uint64(suite.chainA.GetContext().BlockTime().UnixNano()) err = clientState.VerifyPacketAcknowledgement( @@ -637,18 +642,16 @@ func (suite *TendermintTestSuite) TestVerifyPacketReceiptAbsence() { suite.SetupTest() // reset // setup testing conditions - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.NewHeight(0, 100), 0) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0) // send packet, but no recv - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) - // need to update chainA's client representing chainB to prove missing ack - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) - var ok bool - clientStateI := suite.chainA.GetClientState(clientA) + clientStateI := suite.chainA.GetClientState(path.EndpointA.ClientID) clientState, ok = clientStateI.(*types.ClientState) suite.Require().True(ok) @@ -656,11 +659,11 @@ func (suite *TendermintTestSuite) TestVerifyPacketReceiptAbsence() { // make packet receipt absence proof receiptKey := host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) - proof, proofHeight = suite.chainB.QueryProof(receiptKey) + proof, proofHeight = path.EndpointB.QueryProof(receiptKey) tc.malleate() // make changes as necessary - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) currentTime := uint64(suite.chainA.GetContext().BlockTime().UnixNano()) err = clientState.VerifyPacketReceiptAbsence( @@ -740,22 +743,21 @@ func (suite *TendermintTestSuite) TestVerifyNextSeqRecv() { suite.SetupTest() // reset // setup testing conditions - clientA, clientB, _, _, channelA, channelB := suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.ORDERED) - packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, channelA.PortID, channelA.ID, channelB.PortID, channelB.ID, clienttypes.NewHeight(0, 100), 0) + path := ibctesting.NewPath(suite.chainA, suite.chainB) + path.SetChannelOrdered() + suite.coordinator.Setup(path) + packet := channeltypes.NewPacket(ibctesting.MockPacketData, 1, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0) // send packet - err := suite.coordinator.SendPacket(suite.chainA, suite.chainB, packet, clientB) + err := path.EndpointA.SendPacket(packet) suite.Require().NoError(err) // next seq recv incremented - err = suite.coordinator.RecvPacket(suite.chainA, suite.chainB, clientA, packet) + err = path.EndpointB.RecvPacket(packet) suite.Require().NoError(err) - // need to update chainA's client representing chainB - suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) - var ok bool - clientStateI := suite.chainA.GetClientState(clientA) + clientStateI := suite.chainA.GetClientState(path.EndpointA.ClientID) clientState, ok = clientStateI.(*types.ClientState) suite.Require().True(ok) @@ -767,7 +769,7 @@ func (suite *TendermintTestSuite) TestVerifyNextSeqRecv() { tc.malleate() // make changes as necessary - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) currentTime := uint64(suite.chainA.GetContext().BlockTime().UnixNano()) err = clientState.VerifyNextSequenceRecv( diff --git a/modules/light-clients/07-tendermint/types/genesis_test.go b/modules/light-clients/07-tendermint/types/genesis_test.go index 9699c669b44..72b876e0251 100644 --- a/modules/light-clients/07-tendermint/types/genesis_test.go +++ b/modules/light-clients/07-tendermint/types/genesis_test.go @@ -11,12 +11,12 @@ import ( func (suite *TendermintTestSuite) TestExportMetadata() { clientState := types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs(), upgradePath, false, false) - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), "clientA", clientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), "clientA", clientState) - gm := clientState.ExportMetadata(suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), "clientA")) + gm := clientState.ExportMetadata(suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), "clientA")) suite.Require().Nil(gm, "client with no metadata returned non-nil exported metadata") - clientStore := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), "clientA") + clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), "clientA") // set some processed times timestamp1 := uint64(time.Now().UnixNano()) @@ -26,7 +26,7 @@ func (suite *TendermintTestSuite) TestExportMetadata() { types.SetProcessedTime(clientStore, clienttypes.NewHeight(0, 1), timestamp1) types.SetProcessedTime(clientStore, clienttypes.NewHeight(0, 2), timestamp2) - gm = clientState.ExportMetadata(suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), "clientA")) + gm = clientState.ExportMetadata(suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), "clientA")) suite.Require().NotNil(gm, "client with metadata returned nil exported metadata") suite.Require().Len(gm, 2, "exported metadata has unexpected length") diff --git a/modules/light-clients/07-tendermint/types/misbehaviour_handle_test.go b/modules/light-clients/07-tendermint/types/misbehaviour_handle_test.go index da1cd6fbfe6..ee8118472ec 100644 --- a/modules/light-clients/07-tendermint/types/misbehaviour_handle_test.go +++ b/modules/light-clients/07-tendermint/types/misbehaviour_handle_test.go @@ -344,16 +344,16 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviourAndUpdateState() { // Set trusted consensus states in client store if tc.consensusState1 != nil { - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientConsensusState(ctx, clientID, tc.height1, tc.consensusState1) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(ctx, clientID, tc.height1, tc.consensusState1) } if tc.consensusState2 != nil { - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientConsensusState(ctx, clientID, tc.height2, tc.consensusState2) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(ctx, clientID, tc.height2, tc.consensusState2) } clientState, err := tc.clientState.CheckMisbehaviourAndUpdateState( ctx, suite.cdc, - suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(ctx, clientID), // pass in clientID prefixed clientStore + suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(ctx, clientID), // pass in clientID prefixed clientStore tc.misbehaviour, ) diff --git a/modules/light-clients/07-tendermint/types/proposal_handle_test.go b/modules/light-clients/07-tendermint/types/proposal_handle_test.go index c9d3e71e20d..6208aec3165 100644 --- a/modules/light-clients/07-tendermint/types/proposal_handle_test.go +++ b/modules/light-clients/07-tendermint/types/proposal_handle_test.go @@ -15,9 +15,9 @@ var ( func (suite *TendermintTestSuite) TestCheckSubstituteUpdateStateBasic() { var ( - substitute string substituteClientState exported.ClientState initialHeight clienttypes.Height + substitutePath *ibctesting.Path ) testCases := []struct { name string @@ -35,8 +35,8 @@ func (suite *TendermintTestSuite) TestCheckSubstituteUpdateStateBasic() { }, { "non-matching substitute", func() { - substitute, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - substituteClientState = suite.chainA.GetClientState(substitute).(*types.ClientState) + suite.coordinator.SetupClients(substitutePath) + substituteClientState = suite.chainA.GetClientState(substitutePath.EndpointA.ClientID).(*types.ClientState) tmClientState, ok := substituteClientState.(*types.ClientState) suite.Require().True(ok) @@ -45,8 +45,8 @@ func (suite *TendermintTestSuite) TestCheckSubstituteUpdateStateBasic() { }, { "updated client is invalid - revision height is zero", func() { - substitute, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - substituteClientState = suite.chainA.GetClientState(substitute).(*types.ClientState) + suite.coordinator.SetupClients(substitutePath) + substituteClientState = suite.chainA.GetClientState(substitutePath.EndpointA.ClientID).(*types.ClientState) tmClientState, ok := substituteClientState.(*types.ClientState) suite.Require().True(ok) // match subject @@ -60,8 +60,8 @@ func (suite *TendermintTestSuite) TestCheckSubstituteUpdateStateBasic() { }, { "updated client is expired", func() { - substitute, _ = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - substituteClientState = suite.chainA.GetClientState(substitute).(*types.ClientState) + suite.coordinator.SetupClients(substitutePath) + substituteClientState = suite.chainA.GetClientState(substitutePath.EndpointA.ClientID).(*types.ClientState) tmClientState, ok := substituteClientState.(*types.ClientState) suite.Require().True(ok) initialHeight = tmClientState.LatestHeight @@ -69,21 +69,21 @@ func (suite *TendermintTestSuite) TestCheckSubstituteUpdateStateBasic() { // match subject tmClientState.AllowUpdateAfterMisbehaviour = true tmClientState.AllowUpdateAfterExpiry = true - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), substitute, tmClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), substitutePath.EndpointA.ClientID, tmClientState) // update substitute a few times - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, substitute, exported.Tendermint) + err := substitutePath.EndpointA.UpdateClient() suite.Require().NoError(err) - substituteClientState = suite.chainA.GetClientState(substitute) + substituteClientState = suite.chainA.GetClientState(substitutePath.EndpointA.ClientID) - err = suite.coordinator.UpdateClient(suite.chainA, suite.chainB, substitute, exported.Tendermint) + err = substitutePath.EndpointA.UpdateClient() suite.Require().NoError(err) - suite.chainA.ExpireClient(tmClientState.TrustingPeriod) - suite.chainB.ExpireClient(tmClientState.TrustingPeriod) + // expire client + suite.coordinator.IncrementTimeBy(tmClientState.TrustingPeriod) suite.coordinator.CommitBlock(suite.chainA, suite.chainB) - substituteClientState = suite.chainA.GetClientState(substitute) + substituteClientState = suite.chainA.GetClientState(substitutePath.EndpointA.ClientID) }, }, } @@ -94,21 +94,22 @@ func (suite *TendermintTestSuite) TestCheckSubstituteUpdateStateBasic() { suite.Run(tc.name, func() { suite.SetupTest() // reset + subjectPath := ibctesting.NewPath(suite.chainA, suite.chainB) + substitutePath = ibctesting.NewPath(suite.chainA, suite.chainB) - subject, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - subjectClientState := suite.chainA.GetClientState(subject).(*types.ClientState) + suite.coordinator.SetupClients(subjectPath) + subjectClientState := suite.chainA.GetClientState(subjectPath.EndpointA.ClientID).(*types.ClientState) subjectClientState.AllowUpdateAfterMisbehaviour = true subjectClientState.AllowUpdateAfterExpiry = true - // expire subject - suite.chainA.ExpireClient(subjectClientState.TrustingPeriod) - suite.chainB.ExpireClient(subjectClientState.TrustingPeriod) + // expire subject client + suite.coordinator.IncrementTimeBy(subjectClientState.TrustingPeriod) suite.coordinator.CommitBlock(suite.chainA, suite.chainB) tc.malleate() - subjectClientStore := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), subject) - substituteClientStore := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), substitute) + subjectClientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), subjectPath.EndpointA.ClientID) + substituteClientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), substitutePath.EndpointA.ClientID) updatedClient, err := subjectClientState.CheckSubstituteAndUpdateState(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), subjectClientStore, substituteClientStore, substituteClientState, initialHeight) suite.Require().Error(err) @@ -270,8 +271,9 @@ func (suite *TendermintTestSuite) TestCheckSubstituteAndUpdateState() { suite.SetupTest() // reset // construct subject using test case parameters - subject, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - subjectClientState := suite.chainA.GetClientState(subject).(*types.ClientState) + subjectPath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(subjectPath) + subjectClientState := suite.chainA.GetClientState(subjectPath.EndpointA.ClientID).(*types.ClientState) subjectClientState.AllowUpdateAfterExpiry = tc.AllowUpdateAfterExpiry subjectClientState.AllowUpdateAfterMisbehaviour = tc.AllowUpdateAfterMisbehaviour @@ -280,8 +282,8 @@ func (suite *TendermintTestSuite) TestCheckSubstituteAndUpdateState() { subjectClientState.FrozenHeight = frozenHeight } if tc.ExpireClient { - suite.chainA.ExpireClient(subjectClientState.TrustingPeriod) - suite.chainB.ExpireClient(subjectClientState.TrustingPeriod) + // expire subject client + suite.coordinator.IncrementTimeBy(subjectClientState.TrustingPeriod) suite.coordinator.CommitBlock(suite.chainA, suite.chainB) } @@ -291,27 +293,28 @@ func (suite *TendermintTestSuite) TestCheckSubstituteAndUpdateState() { // the natural flow of events in practice. The subject will become frozen/expired // and a substitute will be created along with a governance proposal as a response - substitute, _ := suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - substituteClientState := suite.chainA.GetClientState(substitute).(*types.ClientState) + substitutePath := ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(substitutePath) + substituteClientState := suite.chainA.GetClientState(substitutePath.EndpointA.ClientID).(*types.ClientState) substituteClientState.AllowUpdateAfterExpiry = tc.AllowUpdateAfterExpiry substituteClientState.AllowUpdateAfterMisbehaviour = tc.AllowUpdateAfterMisbehaviour - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), substitute, substituteClientState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), substitutePath.EndpointA.ClientID, substituteClientState) initialHeight := substituteClientState.GetLatestHeight() // update substitute a few times for i := 0; i < 3; i++ { - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, substitute, exported.Tendermint) + err := substitutePath.EndpointA.UpdateClient() suite.Require().NoError(err) // skip a block suite.coordinator.CommitBlock(suite.chainA, suite.chainB) } // get updated substitute - substituteClientState = suite.chainA.GetClientState(substitute).(*types.ClientState) + substituteClientState = suite.chainA.GetClientState(substitutePath.EndpointA.ClientID).(*types.ClientState) - subjectClientStore := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), subject) - substituteClientStore := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), substitute) + subjectClientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), subjectPath.EndpointA.ClientID) + substituteClientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), substitutePath.EndpointA.ClientID) updatedClient, err := subjectClientState.CheckSubstituteAndUpdateState(suite.chainA.GetContext(), suite.chainA.App.AppCodec(), subjectClientStore, substituteClientStore, substituteClientState, initialHeight) if tc.expPass { @@ -328,7 +331,7 @@ func (suite *TendermintTestSuite) TestCheckSubstituteAndUpdateState() { func (suite *TendermintTestSuite) TestIsMatchingClientState() { var ( - subject, substitute string + subjectPath, substitutePath *ibctesting.Path subjectClientState, substituteClientState *types.ClientState ) @@ -339,8 +342,8 @@ func (suite *TendermintTestSuite) TestIsMatchingClientState() { }{ { "matching clients", func() { - subjectClientState = suite.chainA.GetClientState(subject).(*types.ClientState) - substituteClientState = suite.chainA.GetClientState(substitute).(*types.ClientState) + subjectClientState = suite.chainA.GetClientState(subjectPath.EndpointA.ClientID).(*types.ClientState) + substituteClientState = suite.chainA.GetClientState(substitutePath.EndpointA.ClientID).(*types.ClientState) }, true, }, { @@ -375,8 +378,10 @@ func (suite *TendermintTestSuite) TestIsMatchingClientState() { suite.Run(tc.name, func() { suite.SetupTest() // reset - subject, _ = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) - substitute, _ = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + subjectPath = ibctesting.NewPath(suite.chainA, suite.chainB) + substitutePath = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.SetupClients(subjectPath) + suite.coordinator.SetupClients(substitutePath) tc.malleate() diff --git a/modules/light-clients/07-tendermint/types/store_test.go b/modules/light-clients/07-tendermint/types/store_test.go index ed9dc5d9ee2..e0230e08963 100644 --- a/modules/light-clients/07-tendermint/types/store_test.go +++ b/modules/light-clients/07-tendermint/types/store_test.go @@ -2,7 +2,6 @@ package types_test import ( clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" host "github.com/cosmos/ibc-go/modules/core/24-host" "github.com/cosmos/ibc-go/modules/core/exported" solomachinetypes "github.com/cosmos/ibc-go/modules/light-clients/06-solomachine/types" @@ -12,8 +11,8 @@ import ( func (suite *TendermintTestSuite) TestGetConsensusState() { var ( - height exported.Height - clientA string + height exported.Height + path *ibctesting.Path ) testCases := []struct { @@ -33,16 +32,16 @@ func (suite *TendermintTestSuite) TestGetConsensusState() { { "not a consensus state interface", func() { // marshal an empty client state and set as consensus state - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) - clientStateBz := suite.chainA.App.IBCKeeper.ClientKeeper.MustMarshalClientState(&types.ClientState{}) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) + clientStateBz := suite.chainA.App.GetIBCKeeper().ClientKeeper.MustMarshalClientState(&types.ClientState{}) store.Set(host.ConsensusStateKey(height), clientStateBz) }, false, }, { "invalid consensus state (solomachine)", func() { // marshal and set solomachine consensus state - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) - consensusStateBz := suite.chainA.App.IBCKeeper.ClientKeeper.MustMarshalConsensusState(&solomachinetypes.ConsensusState{}) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) + consensusStateBz := suite.chainA.App.GetIBCKeeper().ClientKeeper.MustMarshalConsensusState(&solomachinetypes.ConsensusState{}) store.Set(host.ConsensusStateKey(height), consensusStateBz) }, false, }, @@ -53,19 +52,20 @@ func (suite *TendermintTestSuite) TestGetConsensusState() { suite.Run(tc.name, func() { suite.SetupTest() + path = ibctesting.NewPath(suite.chainA, suite.chainB) - clientA, _, _, _, _, _ = suite.coordinator.Setup(suite.chainA, suite.chainB, channeltypes.UNORDERED) - clientState := suite.chainA.GetClientState(clientA) + suite.coordinator.Setup(path) + clientState := suite.chainA.GetClientState(path.EndpointA.ClientID) height = clientState.GetLatestHeight() tc.malleate() // change vars as necessary - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) consensusState, err := types.GetConsensusState(store, suite.chainA.Codec, height) if tc.expPass { suite.Require().NoError(err) - expConsensusState, found := suite.chainA.GetConsensusState(clientA, height) + expConsensusState, found := suite.chainA.GetConsensusState(path.EndpointA.ClientID, height) suite.Require().True(found) suite.Require().Equal(expConsensusState, consensusState) } else { @@ -77,32 +77,37 @@ func (suite *TendermintTestSuite) TestGetConsensusState() { } func (suite *TendermintTestSuite) TestGetProcessedTime() { - // Verify ProcessedTime on CreateClient + // setup + path := ibctesting.NewPath(suite.chainA, suite.chainB) + + suite.coordinator.UpdateTime() // coordinator increments time before creating client expectedTime := suite.chainA.CurrentHeader.Time.Add(ibctesting.TimeIncrement) - clientA, err := suite.coordinator.CreateClient(suite.chainA, suite.chainB, exported.Tendermint) + // Verify ProcessedTime on CreateClient + err := path.EndpointA.CreateClient() suite.Require().NoError(err) - clientState := suite.chainA.GetClientState(clientA) + clientState := suite.chainA.GetClientState(path.EndpointA.ClientID) height := clientState.GetLatestHeight() - store := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) actualTime, ok := types.GetProcessedTime(store, height) suite.Require().True(ok, "could not retrieve processed time for stored consensus state") suite.Require().Equal(uint64(expectedTime.UnixNano()), actualTime, "retrieved processed time is not expected value") - // Verify ProcessedTime on UpdateClient + suite.coordinator.UpdateTime() // coordinator increments time before updating client expectedTime = suite.chainA.CurrentHeader.Time.Add(ibctesting.TimeIncrement) - err = suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + // Verify ProcessedTime on UpdateClient + err = path.EndpointA.UpdateClient() suite.Require().NoError(err) - clientState = suite.chainA.GetClientState(clientA) + clientState = suite.chainA.GetClientState(path.EndpointA.ClientID) height = clientState.GetLatestHeight() - store = suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + store = suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) actualTime, ok = types.GetProcessedTime(store, height) suite.Require().True(ok, "could not retrieve processed time for stored consensus state") suite.Require().Equal(uint64(expectedTime.UnixNano()), actualTime, "retrieved processed time is not expected value") diff --git a/modules/light-clients/07-tendermint/types/update_test.go b/modules/light-clients/07-tendermint/types/update_test.go index f72d6fba7a1..672defe7954 100644 --- a/modules/light-clients/07-tendermint/types/update_test.go +++ b/modules/light-clients/07-tendermint/types/update_test.go @@ -242,7 +242,7 @@ func (suite *TendermintTestSuite) TestCheckHeaderAndUpdateState() { ctx := suite.chainA.GetContext().WithBlockTime(currentTime) // Set trusted consensus state in client store - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientConsensusState(ctx, clientID, consStateHeight, consensusState) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(ctx, clientID, consStateHeight, consensusState) height := newHeader.GetHeight() expectedConsensus := &types.ConsensusState{ @@ -254,7 +254,7 @@ func (suite *TendermintTestSuite) TestCheckHeaderAndUpdateState() { newClientState, consensusState, err := clientState.CheckHeaderAndUpdateState( ctx, suite.cdc, - suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientID), // pass in clientID prefixed clientStore + suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), clientID), // pass in clientID prefixed clientStore newHeader, ) diff --git a/modules/light-clients/07-tendermint/types/upgrade_test.go b/modules/light-clients/07-tendermint/types/upgrade_test.go index 263c11cda71..6c1baef6f3f 100644 --- a/modules/light-clients/07-tendermint/types/upgrade_test.go +++ b/modules/light-clients/07-tendermint/types/upgrade_test.go @@ -1,11 +1,13 @@ package types_test import ( + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types" "github.com/cosmos/ibc-go/modules/core/exported" "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" - upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + ibctesting "github.com/cosmos/ibc-go/testing" ) func (suite *TendermintTestSuite) TestVerifyUpgrade() { @@ -13,7 +15,7 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { upgradedClient exported.ClientState upgradedConsState exported.ConsensusState lastHeight clienttypes.Height - clientA string + path *ibctesting.Path proofUpgradedClient, proofUpgradedConsState []byte upgradedClientBz, upgradedConsStateBz []byte err error @@ -31,16 +33,16 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -61,16 +63,16 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -86,16 +88,16 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+10)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -115,16 +117,16 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -139,17 +141,17 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // change upgradedClient client-specified parameters upgradedClient = types.NewClientState("wrongchainID", types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, newClientHeight, commitmenttypes.GetSDKSpecs(), upgradePath, true, true) suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -161,17 +163,17 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { name: "unsuccessful upgrade: client-specified parameters do not match previous client", setup: func() { // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // change upgradedClient client-specified parameters upgradedClient = types.NewClientState("newChainId", types.DefaultTrustLevel, ubdPeriod, ubdPeriod+trustingPeriod, maxClockDrift+5, lastHeight, commitmenttypes.GetSDKSpecs(), upgradePath, true, false) suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -186,8 +188,8 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // change submitted upgradedConsensusState upgradedConsState = &types.ConsensusState{ @@ -197,10 +199,10 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -211,9 +213,9 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { { name: "unsuccessful upgrade: client proof unmarshal failed", setup: func() { - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedConsState, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedConsStateKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -225,9 +227,9 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { { name: "unsuccessful upgrade: consensus state proof unmarshal failed", setup: func() { - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -244,9 +246,9 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { // upgrade Height is at next block lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -262,9 +264,9 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { // upgrade Height is at next block lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -279,15 +281,15 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -296,7 +298,7 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { // SetClientState with empty upgrade path tmClient, _ := cs.(*types.ClientState) tmClient.UpgradePath = []string{""} - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), clientA, tmClient) + suite.chainA.App.GetIBCKeeper().ClientKeeper.SetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID, tmClient) }, expPass: false, }, @@ -307,15 +309,15 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -330,15 +332,15 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+100)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -350,18 +352,18 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { name: "unsuccessful upgrade: client is expired", setup: func() { // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) // expire chainB's client suite.chainA.ExpireClient(ubdPeriod) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -376,15 +378,15 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -404,16 +406,16 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { lastHeight = clienttypes.NewHeight(0, uint64(suite.chainB.GetContext().BlockHeight()+1)) // zero custom fields and store in upgrade store - suite.chainB.App.UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) - suite.chainB.App.UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedClient(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedClientBz) + suite.chainB.GetSimApp().UpgradeKeeper.SetUpgradedConsensusState(suite.chainB.GetContext(), int64(lastHeight.GetRevisionHeight()), upgradedConsStateBz) // commit upgrade store changes and update clients suite.coordinator.CommitBlock(suite.chainB) - err := suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, exported.Tendermint) + err := path.EndpointA.UpdateClient() suite.Require().NoError(err) - cs, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetClientState(suite.chainA.GetContext(), clientA) + cs, found := suite.chainA.App.GetIBCKeeper().ClientKeeper.GetClientState(suite.chainA.GetContext(), path.EndpointA.ClientID) suite.Require().True(found) proofUpgradedClient, _ = suite.chainB.QueryUpgradeProof(upgradetypes.UpgradedClientKey(int64(lastHeight.GetRevisionHeight())), cs.GetLatestHeight().GetRevisionHeight()) @@ -428,8 +430,9 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { // reset suite suite.SetupTest() + path = ibctesting.NewPath(suite.chainA, suite.chainB) - clientA, _ = suite.coordinator.SetupClients(suite.chainA, suite.chainB, exported.Tendermint) + suite.coordinator.SetupClients(path) upgradedClient = types.NewClientState("newChainId", types.DefaultTrustLevel, trustingPeriod, ubdPeriod+trustingPeriod, maxClockDrift, newClientHeight, commitmenttypes.GetSDKSpecs(), upgradePath, false, false) upgradedClient = upgradedClient.ZeroCustomFields() upgradedClientBz, err = clienttypes.MarshalClientState(suite.chainA.App.AppCodec(), upgradedClient) @@ -443,8 +446,8 @@ func (suite *TendermintTestSuite) TestVerifyUpgrade() { tc.setup() - cs := suite.chainA.GetClientState(clientA) - clientStore := suite.chainA.App.IBCKeeper.ClientKeeper.ClientStore(suite.chainA.GetContext(), clientA) + cs := suite.chainA.GetClientState(path.EndpointA.ClientID) + clientStore := suite.chainA.App.GetIBCKeeper().ClientKeeper.ClientStore(suite.chainA.GetContext(), path.EndpointA.ClientID) // Call ZeroCustomFields on upgraded clients to clear any client-chosen parameters in test-case upgradedClient upgradedClient = upgradedClient.ZeroCustomFields() diff --git a/testing/README.md b/testing/README.md new file mode 100644 index 00000000000..7808980b777 --- /dev/null +++ b/testing/README.md @@ -0,0 +1,289 @@ +# IBC Testing Package + +## Components + +The testing package comprises of four parts constructed as a stack. +- coordinator +- chain +- path +- endpoint + +A coordinator sits at the highest level and contains all the chains which have been initialized. +It also stores and updates the current global time. The time is manually incremented by a `TimeIncrement`. +This allows all the chains to remain in synchrony avoiding the issue of a counterparty being perceived to +be in the future. The coordinator also contains functions to do basic setup of clients, connections, and channels +between two chains. + +A chain is an SDK application (as represented by an app.go file). Inside the chain is an `TestingApp` which allows +the chain to simulate block production and transaction processing. The chain contains by default a single tendermint +validator. A chain is used to process SDK messages. + +A path connects two channel endpoints. It contains all the information needed to relay between two endpoints. + +An endpoint represents a channel (and its associated client and connections) on some specific chain. It contains +references to the chain it is on and the counterparty endpoint it is connected to. The endpoint contains functions +to interact with initialization and updates of its associated clients, connections, and channels. It can send, receive, +and acknowledge packets. + +In general: +- endpoints are used for initialization and execution of IBC logic on one side of an IBC connection +- paths are used to relay packets +- chains are used to commit SDK messages +- coordinator is used to setup a path between two chains + +## Integration + +To integrate the testing package into your tests, you will need to define: +- a testing application +- a function to initialize the testing application + +### TestingApp + +Your project will likely already have an application defined. This application +will need to be extended to fulfill the `TestingApp` interface. + +```go +type TestingApp interface { + abci.Application + + // ibc-go additions + GetBaseApp() *baseapp.BaseApp + GetStakingKeeper() stakingkeeper.Keeper + GetIBCKeeper() *keeper.Keeper + GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper + GetTxConfig() client.TxConfig + + // Implemented by SimApp + AppCodec() codec.Marshaler + + // Implemented by BaseApp + LastCommitID() sdk.CommitID + LastBlockHeight() int64 +} +``` + +To begin, you will need to extend your application by adding the following functions: + +```go +// TestingApp functions +// Example using SimApp to implement TestingApp + +// GetBaseApp implements the TestingApp interface. +func (app *SimApp) GetBaseApp() *baseapp.BaseApp { + return app.BaseApp +} + +// GetStakingKeeper implements the TestingApp interface. +func (app *SimApp) GetStakingKeeper() stakingkeeper.Keeper { + return app.StakingKeeper +} + +// GetIBCKeeper implements the TestingApp interface. +func (app *SimApp) GetIBCKeeper() *ibckeeper.Keeper { + return app.IBCKeeper +} + +// GetScopedIBCKeeper implements the TestingApp interface. +func (app *SimApp) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper { + return app.ScopedIBCKeeper +} + +// GetTxConfig implements the TestingApp interface. +func (app *SimApp) GetTxConfig() client.TxConfig { + return MakeTestEncodingConfig().TxConfig +} + +``` + +Your application may need to define `AppCodec()` if it does not already exist: + +```go +// AppCodec returns SimApp's app codec. +// +// NOTE: This is solely to be used for testing purposes as it may be desirable +// for modules to register their own custom testing types. +func (app *SimApp) AppCodec() codec.Marshaler { + return app.appCodec +} +``` + +It is assumed your application contains an embedded BaseApp and thus implements the abci.Application interface, `LastCommitID()` and `LastBlockHeight()` + +### Initialize TestingApp + +The testing package requires that you provide a function to initialize your TestingApp. This is how ibc-go implements the initialize function with its `SimApp`: + +```go +func SetupTestingApp() (TestingApp, map[string]json.RawMessage) { + db := dbm.NewMemDB() + encCdc := simapp.MakeTestEncodingConfig() + app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 5, encCdc, simapp.EmptyAppOptions{}) + return app, simapp.NewDefaultGenesisState(encCdc.Marshaler) +} +``` + +This function returns the TestingApp and the default genesis state used to initialize the testing app. + +Change the value of `DefaultTestingAppInit` to use your function: +```go +func init() { + ibctesting.DefaultTestingAppInit = MySetupTestingAppFunction +} + +``` + +## Example + +Here is an example of how to setup your testing environment in every package you are testing: +```go +// KeeperTestSuite is a testing suite to test keeper functions. +type KeeperTestSuite struct { + suite.Suite + + coordinator *ibctesting.Coordinator + + // testing chains used for convenience and readability + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain +} + +// TestKeeperTestSuite runs all the tests within this package. +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} + +// SetupTest creates a coordinator with 2 test chains. +func (suite *KeeperTestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) // initializes 2 test chains + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) // convenience and readability + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) // convenience and readability +} + +``` + +To create interaction between chainA and chainB, we need to contruct a `Path` these chains will use. +A path contains two endpoints, `EndpointA` and `EndpointB` (corresponding to the order of the chains passed +into the `NewPath` function). A path is a pointer and its values will be filled in as necessary during the +setup portion of testing. + +Endpoint Struct: +```go +// Endpoint is a which represents a channel endpoint and its associated +// client and connections. It contains client, connection, and channel +// configuration parameters. Endpoint functions will utilize the parameters +// set in the configuration structs when executing IBC messages. +type Endpoint struct { + Chain *TestChain + Counterparty *Endpoint + ClientID string + ConnectionID string + ChannelID string + + ClientConfig ClientConfig + ConnectionConfig *ConnectionConfig + ChannelConfig *ChannelConfig +} +``` + +The fields empty after `NewPath` is called are `ClientID`, `ConnectionID` and +`ChannelID` as the clients, connections, and channels for these endpoints have not yet been created. The +`ClientConfig`, `ConnectionConfig` and `ChannelConfig` contain all the necessary information for clients, +connections, and channels to be initialized. If you would like to use endpoints which are intitialized to +use your Port IDs, you might add a helper function similar to the one found in transfer: + +```go +func NewTransferPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path { + path := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig.PortID = ibctesting.TransferPort + path.EndpointB.ChannelConfig.PortID = ibctesting.TransferPort + + return path +} + +``` + +Path configurations should be set to the desired values before calling any `Setup` coordinator functions. + +To initialize the clients, connections, and channels for a path we can call the Setup functions of the coordinator: +- Setup() -> setup clients, connections, channels +- SetupClients() -> setup clients only +- SetupConnections() -> setup clients and connections only + + +Here is a basic example of the testing package being used to simulate IBC functionality: + +```go + path := ibctesting.NewPath(suite.chainA, suite.chainB) // clientID, connectionID, channelID empty + suite.coordinator.Setup(path) // clientID, connectionID, channelID filled + suite.Require().Equal("07-tendermint-0", path.EndpointA.ClientID) + suite.Require().Equal("connection-0", path.EndpointA.ClientID) + suite.Require().Equal("channel-0", path.EndpointA.ClientID) + + // create packet 1 + packet1 := NewPacket() // NewPacket would construct your packet + + // send on endpointA + path.EndpointA.SendPacket(packet1) + + // receive on endpointB + path.EndpointB.RecvPacket(packet1) + + // acknowledge the receipt of the packet + path.EndpointA.AcknowledgePacket(packet1, ack) + + // we can also relay + packet2 := NewPacket() + + path.EndpointA.SendPacket(packet2) + + path.Relay(packet2, expectedAck) + + // if needed we can update our clients + path.EndpointB.UpdateClient() +``` + +### Transfer Testing Example + +If ICS 20 had its own simapp, its testing setup might include a `testing/app.go` file with the following contents: + +```go +package transfertesting + +import ( + "encoding/json" + + "github.com/tendermint/tendermint/libs/log" + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/ibc-go/modules/apps/transfer/simapp" + ibctesting "github.com/cosmos/ibc-go/testing" +) + +func SetupTransferTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) { + db := dbm.NewMemDB() + encCdc := simapp.MakeTestEncodingConfig() + app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 5, encCdc, simapp.EmptyAppOptions{}) + return app, simapp.NewDefaultGenesisState(encCdc.Marshaler) +} + +func init() { + ibctesting.DefaultTestingAppInit = SetupTransferTestingApp +} + +func NewTransferPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path { + path := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig.PortID = ibctesting.TransferPort + path.EndpointB.ChannelConfig.PortID = ibctesting.TransferPort + + return path +} + +func GetTransferSimApp(chain *ibctesting.TestChain) *simapp.SimApp { + app, ok := chain.App.(*simapp.SimApp) + if !ok { + panic("not transfer app") + } + + return app +} +``` diff --git a/testing/app.go b/testing/app.go new file mode 100644 index 00000000000..fb4ecbcbecf --- /dev/null +++ b/testing/app.go @@ -0,0 +1,136 @@ +package ibctesting + +import ( + "encoding/json" + "testing" + "time" + + "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/log" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmtypes "github.com/tendermint/tendermint/types" + dbm "github.com/tendermint/tm-db" + + "github.com/cosmos/ibc-go/modules/core/keeper" + "github.com/cosmos/ibc-go/testing/simapp" +) + +var DefaultTestingAppInit func() (TestingApp, map[string]json.RawMessage) = SetupTestingApp + +type TestingApp interface { + abci.Application + + // ibc-go additions + GetBaseApp() *baseapp.BaseApp + GetStakingKeeper() stakingkeeper.Keeper + GetIBCKeeper() *keeper.Keeper + GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper + GetTxConfig() client.TxConfig + + // Implemented by SimApp + AppCodec() codec.Marshaler + + // Implemented by BaseApp + LastCommitID() sdk.CommitID + LastBlockHeight() int64 +} + +func SetupTestingApp() (TestingApp, map[string]json.RawMessage) { + db := dbm.NewMemDB() + encCdc := simapp.MakeTestEncodingConfig() + app := simapp.NewSimApp(log.NewNopLogger(), db, nil, true, map[int64]bool{}, simapp.DefaultNodeHome, 5, encCdc, simapp.EmptyAppOptions{}) + return app, simapp.NewDefaultGenesisState(encCdc.Marshaler) +} + +// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts +// that also act as delegators. For simplicity, each validator is bonded with a delegation +// of one consensus engine unit (10^6) in the default token of the simapp from first genesis +// account. A Nop logger is set in SimApp. +func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) TestingApp { + app, genesisState := DefaultTestingAppInit() + // set genesis accounts + authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) + genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) + + validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) + delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) + + bondAmt := sdk.NewInt(1000000) + + for _, val := range valSet.Validators { + pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) + require.NoError(t, err) + pkAny, err := codectypes.NewAnyWithValue(pk) + require.NoError(t, err) + validator := stakingtypes.Validator{ + OperatorAddress: sdk.ValAddress(val.Address).String(), + ConsensusPubkey: pkAny, + Jailed: false, + Status: stakingtypes.Bonded, + Tokens: bondAmt, + DelegatorShares: sdk.OneDec(), + Description: stakingtypes.Description{}, + UnbondingHeight: int64(0), + UnbondingTime: time.Unix(0, 0).UTC(), + Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), + MinSelfDelegation: sdk.ZeroInt(), + } + validators = append(validators, validator) + delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) + + } + // set validators and delegations + stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) + genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) + + totalSupply := sdk.NewCoins() + for _, b := range balances { + // add genesis acc tokens and delegated tokens to total supply + totalSupply = totalSupply.Add(b.Coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...) + } + + // add bonded amount to bonded pool module account + balances = append(balances, banktypes.Balance{ + Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), + Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, + }) + + // update total supply + bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}) + genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) + + stateBytes, err := json.MarshalIndent(genesisState, "", " ") + require.NoError(t, err) + + // init chain will set the validator set and initialize the genesis accounts + app.InitChain( + abci.RequestInitChain{ + Validators: []abci.ValidatorUpdate{}, + ConsensusParams: simapp.DefaultConsensusParams, + AppStateBytes: stateBytes, + }, + ) + + // commit genesis changes + app.Commit() + app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ + Height: app.LastBlockHeight() + 1, + AppHash: app.LastCommitID().Hash, + ValidatorsHash: valSet.Hash(), + NextValidatorsHash: valSet.Hash(), + }}) + + return app +} diff --git a/testing/chain.go b/testing/chain.go index 19aa1f2cedf..19ee21839f6 100644 --- a/testing/chain.go +++ b/testing/chain.go @@ -3,18 +3,9 @@ package ibctesting import ( "bytes" "fmt" - "strconv" "testing" "time" - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto/tmhash" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - tmprotoversion "github.com/tendermint/tendermint/proto/tendermint/version" - tmtypes "github.com/tendermint/tendermint/types" - tmversion "github.com/tendermint/tendermint/version" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -23,13 +14,19 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" "github.com/cosmos/cosmos-sdk/x/staking/teststaking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - ibctransfertypes "github.com/cosmos/ibc-go/modules/apps/transfer/types" + "github.com/stretchr/testify/require" + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto/tmhash" + tmproto "github.com/tendermint/tendermint/proto/tendermint/types" + tmprotoversion "github.com/tendermint/tendermint/proto/tendermint/version" + tmtypes "github.com/tendermint/tendermint/types" + tmversion "github.com/tendermint/tendermint/version" + clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" - connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types" - channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types" host "github.com/cosmos/ibc-go/modules/core/24-host" "github.com/cosmos/ibc-go/modules/core/exported" @@ -39,44 +36,6 @@ import ( "github.com/cosmos/ibc-go/testing/simapp" ) -const ( - // Default params constants used to create a TM client - TrustingPeriod time.Duration = time.Hour * 24 * 7 * 2 - UnbondingPeriod time.Duration = time.Hour * 24 * 7 * 3 - MaxClockDrift time.Duration = time.Second * 10 - DefaultDelayPeriod uint64 = 0 - - DefaultChannelVersion = ibctransfertypes.Version - InvalidID = "IDisInvalid" - - ConnectionIDPrefix = "conn" - ChannelIDPrefix = "chan" - - TransferPort = ibctransfertypes.ModuleName - MockPort = mock.ModuleName - - // used for testing UpdateClientProposal - Title = "title" - Description = "description" -) - -var ( - DefaultOpenInitVersion *connectiontypes.Version - - // Default params variables used to create a TM client - DefaultTrustLevel ibctmtypes.Fraction = ibctmtypes.DefaultTrustLevel - TestCoin = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) - - UpgradePath = []string{"upgrade", "upgradedIBCState"} - - ConnectionVersion = connectiontypes.ExportedVersionsToProto(connectiontypes.GetCompatibleVersions())[0] - - MockAcknowledgement = mock.MockAcknowledgement.Acknowledgement() - MockPacketData = mock.MockPacketData - MockFailPacketData = mock.MockFailPacketData - MockCanaryCapabilityName = mock.MockCanaryCapabilityName -) - // TestChain is a testing struct that wraps a simapp with the last TM Header, the current ABCI // header and the validators of the TestChain. It also contains a field called ChainID. This // is the clientID that *other* chains use to refer to this TestChain. The SenderAccount @@ -85,7 +44,8 @@ var ( type TestChain struct { t *testing.T - App *simapp.SimApp + Coordinator *Coordinator + App TestingApp ChainID string LastHeader *ibctmtypes.Header // header for last block height committed CurrentHeader tmproto.Header // header for current block height @@ -98,10 +58,6 @@ type TestChain struct { senderPrivKey cryptotypes.PrivKey SenderAccount authtypes.AccountI - - // IBC specific helpers - ClientIDs []string // ClientID's used on this chain - Connections []*TestConnection // track connectionID's created for this chain } // NewTestChain initializes a new TestChain instance with a single validator set using a @@ -112,7 +68,7 @@ type TestChain struct { // // Time management is handled by the Coordinator in order to ensure synchrony between chains. // Each update of any chain increments the block header time for all chains by 5 seconds. -func NewTestChain(t *testing.T, chainID string) *TestChain { +func NewTestChain(t *testing.T, coord *Coordinator, chainID string) *TestChain { // generate validator private/public key privVal := mock.NewPV() pubKey, err := privVal.GetPubKey() @@ -131,54 +87,65 @@ func NewTestChain(t *testing.T, chainID string) *TestChain { Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100000000000000))), } - app := simapp.SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance) + app := SetupWithGenesisValSet(t, valSet, []authtypes.GenesisAccount{acc}, balance) // create current header and call begin block header := tmproto.Header{ ChainID: chainID, Height: 1, - Time: globalStartTime, + Time: coord.CurrentTime.UTC(), } - txConfig := simapp.MakeTestEncodingConfig().TxConfig + txConfig := app.GetTxConfig() // create an account to send transactions from chain := &TestChain{ t: t, + Coordinator: coord, ChainID: chainID, App: app, CurrentHeader: header, - QueryServer: app.IBCKeeper, + QueryServer: app.GetIBCKeeper(), TxConfig: txConfig, Codec: app.AppCodec(), Vals: valSet, Signers: signers, senderPrivKey: senderPrivKey, SenderAccount: acc, - ClientIDs: make([]string, 0), - Connections: make([]*TestConnection, 0), } - cap := chain.App.IBCKeeper.PortKeeper.BindPort(chain.GetContext(), MockPort) - err = chain.App.ScopedIBCMockKeeper.ClaimCapability(chain.GetContext(), cap, host.PortPath(MockPort)) - require.NoError(t, err) - - chain.NextBlock() + coord.CommitBlock(chain) return chain } // GetContext returns the current context for the application. func (chain *TestChain) GetContext() sdk.Context { - return chain.App.BaseApp.NewContext(false, chain.CurrentHeader) + return chain.App.GetBaseApp().NewContext(false, chain.CurrentHeader) +} + +// GetSimApp returns the SimApp to allow usage ofnon-interface fields. +// CONTRACT: This function should not be called by third parties implementing +// their own SimApp. +func (chain *TestChain) GetSimApp() *simapp.SimApp { + app, ok := chain.App.(*simapp.SimApp) + require.True(chain.t, ok) + + return app } // QueryProof performs an abci query with the given key and returns the proto encoded merkle proof // for the query and the height at which the proof will succeed on a tendermint verifier. func (chain *TestChain) QueryProof(key []byte) ([]byte, clienttypes.Height) { + return chain.QueryProofAtHeight(key, chain.App.LastBlockHeight()) +} + +// QueryProof performs an abci query with the given key and returns the proto encoded merkle proof +// for the query and the height at which the proof will succeed on a tendermint verifier. +func (chain *TestChain) QueryProofAtHeight(key []byte, height int64) ([]byte, clienttypes.Height) { res := chain.App.Query(abci.RequestQuery{ Path: fmt.Sprintf("store/%s/key", host.StoreKey), - Height: chain.App.LastBlockHeight() - 1, + Height: height - 1, Data: key, Prove: true, }) @@ -221,19 +188,6 @@ func (chain *TestChain) QueryUpgradeProof(key []byte, height uint64) ([]byte, cl return proof, clienttypes.NewHeight(revision, uint64(res.Height+1)) } -// QueryClientStateProof performs and abci query for a client state -// stored with a given clientID and returns the ClientState along with the proof -func (chain *TestChain) QueryClientStateProof(clientID string) (exported.ClientState, []byte) { - // retrieve client state to provide proof for - clientState, found := chain.App.IBCKeeper.ClientKeeper.GetClientState(chain.GetContext(), clientID) - require.True(chain.t, found) - - clientKey := host.FullClientStateKey(clientID) - proofClient, _ := chain.QueryProof(clientKey) - - return clientState, proofClient -} - // QueryConsensusStateProof performs an abci query for a consensus state // stored on the given clientID. The proof and consensusHeight are returned. func (chain *TestChain) QueryConsensusStateProof(clientID string) ([]byte, clienttypes.Height) { @@ -268,7 +222,6 @@ func (chain *TestChain) NextBlock() { } chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader}) - } // sendMsgs delivers a transaction through the application without returning the result. @@ -281,10 +234,14 @@ func (chain *TestChain) sendMsgs(msgs ...sdk.Msg) error { // number and updates the TestChain's headers. It returns the result and error if one // occurred. func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { - _, r, err := simapp.SignCheckDeliver( + + // ensure the chain has the latest time + chain.Coordinator.UpdateTimeForChain(chain) + + _, r, err := simapp.SignAndDeliver( chain.t, chain.TxConfig, - chain.App.BaseApp, + chain.App.GetBaseApp(), chain.GetContext().BlockHeader(), msgs, chain.ChainID, @@ -296,19 +253,21 @@ func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { return nil, err } - // SignCheckDeliver calls app.Commit() + // SignAndDeliver calls app.Commit() chain.NextBlock() // increment sequence for successful transaction execution chain.SenderAccount.SetSequence(chain.SenderAccount.GetSequence() + 1) + chain.Coordinator.IncrementTime() + return r, nil } // GetClientState retrieves the client state for the provided clientID. The client is // expected to exist otherwise testing will fail. func (chain *TestChain) GetClientState(clientID string) exported.ClientState { - clientState, found := chain.App.IBCKeeper.ClientKeeper.GetClientState(chain.GetContext(), clientID) + clientState, found := chain.App.GetIBCKeeper().ClientKeeper.GetClientState(chain.GetContext(), clientID) require.True(chain.t, found) return clientState @@ -317,13 +276,13 @@ func (chain *TestChain) GetClientState(clientID string) exported.ClientState { // GetConsensusState retrieves the consensus state for the provided clientID and height. // It will return a success boolean depending on if consensus state exists or not. func (chain *TestChain) GetConsensusState(clientID string, height exported.Height) (exported.ConsensusState, bool) { - return chain.App.IBCKeeper.ClientKeeper.GetClientConsensusState(chain.GetContext(), clientID, height) + return chain.App.GetIBCKeeper().ClientKeeper.GetClientConsensusState(chain.GetContext(), clientID, height) } // GetValsAtHeight will return the validator set of the chain at a given height. It will return // a success boolean depending on if the validator set exists or not at that height. func (chain *TestChain) GetValsAtHeight(height int64) (*tmtypes.ValidatorSet, bool) { - histInfo, ok := chain.App.StakingKeeper.GetHistoricalInfo(chain.GetContext(), height) + histInfo, ok := chain.App.GetStakingKeeper().GetHistoricalInfo(chain.GetContext(), height) if !ok { return nil, false } @@ -337,28 +296,10 @@ func (chain *TestChain) GetValsAtHeight(height int64) (*tmtypes.ValidatorSet, bo return tmtypes.NewValidatorSet(tmValidators), true } -// GetConnection retrieves an IBC Connection for the provided TestConnection. The -// connection is expected to exist otherwise testing will fail. -func (chain *TestChain) GetConnection(testConnection *TestConnection) connectiontypes.ConnectionEnd { - connection, found := chain.App.IBCKeeper.ConnectionKeeper.GetConnection(chain.GetContext(), testConnection.ID) - require.True(chain.t, found) - - return connection -} - -// GetChannel retrieves an IBC Channel for the provided TestChannel. The channel -// is expected to exist otherwise testing will fail. -func (chain *TestChain) GetChannel(testChannel TestChannel) channeltypes.Channel { - channel, found := chain.App.IBCKeeper.ChannelKeeper.GetChannel(chain.GetContext(), testChannel.PortID, testChannel.ID) - require.True(chain.t, found) - - return channel -} - // GetAcknowledgement retrieves an acknowledgement for the provided packet. If the // acknowledgement does not exist then testing will fail. func (chain *TestChain) GetAcknowledgement(packet exported.PacketI) []byte { - ack, found := chain.App.IBCKeeper.ChannelKeeper.GetPacketAcknowledgement(chain.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) + ack, found := chain.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(chain.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) require.True(chain.t, found) return ack @@ -366,130 +307,7 @@ func (chain *TestChain) GetAcknowledgement(packet exported.PacketI) []byte { // GetPrefix returns the prefix for used by a chain in connection creation func (chain *TestChain) GetPrefix() commitmenttypes.MerklePrefix { - return commitmenttypes.NewMerklePrefix(chain.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes()) -} - -// NewClientID appends a new clientID string in the format: -// ClientFor -func (chain *TestChain) NewClientID(clientType string) string { - clientID := fmt.Sprintf("%s-%s", clientType, strconv.Itoa(len(chain.ClientIDs))) - chain.ClientIDs = append(chain.ClientIDs, clientID) - return clientID -} - -// AddTestConnection appends a new TestConnection which contains references -// to the connection id, client id and counterparty client id. -func (chain *TestChain) AddTestConnection(clientID, counterpartyClientID string) *TestConnection { - conn := chain.ConstructNextTestConnection(clientID, counterpartyClientID) - - chain.Connections = append(chain.Connections, conn) - return conn -} - -// ConstructNextTestConnection constructs the next test connection to be -// created given a clientID and counterparty clientID. The connection id -// format: -conn -func (chain *TestChain) ConstructNextTestConnection(clientID, counterpartyClientID string) *TestConnection { - connectionID := connectiontypes.FormatConnectionIdentifier(uint64(len(chain.Connections))) - return &TestConnection{ - ID: connectionID, - ClientID: clientID, - NextChannelVersion: DefaultChannelVersion, - CounterpartyClientID: counterpartyClientID, - } -} - -// GetFirstTestConnection returns the first test connection for a given clientID. -// The connection may or may not exist in the chain state. -func (chain *TestChain) GetFirstTestConnection(clientID, counterpartyClientID string) *TestConnection { - if len(chain.Connections) > 0 { - return chain.Connections[0] - } - - return chain.ConstructNextTestConnection(clientID, counterpartyClientID) -} - -// AddTestChannel appends a new TestChannel which contains references to the port and channel ID -// used for channel creation and interaction. See 'NextTestChannel' for channel ID naming format. -func (chain *TestChain) AddTestChannel(conn *TestConnection, portID string) TestChannel { - channel := chain.NextTestChannel(conn, portID) - conn.Channels = append(conn.Channels, channel) - return channel -} - -// NextTestChannel returns the next test channel to be created on this connection, but does not -// add it to the list of created channels. This function is expected to be used when the caller -// has not created the associated channel in app state, but would still like to refer to the -// non-existent channel usually to test for its non-existence. -// -// channel ID format: -chan -// -// The port is passed in by the caller. -func (chain *TestChain) NextTestChannel(conn *TestConnection, portID string) TestChannel { - nextChanSeq := chain.App.IBCKeeper.ChannelKeeper.GetNextChannelSequence(chain.GetContext()) - channelID := channeltypes.FormatChannelIdentifier(nextChanSeq) - return TestChannel{ - PortID: portID, - ID: channelID, - ClientID: conn.ClientID, - CounterpartyClientID: conn.CounterpartyClientID, - Version: conn.NextChannelVersion, - } -} - -// ConstructMsgCreateClient constructs a message to create a new client state (tendermint or solomachine). -// NOTE: a solo machine client will be created with an empty diversifier. -func (chain *TestChain) ConstructMsgCreateClient(counterparty *TestChain, clientID string, clientType string) *clienttypes.MsgCreateClient { - var ( - clientState exported.ClientState - consensusState exported.ConsensusState - ) - - switch clientType { - case exported.Tendermint: - height := counterparty.LastHeader.GetHeight().(clienttypes.Height) - clientState = ibctmtypes.NewClientState( - counterparty.ChainID, DefaultTrustLevel, TrustingPeriod, UnbondingPeriod, MaxClockDrift, - height, commitmenttypes.GetSDKSpecs(), UpgradePath, false, false, - ) - consensusState = counterparty.LastHeader.ConsensusState() - case exported.Solomachine: - solo := NewSolomachine(chain.t, chain.Codec, clientID, "", 1) - clientState = solo.ClientState() - consensusState = solo.ConsensusState() - default: - chain.t.Fatalf("unsupported client state type %s", clientType) - } - - msg, err := clienttypes.NewMsgCreateClient( - clientState, consensusState, chain.SenderAccount.GetAddress().String(), - ) - require.NoError(chain.t, err) - return msg -} - -// CreateTMClient will construct and execute a 07-tendermint MsgCreateClient. A counterparty -// client will be created on the (target) chain. -func (chain *TestChain) CreateTMClient(counterparty *TestChain, clientID string) error { - // construct MsgCreateClient using counterparty - msg := chain.ConstructMsgCreateClient(counterparty, clientID, exported.Tendermint) - return chain.sendMsgs(msg) -} - -// UpdateTMClient will construct and execute a 07-tendermint MsgUpdateClient. The counterparty -// client will be updated on the (target) chain. UpdateTMClient mocks the relayer flow -// necessary for updating a Tendermint client. -func (chain *TestChain) UpdateTMClient(counterparty *TestChain, clientID string) error { - header, err := chain.ConstructUpdateTMClientHeader(counterparty, clientID) - require.NoError(chain.t, err) - - msg, err := clienttypes.NewMsgUpdateClient( - clientID, header, - chain.SenderAccount.GetAddress().String(), - ) - require.NoError(chain.t, err) - - return chain.sendMsgs(msg) + return commitmenttypes.NewMerklePrefix(chain.App.GetIBCKeeper().ConnectionKeeper.GetCommitmentPrefix().Bytes()) } // ConstructUpdateTMClientHeader will construct a valid 07-tendermint Header to update the @@ -534,7 +352,7 @@ func (chain *TestChain) ConstructUpdateTMClientHeader(counterparty *TestChain, c // ExpireClient fast forwards the chain's block time by the provided amount of time which will // expire any clients with a trusting period less than or equal to this amount of time. func (chain *TestChain) ExpireClient(amount time.Duration) { - chain.CurrentHeader.Time = chain.CurrentHeader.Time.Add(amount) + chain.Coordinator.IncrementTimeBy(amount) } // CurrentTMClientHeader creates a TM header using the current header parameters @@ -637,105 +455,21 @@ func CreateSortedSignerArray(altPrivVal, suitePrivVal tmtypes.PrivValidator, } } -// ConnectionOpenInit will construct and execute a MsgConnectionOpenInit. -func (chain *TestChain) ConnectionOpenInit( - counterparty *TestChain, - connection, counterpartyConnection *TestConnection, -) error { - msg := connectiontypes.NewMsgConnectionOpenInit( - connection.ClientID, - connection.CounterpartyClientID, - counterparty.GetPrefix(), DefaultOpenInitVersion, DefaultDelayPeriod, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - -// ConnectionOpenTry will construct and execute a MsgConnectionOpenTry. -func (chain *TestChain) ConnectionOpenTry( - counterparty *TestChain, - connection, counterpartyConnection *TestConnection, -) error { - counterpartyClient, proofClient := counterparty.QueryClientStateProof(counterpartyConnection.ClientID) - - connectionKey := host.ConnectionKey(counterpartyConnection.ID) - proofInit, proofHeight := counterparty.QueryProof(connectionKey) - - proofConsensus, consensusHeight := counterparty.QueryConsensusStateProof(counterpartyConnection.ClientID) - - msg := connectiontypes.NewMsgConnectionOpenTry( - "", connection.ClientID, // does not support handshake continuation - counterpartyConnection.ID, counterpartyConnection.ClientID, - counterpartyClient, counterparty.GetPrefix(), []*connectiontypes.Version{ConnectionVersion}, DefaultDelayPeriod, - proofInit, proofClient, proofConsensus, - proofHeight, consensusHeight, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - -// ConnectionOpenAck will construct and execute a MsgConnectionOpenAck. -func (chain *TestChain) ConnectionOpenAck( - counterparty *TestChain, - connection, counterpartyConnection *TestConnection, -) error { - counterpartyClient, proofClient := counterparty.QueryClientStateProof(counterpartyConnection.ClientID) - - connectionKey := host.ConnectionKey(counterpartyConnection.ID) - proofTry, proofHeight := counterparty.QueryProof(connectionKey) - - proofConsensus, consensusHeight := counterparty.QueryConsensusStateProof(counterpartyConnection.ClientID) - - msg := connectiontypes.NewMsgConnectionOpenAck( - connection.ID, counterpartyConnection.ID, counterpartyClient, // testing doesn't use flexible selection - proofTry, proofClient, proofConsensus, - proofHeight, consensusHeight, - ConnectionVersion, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - -// ConnectionOpenConfirm will construct and execute a MsgConnectionOpenConfirm. -func (chain *TestChain) ConnectionOpenConfirm( - counterparty *TestChain, - connection, counterpartyConnection *TestConnection, -) error { - connectionKey := host.ConnectionKey(counterpartyConnection.ID) - proof, height := counterparty.QueryProof(connectionKey) - - msg := connectiontypes.NewMsgConnectionOpenConfirm( - connection.ID, - proof, height, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - // CreatePortCapability binds and claims a capability for the given portID if it does not // already exist. This function will fail testing on any resulting error. // NOTE: only creation of a capbility for a transfer or mock port is supported // Other applications must bind to the port in InitGenesis or modify this code. -func (chain *TestChain) CreatePortCapability(portID string) { +func (chain *TestChain) CreatePortCapability(scopedKeeper capabilitykeeper.ScopedKeeper, portID string) { // check if the portId is already binded, if not bind it - _, ok := chain.App.ScopedIBCKeeper.GetCapability(chain.GetContext(), host.PortPath(portID)) + _, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.PortPath(portID)) if !ok { // create capability using the IBC capability keeper - cap, err := chain.App.ScopedIBCKeeper.NewCapability(chain.GetContext(), host.PortPath(portID)) + cap, err := chain.App.GetScopedIBCKeeper().NewCapability(chain.GetContext(), host.PortPath(portID)) require.NoError(chain.t, err) - switch portID { - case MockPort: - // claim capability using the mock capability keeper - err = chain.App.ScopedIBCMockKeeper.ClaimCapability(chain.GetContext(), cap, host.PortPath(portID)) - require.NoError(chain.t, err) - case TransferPort: - // claim capability using the transfer capability keeper - err = chain.App.ScopedTransferKeeper.ClaimCapability(chain.GetContext(), cap, host.PortPath(portID)) - require.NoError(chain.t, err) - default: - panic(fmt.Sprintf("unsupported ibc testing package port ID %s", portID)) - } + // claim capability using the scopedKeeper + err = scopedKeeper.ClaimCapability(chain.GetContext(), cap, host.PortPath(portID)) + require.NoError(chain.t, err) } chain.App.Commit() @@ -746,22 +480,23 @@ func (chain *TestChain) CreatePortCapability(portID string) { // GetPortCapability returns the port capability for the given portID. The capability must // exist, otherwise testing will fail. func (chain *TestChain) GetPortCapability(portID string) *capabilitytypes.Capability { - cap, ok := chain.App.ScopedIBCKeeper.GetCapability(chain.GetContext(), host.PortPath(portID)) + cap, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.PortPath(portID)) require.True(chain.t, ok) return cap } // CreateChannelCapability binds and claims a capability for the given portID and channelID -// if it does not already exist. This function will fail testing on any resulting error. -func (chain *TestChain) CreateChannelCapability(portID, channelID string) { +// if it does not already exist. This function will fail testing on any resulting error. The +// scoped keeper passed in will claim the new capability. +func (chain *TestChain) CreateChannelCapability(scopedKeeper capabilitykeeper.ScopedKeeper, portID, channelID string) { capName := host.ChannelCapabilityPath(portID, channelID) // check if the portId is already binded, if not bind it - _, ok := chain.App.ScopedIBCKeeper.GetCapability(chain.GetContext(), capName) + _, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), capName) if !ok { - cap, err := chain.App.ScopedIBCKeeper.NewCapability(chain.GetContext(), capName) + cap, err := chain.App.GetScopedIBCKeeper().NewCapability(chain.GetContext(), capName) require.NoError(chain.t, err) - err = chain.App.ScopedTransferKeeper.ClaimCapability(chain.GetContext(), cap, capName) + err = scopedKeeper.ClaimCapability(chain.GetContext(), cap, capName) require.NoError(chain.t, err) } @@ -773,139 +508,8 @@ func (chain *TestChain) CreateChannelCapability(portID, channelID string) { // GetChannelCapability returns the channel capability for the given portID and channelID. // The capability must exist, otherwise testing will fail. func (chain *TestChain) GetChannelCapability(portID, channelID string) *capabilitytypes.Capability { - cap, ok := chain.App.ScopedIBCKeeper.GetCapability(chain.GetContext(), host.ChannelCapabilityPath(portID, channelID)) + cap, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.ChannelCapabilityPath(portID, channelID)) require.True(chain.t, ok) return cap } - -// ChanOpenInit will construct and execute a MsgChannelOpenInit. -func (chain *TestChain) ChanOpenInit( - ch, counterparty TestChannel, - order channeltypes.Order, - connectionID string, -) error { - msg := channeltypes.NewMsgChannelOpenInit( - ch.PortID, - ch.Version, order, []string{connectionID}, - counterparty.PortID, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - -// ChanOpenTry will construct and execute a MsgChannelOpenTry. -func (chain *TestChain) ChanOpenTry( - counterparty *TestChain, - ch, counterpartyCh TestChannel, - order channeltypes.Order, - connectionID string, -) error { - proof, height := counterparty.QueryProof(host.ChannelKey(counterpartyCh.PortID, counterpartyCh.ID)) - - msg := channeltypes.NewMsgChannelOpenTry( - ch.PortID, "", // does not support handshake continuation - ch.Version, order, []string{connectionID}, - counterpartyCh.PortID, counterpartyCh.ID, counterpartyCh.Version, - proof, height, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - -// ChanOpenAck will construct and execute a MsgChannelOpenAck. -func (chain *TestChain) ChanOpenAck( - counterparty *TestChain, - ch, counterpartyCh TestChannel, -) error { - proof, height := counterparty.QueryProof(host.ChannelKey(counterpartyCh.PortID, counterpartyCh.ID)) - - msg := channeltypes.NewMsgChannelOpenAck( - ch.PortID, ch.ID, - counterpartyCh.ID, counterpartyCh.Version, // testing doesn't use flexible selection - proof, height, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - -// ChanOpenConfirm will construct and execute a MsgChannelOpenConfirm. -func (chain *TestChain) ChanOpenConfirm( - counterparty *TestChain, - ch, counterpartyCh TestChannel, -) error { - proof, height := counterparty.QueryProof(host.ChannelKey(counterpartyCh.PortID, counterpartyCh.ID)) - - msg := channeltypes.NewMsgChannelOpenConfirm( - ch.PortID, ch.ID, - proof, height, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - -// ChanCloseInit will construct and execute a MsgChannelCloseInit. -// -// NOTE: does not work with ibc-transfer module -func (chain *TestChain) ChanCloseInit( - counterparty *TestChain, - channel TestChannel, -) error { - msg := channeltypes.NewMsgChannelCloseInit( - channel.PortID, channel.ID, - chain.SenderAccount.GetAddress().String(), - ) - return chain.sendMsgs(msg) -} - -// GetPacketData returns a ibc-transfer marshalled packet to be used for -// callback testing. -func (chain *TestChain) GetPacketData(counterparty *TestChain) []byte { - packet := ibctransfertypes.FungibleTokenPacketData{ - Denom: TestCoin.Denom, - Amount: TestCoin.Amount.Uint64(), - Sender: chain.SenderAccount.GetAddress().String(), - Receiver: counterparty.SenderAccount.GetAddress().String(), - } - - return packet.GetBytes() -} - -// SendPacket simulates sending a packet through the channel keeper. No message needs to be -// passed since this call is made from a module. -func (chain *TestChain) SendPacket( - packet exported.PacketI, -) error { - channelCap := chain.GetChannelCapability(packet.GetSourcePort(), packet.GetSourceChannel()) - - // no need to send message, acting as a module - err := chain.App.IBCKeeper.ChannelKeeper.SendPacket(chain.GetContext(), channelCap, packet) - if err != nil { - return err - } - - // commit changes - chain.App.Commit() - chain.NextBlock() - - return nil -} - -// WriteAcknowledgement simulates writing an acknowledgement to the chain. -func (chain *TestChain) WriteAcknowledgement( - packet exported.PacketI, -) error { - channelCap := chain.GetChannelCapability(packet.GetDestPort(), packet.GetDestChannel()) - - // no need to send message, acting as a handler - err := chain.App.IBCKeeper.ChannelKeeper.WriteAcknowledgement(chain.GetContext(), channelCap, packet, MockAcknowledgement) - if err != nil { - return err - } - - // commit changes - chain.App.Commit() - chain.NextBlock() - - return nil -} diff --git a/testing/config.go b/testing/config.go new file mode 100644 index 00000000000..7db94150cfb --- /dev/null +++ b/testing/config.go @@ -0,0 +1,65 @@ +package ibctesting + +import ( + "time" + + connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types" + channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" + "github.com/cosmos/ibc-go/modules/core/exported" + ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" + "github.com/cosmos/ibc-go/testing/mock" +) + +type ClientConfig interface { + GetClientType() string +} + +type TendermintConfig struct { + TrustLevel ibctmtypes.Fraction + TrustingPeriod time.Duration + UnbondingPeriod time.Duration + MaxClockDrift time.Duration + AllowUpdateAfterExpiry bool + AllowUpdateAfterMisbehaviour bool +} + +func NewTendermintConfig() *TendermintConfig { + return &TendermintConfig{ + TrustLevel: DefaultTrustLevel, + TrustingPeriod: TrustingPeriod, + UnbondingPeriod: UnbondingPeriod, + MaxClockDrift: MaxClockDrift, + AllowUpdateAfterExpiry: false, + AllowUpdateAfterMisbehaviour: false, + } +} + +func (tmcfg *TendermintConfig) GetClientType() string { + return exported.Tendermint +} + +type ConnectionConfig struct { + DelayPeriod uint64 + Version *connectiontypes.Version +} + +func NewConnectionConfig() *ConnectionConfig { + return &ConnectionConfig{ + DelayPeriod: DefaultDelayPeriod, + Version: ConnectionVersion, + } +} + +type ChannelConfig struct { + PortID string + Version string + Order channeltypes.Order +} + +func NewChannelConfig() *ChannelConfig { + return &ChannelConfig{ + PortID: mock.ModuleName, + Version: DefaultChannelVersion, + Order: channeltypes.UNORDERED, + } +} diff --git a/testing/coordinator.go b/testing/coordinator.go index 9bf6f040dc8..c7a5d6edfd1 100644 --- a/testing/coordinator.go +++ b/testing/coordinator.go @@ -8,15 +8,11 @@ import ( "github.com/stretchr/testify/require" abci "github.com/tendermint/tendermint/abci/types" - - sdk "github.com/cosmos/cosmos-sdk/types" - channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" - host "github.com/cosmos/ibc-go/modules/core/24-host" - "github.com/cosmos/ibc-go/modules/core/exported" ) +const ChainIDPrefix = "testchain" + var ( - ChainIDPrefix = "testchain" globalStartTime = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC) TimeIncrement = time.Second * 5 ) @@ -26,334 +22,146 @@ var ( type Coordinator struct { t *testing.T - Chains map[string]*TestChain + CurrentTime time.Time + Chains map[string]*TestChain } // NewCoordinator initializes Coordinator with N TestChain's func NewCoordinator(t *testing.T, n int) *Coordinator { chains := make(map[string]*TestChain) + coord := &Coordinator{ + t: t, + CurrentTime: globalStartTime, + } for i := 0; i < n; i++ { chainID := GetChainID(i) - chains[chainID] = NewTestChain(t, chainID) + chains[chainID] = NewTestChain(t, coord, chainID) } - return &Coordinator{ - t: t, - Chains: chains, + coord.Chains = chains + + return coord +} + +// IncrementTime iterates through all the TestChain's and increments their current header time +// by 5 seconds. +// +// CONTRACT: this function must be called after every Commit on any TestChain. +func (coord *Coordinator) IncrementTime() { + coord.IncrementTimeBy(TimeIncrement) +} + +// IncrementTimeBy iterates through all the TestChain's and increments their current header time +// by specified time. +func (coord *Coordinator) IncrementTimeBy(increment time.Duration) { + coord.CurrentTime = coord.CurrentTime.Add(increment).UTC() + coord.UpdateTime() + +} + +// UpdateTime updates all clocks for the TestChains to the current global time. +func (coord *Coordinator) UpdateTime() { + for _, chain := range coord.Chains { + coord.UpdateTimeForChain(chain) } } +// UpdateTimeForChain updates the clock for a specific chain. +func (coord *Coordinator) UpdateTimeForChain(chain *TestChain) { + chain.CurrentHeader.Time = coord.CurrentTime.UTC() + chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader}) +} + // Setup constructs a TM client, connection, and channel on both chains provided. It will // fail if any error occurs. The clientID's, TestConnections, and TestChannels are returned // for both chains. The channels created are connected to the ibc-transfer application. -func (coord *Coordinator) Setup( - chainA, chainB *TestChain, order channeltypes.Order, -) (string, string, *TestConnection, *TestConnection, TestChannel, TestChannel) { - clientA, clientB, connA, connB := coord.SetupClientConnections(chainA, chainB, exported.Tendermint) +func (coord *Coordinator) Setup(path *Path) { + coord.SetupConnections(path) // channels can also be referenced through the returned connections - channelA, channelB := coord.CreateMockChannels(chainA, chainB, connA, connB, order) - - return clientA, clientB, connA, connB, channelA, channelB + coord.CreateChannels(path) } // SetupClients is a helper function to create clients on both chains. It assumes the // caller does not anticipate any errors. -func (coord *Coordinator) SetupClients( - chainA, chainB *TestChain, - clientType string, -) (string, string) { - - clientA, err := coord.CreateClient(chainA, chainB, clientType) +func (coord *Coordinator) SetupClients(path *Path) { + err := path.EndpointA.CreateClient() require.NoError(coord.t, err) - clientB, err := coord.CreateClient(chainB, chainA, clientType) + err = path.EndpointB.CreateClient() require.NoError(coord.t, err) - - return clientA, clientB } // SetupClientConnections is a helper function to create clients and the appropriate // connections on both the source and counterparty chain. It assumes the caller does not // anticipate any errors. -func (coord *Coordinator) SetupClientConnections( - chainA, chainB *TestChain, - clientType string, -) (string, string, *TestConnection, *TestConnection) { - - clientA, clientB := coord.SetupClients(chainA, chainB, clientType) - - connA, connB := coord.CreateConnection(chainA, chainB, clientA, clientB) - - return clientA, clientB, connA, connB -} - -// CreateClient creates a counterparty client on the source chain and returns the clientID. -func (coord *Coordinator) CreateClient( - source, counterparty *TestChain, - clientType string, -) (clientID string, err error) { - coord.CommitBlock(source, counterparty) - - clientID = source.NewClientID(clientType) - - switch clientType { - case exported.Tendermint: - err = source.CreateTMClient(counterparty, clientID) - - default: - err = fmt.Errorf("client type %s is not supported", clientType) - } - - if err != nil { - return "", err - } - - coord.IncrementTime() - - return clientID, nil -} - -// UpdateClient updates a counterparty client on the source chain. -func (coord *Coordinator) UpdateClient( - source, counterparty *TestChain, - clientID string, - clientType string, -) (err error) { - coord.CommitBlock(source, counterparty) - - switch clientType { - case exported.Tendermint: - err = source.UpdateTMClient(counterparty, clientID) - - default: - err = fmt.Errorf("client type %s is not supported", clientType) - } - - if err != nil { - return err - } +func (coord *Coordinator) SetupConnections(path *Path) { + coord.SetupClients(path) - coord.IncrementTime() - - return nil + coord.CreateConnections(path) } // CreateConnection constructs and executes connection handshake messages in order to create // OPEN channels on chainA and chainB. The connection information of for chainA and chainB // are returned within a TestConnection struct. The function expects the connections to be // successfully opened otherwise testing will fail. -func (coord *Coordinator) CreateConnection( - chainA, chainB *TestChain, - clientA, clientB string, -) (*TestConnection, *TestConnection) { +func (coord *Coordinator) CreateConnections(path *Path) { - connA, connB, err := coord.ConnOpenInit(chainA, chainB, clientA, clientB) + err := path.EndpointA.ConnOpenInit() require.NoError(coord.t, err) - err = coord.ConnOpenTry(chainB, chainA, connB, connA) + err = path.EndpointB.ConnOpenTry() require.NoError(coord.t, err) - err = coord.ConnOpenAck(chainA, chainB, connA, connB) + err = path.EndpointA.ConnOpenAck() require.NoError(coord.t, err) - err = coord.ConnOpenConfirm(chainB, chainA, connB, connA) + err = path.EndpointB.ConnOpenConfirm() require.NoError(coord.t, err) - return connA, connB + // ensure counterparty is up to date + path.EndpointA.UpdateClient() } // CreateMockChannels constructs and executes channel handshake messages to create OPEN // channels that use a mock application module that returns nil on all callbacks. This // function is expects the channels to be successfully opened otherwise testing will // fail. -func (coord *Coordinator) CreateMockChannels( - chainA, chainB *TestChain, - connA, connB *TestConnection, - order channeltypes.Order, -) (TestChannel, TestChannel) { - return coord.CreateChannel(chainA, chainB, connA, connB, MockPort, MockPort, order) +func (coord *Coordinator) CreateMockChannels(path *Path) { + path.EndpointA.ChannelConfig.PortID = MockPort + path.EndpointB.ChannelConfig.PortID = MockPort + + coord.CreateChannels(path) } // CreateTransferChannels constructs and executes channel handshake messages to create OPEN // ibc-transfer channels on chainA and chainB. The function expects the channels to be // successfully opened otherwise testing will fail. -func (coord *Coordinator) CreateTransferChannels( - chainA, chainB *TestChain, - connA, connB *TestConnection, - order channeltypes.Order, -) (TestChannel, TestChannel) { - return coord.CreateChannel(chainA, chainB, connA, connB, TransferPort, TransferPort, order) +func (coord *Coordinator) CreateTransferChannels(path *Path) { + path.EndpointA.ChannelConfig.PortID = TransferPort + path.EndpointB.ChannelConfig.PortID = TransferPort + + coord.CreateChannels(path) } // CreateChannel constructs and executes channel handshake messages in order to create // OPEN channels on chainA and chainB. The function expects the channels to be successfully // opened otherwise testing will fail. -func (coord *Coordinator) CreateChannel( - chainA, chainB *TestChain, - connA, connB *TestConnection, - sourcePortID, counterpartyPortID string, - order channeltypes.Order, -) (TestChannel, TestChannel) { - - channelA, channelB, err := coord.ChanOpenInit(chainA, chainB, connA, connB, sourcePortID, counterpartyPortID, order) +func (coord *Coordinator) CreateChannels(path *Path) { + err := path.EndpointA.ChanOpenInit() require.NoError(coord.t, err) - err = coord.ChanOpenTry(chainB, chainA, channelB, channelA, connB, order) + err = path.EndpointB.ChanOpenTry() require.NoError(coord.t, err) - err = coord.ChanOpenAck(chainA, chainB, channelA, channelB) + err = path.EndpointA.ChanOpenAck() require.NoError(coord.t, err) - err = coord.ChanOpenConfirm(chainB, chainA, channelB, channelA) + err = path.EndpointB.ChanOpenConfirm() require.NoError(coord.t, err) - return channelA, channelB -} - -// SendPacket sends a packet through the channel keeper on the source chain and updates the -// counterparty client for the source chain. -func (coord *Coordinator) SendPacket( - source, counterparty *TestChain, - packet exported.PacketI, - counterpartyClientID string, -) error { - if err := source.SendPacket(packet); err != nil { - return err - } - coord.IncrementTime() - - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - counterpartyClientID, exported.Tendermint, - ) -} - -// RecvPacket receives a channel packet on the counterparty chain and updates -// the client on the source chain representing the counterparty. -func (coord *Coordinator) RecvPacket( - source, counterparty *TestChain, - sourceClient string, - packet channeltypes.Packet, -) error { - // get proof of packet commitment on source - packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - proof, proofHeight := source.QueryProof(packetKey) - - // Increment time and commit block so that 5 second delay period passes between send and receive - coord.IncrementTime() - coord.CommitBlock(source, counterparty) - - recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, counterparty.SenderAccount.GetAddress().String()) - - // receive on counterparty and update source client - return coord.SendMsgs(counterparty, source, sourceClient, []sdk.Msg{recvMsg}) -} - -// WriteAcknowledgement writes an acknowledgement to the channel keeper on the source chain and updates the -// counterparty client for the source chain. -func (coord *Coordinator) WriteAcknowledgement( - source, counterparty *TestChain, - packet exported.PacketI, - counterpartyClientID string, -) error { - if err := source.WriteAcknowledgement(packet); err != nil { - return err - } - coord.IncrementTime() - - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - counterpartyClientID, exported.Tendermint, - ) -} - -// AcknowledgePacket acknowledges on the source chain the packet received on -// the counterparty chain and updates the client on the counterparty representing -// the source chain. -// TODO: add a query for the acknowledgement by events -// - https://github.com/cosmos/cosmos-sdk/issues/6509 -func (coord *Coordinator) AcknowledgePacket( - source, counterparty *TestChain, - counterpartyClient string, - packet channeltypes.Packet, ack []byte, -) error { - // get proof of acknowledgement on counterparty - packetKey := host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) - proof, proofHeight := counterparty.QueryProof(packetKey) - - // Increment time and commit block so that 5 second delay period passes between send and receive - coord.IncrementTime() - coord.CommitBlock(source, counterparty) - - ackMsg := channeltypes.NewMsgAcknowledgement(packet, ack, proof, proofHeight, source.SenderAccount.GetAddress().String()) - return coord.SendMsgs(source, counterparty, counterpartyClient, []sdk.Msg{ackMsg}) -} - -// RelayPacket receives a channel packet on counterparty, queries the ack -// and acknowledges the packet on source. The clients are updated as needed. -func (coord *Coordinator) RelayPacket( - source, counterparty *TestChain, - sourceClient, counterpartyClient string, - packet channeltypes.Packet, ack []byte, -) error { - // Increment time and commit block so that 5 second delay period passes between send and receive - coord.IncrementTime() - coord.CommitBlock(counterparty) - - if err := coord.RecvPacket(source, counterparty, sourceClient, packet); err != nil { - return err - } - - // Increment time and commit block so that 5 second delay period passes between send and receive - coord.IncrementTime() - coord.CommitBlock(source) - - return coord.AcknowledgePacket(source, counterparty, counterpartyClient, packet, ack) -} - -// IncrementTime iterates through all the TestChain's and increments their current header time -// by 5 seconds. -// -// CONTRACT: this function must be called after every commit on any TestChain. -func (coord *Coordinator) IncrementTime() { - for _, chain := range coord.Chains { - chain.CurrentHeader.Time = chain.CurrentHeader.Time.Add(TimeIncrement) - chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader}) - } -} - -// IncrementTimeBy iterates through all the TestChain's and increments their current header time -// by specified time. -func (coord *Coordinator) IncrementTimeBy(increment time.Duration) { - for _, chain := range coord.Chains { - chain.CurrentHeader.Time = chain.CurrentHeader.Time.Add(increment) - chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader}) - } -} - -// SendMsg delivers a single provided message to the chain. The counterparty -// client is update with the new source consensus state. -func (coord *Coordinator) SendMsg(source, counterparty *TestChain, counterpartyClientID string, msg sdk.Msg) error { - return coord.SendMsgs(source, counterparty, counterpartyClientID, []sdk.Msg{msg}) -} - -// SendMsgs delivers the provided messages to the chain. The counterparty -// client is updated with the new source consensus state. -func (coord *Coordinator) SendMsgs(source, counterparty *TestChain, counterpartyClientID string, msgs []sdk.Msg) error { - if err := source.sendMsgs(msgs...); err != nil { - return err - } - - coord.IncrementTime() - - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - counterpartyClientID, exported.Tendermint, - ) + // ensure counterparty is up to date + path.EndpointA.UpdateClient() } // GetChain returns the TestChain using the given chainID and returns an error if it does @@ -390,311 +198,49 @@ func (coord *Coordinator) CommitNBlocks(chain *TestChain, n uint64) { } } -// ConnOpenInit initializes a connection on the source chain with the state INIT -// using the OpenInit handshake call. -// -// NOTE: The counterparty testing connection will be created even if it is not created in the -// application state. -func (coord *Coordinator) ConnOpenInit( - source, counterparty *TestChain, - clientID, counterpartyClientID string, -) (*TestConnection, *TestConnection, error) { - sourceConnection := source.AddTestConnection(clientID, counterpartyClientID) - counterpartyConnection := counterparty.AddTestConnection(counterpartyClientID, clientID) - - // initialize connection on source - if err := source.ConnectionOpenInit(counterparty, sourceConnection, counterpartyConnection); err != nil { - return sourceConnection, counterpartyConnection, err - } - coord.IncrementTime() - - // update source client on counterparty connection - if err := coord.UpdateClient( - counterparty, source, - counterpartyClientID, exported.Tendermint, - ); err != nil { - return sourceConnection, counterpartyConnection, err - } - - return sourceConnection, counterpartyConnection, nil -} - -// ConnOpenInitOnBothChains initializes a connection on the source chain with the state INIT +// ConnOpenInitOnBothChains initializes a connection on both endpoints with the state INIT // using the OpenInit handshake call. -func (coord *Coordinator) ConnOpenInitOnBothChains( - source, counterparty *TestChain, - clientID, counterpartyClientID string, -) (*TestConnection, *TestConnection, error) { - sourceConnection := source.AddTestConnection(clientID, counterpartyClientID) - counterpartyConnection := counterparty.AddTestConnection(counterpartyClientID, clientID) - - // initialize connection on source - if err := source.ConnectionOpenInit(counterparty, sourceConnection, counterpartyConnection); err != nil { - return sourceConnection, counterpartyConnection, err - } - coord.IncrementTime() - - // initialize connection on counterparty - if err := counterparty.ConnectionOpenInit(source, counterpartyConnection, sourceConnection); err != nil { - return sourceConnection, counterpartyConnection, err - } - coord.IncrementTime() - - // update counterparty client on source connection - if err := coord.UpdateClient( - source, counterparty, - clientID, exported.Tendermint, - ); err != nil { - return sourceConnection, counterpartyConnection, err - } - - // update source client on counterparty connection - if err := coord.UpdateClient( - counterparty, source, - counterpartyClientID, exported.Tendermint, - ); err != nil { - return sourceConnection, counterpartyConnection, err - } - - return sourceConnection, counterpartyConnection, nil -} - -// ConnOpenTry initializes a connection on the source chain with the state TRYOPEN -// using the OpenTry handshake call. -func (coord *Coordinator) ConnOpenTry( - source, counterparty *TestChain, - sourceConnection, counterpartyConnection *TestConnection, -) error { - // initialize TRYOPEN connection on source - if err := source.ConnectionOpenTry(counterparty, sourceConnection, counterpartyConnection); err != nil { +func (coord *Coordinator) ConnOpenInitOnBothChains(path *Path) error { + if err := path.EndpointA.ConnOpenInit(); err != nil { return err } - coord.IncrementTime() - - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - counterpartyConnection.ClientID, exported.Tendermint, - ) -} -// ConnOpenAck initializes a connection on the source chain with the state OPEN -// using the OpenAck handshake call. -func (coord *Coordinator) ConnOpenAck( - source, counterparty *TestChain, - sourceConnection, counterpartyConnection *TestConnection, -) error { - // set OPEN connection on source using OpenAck - if err := source.ConnectionOpenAck(counterparty, sourceConnection, counterpartyConnection); err != nil { + if err := path.EndpointB.ConnOpenInit(); err != nil { return err } - coord.IncrementTime() - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - counterpartyConnection.ClientID, exported.Tendermint, - ) -} - -// ConnOpenConfirm initializes a connection on the source chain with the state OPEN -// using the OpenConfirm handshake call. -func (coord *Coordinator) ConnOpenConfirm( - source, counterparty *TestChain, - sourceConnection, counterpartyConnection *TestConnection, -) error { - if err := source.ConnectionOpenConfirm(counterparty, sourceConnection, counterpartyConnection); err != nil { + if err := path.EndpointA.UpdateClient(); err != nil { return err } - coord.IncrementTime() - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - counterpartyConnection.ClientID, exported.Tendermint, - ) -} - -// ChanOpenInit initializes a channel on the source chain with the state INIT -// using the OpenInit handshake call. -// -// NOTE: The counterparty testing channel will be created even if it is not created in the -// application state. -func (coord *Coordinator) ChanOpenInit( - source, counterparty *TestChain, - connection, counterpartyConnection *TestConnection, - sourcePortID, counterpartyPortID string, - order channeltypes.Order, -) (TestChannel, TestChannel, error) { - sourceChannel := source.AddTestChannel(connection, sourcePortID) - counterpartyChannel := counterparty.AddTestChannel(counterpartyConnection, counterpartyPortID) - - // NOTE: only creation of a capability for a transfer or mock port is supported - // Other applications must bind to the port in InitGenesis or modify this code. - source.CreatePortCapability(sourceChannel.PortID) - coord.IncrementTime() - - // initialize channel on source - if err := source.ChanOpenInit(sourceChannel, counterpartyChannel, order, connection.ID); err != nil { - return sourceChannel, counterpartyChannel, err - } - coord.IncrementTime() - - // update source client on counterparty connection - if err := coord.UpdateClient( - counterparty, source, - counterpartyConnection.ClientID, exported.Tendermint, - ); err != nil { - return sourceChannel, counterpartyChannel, err + if err := path.EndpointB.UpdateClient(); err != nil { + return err } - return sourceChannel, counterpartyChannel, nil + return nil } // ChanOpenInitOnBothChains initializes a channel on the source chain and counterparty chain // with the state INIT using the OpenInit handshake call. -func (coord *Coordinator) ChanOpenInitOnBothChains( - source, counterparty *TestChain, - connection, counterpartyConnection *TestConnection, - sourcePortID, counterpartyPortID string, - order channeltypes.Order, -) (TestChannel, TestChannel, error) { - sourceChannel := source.AddTestChannel(connection, sourcePortID) - counterpartyChannel := counterparty.AddTestChannel(counterpartyConnection, counterpartyPortID) - +func (coord *Coordinator) ChanOpenInitOnBothChains(path *Path) error { // NOTE: only creation of a capability for a transfer or mock port is supported // Other applications must bind to the port in InitGenesis or modify this code. - source.CreatePortCapability(sourceChannel.PortID) - counterparty.CreatePortCapability(counterpartyChannel.PortID) - coord.IncrementTime() - - // initialize channel on source - if err := source.ChanOpenInit(sourceChannel, counterpartyChannel, order, connection.ID); err != nil { - return sourceChannel, counterpartyChannel, err - } - coord.IncrementTime() - - // initialize channel on counterparty - if err := counterparty.ChanOpenInit(counterpartyChannel, sourceChannel, order, counterpartyConnection.ID); err != nil { - return sourceChannel, counterpartyChannel, err - } - coord.IncrementTime() - - // update counterparty client on source connection - if err := coord.UpdateClient( - source, counterparty, - connection.ClientID, exported.Tendermint, - ); err != nil { - return sourceChannel, counterpartyChannel, err - } - - // update source client on counterparty connection - if err := coord.UpdateClient( - counterparty, source, - counterpartyConnection.ClientID, exported.Tendermint, - ); err != nil { - return sourceChannel, counterpartyChannel, err - } - return sourceChannel, counterpartyChannel, nil -} - -// ChanOpenTry initializes a channel on the source chain with the state TRYOPEN -// using the OpenTry handshake call. -func (coord *Coordinator) ChanOpenTry( - source, counterparty *TestChain, - sourceChannel, counterpartyChannel TestChannel, - connection *TestConnection, - order channeltypes.Order, -) error { - - // initialize channel on source - if err := source.ChanOpenTry(counterparty, sourceChannel, counterpartyChannel, order, connection.ID); err != nil { + if err := path.EndpointA.ChanOpenInit(); err != nil { return err } - coord.IncrementTime() - - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - connection.CounterpartyClientID, exported.Tendermint, - ) -} - -// ChanOpenAck initializes a channel on the source chain with the state OPEN -// using the OpenAck handshake call. -func (coord *Coordinator) ChanOpenAck( - source, counterparty *TestChain, - sourceChannel, counterpartyChannel TestChannel, -) error { - if err := source.ChanOpenAck(counterparty, sourceChannel, counterpartyChannel); err != nil { + if err := path.EndpointB.ChanOpenInit(); err != nil { return err } - coord.IncrementTime() - - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - sourceChannel.CounterpartyClientID, exported.Tendermint, - ) -} -// ChanOpenConfirm initializes a channel on the source chain with the state OPEN -// using the OpenConfirm handshake call. -func (coord *Coordinator) ChanOpenConfirm( - source, counterparty *TestChain, - sourceChannel, counterpartyChannel TestChannel, -) error { - - if err := source.ChanOpenConfirm(counterparty, sourceChannel, counterpartyChannel); err != nil { + if err := path.EndpointA.UpdateClient(); err != nil { return err } - coord.IncrementTime() - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - sourceChannel.CounterpartyClientID, exported.Tendermint, - ) -} - -// ChanCloseInit closes a channel on the source chain resulting in the channels state -// being set to CLOSED. -// -// NOTE: does not work with ibc-transfer module -func (coord *Coordinator) ChanCloseInit( - source, counterparty *TestChain, - channel TestChannel, -) error { - - if err := source.ChanCloseInit(counterparty, channel); err != nil { + if err := path.EndpointB.UpdateClient(); err != nil { return err } - coord.IncrementTime() - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - channel.CounterpartyClientID, exported.Tendermint, - ) -} - -// SetChannelClosed sets a channel state to CLOSED. -func (coord *Coordinator) SetChannelClosed( - source, counterparty *TestChain, - testChannel TestChannel, -) error { - channel := source.GetChannel(testChannel) - - channel.State = channeltypes.CLOSED - source.App.IBCKeeper.ChannelKeeper.SetChannel(source.GetContext(), testChannel.PortID, testChannel.ID, channel) - - coord.CommitBlock(source) - - // update source client on counterparty connection - return coord.UpdateClient( - counterparty, source, - testChannel.CounterpartyClientID, exported.Tendermint, - ) + return nil } diff --git a/testing/endpoint.go b/testing/endpoint.go new file mode 100644 index 00000000000..3a9e8dbd9e4 --- /dev/null +++ b/testing/endpoint.go @@ -0,0 +1,464 @@ +package ibctesting + +import ( + "fmt" + + // sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + + clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" + connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types" + channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" + commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types" + host "github.com/cosmos/ibc-go/modules/core/24-host" + "github.com/cosmos/ibc-go/modules/core/exported" + ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" +) + +// Endpoint is a which represents a channel endpoint and its associated +// client and connections. It contains client, connection, and channel +// configuration parameters. Endpoint functions will utilize the parameters +// set in the configuration structs when executing IBC messages. +type Endpoint struct { + Chain *TestChain + Counterparty *Endpoint + ClientID string + ConnectionID string + ChannelID string + + ClientConfig ClientConfig + ConnectionConfig *ConnectionConfig + ChannelConfig *ChannelConfig +} + +// NewEndpoint constructs a new endpoint without the counterparty. +// CONTRACT: the counterparty endpoint must be set by the caller. +func NewEndpoint( + chain *TestChain, clientConfig ClientConfig, + connectionConfig *ConnectionConfig, channelConfig *ChannelConfig, +) *Endpoint { + return &Endpoint{ + Chain: chain, + ClientConfig: clientConfig, + ConnectionConfig: connectionConfig, + ChannelConfig: channelConfig, + } +} + +// NewDefaultEndpoint constructs a new endpoint using default values. +// CONTRACT: the counterparty endpoitn must be set by the caller. +func NewDefaultEndpoint(chain *TestChain) *Endpoint { + return &Endpoint{ + Chain: chain, + ClientConfig: NewTendermintConfig(), + ConnectionConfig: NewConnectionConfig(), + ChannelConfig: NewChannelConfig(), + } +} + +// QueryProof queries proof associated with this endpoint using the lastest client state +// height on the counterparty chain. +func (endpoint *Endpoint) QueryProof(key []byte) ([]byte, clienttypes.Height) { + // obtain the counterparty client representing the chain associated with the endpoint + clientState := endpoint.Counterparty.Chain.GetClientState(endpoint.Counterparty.ClientID) + + // query proof on the counterparty using the latest height of the IBC client + return endpoint.QueryProofAtHeight(key, clientState.GetLatestHeight().GetRevisionHeight()) +} + +// QueryProofAtHeight queries proof associated with this endpoint using the proof height +// providied +func (endpoint *Endpoint) QueryProofAtHeight(key []byte, height uint64) ([]byte, clienttypes.Height) { + // query proof on the counterparty using the latest height of the IBC client + return endpoint.Chain.QueryProofAtHeight(key, int64(height)) +} + +// CreateClient creates an IBC client on the endpoint. It will update the +// clientID for the endpoint if the message is successfully executed. +// NOTE: a solo machine client will be created with an empty diversifier. +func (endpoint *Endpoint) CreateClient() (err error) { + // ensure counterparty has committed state + endpoint.Chain.Coordinator.CommitBlock(endpoint.Counterparty.Chain) + + var ( + clientState exported.ClientState + consensusState exported.ConsensusState + ) + + switch endpoint.ClientConfig.GetClientType() { + case exported.Tendermint: + tmConfig, ok := endpoint.ClientConfig.(*TendermintConfig) + require.True(endpoint.Chain.t, ok) + + height := endpoint.Counterparty.Chain.LastHeader.GetHeight().(clienttypes.Height) + clientState = ibctmtypes.NewClientState( + endpoint.Counterparty.Chain.ChainID, tmConfig.TrustLevel, tmConfig.TrustingPeriod, tmConfig.UnbondingPeriod, tmConfig.MaxClockDrift, + height, commitmenttypes.GetSDKSpecs(), UpgradePath, tmConfig.AllowUpdateAfterExpiry, tmConfig.AllowUpdateAfterMisbehaviour, + ) + consensusState = endpoint.Counterparty.Chain.LastHeader.ConsensusState() + case exported.Solomachine: + // TODO + // solo := NewSolomachine(chain.t, endpoint.Chain.Codec, clientID, "", 1) + // clientState = solo.ClientState() + // consensusState = solo.ConsensusState() + + default: + err = fmt.Errorf("client type %s is not supported", endpoint.ClientConfig.GetClientType()) + } + + if err != nil { + return err + } + + msg, err := clienttypes.NewMsgCreateClient( + clientState, consensusState, endpoint.Chain.SenderAccount.GetAddress().String(), + ) + require.NoError(endpoint.Chain.t, err) + + res, err := endpoint.Chain.SendMsgs(msg) + if err != nil { + return err + } + + endpoint.ClientID, err = ParseClientIDFromEvents(res.GetEvents()) + require.NoError(endpoint.Chain.t, err) + + return nil +} + +// UpdateClient updates the IBC client associated with the endpoint. +func (endpoint *Endpoint) UpdateClient() (err error) { + // ensure counterparty has committed state + endpoint.Chain.Coordinator.CommitBlock(endpoint.Counterparty.Chain) + + var ( + header exported.Header + ) + + switch endpoint.ClientConfig.GetClientType() { + case exported.Tendermint: + header, err = endpoint.Chain.ConstructUpdateTMClientHeader(endpoint.Counterparty.Chain, endpoint.ClientID) + + default: + err = fmt.Errorf("client type %s is not supported", endpoint.ClientConfig.GetClientType()) + } + + if err != nil { + return err + } + + msg, err := clienttypes.NewMsgUpdateClient( + endpoint.ClientID, header, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + require.NoError(endpoint.Chain.t, err) + + return endpoint.Chain.sendMsgs(msg) + +} + +// ConnOpenInit will construct and execute a MsgConnectionOpenInit on the associated endpoint. +func (endpoint *Endpoint) ConnOpenInit() error { + msg := connectiontypes.NewMsgConnectionOpenInit( + endpoint.ClientID, + endpoint.Counterparty.ClientID, + endpoint.Counterparty.Chain.GetPrefix(), DefaultOpenInitVersion, endpoint.ConnectionConfig.DelayPeriod, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + res, err := endpoint.Chain.SendMsgs(msg) + if err != nil { + return err + } + + endpoint.ConnectionID, err = ParseConnectionIDFromEvents(res.GetEvents()) + require.NoError(endpoint.Chain.t, err) + + return nil +} + +// ConnOpenTry will construct and execute a MsgConnectionOpenTry on the associated endpoint. +func (endpoint *Endpoint) ConnOpenTry() error { + endpoint.UpdateClient() + + counterpartyClient, proofClient, proofConsensus, consensusHeight, proofInit, proofHeight := endpoint.QueryConnectionHandshakeProof() + + msg := connectiontypes.NewMsgConnectionOpenTry( + "", endpoint.ClientID, // does not support handshake continuation + endpoint.Counterparty.ConnectionID, endpoint.Counterparty.ClientID, + counterpartyClient, endpoint.Counterparty.Chain.GetPrefix(), []*connectiontypes.Version{ConnectionVersion}, endpoint.ConnectionConfig.DelayPeriod, + proofInit, proofClient, proofConsensus, + proofHeight, consensusHeight, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + res, err := endpoint.Chain.SendMsgs(msg) + if err != nil { + return err + } + + if endpoint.ConnectionID == "" { + endpoint.ConnectionID, err = ParseConnectionIDFromEvents(res.GetEvents()) + require.NoError(endpoint.Chain.t, err) + } + + return nil +} + +// ConnOpenAck will construct and execute a MsgConnectionOpenAck on the associated endpoint. +func (endpoint *Endpoint) ConnOpenAck() error { + endpoint.UpdateClient() + + counterpartyClient, proofClient, proofConsensus, consensusHeight, proofTry, proofHeight := endpoint.QueryConnectionHandshakeProof() + + msg := connectiontypes.NewMsgConnectionOpenAck( + endpoint.ConnectionID, endpoint.Counterparty.ConnectionID, counterpartyClient, // testing doesn't use flexible selection + proofTry, proofClient, proofConsensus, + proofHeight, consensusHeight, + ConnectionVersion, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + return endpoint.Chain.sendMsgs(msg) +} + +// ConnOpenConfirm will construct and execute a MsgConnectionOpenConfirm on the associated endpoint. +func (endpoint *Endpoint) ConnOpenConfirm() error { + endpoint.UpdateClient() + + connectionKey := host.ConnectionKey(endpoint.Counterparty.ConnectionID) + proof, height := endpoint.Counterparty.Chain.QueryProof(connectionKey) + + msg := connectiontypes.NewMsgConnectionOpenConfirm( + endpoint.ConnectionID, + proof, height, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + return endpoint.Chain.sendMsgs(msg) +} + +// QueryConnectionHandshakeProof returns all the proofs necessary to execute OpenTry or Open Ack of +// the connection handshakes. It returns the counterparty client state, proof of the counterparty +// client state, proof of the counterparty consensus state, the consensus state height, proof of +// the counterparty connection, and the proof height for all the proofs returned. +func (endpoint *Endpoint) QueryConnectionHandshakeProof() ( + clientState exported.ClientState, proofClient, + proofConsensus []byte, consensusHeight clienttypes.Height, + proofConnection []byte, proofHeight clienttypes.Height, +) { + // obtain the client state on the counterparty chain + clientState = endpoint.Counterparty.Chain.GetClientState(endpoint.Counterparty.ClientID) + + // query proof for the client state on the counterparty + clientKey := host.FullClientStateKey(endpoint.Counterparty.ClientID) + proofClient, proofHeight = endpoint.Counterparty.QueryProof(clientKey) + + consensusHeight = clientState.GetLatestHeight().(clienttypes.Height) + + // query proof for the consensus state on the counterparty + consensusKey := host.FullConsensusStateKey(endpoint.Counterparty.ClientID, consensusHeight) + proofConsensus, _ = endpoint.Counterparty.QueryProofAtHeight(consensusKey, proofHeight.GetRevisionHeight()) + + // query proof for the connection on the counterparty + connectionKey := host.ConnectionKey(endpoint.Counterparty.ConnectionID) + proofConnection, _ = endpoint.Counterparty.QueryProofAtHeight(connectionKey, proofHeight.GetRevisionHeight()) + + return +} + +// ChanOpenInit will construct and execute a MsgChannelOpenInit on the associated endpoint. +func (endpoint *Endpoint) ChanOpenInit() error { + msg := channeltypes.NewMsgChannelOpenInit( + endpoint.ChannelConfig.PortID, + endpoint.ChannelConfig.Version, endpoint.ChannelConfig.Order, []string{endpoint.ConnectionID}, + endpoint.Counterparty.ChannelConfig.PortID, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + res, err := endpoint.Chain.SendMsgs(msg) + if err != nil { + return err + } + + endpoint.ChannelID, err = ParseChannelIDFromEvents(res.GetEvents()) + require.NoError(endpoint.Chain.t, err) + + return nil +} + +// ChanOpenTry will construct and execute a MsgChannelOpenTry on the associated endpoint. +func (endpoint *Endpoint) ChanOpenTry() error { + endpoint.UpdateClient() + + channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) + proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) + + msg := channeltypes.NewMsgChannelOpenTry( + endpoint.ChannelConfig.PortID, "", // does not support handshake continuation + endpoint.ChannelConfig.Version, endpoint.ChannelConfig.Order, []string{endpoint.ConnectionID}, + endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID, endpoint.Counterparty.ChannelConfig.Version, + proof, height, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + res, err := endpoint.Chain.SendMsgs(msg) + if err != nil { + return err + } + + if endpoint.ChannelID == "" { + endpoint.ChannelID, err = ParseChannelIDFromEvents(res.GetEvents()) + require.NoError(endpoint.Chain.t, err) + } + + return nil +} + +// ChanOpenAck will construct and execute a MsgChannelOpenAck on the associated endpoint. +func (endpoint *Endpoint) ChanOpenAck() error { + endpoint.UpdateClient() + + channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) + proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) + + msg := channeltypes.NewMsgChannelOpenAck( + endpoint.ChannelConfig.PortID, endpoint.ChannelID, + endpoint.Counterparty.ChannelID, endpoint.Counterparty.ChannelConfig.Version, // testing doesn't use flexible selection + proof, height, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + return endpoint.Chain.sendMsgs(msg) +} + +// ChanOpenConfirm will construct and execute a MsgChannelOpenConfirm on the associated endpoint. +func (endpoint *Endpoint) ChanOpenConfirm() error { + endpoint.UpdateClient() + + channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) + proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) + + msg := channeltypes.NewMsgChannelOpenConfirm( + endpoint.ChannelConfig.PortID, endpoint.ChannelID, + proof, height, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + return endpoint.Chain.sendMsgs(msg) +} + +// ChanCloseInit will construct and execute a MsgChannelCloseInit on the associated endpoint. +// +// NOTE: does not work with ibc-transfer module +func (endpoint *Endpoint) ChanCloseInit() error { + msg := channeltypes.NewMsgChannelCloseInit( + endpoint.ChannelConfig.PortID, endpoint.ChannelID, + endpoint.Chain.SenderAccount.GetAddress().String(), + ) + return endpoint.Chain.sendMsgs(msg) +} + +// SendPacket sends a packet through the channel keeper using the associated endpoint +// The counterparty client is updated so proofs can be sent to the counterparty chain. +func (endpoint *Endpoint) SendPacket(packet exported.PacketI) error { + channelCap := endpoint.Chain.GetChannelCapability(packet.GetSourcePort(), packet.GetSourceChannel()) + + // no need to send message, acting as a module + err := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.SendPacket(endpoint.Chain.GetContext(), channelCap, packet) + if err != nil { + return err + } + + // commit changes since no message was sent + endpoint.Chain.Coordinator.CommitBlock(endpoint.Chain) + + return endpoint.Counterparty.UpdateClient() +} + +// RecvPacket receives a packet on the associated endpoint. +// The counterparty client is updated. +func (endpoint *Endpoint) RecvPacket(packet channeltypes.Packet) error { + // get proof of packet commitment on source + packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + proof, proofHeight := endpoint.Counterparty.Chain.QueryProof(packetKey) + + recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, endpoint.Chain.SenderAccount.GetAddress().String()) + + // receive on counterparty and update source client + if err := endpoint.Chain.sendMsgs(recvMsg); err != nil { + return err + } + + return endpoint.Counterparty.UpdateClient() +} + +// WriteAcknowledgement writes an acknowledgement on the channel associated with the endpoint. +// The counterparty client is updated. +func (endpoint *Endpoint) WriteAcknowledgement(ack exported.Acknowledgement, packet exported.PacketI) error { + channelCap := endpoint.Chain.GetChannelCapability(packet.GetDestPort(), packet.GetDestChannel()) + + // no need to send message, acting as a handler + err := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.WriteAcknowledgement(endpoint.Chain.GetContext(), channelCap, packet, ack.Acknowledgement()) + if err != nil { + return err + } + + // commit changes since no message was sent + endpoint.Chain.Coordinator.CommitBlock(endpoint.Chain) + + return endpoint.Counterparty.UpdateClient() +} + +// AcknowledgePacket sends a MsgAcknowledgement to the channel associated with the endpoint. +// TODO: add a query for the acknowledgement by events +// - https://github.com/cosmos/cosmos-sdk/issues/6509 +func (endpoint *Endpoint) AcknowledgePacket(packet channeltypes.Packet, ack []byte) error { + // get proof of acknowledgement on counterparty + packetKey := host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) + proof, proofHeight := endpoint.Counterparty.QueryProof(packetKey) + + ackMsg := channeltypes.NewMsgAcknowledgement(packet, ack, proof, proofHeight, endpoint.Chain.SenderAccount.GetAddress().String()) + + return endpoint.Chain.sendMsgs(ackMsg) +} + +// SetChannelClosed sets a channel state to CLOSED. +func (endpoint *Endpoint) SetChannelClosed() error { + channel := endpoint.GetChannel() + + channel.State = channeltypes.CLOSED + endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.SetChannel(endpoint.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID, channel) + + endpoint.Chain.Coordinator.CommitBlock(endpoint.Chain) + + return endpoint.Counterparty.UpdateClient() +} + +// GetClientState retrieves the Client State for this endpoint. The +// client state is expected to exist otherwise testing will fail. +func (endpoint *Endpoint) GetClientState() exported.ClientState { + return endpoint.Chain.GetClientState(endpoint.ClientID) +} + +// GetConnection retrieves an IBC Connection for the endpoint. The +// connection is expected to exist otherwise testing will fail. +func (endpoint *Endpoint) GetConnection() connectiontypes.ConnectionEnd { + connection, found := endpoint.Chain.App.GetIBCKeeper().ConnectionKeeper.GetConnection(endpoint.Chain.GetContext(), endpoint.ConnectionID) + require.True(endpoint.Chain.t, found) + + return connection +} + +// GetChannel retrieves an IBC Channel for the endpoint. The channel +// is expected to exist otherwise testing will fail. +func (endpoint *Endpoint) GetChannel() channeltypes.Channel { + channel, found := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetChannel(endpoint.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID) + require.True(endpoint.Chain.t, found) + + return channel +} + +// QueryClientStateProof performs and abci query for a client stat associated +// with this endpoint and returns the ClientState along with the proof. +func (endpoint *Endpoint) QueryClientStateProof() (exported.ClientState, []byte) { + // retrieve client state to provide proof for + clientState := endpoint.GetClientState() + + clientKey := host.FullClientStateKey(endpoint.ClientID) + proofClient, _ := endpoint.QueryProof(clientKey) + + return clientState, proofClient +} diff --git a/testing/events.go b/testing/events.go new file mode 100644 index 00000000000..b8eb2822a4d --- /dev/null +++ b/testing/events.go @@ -0,0 +1,56 @@ +package ibctesting + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" + connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types" + channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" +) + +// ParseClientIDFromEvents parses events emitted from a MsgCreateClient and returns the +// client identifier. +func ParseClientIDFromEvents(events sdk.Events) (string, error) { + for _, ev := range events { + if ev.Type == clienttypes.EventTypeCreateClient { + for _, attr := range ev.Attributes { + if string(attr.Key) == clienttypes.AttributeKeyClientID { + return string(attr.Value), nil + } + } + } + } + return "", fmt.Errorf("client identifier event attribute not found") +} + +// ParseConnectionIDFromEvents parses events emitted from a MsgConnectionOpenInit or +// MsgConnectionOpenTry and returns the connection identifier. +func ParseConnectionIDFromEvents(events sdk.Events) (string, error) { + for _, ev := range events { + if ev.Type == connectiontypes.EventTypeConnectionOpenInit || + ev.Type == connectiontypes.EventTypeConnectionOpenTry { + for _, attr := range ev.Attributes { + if string(attr.Key) == connectiontypes.AttributeKeyConnectionID { + return string(attr.Value), nil + } + } + } + } + return "", fmt.Errorf("connection identifier event attribute not found") +} + +// ParseChannelIDFromEvents parses events emitted from a MsgChannelOpenInit or +// MsgChannelOpenTry and returns the channel identifier. +func ParseChannelIDFromEvents(events sdk.Events) (string, error) { + for _, ev := range events { + if ev.Type == channeltypes.EventTypeChannelOpenInit || ev.Type == channeltypes.EventTypeChannelOpenTry { + for _, attr := range ev.Attributes { + if string(attr.Key) == channeltypes.AttributeKeyChannelID { + return string(attr.Value), nil + } + } + } + } + return "", fmt.Errorf("channel identifier event attribute not found") +} diff --git a/testing/mock/mock.go b/testing/mock/mock.go index 1ac33f8f392..5bd1b123bec 100644 --- a/testing/mock/mock.go +++ b/testing/mock/mock.go @@ -34,6 +34,12 @@ var ( MockCanaryCapabilityName = "mock canary capability name" ) +// Expected Interface +// PortKeeper defines the expected IBC port keeper +type PortKeeper interface { + BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability +} + // AppModuleBasic is the mock AppModuleBasic. type AppModuleBasic struct{} @@ -78,12 +84,14 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { type AppModule struct { AppModuleBasic scopedKeeper capabilitykeeper.ScopedKeeper + portKeeper PortKeeper } // NewAppModule returns a mock AppModule instance. -func NewAppModule(sk capabilitykeeper.ScopedKeeper) AppModule { +func NewAppModule(sk capabilitykeeper.ScopedKeeper, pk PortKeeper) AppModule { return AppModule{ scopedKeeper: sk, + portKeeper: pk, } } @@ -110,6 +118,10 @@ func (am AppModule) RegisterServices(module.Configurator) {} // InitGenesis implements the AppModule interface. func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate { + // bind mock port ID + cap := am.portKeeper.BindPort(ctx, ModuleName) + am.scopedKeeper.ClaimCapability(ctx, cap, host.PortPath(ModuleName)) + return []abci.ValidatorUpdate{} } @@ -118,6 +130,9 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json return nil } +// ConsensusVersion implements AppModule/ConsensusVersion. +func (AppModule) ConsensusVersion() uint64 { return 1 } + // BeginBlock implements the AppModule interface func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { } diff --git a/testing/path.go b/testing/path.go new file mode 100644 index 00000000000..7b6e06731fe --- /dev/null +++ b/testing/path.go @@ -0,0 +1,75 @@ +package ibctesting + +import ( + "bytes" + "fmt" + + channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" +) + +// Path contains two endpoints representing two chains connected over IBC +type Path struct { + EndpointA *Endpoint + EndpointB *Endpoint +} + +// NewPath constructs an endpoint for each chain using the default values +// for the endpoints. Each endpoint is updated to have a pointer to the +// counterparty endpoint. +func NewPath(chainA, chainB *TestChain) *Path { + endpointA := NewDefaultEndpoint(chainA) + endpointB := NewDefaultEndpoint(chainB) + + endpointA.Counterparty = endpointB + endpointB.Counterparty = endpointA + + return &Path{ + EndpointA: endpointA, + EndpointB: endpointB, + } +} + +// SetChannelOrdered sets the channel order for both endpoints to ORDERED. +func (path *Path) SetChannelOrdered() { + path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED +} + +// RelayPacket attempts to relay the packet first on EndpointA and then on EndpointB +// if EndpointA does not contain a packet commitment for that packet. An error is returned +// if a relay step fails or the packet commitment does not exist on either endpoint. +func (path *Path) RelayPacket(packet channeltypes.Packet, ack []byte) error { + pc := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.App.AppCodec(), packet)) { + + // packet found, relay from A to B + path.EndpointB.UpdateClient() + + if err := path.EndpointB.RecvPacket(packet); err != nil { + return err + } + + if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil { + return err + } + return nil + + } + + pc = path.EndpointB.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) + if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointB.Chain.App.AppCodec(), packet)) { + + // packet found, relay B to A + path.EndpointA.UpdateClient() + + if err := path.EndpointA.RecvPacket(packet); err != nil { + return err + } + if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil { + return err + } + return nil + } + + return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet") +} diff --git a/testing/simapp/app.go b/testing/simapp/app.go index 7e4ba042226..dc7de557c41 100644 --- a/testing/simapp/app.go +++ b/testing/simapp/app.go @@ -131,6 +131,7 @@ var ( upgrade.AppModuleBasic{}, evidence.AppModuleBasic{}, transfer.AppModuleBasic{}, + ibcmock.AppModuleBasic{}, authz.AppModuleBasic{}, vesting.AppModuleBasic{}, ) @@ -328,7 +329,7 @@ func NewSimApp( // NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do // note replicate if you do not need to test core IBC or light clients. - mockModule := ibcmock.NewAppModule(scopedIBCMockKeeper) + mockModule := ibcmock.NewAppModule(scopedIBCMockKeeper, &app.IBCKeeper.PortKeeper) // Create static IBC router, add transfer route, then set and seal it ibcRouter := porttypes.NewRouter() @@ -373,6 +374,7 @@ func NewSimApp( params.NewAppModule(app.ParamsKeeper), authz.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), transferModule, + mockModule, ) // During begin block slashing happens after distr.BeginBlocker so that @@ -394,7 +396,7 @@ func NewSimApp( capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName, stakingtypes.ModuleName, slashingtypes.ModuleName, govtypes.ModuleName, minttypes.ModuleName, crisistypes.ModuleName, ibchost.ModuleName, genutiltypes.ModuleName, evidencetypes.ModuleName, authztypes.ModuleName, ibctransfertypes.ModuleName, - feegranttypes.ModuleName, + ibcmock.ModuleName, feegranttypes.ModuleName, ) app.mm.RegisterInvariants(&app.CrisisKeeper) @@ -568,6 +570,33 @@ func (app *SimApp) GetSubspace(moduleName string) paramstypes.Subspace { return subspace } +// TestingApp functions + +// GetBaseApp implements the TestingApp interface. +func (app *SimApp) GetBaseApp() *baseapp.BaseApp { + return app.BaseApp +} + +// GetStakingKeeper implements the TestingApp interface. +func (app *SimApp) GetStakingKeeper() stakingkeeper.Keeper { + return app.StakingKeeper +} + +// GetIBCKeeper implements the TestingApp interface. +func (app *SimApp) GetIBCKeeper() *ibckeeper.Keeper { + return app.IBCKeeper +} + +// GetScopedIBCKeeper implements the TestingApp interface. +func (app *SimApp) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper { + return app.ScopedIBCKeeper +} + +// GetTxConfig implements the TestingApp interface. +func (app *SimApp) GetTxConfig() client.TxConfig { + return MakeTestEncodingConfig().TxConfig +} + // SimulationManager implements the SimulationApp interface func (app *SimApp) SimulationManager() *module.SimulationManager { return app.sm diff --git a/testing/simapp/sim_test.go b/testing/simapp/sim_test.go index acac8731f78..6a297c11408 100644 --- a/testing/simapp/sim_test.go +++ b/testing/simapp/sim_test.go @@ -260,7 +260,7 @@ func TestAppSimulationAfterImport(t *testing.T) { _, _, err = simulation.SimulateFromSeed( t, os.Stdout, - newApp.BaseApp, + newApp.GetBaseApp(), AppStateFn(app.AppCodec(), app.SimulationManager()), simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 SimulationOperations(newApp, newApp.AppCodec(), config), diff --git a/testing/simapp/test_helpers.go b/testing/simapp/test_helpers.go index 69af7433e03..8d92dbdeb5b 100644 --- a/testing/simapp/test_helpers.go +++ b/testing/simapp/test_helpers.go @@ -18,8 +18,6 @@ import ( bam "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -27,7 +25,6 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/cosmos/ibc-go/testing/simapp/helpers" ) @@ -83,87 +80,6 @@ func Setup(isCheckTx bool) *SimApp { return app } -// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts -// that also act as delegators. For simplicity, each validator is bonded with a delegation -// of one consensus engine unit (10^6) in the default token of the simapp from first genesis -// account. A Nop logger is set in SimApp. -func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp { - app, genesisState := setup(true, 5) - // set genesis accounts - authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) - genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) - - validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) - delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) - - bondAmt := sdk.NewInt(1000000) - - for _, val := range valSet.Validators { - pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) - require.NoError(t, err) - pkAny, err := codectypes.NewAnyWithValue(pk) - require.NoError(t, err) - validator := stakingtypes.Validator{ - OperatorAddress: sdk.ValAddress(val.Address).String(), - ConsensusPubkey: pkAny, - Jailed: false, - Status: stakingtypes.Bonded, - Tokens: bondAmt, - DelegatorShares: sdk.OneDec(), - Description: stakingtypes.Description{}, - UnbondingHeight: int64(0), - UnbondingTime: time.Unix(0, 0).UTC(), - Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - MinSelfDelegation: sdk.ZeroInt(), - } - validators = append(validators, validator) - delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) - - } - // set validators and delegations - stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) - genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) - - totalSupply := sdk.NewCoins() - for _, b := range balances { - // add genesis acc tokens and delegated tokens to total supply - totalSupply = totalSupply.Add(b.Coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, bondAmt))...) - } - - // add bonded amount to bonded pool module account - balances = append(balances, banktypes.Balance{ - Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), - Coins: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, bondAmt)}, - }) - - // update total supply - bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{}) - genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) - - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - require.NoError(t, err) - - // init chain will set the validator set and initialize the genesis accounts - app.InitChain( - abci.RequestInitChain{ - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: DefaultConsensusParams, - AppStateBytes: stateBytes, - }, - ) - - // commit genesis changes - app.Commit() - app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{ - Height: app.LastBlockHeight() + 1, - AppHash: app.LastCommitID().Hash, - ValidatorsHash: valSet.Hash(), - NextValidatorsHash: valSet.Hash(), - }}) - - return app -} - // SetupWithGenesisAccounts initializes a new SimApp with the provided genesis // accounts and possible balances. func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *SimApp { @@ -316,11 +232,9 @@ func CheckBalance(t *testing.T, app *SimApp, addr sdk.AccAddress, balances sdk.C require.True(t, balances.IsEqual(app.BankKeeper.GetAllBalances(ctxCheck, addr))) } -// SignCheckDeliver checks a generated signed transaction and simulates a -// block commitment with the given transaction. A test assertion is made using -// the parameter 'expPass' against the result. A corresponding result is -// returned. -func SignCheckDeliver( +// SignAndDeliver signs and delivers a transaction. No simulation occurs as the +// ibc testing package causes checkState and deliverState to diverge in block time. +func SignAndDeliver( t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header tmproto.Header, msgs []sdk.Msg, chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey, ) (sdk.GasInfo, *sdk.Result, error) { @@ -336,19 +250,6 @@ func SignCheckDeliver( priv..., ) require.NoError(t, err) - txBytes, err := txCfg.TxEncoder()(tx) - require.Nil(t, err) - - // Must simulate now as CheckTx doesn't run Msgs anymore - _, res, err := app.Simulate(txBytes) - - if expSimPass { - require.NoError(t, err) - require.NotNil(t, res) - } else { - require.Error(t, err) - require.Nil(t, res) - } // Simulate a sending a transaction and committing a block app.BeginBlock(abci.RequestBeginBlock{Header: header}) diff --git a/testing/solomachine.go b/testing/solomachine.go index c418a15a4e7..ff4cc651d55 100644 --- a/testing/solomachine.go +++ b/testing/solomachine.go @@ -19,8 +19,6 @@ import ( solomachinetypes "github.com/cosmos/ibc-go/modules/light-clients/06-solomachine/types" ) -var prefix = commitmenttypes.NewMerklePrefix([]byte("ibc")) - // Solomachine is a testing helper used to simulate a counterparty // solo machine client. type Solomachine struct { diff --git a/testing/types.go b/testing/types.go deleted file mode 100644 index 9712a951d7e..00000000000 --- a/testing/types.go +++ /dev/null @@ -1,44 +0,0 @@ -package ibctesting - -import ( - channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" -) - -// TestConnection is a testing helper struct to keep track of the connectionID, source clientID, -// counterparty clientID, and the next channel version used in creating and interacting with a -// connection. -type TestConnection struct { - ID string - ClientID string - CounterpartyClientID string - NextChannelVersion string - Channels []TestChannel -} - -// FirstOrNextTestChannel returns the first test channel if it exists, otherwise it -// returns the next test channel to be created. This function is expected to be used -// when the caller does not know if the channel has or has not been created in app -// state, but would still like to refer to it to test existence or non-existence. -func (conn *TestConnection) FirstOrNextTestChannel(portID string) TestChannel { - if len(conn.Channels) > 0 { - return conn.Channels[0] - } - return TestChannel{ - PortID: portID, - ID: channeltypes.FormatChannelIdentifier(0), - ClientID: conn.ClientID, - CounterpartyClientID: conn.CounterpartyClientID, - Version: conn.NextChannelVersion, - } -} - -// TestChannel is a testing helper struct to keep track of the portID and channelID -// used in creating and interacting with a channel. The clientID and counterparty -// client ID are also tracked to cut down on querying and argument passing. -type TestChannel struct { - PortID string - ID string - ClientID string - CounterpartyClientID string - Version string -} diff --git a/testing/values.go b/testing/values.go new file mode 100644 index 00000000000..d71356ec401 --- /dev/null +++ b/testing/values.go @@ -0,0 +1,58 @@ +/* + This file contains the variables, constants, and default values + used in the testing package and commonly defined in tests. +*/ +package ibctesting + +import ( + "time" + + sdk "github.com/cosmos/cosmos-sdk/types" + + ibctransfertypes "github.com/cosmos/ibc-go/modules/apps/transfer/types" + connectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types" + commitmenttypes "github.com/cosmos/ibc-go/modules/core/23-commitment/types" + ibctmtypes "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types" + "github.com/cosmos/ibc-go/testing/mock" +) + +const ( + FirstChannelID = "channel-0" + FirstConnectionID = "connection-0" + + // Default params constants used to create a TM client + TrustingPeriod time.Duration = time.Hour * 24 * 7 * 2 + UnbondingPeriod time.Duration = time.Hour * 24 * 7 * 3 + MaxClockDrift time.Duration = time.Second * 10 + DefaultDelayPeriod uint64 = 0 + + DefaultChannelVersion = ibctransfertypes.Version + InvalidID = "IDisInvalid" + + // Application Ports + TransferPort = ibctransfertypes.ModuleName + MockPort = mock.ModuleName + + // used for testing proposals + Title = "title" + Description = "description" +) + +var ( + DefaultOpenInitVersion *connectiontypes.Version + + // Default params variables used to create a TM client + DefaultTrustLevel ibctmtypes.Fraction = ibctmtypes.DefaultTrustLevel + TestCoin = sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)) + + UpgradePath = []string{"upgrade", "upgradedIBCState"} + + ConnectionVersion = connectiontypes.ExportedVersionsToProto(connectiontypes.GetCompatibleVersions())[0] + + MockAcknowledgement = mock.MockAcknowledgement.Acknowledgement() + MockPacketData = mock.MockPacketData + MockFailPacketData = mock.MockFailPacketData + MockCanaryCapabilityName = mock.MockCanaryCapabilityName + + prefix = commitmenttypes.NewMerklePrefix([]byte("ibc")) +)