From db60e658f8978ed3e867f7c7a4eac6617b5803ba Mon Sep 17 00:00:00 2001 From: colin axner <25233464+colin-axner@users.noreply.github.com> Date: Thu, 2 Jul 2020 22:08:29 +0200 Subject: [PATCH] refactor connection handshake_test (#6576) * refactor connopeninit test * updated opentry * refactor openack * update openconfirm * fix typo and test issue * fix missing code cov hits Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- x/ibc/03-connection/keeper/handshake_test.go | 447 +++++++++++-------- x/ibc/03-connection/keeper/keeper_test.go | 63 +-- x/ibc/03-connection/keeper/verify_test.go | 126 +++--- x/ibc/04-channel/keeper/keeper_test.go | 2 +- x/ibc/testing/chain.go | 29 +- x/ibc/testing/coordinator.go | 4 +- 6 files changed, 395 insertions(+), 276 deletions(-) diff --git a/x/ibc/03-connection/keeper/handshake_test.go b/x/ibc/03-connection/keeper/handshake_test.go index d141bd6e3..3e83c9580 100644 --- a/x/ibc/03-connection/keeper/handshake_test.go +++ b/x/ibc/03-connection/keeper/handshake_test.go @@ -1,137 +1,162 @@ package keeper_test import ( - "fmt" + "time" + clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types" - connectiontypes "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types" - commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" + ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" host "github.com/cosmos/cosmos-sdk/x/ibc/24-host" ) -// TestConnOpenInit - Chain A (ID #1) initializes (INIT state) a connection with -// Chain B (ID #2) which is yet UNINITIALIZED +// TestConnOpenInit - chainA initializes (INIT state) a connection with +// chainB which is yet UNINITIALIZED func (suite *KeeperTestSuite) TestConnOpenInit() { + var ( + clientA string + clientB string + ) + testCases := []struct { msg string malleate func() expPass bool }{ {"success", func() { - suite.chainA.CreateClient(suite.chainB) + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) }, true}, {"connection already exists", func() { - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.INIT) + clientA, clientB, _, _ = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, clientexported.Tendermint) + }, false}, + {"couldn't add connection to client", func() { + // swap client identifiers to result in client that does not exist + clientB, clientA = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) }, false}, - {"couldn't add connection to client", func() {}, false}, } - counterparty := connectiontypes.NewCounterparty(testClientIDB, testConnectionIDB, commitmenttypes.NewMerklePrefix(suite.chainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) - - for i, tc := range testCases { + for _, tc := range testCases { tc := tc - i := i - suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + suite.Run(tc.msg, func() { suite.SetupTest() // reset tc.malleate() - err := suite.chainA.App.IBCKeeper.ConnectionKeeper.ConnOpenInit(suite.chainA.GetContext(), testConnectionIDA, testClientIDB, counterparty) + + connA := suite.chainA.GetFirstTestConnection(clientA, clientB) + connB := suite.chainB.GetFirstTestConnection(clientB, clientA) + counterparty := types.NewCounterparty(clientB, connB.ID, suite.chainB.GetPrefix()) + + err := suite.chainA.App.IBCKeeper.ConnectionKeeper.ConnOpenInit(suite.chainA.GetContext(), connA.ID, clientA, counterparty) if tc.expPass { - suite.Require().NoError(err, "valid test case %d failed: %s", i, tc.msg) + suite.Require().NoError(err) } else { - suite.Require().Error(err, "invalid test case %d passed: %s", i, tc.msg) + suite.Require().Error(err) } }) } } -// TestConnOpenTry - Chain B (ID #2) calls ConnOpenTry to verify the state of -// connection on Chain A (ID #1) is INIT +// TestConnOpenTry - chainB calls ConnOpenTry to verify the state of +// connection on chainA is INIT func (suite *KeeperTestSuite) TestConnOpenTry() { - // counterparty for A on B - counterparty := connectiontypes.NewCounterparty( - testClientIDB, testConnectionIDA, commitmenttypes.NewMerklePrefix(suite.chainB.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes()), + var ( + clientA string + clientB string + consensusHeight uint64 ) testCases := []struct { msg string - malleate func() uint64 + malleate func() expPass bool }{ - {"success", func() uint64 { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.INIT) - suite.chainB.updateClient(suite.chainA) - suite.chainA.updateClient(suite.chainB) - suite.chainB.updateClient(suite.chainA) - suite.chainA.updateClient(suite.chainB) - return suite.chainB.Header.GetHeight() - 1 + {"success", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) }, true}, - {"consensus height > latest height", func() uint64 { - return 0 + {"consensus height > latest height", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + consensusHeight = uint64(suite.chainB.GetContext().BlockHeight()) + 1 }, false}, - {"self consensus state not found", func() uint64 { - //suite.ctx = suite.ctx.WithBlockHeight(100) - return 100 + {"self consensus state not found", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + consensusHeight = 1 }, false}, - {"connection state verification invalid", func() uint64 { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.UNINITIALIZED) - suite.chainB.updateClient(suite.chainA) - return 0 + {"connection state verification failed", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + // chainA connection not created }, false}, - {"consensus state verification invalid", func() uint64 { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.INIT) - suite.chainB.updateClient(suite.chainA) - suite.chainA.updateClient(suite.chainB) - return suite.chainB.Header.GetHeight() + {"consensus state verification failed", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + // give chainA wrong consensus state for chainB + consState, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainA.GetContext(), clientA) + 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, tmConsState.Height, tmConsState) + + _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) }, false}, - {"invalid previous connection", func() uint64 { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDA, testClientIDB, types.UNINITIALIZED) - suite.chainB.updateClient(suite.chainA) - suite.chainA.updateClient(suite.chainB) - return 0 - }, false}, - {"couldn't add connection to client", func() uint64 { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.createConnection(testConnectionIDB, testConnectionIDA, testClientIDB, testClientIDA, types.UNINITIALIZED) - suite.chainB.updateClient(suite.chainA) - suite.chainA.updateClient(suite.chainB) - return 0 + {"invalid previous connection is in TRYOPEN", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + + // open init chainA + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + // open try chainB + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) }, false}, } - for i, tc := range testCases { + for _, tc := range testCases { tc := tc - i := i - suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { - suite.SetupTest() // reset - consensusHeight := tc.malleate() + suite.Run(tc.msg, func() { + suite.SetupTest() // reset + consensusHeight = 0 // must be explicity changed in malleate - connectionKey := host.KeyConnection(testConnectionIDA) - proofInit, proofHeight := queryProof(suite.chainA, connectionKey) + tc.malleate() - consensusKey := prefixedClientKey(testClientIDB, host.KeyConsensusState(consensusHeight)) - proofConsensus, _ := queryProof(suite.chainA, consensusKey) + connA := suite.chainA.GetFirstTestConnection(clientA, clientB) + connB := suite.chainB.GetFirstTestConnection(clientB, clientA) + counterparty := types.NewCounterparty(clientA, connA.ID, suite.chainA.GetPrefix()) + + connectionKey := host.KeyConnection(connA.ID) + proofInit, proofHeight := suite.chainA.QueryProof(connectionKey) + + // retrieve consensus state to provide proof for + consState, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainA.GetContext(), clientA) + suite.Require().True(found) + + if consensusHeight == 0 { + consensusHeight = consState.GetHeight() + } + consensusKey := host.FullKeyClientPath(clientA, host.KeyConsensusState(consensusHeight)) + proofConsensus, _ := suite.chainA.QueryProof(consensusKey) err := suite.chainB.App.IBCKeeper.ConnectionKeeper.ConnOpenTry( - suite.chainB.GetContext(), testConnectionIDB, counterparty, testClientIDA, - connectiontypes.GetCompatibleVersions(), proofInit, proofConsensus, - proofHeight+1, consensusHeight, + suite.chainB.GetContext(), connB.ID, counterparty, clientB, + types.GetCompatibleVersions(), proofInit, proofConsensus, + proofHeight, consensusHeight, ) if tc.expPass { - suite.Require().NoError(err, "valid test case %d failed with consensus height %d and proof height %d: %s", i, consensusHeight, proofHeight, tc.msg) + suite.Require().NoError(err) } else { - suite.Require().Error(err, "invalid test case %d passed with consensus height %d and proof height %d: %s", i, consensusHeight, proofHeight, tc.msg) + suite.Require().Error(err) } }) } @@ -140,157 +165,221 @@ func (suite *KeeperTestSuite) TestConnOpenTry() { // TestConnOpenAck - Chain A (ID #1) calls TestConnOpenAck to acknowledge (ACK state) // the initialization (TRYINIT) of the connection on Chain B (ID #2). func (suite *KeeperTestSuite) TestConnOpenAck() { - version := connectiontypes.GetCompatibleVersions()[0] + var ( + clientA string + clientB string + consensusHeight uint64 + version string + ) testCases := []struct { msg string - version string - malleate func() uint64 + malleate func() expPass bool }{ - {"success", version, func() uint64 { - suite.chainA.CreateClient(suite.chainB) - suite.chainB.CreateClient(suite.chainA) - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDA, testClientIDB, types.TRYOPEN) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.INIT) - suite.chainB.updateClient(suite.chainA) - suite.chainA.updateClient(suite.chainB) - return suite.chainB.Header.GetHeight() + {"success", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) }, true}, - {"success from tryopen", version, func() uint64 { - suite.chainA.CreateClient(suite.chainB) - suite.chainB.CreateClient(suite.chainA) - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDA, testClientIDB, types.TRYOPEN) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.TRYOPEN) - suite.chainB.updateClient(suite.chainA) - suite.chainA.updateClient(suite.chainB) - return suite.chainB.Header.GetHeight() + {"success from tryopen", func() { + // chainA is in TRYOPEN, chainB is in TRYOPEN + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connB, connA, err := suite.coordinator.ConnOpenInit(suite.chainB, suite.chainA, clientB, clientA) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenTry(suite.chainA, suite.chainB, connA, connB) + suite.Require().NoError(err) + + // set chainB to TRYOPEN + connection := suite.chainB.GetConnection(connB) + connection.State = types.TRYOPEN + 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, clientexported.Tendermint) + + suite.coordinator.UpdateClient(suite.chainA, suite.chainB, clientA, clientexported.Tendermint) }, true}, - {"consensus height > latest height", version, func() uint64 { - return 10 + {"consensus height > latest height", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) + + consensusHeight = uint64(suite.chainA.GetContext().BlockHeight()) + 1 }, false}, - {"connection not found", version, func() uint64 { - return 2 + {"connection not found", func() { + // connections are never created + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) }, false}, - {"connection state is not INIT", version, func() uint64 { - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.UNINITIALIZED) - suite.chainB.updateClient(suite.chainA) - return suite.chainB.Header.GetHeight() + {"connection state is not INIT", func() { + // connection state is already OPEN on chainA + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenAck(suite.chainA, suite.chainB, connA, connB) + suite.Require().NoError(err) }, false}, - {"incompatible IBC versions", "2.0", func() uint64 { - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.INIT) - suite.chainB.updateClient(suite.chainA) - return suite.chainB.Header.GetHeight() + {"incompatible IBC versions", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) + + // set version to a non-compatible version + version = "2.0" }, false}, - {"self consensus state not found", version, func() uint64 { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.INIT) - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDA, testClientIDB, types.TRYOPEN) - suite.chainB.updateClient(suite.chainA) - return suite.chainB.Header.GetHeight() + {"self consensus state not found", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) + + consensusHeight = 1 }, false}, - {"connection state verification failed", version, func() uint64 { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.INIT) - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDA, testClientIDB, types.UNINITIALIZED) - suite.chainB.updateClient(suite.chainA) - return suite.chainB.Header.GetHeight() + {"connection state verification failed", func() { + // chainB connection is not in INIT + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + _, _, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) }, false}, - {"consensus state verification failed", version, func() uint64 { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.INIT) - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDA, testClientIDB, types.UNINITIALIZED) - suite.chainB.updateClient(suite.chainA) - return suite.chainB.Header.GetHeight() + {"consensus state verification failed", func() { + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + // give chainB wrong consensus state for chainA + consState, found := suite.chainB.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainB.GetContext(), clientB) + 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, tmConsState.Height, tmConsState) + + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) }, false}, } - for i, tc := range testCases { + for _, tc := range testCases { tc := tc - i := i - suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { - suite.SetupTest() // reset + suite.Run(tc.msg, func() { + suite.SetupTest() // reset + version = types.GetCompatibleVersions()[0] // must be explicitly changed in malleate + consensusHeight = 0 // must be explicitly changed in malleate - consensusHeight := tc.malleate() + tc.malleate() - connectionKey := host.KeyConnection(testConnectionIDB) - proofTry, proofHeight := queryProof(suite.chainB, connectionKey) + connA := suite.chainA.GetFirstTestConnection(clientA, clientB) + connB := suite.chainB.GetFirstTestConnection(clientB, clientA) - consensusKey := prefixedClientKey(testClientIDA, host.KeyConsensusState(consensusHeight)) - proofConsensus, _ := queryProof(suite.chainB, consensusKey) + connectionKey := host.KeyConnection(connB.ID) + proofTry, proofHeight := suite.chainB.QueryProof(connectionKey) + + // retrieve consensus state to provide proof for + consState, found := suite.chainB.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainB.GetContext(), clientB) + suite.Require().True(found) + + if consensusHeight == 0 { + consensusHeight = consState.GetHeight() + } + consensusKey := host.FullKeyClientPath(clientB, host.KeyConsensusState(consensusHeight)) + proofConsensus, _ := suite.chainB.QueryProof(consensusKey) err := suite.chainA.App.IBCKeeper.ConnectionKeeper.ConnOpenAck( - suite.chainA.GetContext(), testConnectionIDA, tc.version, proofTry, proofConsensus, - proofHeight+1, consensusHeight, + suite.chainA.GetContext(), connA.ID, version, + proofTry, proofConsensus, proofHeight, consensusHeight, ) if tc.expPass { - suite.Require().NoError(err, "valid test case %d failed with consensus height %d: %s", i, consensusHeight, tc.msg) + suite.Require().NoError(err) } else { - suite.Require().Error(err, "invalid test case %d passed with consensus height %d: %s", i, consensusHeight, tc.msg) + suite.Require().Error(err) } }) } } -// TestConnOpenConfirm - Chain B (ID #2) calls ConnOpenConfirm to confirm that -// Chain A (ID #1) state is now OPEN. +// TestConnOpenConfirm - chainB calls ConnOpenConfirm to confirm that +// chainA state is now OPEN. func (suite *KeeperTestSuite) TestConnOpenConfirm() { - testCases := []testCase{ + var ( + clientA string + clientB string + ) + testCases := []struct { + msg string + malleate func() + expPass bool + }{ {"success", func() { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.OPEN) - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDA, testClientIDB, types.TRYOPEN) - suite.chainB.updateClient(suite.chainA) + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenAck(suite.chainA, suite.chainB, connA, connB) + suite.Require().NoError(err) }, true}, - {"connection not found", func() {}, false}, + {"connection not found", func() { + // connections are never created + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + }, false}, {"chain B's connection state is not TRYOPEN", func() { - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDA, testClientIDB, types.UNINITIALIZED) - suite.chainA.createConnection(testConnectionIDB, testConnectionIDA, testClientIDB, testClientIDA, types.OPEN) - suite.chainA.updateClient(suite.chainB) + // connections are OPEN + clientA, clientB, _, _ = suite.coordinator.SetupClientConnections(suite.chainA, suite.chainB, clientexported.Tendermint) }, false}, {"connection state verification failed", func() { - suite.chainB.CreateClient(suite.chainA) - suite.chainA.CreateClient(suite.chainB) - suite.chainB.updateClient(suite.chainA) - suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.INIT) - suite.chainB.createConnection(testConnectionIDB, testConnectionIDA, testClientIDB, testClientIDA, types.TRYOPEN) - suite.chainA.updateClient(suite.chainA) + // chainA is in INIT + clientA, clientB = suite.coordinator.SetupClients(suite.chainA, suite.chainB, clientexported.Tendermint) + connA, connB, err := suite.coordinator.ConnOpenInit(suite.chainA, suite.chainB, clientA, clientB) + suite.Require().NoError(err) + + err = suite.coordinator.ConnOpenTry(suite.chainB, suite.chainA, connB, connA) + suite.Require().NoError(err) }, false}, } - for i, tc := range testCases { + for _, tc := range testCases { tc := tc - i := i - suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { + + suite.Run(tc.msg, func() { suite.SetupTest() // reset tc.malleate() - connectionKey := host.KeyConnection(testConnectionIDA) - proofAck, proofHeight := queryProof(suite.chainA, connectionKey) + connA := suite.chainA.GetFirstTestConnection(clientA, clientB) + connB := suite.chainA.GetFirstTestConnection(clientB, clientA) + + connectionKey := host.KeyConnection(connA.ID) + proofAck, proofHeight := suite.chainA.QueryProof(connectionKey) + + err := suite.chainB.App.IBCKeeper.ConnectionKeeper.ConnOpenConfirm( + suite.chainB.GetContext(), connB.ID, proofAck, proofHeight, + ) if tc.expPass { - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.ConnOpenConfirm( - suite.chainB.GetContext(), testConnectionIDB, proofAck, proofHeight+1, - ) - suite.Require().NoError(err, "valid test case %d failed: %s", i, tc.msg) + suite.Require().NoError(err) } else { - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.ConnOpenConfirm( - suite.chainB.GetContext(), testConnectionIDB, proofAck, proofHeight+1, - ) - suite.Require().Error(err, "invalid test case %d passed: %s", i, tc.msg) + suite.Require().Error(err) } }) } } - -type testCase = struct { - msg string - malleate func() - expPass bool -} diff --git a/x/ibc/03-connection/keeper/keeper_test.go b/x/ibc/03-connection/keeper/keeper_test.go index 63adc41b5..de7b01012 100644 --- a/x/ibc/03-connection/keeper/keeper_test.go +++ b/x/ibc/03-connection/keeper/keeper_test.go @@ -20,6 +20,7 @@ import ( ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" host "github.com/cosmos/cosmos-sdk/x/ibc/24-host" + ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -50,20 +51,32 @@ var ( type KeeperTestSuite struct { suite.Suite + coordinator *ibctesting.Coordinator + + // testing chains used for convenience and readability + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain + + // TODO: delete cdc *codec.Codec // ChainA testing fields - chainA *TestChain + oldchainA *TestChain // ChainB testing fields - chainB *TestChain + oldchainB *TestChain } func (suite *KeeperTestSuite) SetupTest() { - suite.chainA = NewTestChain(testClientIDA) - suite.chainB = NewTestChain(testClientIDB) + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) - suite.cdc = suite.chainA.App.Codec() + // TODO: delete + suite.oldchainA = NewTestChain(testClientIDA) + suite.oldchainB = NewTestChain(testClientIDB) + + suite.cdc = suite.oldchainA.App.Codec() } // nolint: unused @@ -88,32 +101,32 @@ func TestKeeperTestSuite(t *testing.T) { } func (suite *KeeperTestSuite) TestSetAndGetConnection() { - _, existed := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetConnection(suite.chainA.GetContext(), testConnectionIDA) + _, existed := suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetConnection(suite.oldchainA.GetContext(), testConnectionIDA) suite.Require().False(existed) - counterparty := types.NewCounterparty(testClientIDA, testConnectionIDA, commitmenttypes.NewMerklePrefix(suite.chainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) + counterparty := types.NewCounterparty(testClientIDA, testConnectionIDA, commitmenttypes.NewMerklePrefix(suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) expConn := types.NewConnectionEnd(types.INIT, testConnectionIDB, testClientIDB, counterparty, types.GetCompatibleVersions()) - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainA.GetContext(), testConnectionIDA, expConn) - conn, existed := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetConnection(suite.chainA.GetContext(), testConnectionIDA) + suite.oldchainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.oldchainA.GetContext(), testConnectionIDA, expConn) + conn, existed := suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetConnection(suite.oldchainA.GetContext(), testConnectionIDA) suite.Require().True(existed) suite.Require().EqualValues(expConn, conn) } func (suite *KeeperTestSuite) TestSetAndGetClientConnectionPaths() { - _, existed := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetClientConnectionPaths(suite.chainA.GetContext(), testClientIDA) + _, existed := suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetClientConnectionPaths(suite.oldchainA.GetContext(), testClientIDA) suite.False(existed) - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetClientConnectionPaths(suite.chainA.GetContext(), testClientIDB, types.GetCompatibleVersions()) - paths, existed := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetClientConnectionPaths(suite.chainA.GetContext(), testClientIDB) + suite.oldchainA.App.IBCKeeper.ConnectionKeeper.SetClientConnectionPaths(suite.oldchainA.GetContext(), testClientIDB, types.GetCompatibleVersions()) + paths, existed := suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetClientConnectionPaths(suite.oldchainA.GetContext(), testClientIDB) suite.True(existed) suite.EqualValues(types.GetCompatibleVersions(), paths) } func (suite KeeperTestSuite) TestGetAllConnections() { // Connection (Counterparty): A(C) -> C(B) -> B(A) - counterparty1 := types.NewCounterparty(testClientIDA, testConnectionIDA, commitmenttypes.NewMerklePrefix(suite.chainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) - counterparty2 := types.NewCounterparty(testClientIDB, testConnectionIDB, commitmenttypes.NewMerklePrefix(suite.chainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) - counterparty3 := types.NewCounterparty(testClientID3, testConnectionID3, commitmenttypes.NewMerklePrefix(suite.chainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) + counterparty1 := types.NewCounterparty(testClientIDA, testConnectionIDA, commitmenttypes.NewMerklePrefix(suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) + counterparty2 := types.NewCounterparty(testClientIDB, testConnectionIDB, commitmenttypes.NewMerklePrefix(suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) + counterparty3 := types.NewCounterparty(testClientID3, testConnectionID3, commitmenttypes.NewMerklePrefix(suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes())) conn1 := types.NewConnectionEnd(types.INIT, testConnectionIDA, testClientIDA, counterparty3, types.GetCompatibleVersions()) conn2 := types.NewConnectionEnd(types.INIT, testConnectionIDB, testClientIDB, counterparty1, types.GetCompatibleVersions()) @@ -122,10 +135,10 @@ func (suite KeeperTestSuite) TestGetAllConnections() { expConnections := []types.ConnectionEnd{conn1, conn2, conn3} for i := range expConnections { - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.chainA.GetContext(), expConnections[i].ID, expConnections[i]) + suite.oldchainA.App.IBCKeeper.ConnectionKeeper.SetConnection(suite.oldchainA.GetContext(), expConnections[i].ID, expConnections[i]) } - connections := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetAllConnections(suite.chainA.GetContext()) + connections := suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetAllConnections(suite.oldchainA.GetContext()) suite.Require().Len(connections, len(expConnections)) suite.Require().Equal(expConnections, connections) } @@ -138,7 +151,7 @@ func (suite KeeperTestSuite) TestGetAllClientConnectionPaths() { } for i := range clients { - suite.chainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.chainA.GetContext(), clients[i]) + suite.oldchainA.App.IBCKeeper.ClientKeeper.SetClientState(suite.oldchainA.GetContext(), clients[i]) } expPaths := []types.ConnectionPaths{ @@ -147,10 +160,10 @@ func (suite KeeperTestSuite) TestGetAllClientConnectionPaths() { } for i := range expPaths { - suite.chainA.App.IBCKeeper.ConnectionKeeper.SetClientConnectionPaths(suite.chainA.GetContext(), expPaths[i].ClientID, expPaths[i].Paths) + suite.oldchainA.App.IBCKeeper.ConnectionKeeper.SetClientConnectionPaths(suite.oldchainA.GetContext(), expPaths[i].ClientID, expPaths[i].Paths) } - connPaths := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetAllClientConnectionPaths(suite.chainA.GetContext()) + connPaths := suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetAllClientConnectionPaths(suite.oldchainA.GetContext()) suite.Require().Len(connPaths, 2) suite.Require().Equal(connPaths, expPaths) } @@ -164,7 +177,7 @@ func (suite *KeeperTestSuite) TestGetTimestampAtHeight() { expPass bool }{ {"verification success", func() { - suite.chainA.CreateClient(suite.chainB) + suite.oldchainA.CreateClient(suite.oldchainB) }, true}, {"client state not found", func() {}, false}, } @@ -175,15 +188,15 @@ func (suite *KeeperTestSuite) TestGetTimestampAtHeight() { tc.malleate() // create and store a connection to chainB on chainA - connection := suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.OPEN) + connection := suite.oldchainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.OPEN) - actualTimestamp, err := suite.chainA.App.IBCKeeper.ConnectionKeeper.GetTimestampAtHeight( - suite.chainA.GetContext(), connection, uint64(suite.chainB.Header.Height), + actualTimestamp, err := suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetTimestampAtHeight( + suite.oldchainA.GetContext(), connection, uint64(suite.oldchainB.Header.Height), ) if tc.expPass { suite.Require().NoError(err, "valid test case %d failed: %s", i, tc.msg) - suite.Require().EqualValues(uint64(suite.chainB.Header.Time.UnixNano()), actualTimestamp) + suite.Require().EqualValues(uint64(suite.oldchainB.Header.Time.UnixNano()), actualTimestamp) } else { suite.Require().Error(err, "invalid test case %d passed: %s", i, tc.msg) } diff --git a/x/ibc/03-connection/keeper/verify_test.go b/x/ibc/03-connection/keeper/verify_test.go index 8cc06d1de..d7b6da145 100644 --- a/x/ibc/03-connection/keeper/verify_test.go +++ b/x/ibc/03-connection/keeper/verify_test.go @@ -22,7 +22,7 @@ func (suite *KeeperTestSuite) TestVerifyClientConsensusState() { // create connection on chainA to chainB counterparty := types.NewCounterparty( testClientIDA, testConnectionIDA, - commitmenttypes.NewMerklePrefix(suite.chainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes()), + commitmenttypes.NewMerklePrefix(suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes()), ) connection1 := types.NewConnectionEnd( types.UNINITIALIZED, testConnectionIDB, testClientIDB, counterparty, @@ -36,17 +36,17 @@ func (suite *KeeperTestSuite) TestVerifyClientConsensusState() { expPass bool }{ {"verification success", connection1, func() clientexported.ConsensusState { - suite.chainA.CreateClient(suite.chainB) - suite.chainB.CreateClient(suite.chainA) - consState := suite.chainA.Header.ConsensusState() + suite.oldchainA.CreateClient(suite.oldchainB) + suite.oldchainB.CreateClient(suite.oldchainA) + consState := suite.oldchainA.Header.ConsensusState() return consState }, true}, {"client state not found", connection1, func() clientexported.ConsensusState { - return suite.chainB.Header.ConsensusState() + return suite.oldchainB.Header.ConsensusState() }, false}, {"verification failed", connection1, func() clientexported.ConsensusState { - suite.chainA.CreateClient(suite.chainA) - return suite.chainA.Header.ConsensusState() + suite.oldchainA.CreateClient(suite.oldchainA) + return suite.oldchainA.Header.ConsensusState() }, false}, } @@ -61,18 +61,18 @@ func (suite *KeeperTestSuite) TestVerifyClientConsensusState() { consState := tc.malleate() // perform a couple updates of chain B on chain A - suite.chainA.updateClient(suite.chainB) - suite.chainA.updateClient(suite.chainB) + suite.oldchainA.updateClient(suite.oldchainB) + suite.oldchainA.updateClient(suite.oldchainB) // TODO: is this the right consensus height - consensusHeight := suite.chainA.Header.GetHeight() + consensusHeight := suite.oldchainA.Header.GetHeight() consensusKey := prefixedClientKey(testClientIDA, host.KeyConsensusState(consensusHeight)) // get proof that chainB stored chainA' consensus state - proof, proofHeight := queryProof(suite.chainB, consensusKey) + proof, proofHeight := queryProof(suite.oldchainB, consensusKey) - err := suite.chainA.App.IBCKeeper.ConnectionKeeper.VerifyClientConsensusState( - suite.chainA.GetContext(), tc.connection, proofHeight+1, consensusHeight, proof, consState, + err := suite.oldchainA.App.IBCKeeper.ConnectionKeeper.VerifyClientConsensusState( + suite.oldchainA.GetContext(), tc.connection, proofHeight+1, consensusHeight, proof, consState, ) if tc.expPass { @@ -93,14 +93,14 @@ func (suite *KeeperTestSuite) TestVerifyConnectionState() { expPass bool }{ {"verification success", func() { - suite.chainA.CreateClient(suite.chainB) - suite.chainB.CreateClient(suite.chainA) + suite.oldchainA.CreateClient(suite.oldchainB) + suite.oldchainB.CreateClient(suite.oldchainA) invalidProofHeight = 0 // don't use this }, true}, {"client state not found", func() {}, false}, {"verification failed", func() { - suite.chainA.CreateClient(suite.chainB) - suite.chainB.CreateClient(suite.chainA) + suite.oldchainA.CreateClient(suite.oldchainB) + suite.oldchainB.CreateClient(suite.oldchainA) invalidProofHeight = 10 // make proofHeight incorrect }, false}, } @@ -117,16 +117,16 @@ func (suite *KeeperTestSuite) TestVerifyConnectionState() { tc.malleate() // create and store connection on chain A - expectedConnection := suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.OPEN) + expectedConnection := suite.oldchainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDB, testClientIDA, types.OPEN) // // create expected connection // TODO: why is this commented // expectedConnection := types.NewConnectionEnd(types.INIT, testClientIDB, counterparty, []string{"1.0.0"}) // perform a couple updates of chain A on chain B - suite.chainB.updateClient(suite.chainA) - suite.chainB.updateClient(suite.chainA) - proof, proofHeight := queryProof(suite.chainA, connectionKey) + suite.oldchainB.updateClient(suite.oldchainA) + suite.oldchainB.updateClient(suite.oldchainA) + proof, proofHeight := queryProof(suite.oldchainA, connectionKey) // if invalidProofHeight has been set, use that value instead if invalidProofHeight != 0 { proofHeight = invalidProofHeight @@ -136,8 +136,8 @@ func (suite *KeeperTestSuite) TestVerifyConnectionState() { counterparty := types.NewCounterparty(testClientIDB, testConnectionIDB, commitmenttypes.NewMerklePrefix([]byte("ibc"))) connection := types.NewConnectionEnd(types.UNINITIALIZED, testConnectionIDA, testClientIDA, counterparty, []string{"1.0.0"}) // Ensure chain B can verify connection exists in chain A - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.VerifyConnectionState( - suite.chainB.GetContext(), connection, proofHeight+1, proof, testConnectionIDA, expectedConnection, + err := suite.oldchainB.App.IBCKeeper.ConnectionKeeper.VerifyConnectionState( + suite.oldchainB.GetContext(), connection, proofHeight+1, proof, testConnectionIDA, expectedConnection, ) if tc.expPass { @@ -155,7 +155,7 @@ func (suite *KeeperTestSuite) TestVerifyChannelState() { // create connection of chainB to pass into verify function counterparty := types.NewCounterparty( testClientIDB, testConnectionIDB, - commitmenttypes.NewMerklePrefix(suite.chainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes()), + commitmenttypes.NewMerklePrefix(suite.oldchainA.App.IBCKeeper.ConnectionKeeper.GetCommitmentPrefix().Bytes()), ) connection := types.NewConnectionEnd( @@ -170,14 +170,14 @@ func (suite *KeeperTestSuite) TestVerifyChannelState() { expPass bool }{ {"verification success", 0, func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, true}, {"client state not found", 0, func() {}, false}, {"consensus state not found", 100, func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, false}, {"verification failed", 7, func() { - suite.chainB.CreateClient(suite.chainB) + suite.oldchainB.CreateClient(suite.oldchainB) }, false}, } @@ -191,23 +191,23 @@ func (suite *KeeperTestSuite) TestVerifyChannelState() { tc.malleate() // Create and store channel on chain A - channel := suite.chainA.createChannel( + channel := suite.oldchainA.createChannel( testPort1, testChannel1, testPort2, testChannel2, channeltypes.OPEN, channeltypes.ORDERED, testConnectionIDA, ) // Update chainA client on chainB - suite.chainB.updateClient(suite.chainA) + suite.oldchainB.updateClient(suite.oldchainA) // Check that Chain B can verify channel is stored on chainA - proof, proofHeight := queryProof(suite.chainA, channelKey) + proof, proofHeight := queryProof(suite.oldchainA, channelKey) // if testcase proofHeight is not 0, replace proofHeight with this value if tc.proofHeight != 0 { proofHeight = tc.proofHeight } - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.VerifyChannelState( - suite.chainB.GetContext(), connection, proofHeight+1, proof, testPort1, + err := suite.oldchainB.App.IBCKeeper.ConnectionKeeper.VerifyChannelState( + suite.oldchainB.GetContext(), connection, proofHeight+1, proof, testPort1, testChannel1, channel, ) @@ -231,11 +231,11 @@ func (suite *KeeperTestSuite) TestVerifyPacketCommitment() { expPass bool }{ {"verification success", 0, func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, true}, {"client state not found", 0, func() {}, false}, {"consensus state not found", 100, func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, false}, } @@ -250,21 +250,21 @@ func (suite *KeeperTestSuite) TestVerifyPacketCommitment() { tc.malleate() // Set PacketCommitment on chainA - connection := suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.OPEN) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.chainA.GetContext(), testPort1, testChannel1, 1, commitmentBz) + connection := suite.oldchainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.OPEN) + suite.oldchainA.App.IBCKeeper.ChannelKeeper.SetPacketCommitment(suite.oldchainA.GetContext(), testPort1, testChannel1, 1, commitmentBz) // Update ChainA client on chainB - suite.chainB.updateClient(suite.chainA) + suite.oldchainB.updateClient(suite.oldchainA) // Check that ChainB can verify PacketCommitment stored in chainA - proof, proofHeight := queryProof(suite.chainA, commitmentKey) + proof, proofHeight := queryProof(suite.oldchainA, commitmentKey) // if testcase proofHeight is not 0, replace proofHeight with this value if tc.proofHeight != 0 { proofHeight = tc.proofHeight } - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.VerifyPacketCommitment( - suite.chainB.GetContext(), connection, proofHeight+1, proof, testPort1, + err := suite.oldchainB.App.IBCKeeper.ConnectionKeeper.VerifyPacketCommitment( + suite.oldchainB.GetContext(), connection, proofHeight+1, proof, testPort1, testChannel1, 1, commitmentBz, ) @@ -288,11 +288,11 @@ func (suite *KeeperTestSuite) TestVerifyPacketAcknowledgement() { expPass bool }{ {"verification success", 0, func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, true}, {"client state not found", 0, func() {}, false}, {"consensus state not found", 100, func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, false}, } @@ -303,19 +303,19 @@ func (suite *KeeperTestSuite) TestVerifyPacketAcknowledgement() { suite.SetupTest() // reset tc.malleate() - connection := suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.OPEN) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(suite.chainA.GetContext(), testPort1, testChannel1, 1, channeltypes.CommitAcknowledgement(ack)) - suite.chainB.updateClient(suite.chainA) + connection := suite.oldchainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.OPEN) + suite.oldchainA.App.IBCKeeper.ChannelKeeper.SetPacketAcknowledgement(suite.oldchainA.GetContext(), testPort1, testChannel1, 1, channeltypes.CommitAcknowledgement(ack)) + suite.oldchainB.updateClient(suite.oldchainA) // TODO check this proof height - proof, proofHeight := queryProof(suite.chainA, packetAckKey) + proof, proofHeight := queryProof(suite.oldchainA, packetAckKey) // if testcase proofHeight is not 0, replace proofHeight with this value if tc.proofHeight != 0 { proofHeight = tc.proofHeight } - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.VerifyPacketAcknowledgement( - suite.chainB.GetContext(), connection, proofHeight+1, proof, testPort1, + err := suite.oldchainB.App.IBCKeeper.ConnectionKeeper.VerifyPacketAcknowledgement( + suite.oldchainB.GetContext(), connection, proofHeight+1, proof, testPort1, testChannel1, 1, ack, ) @@ -338,11 +338,11 @@ func (suite *KeeperTestSuite) TestVerifyPacketAcknowledgementAbsence() { expPass bool }{ {"verification success", 0, func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, true}, {"client state not found", 0, func() {}, false}, {"consensus state not found", 100, func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, false}, } @@ -353,17 +353,17 @@ func (suite *KeeperTestSuite) TestVerifyPacketAcknowledgementAbsence() { suite.SetupTest() // reset tc.malleate() - connection := suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.OPEN) - suite.chainB.updateClient(suite.chainA) + connection := suite.oldchainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.OPEN) + suite.oldchainB.updateClient(suite.oldchainA) - proof, proofHeight := queryProof(suite.chainA, packetAckKey) + proof, proofHeight := queryProof(suite.oldchainA, packetAckKey) // if testcase proofHeight is not 0, replace proofHeight with this value if tc.proofHeight != 0 { proofHeight = tc.proofHeight } - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.VerifyPacketAcknowledgementAbsence( - suite.chainB.GetContext(), connection, proofHeight+1, proof, testPort1, + err := suite.oldchainB.App.IBCKeeper.ConnectionKeeper.VerifyPacketAcknowledgementAbsence( + suite.oldchainB.GetContext(), connection, proofHeight+1, proof, testPort1, testChannel1, 1, ) @@ -386,11 +386,11 @@ func (suite *KeeperTestSuite) TestVerifyNextSequenceRecv() { expPass bool }{ {"verification success", uint64(0), func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, true}, {"client state not found", uint64(0), func() {}, false}, {"consensus state not found", uint64(100), func() { - suite.chainB.CreateClient(suite.chainA) + suite.oldchainB.CreateClient(suite.oldchainA) }, false}, } @@ -401,18 +401,18 @@ func (suite *KeeperTestSuite) TestVerifyNextSequenceRecv() { suite.SetupTest() // reset tc.malleate() - connection := suite.chainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.OPEN) - suite.chainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(suite.chainA.GetContext(), testPort1, testChannel1, 1) - suite.chainB.updateClient(suite.chainA) + connection := suite.oldchainA.createConnection(testConnectionIDA, testConnectionIDB, testClientIDA, testClientIDB, types.OPEN) + suite.oldchainA.App.IBCKeeper.ChannelKeeper.SetNextSequenceRecv(suite.oldchainA.GetContext(), testPort1, testChannel1, 1) + suite.oldchainB.updateClient(suite.oldchainA) - proof, proofHeight := queryProof(suite.chainA, nextSeqRcvKey) + proof, proofHeight := queryProof(suite.oldchainA, nextSeqRcvKey) // if testcase proofHeight is not 0, replace proofHeight with this value if tc.proofHeight != 0 { proofHeight = tc.proofHeight } - err := suite.chainB.App.IBCKeeper.ConnectionKeeper.VerifyNextSequenceRecv( - suite.chainB.GetContext(), connection, proofHeight+1, proof, testPort1, + err := suite.oldchainB.App.IBCKeeper.ConnectionKeeper.VerifyNextSequenceRecv( + suite.oldchainB.GetContext(), connection, proofHeight+1, proof, testPort1, testChannel1, 1, ) diff --git a/x/ibc/04-channel/keeper/keeper_test.go b/x/ibc/04-channel/keeper/keeper_test.go index dc3e03b16..529bbbb64 100644 --- a/x/ibc/04-channel/keeper/keeper_test.go +++ b/x/ibc/04-channel/keeper/keeper_test.go @@ -16,7 +16,7 @@ type KeeperTestSuite struct { coordinator *ibctesting.Coordinator - // testing chains used for convience and readability + // testing chains used for convenience and readability chainA *ibctesting.TestChain chainB *ibctesting.TestChain } diff --git a/x/ibc/testing/chain.go b/x/ibc/testing/chain.go index 99acf6bbd..b352ae520 100644 --- a/x/ibc/testing/chain.go +++ b/x/ibc/testing/chain.go @@ -255,19 +255,36 @@ func (chain *TestChain) NewClientID(counterpartyChainID string) string { return clientID } -// NewConnection appends a new TestConnection which contains references to the connection id, -// client id and counterparty client id. The connection id format: +// 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: // connectionid -func (chain *TestChain) NewTestConnection(clientID, counterpartyClientID string) *TestConnection { +func (chain *TestChain) ConstructNextTestConnection(clientID, counterpartyClientID string) *TestConnection { connectionID := ConnectionIDPrefix + strconv.Itoa(len(chain.Connections)) - conn := &TestConnection{ + return &TestConnection{ ID: connectionID, ClientID: clientID, CounterpartyClientID: counterpartyClientID, } +} - chain.Connections = append(chain.Connections, conn) - return conn +// FirstTestConnection 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) } // CreateTMClient will construct and execute a 07-tendermint MsgCreateClient. A counterparty diff --git a/x/ibc/testing/coordinator.go b/x/ibc/testing/coordinator.go index cf1adcf13..d2df26287 100644 --- a/x/ibc/testing/coordinator.go +++ b/x/ibc/testing/coordinator.go @@ -286,8 +286,8 @@ func (coord *Coordinator) ConnOpenInit( source, counterparty *TestChain, clientID, counterpartyClientID string, ) (*TestConnection, *TestConnection, error) { - sourceConnection := source.NewTestConnection(clientID, counterpartyClientID) - counterpartyConnection := counterparty.NewTestConnection(counterpartyClientID, clientID) + sourceConnection := source.AddTestConnection(clientID, counterpartyClientID) + counterpartyConnection := counterparty.AddTestConnection(counterpartyClientID, clientID) // initialize connection on source if err := source.ConnectionOpenInit(counterparty, sourceConnection, counterpartyConnection); err != nil {