github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/apps/27-interchain-accounts/host/keeper/handshake_test.go (about) 1 package keeper_test 2 3 import ( 4 capabilitytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/types" 5 icatypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/27-interchain-accounts/types" 6 channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types" 7 host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host" 8 ibctesting "github.com/fibonacci-chain/fbc/libs/ibc-go/testing" 9 ) 10 11 func (suite *KeeperTestSuite) TestOnChanOpenTry() { 12 var ( 13 channel *channeltypes.Channel 14 path *ibctesting.Path 15 chanCap *capabilitytypes.Capability 16 metadata icatypes.Metadata 17 ) 18 19 testCases := []struct { 20 name string 21 malleate func() 22 expPass bool 23 }{ 24 { 25 "success", 26 func() { 27 path.EndpointB.SetChannel(*channel) 28 }, 29 true, 30 }, 31 { 32 "success - reopening closed active channel", 33 func() { 34 // create a new channel and set it in state 35 ch := channeltypes.NewChannel(channeltypes.CLOSED, channeltypes.ORDERED, channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID), []string{path.EndpointA.ConnectionID}, TestVersion) 36 suite.chainB.GetSimApp().GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, ch) 37 38 // set the active channelID in state 39 suite.chainB.GetSimApp().ICAHostKeeper.SetActiveChannelID(suite.chainB.GetContext(), ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID, path.EndpointB.ChannelID) 40 }, true, 41 }, 42 { 43 "invalid metadata - previous metadata is different", 44 func() { 45 // create a new channel and set it in state 46 ch := channeltypes.NewChannel(channeltypes.CLOSED, channeltypes.ORDERED, channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID), []string{path.EndpointA.ConnectionID}, TestVersion) 47 suite.chainB.GetSimApp().GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, ch) 48 49 // set the active channelID in state 50 suite.chainB.GetSimApp().ICAHostKeeper.SetActiveChannelID(suite.chainB.GetContext(), ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID, path.EndpointB.ChannelID) 51 52 // modify metadata 53 metadata.Version = "ics27-2" 54 55 versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata) 56 suite.Require().NoError(err) 57 58 path.EndpointA.ChannelConfig.Version = string(versionBytes) 59 }, false, 60 }, 61 { 62 "invalid order - UNORDERED", 63 func() { 64 channel.Ordering = channeltypes.UNORDERED 65 }, 66 false, 67 }, 68 { 69 "invalid port ID", 70 func() { 71 path.EndpointB.ChannelConfig.PortID = "invalid-port-id" 72 }, 73 false, 74 }, 75 { 76 "invalid counterparty port ID", 77 func() { 78 channel.Counterparty.PortId = "invalid-port-id" 79 }, 80 false, 81 }, 82 { 83 "connection not found", 84 func() { 85 channel.ConnectionHops = []string{"invalid-connnection-id"} 86 path.EndpointB.SetChannel(*channel) 87 }, 88 false, 89 }, 90 { 91 "invalid metadata bytestring", 92 func() { 93 path.EndpointA.ChannelConfig.Version = "invalid-metadata-bytestring" 94 }, 95 false, 96 }, 97 { 98 "unsupported encoding format", 99 func() { 100 metadata.Encoding = "invalid-encoding-format" 101 102 versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata) 103 suite.Require().NoError(err) 104 105 path.EndpointA.ChannelConfig.Version = string(versionBytes) 106 }, 107 false, 108 }, 109 { 110 "unsupported transaction type", 111 func() { 112 metadata.TxType = "invalid-tx-types" 113 114 versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata) 115 suite.Require().NoError(err) 116 117 path.EndpointA.ChannelConfig.Version = string(versionBytes) 118 }, 119 false, 120 }, 121 { 122 "invalid controller connection ID", 123 func() { 124 metadata.ControllerConnectionId = "invalid-connnection-id" 125 126 versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata) 127 suite.Require().NoError(err) 128 129 path.EndpointA.ChannelConfig.Version = string(versionBytes) 130 }, 131 false, 132 }, 133 { 134 "invalid host connection ID", 135 func() { 136 metadata.HostConnectionId = "invalid-connnection-id" 137 138 versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata) 139 suite.Require().NoError(err) 140 141 path.EndpointA.ChannelConfig.Version = string(versionBytes) 142 }, 143 false, 144 }, 145 { 146 "invalid counterparty version", 147 func() { 148 metadata.Version = "invalid-version" 149 150 versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata) 151 suite.Require().NoError(err) 152 153 path.EndpointA.ChannelConfig.Version = string(versionBytes) 154 }, 155 false, 156 }, 157 { 158 "capability already claimed", 159 func() { 160 path.EndpointB.SetChannel(*channel) 161 err := suite.chainB.GetSimApp().ScopedICAHostKeeper.ClaimCapability(suite.chainB.GetContext(), chanCap, host.ChannelCapabilityPath(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)) 162 suite.Require().NoError(err) 163 }, 164 false, 165 }, 166 { 167 "active channel already set", 168 func() { 169 // create a new channel and set it in state 170 ch := channeltypes.NewChannel(channeltypes.OPEN, channeltypes.ORDERED, channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID), []string{path.EndpointA.ConnectionID}, ibctesting.DefaultChannelVersion) 171 suite.chainB.GetSimApp().GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, ch) 172 173 // set the active channelID in state 174 suite.chainB.GetSimApp().ICAHostKeeper.SetActiveChannelID(suite.chainB.GetContext(), ibctesting.FirstConnectionID, path.EndpointA.ChannelConfig.PortID, path.EndpointB.ChannelID) 175 }, false, 176 }, 177 } 178 179 for _, tc := range testCases { 180 tc := tc 181 182 suite.Run(tc.name, func() { 183 suite.SetupTest() // reset 184 185 path = NewICAPath(suite.chainA, suite.chainB) 186 suite.coordinator.SetupConnections(path) 187 188 err := RegisterInterchainAccount(path.EndpointA, TestOwnerAddress) 189 suite.Require().NoError(err) 190 191 // set the channel id on host 192 channelSequence := path.EndpointB.Chain.GetSimApp().GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(path.EndpointB.Chain.GetContext()) 193 path.EndpointB.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence) 194 195 // default values 196 metadata = icatypes.NewMetadata(icatypes.Version, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID, "", icatypes.EncodingProtobuf, icatypes.TxTypeSDKMultiMsg) 197 versionBytes, err := icatypes.ModuleCdc.MarshalJSON(&metadata) 198 suite.Require().NoError(err) 199 200 counterparty := channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) 201 channel = &channeltypes.Channel{ 202 State: channeltypes.TRYOPEN, 203 Ordering: channeltypes.ORDERED, 204 Counterparty: counterparty, 205 ConnectionHops: []string{path.EndpointB.ConnectionID}, 206 Version: string(versionBytes), 207 } 208 209 chanCap, err = suite.chainB.GetSimApp().GetScopedIBCKeeper().NewCapability(suite.chainB.GetContext(), host.ChannelCapabilityPath(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)) 210 suite.Require().NoError(err) 211 212 tc.malleate() // malleate mutates test data 213 214 version, err := suite.chainB.GetSimApp().ICAHostKeeper.OnChanOpenTry(suite.chainB.GetContext(), channel.Ordering, channel.GetConnectionHops(), 215 path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, chanCap, channel.Counterparty, path.EndpointA.ChannelConfig.Version, 216 ) 217 218 if tc.expPass { 219 suite.Require().NoError(err) 220 } else { 221 suite.Require().Error(err) 222 suite.Require().Equal("", version) 223 } 224 }) 225 } 226 } 227 228 func (suite *KeeperTestSuite) TestOnChanOpenConfirm() { 229 var path *ibctesting.Path 230 231 testCases := []struct { 232 name string 233 malleate func() 234 expPass bool 235 }{ 236 { 237 "success", func() {}, true, 238 }, 239 { 240 "channel not found", 241 func() { 242 path.EndpointB.ChannelID = "invalid-channel-id" 243 path.EndpointB.ChannelConfig.PortID = "invalid-port-id" 244 }, 245 false, 246 }, 247 } 248 249 for _, tc := range testCases { 250 tc := tc 251 252 suite.Run(tc.name, func() { 253 suite.SetupTest() // reset 254 255 path = NewICAPath(suite.chainA, suite.chainB) 256 suite.coordinator.SetupConnections(path) 257 258 err := RegisterInterchainAccount(path.EndpointA, TestOwnerAddress) 259 suite.Require().NoError(err) 260 261 err = path.EndpointB.ChanOpenTry() 262 suite.Require().NoError(err) 263 264 err = path.EndpointA.ChanOpenAck() 265 suite.Require().NoError(err) 266 267 tc.malleate() // malleate mutates test data 268 269 err = suite.chainB.GetSimApp().ICAHostKeeper.OnChanOpenConfirm(suite.chainB.GetContext(), 270 path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) 271 272 if tc.expPass { 273 suite.Require().NoError(err) 274 } else { 275 suite.Require().Error(err) 276 } 277 }) 278 } 279 } 280 281 func (suite *KeeperTestSuite) TestOnChanCloseConfirm() { 282 var path *ibctesting.Path 283 284 testCases := []struct { 285 name string 286 malleate func() 287 expPass bool 288 }{ 289 { 290 "success", func() {}, true, 291 }, 292 } 293 294 for _, tc := range testCases { 295 suite.Run(tc.name, func() { 296 suite.SetupTest() // reset 297 298 path = NewICAPath(suite.chainA, suite.chainB) 299 suite.coordinator.SetupConnections(path) 300 301 err := SetupICAPath(path, TestOwnerAddress) 302 suite.Require().NoError(err) 303 304 tc.malleate() // malleate mutates test data 305 306 err = suite.chainB.GetSimApp().ICAHostKeeper.OnChanCloseConfirm(suite.chainB.GetContext(), 307 path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) 308 309 if tc.expPass { 310 suite.Require().NoError(err) 311 } else { 312 suite.Require().Error(err) 313 } 314 }) 315 } 316 }