github.com/okex/exchain@v1.8.0/libs/ibc-go/modules/core/03-connection/keeper/keeper_test.go (about) 1 package keeper_test 2 3 import ( 4 "fmt" 5 types2 "github.com/okex/exchain/libs/tendermint/types" 6 "testing" 7 8 "github.com/stretchr/testify/suite" 9 10 "github.com/okex/exchain/libs/ibc-go/modules/core/03-connection/types" 11 ibctesting "github.com/okex/exchain/libs/ibc-go/testing" 12 ) 13 14 type KeeperTestSuite struct { 15 suite.Suite 16 17 coordinator *ibctesting.Coordinator 18 19 // testing chains used for convenience and readability 20 chainA ibctesting.TestChainI 21 chainB ibctesting.TestChainI 22 } 23 24 func (suite *KeeperTestSuite) SetupTest() { 25 types2.UnittestOnlySetMilestoneVenus1Height(-1) 26 suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) 27 suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) 28 suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) 29 } 30 31 func TestKeeperTestSuite(t *testing.T) { 32 suite.Run(t, new(KeeperTestSuite)) 33 } 34 35 func (suite *KeeperTestSuite) TestSetAndGetConnection() { 36 path := ibctesting.NewPath(suite.chainA, suite.chainB) 37 suite.coordinator.SetupClients(path) 38 firstConnection := "connection-0" 39 40 // check first connection does not exist 41 _, existed := suite.chainA.App().GetIBCKeeper().ConnectionKeeper.GetConnection(suite.chainA.GetContext(), firstConnection) 42 suite.Require().False(existed) 43 44 suite.coordinator.CreateConnections(path) 45 _, existed = suite.chainA.App().GetIBCKeeper().ConnectionKeeper.GetConnection(suite.chainA.GetContext(), firstConnection) 46 suite.Require().True(existed) 47 } 48 49 func (suite *KeeperTestSuite) TestSetAndGetClientConnectionPaths() { 50 51 path := ibctesting.NewPath(suite.chainA, suite.chainB) 52 suite.coordinator.SetupClients(path) 53 54 _, existed := suite.chainA.App().GetIBCKeeper().ConnectionKeeper.GetClientConnectionPaths(suite.chainA.GetContext(), path.EndpointA.ClientID) 55 suite.False(existed) 56 57 connections := []string{"connectionA", "connectionB"} 58 suite.chainA.App().GetIBCKeeper().ConnectionKeeper.SetClientConnectionPaths(suite.chainA.GetContext(), path.EndpointA.ClientID, connections) 59 paths, existed := suite.chainA.App().GetIBCKeeper().ConnectionKeeper.GetClientConnectionPaths(suite.chainA.GetContext(), path.EndpointA.ClientID) 60 suite.True(existed) 61 suite.EqualValues(connections, paths) 62 } 63 64 // create 2 connections: A0 - B0, A1 - B1 65 func (suite KeeperTestSuite) TestGetAllConnections() { 66 path1 := ibctesting.NewPath(suite.chainA, suite.chainB) 67 suite.coordinator.SetupConnections(path1) 68 69 path2 := ibctesting.NewPath(suite.chainA, suite.chainB) 70 path2.EndpointA.ClientID = path1.EndpointA.ClientID 71 path2.EndpointB.ClientID = path1.EndpointB.ClientID 72 73 suite.coordinator.CreateConnections(path2) 74 75 counterpartyB0 := types.NewCounterparty(path1.EndpointB.ClientID, path1.EndpointB.ConnectionID, suite.chainB.GetPrefix()) // connection B0 76 counterpartyB1 := types.NewCounterparty(path2.EndpointB.ClientID, path2.EndpointB.ConnectionID, suite.chainB.GetPrefix()) // connection B1 77 78 conn1 := types.NewConnectionEnd(types.OPEN, path1.EndpointA.ClientID, counterpartyB0, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) // A0 - B0 79 conn2 := types.NewConnectionEnd(types.OPEN, path2.EndpointA.ClientID, counterpartyB1, types.ExportedVersionsToProto(types.GetCompatibleVersions()), 0) // A1 - B1 80 81 iconn1 := types.NewIdentifiedConnection(path1.EndpointA.ConnectionID, conn1) 82 iconn2 := types.NewIdentifiedConnection(path2.EndpointA.ConnectionID, conn2) 83 84 expConnections := []types.IdentifiedConnection{iconn1, iconn2} 85 86 connections := suite.chainA.App().GetIBCKeeper().ConnectionKeeper.GetAllConnections(suite.chainA.GetContext()) 87 suite.Require().Len(connections, len(expConnections)) 88 suite.Require().Equal(expConnections, connections) 89 } 90 91 // the test creates 2 clients path.EndpointA.ClientID0 and path.EndpointA.ClientID1. path.EndpointA.ClientID0 has a single 92 // connection and path.EndpointA.ClientID1 has 2 connections. 93 func (suite KeeperTestSuite) TestGetAllClientConnectionPaths() { 94 path1 := ibctesting.NewPath(suite.chainA, suite.chainB) 95 path2 := ibctesting.NewPath(suite.chainA, suite.chainB) 96 suite.coordinator.SetupConnections(path1) 97 suite.coordinator.SetupConnections(path2) 98 99 path3 := ibctesting.NewPath(suite.chainA, suite.chainB) 100 path3.EndpointA.ClientID = path2.EndpointA.ClientID 101 path3.EndpointB.ClientID = path2.EndpointB.ClientID 102 suite.coordinator.CreateConnections(path3) 103 104 expPaths := []types.ConnectionPaths{ 105 types.NewConnectionPaths(path1.EndpointA.ClientID, []string{path1.EndpointA.ConnectionID}), 106 types.NewConnectionPaths(path2.EndpointA.ClientID, []string{path2.EndpointA.ConnectionID, path3.EndpointA.ConnectionID}), 107 } 108 109 connPaths := suite.chainA.App().GetIBCKeeper().ConnectionKeeper.GetAllClientConnectionPaths(suite.chainA.GetContext()) 110 suite.Require().Len(connPaths, 2) 111 suite.Require().Equal(expPaths, connPaths) 112 } 113 114 // TestGetTimestampAtHeight verifies if the clients on each chain return the 115 // correct timestamp for the other chain. 116 func (suite *KeeperTestSuite) TestGetTimestampAtHeight() { 117 var connection types.ConnectionEnd 118 119 cases := []struct { 120 msg string 121 malleate func() 122 expPass bool 123 }{ 124 {"verification success", func() { 125 path := ibctesting.NewPath(suite.chainA, suite.chainB) 126 suite.coordinator.SetupConnections(path) 127 connection = path.EndpointA.GetConnection() 128 }, true}, 129 {"consensus state not found", func() { 130 // any non-nil value of connection is valid 131 suite.Require().NotNil(connection) 132 }, false}, 133 } 134 135 for _, tc := range cases { 136 suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { 137 suite.SetupTest() // reset 138 139 tc.malleate() 140 141 actualTimestamp, err := suite.chainA.App().GetIBCKeeper().ConnectionKeeper.GetTimestampAtHeight( 142 suite.chainA.GetContext(), connection, suite.chainB.LastHeader().GetHeight(), 143 ) 144 145 if tc.expPass { 146 suite.Require().NoError(err) 147 suite.Require().EqualValues(uint64(suite.chainB.LastHeader().GetTime().UnixNano()), actualTimestamp) 148 } else { 149 suite.Require().Error(err) 150 } 151 }) 152 } 153 }