github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/core/03-connection/keeper/keeper_test.go (about)

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