github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/apps/27-interchain-accounts/controller/keeper/relay_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     5  	banktypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/bank"
     6  	capabilitytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/types"
     7  	icatypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/27-interchain-accounts/types"
     8  	clienttypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/02-client/types"
     9  	channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
    10  	host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host"
    11  	ibctesting "github.com/fibonacci-chain/fbc/libs/ibc-go/testing"
    12  )
    13  
    14  // TODO,再加
    15  func (suite *KeeperTestSuite) TestSendTx() {
    16  	var (
    17  		path             *ibctesting.Path
    18  		packetData       icatypes.InterchainAccountPacketData
    19  		chanCap          *capabilitytypes.Capability
    20  		timeoutTimestamp uint64
    21  	)
    22  
    23  	testCases := []struct {
    24  		msg      string
    25  		malleate func()
    26  		expPass  bool
    27  	}{
    28  		{
    29  			"success",
    30  			func() {
    31  				interchainAccountAddr, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID)
    32  				suite.Require().True(found)
    33  
    34  				msg := &banktypes.MsgSendAdapter{
    35  					FromAddress: interchainAccountAddr,
    36  					ToAddress:   suite.chainB.SenderAccount().GetAddress().String(),
    37  					Amount:      sdk.CoinAdapters{sdk.NewCoinAdapter(sdk.DefaultBondDenom, sdk.NewInt(100))},
    38  				}
    39  
    40  				data, err := icatypes.SerializeCosmosTx(suite.chainB.GetSimApp().AppCodec(), []sdk.MsgAdapter{msg})
    41  				suite.Require().NoError(err)
    42  
    43  				packetData = icatypes.InterchainAccountPacketData{
    44  					Type: icatypes.EXECUTE_TX,
    45  					Data: data,
    46  				}
    47  			},
    48  			true,
    49  		},
    50  		{
    51  			"success with multiple sdk.Msg",
    52  			func() {
    53  				interchainAccountAddr, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID)
    54  				suite.Require().True(found)
    55  
    56  				msgsBankSend := []sdk.MsgAdapter{
    57  					&banktypes.MsgSendAdapter{
    58  						FromAddress: interchainAccountAddr,
    59  						ToAddress:   suite.chainB.SenderAccount().GetAddress().String(),
    60  						Amount:      sdk.CoinAdapters{sdk.NewCoinAdapter(sdk.DefaultBondDenom, sdk.NewInt(100))},
    61  					},
    62  					&banktypes.MsgSendAdapter{
    63  						FromAddress: interchainAccountAddr,
    64  						ToAddress:   suite.chainB.SenderAccount().GetAddress().String(),
    65  						Amount:      sdk.CoinAdapters{sdk.NewCoinAdapter(sdk.DefaultBondDenom, sdk.NewInt(100))},
    66  					},
    67  				}
    68  
    69  				data, err := icatypes.SerializeCosmosTx(suite.chainB.GetSimApp().AppCodec(), msgsBankSend)
    70  				suite.Require().NoError(err)
    71  
    72  				packetData = icatypes.InterchainAccountPacketData{
    73  					Type: icatypes.EXECUTE_TX,
    74  					Data: data,
    75  				}
    76  			},
    77  			true,
    78  		},
    79  		{
    80  			"data is nil",
    81  			func() {
    82  				packetData = icatypes.InterchainAccountPacketData{
    83  					Type: icatypes.EXECUTE_TX,
    84  					Data: nil,
    85  				}
    86  			},
    87  			false,
    88  		},
    89  		{
    90  			"active channel not found",
    91  			func() {
    92  				path.EndpointA.ChannelConfig.PortID = "invalid-port-id"
    93  			},
    94  			false,
    95  		},
    96  		{
    97  			"channel does not exist",
    98  			func() {
    99  				suite.chainA.GetSimApp().ICAControllerKeeper.SetActiveChannelID(suite.chainA.GetContext(), ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID, "channel-100")
   100  			},
   101  			false,
   102  		},
   103  		{
   104  			"channel in INIT state - optimistic packet sends fail",
   105  			func() {
   106  				channel, found := suite.chainA.GetSimApp().IBCKeeper.V2Keeper.ChannelKeeper.GetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   107  				suite.Require().True(found)
   108  
   109  				channel.State = channeltypes.INIT
   110  				suite.chainA.GetSimApp().IBCKeeper.V2Keeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel)
   111  			},
   112  			false,
   113  		},
   114  		{
   115  			"sendPacket fails - channel closed",
   116  			func() {
   117  				err := path.EndpointA.SetChannelClosed()
   118  				suite.Require().NoError(err)
   119  			},
   120  			false,
   121  		},
   122  		{
   123  			"invalid channel capability provided",
   124  			func() {
   125  				chanCap = nil
   126  			},
   127  			false,
   128  		},
   129  		{
   130  			"timeout timestamp is not in the future",
   131  			func() {
   132  				interchainAccountAddr, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID)
   133  				suite.Require().True(found)
   134  
   135  				msg := &banktypes.MsgSendAdapter{
   136  					FromAddress: interchainAccountAddr,
   137  					ToAddress:   suite.chainB.SenderAccount().GetAddress().String(),
   138  					Amount:      sdk.CoinAdapters{sdk.NewCoinAdapter(sdk.DefaultBondDenom, sdk.NewInt(100))},
   139  				}
   140  
   141  				data, err := icatypes.SerializeCosmosTx(suite.chainB.GetSimApp().AppCodec(), []sdk.MsgAdapter{msg})
   142  				suite.Require().NoError(err)
   143  
   144  				packetData = icatypes.InterchainAccountPacketData{
   145  					Type: icatypes.EXECUTE_TX,
   146  					Data: data,
   147  				}
   148  
   149  				ctx := suite.chainA.GetContext()
   150  				v := &ctx
   151  				timeoutTimestamp = uint64(v.BlockTime().UnixNano())
   152  			},
   153  			false,
   154  		},
   155  	}
   156  
   157  	for _, tc := range testCases {
   158  		tc := tc
   159  
   160  		suite.Run(tc.msg, func() {
   161  			suite.SetupTest()             // reset
   162  			timeoutTimestamp = ^uint64(0) // default
   163  
   164  			path = NewICAPath(suite.chainA, suite.chainB)
   165  			suite.coordinator.SetupConnections(path)
   166  
   167  			err := SetupICAPath(path, TestOwnerAddress)
   168  			suite.Require().NoError(err)
   169  
   170  			var ok bool
   171  			chanCap, ok = suite.chainA.GetSimApp().ScopedICAMockKeeper.GetCapability(path.EndpointA.Chain.GetContext(), host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID))
   172  			suite.Require().True(ok)
   173  
   174  			tc.malleate() // malleate mutates test data
   175  
   176  			_, err = suite.chainA.GetSimApp().ICAControllerKeeper.SendTx(suite.chainA.GetContext(), chanCap, ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID, packetData, timeoutTimestamp)
   177  
   178  			if tc.expPass {
   179  				suite.Require().NoError(err)
   180  			} else {
   181  				suite.Require().Error(err)
   182  			}
   183  		})
   184  	}
   185  }
   186  
   187  func (suite *KeeperTestSuite) TestOnTimeoutPacket() {
   188  	var path *ibctesting.Path
   189  
   190  	testCases := []struct {
   191  		msg      string
   192  		malleate func()
   193  		expPass  bool
   194  	}{
   195  		{
   196  			"success",
   197  			func() {},
   198  			true,
   199  		},
   200  	}
   201  
   202  	for _, tc := range testCases {
   203  		suite.Run(tc.msg, func() {
   204  			suite.SetupTest() // reset
   205  
   206  			path = NewICAPath(suite.chainA, suite.chainB)
   207  			suite.coordinator.SetupConnections(path)
   208  
   209  			err := SetupICAPath(path, TestOwnerAddress)
   210  			suite.Require().NoError(err)
   211  
   212  			tc.malleate() // malleate mutates test data
   213  
   214  			packet := channeltypes.NewPacket(
   215  				[]byte{},
   216  				1,
   217  				path.EndpointA.ChannelConfig.PortID,
   218  				path.EndpointA.ChannelID,
   219  				path.EndpointB.ChannelConfig.PortID,
   220  				path.EndpointB.ChannelID,
   221  				clienttypes.NewHeight(0, 100),
   222  				0,
   223  			)
   224  
   225  			err = suite.chainA.GetSimApp().ICAControllerKeeper.OnTimeoutPacket(suite.chainA.GetContext(), packet)
   226  
   227  			if tc.expPass {
   228  				suite.Require().NoError(err)
   229  			} else {
   230  				suite.Require().Error(err)
   231  			}
   232  		})
   233  	}
   234  }